BEGIN{
    $ENV{'PATH'}=$ENV{'PATH'}.":/local/workspace01/zhangju/script/perl";
    push(@INC,"/local/workspace01/zhangju/script/perl");
}

use Bio::SeqIO;
use Bio::Seq;
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Date;
use Bio::DB::GenBank;

sub genbank_get{
	
	open FILE, "organism_name.txt" or die $!;
	my $count=0;
	my %taxon_h=();
	while(<FILE>){
		if($count>0){
			my @taxonomy = split('\t',$_);
			$taxon_h{$taxonomy[0]}=$taxonomy[1];
		}
		$count++;
	}
	my @effectors = ('chitinase','lysozyme','prophenoloxidase','nitric oxide synthase',
	   'glutathione S-transferase','heat shock','tep','totA');
	
	my $gb = new Bio::DB::GenBank;
  # get a stream via a query string

	while( my ($short,$organism)=each(%taxon_h)){
		my $total_file = $short.'_Genbank.fasta';
		open(TOTALFILE, ">".$total_file);
		foreach (@effectors){
			my $query = Bio::DB::Query::GenBank->new
			    (-query   =>$organism.'[Organism] AND '.$_,
			 	 -db      => 'protein');
			my $seqio = $gb->get_Stream_by_query($query);
			while( my $seq =  $seqio->next_seq ) {
	    		print TOTALFILE ">".$seq->id."\t".$seq->desc."\n".$seq->seq."\n";
			}				
		}
 	    close TOTALFILE;
	}
}  

sub uniprot_get{


	my $taxon = 7227; # $ARGV[0]; # Taxonomy identifier of organism.
#	my $query = "http://www.uniprot.org/uniprot/?query=organism:$taxon+AND+chitinase&format=fasta&include=yes";
	open FILE, "taxonomy.txt" or die $!;
	my $count=0;
	my %taxon_h=();
	while(<FILE>){
		if($count>0){
			my @taxonomy = split('\t',$_);
			$taxon_h{$taxonomy[1]}=$taxonomy[2];
		}
		$count++;
	}
	my @effectors = ('chitinase','lysozyme','prophenoloxidase','nitric oxide synthase',
	   'glutathione S-transferase','heat shock','tep','totA');
	
	foreach (@effectors){
		my $effector=$_;
	   	my $total_file = $effector.'_UniProt_Level1or2.fasta';
		open(TOTALFILE, ">".$total_file);
		while( my ($organism,$taxon)=each(%taxon_h)){
			my $file = $organism.'.fasta';
			my $evidence1="evidence+at+protein+level";
			my $evidence2="evidence+at+transcript+level";
			my $query = "http://www.uniprot.org/uniprot/?query=organism:".$taxon."+AND+name:".$effector."+AND+((existence:$evidence1) + OR + (existence:$evidence2))"."&format=fasta&include=yes";
			my $contact = 'dlyzxl@yahoo.com'; # Please set your email address here to help us debug in case of problems.
			my $agent = LWP::UserAgent->new(agent => "libwww-perl $contact");
			my $response = $agent->mirror($query, $file);
			if ($response->is_success) {
			  my $results = $response->header('X-Total-Results');
			  my $release = $response->header('X-UniProt-Release');
			  my $date = sprintf("%4d-%02d-%02d", HTTP::Date::parse_date($response->header('Last-Modified')));
			  print "Downloaded $results entries of UniProt release $release ($date) to file $file\n";
			  open TEMPFILE, "<".$file;
			  my $query_result = do { local $/; <TEMPFILE> };
			  close TEMPFILE;
			  print TOTALFILE $query_result;
			}
			elsif ($response->code == HTTP::Status::RC_NOT_MODIFIED) {
			  print "Data for taxon $taxon is up-to-date.\n";
			}
			else {
			  die 'Failed, got ' . $response->status_line .
			    ' for ' . $response->request->uri . "\n";
			}	
			unlink $file;
		}
		close TOTALFILE;
	}   
	exit;
	foreach (@effectors){
		my $effector=$_;
	   	my $total_file = $effector.'_UniProt_effector.fasta';
		open(TOTALFILE, ">".$total_file);
		while( my ($organism,$taxon)=each(%taxon_h)){
			my $file = $organism.'.fasta';
			my $query = "http://www.uniprot.org/uniprot/?query=organism:".$taxon."+AND+name:".$effector."&format=fasta&include=yes";
			my $contact = 'dlyzxl@yahoo.com'; # Please set your email address here to help us debug in case of problems.
			my $agent = LWP::UserAgent->new(agent => "libwww-perl $contact");
			my $response = $agent->mirror($query, $file);
			if ($response->is_success) {
			  my $results = $response->header('X-Total-Results');
			  my $release = $response->header('X-UniProt-Release');
			  my $date = sprintf("%4d-%02d-%02d", HTTP::Date::parse_date($response->header('Last-Modified')));
			  print "Downloaded $results entries of UniProt release $release ($date) to file $file\n";
			  open TEMPFILE, "<".$file;
			  my $query_result = do { local $/; <TEMPFILE> };
			  close TEMPFILE;
			  print TOTALFILE $query_result;
			}
			elsif ($response->code == HTTP::Status::RC_NOT_MODIFIED) {
			  print "Data for taxon $taxon is up-to-date.\n";
			}
			else {
			  die 'Failed, got ' . $response->status_line .
			    ' for ' . $response->request->uri . "\n";
			}	
			unlink $file;
		}
		close TOTALFILE;
	}
	
	while( my ($organism,$taxon)=each(%taxon_h)){
		my $total_file = $organism.'_UniProt_geneName.fasta';
		open(TOTALFILE, ">".$total_file);
		foreach (@effectors){
			my $effector=$_;
			my $file = $effector.'.fasta';
			my $query = "http://www.uniprot.org/uniprot/?query=organism:".$taxon."+AND+name:".$effector."&format=fasta&include=yes";
			my $contact = 'dlyzxl@yahoo.com'; # Please set your email address here to help us debug in case of problems.
			my $agent = LWP::UserAgent->new(agent => "libwww-perl $contact");
			my $response = $agent->mirror($query, $file);
			if ($response->is_success) {
			  my $results = $response->header('X-Total-Results');
			  my $release = $response->header('X-UniProt-Release');
			  my $date = sprintf("%4d-%02d-%02d", HTTP::Date::parse_date($response->header('Last-Modified')));
			  print "Downloaded $results entries of UniProt release $release ($date) to file $file\n";
			  open TEMPFILE, "<".$file;
			  my $query_result = do { local $/; <TEMPFILE> };
			  close TEMPFILE;
			  print TOTALFILE $query_result;
			}
			elsif ($response->code == HTTP::Status::RC_NOT_MODIFIED) {
			  print "Data for taxon $taxon is up-to-date.\n";
			}
			else {
			  die 'Failed, got ' . $response->status_line .
			    ' for ' . $response->request->uri . "\n";
			}	
			unlink $file;
		}
		close TOTALFILE;
	}
}

sub uniprot_get_test{

	my $taxon = 7227;
	my $query = "http://www.uniprot.org/uniprot/?query=organism:$taxon+AND+chitinase&format=fasta&include=yes";
	my $file = $taxon . '.fasta';
	
	my $contact = ''; # Please set your email address here to help us debug in case of problems.
	my $agent = LWP::UserAgent->new(agent => "libwww-perl $contact");
	my $response = $agent->mirror($query, $file);
	
	if ($response->is_success) {
	  my $results = $response->header('X-Total-Results');
	  my $release = $response->header('X-UniProt-Release');
	  my $date = sprintf("%4d-%02d-%02d", HTTP::Date::parse_date($response->header('Last-Modified')));
	  print "Downloaded $results entries of UniProt release $release ($date) to file $file\n";
	}
	elsif ($response->code == HTTP::Status::RC_NOT_MODIFIED) {
	  print "Data for taxon $taxon is up-to-date.\n";
	}
	else {
	  die 'Failed, got ' . $response->status_line .
	    ' for ' . $response->request->uri . "\n";
	}


}

#&genbank_get();
&uniprot_get();