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