#package Bio::Tools::Run::RemoteBlast;
#package Bio::Search::Result::ResultI;
use Bio::Tools::Run::RemoteBlast;
use strict;
use LWP::Simple;
#use Bio::Search::Result::ResultI;
  use strict;
  my $prog = 'blastp';
  my $db   = 'nr';
  my $e_val= '1e-10';

  my @params = ( '-prog' => $prog,
         '-data' => $db,
         '-expect' => $e_val,
         '-readmethod' => 'SearchIO' );
my $db = 'nucleotide';
my $form = 'fasta';
my $type = 't';
  my $factory = Bio::Tools::Run::RemoteBlast->new(@params);
my @nam= ("te","xx");
my $nl=@nam;
my $vn;
  #change a paramter
#  $Bio::Tools::Run::RemoteBlast::HEADER{'ENTREZ_QUERY'} = 'Homo sapiens [ORGN]';

  #remove a parameter
 # delete $Bio::Tools::Run::RemoteBlast::HEADER{'FILTER'};

  my $v = 1;
  #$v is just to turn on and off the messages
  print F2 "Result;query_start;query_end;match_start;match_end;Queryi;E-val;Identity\n";
$Bio::Tools::Run::RemoteBlast::HEADER{'ENTREZ_QUERY'} = 'Xenopus laevis[Organism]';
for ($vn=0; $vn<$nl; $vn++)
{  my $file="xenopous.".$nam[$vn];
 my @ar;my $na;
 open (F2, ">$file") or die "Can\'t open $file: $!\n";
my $filename=$nam[$vn].".pep";
print "$filename\n";
  my $str = Bio::SeqIO->new(-file=>$filename, '-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 $queryname= $result->query_name();
                                 @ar = split(';',$queryname);$na=$ar[0];
          			my $filename = $na."\.bls";
           			#my $filename="1.out";
                                  print "File $filename\n";
          			$factory->save_output($filename);
          			$factory->remove_rid($rid);
          		      while ( my $hit = $result->next_hit ) 
				{
            			next unless ( $v > 0);
         		    	      #print  $hit->name;print "\n";
           				my $acc=$hit->name;
           				my @pa = split (/\|/,$acc);
                     		print F2 $acc;#my $fg=<STDIN>;
            			while( my $hsp = $hit->next_hsp ) 
					{
             	   			my $qstart=$hsp->start('query');
           					my $qend=$hsp->end('query');
	   					my $hstart=$hsp->start('subject');
           					my $hend=$hsp->end('subject');
						my $hiden=$hsp->frac_identical('query'); #% identity
						my $hexp=$hsp->significance; # evalue id taken out
	   					print  F2 ";$qstart;$qend;$hstart;$hend;$na;$hexp;$hiden\n";
	  					last;
          			last;	}
				last;}
      		}
    }
  }

  # This example shows how to change a CGI parameter:
  $Bio::Tools::Run::RemoteBlast::HEADER{'MATRIX_NAME'} = 'BLOSUM62';
$Bio::Tools::Run::RemoteBlast::HEADER{'ENTREZ_QUERY'} = 'Xenopus laevis[Organism]';
}close F2;


  # And this is how to delete a CGI parameter:
  delete $Bio::Tools::Run::RemoteBlast::HEADER{'FILTER'};
}
