3 # Copyright (C) 2007-2009 Martin A. Hansen.
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.
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.
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.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # BLAST sequences in the stream against a specified database or genome.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 use Maasha::Biopieces;
37 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
40 my ( $options, $in, $out, $tmp_dir, $tmp_in, $tmp_out, $q_type, $s_type, $record, $entry,
41 $fh_in, $fh_out, $program );
43 $options = Maasha::Biopieces::parse_options(
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 },
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" };
57 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
58 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
60 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
62 $tmp_dir = Maasha::Biopieces::get_tmpdir();
63 $tmp_in = "$tmp_dir/blast_query.seq";
64 $tmp_out = "$tmp_dir/blast.result";
66 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
68 while ( $record = Maasha::Biopieces::get_record( $in ) )
70 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
72 $q_type = Maasha::Seq::seq_guess_type( $record->{ 'SEQ' } ) if not $q_type;
74 Maasha::Fasta::put_entry( $entry, $fh_out );
77 Maasha::Biopieces::put_record( $record, $out );
82 $s_type = guess_database_type( $options->{ 'database' } );
84 $program = $options->{ 'program' } || guess_program( $q_type, $s_type );
86 if ( $options->{ 'filter' } eq 'yes' ) {
87 $options->{ 'filter' } = 'T';
89 $options->{ 'filter' } = 'F';
92 if ( $options->{ 'verbose' } )
98 "-e $options->{ 'e_val' }",
99 "-a $options->{ 'cpus' }",
102 "-d $options->{ 'database' }",
103 "-F $options->{ 'filter' }",
115 "-e $options->{ 'e_val' }",
116 "-a $options->{ 'cpus' }",
119 "-d $options->{ 'database' }",
120 "-F $options->{ 'filter' }",
130 $fh_in = Maasha::Filesys::file_read_open( $tmp_out );
132 while ( $entry = get_tab_entry( $fh_in ) )
134 $record = blast_tab2biopiece( $entry );
136 Maasha::Biopieces::put_record( $record, $out );
143 Maasha::Biopieces::close_stream( $in );
144 Maasha::Biopieces::close_stream( $out );
147 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
150 sub guess_database_type
152 # Martin A. Hansen, May 2009
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.
159 if ( -f $options->{ 'database' } . ".phr" ) {
169 # Martin A. Hansen, May 2009
171 # Guess what BLAST program to use based on the
172 # sequence type of the query and subject sequences.
174 my ( $q_type, # query sequence type
175 $s_type, # subject sequence type
182 if ( $q_type ne "protein" and $s_type ne "protein" ) {
184 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
186 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
188 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
189 $program = "tblastn";
199 # Martin A. Hansen, May 2009.
201 # Get the next tabular entry from a filehandle to a BLAST file.
203 my ( $fh, # filehandle
208 my ( $line, @fields );
210 while ( $line = <$fh> )
212 next if $line =~ /^#/;
214 @fields = split /\s+/, $line;
216 return wantarray ? @fields : \@fields;
223 sub blast_tab2biopiece
225 # Martin A. Hansen, May 2009.
227 # Get the next BLAST tabular entry and convert it to
228 # a biopiece record that is returned.
230 my ( $entry, # BLAST tabular entry
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 ];
251 if ( $record{ "S_BEG" } > $record{ "S_END" } )
253 $record{ "STRAND" } = '-';
255 ( $record{ "S_BEG" }, $record{ "S_END" } ) = ( $record{ "S_END" }, $record{ "S_BEG" } );
259 $record{ "STRAND" } = '+';
262 return wantarray ? %record : \%record;
266 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
271 Maasha::Biopieces::status_set();
277 Maasha::Biopieces::status_log();
281 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<