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, $progs_ok, $program );
43 $progs_ok = 'blastn,blastp,tblastn,blastx,tblastx';
45 $options = Maasha::Biopieces::parse_options(
47 { long => 'database', short => 'd', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
48 { long => 'genome', short => 'g', type => 'genome', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
49 { long => 'program', short => 'p', type => 'string', mandatory => 'no', default => undef, allowed => $progs_ok, disallowed => undef },
50 { long => 'e_val', short => 'e', type => 'float', mandatory => 'no', default => 10, allowed => undef, disallowed => undef },
51 { long => 'filter', short => 'f', type => 'string', mandatory => 'no', default => 'no', allowed => 'yes,no', disallowed => undef },
52 { long => 'cpus', short => 'c', type => 'uint', mandatory => 'no', default => 1, allowed => undef, disallowed => 0 },
53 { long => 'no_gaps', short => 'G', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
54 { long => 'megablast', short => 'm', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
55 { long => 'extend_threshold', short => 'E', type => 'uint', mandatory => 'no', default => 0, allowed => undef, disallowed => undef },
56 { long => 'word_size', short => 'W', type => 'uint', mandatory => 'no', default => 0, allowed => undef, disallowed => undef },
57 { long => 'single_hit', short => 's', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
61 Maasha::Common::error( qq(both --database and --genome specified) ) if $options->{ "genome" } and $options->{ "database" };
62 Maasha::Common::error( qq(no --database or --genome specified) ) if not $options->{ "genome" } and not $options->{ "database" };
64 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
65 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
67 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
69 $tmp_dir = Maasha::Biopieces::get_tmpdir();
70 $tmp_in = "$tmp_dir/blast_query.seq";
71 $tmp_out = "$tmp_dir/blast.result";
73 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
75 while ( $record = Maasha::Biopieces::get_record( $in ) )
77 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
79 $q_type = Maasha::Seq::seq_guess_type( $record->{ 'SEQ' } ) if not $q_type;
81 Maasha::Fasta::put_entry( $entry, $fh_out );
84 Maasha::Biopieces::put_record( $record, $out );
89 $s_type = guess_database_type( $options->{ 'database' } );
91 $program = $options->{ 'program' } || guess_program( $q_type, $s_type );
93 if ( $options->{ 'filter' } eq 'yes' ) {
94 $options->{ 'filter' } = 'T';
96 $options->{ 'filter' } = 'F';
99 if ( $options->{ 'no_gaps' } ) {
100 $options->{ 'gapped' } = 'F';
102 $options->{ 'gapped' } = 'T';
105 if ( $options->{ 'megablast' } ) {
106 $options->{ 'megablast' } = 'T';
108 $options->{ 'megablast' } = 'F';
111 if ( $options->{ 'single_hit' } ) {
112 $options->{ 'single_hit' } = 1
114 $options->{ 'single_hit' } = 0
117 if ( $options->{ 'verbose' } )
123 "-e $options->{ 'e_val' }",
124 "-a $options->{ 'cpus' }",
125 "-g $options->{ 'gapped' }",
126 "-n $options->{ 'megablast' }",
129 "-d $options->{ 'database' }",
130 "-F $options->{ 'filter' }",
131 "-P $options->{ 'single_hit' }",
132 "-W $options->{ 'word_size' }",
133 "-f $options->{ 'extend_threshold' }",
145 "-e $options->{ 'e_val' }",
146 "-a $options->{ 'cpus' }",
149 "-d $options->{ 'database' }",
150 "-F $options->{ 'filter' }",
151 "-P $options->{ 'single_hit' }",
152 "-W $options->{ 'word_size' }",
153 "-f $options->{ 'extend_threshold' }",
163 $fh_in = Maasha::Filesys::file_read_open( $tmp_out );
165 while ( $entry = get_tab_entry( $fh_in ) )
167 $record = blast_tab2biopiece( $entry );
169 Maasha::Biopieces::put_record( $record, $out );
176 Maasha::Biopieces::close_stream( $in );
177 Maasha::Biopieces::close_stream( $out );
180 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
183 sub guess_database_type
185 # Martin A. Hansen, May 2009
187 # Guess the type of BLAST database from the database
188 # filename assuming that it is a protein database if
189 # a .phr file exists.
192 if ( -f $options->{ 'database' } . ".phr" or -f $options->{ 'database' } . ".pal" ) {
202 # Martin A. Hansen, May 2009
204 # Guess what BLAST program to use based on the
205 # sequence type of the query and subject sequences.
207 my ( $q_type, # query sequence type
208 $s_type, # subject sequence type
215 if ( $q_type ne "PROTEIN" and $s_type ne "PROTEIN" ) {
217 } elsif ( $q_type eq "PROTEIN" and $s_type eq "PROTEIN" ) {
219 } elsif ( $q_type ne "PROTEIN" and $s_type eq "PROTEIN" ) {
221 } elsif ( $q_type eq "PROTEIN" and $s_type ne "PROTEIN" ) {
222 $program = "tblastn";
232 # Martin A. Hansen, May 2009.
234 # Get the next tabular entry from a filehandle to a BLAST file.
236 my ( $fh, # filehandle
241 my ( $line, @fields );
243 while ( $line = <$fh> )
245 next if $line =~ /^#/;
247 @fields = split /\s+/, $line;
249 return wantarray ? @fields : \@fields;
256 sub blast_tab2biopiece
258 # Martin A. Hansen, May 2009.
260 # Get the next BLAST tabular entry and convert it to
261 # a biopiece record that is returned.
263 my ( $entry, # BLAST tabular entry
270 $record{ "REC_TYPE" } = "BLAST";
271 $record{ "Q_ID" } = $entry->[ 0 ];
272 $record{ "S_ID" } = $entry->[ 1 ];
273 $record{ "IDENT" } = $entry->[ 2 ];
274 $record{ "ALIGN_LEN" } = $entry->[ 3 ];
275 $record{ "MISMATCHES" } = $entry->[ 4 ];
276 $record{ "GAPS" } = $entry->[ 5 ];
277 $record{ "Q_BEG" } = $entry->[ 6 ] - 1; # BLAST is 1-based
278 $record{ "Q_END" } = $entry->[ 7 ] - 1; # BLAST is 1-based
279 $record{ "S_BEG" } = $entry->[ 8 ] - 1; # BLAST is 1-based
280 $record{ "S_END" } = $entry->[ 9 ] - 1; # BLAST is 1-based
281 $record{ "E_VAL" } = $entry->[ 10 ];
282 $record{ "BIT_SCORE" } = $entry->[ 11 ];
284 if ( $record{ "S_BEG" } > $record{ "S_END" } )
286 $record{ "STRAND" } = '-';
288 ( $record{ "S_BEG" }, $record{ "S_END" } ) = ( $record{ "S_END" }, $record{ "S_BEG" } );
292 $record{ "STRAND" } = '+';
295 return wantarray ? %record : \%record;
299 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
304 Maasha::Biopieces::status_set();
310 Maasha::Biopieces::status_log();
314 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<