]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/blast_seq
fixed seq qual length check
[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     );
137 }
138 else
139 {
140     Maasha::Common::run(
141         "blastall",
142         join( " ",
143             "-p $program",
144             "-e $options->{ 'e_val' }",
145             "-a $options->{ 'cpus' }",
146             "-m 8",
147             "-i $tmp_in",
148             "-d $options->{ 'database' }",
149             "-F $options->{ 'filter' }",
150             "-P $options->{ 'single_hit' }",
151             "-W $options->{ 'word_size' }",
152             "-f $options->{ 'extend_threshold' }",
153             "-o $tmp_out",
154             "> /dev/null 2>&1"
155         )
156     );
157 }
158
159 unlink $tmp_in;
160
161 $fh_in = Maasha::Filesys::file_read_open( $tmp_out );
162
163 while ( $entry = get_tab_entry( $fh_in ) )
164 {
165     $record = blast_tab2biopiece( $entry );
166
167     Maasha::Biopieces::put_record( $record, $out );
168 }
169
170 close $fh_out;
171
172 unlink $tmp_out;
173
174 Maasha::Biopieces::close_stream( $in );
175 Maasha::Biopieces::close_stream( $out );
176
177
178 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
179
180
181 sub guess_database_type
182 {
183     # Martin A. Hansen, May 2009
184
185     # Guess the type of BLAST database from the database
186     # filename assuming that it is a protein database if
187     # a .phr file exists.
188
189     # Returns string;
190     if ( -f $options->{ 'database' } . ".phr" or -f $options->{ 'database' } . ".pal" ) {
191         return "PROTEIN";
192     } else {
193         return "NUCLEOTIDE";
194     }
195 }
196
197
198 sub guess_program
199 {
200     # Martin A. Hansen, May 2009
201
202     # Guess what BLAST program to use based on the
203     # sequence type of the query and subject sequences.
204
205     my ( $q_type,   # query sequence type
206          $s_type,   # subject sequence type
207        ) = @_;
208
209     # Returns string.
210
211     my ( $program );
212
213     if ( $q_type      ne "PROTEIN" and $s_type ne "PROTEIN" ) {
214         $program = "blastn";
215     } elsif ( $q_type eq "PROTEIN" and $s_type eq "PROTEIN" ) {
216         $program = "blastp";
217     } elsif ( $q_type ne "PROTEIN" and $s_type eq "PROTEIN" ) {
218         $program = "blastx";
219     } elsif ( $q_type eq "PROTEIN" and $s_type ne "PROTEIN" ) {
220         $program = "tblastn";
221     }
222
223     return $program;
224 }
225
226
227
228 sub get_tab_entry
229 {
230     # Martin A. Hansen, May 2009.
231
232     # Get the next tabular entry from a filehandle to a BLAST file.
233
234     my ( $fh,   # filehandle
235        ) = @_;
236
237     # Returns a list
238
239     my ( $line, @fields );
240
241     while ( $line = <$fh> )
242     {
243         next if $line =~ /^#/;
244     
245         @fields = split /\s+/, $line;
246
247         return wantarray ? @fields : \@fields;
248     }
249
250     return undef;
251 }
252
253
254 sub blast_tab2biopiece
255 {
256     # Martin A. Hansen, May 2009.
257
258     # Get the next BLAST tabular entry and convert it to
259     # a biopiece record that is returned.
260
261     my ( $entry,    # BLAST tabular entry
262        ) = @_;
263
264     # Returns a hashref.
265
266     my ( %record );
267
268     $record{ "REC_TYPE" }   = "BLAST";
269     $record{ "Q_ID" }       = $entry->[ 0 ];
270     $record{ "S_ID" }       = $entry->[ 1 ];
271     $record{ "IDENT" }      = $entry->[ 2 ];
272     $record{ "ALIGN_LEN" }  = $entry->[ 3 ];
273     $record{ "MISMATCHES" } = $entry->[ 4 ];
274     $record{ "GAPS" }       = $entry->[ 5 ];
275     $record{ "Q_BEG" }      = $entry->[ 6 ] - 1; # BLAST is 1-based
276     $record{ "Q_END" }      = $entry->[ 7 ] - 1; # BLAST is 1-based
277     $record{ "S_BEG" }      = $entry->[ 8 ] - 1; # BLAST is 1-based
278     $record{ "S_END" }      = $entry->[ 9 ] - 1; # BLAST is 1-based
279     $record{ "E_VAL" }      = $entry->[ 10 ];
280     $record{ "BIT_SCORE" }  = $entry->[ 11 ];
281
282     if ( $record{ "S_BEG" } > $record{ "S_END" } )
283     {
284         $record{ "STRAND" } = '-';
285
286         ( $record{ "S_BEG" }, $record{ "S_END" } ) = ( $record{ "S_END" }, $record{ "S_BEG" } );
287     }
288     else
289     {
290         $record{ "STRAND" } = '+';
291     }
292
293     return wantarray ? %record : \%record;
294 }
295
296
297 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
298
299
300 BEGIN
301 {
302     Maasha::Biopieces::status_set();
303 }
304
305
306 END
307 {
308     Maasha::Biopieces::status_log();
309 }
310
311
312 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
313
314
315 __END__