-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseTaxonomizedBLAST.pl
executable file
·213 lines (179 loc) · 6.37 KB
/
parseTaxonomizedBLAST.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
#!/usr/bin/env perl
## Pombert Lab, IIT 2019
my $name = 'parseTaxonomizedBLAST.pl';
my $version = '0.2b';
my $updated = '2024-06-02';
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
################################################################################
# Usage definition
################################################################################
my $usage = <<"OPTIONS";
NAME ${name}
VERSION ${version}
UPDATED ${updated}
SYNOPSIS Parses the content of taxonomized BLAST searches
REQS -outfmt '6 qseqid sseqid qstart qend pident length bitscore evalue staxids sscinames sskingdoms sblastnames'
USAGE ${name} \\
-b *.outfmt.6 \\
-f *.fasta \\
-n Streptococcus 'Streptococcus suis' 'Streptococcus sp.' \\
-e 1e-25 \\
-o output.fasta \\
-v
OPTIONS:
-b (--blast) BLAST input file(s)
-f (--fasta) FASTA file(s)
-n (--name) Names to be queried
-i (--inverse) Invert selection; returns queries NOT matching specified names
-c (--column) Which columns to query: sscinames, sskingdoms or sblastnames [Default: sscinames]
-e (--evalue) E-value cutoff for target organism(s) [Default: 1e-10]
-o (--output) FASTA output file containing the desired sequences
-v (--verbose) Verbose [Default: off]
--version Show script version
OPTIONS
unless (@ARGV){
print "\n$usage\n";
exit(0);
};
################################################################################
### Command line options
################################################################################
my @blast;
my @fasta;
my @target;
my $column = 'sscinames';
my $evalue = 1e-10;
my $output;
my $inverse;
my $verbose;
my $sc_version;
GetOptions(
'b|blast=s@{1,}' => \@blast,
'f|fasta=s@{1,}' => \@fasta,
'n|name=s@{1,}' => \@target,
'c|column=s' => \$column,
'e|evalue=s' => \$evalue,
'o|output=s' => \$output,
'I|inverse' => \$inverse,
'v|verbose' => \$verbose,
'version' => \$sc_version
);
unless ( ($column eq 'sscinames') || ($column eq 'sskingdoms') || ($column eq 'sblastnames') ){
print "\nError. Column name $column is not recognised.\n";
print "Please use either sscinames, sskingdoms or sblastnames\n\n";
exit;
}
################################################################################
### Version
################################################################################
if ($sc_version){
print "\n";
print "Script: $name\n";
print "Version: $version\n";
print "Updated: $updated\n\n";
exit(0);
}
################################################################################
### Creating dababase of sequences from FASTA files
################################################################################
my %sequences;
for my $fasta (@fasta){
open FASTA, "<", $fasta or die "Can't open file $fasta: $!\n";
my $name;
while (my $line = <FASTA>){
chomp $line;
if ($line =~ /^>(\S+)/){ $name = $1; }
else { $sequences{$name} .= $line; }
}
}
### Creating db of names to search for
my %scinames;
for my $names (@target){
$scinames{$names} = $name;
}
################################################################################
### Parsing BLAST 'outfmt 6' file(s)
################################################################################
my %blasts;
while (my $blast = shift@blast){
open BLAST, "<", $blast or die "Can't open file $blast: $!\n";
while (my $line = <BLAST>){
chomp $line;
my @columns = split("\t", $line);
my $query = $columns[0];
my $bitscore = $columns[6];
my $ev = $columns[7];
## Columns:
## [0] query, [1] target, [2] qstart, [3] qend, [4] pident, [5] length,
## [6] bitscore, [7] evalue, [8] taxid, [9] sciname, [10] kingdom, [11] blastname
unless ($ev <= $evalue){ next; }
if (exists $blasts{$blast}{$query}){
if ($bitscore > $blasts{$blast}{$query}[6]){ ## Checking for better hit(s) based on bitscores
for (0..$#columns){
$blasts{$blast}{$query}[$_] = $columns[$_];
}
}
}
else {
for (0..$#columns){
$blasts{$blast}{$query}[$_] = $columns[$_];
}
}
}
}
################################################################################
### Output file
################################################################################
open OUT, ">", $output or die "Can't create file $output: $!\n";
my @blasts = sort (keys %blasts);
for my $blast (@blasts) {
for my $query (sort (keys %{$blasts{$blast}})){
## Setting which column to parse
my $regex;
if ($column eq 'sscinames'){ $regex = $blasts{$blast}{$query}[9]; }
elsif ($column eq 'sskingdoms'){ $regex = $blasts{$blast}{$query}[10]; }
elsif ($column eq 'sblastnames'){ $regex = $blasts{$blast}{$query}[11]; }
if ($verbose){
print "Best hit for $query = $regex\n";
}
my @names = keys %scinames;
my $flag = undef;
for my $name (@names){
if (!defined $flag){
unless ($inverse){
if ($regex =~ /$name/i){
if ($verbose){
print "Match found for $name: ";
print "@{$blasts{$blast}{$query}}\n";
}
$flag = 'match';
fasta($query);
}
}
else {
if ($regex !~ /$name/i){
if ($verbose){
print "Match different from $name: ";
print "@{$blasts{$blast}{$query}}\n";
}
$flag = 'match';
fasta($query);
}
}
}
}
}
}
################################################################################
### Subroutine(s)
################################################################################
sub fasta {
my $seq = $_[0];
print OUT ">$seq\n";
my @seq = unpack ("(A60)*", $sequences{$seq});
while (my $tmp = shift@seq){
print OUT "$tmp\n";
}
}