-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateGenomes.pl
executable file
·447 lines (427 loc) · 13.7 KB
/
updateGenomes.pl
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/usr/bin/env perl
use strict;
use Digest::MD5 qw(md5_hex);
use Getopt::Long;
my @acceptable = qw(
prokaryotes
animals
fungi
plants
protists
);
my $acceptable = join("|",@acceptable);
my @allstatus = qw(
Complete
Chromosome
Scaffold
Contig
);
my $defDry = 'T';
my $defNew = 'T';
my $allstatus = join("|",@allstatus);
my @status = ();
my $group = '';
my $dry = $defDry;
my $new = $defNew;
my $ownname = $0;
$ownname =~ s{\s+/}{};
my $helpMsg
= qq(about:\n)
. qq( This program downloads genomes from NCBI's RefSeq database\n\n)
. qq(usage:\n)
. qq( $ownname -g [group] [options]\n\n)
. qq(options:\n)
. qq( -g group to download [$acceptable],\n)
. qq( required\n)
. qq( -s status to download [$allstatus], can be\n)
. qq( more than one, default: @allstatus\n)
. qq( -d run a dry run indicating no longer available genomes [T|F],\n)
. qq( default: $defDry\n)
. qq( -n only bring new genomes, don't update present ones [T|F],\n)
. qq( default: $defNew\n)
. qq(\n)
;
my $options = GetOptions(
"g=s" => \$group,
"s=s{,}" => \@status,
"d=s" => \$dry,
"n=s" => \$new,
) or die "$helpMsg";
if( !$group ) {
die
" You should indicate a group to download:\n"
. " [$acceptable]\n"
. $helpMsg;
}
if( $group !~ m{^($acceptable)$}i ) {
die
" $group is not an acceptable option:\n"
. " [$acceptable]\n"
. $helpMsg;
}
$group = lc($group);
my $groupMatch = ucfirst($group);
my $localDir = "ncbi";
my $localLists = $localDir . "/genomeInfo";
my $localGnms = $localDir . "/$groupMatch";
my $listFile
= $group eq "prokaryotes" ? "$localLists/$group.txt"
: "$localLists/eukaryotes.txt";
my $assemblyfile
= "$localLists/assembly_summary_refseq.txt";
my $logDir = "ncbi/logs";
if( !@status || grep { m{^all$} } @status ) {
@status = @allstatus;
}
else {
my $countpref = @status;
my @newstatus = ();
if( $countpref > 0 ) {
for my $try ( @status ) {
if( my @matches = grep { m{\b$try\b}i } @allstatus ) {
print $matches[0], " matched\n";
push(@newstatus,$matches[0]);
}
}
}
my $countadded = @newstatus;
if( $countadded == 0 ) {
if( $countpref > 0 ) {
die " @status are not genome status:\n"
. " [$allstatus]\n";
}
}
@status = @newstatus;
}
my $statusMatch = join("|",@status);
print $statusMatch,"<--status to match\n";
$dry = $dry =~ m{^(T|F)$}i ? uc($1): $defDry;
$new = $new =~ m{^(T|F)$}i ? uc($1): $defNew;
if( $dry eq "T" ) {
print "will only enlist md5 download commands for $group\n";
}
else {
print "will download $group\n";
}
if( $new eq 'T' ) {
print "will download new files, won't update already present\n";
}
else {
print "will run a complete update\n";
}
########################################################################
############# reading the list of genomes to ensure we know which ones
############# are in the group we want
########################################################################
print "reading full genome list:\n $listFile\n";
my( $reforigCount,$reforigStatus ) = readGlist("$listFile","$statusMatch");
print "the whole genome files contain:\n";
for my $status ( @status ) {
if( exists $reforigCount->{"$status"} ) {
print " ",join(" ",$reforigCount->{"$status"},$status,"genomes"),"\n";
}
}
print "finding corresponding RefSeq genomes:\n";
my ($heading,$refInfo,$refStatus,$refCount)
= readRefSeq($assemblyfile,$reforigCount,$reforigStatus);
my $total2get = 0;
print "the RefSeq genome database contains:\n";
for my $status ( @status ) {
if( exists $reforigCount->{"$status"} ) {
my $refseq
= exists $refCount->{"$status"} ? $refCount->{"$status"}: 0;
print " ",join(" ",$refseq,"of",
$reforigCount->{"$status"},$status,"genomes"),"\n";
$total2get += $refseq;
}
}
if( $total2get < 1 ) {
die "no genomes to download\n";
}
else {
print "will download $total2get genomes from RefSeq\n";
}
########################################################################
######### make directories for results
########################################################################
unless( -d "$localDir" ){
system "mkdir -p $localGnms" unless( -d "$localGnms");
}
unless( -d "$logDir" ){
mkdir("$logDir") unless( -d "$logDir");
}
########################################################################
######## verify and bring genomes:
########################################################################
print "downloading from RefSeq:\n";
for my $status ( @status ) {
my @ids = sort grep { $refStatus->{"$_"} eq "$status" } keys %{ $refInfo };
my $count = @ids;
if( $count > 0 ) {
print " ",join(" ","downloading",$count,$status,
"genomes from RefSeq"),"\n";
system("mkdir -p $localGnms/$status") unless( -d "$localGnms/$status" );
bringGenomes($status,\@ids,$refInfo);
}
}
#### now let's check if all directories correspond to bichos in use
#### erase otherwise
print " checking for genomes to erase\n";
my $toerase = 0;
my $tokeep = 0;
my $erasefl = "$logDir/eraser-$group.log";
if( -f "$erasefl" ) {
unlink("$erasefl");
}
open( my $BORRADOR,">","$erasefl.tmp" );
for my $status ( @status ) {
open( my $STATUSF,"|-","bzip2 -9 > $localGnms/$status.info.bz2" );
print {$STATUSF} $heading;
my $statusDir = join("/",$localGnms,$status);
opendir( my $CHECKD,"$statusDir" );
my @subdirs = grep{ m{^[A-Z]} } readdir($CHECKD);
closedir($CHECKD);
for my $subdir ( @subdirs ) {
if( exists $refInfo->{"$subdir"} ) {
print {$STATUSF} $refInfo->{"$subdir"},"\n";
$tokeep++;
}
else {
print {$BORRADOR} "rm -rf $statusDir/$subdir\n";
$toerase++;
}
}
close($STATUSF);
}
close($BORRADOR);
if( $toerase > 0 ) {
print "$toerase directories to erase\n";
print "$tokeep directories to keep\n";
rename("$erasefl.tmp","$erasefl");
}
else{
print "nothing to erase\n";
print "$tokeep directories to keep\n";
unlink("$erasefl.tmp");
}
print "\n\tdone with $0\n\n";
sub check_md5s {
my $localPath = $_[0];
if( -d "$localPath" ) {
my $md5file = $localPath . "/md5checksums.txt";
if( -f "$md5file" ) {
my %md5sum = ();
open( my $MD5F,"<","$md5file" );
while(<$MD5F>) {
my($md5sum,$file) = split;
$file =~ s{.*/}{};
$md5sum{"$file"} = $md5sum;
#print join("\t",$file,$md5sum,$md5sum{"$file"}),"\n";
}
close($MD5F);
### learn file names
opendir( my $LOCALD,"$localPath");
my @files2check = grep { m{\.gz$} } readdir($LOCALD);
closedir($LOCALD);
my $count_f = @files2check;
if( $count_f > 0 ) {
my $failed = 0;
for my $file2check ( @files2check ) {
my $full_file = $localPath . "/" . $file2check;
open( my $FL2CH,"<","$full_file" );
binmode($FL2CH);
my $md5sum = Digest::MD5->new->addfile($FL2CH)->hexdigest;
close($FL2CH);
if( $md5sum ne $md5sum{"$file2check"} ) {
$failed++;
#unlink("$full_file");
}
}
if( $failed > 0 ) {
return();
}
else {
return("Good2go");
}
}
else {
return();
}
}
else {
return();
}
}
else {
return();
}
}
sub readGlist {
my($listFile,$statusMatch) = @_;
### indexes for each necessary item
my $iGroup = '';
my $iStatus = '';
my $iAssembly = '';
### to save the data:
my %count = ();
my %status = ();
open( my $GNMS,"<","$listFile" )
or die "I need a $listFile (run updateGenomeInfo.pl first)\n";
GNMLINE:
while(<$GNMS>) {
chomp;
my @items = split(/\t/,$_);
if( m{^#} ) {
for my $index ( 0 .. $#items ) {
if( $items[$index] =~ m{^Group} ) {
$iGroup = $index;
}
if( $items[$index] =~ m{^Status} ) {
$iStatus = $index;
}
if( $items[$index] =~ m{^Assembly} ) {
$iAssembly = $index;
}
}
}
else {
if( $group ne "prokaryotes" ) {
unless( $items[$iGroup] eq "$groupMatch" ) {
next GNMLINE ;
}
}
my $status
= $items[$iStatus] =~ m{$statusMatch}i ? ucfirst(lc($&))
: 'none';
if( $status eq 'none' ) {
next GNMLINE;
}
my $assemblyID = $items[$iAssembly];
$count{"$status"}++;
$status{"$assemblyID"} = $status;
}
}
close($GNMS);
my $cstatus = keys %status;
if( $cstatus > 0 ) {
return(\%count,\%status);
}
else {
return();
}
}
sub readRefSeq {
my($assemblyFile,$refCount,$refStatus) = @_;
### open assembly report to learn path to sequences/genome files
my %genomeInfo = ();
my %count = ();
my %status = ();
my $headInfo = "";
open( my $ASSEM,"<","$assemblyFile" );
ASSEMBLY:
while(<$ASSEM>) {
if( m{^#} ) {
$headInfo .= $_;
next ASSEMBLY;
}
chomp;
my @items = split(/\t/,$_);
### item 10 is version status, we only want "latest"
next ASSEMBLY if( $items[10] ne "latest");
my $rsyncPath = $items[19] =~ m{(https|ftp)://} ? $items[19] : "none";
next ASSEMBLY if( $rsyncPath eq "none");
my $assembly_accession = $items[0];
my $assembly_id = $items[17];
##### by checking the status I'm also checking that this is one of the
##### the genomes I want to download
my $status
= $refStatus->{"$assembly_id"} =~ m{$statusMatch} ? $&
: "none";
next ASSEMBLY if( $status eq "none" );
##### we want to use rsync, rather than ftp or wget
$rsyncPath =~ s{https|ftp}{rsync}g;
my $local_subdir = $assembly_accession;
$genomeInfo{"$assembly_accession"} = $_;
$status{"$assembly_accession"} = $status;
$count{"$status"}++;
}
close($ASSEM);
my $clines = keys %genomeInfo;
if( $clines > 0 ) {
return($headInfo,\%genomeInfo,\%status,\%count);
}
}
sub bringGenomes {
my ($status,$refIDs,$refInfo) = @_;
my $maxTries = 5;
my $rsyncMD5
= qq(rsync -aqL)
. qq( --timeout=15 --contimeout=10 )
. qq( );
my $rsyncCmd
= qq(rsync -aqL)
. qq( --timeout=15 --contimeout=10)
. qq( --exclude='*/')
. qq( --delete --delete-excluded)
. qq( --prune-empty-dirs)
. qq( );
ASSEMBLYID:
for my $gnmID ( @{ $refIDs } ) {
my $info = $refInfo->{"$gnmID"};
my @items = split(/\t/,$info);
my $rsyncPath = $items[19];
my $assembly_accession = $items[0];
my $assembly_id = $items[17];
##### we want to use rsync, rather than ftp or wget
$rsyncPath =~ s{https|ftp}{rsync}g;
my $local_subdir = $assembly_accession;
if( length("$local_subdir") > 1 ) {
my $localPath = join("/",$localGnms,$status,$local_subdir);
if( $new eq 'T' && -d "$localPath" ) {
next ASSEMBLYID;
}
##### added to make sure newed md5 file helps discover if the
##### remote files are the same as the local files
my $returnStatus = 1;
my $tries = 1;
my $md5command
= "$rsyncMD5 $rsyncPath/md5checksums.txt $localPath/ 1>/dev/null";
if( $dry eq 'T' ) {
print $md5command,"\n";
}
else {
while( $returnStatus != 0 && $tries < $maxTries ) {
print " bringing md5checksums $local_subdir $status (try $tries)\n";
if( $tries > 1 ) {
sleep 15;
}
my $output = qx($md5command);
$returnStatus = $?;
$tries++;
}
##### now let's check with new md5 file:
if( check_md5s("$localPath") ) {
## if it works, it means all files are fine
## thus, do nothing here
}
else {
my $returnStatus = 1;
my $tries = 1;
while( $returnStatus != 0 && $tries < $maxTries ) {
print " bringing genome files $local_subdir $status (try $tries)\n";
if( $tries > 1 ) {
sleep 15;
}
my $output
= qx($rsyncCmd $rsyncPath/ $localPath 1>/dev/null);
$returnStatus = $?;
$tries++;
}
}
}
}
else {
print "problem with $assembly_id\n";
}
}
}