use strict;
use warnings;
#Bio::SeqIO means a SeqIO.pm file under Bio folder--this is how module is structured
use Bio::SeqIO;
use Data::Dumper; 
use Bio::Seq; 
#now we start to try some modules that is used in BioPerl--install according to instructions from BioPerl.org
#or "use lib "path_to_the_folder_containes_Bio""

#there is no need to understand how those packages works--that's the work for people who wrote it--Biologists only need to use the code
#just copy the code from synposis part of the module and then modify it

#read in something..... this helps a lot when you use Bio::SeqIO since you no longer need to worry about different format (even for fasta.)
my $in  = Bio::SeqIO->new(-file => "example.fasta" , -format => 'Fasta');
print $in, "\n";
#yep---it's a data structure somehow like a reference, in order to see what's in there, use the next line
print Dumper($in), "\n"; 
#not so helpful because still can't get info directly. But don't worry, those data structure comes with functions that get things out from them, we will see soon
my $out = Bio::SeqIO->new(-file => ">outputfilename" ,-format => 'EMBL');
#open a new sequence file with genbank format, remember ">" when you open a new file

#now we read sequence out and do a remote blast. check if you have a RemoteBlast.pm under Run under Tools under Bio.... 

#!!! People, please realize that, internet might be harmful and makes you procrastinate, BUT it's essential to do a remote BLAST

use Bio::Tools::Run::RemoteBlast;
while ( my $seq = $in->next_seq() ) {
#this is how sequence is read
$out->write_seq($seq); 
#this is how sequence is writen

print $seq, "\n"; 
#it's a Bio::Seq thing and we need to use a little bit of functions
print $seq->id, "\n";
print $seq->seq, "\n";
print $seq->description, "\n";  

#now we try a little bit of Blast--copy the monster code from SYNPOSIS of RemoteBlast.pm and don't care much how it really works--expect change database and blast program
#$v is just to turn on and off the messages
  my $v = 1;
my $prog = 'blastn';#nucleotide blast
  my $db   = 'nt'; #database is nucleotide of NCBI
  my $e_val= '1e-10';

  my @params = ( '-prog' => $prog,
         '-data' => $db,
         '-expect' => $e_val,
         '-readmethod' => 'SearchIO' );
#build a virtual machine that does the blast for us--but it's not running before you put sequence there
  my $factory = Bio::Tools::Run::RemoteBlast->new(@params);
#now it really does blast using the sequence you give it
    my $r = $factory->submit_blast($seq);
#when you found the following boring or horrifying--just do something like silencing few lines or just extract useful parameters like $result->query_name(),$hit->name, or $hsp->score. I assume you are familiar with those concepts when doing a blast
 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";
            }
          }
        }
      }
    }



}


#you can check Bio::Tools::Run folder (please find the BioPerl_run_1.6.1....XXX instead of only BioPerl_1.6.1...., the first folder is extention package for connecting different programs and so on) and will find amazingly amount of programs you can actually connect using perl.    
