Bio::Tools::Run RemoteBlast
SummaryIncluded librariesPackage variablesSynopsisDescriptionGeneral documentationMethods
Toolbar
WebCvs
Summary
Bio::Tools::Run::RemoteBlast - Object for remote execution of the NCBI Blast
via HTTP
Package variables
No package variables defined.
Included modules
Bio::SearchIO
Bio::SeqIO
HTTP::Request::Common
IO::String
LWP
Inherit
Bio::Root::IO Exporter
Synopsis
  #Remote-blast "factory object" creation and blast-parameter initialization
use Bio::Tools::Run::RemoteBlast; use strict; my $prog = 'blastp'; my $db = 'swissprot'; my $e_val= '1e-10'; my @params = ( '-prog' => $prog, '-data' => $db, '-expect' => $e_val, '-readmethod' => 'SearchIO' ); my $factory = Bio::Tools::Run::RemoteBlast->new(@params); #change a query parameter $Bio::Tools::Run::RemoteBlast::HEADER{'ENTREZ_QUERY'} = 'Homo sapiens [ORGN]'; #change a retrieval parameter $Bio::Tools::Run::RemoteBlast::RETRIEVALHEADER{'DESCRIPTIONS'} = 1000; #remove a parameter delete $Bio::Tools::Run::RemoteBlast::HEADER{'FILTER'}; #$v is just to turn on and off the messages my $v = 1; my $str = Bio::SeqIO->new(-file=>'amino.fa' , -format => 'fasta' ); while (my $input = $str->next_seq()){ #Blast a sequence against a database: #Alternatively, you could pass in a file with many #sequences rather than loop through sequence one at a time #Remove the loop starting 'while (my $input = $str->next_seq())' #and swap the two lines below for an example of that. my $r = $factory->submit_blast($input); #my $r = $factory->submit_blast('amino.fa'); print STDERR "waiting..." if( $v > 0 ); while ( my @rids = $factory->each_rid ) { foreach my $rid ( @rids ) { my $rc = $factory->retrieve_blast($rid); if( !ref($rc) ) { if( $rc < 0 ) { $factory->remove_rid($rid); } print STDERR "." if ( $v > 0 ); sleep 5; } else { my $result = $rc->next_result(); #save the output my $filename = $result->query_name()."\.out"; $factory->save_output($filename); $factory->remove_rid($rid); print "\nQuery Name: ", $result->query_name(), "\n"; while ( my $hit = $result->next_hit ) { next unless ( $v > 0); print "\thit name is ", $hit->name, "\n"; while( my $hsp = $hit->next_hsp ) { print "\t\tscore is ", $hsp->score, "\n"; } } } } } } # This example shows how to change a CGI parameter: $Bio::Tools::Run::RemoteBlast::HEADER{'MATRIX_NAME'} = 'BLOSUM45'; $Bio::Tools::Run::RemoteBlast::HEADER{'GAPCOSTS'} = '15 2'; # And this is how to delete a CGI parameter: delete $Bio::Tools::Run::RemoteBlast::HEADER{'FILTER'};
Description
Class for remote execution of the NCBI Blast via HTTP.
For a description of the many CGI parameters see:
http://www.ncbi.nlm.nih.gov/BLAST/Doc/urlapi.html
Various additional options and input formats are available.
Methods
new
No description
Code
retrieve_parameterDescriptionCode
submit_parameterDescriptionCode
headerDescriptionCode
readmethodDescriptionCode
programDescriptionCode
databaseDescriptionCode
expectDescriptionCode
uaDescriptionCode
proxyDescriptionCode
add_rid
No description
Code
remove_rid
No description
Code
each_rid
No description
Code
submit_blastDescriptionCode
retrieve_blastDescriptionCode
save_outputDescriptionCode
_load_input
No description
Code
set_url_baseDescriptionCode
get_url_baseDescriptionCode
get_rtoeDescriptionCode
Methods description
retrieve_parametercode    nextTop
 Title   : retrieve_parameter
Usage : my $db = $self->retrieve_parameter
Function: Get/Set the named parameter for the retrieve_blast operation.
Returns : string
Args : $name : name of GET parameter
$val : optional value to set the parameter to
submit_parametercodeprevnextTop
 Title   : submit_parameter
Usage : my $db = $self->submit_parameter
Function: Get/Set the named parameter for the submit_blast operation.
Returns : string
Args : $name : name of PUT parameter
$val : optional value to set the parameter to
headercodeprevnextTop
 Title   : header
Usage : my $header = $self->header
Function: Get HTTP header for blast query
Returns : string
Args : none
readmethodcodeprevnextTop
 Title   : readmethod
Usage : my $readmethod = $self->readmethod
Function: Get/Set the method to read the blast report
Returns : string
Args : string [ blast, blasttable, xml ]
programcodeprevnextTop
 Title   : program
Usage : my $prog = $self->program
Function: Get/Set the program to run. Retained for backwards-compatibility.
Returns : string
Args : string [ blastp, blastn, blastx, tblastn, tblastx ]
databasecodeprevnextTop
 Title   : database
Usage : my $db = $self->database
Function: Get/Set the database to search. Retained for backwards-compatibility.
Returns : string
Args : string [ swissprot, nr, nt, etc... ]
expectcodeprevnextTop
 Title   : expect
Usage : my $expect = $self->expect
Function: Get/Set the E value cutoff. Retained for backwards-compatibility.
Returns : string
Args : string [ '1e-4' ]
uacodeprevnextTop
 Title   : ua
Usage : my $ua = $self->ua or
$self->ua($ua)
Function: Get/Set a LWP::UserAgent for use
Returns : reference to LWP::UserAgent Object
Args : none
Comments: Will create a UserAgent if none has been requested before.
proxycodeprevnextTop
 Title   : proxy
Usage : $httpproxy = $db->proxy('http') or
$db->proxy(['http','ftp'], 'http://myproxy' )
Function: Get/Set a proxy for use of proxy
Returns : a string indicating the proxy
Args : $protocol : an array ref of the protocol(s) to set/get
$proxyurl : url of the proxy to use for the specified protocol
submit_blastcodeprevnextTop
 Title   : submit_blast
Usage : $self->submit_blast([$seq1,$seq2]);
Function: Submit blast jobs to ncbi blast queue on sequence(s)
Returns : Blast report object as defined by $self->readmethod
Args : input can be:
* sequence object
* array ref of sequence objects
* filename of file containing fasta formatted sequences
retrieve_blastcodeprevnextTop
 Title   : retrieve_blast
Usage : my $blastreport = $blastfactory->retrieve_blast($rid);
Function: Attempts to retrieve a blast report from remote blast queue
Returns : scalar int (constant) or Bio::SearchIO object
Constants:
NOT_FINISHED (= 0) : 'job not finished'
code on error:
ERR_QBSTATUS (= 1) : return line matches 'Status=ERROR'
ERR_NOCONTENT (= 2): HTTP request successful, but no content
returned
ERR_HTTPFAIL (= 4) : HTTP request failed
ERR_QBNONSPEC (= 8): return line matches 'ERROR' (not status line)
Args : Remote Blast ID (RID)
save_outputcodeprevnextTop
 Title   : saveoutput
Usage : my $saveoutput = $self->save_output($filename)
Function: Method to save the blast report
Returns : 1 (throws error otherwise)
Args : string [rid, filename]
set_url_basecodeprevnextTop
 Title   : set_url_base
Usage : $self->set_url_base($url)
Function: Method to override the default NCBI BLAST database
Returns : None
Args : string (database url like
NOTE : This is highly experimental; we cannot maintain support on
databases other than the default NCBI database at this time
get_url_basecodeprevnextTop
 Title   : get_url_base
Usage : my $url = $self->set_url_base
Function: Get the current URL for BLAST database searching
Returns : string (URL used for remote blast searches)
Args : None
get_rtoecodeprevnextTop
 Title   : get_rtoe
Usage : my $url = $self->rtoe
Function: Retrieve the retrieval time (defined after submit_blast())
Returns : number
Args : None
Methods code
newdescriptionprevnextTop
sub new {
        my ($caller, @args) = @_;
        # chained new
my $self = $caller->SUPER::new(@args); # so that tempfiles are cleaned up
$self->_initialize_io(); my ($prog, $data, $readmethod, $url_base) = $self->_rearrange([qw(PROG DATA READMETHOD URL_BASE)], @args); # Use these two parameters for backward-compatibility.
# Overridden by PROGRAM and DATABASE if supplied.
$self->submit_parameter('PROGRAM',$prog) if $prog; $self->submit_parameter('DATABASE',$data) if $data; $readmethod = 'SearchIO' unless defined $readmethod; $self->readmethod($readmethod); # Now read the rest of the parameters and set them all
# PUT parameters first
my @putValues = $self->_rearrange([keys %PUTPARAMS],@args); my %putNames; @putNames{keys %PUTPARAMS} = @putValues; foreach my $putName (keys %putNames) { $self->submit_parameter($putName,$putNames{$putName}); } # GET parameters second
my @getValues = $self->_rearrange([keys %GETPARAMS],@args); my %getNames; @getNames{keys %GETPARAMS} = @getValues; foreach my $getName (keys %getNames) { $self->retrieve_parameter($getName,$getNames{$getName}); } # private variable to keep track of total rids
$self->{'_total_rids'} = 0; $url_base ||= $URLBASE; # default to regular NCBI BLAST URL
$self->set_url_base($url_base); return $self;
}
retrieve_parameterdescriptionprevnextTop
sub retrieve_parameter {
        my ($self, $name, $val) = @_;
        $name = uc($name);
        $self->throw($name." is not a valid GET parameter.") unless
          exists $GETPARAMS{$name};
        if (defined $val) {
        my $regex = $GETPARAMS{$name};
        $val =~ m/^$regex$/i or 
$self->throw("Value ".$val." for GET parameter ".$name." does not match expression ".$regex.". Rejecting.");
$RETRIEVALHEADER{$name} = $val; } return $RETRIEVALHEADER{$name};
}
submit_parameterdescriptionprevnextTop
sub submit_parameter {
    my ($self, $name, $val) = @_;
    $name = uc($name);
    $self->throw($name." is not a valid PUT parameter.") unless
        exists $PUTPARAMS{$name};
    if (defined $val) {
        my $regex = $PUTPARAMS{$name};
        $val =~ m/^$regex$/i or 
$self->throw("Value ".$val." for PUT parameter ".$name." does not match expression ".$regex.". Rejecting.");
$HEADER{$name} = $val; } return $HEADER{$name};
}
headerdescriptionprevnextTop
sub header {
    my ($self) = @_;
    return %HEADER;
}
readmethoddescriptionprevnextTop
sub readmethod {
    my ($self, $val) = @_;
    if( defined $val ) {
        if ($val =~ /bplite/i) {
            $self->throw("Use of Bio::Tools::BPlite is deprecated; use Bio::SearchIO modules instead");
        }
        $self->{'_readmethod'} = $val;
    }
    return $self->{'_readmethod'};
}
programdescriptionprevnextTop
sub program {
    my ($self, $val) = @_;
    return $self->submit_parameter('PROGRAM',$val);
}
databasedescriptionprevnextTop
sub database {
    my ($self, $val) = @_;
    return $self->submit_parameter('DATABASE',$val);
}
expectdescriptionprevnextTop
sub expect {
    my ($self, $val) = @_;
    return $self->submit_parameter('EXPECT',$val);
}
uadescriptionprevnextTop
sub ua {
    my ($self, $value) = @_;    
    if( ! defined $self->{'_ua'} ) {
        $self->{'_ua'} = LWP::UserAgent->new(env_proxy => 1, parse_head => 0);
        my $nm = ref($self);
        $nm =~ s/::/_/g;
        $self->{'_ua'}->agent("bioperl-$nm/$MODVERSION");
    }
    return $self->{'_ua'};
}
proxydescriptionprevnextTop
sub proxy {
    my ($self,$protocol,$proxy) = @_;
    return if ( !defined $self->ua || !defined $protocol
                      || !defined $proxy );
    return $self->ua->proxy($protocol,$proxy);
}
add_riddescriptionprevnextTop
sub add_rid {
    my ($self, @vals) = @_;
    foreach ( @vals ) {
        $self->{'_rids'}->{$_} = $self->{'_total_rids'};
        $self->{'_total_rids'}++; 
    }
    return scalar keys %{$self->{'_rids'}};
}
remove_riddescriptionprevnextTop
sub remove_rid {
    my ($self, @vals) = @_;
    foreach ( @vals ) {
        delete $self->{'_rids'}->{$_};
    }
    return scalar keys %{$self->{'_rids'}};
}
each_riddescriptionprevnextTop
sub each_rid {
    my ($self) = @_;
    # sort on key value, a little tricky...
my @sort_rids = sort {$self->{'_rids'}->{$a} <=> $self->{'_rids'}->{$b}} keys %{$self->{'_rids'}}; return @sort_rids;
}
submit_blastdescriptionprevnextTop
sub submit_blast {
    my ($self, $input) = @_;
    my @seqs = $self->_load_input($input);
    my $url_base = $self->get_url_base;
    return 0 unless ( @seqs );
    my $tcount = 0;
    my %header = $self->header;
    $header{$_} ||= $RETRIEVALHEADER{$_} foreach (keys %RETRIEVALHEADER);    
    foreach my $seq ( @seqs ) {
        #If query has a fasta header, the output has the query line.
$header{'QUERY'} = ">".(defined $seq->display_id() ? $seq->display_id() : ""). " ".(defined $seq->desc() ? $seq->desc() : "")."\n".$seq->seq(); my $request = POST $url_base, [%header]; $self->debug($request->as_string) if ( $self->verbose > 1); my $response = $self->ua->request( $request); if( $response->is_success ) { my @subdata = split(/\n/, $response->content ); my $count = 0; foreach ( @subdata ) { if( /^\s+RID\s+=\s+(\S+)/ ) { $count++; #$self->debug("RID: $1\n");
$self->add_rid($1); } elsif (/^\s+RTOE\s+=\s+(.*$)/) { $self->{rtoe} = $1; $count++; } last if $count >= 2; } if( $count == 0 ) { $self->warn("req was ". $request->as_string() . "\n"); $self->warn(join('', @subdata)); } $tcount += $count; } else { # should try and be a little more verbose here
$self->warn("req was ". $request->as_string() . "\n" . $response->error_as_HTML); $tcount = -1; } } return $tcount;
}
retrieve_blastdescriptionprevnextTop
sub retrieve_blast {
    my($self, $rid) = @_;
    my $url_base = $self->get_url_base;
    my %hdr = %RETRIEVALHEADER;
    $hdr{'RID'} = $rid;
    
    my $req = HTTP::Request->new(
            GET => $url_base."?CMD=Get&FORMAT_OBJECT=SearchInfo&RID=$rid",
            );
    #$self->debug("SearchInfo request is " . $req->as_string());
my $response = $self->ua->request($req); if( $response->is_success ) { my $status; if($response->content =~ /Status=(WAITING|ERROR|FAILED|UNKNOWN|READY)/i ) { $status = $1; if( $status eq 'ERROR' ) { $self->warn("Server Error"); return ERR_QBSTATUS; } elsif( $status eq 'FAILED' ) { $self->warn("Request Failed"); return ERR_QBSTATUS; } } else { $self->warn("Error: No status reported\n"); } if ( $status ne 'READY' ) { return 0; } else { my ($fh,$tempfile) = $self->tempfile(); close $fh; my $req = POST $url_base, [%hdr]; $self->debug("retrieve request is " . $req->as_string()); my $response = $self->ua->request($req, $tempfile); my $blastobj; my $mthd = $self->readmethod; $mthd = ($mthd =~ /blasttable/i) ? 'blasttable' : ($mthd =~ /xml/i) ? 'blastxml' : ($mthd =~ /pull/i) ? 'blast_pull' : 'blast'; $blastobj = Bio::SearchIO->new( -file => $tempfile, -format => $mthd); ## store filename in object ##
$self->file($tempfile); return $blastobj; } } else { $self->warn($response->error_as_HTML); return ERR_HTTPFAIL; }
}
save_outputdescriptionprevnextTop
sub save_output {
        my ($self, $filename) = @_;
        if( ! defined $filename ) {
                $self->throw("Can't save blast output.  You must specify a filename to save to.");
        }
        my $blastfile = $self->file;
        #open temp file and output file, have to filter out some HTML
open(my $TMP, $blastfile) or $self->throw("cannot open $blastfile"); open(my $SAVEOUT, ">", $filename) or $self->throw("cannot open $filename"); my $seentop = 0; while(<$TMP>) { next if (/<pre>/); if(/^(?:[T]?BLAST[NPX])\s*.+$/i || /^RPS-BLAST\s*.+$/i || /<\?xml\sversion=/ || /^#\s+(?:[T]?BLAST[NPX])\s*.+$/) { $seentop=1; } next if !$seentop; if( $seentop ) { print $SAVEOUT $_; } } return 1;
}
_load_inputdescriptionprevnextTop
sub _load_input {
        my ($self, $input) = @_;

        if( ! defined $input ) {
                $self->throw("Calling remote blast with no input");
        }
        my @seqs;
        if( ! ref $input ) {
                if( -e $input ) {
                        my $seqio = Bio::SeqIO->new(-format => 'fasta',
                                                                                                -file => $input);
                        while( my $seq = $seqio->next_seq ) {
                                push @seqs, $seq;
                        }
                } else {
                        $self->throw("Input $input was not a valid filename");
                }
        } elsif( ref($input) =~ /ARRAY/i ) {
                foreach ( @$input ) {
                        if( ref($_) && $_->isa('Bio::PrimarySeqI') ) {
                                push @seqs, $_;
                        } else {
                                $self->warn("Trying to add a " . ref($_) .
                                                                " but expected a Bio::PrimarySeqI");
                        }
                }
                if( ! @seqs) {
                        $self->throw("Did not pass in valid input -- no sequence objects found");
                }
        } elsif( $input->isa('Bio::PrimarySeqI') ) {
                push @seqs, $input;
        }
        return @seqs;
}
set_url_basedescriptionprevnextTop
sub set_url_base {
    my $self = shift;
    $self->{'_urlbase'} = shift if @_;
}
get_url_basedescriptionprevnextTop
sub get_url_base {
    my $self = shift;
    return $self->{'_urlbase'};
}
get_rtoedescriptionprevnextTop
sub get_rtoe {
    my $self = shift;
    return $self->{rtoe};
}

1;

__END__
}
General documentation
FEEDBACKTop
Mailing ListsTop
User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to one
of the Bioperl mailing lists. Your participation is much appreciated.
  bioperl-l@bioperl.org                  - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
Support Top
Please direct usage questions or support issues to the mailing list:
bioperl-l@bioperl.org
rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.
Reporting BugsTop
Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution. Bug reports can be submitted via the
web:
  https://redmine.open-bio.org/projects/bioperl/
AUTHOR Top
Please do NOT contact Jason directly about this module. Please post to
the bioperl mailing list (FEEDBACK). If you would like to be the
official maintainer of this module, please volunteer on the list and
we will make it official in this POD.
First written by Jason Stajich, many others have helped keep it running.
APPENDIXTop
The rest of the documentation details each of the object
methods. Internal methods are usually preceded with a _