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 },
58 Maasha::Common::error( qq(both --database and --genome specified) ) if $options->{ "genome" } and $options->{ "database" };
59 Maasha::Common::error( qq(no --database or --genome specified) ) if not $options->{ "genome" } and not $options->{ "database" };
61 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
62 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
64 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
66 $tmp_dir = Maasha::Biopieces::get_tmpdir();
67 $tmp_in = "$tmp_dir/blast_query.seq";
68 $tmp_out = "$tmp_dir/blast.result";
70 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
72 while ( $record = Maasha::Biopieces::get_record( $in ) )
74 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
76 $q_type = Maasha::Seq::seq_guess_type( $record->{ 'SEQ' } ) if not $q_type;
78 Maasha::Fasta::put_entry( $entry, $fh_out );
81 Maasha::Biopieces::put_record( $record, $out );
86 $s_type = guess_database_type( $options->{ 'database' } );
88 $program = $options->{ 'program' } || guess_program( $q_type, $s_type );
90 if ( $options->{ 'filter' } eq 'yes' ) {
91 $options->{ 'filter' } = 'T';
93 $options->{ 'filter' } = 'F';
96 if ( $options->{ 'no_gaps' } ) {
97 $options->{ 'gapped' } = 'F';
99 $options->{ 'gapped' } = 'T';
102 if ( $options->{ 'megablast' } ) {
103 $options->{ 'megablast' } = 'T';
105 $options->{ 'megablast' } = 'F';
108 if ( $options->{ 'verbose' } )
114 "-e $options->{ 'e_val' }",
115 "-a $options->{ 'cpus' }",
116 "-g $options->{ 'gapped' }",
117 "-n $options->{ 'megablast' }",
120 "-d $options->{ 'database' }",
121 "-F $options->{ 'filter' }",
133 "-e $options->{ 'e_val' }",
134 "-a $options->{ 'cpus' }",
137 "-d $options->{ 'database' }",
138 "-F $options->{ 'filter' }",
148 $fh_in = Maasha::Filesys::file_read_open( $tmp_out );
150 while ( $entry = get_tab_entry( $fh_in ) )
152 $record = blast_tab2biopiece( $entry );
154 Maasha::Biopieces::put_record( $record, $out );
161 Maasha::Biopieces::close_stream( $in );
162 Maasha::Biopieces::close_stream( $out );
165 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
168 sub guess_database_type
170 # Martin A. Hansen, May 2009
172 # Guess the type of BLAST database from the database
173 # filename assuming that it is a protein database if
174 # a .phr file exists.
177 if ( -f $options->{ 'database' } . ".phr" ) {
187 # Martin A. Hansen, May 2009
189 # Guess what BLAST program to use based on the
190 # sequence type of the query and subject sequences.
192 my ( $q_type, # query sequence type
193 $s_type, # subject sequence type
200 if ( $q_type ne "PROTEIN" and $s_type ne "PROTEIN" ) {
202 } elsif ( $q_type eq "PROTEIN" and $s_type eq "PROTEIN" ) {
204 } elsif ( $q_type ne "PROTEIN" and $s_type eq "PROTEIN" ) {
206 } elsif ( $q_type eq "PROTEIN" and $s_type ne "PROTEIN" ) {
207 $program = "tblastn";
217 # Martin A. Hansen, May 2009.
219 # Get the next tabular entry from a filehandle to a BLAST file.
221 my ( $fh, # filehandle
226 my ( $line, @fields );
228 while ( $line = <$fh> )
230 next if $line =~ /^#/;
232 @fields = split /\s+/, $line;
234 return wantarray ? @fields : \@fields;
241 sub blast_tab2biopiece
243 # Martin A. Hansen, May 2009.
245 # Get the next BLAST tabular entry and convert it to
246 # a biopiece record that is returned.
248 my ( $entry, # BLAST tabular entry
255 $record{ "REC_TYPE" } = "BLAST";
256 $record{ "Q_ID" } = $entry->[ 0 ];
257 $record{ "S_ID" } = $entry->[ 1 ];
258 $record{ "IDENT" } = $entry->[ 2 ];
259 $record{ "ALIGN_LEN" } = $entry->[ 3 ];
260 $record{ "MISMATCHES" } = $entry->[ 4 ];
261 $record{ "GAPS" } = $entry->[ 5 ];
262 $record{ "Q_BEG" } = $entry->[ 6 ] - 1; # BLAST is 1-based
263 $record{ "Q_END" } = $entry->[ 7 ] - 1; # BLAST is 1-based
264 $record{ "S_BEG" } = $entry->[ 8 ] - 1; # BLAST is 1-based
265 $record{ "S_END" } = $entry->[ 9 ] - 1; # BLAST is 1-based
266 $record{ "E_VAL" } = $entry->[ 10 ];
267 $record{ "BIT_SCORE" } = $entry->[ 11 ];
269 if ( $record{ "S_BEG" } > $record{ "S_END" } )
271 $record{ "STRAND" } = '-';
273 ( $record{ "S_BEG" }, $record{ "S_END" } ) = ( $record{ "S_END" }, $record{ "S_BEG" } );
277 $record{ "STRAND" } = '+';
280 return wantarray ? %record : \%record;
284 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
289 Maasha::Biopieces::status_set();
295 Maasha::Biopieces::status_log();
299 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<