1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| #!/usr/local/bin/perl
use strict;
use warnings;
use DBI;
use Bio::DB::GenBank;
use Date::Calc qw(:all);
use Time::localtime;
my ( $start_hour, $start_min, $start_sec ) = Now( [+1] );
my $Depart = ctime();
my $Annee = localtime->year() + 1900;
my $Mois = localtime->mon() + 1;
my $Jour = localtime->mday();
my $Heure = localtime->hour();
my $Min = localtime->min();
my $Sec = localtime->sec();
# Date pour MySQL (date du jour)
my $Date
= sprintf( "%04d", $Annee ) . "-"
. sprintf( "%02d", $Mois ) . "-"
. sprintf( "%02d", $Jour ) . " "
. sprintf( "%02d", $Heure ) . ":"
. sprintf( "%02d", $Min ) . ":"
. sprintf( "%02d", $Sec );
$Date = quotemeta($Date);
my $driver = "mysql";
my $server = "localhost";
my $database = "Pierre_Alain";
my $url = "DBI:$driver:$database:$server";
my $Table = "samson";
my ( $user, $password ) = PASS();
my $DBconnect = DBI->connect( $url, $user, $password ) or die "Failure!\n";
=header
# efface la table si elle existe déjà
my$sql0="drop table IF exists $Table;";
my$sth0 = $DBconnect->prepare($sql0) or print "prepare error\n";
$sth0->execute or die "Could not execute SQL0 statement ... maybe invalid?";
$sth0->finish;
# Créer la table
my$sql = "CREATE TABLE $Table (Accession VARCHAR(30) PRIMARY KEY, Id INT(10), Organism VARCHAR(50), Taxon INT(10), Seq_length SMALLINT(5), Sequence LONGTEXT, Date DATE) ENGINE = InnoDB;";
my$sth = $DBconnect->prepare($sql) or print "prepare error\n";
$sth->execute or die "Could not execute SQL statement ... maybe invalid?";
$sth->finish;
print "\nTABLE $Table cree\n\n\n";
=cut
my @request = ( 'DQ094418', 'DQ094419' );
print @request . " acc à rechercher\n\n";
my $gb = new Bio::DB::GenBank;
my $sql0 = "SELECT Id FROM $Table WHERE accession = ? ";
my $sth0 = $DBconnect->prepare($sql0) or print "prepare error\n";
# <========== c'est mieux :-)
my $sql = <<"SQL";
SELECT fungi2.id, fungi2.organism,
fungi2_accessions.accession, fungi2.seq_length,
fungi2.sequence
FROM fungi2
LEFT JOIN fungi2_accessions
USING ( id )
WHERE accession = ?
SQL
my $sth = $DBconnect->prepare($sql) or print "prepare error\n";
foreach my $acc (@request) {
# si l'acc est déjà dans Samson, on passe au suivant
$sth0->execute($acc)
or die "Could not execute SQL statement ... maybe invalid?";
my $exi = $sth0->rows;
if ( $exi == 0 ) { next; } # <========== c'est mieux :-)
# si l'acc est déjà dans Fungi2, on garde le même Id
# <========== c'est mieux :-)
$sth->execute($acc)
or die "Could not execute SQL statement ... maybe invalid?";
my $num = $sth->rows;
if ( $num > 0 ) {
my $info;
eval { $info = $gb->get_Seq_by_acc($acc) };
if ($@) {
print "ERREUR : pb accession $acc\n";
}
else {
my $taxon = 0;
my @Features = $info->get_SeqFeatures;
my $FeaturesDNA = $Features[0];
foreach my $k ( keys %$FeaturesDNA ) {
if ( $k eq "annotation" ) {
my $SousObjet1 = ( $FeaturesDNA->{$k} );
my $SousObjet2 = ( $SousObjet1->{"_annotation"} );
my $SousObjet4 = ( $SousObjet2->{"db_xref"} );
$taxon = ${$SousObjet4}[0];
$taxon =~ s/\D//g;
last;
} #if
} # foreach my $k (keys %$FeaturesDNA)
# <========== c'est mieux :-)
my $sql2 = <<"SQL2";
INSERT INTO $Table (Id, Organism, Taxon, Accession, Seq_length, Sequence, Date)
VALUES (?, ?, ?, ?, ?, ?, ?)
SQL2
my $sth2 = $DBconnect->prepare($sql2)
or print "prepare error\n";
while ( my @row = $sth->fetchrow_array ) {
$sth2->execute(
$row[0], $row[1], $taxon, $row[2],
$row[3], $row[4], $Date
)
or die
"Could not execute SQL statement ... maybe invalid?";
$sth2->finish;
}
$sth->finish;
}
}
else {
# Recherche dans Genbank
#--------------------------
my $info;
eval { $info = $gb->get_Seq_by_acc($acc) };
if ($@) {
print "ERREUR : pb accession $acc\n";
}
else {
my $seq = $info->seq();
my $seq_length = length($seq);
my $organism = "";
my $taxon = 0;
my @Features = $info->get_SeqFeatures;
my $FeaturesDNA = $Features[0];
foreach my $k ( keys %$FeaturesDNA ) {
if ( $k eq "annotation" ) {
my $SousObjet1 = ( $FeaturesDNA->{$k} );
my $SousObjet2 = ( $SousObjet1->{"_annotation"} );
my $SousObjet3 = ( $SousObjet2->{"organism"} );
$organism = ${$SousObjet3}[0];
my $SousObjet4 = ( $SousObjet2->{"db_xref"} );
$taxon = ${$SousObjet4}[0];
$taxon =~ s/\D//g;
last;
} #if
} # foreach my $k (keys %$FeaturesDNA)
my $sql2 = <<"SQL2";
INSERT INTO $Table (Organism, Taxon, Accession, Seq_length, Sequence, Date)
VALUES ('$organism', '$taxon', '$acc', '$seq_length', '$seq', '$Date')
SQL2
my $sth2 = $DBconnect->prepare($sql2)
or print "prepare error\n";
$sth2->execute
or die "Could not execute SQL statement ... maybe invalid?";
$sth2->finish;
}
}
} # fin de foreach my $acc (@request)
# DECONNEXION A LA BASE DE DONNEES
#-----------------------------------
$DBconnect->disconnect();
print "\n\n\n\n FIN\n\n";
my ( $end_hour, $end_min, $end_sec ) = Now( [+1] );
my ( $D_y, $D_m, $D_d, $Dh, $Dm, $Ds )
= Delta_YMDHMS( 1, 1, 1, $start_hour, $start_min, $start_sec, 1, 1, 1,
$end_hour, $end_min, $end_sec );
print $Dh. " h " . $Dm . " min " . $Ds . " sec\n";
sub PASS {
my $password_file = "P:/Perl/InfoPass.txt";
open( FILE, $password_file ) or die "Can't open file\n";
my @line = <FILE>;
close(FILE);
chomp( $line[0] );
chomp( $line[1] );
return ( $line[0], $line[1] );
} |
Partager