forked from DaehwanKimLab/centrifuge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
centrifuge-compress.pl
executable file
·595 lines (529 loc) · 13 KB
/
centrifuge-compress.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/usr/bin/env perl
# Read and merge the sequence for the chosen level
use strict ;
use warnings ;
use threads ;
use threads::shared ;
use FindBin qw($Bin);
use File::Basename;
use File::Find;
use Getopt::Long;
use Cwd;
use Cwd 'cwd' ;
use Cwd 'abs_path' ;
my $CWD = dirname( abs_path( $0 ) ) ;
my $PWD = abs_path( "./" ) ;
my $usage = "USAGE: perl ".basename($0)." path_to_download_files path_to_taxnonomy [-map header_to_taxid_map -o compressed -noCompress -t 1 -maxG 50000000 -noDustmasker]\n" ;
my $level = "species" ;
my $output = "compressed" ;
my $bssPath = $CWD ; # take path of binary as script directory
my $numOfThreads = 1 ;
my $noCompress = 0 ;
my $noDustmasker = 0 ;
my $verbose = 0;
my $maxGenomeSizeForCompression = 50000000 ;
my $mapFile = "" ;
GetOptions ("level|l=s" => \$level,
"output|o=s" => \$output,
"bss=s" => \$bssPath,
"threads|t=i" => \$numOfThreads,
"maxG=i" => \$maxGenomeSizeForCompression,
"map=s" => \$mapFile,
"verbose|v" => \$verbose,
"noCompress" => \$noCompress,
"noDustmasker" => \$noDustmasker)
or die("Error in command line arguments. \n\n$usage");
die $usage unless @ARGV == 2;
my $path = $ARGV[0] ;
my $taxPath = $ARGV[1] ;
my $i ;
my %gidToFile ;
my %gidUsed ;
my %tidToGid ; # each tid can corresponds several gid
my %gidToTid ;
my %speciesList ; # hold the tid in the species
my %species ; # hold the species tid
my %genus ; # hold the genus tid
my %speciesToGenus ;
my %fileUsed : shared ;
my $fileUsedLock : shared ;
my %taxTree ;
my @speciesListKey ;
my $speciesUsed : shared ;
my $debug: shared ;
my %speciesIdToName : shared ;
my %idToTaxId : shared ;
my %newIdToTaxId : shared ;
my %idToGenomeSize : shared ;
my $idMapLock : shared ;
my $step = 1 ;
# check the depended softwares
if ( `which jellyfish` eq "" )
{
die "Could not find Jellyfish.\n" ;
}
else
{
my $version = `jellyfish --version` ;
chomp $version ;
if ( ( split /\s+/, $version )[1] lt 2 )
{
die "Jellyfish on your system is ", $version, " , centrifuge-compress.pl requires at least 2.0.0\n" ;
}
}
if ( `which nucmer` eq "" )
{
die "Could not find Nucmer.\n"
}
if ( `which dustmasker` eq "" )
{
print STDERR "Could not find dustmasker. And will turn on -noDustmasker option.\n" ;
$noDustmasker = 1 ;
}
#Extract the gid we met in the file
if ( $mapFile ne "" )
{
print STDERR "Step $step: Reading in the provided mapping list of ids to taxionomy ids.\n";
++$step ;
open FP1, $mapFile ;
while ( <FP1> )
{
chomp ;
my @cols = split ;
$idToTaxId{ $cols[0] } = $cols[1] ;
if ( $noCompress == 1 )
{
$newIdToTaxId{ $cols[0] } = $cols[1] ;
}
}
}
print STDERR "Step $step: Collecting all .fna files in $path and getting gids\n";
++$step ;
if ( $noCompress == 1 )
{
`rm tmp_output.fa` ;
}
find ( { wanted=>sub {
return unless -f ; # Must be a file
return unless -s; # Must be non-zero
if ( !( /\.f[nf]?a$/ || /\.ffn$/ || /\.fasta$/ ) )
{
return ;
}
my $fullfile = $File::Find::name; ## file with full path, but the CWD is actually the file's path
my $file = $_; ## file name
open FP2, $file or die "Error opening $file: $!";
my $head = <FP2> ;
close FP2 ;
chomp $head ;
my $headId = substr( ( split /\s+/, $head )[0], 1 ) ;
if ( $noCompress == 1 )
{
# it seems the find will change the working directory
system_call( "cat $PWD/$fullfile >> $PWD/tmp_output.fa" ) ;
if ( defined $idToTaxId{ $headId } )
{
$newIdToTaxId{ $headId } = $idToTaxId{ $headId } ;
}
else
{
$newIdToTaxId{ $headId } = -1 ;
my @cols = split /\|/, $headId ;
my $subHeader = $cols[0]."\|".$cols[1] ;
if ( $headId =~ /gi\|([0-9]+)?\|/ )
{
$newIdToTaxId{ $subHeader } = -1 ;
#print STDERR "$headId $subHeader\n" if $verbose ;
}
elsif ( $headId =~ /taxid\|([0-9]+)?[\|\s]/ )
{
$newIdToTaxId{ $headId } = $1 ;
$newIdToTaxId{ $subHeader } = $1 ;
}
elsif ( scalar( @cols ) > 2 )
{
$newIdToTaxId{ $subHeader } = -1 ;
}
}
}
if ( defined $idToTaxId{ $headId } )
{
my $tid = $idToTaxId{ $headId } ;
my $dummyGid = "centrifuge_gid_".$fullfile."_$tid" ;
$gidUsed{ $dummyGid } = 1 ;
$gidToFile{ $dummyGid } = $fullfile ;
$fileUsed{ $fullfile } = 0 ;
push @{ $tidToGid{ $tid } }, $dummyGid ;
print STDERR "tid=$tid $file\n" if $verbose;
}
elsif ( $head =~ /^>gi\|([0-9]+)?\|/ ) {
my $gid = $1 ;
print STDERR "gid=$gid $file\n" if $verbose;
if ( defined $gidUsed{ $gid } )
{
print "Repeated gid $gid\n" if $verbose ;
$fileUsed{ $fullfile } = 1 ;
}
else
{
$fileUsed{ $fullfile } = 0 ;
$gidToFile{ $gid } = $fullfile ;
}
$gidUsed{ $gid } = 1 ;
} elsif ( $head =~ /taxid\|([0-9]+)?[\|\s]/ ) {
my $tid = $1 ;
my $dummyGid = "centrifuge_gid_".$fullfile."_$1" ;
$gidUsed{ $dummyGid } = 1 ;
$gidToFile{ $dummyGid } = $fullfile ;
$fileUsed{ $fullfile } = 0 ;
push @{ $tidToGid{ $tid } }, $dummyGid ;
print STDERR "tid=$tid $file\n" if $verbose;
} else {
print STDERR "Excluding $fullfile: Wrong header.\n";
}
}, follow=>1 }, $path );
if ( $noCompress == 1 )
{
# Remove the Ns from the file
if ( $noDustmasker == 1 )
{
system_call("perl $bssPath/centrifuge-RemoveN.pl tmp_output.fa | perl $bssPath/centrifuge-RemoveEmptySequence.pl > $output.fa") ;
}
else
{
system_call("perl $bssPath/centrifuge-RemoveN.pl tmp_output.fa > tmp_output_fmt.fa") ;
system_call( "dustmasker -infmt fasta -in tmp_output_fmt.fa -level 20 -outfmt fasta | sed '/^>/! s/[^AGCT]//g' > tmp_output_dustmasker.fa" ) ;
system_call("perl $bssPath/centrifuge-RemoveN.pl tmp_output_dustmasker.fa | perl $bssPath/centrifuge-RemoveEmptySequence.pl > $output.fa") ;
}
}
# Extract the tid that are associated with the gids
print STDERR "Step $step: Extract the taxonomy ids that are associated with the gids\n";
++$step ;
open FP1, "$taxPath/gi_taxid_nucl.dmp" ;
while ( <FP1> )
{
chomp ;
my @cols = split ;
#print $cols[0], "\n" ;
next if ( @ARGV < 2 ) ;
if ( defined( $gidUsed{ $cols[0] } ) )
{
push @{ $tidToGid{ $cols[1] } }, $cols[0] ;
$gidToTid{ $cols[0] } = $cols[1] ;
print STDERR "gid=", $cols[0], " tid=", $cols[1], " ", $gidToFile{ $cols[0] }, "\n" if $verbose;
}
}
close FP1 ;
if ( $noCompress == 1 )
{
open FP1, ">tmp_$output.map" ;
foreach my $key (keys %newIdToTaxId )
{
if ( $newIdToTaxId{$key} != -1 )
{
print FP1 "$key\t", $newIdToTaxId{$key}, "\n" ;
}
elsif ( $key =~ /gi\|([0-9]+)?/ )
{
#if ( defined $gidToTid{ $1 } )
#{
# $newIdToTaxId{ $key } = $gidToTid{ $1 } ;
#}
my $taxId = 1 ;
if (defined $gidToTid{ $1 } )
{
$taxId = $gidToTid{ $1 } ;
}
print FP1 "$key\t", $taxId, "\n" ;
}
else
{
print FP1 "$key\t1\n" ;
}
}
close FP1 ;
system_call( "sort tmp_$output.map | uniq > $output.map" ) ;
exit ;
}
# Organize the tree
print STDERR "Step $step: Organizing the taxonomical tree\n";
++$step ;
open FP1, "$taxPath/nodes.dmp" or die "Couldn't open $taxPath/nodes.dmp: $!";
while ( <FP1> ) {
chomp ;
my @cols = split ;
#next if ( !defined $tidToGid{ $cols[0] } ) ;
my $tid = $cols[0] ;
my $parentTid = $cols[2] ;
my $rank = $cols[4] ;
#print "subspecies: $tid $parentTid\n" ;
#push @{ $species{ $parentTid } }, $tid ;
#$tidToSpecies{ $tid } = $parentTid ;
$taxTree{ $cols[0] } = $cols[2] ;
#print $cols[0], "=>", $cols[2], "\n" ;
if ( $rank eq "species" )
{
$species{ $cols[0] } = 1 ;
}
elsif ( $rank eq "genus" )
{
$genus{ $cols[0] } = 1 ;
}
}
close FP1 ;
# Put the sub-species taxonomy id into the corresponding species.
print STDERR "Step $step: Putting the sub-species taxonomy id into the corresponding species\n";
++$step ;
for $i ( keys %tidToGid )
{
my $p = $i ;
my $found = 0 ;
while ( 1 )
{
last if ( $p <= 1 ) ;
if ( defined $species{ $p } )
{
$found = 1 ;
last ;
}
if ( defined $taxTree{ $p } )
{
$p = $taxTree{ $p } ;
}
else
{
last ;
}
}
if ( $found == 1 )
{
push @{ $speciesList{ $p } }, $i ;
}
}
print STDERR "Step $step: Reading the name of the species\n";
++$step ;
open FP1, "$taxPath/names.dmp" or die "Could not open $taxPath/names.dmp: $!";
while ( <FP1> )
{
next if (!/scientific name/ ) ;
my @cols = split /\t/ ;
if ( defined $species{ $cols[0] } )
{
$speciesIdToName{ $cols[0] } = $cols[2] ;
}
}
close FP1 ;
#exit ;
sub GetGenomeSize
{
open FPfa, $_[0] ;
my $size = 0 ;
while ( <FPfa> )
{
next if ( /^>/ ) ;
$size += length( $_ ) - 1 ;
}
close FPfa ;
return $size ;
}
# Compress one species.
sub solve
{
# Extracts the files
#print "find $pwd -name *.fna > tmp.list\n" ;
my $tid = threads->tid() - 1 ;
#system_call("find $pwd -name *.fna > tmp_$tid.list") ;
#system_call("find $pwd -name *.fa >> tmp.list") ; # Just in case
# Build the header
my $genusId ;
my $speciesId = $_[0] ;
my $speciesName ;
my $i ;
my $file ;
my @subspeciesList = @{ $speciesList{ $speciesId } } ;
$genusId = $taxTree{ $speciesId } ;
while ( 1 )
{
if ( !defined $genusId || $genusId <= 1 )
{
$genusId = 0 ;
last ;
}
if ( defined $genus{ $genusId } )
{
last ;
}
else
{
$genusId = $taxTree{ $genusId } ;
}
}
my $FP1 ;
open FP1, ">tmp_$tid.list" ;
my $genomeSize = 0 ;
my $avgGenomeSize = 0 ;
foreach $i ( @subspeciesList )
{
foreach my $j ( @{$tidToGid{ $i } } )
{
#{
# lock( $debug ) ;
# print "Merge ", $gidToFile{ $j }, "\n" ;
#}
$file = $gidToFile{ $j } ;
{
lock( $fileUsedLock ) ;
$fileUsed{ $file } = 1 ;
}
print FP1 $file, "\n" ;
my $tmp = GetGenomeSize( $file ) ;
if ( $tmp > $genomeSize )
{
$genomeSize = $tmp ;
}
$avgGenomeSize += $tmp ;
}
}
close FP1 ;
#$genomeSize = int( $genomeSize / scalar( @subspeciesList ) ) ;
$avgGenomeSize = int( $avgGenomeSize / scalar( @subspeciesList ) ) ;
#print $file, "\n" ;
#if ( $file =~ /\/(\w*?)uid/ )
#{
# $speciesName = ( split /_/, $1 )[0]."_". ( split /_/, $1 )[1] ;
#}
if ( defined $speciesIdToName{ $speciesId } )
{
$speciesName = $speciesIdToName{ $speciesId } ;
$speciesName =~ s/ /_/g ;
}
else
{
$speciesName = "Unknown_species_name" ;
}
my $id = $speciesId ;#( $speciesId << 32 ) | $genusId ;
my $header = ">cid|$id $speciesName $avgGenomeSize ".scalar( @subspeciesList ) ;
print STDERR "$header\n" ;
{
lock( $idMapLock ) ;
$newIdToTaxId{ "cid|$id" } = $speciesId ;
$idToGenomeSize{ "cid|$id" } = $avgGenomeSize ;
}
#return ;
# Build the sequence
my $seq = "" ;
if ( $noCompress == 0 && ( $maxGenomeSizeForCompression < 0 || $genomeSize <= $maxGenomeSizeForCompression ) ) #$genomeSize < 50000000 )
{
system_call("perl $bssPath/centrifuge-BuildSharedSequence.pl tmp_$tid.list -prefix tmp_${tid}_$id" ) ;
# Merge all the fragmented sequence into one big chunk.
system_call("cat tmp_${tid}_${id}_*.fa > tmp_${tid}_$id.fa");
open FP1, "tmp_${tid}_$id.fa" ;
while ( <FP1> )
{
chomp ;
next if ( /^>/ ) ;
$seq .= $_ ;
}
close FP1 ;
}
else
{
foreach $i ( @subspeciesList )
{
foreach my $j ( @{$tidToGid{ $i } } )
{
$file = $gidToFile{ $j } ;
open FP1, $file ;
while ( <FP1> )
{
#chomp ;
next if ( /^>/ ) ;
$seq .= $_ ;
}
close FP1 ;
}
}
}
open fpOut, ">>${output}_${tid}" ;
print fpOut "$header\n$seq\n" ;
close fpOut ;
unlink glob("tmp_${tid}_*");
}
sub system_call {
print STDERR "SYSTEM CALL: ".join(" ",@_);
system(@_) == 0
or die "system @_ failed: $?";
print STDERR " finished\n";
}
sub threadWrapper
{
my $tid = threads->tid() - 1 ;
unlink("${output}_${tid}");
while ( 1 )
{
my $u ;
{
lock $speciesUsed ;
$u = $speciesUsed ;
++$speciesUsed ;
}
last if ( $u >= scalar( @speciesListKey ) ) ;
solve( $speciesListKey[ $u ] ) ;
}
}
print STDERR "Step $step: Merging sub-species\n";
++$step ;
my @threads ;
@speciesListKey = keys %speciesList ;
$speciesUsed = 0 ;
for ( $i = 0 ; $i < $numOfThreads ; ++$i )
{
push @threads, $i ;
}
foreach (@threads)
{
$_ = threads->create( \&threadWrapper ) ;
}
foreach (@threads)
{
$_->join() ;
}
# merge the files generated from each threads
system_call("cat ${output}_* > tmp_output.fa");
unlink glob("${output}_*");
#print unused files
foreach $i ( keys %fileUsed )
{
if ( $fileUsed{ $i } == 0 )
{
#print $i, "\n" ;
#`cat $i >> tmp_output.fa` ;
print "Unused file: $i\n" ;
}
}
# Remove the Ns from the file
if ( $noDustmasker == 1 )
{
system_call("perl $bssPath/centrifuge-RemoveN.pl tmp_output.fa | perl $bssPath/centrifuge-RemoveEmptySequence.pl > $output.fa") ;
}
else
{
system_call("perl $bssPath/centrifuge-RemoveN.pl tmp_output.fa > tmp_output_fmt.fa") ;
system_call( "dustmasker -infmt fasta -in tmp_output_fmt.fa -level 20 -outfmt fasta | sed '/^>/! s/[^AGCT]//g' > tmp_output_dustmasker.fa" ) ;
system_call("perl $bssPath/centrifuge-RemoveN.pl tmp_output_dustmasker.fa | perl $bssPath/centrifuge-RemoveEmptySequence.pl > $output.fa") ;
}
# Output the mapping of the ids to species
open FP1, ">$output.map" ;
foreach my $key (keys %newIdToTaxId )
{
print FP1 "$key\t", $newIdToTaxId{ $key }, "\n" ;
}
close FP1 ;
# Output the genome sizem map
open FP1, ">$output.size" ;
foreach my $key ( keys %newIdToTaxId )
{
print FP1 $newIdToTaxId{ $key }, "\t", $idToGenomeSize{ $key }, "\n" ;
}
close FP1 ;
unlink glob("tmp_*") ;