]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/blast_seq
fixed classify_taxonomy
[biopieces.git] / bp_bin / blast_seq
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2007-2009 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # BLAST sequences in the stream against a specified database or genome.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::Biopieces;
32 use Maasha::Common;
33 use Maasha::Seq;
34 use Maasha::Fasta;
35
36
37 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38
39
40 my ( $options, $in, $out, $tmp_dir, $tmp_in, $tmp_out, $q_type, $s_type, $record, $entry,
41      $fh_in, $fh_out, $progs_ok, $program );
42
43 $progs_ok = 'blastn,blastp,tblastn,blastx,tblastx';
44
45 $options = Maasha::Biopieces::parse_options(
46     [
47         { long => 'database',  short => 'd', type => 'file',   mandatory => 'no', default => undef, allowed => undef,     disallowed => undef },
48         { long => 'genome',    short => 'g', type => 'genome', mandatory => 'no', default => undef, allowed => undef,     disallowed => undef },
49         { long => 'program',   short => 'p', type => 'string', mandatory => 'no', default => undef, allowed => $progs_ok, disallowed => undef },
50         { long => 'e_val',     short => 'e', type => 'float',  mandatory => 'no', default => 10,    allowed => undef,     disallowed => undef },
51         { long => 'filter',    short => 'f', type => 'string', mandatory => 'no', default => 'no',  allowed => 'yes,no',  disallowed => undef },
52         { long => 'cpus',      short => 'c', type => 'uint',   mandatory => 'no', default => 1,     allowed => undef,     disallowed => 0 },
53         { long => 'no_gaps',   short => 'G', type => 'flag',   mandatory => 'no', default => undef, allowed => undef,     disallowed => undef },
54         { long => 'megablast', short => 'm', type => 'flag',   mandatory => 'no', default => undef, allowed => undef,     disallowed => undef },
55     ]   
56 );
57
58 Maasha::Common::error( qq(both --database and --genome specified) ) if     $options->{ "genome" } and     $options->{ "database" };
59 Maasha::Common::error( qq(no --database or --genome specified) )    if not $options->{ "genome" } and not $options->{ "database" };
60
61 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
62 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
63
64 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
65
66 $tmp_dir = Maasha::Biopieces::get_tmpdir();
67 $tmp_in  = "$tmp_dir/blast_query.seq";
68 $tmp_out = "$tmp_dir/blast.result";
69
70 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
71
72 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
73 {
74     if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
75     {
76         $q_type = Maasha::Seq::seq_guess_type( $record->{ 'SEQ' } ) if not $q_type;
77
78         Maasha::Fasta::put_entry( $entry, $fh_out );
79     }
80
81     Maasha::Biopieces::put_record( $record, $out );
82 }
83
84 close $fh_out;
85
86 $s_type = guess_database_type( $options->{ 'database' } );
87
88 $program = $options->{ 'program' } || guess_program( $q_type, $s_type );
89
90 if ( $options->{ 'filter' } eq 'yes' ) {
91      $options->{ 'filter' } = 'T';
92 } else {
93      $options->{ 'filter' } = 'F';
94 }
95
96 if ( $options->{ 'no_gaps' } ) {
97     $options->{ 'gapped' } = 'F';
98 } else {
99     $options->{ 'gapped' } = 'T';
100 }
101
102 if ( $options->{ 'megablast' } ) {
103     $options->{ 'megablast' } = 'T';
104 } else {
105     $options->{ 'megablast' } = 'F';
106 }
107
108 if ( $options->{ 'verbose' } )
109 {
110     Maasha::Common::run(
111         "blastall",
112         join( " ",
113             "-p $program",
114             "-e $options->{ 'e_val' }",
115             "-a $options->{ 'cpus' }",
116             "-g $options->{ 'gapped' }",
117             "-n $options->{ 'megablast' }",
118             "-m 8",
119             "-i $tmp_in",
120             "-d $options->{ 'database' }",
121             "-F $options->{ 'filter' }",
122             "-o $tmp_out",
123         ),
124         1
125     );
126 }
127 else
128 {
129     Maasha::Common::run(
130         "blastall",
131         join( " ",
132             "-p $program",
133             "-e $options->{ 'e_val' }",
134             "-a $options->{ 'cpus' }",
135             "-m 8",
136             "-i $tmp_in",
137             "-d $options->{ 'database' }",
138             "-F $options->{ 'filter' }",
139             "-o $tmp_out",
140             "> /dev/null 2>&1"
141         ),
142         1
143     );
144 }
145
146 unlink $tmp_in;
147
148 $fh_in = Maasha::Filesys::file_read_open( $tmp_out );
149
150 while ( $entry = get_tab_entry( $fh_in ) )
151 {
152     $record = blast_tab2biopiece( $entry );
153
154     Maasha::Biopieces::put_record( $record, $out );
155 }
156
157 close $fh_out;
158
159 unlink $tmp_out;
160
161 Maasha::Biopieces::close_stream( $in );
162 Maasha::Biopieces::close_stream( $out );
163
164
165 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
166
167
168 sub guess_database_type
169 {
170     # Martin A. Hansen, May 2009
171
172     # Guess the type of BLAST database from the database
173     # filename assuming that it is a protein database if
174     # a .phr file exists.
175
176     # Returns string;
177     if ( -f $options->{ 'database' } . ".phr" or -f $options->{ 'database' } . ".pal" ) {
178         return "PROTEIN";
179     } else {
180         return "NUCLEOTIDE";
181     }
182 }
183
184
185 sub guess_program
186 {
187     # Martin A. Hansen, May 2009
188
189     # Guess what BLAST program to use based on the
190     # sequence type of the query and subject sequences.
191
192     my ( $q_type,   # query sequence type
193          $s_type,   # subject sequence type
194        ) = @_;
195
196     # Returns string.
197
198     my ( $program );
199
200     if ( $q_type      ne "PROTEIN" and $s_type ne "PROTEIN" ) {
201         $program = "blastn";
202     } elsif ( $q_type eq "PROTEIN" and $s_type eq "PROTEIN" ) {
203         $program = "blastp";
204     } elsif ( $q_type ne "PROTEIN" and $s_type eq "PROTEIN" ) {
205         $program = "blastx";
206     } elsif ( $q_type eq "PROTEIN" and $s_type ne "PROTEIN" ) {
207         $program = "tblastn";
208     }
209
210     return $program;
211 }
212
213
214
215 sub get_tab_entry
216 {
217     # Martin A. Hansen, May 2009.
218
219     # Get the next tabular entry from a filehandle to a BLAST file.
220
221     my ( $fh,   # filehandle
222        ) = @_;
223
224     # Returns a list
225
226     my ( $line, @fields );
227
228     while ( $line = <$fh> )
229     {
230         next if $line =~ /^#/;
231     
232         @fields = split /\s+/, $line;
233
234         return wantarray ? @fields : \@fields;
235     }
236
237     return undef;
238 }
239
240
241 sub blast_tab2biopiece
242 {
243     # Martin A. Hansen, May 2009.
244
245     # Get the next BLAST tabular entry and convert it to
246     # a biopiece record that is returned.
247
248     my ( $entry,    # BLAST tabular entry
249        ) = @_;
250
251     # Returns a hashref.
252
253     my ( %record );
254
255     $record{ "REC_TYPE" }   = "BLAST";
256     $record{ "Q_ID" }       = $entry->[ 0 ];
257     $record{ "S_ID" }       = $entry->[ 1 ];
258     $record{ "IDENT" }      = $entry->[ 2 ];
259     $record{ "ALIGN_LEN" }  = $entry->[ 3 ];
260     $record{ "MISMATCHES" } = $entry->[ 4 ];
261     $record{ "GAPS" }       = $entry->[ 5 ];
262     $record{ "Q_BEG" }      = $entry->[ 6 ] - 1; # BLAST is 1-based
263     $record{ "Q_END" }      = $entry->[ 7 ] - 1; # BLAST is 1-based
264     $record{ "S_BEG" }      = $entry->[ 8 ] - 1; # BLAST is 1-based
265     $record{ "S_END" }      = $entry->[ 9 ] - 1; # BLAST is 1-based
266     $record{ "E_VAL" }      = $entry->[ 10 ];
267     $record{ "BIT_SCORE" }  = $entry->[ 11 ];
268
269     if ( $record{ "S_BEG" } > $record{ "S_END" } )
270     {
271         $record{ "STRAND" } = '-';
272
273         ( $record{ "S_BEG" }, $record{ "S_END" } ) = ( $record{ "S_END" }, $record{ "S_BEG" } );
274     }
275     else
276     {
277         $record{ "STRAND" } = '+';
278     }
279
280     return wantarray ? %record : \%record;
281 }
282
283
284 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
285
286
287 BEGIN
288 {
289     Maasha::Biopieces::status_set();
290 }
291
292
293 END
294 {
295     Maasha::Biopieces::status_log();
296 }
297
298
299 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
300
301
302 __END__