forked from jmeneghin/perl-for-reysenbach-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblast_meta_summary.pl
More file actions
240 lines (239 loc) · 7.63 KB
/
blast_meta_summary.pl
File metadata and controls
240 lines (239 loc) · 7.63 KB
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
#!/usr/bin/perl -w
####################################
### Summarize Blast Output ###
### Jennifer Meneghin ###
### June 15, 2009 ###
### Updated September 09, 2009 ###
### Updated August 18, 2010 ###
####################################
#---------------------------------------------------------------------------------------------------------------------------
#Deal with passed parameters
#---------------------------------------------------------------------------------------------------------------------------
#If no arguments are passed, show usage message and exit program.
if ($#ARGV == -1) {
&usage;
}
$in_file = "";
$out_file = "summary.out";
$db_file = "";
$perkb = 0;
$spit = 0;
$text_file = "/vol/share/biology/cog/COG_12Bacteria.txt";
$sum_col = 3;
%my_args = @ARGV;
for $i (sort keys %my_args) {
if ($i eq "-i") {
$in_file = $my_args{$i};
}
elsif ($i eq "-o") {
$out_file = $my_args{$i};
}
elsif ($i eq "-d") {
$db_file = $my_args{$i};
$perkb = 1;
}
elsif ($i eq "-s") {
if (lc($my_args{$i}) eq "y") {
$spit = 1;
}
else {
$spit = 0;
}
}
elsif ($i eq "-t") {
$text_file = $my_args{$i};
}
elsif ($i eq "-c") {
$sum_col = $my_args{$i};
if (!($sum_col =~ /^\d+$/)) {
print "\nSummary column must be an integer: $sum_col\n";
&usage;
}
if ($sum_col < 1) {
print "\nSummary column must be a postive integer (> 0): $sum_col\n";
&usage;
}
}
else {
print "\nUnrecognized argument: $i\n\n";
&usage;
}
}
unless ( open(TEXT, "$text_file") ) {
print "\nGot a bad text file: $text_file\n\n";
&usage;
}
unless ( open(IN, "$in_file") ) {
print "\nGot a bad input file: $in_file\n\n";
&usage;
}
unless ( open(OUT, ">$out_file") ) {
print "\nGot a bad output file: $out_file\n\n";
&usage;
}
if ($perkb == 1 && length($db_file) > 0) {
unless ( open(DB, "$db_file") ) {
print "\nGot a bad database file: $db_file\n\n";
&usage;
}
}
if ($spit == 1) {
unless ( open(SPIT, ">suspected_rrna.txt") ) {
print "Got a bad output file: suspected_rrna.txt\n";
exit;
}
}
#Everything looks good. Print the parameters we've found.
print "Parameters:\ninput file = $in_file\noutput file = $out_file";
if ($perkb == 1 && length($db_file) > 0) {
print "\ndatabase file = $db_file";
}
if ($spit == 1) {
print "\nspit file = suspected_rrna.txt\n";
}
print "\n\n";
#-----------------------------------------------------------------------------------------------------------------------------------------
#The main event
#-----------------------------------------------------------------------------------------------------------------------------------------
%genbank_percent = ();
%genbank_count = ();
%functions = ();
while (<IN>) {
chomp;
@fields = split(/\t/);
$genbank_id = $fields[1];
$percent_identity = $fields[2];
if ( $genbank_count{$genbank_id} ) {
$genbank_percent{$genbank_id} = $genbank_percent{$genbank_id} + $percent_identity;
$genbank_count{$genbank_id} = $genbank_count{$genbank_id} + 1;
}
else {
$genbank_percent{$genbank_id} = $percent_identity;
$genbank_count{$genbank_id} = 1;
}
}
while (<TEXT>) {
$line = $_;
chomp($line);
@tab_fields = split(/\t/, $line);
$id = $tab_fields[0];
$cog = "Unkown";
if ($#tab_fields >= ($sum_col-1)) {
$cog = $tab_fields[$sum_col-1];
}
$cog =~ s/"//g;
$rest = $line;
$rest =~ s/^.+?\t(.+)$/$1/;
if ($genbank_count{$id}) {
$functions{$cog}{$id} = $rest;
}
}
%fasta_lengths = ();
$seq = "";
if ($perkb == 1) {
while (<DB>) {
chomp;
if (/^>/) {
#finish up previous line.
if (length($seq) > 0) {
$fasta_lengths{$id} = length($seq);
}
$id = $_;
$id =~ s/^>(.+?)\s.+$/$1/g;
$seq = "";
}
else {
$seq = $seq . $_;
}
}
$fasta_lengths{$id} = length($seq);
}
if ($perkb == 1) {
print "CATEGORY\tCOUNT\tCOUNT PER KB\tAVERAGE PERCENT IDENTITY\n";
print OUT "CATEGORY\tCOUNT\tCOUNT PER KB\tAVERAGE PERCENT IDENTITY\n";
}
else {
print "CATEGORY\tCOUNT\tAVERAGE PERCENT IDENTITY\n";
print OUT "CATEGORY\tCOUNT\tAVERAGE PERCENT IDENTITY\n";
}
foreach $j (sort by_id keys %functions) {
$gb_count_sum = 0;
$gb_percent_sum = 0;
$gb_count_perkb_sum = 0;
foreach $i (sort by_id keys %genbank_count) {
if ($functions{$j}{$i}) {
if ($spit == 1) {
if ($functions{$j}{$i} =~ /5S ribosomal RNA/ ||
$functions{$j}{$i} =~ /16S ribosomal RNA/ ||
$functions{$j}{$i} =~ /23S ribosomal RNA/ ||
$functions{$j}{$i} =~ /28S ribosomal RNA/ ||
$functions{$j}{$i} =~ /18S ribosomal RNA/) {
$average = $genbank_percent{$i} / $genbank_count{$i};
#print "$i\t$genbank_count{$i}\t$average\t$functions{$j}{$i}\n";
print SPIT "$i\t$genbank_count{$i}\t$average\t$functions{$j}{$i}\n";
}
else {
$gb_count_sum = $gb_count_sum + $genbank_count{$i};
$gb_percent_sum = $gb_percent_sum + $genbank_percent{$i};
if ($perkb == 1 && $fasta_lengths{$i} > 0) {
$gb_count_perkb_sum = $gb_count_perkb_sum + (($genbank_count{$i}*1000)/$fasta_lengths{$i});
}
elsif ($perkb == 1) {
print "gotta bad fasta length?!\n";
}
}
}
else {
$gb_count_sum = $gb_count_sum + $genbank_count{$i};
$gb_percent_sum = $gb_percent_sum + $genbank_percent{$i};
if ($perkb == 1 && $fasta_lengths{$i} > 0) {
$gb_count_perkb_sum = $gb_count_perkb_sum + (($genbank_count{$i}*1000)/$fasta_lengths{$i});
}
elsif ($perkb == 1) {
print "gotta bad fasta length?!\n";
}
}
}
}
$average = 0;
if ($gb_count_sum > 0) {
$average = $gb_percent_sum / $gb_count_sum;
}
if ($perkb == 1) {
print "$j\t$gb_count_sum\t$gb_count_perkb_sum\t$average\n";
print OUT "$j\t$gb_count_sum\t$gb_count_perkb_sum\t$average\n";
}
else {
print "$j\t$gb_count_sum\t$average\n";
print OUT "$j\t$gb_count_sum\t$average\n";
}
}
close(TEXT);
close(IN);
close(OUT);
if (length($db_file) > 0) {
close(DB);
}
sub by_id { $a cmp $b; }
sub usage {
print "\nBLAST META SUMMARY 4.3\n";
print "Jennifer Meneghin\n";
print "August 18, 2010\n\n";
print "Usage: blast_meta_summary.pl\n\n";
print "Parameters:\n";
print "-i input_file\tThe blast output file in short format\n";
print "-o output_file\tThe output file to create (optional. Default = summary.out)\n";
print "-d fasta_file\tIf you want to calculate count per KB, you need to provide the fasta file used as the blast database\n";
print "\t\t(optional. Should be a copy of 12bacteria.fasta if you are using COG_12Bacteria.txt)\n";
print "-s y/n\t\ty if you want to \"spit\" the ribosomal lines, n if you do not (optional. Default = n)\n";
print "-t text_file\tA tabbed delimmited file with BLAST database fasta ID in the first column\n";
print "\t\t(optional. Default = /vol/share/biology/cog/COG_12Bacteria.txt on the research server.)\n";
print "-c N\t\tThe column to summarize by in the text_file (optional. Default = 3)\n\n";
print "This program takes a skimmed BLAST output file in short format as it's input file,\n";
print "and returns a tab delimmited list of unique categories found, the number of times each appeared,\n";
print "and the average percent identity found for each.\n\n";
print "If a text_file is not provided this script requires /vol/share/biology/cog/COG_12Bacteria.txt in order to work properly.\n";
print "If COG_12Bacteria.txt is used, -c 3 (the default) will summarize by organism, -c 7 will summarize by COG category, and -c 4 will summarize by COG number.\n\n";
print "If the database file used in the BLAST (in fasta format) is provided, it will also report the count per KB.\n\n";
exit;
}