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