]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/blast_seq
implementing status solution
[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 ( $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 Maasha::Biopieces::close_stream( $in );
145 Maasha::Biopieces::close_stream( $out );
146
147
148 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
149
150
151 sub guess_database_type
152 {
153     # Martin A. Hansen, May 2009
154
155     # Guess the type of BLAST database from the database
156     # filename assuming that it is a protein database if
157     # a .phr file exists.
158
159     # Returns string;
160     if ( -f $options->{ 'database' } . ".phr" ) {
161         return "protein";
162     } else {
163         return "nucleotide";
164     }
165 }
166
167
168 sub guess_program
169 {
170     # Martin A. Hansen, May 2009
171
172     # Guess what BLAST program to use based on the
173     # sequence type of the query and subject sequences.
174
175     my ( $q_type,   # query sequence type
176          $s_type,   # subject sequence type
177        ) = @_;
178
179     # Returns string.
180
181     my ( $program );
182
183     if ( $q_type      ne "protein" and $s_type ne "protein" ) {
184         $program = "blastn";
185     } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
186         $program = "blastp";
187     } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
188         $program = "blastx";
189     } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
190         $program = "tblastn";
191     }
192
193     return $program;
194 }
195
196
197
198 sub get_tab_entry
199 {
200     # Martin A. Hansen, May 2009.
201
202     # Get the next tabular entry from a filehandle to a BLAST file.
203
204     my ( $fh,   # filehandle
205        ) = @_;
206
207     # Returns a list
208
209     my ( $line, @fields );
210
211     while ( $line = <$fh> )
212     {
213         next if $line =~ /^#/;
214     
215         @fields = split /\s+/, $line;
216
217         use Data::Dumper;
218
219         return wantarray ? @fields : \@fields;
220     }
221
222     return undef;
223 }
224
225
226 sub blast_tab2biopiece
227 {
228     # Martin A. Hansen, May 2009.
229
230     # Get the next BLAST tabular entry and convert it to
231     # a biopiece record that is returned.
232
233     my ( $entry,    # BLAST tabular entry
234        ) = @_;
235
236     # Returns a hashref.
237
238     my ( %record );
239
240     $record{ "REC_TYPE" }   = "BLAST";
241     $record{ "Q_ID" }       = $entry->[ 0 ];
242     $record{ "S_ID" }       = $entry->[ 1 ];
243     $record{ "IDENT" }      = $entry->[ 2 ];
244     $record{ "ALIGN_LEN" }  = $entry->[ 3 ];
245     $record{ "MISMATCHES" } = $entry->[ 4 ];
246     $record{ "GAPS" }       = $entry->[ 5 ];
247     $record{ "Q_BEG" }      = $entry->[ 6 ] - 1; # BLAST is 1-based
248     $record{ "Q_END" }      = $entry->[ 7 ] - 1; # BLAST is 1-based
249     $record{ "S_BEG" }      = $entry->[ 8 ] - 1; # BLAST is 1-based
250     $record{ "S_END" }      = $entry->[ 9 ] - 1; # BLAST is 1-based
251     $record{ "E_VAL" }      = $entry->[ 10 ];
252     $record{ "BIT_SCORE" }  = $entry->[ 11 ];
253
254     if ( $record{ "S_BEG" } > $record{ "S_END" } )
255     {
256         $record{ "STRAND" } = '-';
257
258         ( $record{ "S_BEG" }, $record{ "S_END" } ) = ( $record{ "S_END" }, $record{ "S_BEG" } );
259     }
260     else
261     {
262         $record{ "STRAND" } = '+';
263     }
264
265     return wantarray ? %record : \%record;
266 }
267
268
269 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
270
271
272 BEGIN
273 {
274     Maasha::Biopieces::status_set();
275 }
276
277
278 END
279 {
280     Maasha::Biopieces::status_log();
281 }
282
283
284 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
285
286
287 __END__