]> git.donarmstrong.com Git - biopieces.git/blobdiff - bp_bin/blast_seq
rewrite of FASTQ internals
[biopieces.git] / bp_bin / blast_seq
index c8c800fbc01100b8cd9e82c26358933e470ca42e..3feaa655d8837aa125d316754eacc54635026178 100755 (executable)
@@ -1,6 +1,315 @@
 #!/usr/bin/env perl
 
+# Copyright (C) 2007-2009 Martin A. Hansen.
+
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+# http://www.gnu.org/copyleft/gpl.html
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+# BLAST sequences in the stream against a specified database or genome.
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
 use warnings;
 use strict;
+use Maasha::Biopieces;
+use Maasha::Common;
+use Maasha::Seq;
+use Maasha::Fasta;
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+my ( $options, $in, $out, $tmp_dir, $tmp_in, $tmp_out, $q_type, $s_type, $record, $entry,
+     $fh_in, $fh_out, $progs_ok, $program );
+
+$progs_ok = 'blastn,blastp,tblastn,blastx,tblastx';
+
+$options = Maasha::Biopieces::parse_options(
+    [
+        { long => 'database',         short => 'd', type => 'file',   mandatory => 'no', default => undef, allowed => undef,     disallowed => undef },
+        { long => 'genome',           short => 'g', type => 'genome', mandatory => 'no', default => undef, allowed => undef,     disallowed => undef },
+        { long => 'program',          short => 'p', type => 'string', mandatory => 'no', default => undef, allowed => $progs_ok, disallowed => undef },
+        { long => 'e_val',            short => 'e', type => 'float',  mandatory => 'no', default => 10,    allowed => undef,     disallowed => undef },
+        { long => 'filter',           short => 'f', type => 'string', mandatory => 'no', default => 'no',  allowed => 'yes,no',  disallowed => undef },
+        { long => 'cpus',             short => 'c', type => 'uint',   mandatory => 'no', default => 1,     allowed => undef,     disallowed => 0 },
+        { long => 'no_gaps',          short => 'G', type => 'flag',   mandatory => 'no', default => undef, allowed => undef,     disallowed => undef },
+        { long => 'megablast',        short => 'm', type => 'flag',   mandatory => 'no', default => undef, allowed => undef,     disallowed => undef },
+        { long => 'extend_threshold', short => 'E', type => 'uint',   mandatory => 'no', default => 0,     allowed => undef,     disallowed => undef },
+        { long => 'word_size',        short => 'W', type => 'uint',   mandatory => 'no', default => 0,     allowed => undef,     disallowed => undef },
+        { long => 'single_hit',       short => 's', type => 'flag',   mandatory => 'no', default => undef, allowed => undef,     disallowed => undef },
+    ]   
+);
+
+Maasha::Common::error( qq(both --database and --genome specified) ) if     $options->{ "genome" } and     $options->{ "database" };
+Maasha::Common::error( qq(no --database or --genome specified) )    if not $options->{ "genome" } and not $options->{ "database" };
+
+$in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
+$out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
+
+$options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
+
+$tmp_dir = Maasha::Biopieces::get_tmpdir();
+$tmp_in  = "$tmp_dir/blast_query.seq";
+$tmp_out = "$tmp_dir/blast.result";
+
+$fh_out = Maasha::Filesys::file_write_open( $tmp_in );
+
+while ( $record = Maasha::Biopieces::get_record( $in ) ) 
+{
+    if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
+    {
+        $q_type = Maasha::Seq::seq_guess_type( $record->{ 'SEQ' } ) if not $q_type;
+
+        Maasha::Fasta::put_entry( $entry, $fh_out );
+    }
+
+    Maasha::Biopieces::put_record( $record, $out );
+}
+
+close $fh_out;
+
+$s_type = guess_database_type( $options->{ 'database' } );
+
+$program = $options->{ 'program' } || guess_program( $q_type, $s_type );
+
+if ( $options->{ 'filter' } eq 'yes' ) {
+     $options->{ 'filter' } = 'T';
+} else {
+     $options->{ 'filter' } = 'F';
+}
+
+if ( $options->{ 'no_gaps' } ) {
+    $options->{ 'gapped' } = 'F';
+} else {
+    $options->{ 'gapped' } = 'T';
+}
+
+if ( $options->{ 'megablast' } ) {
+    $options->{ 'megablast' } = 'T';
+} else {
+    $options->{ 'megablast' } = 'F';
+}
+
+if ( $options->{ 'single_hit' } ) {
+    $options->{ 'single_hit' } = 1
+} else {
+    $options->{ 'single_hit' } = 0
+}
+
+if ( $options->{ 'verbose' } )
+{
+    Maasha::Common::run(
+        "blastall",
+        join( " ",
+            "-p $program",
+            "-e $options->{ 'e_val' }",
+            "-a $options->{ 'cpus' }",
+            "-g $options->{ 'gapped' }",
+            "-n $options->{ 'megablast' }",
+            "-m 8",
+            "-i $tmp_in",
+            "-d $options->{ 'database' }",
+            "-F $options->{ 'filter' }",
+            "-P $options->{ 'single_hit' }",
+            "-W $options->{ 'word_size' }",
+            "-f $options->{ 'extend_threshold' }",
+            "-o $tmp_out",
+        )
+    );
+}
+else
+{
+    Maasha::Common::run(
+        "blastall",
+        join( " ",
+            "-p $program",
+            "-e $options->{ 'e_val' }",
+            "-a $options->{ 'cpus' }",
+            "-m 8",
+            "-i $tmp_in",
+            "-d $options->{ 'database' }",
+            "-F $options->{ 'filter' }",
+            "-P $options->{ 'single_hit' }",
+            "-W $options->{ 'word_size' }",
+            "-f $options->{ 'extend_threshold' }",
+            "-o $tmp_out",
+            "> /dev/null 2>&1"
+        )
+    );
+}
+
+unlink $tmp_in;
+
+$fh_in = Maasha::Filesys::file_read_open( $tmp_out );
+
+while ( $entry = get_tab_entry( $fh_in ) )
+{
+    $record = blast_tab2biopiece( $entry );
+
+    Maasha::Biopieces::put_record( $record, $out );
+}
+
+close $fh_out;
+
+unlink $tmp_out;
+
+Maasha::Biopieces::close_stream( $in );
+Maasha::Biopieces::close_stream( $out );
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+sub guess_database_type
+{
+    # Martin A. Hansen, May 2009
+
+    # Guess the type of BLAST database from the database
+    # filename assuming that it is a protein database if
+    # a .phr file exists.
+
+    # Returns string;
+    if ( -f $options->{ 'database' } . ".phr" or -f $options->{ 'database' } . ".pal" ) {
+        return "PROTEIN";
+    } else {
+        return "NUCLEOTIDE";
+    }
+}
+
+
+sub guess_program
+{
+    # Martin A. Hansen, May 2009
+
+    # Guess what BLAST program to use based on the
+    # sequence type of the query and subject sequences.
+
+    my ( $q_type,   # query sequence type
+         $s_type,   # subject sequence type
+       ) = @_;
+
+    # Returns string.
+
+    my ( $program );
+
+    if ( $q_type      ne "PROTEIN" and $s_type ne "PROTEIN" ) {
+        $program = "blastn";
+    } elsif ( $q_type eq "PROTEIN" and $s_type eq "PROTEIN" ) {
+        $program = "blastp";
+    } elsif ( $q_type ne "PROTEIN" and $s_type eq "PROTEIN" ) {
+        $program = "blastx";
+    } elsif ( $q_type eq "PROTEIN" and $s_type ne "PROTEIN" ) {
+        $program = "tblastn";
+    }
+
+    return $program;
+}
+
+
+
+sub get_tab_entry
+{
+    # Martin A. Hansen, May 2009.
+
+    # Get the next tabular entry from a filehandle to a BLAST file.
+
+    my ( $fh,   # filehandle
+       ) = @_;
+
+    # Returns a list
+
+    my ( $line, @fields );
+
+    while ( $line = <$fh> )
+    {
+        next if $line =~ /^#/;
+    
+        @fields = split /\s+/, $line;
+
+        return wantarray ? @fields : \@fields;
+    }
+
+    return undef;
+}
+
+
+sub blast_tab2biopiece
+{
+    # Martin A. Hansen, May 2009.
+
+    # Get the next BLAST tabular entry and convert it to
+    # a biopiece record that is returned.
+
+    my ( $entry,    # BLAST tabular entry
+       ) = @_;
+
+    # Returns a hashref.
+
+    my ( %record );
+
+    $record{ "REC_TYPE" }   = "BLAST";
+    $record{ "Q_ID" }       = $entry->[ 0 ];
+    $record{ "S_ID" }       = $entry->[ 1 ];
+    $record{ "IDENT" }      = $entry->[ 2 ];
+    $record{ "ALIGN_LEN" }  = $entry->[ 3 ];
+    $record{ "MISMATCHES" } = $entry->[ 4 ];
+    $record{ "GAPS" }       = $entry->[ 5 ];
+    $record{ "Q_BEG" }      = $entry->[ 6 ] - 1; # BLAST is 1-based
+    $record{ "Q_END" }      = $entry->[ 7 ] - 1; # BLAST is 1-based
+    $record{ "S_BEG" }      = $entry->[ 8 ] - 1; # BLAST is 1-based
+    $record{ "S_END" }      = $entry->[ 9 ] - 1; # BLAST is 1-based
+    $record{ "E_VAL" }      = $entry->[ 10 ];
+    $record{ "BIT_SCORE" }  = $entry->[ 11 ];
+
+    if ( $record{ "S_BEG" } > $record{ "S_END" } )
+    {
+        $record{ "STRAND" } = '-';
+
+        ( $record{ "S_BEG" }, $record{ "S_END" } ) = ( $record{ "S_END" }, $record{ "S_BEG" } );
+    }
+    else
+    {
+        $record{ "STRAND" } = '+';
+    }
+
+    return wantarray ? %record : \%record;
+}
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+BEGIN
+{
+    Maasha::Biopieces::status_set();
+}
+
+
+END
+{
+    Maasha::Biopieces::status_log();
+}
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
 
-use Maasha::Biotools;
+__END__