-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrna2taxon.pl
executable file
·336 lines (275 loc) · 10.5 KB
/
rna2taxon.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
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use FindBin;
use Time::Piece;
use Benchmark;
use Scalar::Util qw(openhandle);
my $EXE = $FindBin::RealScript;
my $VERSION = "1.1.1";
my $DESC = "ribosomal RNA classification: classify fasta format ssu and lsu rRNA using SILVA taxonomy";
my $AUTHOR = 'Xiaoli Dong <[email protected]>';
my $OPSYS = $^O;
my $DBDIR = "$FindBin::RealBin/../db/blast";
my $ssu_db = "silva_SSURef_Nr99.fasta";
my $lsu_db = "silva_LSURef.fasta";
my (@Options,$dbtype, $evalue, $cpus, $identities, $coverage,$length);
setOptions();
my $db = $dbtype eq "ssu" ? $ssu_db : $lsu_db;
my %level_cutoff = ();
my @arr = split(/,/, $identities);
if(@arr < 7){
err("the number of the identity cutoff is less than 6, please redefine it" );
}
$level_cutoff{domain} = $arr[0];
$level_cutoff{phylum} = $arr[1];
$level_cutoff{class} = $arr[2];
$level_cutoff{order} = $arr[3];
$level_cutoff{family} = $arr[4];
$level_cutoff{genus} = $arr[5];
$level_cutoff{species} = $arr[6];
my $fasta = shift @ARGV;
$fasta && -r $fasta or err("Usage: $EXE <RNA fasta sequences file>");
my $t0 = Benchmark->new;
my $blastn_output = "$fasta.blastn.outfmt7.txt";
my $cmd = "blastn -query $fasta -db $DBDIR/$db -dust no -num_threads $cpus -evalue $evalue -out $blastn_output -outfmt \"7 qseqid qlen slen qstart qend sstart send length qcovhsp pident evalue bitscore stitle\" -max_target_seqs 5";
msg("$cmd\n");
if(! -e "$blastn_output"){
runcmd($cmd);
}
open(BLAST_OUT,$blastn_output ) or die "Could not open $blastn_output to read, $!\n";
#$/="\n# BLASTN 2.8.1+\n";
$/="\n# BLASTN";
my $count = 0;
my %taxon_ass = ();
# Fields: query id 0, query length 1, subject length 2, q. start 3, q. end 4, s. start 5, s. end 6, alignment length 7, % query coverage per hsp 8, % identity 9, evalue 10, bit score 11, subject title 12
while(<BLAST_OUT>){
# Query: NODE_278_length_54558_cov_93.7421_1 type=bac_16SrRNA len=180 start=1 end=758 strand=-
my($qid, $rnatype) = $_ =~ /# Query:\s+?(\S+?)\s+type=(\S+)/;
my @lines = split(/\n/, $_);
my @hits = ();
my $seq2taxon = "";
foreach my $line (@lines){
next if $line =~ /^#|^\s+\d\.\d\.\d\+$/;
push(@hits, $line);
}
if (@hits == 0){
$seq2taxon = "$qid\t$rnatype\tunknown\t[RNA_target=db:$db|nohit]";
$taxon_ass{$qid} = $seq2taxon;
next;
}
my $best_hit = $hits[0];
my @best = split(/\t/, $best_hit);
my $qlen = $best[1];
my $cov = $best[8];
my $iden = $best[9];
if( ($rnatype !~ /5S|5_8S/) && ($qlen < $length || $cov < $coverage || $iden < $level_cutoff{phylum})){
$seq2taxon = "$qid\t$rnatype\tunknown\t[RNA_target=db:$db|novalid]";
$taxon_ass{$qid} = $seq2taxon;
}
else{
$seq2taxon = getTaxon_by_bestHit($rnatype, $best_hit);
$taxon_ass{$qid} = $seq2taxon;
}
}
$/="\n";
foreach (keys %taxon_ass){
print $taxon_ass{$_}, "\n";
}
my $t_end = Benchmark->new;
my $td = timediff($t_end, $t0);
msg("classifyRNA takes :",timestr($td)," to run\n");
sub getTaxon_by_bestHit{
my ($rnatype,$best_hit) = @_;
# Fields: query id, query length, subject length, q. start, q. end, s. start, s. end, alignment length, % query coverage per hsp, % identity, evalue, bit score, subject title
my @fields = split(/\t/, $best_hit);
my $qid = $fields[0];
my $qlen = $fields[1];
my $iden = $fields[9];
my $stitle = $fields[12];
my ($ssid, $taxon) = $stitle =~ /(\S.*?)\s+\[(\S.*?)\]$/;
my @taxon_terms = split(/;/, $taxon);
my $target = "rRNA_target=db:$db|$ssid $fields[3] $fields[4] evalue:$fields[10] qcov:$fields[8] identity:$iden";
#the percent identity of a match must exceed the given value of percent identity to be assigned at the given rank: Species 99%, Genus 97%, Family 95%, Order 90%, Class 85%, Phylum 80%.
#species
my $taxon_assign = "$taxon";
if($iden >= $level_cutoff{species} && $#taxon_terms > 6){
$taxon_assign = join(";", @taxon_terms[0 .. 6]);
}
#genus
elsif($iden >= $level_cutoff{genus} && $#taxon_terms > 5 ){
$taxon_assign = join(";", @taxon_terms[0 .. 5]);
}
#family
elsif($iden >= $level_cutoff{family} && $#taxon_terms > 4){
$taxon_assign = join(";", @taxon_terms[0 .. 4]);
}
#order
elsif($iden >= $level_cutoff{order} && $#taxon_terms > 3){
$taxon_assign = join(";", @taxon_terms[0 .. 3]);
}
#class
elsif($iden >= $level_cutoff{class} && $#taxon_terms > 2){
$taxon_assign = join(";", @taxon_terms[0 .. 2]);
}
#phylum
elsif($iden >= $level_cutoff{phylum} && $#taxon_terms > 1){
$taxon_assign = join(";", @taxon_terms[0 .. 1]);
}
#domain
elsif($iden >= $level_cutoff{domain} && $#taxon_terms >= 0){
$taxon_assign = $taxon_terms[0];
}
return "$qid\t$rnatype\t$taxon_assign\t[$target]";
}
#the percent identity of a match must exceed the given value of percent identity to be assigned at the given rank: Species 99%, Genus 97%, Family 95%, Order 90%, Class 85%, Phylum 80%.
sub getTaxon_by_lca{
my ($hits,$queryid, $qlen) = @_;
my @taxons = ();
my $index = 0;
foreach (@$hits){
# Fields: query id, query length, subject length, q. start, q. end, s. start, s. end, alignment length, % query coverage per hsp, % identity, evalue, bit score, subject title
my @fields = split(/\t/, $_);
my $cov = $fields[8];
my $iden = $fields[9];
next if $cov < $coverage;
next if $iden < $level_cutoff{phylum};
my ($taxon) = $fields[12] =~ /\[(\S.*?)\]$/;
my @terms = split(/;/, $taxon);
next if @terms == 0;
@{$taxons[$index++]} = @terms;
}
my $com = lca(@taxons);
return "$queryid\t$qlen\t" . join(";", @$com) . "\n";
}
sub lca {
my @taxons = @_;
my $size = @taxons;
my $test = $taxons[0];
#print STDERR "lca size=$size\n";
#print STDERR "first*********=@$test\n";
for (my $index = 0; $index <$size; $index++){
#$test = $taxons[$index];
#print STDERR "index=$index*********=@$test\n";
}
if (@taxons == 0) {return 0;} # Empty list? Not assigned=0
@taxons=grep {defined} @taxons; # screen out undefined values
if (@taxons==0) {die("undefined values snuck into the taxid list; help?");} # how did undefined values get on the list?
#print " taxid3: ",(map {my @z=@$_;sprintf("%7s ",$z[$#z])} @taxons),"\n";
if (@taxons==0) {return -1;} # No hits
#print " taxid4: ",(map {my @z=@$_;sprintf("%7s ",$z[$#z])} @taxons),"\n";
# Find the lca
my $n;
for ($n=0;$n<100;$n++) {
my @col;
my $size = @taxons;
for (my $m=0,@col=();$m<@taxons;$m++) { push @col,$taxons[$m][$n]; }
if (!defined($col[0]) or !unanimous(@col)) {last;}
}
#if ($n<=0) {print STDERR "n=$n\n"; die("Impossible Error: Lca algorithm couldn't find any nodes in common!? Data:\n".Dumper(\@_,\@taxons)." ");}
my @slice = ();
for(my $i = 0; $i < $n; $i++){
push (@slice, $taxons[0][$i]);
}
#print STDERR "n=$n, common nodes=", $taxons[0][$n-1], "\n";
#print STDERR "n*******=$n, common nodes=@slice\n";
return \@slice;
#return $taxons[0][$n-1]; #Return the taxid of the last common node
}
# Return 1 if the array is unanimous, 0 if not
sub unanimous {
if (!defined $_[0]) { # Unanimously 'undef'ined
if (grep {defined} @_) {return 0;}
return 1;
}
if (grep {!defined or $_ ne $_[0]} @_) {return 0;}
return 1;
}
#----------------------------------------------------------------------
sub delfile {
for my $file (@_) {
msg("Deleting unwanted file:", $file);
unlink $file or warn "Could not unlink $file: $!\n";;
}
}
#----------------------------------------------------------------------
sub msg {
my $t = localtime;
my $line = "[".$t->hms."] @_\n";
print LOG $line if openhandle(\*LOG);
print STDERR $line;
}
sub runcmd {
msg("Running:", @_);
system(@_)==0 or err("Could not run command:", @_);
}
#species 99%, Genus 97%,Family 95%, Order 90%, Class 85%, Phylum 80%
sub setOptions {
use Getopt::Long;
@Options = (
'Options:',
{OPT=>"help", VAR=>\&usage, DESC=>"This help"},
{OPT=>"version", VAR=>\&version, DESC=>"Print version and exit"},
{OPT=>"cpus=i", VAR=>\$cpus, DEFAULT=>8, DESC=>"Number of threads/cores/CPUs to use"},
{OPT=>"dbtype=s",VAR=>\$dbtype, DEFAULT=>'ssu', DESC=>"input sequence type:ssu|lsu, ssu incldues 16s/18s, lsu includes 23s/28s rRNA"},
'Cutoffs:',
{OPT=>"evalue=f",VAR=>\$evalue, DEFAULT=>1E-5, DESC=>"evalue cut-off"},
{OPT=>"length=f",VAR=>\$length, DEFAULT=>200, DESC=>"rRNA gene length cutoff for taxon assignment"},
{OPT=>"coverage=f",VAR=>\$coverage, DEFAULT=>80, DESC=>"The min query coverage: percent of the query sequence taht ovelaps the subject sequence"},
{OPT=>"identities=s",VAR=>\$identities, DEFAULT=>'70,80,85,90,95,97,99', DESC=>"the percent identity of a match must exceed the given value of percent identity to be assigned at the given rank:domain 70, phylum 80, class 85, order 90, family 95, genus 97, species 99"},
#{OPT=>"method=s",VAR=>\$method, DEFAULT=>'besthit', DESC=>"taxon assignment methods:besthit|lca|dominant"},
#{OPT=>"toppercent=s",VAR=>\$toppercent, DEFAULT=>'25', DESC=>"the hits will be used for taxon assignment if the hits are within the distant of this value * best bitscore"},
);
(!@ARGV) && (usage());
&GetOptions(map {$_->{OPT}, $_->{VAR}} grep { ref } @Options) || usage();
# Now setup default values.
foreach (@Options) {
if (ref $_ && defined($_->{DEFAULT}) && !defined(${$_->{VAR}})) {
${$_->{VAR}} = $_->{DEFAULT};
}
}
}
#----------------------------------------------------------------------
sub usage {
print STDERR "Synopsis:\n $EXE $VERSION - $DESC\n";
print STDERR "Author:\n $AUTHOR\n";
print STDERR "Usage:\n $EXE [options] <rRNA fasta sequence file>\n";
foreach (@Options) {
if (ref) {
my $def = defined($_->{DEFAULT}) ? " (default '$_->{DEFAULT}')" : "";
$def = ($def ? ' (default OFF)' : '(default ON)') if $_->{OPT} =~ m/!$/;
my $opt = $_->{OPT};
$opt =~ s/!$//;
$opt =~ s/=s$/ [X]/;
$opt =~ s/=i$/ [N]/;
$opt =~ s/=f$/ [n.n]/;
printf STDERR " --%-15s %s%s\n", $opt, $_->{DESC}, $def;
}
else {
print STDERR "$_\n";
}
}
exit(1);
}
#----------------------------------------------------------------------
sub version {
print STDERR "$EXE $VERSION\n";
exit;
}
sub decode {
my $str = shift;
$str =~ tr/+/ /;
$str =~ s/%([0-9a-fA-F]{2})/chr hex($1)/ge;
return $str;
}
sub encode {
my $str = shift;
$str =~ s/([\t\n\r%&\=;,])/sprintf("%%%X",ord($1))/ge;
return $str;
}
#----------------------------------------------------------------------
sub err {
msg(@_);
exit(2);
}