-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMGmasterbaiter
executable file
·616 lines (529 loc) · 25.1 KB
/
MMGmasterbaiter
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/usr/bin/perl
#By Thomas J Creedy, thomas@tjcreedy.co.uk
use warnings;
use strict;
use Getopt::Long;
use File::Basename;
use File::Path qw(make_path);
use LWP::Simple;
use List::Util qw(sum);
use Data::Dumper;
use Storable;
my $version = "0.1.2"; #1/11/17
my ($contigfile,$baitfile,$taxonomyfile,$librariesfile,$savedfile);
my $interactive;
my $dupcheck;
my $outpath = ".";
my %parameters = (
'threads' => 8,
'namefield' => 1,
'dupid' => 100,
'duplength' => 100,
'baitid' => 99,
'baitlength' => 100,
'taxmin' => 0.25,
);
my $verbose;
my $debug;
my $help;
my $script = basename($0, ());
########################################################
# USAGE
#
my $usage =<<USAGE;
Description:
This script carries out baiting of anonymous contigs using known barcodes to identify contigs. Most simply, the script requires two files: a fasta of contigs and a fasta of barcodes. In the latter file, the taxonomic name of each barcode is by default taken to be the first part of each fasta header separated by one of "_", "-" and/or ".". Thus the taxonomic name of the sequence with header ">col324_sample4.extract1-5" will be taken to be "col324". The part number can be specified using the -namefield option.
Optionally, two further files may be passed: a taxonomy file and a libraries file. Each of these should be a tab- or comma-separated tabular file, with no header row. The first column of the taxonomy file should contain taxonomic names in the same format as the barcode headers, with subsequent columns containing taxonomic ranks, in descending order. These may be in separate columns or in a single column separated by ";". The first column of the libraries file should also contain taxonomic names in the same format as the barcode headers; in this case, the third column should contain a number indicating the MMG library in which this species is expected to be found.
The first function the script undertakes is to parse the barcodes for duplicates. This is an important quality filter before baiting to better ensure that contigs are matched to the correct taxonomic name. If duplicates are found, the script will BLAST the sequence against a local genbank data and generate a best-guess taxonomy for the match based on retrieved ncbi data for the hits. If a taxonomy file is passed to the script, it can automatically attempt to select the correct taxonomic identity for the duplicate based on taxonomies; if not, or if the -interactive flag is set, the user is presented with each duplicate and the list of options to select from. This process can take some time, so the script has the facility to save its current progress in identifying duplicates and be resumed later. If this option is selected, a file is saved to the current directory and an appropriate message is printed for the user; to resume, the same command should be run again with the addition of the -saved option specifying the path to the resume file. At the end of the first function, the unique barcodes are saved to a file.
The second function the script undertakes is to carry out baiting, which is performed with a simple BLAST of the contigs file against the barcodes file. These results are then parsed and a taxonomic name applied to contigs where possible according to the following logic:
> If only one hit is made, the taxonomic name of this hit is assigned to the contig
> Across multiple hits, the mean of the identities for hits from sequences with the same taxonomic name is computed
> The taxonomic name with the highest mean identity is assigned to the contig
If there are no hits for a contig or multiple taxonomic names tie for highest identity no hit is assigned. Multiple log files are output to a folder specified by the -output option. A table of the automatic taxonomic assignments of contigs is output, along with a fasta file containing the contigs with successful taxonomic assignments and with the assignments as sequence headers. The raw baiting BLAST results are output in order to manually validate, select or improve taxonomic assignments.
Usage:
Basic use:
$script -contigs contigs.fa -baits baits.fa -interactive
With taxonomy file:
$script -contigs contigs.fa -baits baits.fa -taxonomy taxonomy.csv [-libraries libraries.csv -interactive]
Resuming a saved process:
$script -contigs contigs.fa -baits baits.fa -taxonomy taxonomy.csv [-libraries libraries.csv -interactive] -saved baits.savedprogress.log
All options:
$script -contigs contigs.fa -baits baits.fa [-taxonomy taxonomy.csv -libraries libraries.csv] [-interactive] [-saved baits.savedprogress.log] [-outpath dir/] [-verbose] [-namefield] [-threads] [-minid_duplicates n -minlen_duplicates n] [-minid_baiting n -minlen_baiting n] [-prop_taxmatch n]
Arguments:
contigs: Required. Path to contigs fasta file.
baits: Required. Path to baits fasta file.
interactive: Required if no taxonomy file. Flag to use interactive selection mode for duplicate resolution. Automatically sets verbose.
dupcheck: Optional. Flag to carry out duplicate checking
taxonomy: Optional. Path to taxonomy tabular file
libraries: Optional. Path to libraries tabular file
saved: Optional. Path to resume saved duplicate resolution session
output: Path to directory in which to save outputs. Default: current directory.
namefield: Field number of bait headers (separated by "-", "." and/or "_"to use as taxonomic name. Default: 1.
threads: Number of threads to use to use for BLAST. Default: 8.
minid_duplicates: Minimum identity parameter passed to BLAST for blasting bait duplicates. Default: 100.
minlen_duplicates: Minimum length parameter passed to BLAST for blasting bait duplicates. Default: 100.
minid_baiting: Minimum identity parameter passed to BLAST for blasting contigs to baits. Default: 99.
minlen_baiting: Minimum length parameter passed to BLAST for blasting contigs to baits. Default: 100.
prop_taxmatch: Sensitivity parameter for BLAST best-guess taxonomy, 0-1. Default: 0.25. Increase for more conservative guesses.
verbose: Prints useful messages along the way
help: Prints out this helpful message.
USAGE
#
######################################################
GetOptions("contigs=s" => \$contigfile,
"baits=s" => \$baitfile,
"dupcheck" => \$dupcheck,
"namefield=s" => \$parameters{namefield},
"threads=i" => \$parameters{threads},
"minid_duplicates=s" => \$parameters{dupid},
"minid_baiting=s" => \$parameters{baitid},
"minlen_duplicates=s"=> \$parameters{duplength},
"minlen_baiting=s" => \$parameters{baitlength},
"prop_taxmatch=s" => \$parameters{taxmin},
"taxonomy=s" => \$taxonomyfile,
"libraries=s" => \$librariesfile,
"saved=s" => \$savedfile,
"output=s" => \$outpath,
"interactive" => \$interactive,
"verbose" => \$verbose,
"debug" => \$debug,
"help" => \$help) or die "Error: Incorrect options, try \"$script --help\" for more information.\n";
print "$usage\n" and exit if($help);
#
# Error catches
#
die "Error: missing bait fasta\n" unless($baitfile);
die "Error: missing contig fasta\n" unless($contigfile);
die "Error: cannot run automatically without taxonomy information, please either supply taxonomy file or run interactively\n" unless($interactive or $taxonomyfile or !$dupcheck);
#
# Startup messages and input parsing
#
# If interactive or debug set verbose to be true whether or not flagged as input
$verbose = 1 if($interactive or $debug);
print "This is $script version $version.\n" if $verbose;
# Create output directory
make_path($outpath) unless(-e $outpath);
# Read in fastas and report
my %baits = read_fasta($baitfile);
my %contigs = read_fasta($contigfile);
printf "Loaded %d bait sequences and %d contig sequences\n", scalar keys %baits, scalar keys %contigs if $verbose;
# Read in other data and report
my %taxonomy = read_taxonomy($taxonomyfile) if $taxonomyfile;
printf "Loaded %d bait taxonomies\n", scalar keys %taxonomy if($taxonomyfile and $verbose);
my %libraries = read_libraries($librariesfile) if $librariesfile;
printf "Loaded %d bait library allocations\n",scalar keys %libraries if($librariesfile and $verbose);
# Parse names of baits
my %baitnames = map { $_ => 1} (keys %taxonomy, keys %libraries);
my $nameconv = parse_baitnames(\%baits,\%baitnames,\%parameters,$verbose);
print "Matched bait ids with names in supplementary data\n" if($verbose and (%taxonomy or %libraries));
print "Nameconv hash follows:\n" if $debug;
print Dumper $nameconv if $debug;
#
# Check for duplicate barcodes and delete from baits #TODO: synonymise libraries?
#
if($dupcheck){
print "\nChecking for duplicate barcodes\n" if $verbose;
# Blast for any duplicate bait sequences, or retrieve saved output
my $dupsets;
my $savedfilename = "$outpath/$script".timestamp().".savedprogress.log";
if($savedfile){
$dupsets = retrieve($savedfile);
print "Loaded saved progress, resuming.\n" if $verbose;
} else {
($dupsets) = blastfordups($baitfile, $nameconv,\%taxonomy,\%baits,\%parameters,$verbose);
printf "%d sets of duplicates found.\n",scalar keys %$dupsets if $verbose;
store $dupsets, $savedfilename;
print "Saving progress so far, run $script again using \"-saved $savedfilename\" to resume from this point if needed.\n";
}
# Resolve matches
open my $synlog, '>', "$outpath/$script.duplicate.log" or die "Can't write to duplicate log\n";
print $synlog "set,duplicate,sequenceid,length", %taxonomy ? ",name,taxonomyblast,suppliedtaxonomy" : "",",action\n";
foreach my $set (sort keys %$dupsets){
#Check to see if there's a stored decision
my $selection = ${$dupsets}{$set}{selection} or undef;
# Decide on action
my @taxmatchsort = sort { ${$dupsets}{$set}{set}{$b}{taxonomymatch} <=> ${$dupsets}{$set}{set}{$a}{taxonomymatch} } keys %{${$dupsets}{$set}{set}} if(%taxonomy);
if($verbose){
print "\nDuplicate set $set\n";
print "BLAST best-guess taxonomy:",scalar @{${$dupsets}{$set}{taxonomy}} > 3 ? join(";",@{${$dupsets}{$set}{taxonomy}}[-4..-1]) : join(";",@{${$dupsets}{$set}{taxonomy}}),"\n";
print "Sequence options:\n";
foreach my $dup (sort keys %{${$dupsets}{$set}{set}}){
print "\t$dup. ${$dupsets}{$set}{set}{$dup}{id}\n";
print "\t\tName: ${$dupsets}{$set}{set}{$dup}{name}\n" if(%taxonomy or %libraries);
print "\t\tTaxonomy: ",join("; ",@{${$dupsets}{$set}{set}{$dup}{taxonomy}}),"\n" if(%taxonomy);
print "\t\tSequence length: ${$dupsets}{$set}{set}{$dup}{size}bp\n"
}
}
#print Dumper ${$dupsets}{$set};
if($interactive or $selection){
if(!$selection){
print "Please enter number corresponding to sequence to use, or:\n\t'x' to delete entire set of duplicates\n\t'k' to keep entire set of duplicates\n\t'q' to save progress and quit\n";
print "(Based on taxonomy, I think number $taxmatchsort[0] might be the best, but what do I know?)\n" if(%taxonomy and ${$dupsets}{$set}{set}{$taxmatchsort[0]}{taxonomymatch}>${$dupsets}{$set}{set}{$taxmatchsort[1]}{taxonomymatch});
while(!$selection){
print "Enter value:";
$selection = <STDIN>;
chomp $selection;
unless(exists(${$dupsets}{$set}{set}{$selection}) or $selection eq "x" or $selection eq "q" or $selection eq "k"){
print "Input \'$selection\' not recognised, please try again:\n";
undef($selection);
}
}
print "OK, ";
} else {
print "Saved selection found, " if $verbose;
}
if($selection eq 'q'){
store $dupsets, "$outpath/$baitfile.savedprogress.log";
print "quitting here. Progress is saved, when you wish to resume, run $script again using \"-saved $outpath/$baitfile.savedprogress.log\" and you will resume duplicate decisions at this sequence.\n";
exit;
} elsif($selection eq 'x'){
print "deleting entire set of duplicates\n" if $verbose;
} elsif($selection eq 'k'){
print "keeping entire set of duplicates\n" if $verbose;
} else {
print "using sequence ${$dupsets}{$set}{set}{$selection}{id}\n" if $verbose;
}
sleep 1;
} else {
if(${$dupsets}{$set}{set}{$taxmatchsort[0]}{taxonomymatch} > 0 and ${$dupsets}{$set}{set}{$taxmatchsort[0]}{taxonomymatch}>${$dupsets}{$set}{set}{$taxmatchsort[1]}{taxonomymatch}){
print "Sequence $taxmatchsort[0] has clearly best taxonomy match, selecting this sequence\n" if $verbose;
$selection = $taxmatchsort[0];
} else {
print "No sequences have distinct taxonomy match, deleting entire set of duplicates\n" if $verbose;
$selection = 'x';
}
}
${$dupsets}{$set}{selection} = $selection;
# Propagate action
my @deleteseqs;
foreach my $dup (keys %{${$dupsets}{$set}{set}}){
print $synlog "$set,$dup,${$dupsets}{$set}{set}{$dup}{id},${$dupsets}{$set}{set}{$dup}{size}";
print $synlog "${$dupsets}{$set}{set}{$dup}{name},",join(";",@{${$dupsets}{$set}{taxonomy}}),join("; ",@{${$dupsets}{$set}{set}{$dup}{taxonomy}}) if(%taxonomy);
if($selection eq "k"){
print $synlog ",kept\n";
} elsif($dup eq $selection){
print $synlog ",selected\n";
} else {
print $synlog ",deleted\n";
push @deleteseqs,${$dupsets}{$set}{set}{$dup}{id};
}
}
delete @baits{@deleteseqs};
}
close $synlog;
open my $OUTbaits, '>', "$outpath/$script.baits.uniques.fa" or die "Can't write unique baits to fasta\n";
foreach my $id (keys %baits){
print $OUTbaits ">$id\n$baits{$id}\n";
}
close $OUTbaits;
printf "Duplicate resolution complete, %d baits remain\n",scalar keys %baits if $verbose;
}
# Else print that taxonomy file not needed, nor is interactive any different from verbose
#
# Do baiting
#
print "\nRunning BLAST to bait contigs with barcodes\n" if $verbose;
my $baitresult = blasttobait(\%baits,\%contigs,$outpath,\%parameters,$verbose);
printf "%d of %d contigs had at least one match\n", scalar keys %$baitresult, scalar keys %contigs if $verbose;
# Process and write results
print "Processing and writing results\n\n" if $verbose;
open my $baitlog, '>', "$outpath/$script.baiting.final.log";
print $baitlog "contigid,nbaitmatches,baitname,baitid,criterion\n";
open my $OUTctgs, '>', "$outpath/contigs.baited.fa";
foreach my $ctgid (keys %contigs){
print "Processing contig $ctgid:\n" if $debug;
my $nresults = scalar keys %{${$baitresult}{$ctgid}};
print Dumper ${$baitresult}{$ctgid} if $debug;
print "\t$nresults baits match, " if $debug;
my $selectid;
my $criterion;
my $selectname;
if($nresults == 0){
print "no identification could be made\n" if $debug;
$criterion = "nobaitmatches";
} elsif($nresults ==1) {
$selectid = (keys %{${$baitresult}{$ctgid}})[0];
print "identified to $selectid\n" if $debug;
$criterion = "onlymatch";
$selectname = ${$nameconv}{$selectid};
} else {
print "selecting match according to identity...\n" if $debug;
my %matchnames;
foreach my $id (keys %{${$baitresult}{$ctgid}}){
my $name = ${$nameconv}{$id};
$matchnames{$name}{repid} = $id;
$matchnames{$name}{nseqs}++;
$matchnames{$name}{sumid} += ${$baitresult}{$ctgid}{$id}{identity};
$matchnames{$name}{totseqs} = scalar (grep {/$name/} values %{$nameconv});
}
print Dumper \%matchnames if $debug;
foreach my $name (keys %matchnames){
$matchnames{$name}{meanid} = $matchnames{$name}{sumid}/$matchnames{$name}{nseqs};
$matchnames{$name}{propseqs} = $matchnames{$name}{nseqs}/$matchnames{$name}{totseqs};
}
if(keys %matchnames > 1){
my @baitname_sort = sort { $matchnames{$a}{meanid} <=> $matchnames{$b}{meanid} || $matchnames{$a}{propseqs} <=> $matchnames{$b}{propseqs} } keys %matchnames;
if($matchnames{$baitname_sort[0]}{meanid} > $matchnames{$baitname_sort[1]}{meanid} and $matchnames{$baitname_sort[0]}{propseqs} > $matchnames{$baitname_sort[1]}{propseqs}){
print "\ttop name match found: $baitname_sort[0]\n" if $debug;
if($matchnames{$baitname_sort[0]}{nseqs} == 1){
$selectid = $matchnames{$baitname_sort[0]}{repid};
$criterion = "topidentity";
$selectname = $baitname_sort[0];
} else {
$criterion = "topnamebatch";
$selectname = $baitname_sort[0];
}
} else {
print "\ttop name match could not be distinguished, equally high matching sequences found with different names\n" if $debug;
$criterion = "nodistinctmatch - see blast results to make manual decision";
}
} else {
$selectname = (keys %matchnames)[0];
print "identified to $selectname\n" if $debug;
$criterion = "onlynamematch";
}
$selectid = "$matchnames{$selectname}{nseqs} of $matchnames{$selectname}{totseqs} baits of $selectname, mean identity $matchnames{$selectname}{meanid}" if(!$selectid and $selectname);
}
print $baitlog "$ctgid,$nresults,",$selectname ? "$selectname," : "NA,", $selectid ? "$selectid," : "NA," ,"$criterion\n";
print $OUTctgs ">$ctgid";
if($selectname){
print $OUTctgs "|$selectname";
#print $OUTctgs "|",$criterion = "topnamebatch" ? "[multiplebaits]" : $selectid;
#print $OUTctgs "|",%taxonomy ? $taxonomy{$selectname} : "";
}
print $OUTctgs "\n$contigs{$ctgid}\n";
}
close $baitlog;
close $OUTctgs;
print "\n$script completed successfully\n";
exit;
sub blasttobait{
my ($baits,$contigs,$outpath,$parameters,$verbose) = @_;
# Create blast database
print "Creating temporary BLAST database and writing temporary contigs\n" if $verbose;
open my $nwbaitfa, '>', "tempblastdb.fa" or die "Couldn't write to temporary fasta for BLAST database";
foreach my $id (keys %$baits){
print $nwbaitfa ">$id\n${$baits}{$id}\n";
}
close $nwbaitfa;
system("makeblastdb -in tempblastdb.fa -title tempblastdb -dbtype nucl -out tempblastdb > /dev/null 2>&1");
open my $tmpctgfa, '>', "tempblastctg.fa" or die "Couldn't write to temporary contigsfile";
foreach my $id (keys %$contigs){
print $tmpctgfa ">$id\n${$contigs}{$id}\n";
}
close $tmpctgfa;
# Run blast
print "BLASTing\n" if $verbose;
my $blastcmd = "blastn -query tempblastctg.fa -db tempblastdb -num_threads ${$parameters}{threads} -max_target_seqs 10 -outfmt \"6 qseqid sseqid pident length evalue\"";
my $blastdump = qx(bash -c '$blastcmd') or die "Can't BLAST to bait\n";
#Parse blast output
print "Parsing BLAST results\n" if $verbose;
my @blastresults = split /\n/,$blastdump;
printf "Total of %d BLAST results\n",scalar @blastresults if $verbose;
my %results;
open my $blastlog,'>',"$outpath/$script.baiting.blast.log" or die "Can't write blast log to $outpath";
print $blastlog "qseqid\tsseqid\tpident\tlength\tevalue\n";
foreach my $row (@blastresults){
print $blastlog "$row\n";
my ($query,$result, $ident,$length,$evalue) = split /[\s\t]/,$row;
if($ident >= ${$parameters}{baitid} and $length >= ${$parameters}{baitlength}){
$results{$query}{$result} = {
'identity' => $ident,
'length' => $length,
};
}
}
close $blastlog;
system("rm tempblast*");
return \%results;
}
sub blastfordups{
my ($baitfile,$nameconv,$taxonomy,$baits,$parameters,$verbose) = @_;
# Create blast database using bait fasta
print "Creating temporary BLAST database\n" if $verbose;
system("makeblastdb -in $baitfile -title tempblastdb -dbtype nucl -out tempblastdb > /dev/null 2>&1");
# Blast fasta against itself and recieve output
print "BLASTing\n" if $verbose;
my $blastcmd = "blastn -query $baitfile -db tempblastdb -num_threads ${$parameters}{threads} -max_target_seqs 10 -outfmt \"6 qseqid sseqid length pident evalue\"";
my $blastdump = qx(bash -c '$blastcmd') or die "Can't BLAST duplicates\n";
# Parse blast output
print "Parsing BLAST output\n" if $verbose;
my @blastresults = split /\n/,$blastdump;
my %identical;
foreach my $row (@blastresults){
my ($query,$result,$length, $ident,$evalue) = split /[\s\t]/,$row;
if($ident >= ${$parameters}{dupid} and $length >= ${$parameters}{duplength} and $query ne $result){
if(exists($identical{$result})){
$identical{$result}{$query} = 1;
} else {
$identical{$query}{$result} = 1;
}
}
}
#Generate detailed report hash
print "Analysing BLAST output (this may take some time!)\n" if $verbose;
my %dupseqs;
my $nset;
foreach my $setid (keys %identical){ # Go through each set and create a hash containing details of each sequence
$nset++;
printf "Duplicate set $nset of %d, contains:\n",scalar keys %identical if $verbose;
my %set;
my @seqs;
my $ndup = 0;
foreach my $id ($setid,keys %{$identical{$setid}}){
$ndup++;
$set{$ndup} = {
'id' => $id,
'name' => ${$nameconv}{$id},
'sequence' => ${$baits}{$id},
'size' => length(${$baits}{$id})
};
push @seqs,${$baits}{$id};
print "\t$id\n" if $verbose;
$set{$ndup}{taxonomy} = ${$taxonomy}{${$nameconv}{$id}} if %$taxonomy;
}
print "$ndup total sequences\n" if $verbose;
print "BLASTing to NCBI for best-guess taxonomy\n" if $verbose;
my @taxonomylongest = taxonomyblast(\@seqs,$parameters,$verbose); # Blast for best-guess taxonomy
if(%$taxonomy and @taxonomylongest){
print "Calculating taxonomy score\n" if $verbose;
my %taxlonghash = map { $_ => 1 } @taxonomylongest;
print Dumper \%taxlonghash;
foreach my $i (keys %set){
my $score = 0;
my $ntaxa = scalar @{$set{$i}{taxonomy}};
#print "Sequence $i has $ntaxa taxonomic ranks:\n" if $verbose;
if($ntaxa > 0){
my $matches = 0;
foreach my $taxon (@{$set{$i}{taxonomy}}){
#print "\tIs $taxon in blast best-guess?\n";
$matches++ if(exists($taxlonghash{$taxon}));
#print "\tCurrent matches = $matches\n";
}
#print "Total matches = $matches\n";
$score = $matches/$ntaxa;
#print "Score is $score\n";
}
$set{$i}{taxonomymatch} = $score;
}
}
$dupseqs{$nset}{taxonomy} = @taxonomylongest ? [@taxonomylongest] : [" none"];
$dupseqs{$nset}{set} = \%set;
}
system("rm tempblastdb*");
return \%dupseqs;
}
sub taxonomyblast{
my ($seqs,$parameters,$verbose) = @_;
my $blastdump;
foreach my $i (0..scalar(@$seqs)-1){
printf "BLASTing sequence %d\n", $i+1 if $verbose;
my $blastcmd = "blastn -query <(echo -e \">sample\\n@$seqs[$i]\") -db /dh/blastdb/nt -num_threads ${$parameters}{threads} -max_target_seqs 25 -outfmt \"6 sacc bitscore\"";
$blastdump .= qx(bash -c '$blastcmd');
}
my @out;
if($blastdump){
print "Retrieving taxonomy information\n" if $verbose;
my @accessions = ($blastdump =~ /(?:^|\n)(\w+)/g);
my $accessionstring = join ",",@accessions;
print $accessionstring,"\n";
my $nucdump = get("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nuccore&id=$accessionstring&retmode=xml&rettype=fasta");
my $taxidstring = join ",",($nucdump =~ /<TSeq_taxid>(.*)<\/TSeq_taxid/g);
print $taxidstring,"\n";
my $taxdump = get("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id=$taxidstring");
my @lineages = ($taxdump =~ /<Lineage>(.*)<\/Lineage>/g);
my @linaoa;
foreach my $i (0..$#lineages){
$linaoa[$i] = [ split /;\s/,$lineages[$i] ];
}
my $ti = -1;
my @htind = (1..$#linaoa);
while($ti < scalar @{$linaoa[0]}-1){
$ti++;
@htind = grep { $linaoa[$_][$ti] =~ $linaoa[0][$ti] } @htind;
$ti-- and last if($#htind/$#linaoa <= ${$parameters}{taxmin});
}
@out = @{$linaoa[0]}[0..$ti];
} else {
print "No BLAST hits for sequence\n" if $verbose;
}
return @out;
}
sub parse_baitnames {
my ($baits, $names,$parameters,$verbose) = @_;
my %nameconv;
my %unmatchednames;
foreach my $id (keys %$baits){
if(%$names){
my @matches = grep {$id =~ /$_/} keys %$names;
die "Error: cannot match $id to a name" unless(@matches);
die "Error: $id matches multiple names: ",join(", ",@matches) if($#matches>0); ##TODO: if necessary, include error catch if name not in both taxonomy and libraries
$nameconv{$id} = $matches[0];
} else {
my $name = (split /[_.-]/,$id)[${$parameters}{namefield}-1];
if($name){
$nameconv{$id} = $name;
} else {
print "Warning: bait $id is missing namefield ${$parameters}{namefield}. The full header will be used instead.\n";
$nameconv{$id} = $id;
}
}
}
##TODO: Report number of names that do not match to any ID (note, multiple IDs may match to same name if multiple bait loci for same species)
return \%nameconv;
}
sub read_taxonomy {
my ($filein) = @_;
my %taxonomy;
open my $INtx, '<', $filein or die "Couldn't open $filein\n";
while(my $row = <$INtx>){
chomp $row;
my @line = split /[,;\t]/,$row;
$taxonomy{$line[0]} = [ @line[1..$#line] ];
}
close $INtx;
return %taxonomy;
}
sub read_libraries {
my ($filein) = @_;
my %libraries;
open my $INlb, '<', $filein or die "Couldn't open $filein\n";
while(my $row = <$INlb>){
chomp $row;
my @line = split /[,\t]/,$row;
$libraries{$line[0]} = $line[2];
}
close $INlb;
return %libraries;
}
sub read_fasta {
my ($fapathin) = @_;
my %fasta;
my $id;
open my $fa_in, '<', $fapathin or die "Couldn't open $fapathin\n";
while(my $row = <$fa_in>){
chomp $row;
$row =~ s/\x0D//g;
if($row =~ /^>(.+)$/){
if(exists $fasta{$1}){
warn "Duplicate sequence names found in $fapathin: $1 - appending number\n" ;
$id = "$1_1";
while(exists $fasta{$id}){
$id =~ s/_(\d+)$//;
$id = sprintf "$id\_%d",$1+1;
}
} else {
$id = $1;
}
die "Error, spaces found in sequence name \"$id\" in $fapathin\n" if($id =~ /\s/);
} elsif($row =~ /^[ATCGUWSMKRYBDHVNatcguwsmkrybdhvn-]+$/){
$fasta{$id} .= $row;
} else { die "Couldn't read fasta format in $fapathin" };
}
close $fa_in;
return %fasta;
}
sub timestamp{
my @time = localtime;
return sprintf "%02d:%02d:%02d",$time[2],$time[1],$time[0];
}