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