-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reann.pm
427 lines (383 loc) · 11.2 KB
/
Reann.pm
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
package Reann;
use 5.010;
use strict;
use warnings;
use Getopt::Long qw(GetOptionsFromString);
use File::Copy;
use SeqFile;
use Blast;
use Rapsearch;
use Diamond;
use LocalTaxonomy;
use Taxonomy;
use SeqUtils;
use Annotator::Report;
my $NUMTHREADS = 4;
my $OUTFOLDER = 'annotator';
my $BLASTOUTFMT = 6;
my $RESTARTFILE = 'restart.txt';
my $CHUNK = 0;
my $SEQFILE = 'tag-fasta.fa';
my $CONFIGFILE = 'tag.conf';
my $OUTPUTFILE = 'report.txt';
my $PREFIX = 'ann';
my $SEQFORMAT = 'fasta';
my $EVALUE = 10;
my $DELIM = "\t";
my $RUNTAXONOMY = 0;
my $REPORTALL = 0;
my $REMOTETAX = 0;
sub new{
my $class = shift;
my $self = shift;
my @programs;
$self->{'num_threads'} //= $NUMTHREADS;
$self->{'outfmt'} //= $BLASTOUTFMT;
$self->{'restart'} //= $RESTARTFILE;
$self->{'chunk'} //= $CHUNK;
$self->{'output'} //= $OUTPUTFILE;
$self->{'prefix'} //= $PREFIX;
$self->{'file'} //= $SEQFILE;
$self->{'config'} //= $CONFIGFILE;
$self->{'folder'} //= $OUTFOLDER;
$self->{'format'} //= $SEQFORMAT;
$self->{'evalue'} //= $EVALUE;
$self->{'delim'} //= $DELIM;
$self->{'tax'} //= $RUNTAXONOMY;
$self->{'remotetax'} //= $REMOTETAX;
if (! -e $self->{config}) {
print "Please supply an appropriate config file (config)\n";
exit;
}
umask 0022;
mkdir $self->{'folder'} if ! -d $self->{'folder'};
my $f = $self->{'file'};
$f =~ s/^.+\\|^.+\///;
copy($self->{'config'},$self->{'folder'});
copy($self->{'file'},$self->{'folder'});
$self->{'file'} = $f;
$self->{'config'} =~ s/^.+\\|^.+\///;
chdir($self->{'folder'});
my %seqFile = %{$self};
$self->{'seqs'} = new SeqFile(\%seqFile);
my $file = shift;
open IN, $self->{'config'};
print $self->{'config'},$/;
while(<IN>){
next if $_ =~ /^\s+$|^\#/;
chomp;
my @f = split;
if ($f[3] =~ /^~/) {
print "Tilde (~) are not allowed in database file paths. Use a full path instead.\n";
exit;
}
if ($f[1] =~ /blast/) {
push(@programs, Blast->new($_,$self));
}
elsif ($f[1] =~ /rapsearch/) {
push(@programs, Rapsearch->new($_,$self));
}
elsif ($f[1] =~ /diamond/) {
push(@programs, Diamond->new($_,$self));
}
else {
die "Program $f[1] not recognized\n";
}
}
close IN;
$self->{'programs'} = \@programs;
open (my $version, ">", "version.txt") or die "Can't open version.txt: $!\n";
foreach (@{$self->{programs}}) {
print $version $_->{version},"\n";
}
close ($version);
$self->{'out'} = ();
bless $self,$class;
return $self;
}
sub run{
my $self = shift;
my ($i,$p) = $self->Restart; # $i is chunk number, $p is Program number
my $r = ($i || $p);
my @files = $self->{'seqs'}->GetFiles;
for(;$i < scalar @files; $i++){
print $files[$i],$/;
for(; $p < scalar @{$self->{'programs'}}; $p++){
my $f = $files[$i];
my ($out,$filter) = $self->{'programs'}[$p]->run($f,$p,$r);
$r = 0;
$self->WriteRestart($i,$p);
$self->{'out'}[$i][$p] = $out;
$self->{'seqs'}->FilterSeqs($i,$filter);
}
$p = 0;
}
$self->WriteRestart($i,$p);
foreach my $f(@files){
RM($f);
}
RM($self->{'config'});
}
sub Restart{
my $self = shift;
my ($x,$y) = (0,0);
if(! -s $self->{'restart'}){
open OUT, ">".$self->{'restart'};
print OUT "0,0",$/;
close OUT;
}
else{
open IN, $self->{'restart'};
while(<IN>){
chomp $_;
my @mm = split ',', $_;
($x,$y) = @mm;
$self->GetCompleted($x,$y);
}
close IN;
}
return ($x,$y);
}
sub GetCompleted{
my $self = shift;
my $fasta = shift;
my $program = shift;
my @files = $self->{'seqs'}->GetFiles;
for(my $x = 0; $x <= $fasta; $x++){
for(my $y = 0; $y < @{$self->{'programs'}}; $y++){
last if $x == $fasta && $y == $program;
$self->{'out'}[$x][$y] = $self->{'programs'}[$y]->GetOutName($files[$x],$y);
}
}
}
sub WriteRestart{
my $self = shift;
my $t = $self->{'restart'}.".temp";
open OUT, ">$t";
print OUT join(",",@_),$/;
close OUT;
$self->Overwrite($self->{'restart'}.".temp",$self->{'restart'});
}
sub RM{
my $cmd;
my $f = shift;
if($^O eq 'MSWin32'){
$cmd = "del ".$f;
}
else{
$cmd = "rm ".$f;
}
`$cmd`;
}
sub Overwrite{
my $self = shift;
my $cmd;
if($^O eq 'MSWin32'){
$cmd = join(' ','move',@_);
}
else{
$cmd = join(' ','mv',@_);
}
`$cmd`;
}
sub PrintParams{
my $self = shift;
$self->{'seqs'}->PrintParams;
}
sub Report{ # edit here to add get all
my $self = shift;
my @h = qw(seqID seq seqLength pid coverage e accession algorithm db qstart qend sstart ssend);
print "Report",$/;
my $d = $self->{'delim'};
my %blast;
my $out = $self->{'prefix'}.".".$self->{'output'};
my @id_list;
for(my $x = 0; $x < scalar @{$self->{'out'}}; $x++){
for(my $y = 0; $y < scalar @{$self->{'out'}[$x]}; $y++){
push(@id_list,$self->{'programs'}[$y]->Parse($self->{'out'}[$x][$y],\%blast));
}
}
foreach my $h (@id_list){
foreach my $k (keys %{$h}){
$self->{'hit_list'}{$k} = 1;
}
}
open OUT, ">$out";
print OUT join($d,@h),$/;
my $seqI = new Bio::SeqIO(-file => $self->{'file'},-format => $self->{'format'});
if(!$self->{'report_all'}){
while(my $seq = $seqI->next_seq){
my $i = $seq->id;
my $reportline = "$d" x 9; # HARD CODING!!! Be careful here
$reportline = join($d,$blast{$i}{'pid'},$blast{$i}{'qc'},
$blast{$i}{'evalue'},$blast{$i}{'accession'},$blast{$i}{'algorithm'},
$blast{$i}{'db'},@{$blast{$i}{'pos'}}) if $blast{$i};
print OUT join($d,$i,$seq->seq,$seq->length,$reportline),$/;
}
}
else{
#finish all report here
while(my $seq = $seqI->next_seq){
my $i = $seq->id;
my $reportline = "$d" x 9;
if(defined($blast{$i})){
foreach my $b (@{$blast{$i}}){
$reportline = join($d,$b->{'pid'},$b->{'qc'},
$b->{'evalue'},$b->{'accession'},$b->{'algorithm'},
$b->{'db'},@{$b->{'pos'}});
print OUT join($d,$i,'',$seq->length,$reportline),$/;
}
}
else{
print OUT join($d,$i,'',$seq->length,$reportline),$/;
}
}
}
close OUT;
$seqI = '';
RM($self->{'file'});
}
sub Taxonomy {
my ($self, %args) = @_;
print "Taxonomy",$/;
my $report = $self->{'prefix'}.".".$self->{'output'};
open IN, "<", $report;
my $header = <IN>;
chomp $header;
my @hf = split (/\t/,$header);
my $nhf = scalar @hf;
$self->{taxout} = $self->{'prefix'}.".wTax.".$self->{'output'};
open OUT, ">", $self->{taxout};
print OUT join("\t", @hf[0..6],
"desc","type","family","species","genome",
@hf[7..$nhf-1], "nsf"),"\n";
#
# get fasta seqs and descriptions from BLAST databases
my %acc;
while (<IN>) {
my ($acc, $db) = (split (/\t/, $_, -1))[6,8];
$db =~ s/\.dmnd//; # remove Diamond suffix (i.e. nr.dmnd -> nr)
$acc{$db}{$acc}{count}++ unless ($acc eq "");
}
foreach my $db (keys %acc) {
print "\tGetting fasta seqs for $db\n";
(my $db_basename = $db) =~ s|^/.*/||; # strip any absolute path to get basename
my $gis_outfile = "$db_basename.gis.txt";
$self->{dbaccfile}{$db} = $gis_outfile; # save the db accessions filename for getting lineage below
open (TMPOUT, ">", $gis_outfile) or die "Can't open $gis_outfile for writing: $!\n";
foreach (keys %{$acc{$db}}) {
print TMPOUT $_, "\n";
}
close TMPOUT;
my $fasta_outfile = $gis_outfile . ".fa";
$self->{dbfastafile}{$db} = $fasta_outfile; # save fasta filename for each database; these files will be used in add_entropy()
`blastdbcmd -db $db -entry_batch $gis_outfile > $fasta_outfile`;
my $seqio = Bio::SeqIO->new(-file => $fasta_outfile, -format => 'fasta');
while (my $seqobj = $seqio->next_seq) {
(my $primary_id = $seqobj->primary_id) =~ s/^lcl\|//; # some versions of blastdbcmd prepend accession number with 'lcl|'
$acc{$db}{$primary_id}{desc} = $seqobj->desc;
}
}
#
# get lineage information
my $lt;
if ($self->{'tax'}) {
print "\tStarting LocalTaxonomy - ";
if ($self->{'remotetax'}) {
print "getting taxonomy solely from NCBI\n";
$lt = LocalTaxonomy->new(remotetax => 1);
}
else {
print "getting taxonomy locally\n";
$lt = LocalTaxonomy->new();
# for each db accession file, use taxonomizr to get lineage
foreach my $db ( keys %{ $self->{dbaccfile} } ) {
my $dbaccfile = $self->{dbaccfile}{$db};
#print "\tGetting lineage for db accession file: ", $dbaccfile, $/;
my $lineagefile = $lt->GetLineage_fromTaxaSQL($dbaccfile);
open my $infile, "<", $lineagefile;
<$infile>; #header
while(<$infile>){
chomp;
my ($acc, undef, $lineage) = split /\t/, $_;
$acc{$db}{$acc}{lineage} = $lineage;
}
close($infile);
#unlink($lineagefile);
}
}
}
else {
print "\tSkipping LocalTaxonomy\n";
}
foreach my $db ( keys %{ $self->{dbaccfile} } ) {
my $dbaccfile = $self->{dbaccfile}{$db};
unlink ($dbaccfile);
}
seek IN, 0, 0; # seek to beginning of report file
<IN>;
while (<IN>) {
chomp;
my @rf = split (/\t/, $_, -1); # row fields (rf); -1 for keeping trailing empty fields
my $nrf = scalar @rf;
my ($desc, $type, $family, $species, $genome) = ('') x 5;
my $accession = $rf[6];
unless ($accession eq "") {
($type, $family, $species, $genome) = ('NULL') x 4;
my $gi = (split (/\|/, $accession))[1];
if (!$gi) {
$gi = $accession; # for when the subject id in blast output is not a fullgi but rather just an ACC.VER value (i.e. APS85757.2)
}
my $algo = $rf[7];
my $db = $rf[8];
$db =~ s/\.dmnd//; # remove Diamond suffix (i.e. nr.dmnd -> nr)
$desc = $acc{$db}{$accession}{desc}; # get description from %acc hash
my $lineage = "";
if ($self->{'tax'}) {
if($self->{'remotetax'}) {
$lineage = $lt->GetLineage($algo, $gi, $self->{'remotetax'});
}
else {
$lineage = $acc{$db}{$accession}{lineage};
}
if ($lineage ne "") {
($type, $family, $species) = lineage2tfs($lineage);
$genome = get_genome_type($family); # get genome type for the family (index 1 of array)
}
}
}
my $is_nsf = has_nsf($rf[1]);
# output
print OUT join ("\t", @rf[0..6],
$desc,$type,$family,$species,$genome,
@rf[7..$nrf-1], $is_nsf),"\n";
}
close OUT;
unlink ($report);
}
sub add_entropy {
print "Entropy\n";
my ($self, %args) = @_;
my $refseqs = $self->{dbfastafile};
my $entropyReport = $self->{prefix} . ".wTax.BE." . $self->{output};
if (-e $self->{taxout}) {
my $ar = Annotator::Report->new(report => $self->{taxout},
refseqs => $refseqs,);
my $tmp = $ar->run_entropy;
move ($tmp, $entropyReport);
#move ($tmp, $self->{taxout});
}
else {
print "Add_entropy will not run since Reann taxReport does not exist.\n";
return
}
# delete db fasta files
foreach my $db ( keys %{ $self->{dbfastafile} } ) {
#print "\tDeleting db fastafile: ", $self->{dbfastafile}->{$db}, $/;
unlink ($self->{dbfastafile}->{$db});
}
}
1;
=cut
Functions as Program object handlers and SeqFile object handler. Creates report file.
=cut