]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/blast_seq
e371b5cfd7ff8a0fb88472dc13b026d85708d11e
[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         { long => 'extend_threshold', short => 'E', type => 'uint',   mandatory => 'no', default => 0,     allowed => undef,     disallowed => undef },
56         { long => 'word_size',        short => 'W', type => 'uint',   mandatory => 'no', default => 0,     allowed => undef,     disallowed => undef },
57         { long => 'single_hit',       short => 's', type => 'flag',   mandatory => 'no', default => undef, allowed => undef,     disallowed => undef },
58     ]   
59 );
60
61 Maasha::Common::error( qq(both --database and --genome specified) ) if     $options->{ "genome" } and     $options->{ "database" };
62 Maasha::Common::error( qq(no --database or --genome specified) )    if not $options->{ "genome" } and not $options->{ "database" };
63
64 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
65 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
66
67 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
68
69 $tmp_dir = Maasha::Biopieces::get_tmpdir();
70 $tmp_in  = "$tmp_dir/blast_query.seq";
71 $tmp_out = "$tmp_dir/blast.result";
72
73 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
74
75 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
76 {
77     if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
78     {
79         $q_type = Maasha::Seq::seq_guess_type( $record->{ 'SEQ' } ) if not $q_type;
80
81         Maasha::Fasta::put_entry( $entry, $fh_out );
82     }
83
84     Maasha::Biopieces::put_record( $record, $out );
85 }
86
87 close $fh_out;
88
89 $s_type = guess_database_type( $options->{ 'database' } );
90
91 $program = $options->{ 'program' } || guess_program( $q_type, $s_type );
92
93 if ( $options->{ 'filter' } eq 'yes' ) {
94      $options->{ 'filter' } = 'T';
95 } else {
96      $options->{ 'filter' } = 'F';
97 }
98
99 if ( $options->{ 'no_gaps' } ) {
100     $options->{ 'gapped' } = 'F';
101 } else {
102     $options->{ 'gapped' } = 'T';
103 }
104
105 if ( $options->{ 'megablast' } ) {
106     $options->{ 'megablast' } = 'T';
107 } else {
108     $options->{ 'megablast' } = 'F';
109 }
110
111 if ( $options->{ 'single_hit' } ) {
112     $options->{ 'single_hit' } = 1
113 } else {
114     $options->{ 'single_hit' } = 0
115 }
116
117 if ( $options->{ 'verbose' } )
118 {
119     Maasha::Common::run(
120         "blastall",
121         join( " ",
122             "-p $program",
123             "-e $options->{ 'e_val' }",
124             "-a $options->{ 'cpus' }",
125             "-g $options->{ 'gapped' }",
126             "-n $options->{ 'megablast' }",
127             "-m 8",
128             "-i $tmp_in",
129             "-d $options->{ 'database' }",
130             "-F $options->{ 'filter' }",
131             "-P $options->{ 'single_hit' }",
132             "-W $options->{ 'word_size' }",
133             "-f $options->{ 'extend_threshold' }",
134             "-o $tmp_out",
135         ),
136         1
137     );
138 }
139 else
140 {
141     Maasha::Common::run(
142         "blastall",
143         join( " ",
144             "-p $program",
145             "-e $options->{ 'e_val' }",
146             "-a $options->{ 'cpus' }",
147             "-m 8",
148             "-i $tmp_in",
149             "-d $options->{ 'database' }",
150             "-F $options->{ 'filter' }",
151             "-P $options->{ 'single_hit' }",
152             "-W $options->{ 'word_size' }",
153             "-f $options->{ 'extend_threshold' }",
154             "-o $tmp_out",
155             "> /dev/null 2>&1"
156         ),
157         1
158     );
159 }
160
161 unlink $tmp_in;
162
163 $fh_in = Maasha::Filesys::file_read_open( $tmp_out );
164
165 while ( $entry = get_tab_entry( $fh_in ) )
166 {
167     $record = blast_tab2biopiece( $entry );
168
169     Maasha::Biopieces::put_record( $record, $out );
170 }
171
172 close $fh_out;
173
174 unlink $tmp_out;
175
176 Maasha::Biopieces::close_stream( $in );
177 Maasha::Biopieces::close_stream( $out );
178
179
180 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
181
182
183 sub guess_database_type
184 {
185     # Martin A. Hansen, May 2009
186
187     # Guess the type of BLAST database from the database
188     # filename assuming that it is a protein database if
189     # a .phr file exists.
190
191     # Returns string;
192     if ( -f $options->{ 'database' } . ".phr" or -f $options->{ 'database' } . ".pal" ) {
193         return "PROTEIN";
194     } else {
195         return "NUCLEOTIDE";
196     }
197 }
198
199
200 sub guess_program
201 {
202     # Martin A. Hansen, May 2009
203
204     # Guess what BLAST program to use based on the
205     # sequence type of the query and subject sequences.
206
207     my ( $q_type,   # query sequence type
208          $s_type,   # subject sequence type
209        ) = @_;
210
211     # Returns string.
212
213     my ( $program );
214
215     if ( $q_type      ne "PROTEIN" and $s_type ne "PROTEIN" ) {
216         $program = "blastn";
217     } elsif ( $q_type eq "PROTEIN" and $s_type eq "PROTEIN" ) {
218         $program = "blastp";
219     } elsif ( $q_type ne "PROTEIN" and $s_type eq "PROTEIN" ) {
220         $program = "blastx";
221     } elsif ( $q_type eq "PROTEIN" and $s_type ne "PROTEIN" ) {
222         $program = "tblastn";
223     }
224
225     return $program;
226 }
227
228
229
230 sub get_tab_entry
231 {
232     # Martin A. Hansen, May 2009.
233
234     # Get the next tabular entry from a filehandle to a BLAST file.
235
236     my ( $fh,   # filehandle
237        ) = @_;
238
239     # Returns a list
240
241     my ( $line, @fields );
242
243     while ( $line = <$fh> )
244     {
245         next if $line =~ /^#/;
246     
247         @fields = split /\s+/, $line;
248
249         return wantarray ? @fields : \@fields;
250     }
251
252     return undef;
253 }
254
255
256 sub blast_tab2biopiece
257 {
258     # Martin A. Hansen, May 2009.
259
260     # Get the next BLAST tabular entry and convert it to
261     # a biopiece record that is returned.
262
263     my ( $entry,    # BLAST tabular entry
264        ) = @_;
265
266     # Returns a hashref.
267
268     my ( %record );
269
270     $record{ "REC_TYPE" }   = "BLAST";
271     $record{ "Q_ID" }       = $entry->[ 0 ];
272     $record{ "S_ID" }       = $entry->[ 1 ];
273     $record{ "IDENT" }      = $entry->[ 2 ];
274     $record{ "ALIGN_LEN" }  = $entry->[ 3 ];
275     $record{ "MISMATCHES" } = $entry->[ 4 ];
276     $record{ "GAPS" }       = $entry->[ 5 ];
277     $record{ "Q_BEG" }      = $entry->[ 6 ] - 1; # BLAST is 1-based
278     $record{ "Q_END" }      = $entry->[ 7 ] - 1; # BLAST is 1-based
279     $record{ "S_BEG" }      = $entry->[ 8 ] - 1; # BLAST is 1-based
280     $record{ "S_END" }      = $entry->[ 9 ] - 1; # BLAST is 1-based
281     $record{ "E_VAL" }      = $entry->[ 10 ];
282     $record{ "BIT_SCORE" }  = $entry->[ 11 ];
283
284     if ( $record{ "S_BEG" } > $record{ "S_END" } )
285     {
286         $record{ "STRAND" } = '-';
287
288         ( $record{ "S_BEG" }, $record{ "S_END" } ) = ( $record{ "S_END" }, $record{ "S_BEG" } );
289     }
290     else
291     {
292         $record{ "STRAND" } = '+';
293     }
294
295     return wantarray ? %record : \%record;
296 }
297
298
299 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
300
301
302 BEGIN
303 {
304     Maasha::Biopieces::status_set();
305 }
306
307
308 END
309 {
310     Maasha::Biopieces::status_log();
311 }
312
313
314 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
315
316
317 __END__