]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/blast_seq
added use strict to all biopieces
[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, $program );
42
43 $options = Maasha::Biopieces::parse_options(
44     [
45         { long => 'database', short => 'd', type => 'file',   mandatory => 'no', default => undef, allowed => undef,                                  disallowed => undef },
46         { long => 'genome',   short => 'g', type => 'genome', mandatory => 'no', default => undef, allowed => undef,                                  disallowed => undef },
47         { long => 'program',  short => 'p', type => 'string', mandatory => 'no', default => undef, allowed => 'blastn,blastp,tblastn,blastx,tblastx', disallowed => undef },
48         { long => 'e_val',    short => 'e', type => 'float',  mandatory => 'no', default => 10,    allowed => undef,                                  disallowed => undef },
49         { long => 'filter',   short => 'f', type => 'string', mandatory => 'no', default => 'no',  allowed => 'yes,no',                               disallowed => undef },
50         { long => 'cpus',     short => 'c', type => 'uint',   mandatory => 'no', default => 1,     allowed => undef,                                  disallowed => 0 },
51     ]   
52 );
53
54 Maasha::Common::error( qq(both --database and --genome specified) ) if     $options->{ "genome" } and     $options->{ "database" };
55 Maasha::Common::error( qq(no --database or --genome specified) )    if not $options->{ "genome" } and not $options->{ "database" };
56
57 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
58 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
59
60 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
61
62 $tmp_dir = Maasha::Biopieces::get_tmpdir();
63 $tmp_in  = "$tmp_dir/blast_query.seq";
64 $tmp_out = "$tmp_dir/blast.result";
65
66 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
67
68 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
69 {
70     if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
71     {
72         $q_type = Maasha::Seq::seq_guess_type( $record->{ 'SEQ' } ) if not $q_type;
73
74         Maasha::Fasta::put_entry( $entry, $fh_out );
75     }
76
77     Maasha::Biopieces::put_record( $record, $out );
78 }
79
80 close $fh_out;
81
82 $s_type = guess_database_type( $options->{ 'database' } );
83
84 $program = $options->{ 'program' } || guess_program( $q_type, $s_type );
85
86 if ( $options->{ 'filter' } eq 'yes' ) {
87      $options->{ 'filter' } = 'T';
88 } else {
89      $options->{ 'filter' } = 'F';
90 }
91
92 if ( $options->{ 'verbose' } )
93 {
94     Maasha::Common::run(
95         "blastall",
96         join( " ",
97             "-p $program",
98             "-e $options->{ 'e_val' }",
99             "-a $options->{ 'cpus' }",
100             "-m 8",
101             "-i $tmp_in",
102             "-d $options->{ 'database' }",
103             "-F $options->{ 'filter' }",
104             "-o $tmp_out",
105         ),
106         1
107     );
108 }
109 else
110 {
111     Maasha::Common::run(
112         "blastall",
113         join( " ",
114             "-p $program",
115             "-e $options->{ 'e_val' }",
116             "-a $options->{ 'cpus' }",
117             "-m 8",
118             "-i $tmp_in",
119             "-d $options->{ 'database' }",
120             "-F $options->{ 'filter' }",
121             "-o $tmp_out",
122             "> /dev/null 2>&1"
123         ),
124         1
125     );
126 }
127
128 unlink $tmp_in;
129
130 $fh_in = Maasha::Filesys::file_read_open( $tmp_out );
131
132 while ( $entry = get_tab_entry( $fh_in ) )
133 {
134     $record = blast_tab2biopiece( $entry );
135
136     Maasha::Biopieces::put_record( $record, $out );
137 }
138
139 close $fh_out;
140
141 unlink $tmp_out;
142
143 Maasha::Biopieces::close_stream( $in );
144 Maasha::Biopieces::close_stream( $out );
145
146
147 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
148
149
150 sub guess_database_type
151 {
152     # Martin A. Hansen, May 2009
153
154     # Guess the type of BLAST database from the database
155     # filename assuming that it is a protein database if
156     # a .phr file exists.
157
158     # Returns string;
159     if ( -f $options->{ 'database' } . ".phr" ) {
160         return "protein";
161     } else {
162         return "nucleotide";
163     }
164 }
165
166
167 sub guess_program
168 {
169     # Martin A. Hansen, May 2009
170
171     # Guess what BLAST program to use based on the
172     # sequence type of the query and subject sequences.
173
174     my ( $q_type,   # query sequence type
175          $s_type,   # subject sequence type
176        ) = @_;
177
178     # Returns string.
179
180     my ( $program );
181
182     if ( $q_type      ne "protein" and $s_type ne "protein" ) {
183         $program = "blastn";
184     } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
185         $program = "blastp";
186     } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
187         $program = "blastx";
188     } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
189         $program = "tblastn";
190     }
191
192     return $program;
193 }
194
195
196
197 sub get_tab_entry
198 {
199     # Martin A. Hansen, May 2009.
200
201     # Get the next tabular entry from a filehandle to a BLAST file.
202
203     my ( $fh,   # filehandle
204        ) = @_;
205
206     # Returns a list
207
208     my ( $line, @fields );
209
210     while ( $line = <$fh> )
211     {
212         next if $line =~ /^#/;
213     
214         @fields = split /\s+/, $line;
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     Maasha::Biopieces::status_set();
272 }
273
274
275 END
276 {
277     Maasha::Biopieces::status_log();
278 }
279
280
281 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
282
283
284 __END__