
use SOAP::Lite;
use LWP::Simple;
use LWP::UserAgent;


sub SOAP::Serializer::as_ArrayOfstring{
  my ($self, $value, $name, $type, $attr) = @_;
  return [$name, {'xsi:type' => 'array', %$attr}, $value];
}

sub SOAP::Serializer::as_ArrayOfint{
  my ($self, $value, $name, $type, $attr) = @_;
  return [$name, {'xsi:type' => 'array', %$attr}, $value];
}

sub color_code{
	
	@objs=[];
	@fgs=[];
	@bgs=[];
	open EXPF,"<",$expression_file;
	foreach(<EXPF>){
		chomp $_;
		@arr=split(/\t/,$_);
		print $arr[0]."\t".$arr[1]."\t",$arr[2]."\n";
		push @objs, "cth:".$arr[0];
		push @fgs, $black;
		if(int($arr[1])*int($arr[2])==-1){
			push @bgs, $red;
		}elsif(int($arr[1])==1 && int($arr[2])==0){
			push @bgs, $lime;
		}elsif(int($arr[1])==0 && int($arr[2])==1){
			push @bgs, $green;
		}elsif(int($arr[1])==1 && int($arr[2])==1){
			push @bgs, $olive;
		}elsif(int($arr[1])==-1 && int($arr[2])==0){
			push @bgs, $orange;
		}elsif(int($arr[1])==0 && int($arr[2])==-1){
			push @bgs, $purple;
		}elsif(int($arr[1])==-1 && int($arr[2])==-1){
			push @bgs, $brown;
		}else{
			push @bgs, "#FFFFFF";
		}
	}
	@obj_list=@objs;
	@fg_list=@fgs;
	@bg_list=@bgs;
	
}


sub color_each_path{
	
	$pathway=shift;
	$id_file=$element_path.$pathway."_element.txt";
	open IDF, "<", $id_file;
	%id_path=();
	foreach(<IDF>){
		chomp $_;
		@lines=split(/\t/,$_);
		$id_path{$lines[0]}=$lines[1];
	}
	@obj_path=[];
	@bg_path=[];
	@fg_path=[];
	$index=1;
	foreach(@obj_list){
		if(exists $id_path{$_}){
			push @obj_path, $_;
			push @bg_path,$bg_list[$index];
			push @fg_path,$fg_list[$index];
			$index++;
		}
	}
	$num=@obj_path;
	if($num>1){
		print "color path - ".$pathway;
		push @color_paths, $pathway;
	}
	@obj=[@obj_path[1..$num-1]];
	@fg=[@fg_path[1..$num-1]];
	@bg=[@bg_path[1..$num-1]];
	$url=$serv->color_pathway_by_objects("path:".$pathway,@obj,@fg,@bg);
	@args=("wget",$url);
	system(@args)==0 or die "system @args failed: $?";
	if($num>1){
		return 1;
	}else{
		return 0;
	}
}

sub color_paths{
	
	unless (-d $path_map){
		mkdir $path_map;
	}
	chdir $path_map;
	open PF, "<", $path_file;
	open CF, ">", $path."path_color.txt";
	foreach(<PF>){
		chomp $_;
		@arr=split(/\s\s\s+/,$_);
		$color=0;
		$color=color_each_path($arr[0]);
		print CF $arr[0]."\t".$arr[1]."\t".$color."\n";
	}
	close(PF);
	close(CF);
	chdir $path;
}

sub source_each_path{
	$each_path=shift;
	$url="http://www.kegg.jp/kegg-bin/show_pathway?".$each_path;
	$content = get($url) or die 'Unable to get page';
	open CF, ">", $path_source.$each_path.".source";
	if(defined $content){
		print CF $content;
	}
	close(CF);
}

sub source_paths{
	unless(-d $path_source){
		mkdir $path_source;
	}
	chdir $path_source;
	open PF, "<", $path_file;
	foreach(<PF>){
		chomp $_;
		@arr=split(/\s\s\s+/,$_);
		source_each_path($arr[0]);
	}
	close(PF);
	chdir $path;
}

sub html_each_path{
	
	$pathway=shift;
	$pathname=shift; #cth0001	ec1.2.2.2
	@arr_pathname=split(/cth/,$pathway);
	$num_path=$arr_pathname[1];
	$img = $pathway.".png";
	$source_file=$path_source.$pathway.".source";
	$html_file=$path_map."/".$pathway.".html";
	open HTML, ">", $html_file or die $!;
	print HTML "<html>\n";
	print HTML "<head>\n<title>".$pathname."</title>\n</head>\n<body>\n";
	print HTML qq(<img src=$img usemap="#mapdata" border="0" />\n);
	print HTML qq(<map name="mapdata">\n);
	open SF,"<",$source_file;
	$pre_url="http://www.genome.jp";
	$pre_ko1="https://img.jgi.doe.gov/cgi-bin/er/main.cgi?section=KeggMap&page=keggMapTaxonGenesKo&taxon_oid=640069309&map_id=map";
#	$pre_ko="https://img.jgi.doe.gov/cgi-bin/er/main.cgi?section=KeggMap&page=keggMapTaxonGenesKo&taxon_oid=640069309&map_id=map".$num_path."&ko=";

	while (<SF>){
		$link=$_;
		chomp $link;
		$line=$_;
#www.genome.jp/kegg-bin/get_htext?br:cth01003
#<area shape=rect        coords=86,71,157,79     
#href="javascript:void(window.open(&quot;/dbget-bin/www_bget?br:cth01003&quot;,
#&quot;_blank&quot;,&quot;width=750,height=640,resizable=yes,scrollbars=yes,
#status=yes,toolbar=yes,location=yes&quot;))"   title="cth01003" />
		
		if(($link =~ m/shape=rect/) && ($link =~ m/javascript:void/) && ($link =~ m/cth\d{5}/) && ($link =~ m/www_bget\?br:/)){
			my $pre_get="href=\"www.genome.jp/kegg-bin/get_htext?br:";
			my @arr4=split(/\t/,$link);
			@tag = ($link =~ /.+\:(cth\d{5})&.+/);
			$arr4[2]=$pre_get.$tag[0]."\"";
			print HTML join("\t",@arr4)."\n";
			
		}elsif(($link =~ m/shape=rect/) && ($link =~ m/javascript:void/) && ($link =~ m/cth\d{5}/)){
			my @arr4=split(/\t/,$link);
			@tag = ($link =~ /.+\?(cth\d{5})&.+/);
			$arr4[2]="href=\"".$tag[0].".html\"";
			print HTML join("\t",@arr4)."\n";
			
		}elsif(($link =~ m/title="Cthe_/) && ($link =~ m/shape=rect/)){
			$str_ko=$pre_ko1.$num_path."&ko=";
			$line=$_;
			$link =~ /.+title="(Cthe_.+)".+/;
			@arr2=split(/, /,$1);
			if ($arr2[0] eq "Cthe_2390"){
				print "pause\n";
			}
			%ko_s=();
			foreach my $each_gene (@arr2){
				@enzyme_s=$gene_enzyme{$each_gene};
				$n1=0;
				while($enzyme_s[0][$n1] ne ""){
					print "enzyme - ".$enzyme3."\n";
					$enzyme3=$enzyme_s[0][0];
					$enzyme3 =~ s/^\s+//;
					$enzyme3 =~ s/\s+$//;
					@arr_ko=$enzyme_ko{$enzyme3};
					$n2=0;
					while ($arr_ko[0][$n2] ne ""){
						$ko=$arr_ko[0][$n2];
						$ko =~ s/^\s+//;
						$ko =~ s/\s+$//;
						print "ko - ".$ko."\n";
						@kos=split(/:/,$ko);
						print $ko."\n";
						unless($ko_s{$kos[1]}){
								$ko_s{$kos[1]}=1;
						}
						$n2++;
					}
					$n1++;
				}			
				
			}
			$ko_c=1;
			for my $key (keys %ko_s){
				if($key eq "ko"){
					next;
				}
				if ($ko_c > 1){
					$str_ko=$str_ko.",".$key;
				}else{
					$str_ko=$str_ko.$key;
				}
	     		$ko_c=$ko_c+1;				
			}
			#$str_ko=$str_ko;
			@arr3=split(/\t/,$line);
			$arr3[2]="href=\"".$str_ko."\"";
			
			$line =~ s/\/dbget-bin\/www_bget\?cth:Cthe_.+"        title/$str_ko        title/;
			#$line =~ s/http:\/\/www.genome.jp\/dbget-bin\/www_bget\?cth:Cthe_.+"/$str_ko/;
			print $line;
			print HTML join("\t",@arr3);#"\t".$line;
			print HTML "\n";
		}elsif($link =~ m/\/kegg-bin\/show_pathway\?cth/g){
			@arr=split(/"/, $_);
			@arr1=split(/\?/,$arr[1]);
			$html=$arr1[1].".html";
			$arr[1]=$html;
			$line=join('"',@arr)."\n";
			print HTML "\t".$line;
		}elsif($_ =~ m/shape=/)	{
			$_ =~ s/\/dbget-bin/$pre_url\/dbget-bin/;
			$_ =~ s/onmouseout=.+/\/>/;
			$_ =~ s/\/kegg-bin\/show_pathway/$pre_url\/kegg-bin\/show_pathway/;
			print HTML "\t".$_;
		}	
	}
	print HTML "\t</map>\n";
	print HTML "</body>\n</html>\n";
	close(HTML);
	
#<area shape=rect        coords=375,547,410,561  href="cth00680&quot;,&quot;_blank&quot;,&quot;width=750,height=640,resizable=yes,scrollbars=yes,status=yes,toolbar=yes,location=yes&quot;)).html"       title="cth00680: Methane metabolism" onmouseover="popupTimer(&quot;cth00680&quot;, &quot;cth00680: Methane metabolism&quot;, &quot;#9933CC&quot;)" onmouseout="hideMapTn()" />
#<area shape=rect        coords=375,547,410,561  href="javascript:void(window.open(&quot;/kegg-bin/show_pathway?cth00680&quot;,&quot;_blank&quot;,&quot;width=750,height=640,resizable=yes,scrollbars=yes,status=yes,toolbar=yes,location=yes&quot;))"   title="cth00680: Methane metabolism" onmouseover="popupTimer(&quot;cth00680&quot;, &quot;cth00680: Methane metabolism&quot;, &quot;#9933CC&quot;)" onmouseout="hideMapTn()" />	
#<area shape=rect        coords=460,620,506,637  href="/dbget-bin/www_bget?cth:Cthe_0140+cth:Cthe_0707+cth:Cthe_0946+cth:Cthe_1292+cth:Cthe_1435+cth:Cthe_2449"  title="Cthe_0140, Cthe_0707, Cthe_0946, Cthe_1292, Cthe_1435, Cthe_2449" />
#https://img.jgi.doe.gov/cgi-bin/er/main.cgi?section=KeggMap&page=keggMapTaxonGenesKo&taxon_oid=640069309&map_id=map00010&ko=K00169,K00170,K00171,K00172
#		<area shape=rect	coords=203,303,287,337	href="http://www.genome.jp/kegg-bin/show_pathway?map00310"	title="map00310: Lysine degradation" onmouseover="popupTimer(&quot;map00310&quot;, &quot;map00310: Lysine degradation&quot;, &quot;#ffffff&quot;)" />
#    <a href="http://www.genome.jp/kegg-bin/show_pathway?map00310"><img src="kegg_icon.gif" style="position:absolute; left:288px; top:304px" alt="map00310" width="16" height="16" /></a>
#</map>
}

sub html_paths{
	
	
	open PF, "<", $path."path_color.txt";
	chdir $path_map;
	open INDF, ">", $path."path_map/index.html";
	print INDF "<html>";
	print INDF "<body>";
	print INDF "<h1>KEGG Colored Pathways - Thermocellum ATCC 27405</h1><br/><br/>\n";
	print INDF "<table border=\"1\">\n<tr>\n";
	for(keys %color_code){
		print INDF "<td><font color=".$color_code{$_}.">".$_."</font></td>\n";
	}
	print INDF "</tr>\n<tr>\n";
	for(keys %color_code){
		print INDF "<td><font color=".$color_code{$_}.">".$color_name{$color_code{$_}}."</font></td>\n";
	}
	print INDF "</tr>\n</table>\n";
	print INDF "<pre><h2>No.     Name</h2></pre><br/>\n";
	$c=1;
	open GeneEF, "<", $gene_enzyme_file;
	while(<GeneEF>){
		chomp $_;
		@arr1=split(/\t/,$_);
		print join('\t',@arr1)."\n";
		$num1=@arr1;
		@arr2=split(/:/,$arr1[0]);
		$gene_enzyme{$arr2[1]}=[@arr1[1..$num1-1]];
	}

	open EKOF, "<", $ec_ko_file;
	while(<EKOF>){
		chomp $_;
		@arr1=split(/\t/,$_);
		if($arr1[0] eq "ec:1.2.7.1"){
			print "pause enzyme\n";
		}
		$num1=@arr1;
		$enzyme_ko{$arr1[0]}=[@arr1[1..$num1-1]];
	}
	foreach(<PF>){
		chomp $_;
		@arr=split(/\t/,$_);
		@arr4=split(/\t/,$_);
		if ($arr[0] eq "cth00110"){
			print "pause";
		}
		html_each_path($arr[0],$arr[1]);
		
		$pre="".$c."     ";
		if($arr4[2] eq "1"){
			print INDF "<pre>".$pre."<a href=".$arr4[0].".html><font color=\"purple\">".$arr4[1]."</font></a></pre><br/>\n";	
		}elsif($arr4[2] eq "2"){
			print INDF "<pre>".$pre."<a href=".$arr4[0].".html><font color=\"orange\">".$arr4[1]."</font></a></pre><br/>\n";	
		}else{
			print INDF "<pre>".$pre."<a href=".$arr4[0].".html>".$arr4[1]."</a></pre><br />\n";			
		}
		$c++;
		
	}
	close(PF);
	close(INDF);
	chdir $path;
}

sub get_pathway{
	$organism=shift; # clostridium thermocellum ATCC 27405
	$int_gene=$serv->get_number_of_genes_by_organism($organism);
	@genes=$serv->get_genes_by_organism($organism,1, $int_gene+2);
	#%enzymes_by_gene=(); # hash of arrays - multiple enzymes by each gene
	@pathways=[];
	shift @pathways;
	$filepath="/local/workspace01/zhangju/kegg_develop/cth_gene_enzyme_pathway.txt";
	open PF, ">", $filepath or die $!;
	for ($i=0;$i<$int_gene;$i++){
		print $i."\n";
		print $genes[0][$i]."\n";
		@enzymes=$serv->get_enzymes_by_gene($genes[0][$i]);
		$j=0;
		while($enzymes[0][$j] ne ""){
			print $enzymes[0][$j];
			@paths=$serv->get_pathways_by_enzymes([$enzymes[0][$j]]);
			$k=0;
			while($paths[0][$k] ne ""){
				$path=$paths[0][$k];
#				$path =~ s/:map/:cth/g;
				if (not (grep {$path eq $_} @pathways)){
					push @pathways, $path;
#					push @maps, $path;
					print PF $path."\n";
					print $path."\n";
				}
				$k++;
			}
			$j++;	
		}

	}
	close(PF);
}

sub get_element_per_path{
	$each_path=shift;
	$element_file=$element_path.$each_path."_element.txt";
	open EF, ">", $element_file;

	@elements=$serv->get_elements_by_pathway($each_path);
	$m=0;
	while($elements[0][$m] ne ""){
		$element_name=$elements[0][$m]{'names'}[0];
		print EF $element_name."\t"."$elements[0][$m]{'element_id'}"."\n";
		$m++;
	}
	close(EF);
}

sub get_element{
	open PF, "<", $path_file;
	unless(-d $element_path){
		mkdir $element_path or die;
	}
	while(<PF>){
		chomp $_;
		@arr=split(/\s\s\s\s+/,$_);
		get_element_per_path($arr[0]);

	}
	close(PF);
}

sub get_enzyme{
	open EF, ">", $enzyme_file;
	open GeneEF,">",$gene_enzyme_file;
	open GF, "<", $gene_file;
	%enzyme_h=();
	while(<GF>){
		chomp $_;
		my $str_gene=$_;
		print $_."\n";
		@enzymes=$serv->get_enzymes_by_gene($_);
		$j=0;
		print EF $_;
	    print GeneEF $str_gene;
		while($enzymes[0][$j] ne ""){
			print GeneEF "\t".$enzymes[0][$j];
			unless($enzyme_h{$enzymes[0][$j]}){
				$enzyme_h{$enzymes[0][$j]}=1;
				print EF $enzymes[0][$j]."\n";
				print $enzymes[0][$j]."\n";
			}
			$j++;
		}
		print GeneEF "\n";
	}
	close(EF);
}

sub get_gene{
	open GF, ">", $gene_file;
	$int_gene=$serv->get_number_of_genes_by_organism($organism);
	@genes=$serv->get_genes_by_organism($organism,1, $int_gene+2);
	for ($i=0;$i<$int_gene;$i++){
		print $i."\n";
		print GF $genes[0][$i]."\n";
	
	}
	close(GF);	

}

sub uniq2 { # remove duplicate items in an array and keep the original order
    my %seen = ();
    my @r = ();
    foreach my $a (@_) {
        unless ($seen{$a}) {
            push @r, $a;
            $seen{$a} = 1;
        }
    }
    return @r;
}

sub get_ec_ko{
	open EF, "<", $enzyme_file;
	open KOF, ">", $ec_ko_file;
	while(<EF>){
		chomp $_;
		@genes=$serv->get_genes_by_enzyme($_,"cth");
		$str_enzyme=$_;
		$j=0;
		print $str_enzyme;
		my %ko_s1=();
		while($genes[0][$j] ne ""){
			@kos=$serv->get_ko_by_gene($genes[0][$j]);
			$j++;
			if ($kos[0][0] ne ""){
				$k=0;
				while($kos[0][$k] ne ""){
					unless($ko_s1{$kos[0][$k]}){
						$ko_s1{$kos[0][$k]}=1;
						print "\t".$kos[0][$k];
					}
					$k++;
				}
				
			}
		}
		my $ko_count= (keys %ko_s1);
		if($ko_count>0){
			print KOF $str_enzyme;
			foreach my $ko_1 (keys %ko_s1){
				print KOF "\t".$ko_1;
			}	
			print KOF "\n";
		}
		print "\n";
	}
	close(KOF);
}
sub test{
	$str2="abc";
	$str2 =~ /^(ab)c/;
	print $1;
	exit;
	open TF, ">", "test.txt";
	$i=0;
	while($i<100){
		print TF "".$i."\n";
		$i++;
	}
	close(TF);
	

}

sub global_variable{
	
	our	$red='FF0000'; #conflict one up one down
	our $orange='#FFA500';#microarray only down
	our $purple='#800080';#proteomics only down
	our $brown='#A52A2A';#both down
	our $lime='#00FF00';#microarray only up
	our $green='#008000';#proteomics only up
	our $olive='#808000';#both up
	our $black="#000000";#foreground
	our %color_code=();
	$color_code{"microarray up"}=$lime;
	$color_code{"proteomics up"}=$green;
	$color_code{"both up"}=$olive;
	$color_code{"microarray down"}=$orange;
	$color_code{"proteomics down"}=$purple;
	$color_code{"both down"}=$brown;
	$color_code{"conflict"}=$red;
	our %color_name=();
	$color_name{$red}='red';
	$color_name{$green}='green';
	$color_name{$olive}='olive';
	$color_name{$orange}='orange';
	$color_name{$purple}='purple';
	$color_name{$brown}='brown';
	$color_name{$lime}='lime';
	
}

#map_html("cth00780");
#test();

$wsdl = 'http://soap.genome.jp/KEGG.wsdl';
our $serv = SOAP::Lite->service($wsdl);
our $path="/local/workspace01/zhangju/kegg_develop/";
chdir $path;
our $gene_file=$path."cth_genes.txt";
our $organism="cth";
global_variable();
#get_gene();
our $enzyme_file=$path."cth_enzymes.txt";
our $gene_enzyme_file=$path."cth_gene_enzymes.txt";
#get_enzyme();
our $ec_ko_file=$path."cth_ec_ko.txt";
#get_ec_ko();
our $path_file=$path."cth_pathway.txt";
#get_pathway("cth");
our $element_path=$path."path_element/";
#get_element();
our @obj_list=[];
our @fg_list=[];
our @bg_list=[];
our $path_map=$path."path_map/";
our $path_source=$path."path_source/";
our $expression_file=$path."gene_up_down.txt";
our @color_paths=();
#color_code();
#color_paths();
#source_paths();
our %gene_enzyme=();
our %enzyme_ko=();
html_paths();
#our %cth_paths=();
#cth_path();
#get_pathway("cth");
exit;

$wsdl = 'http://soap.genome.jp/KEGG.wsdl';

$serv = SOAP::Lite->service($wsdl);
@element_id_list=[1,3,5,7];
@fg_list  = [ '#ff0000', '#0000ff', '#ff0000', '#0000ff' ];
@bg_list  = [ '#ffff00', '#ffff00', '#ffcc00', '#ffcc00' ];
$url2=$serv->get_html_of_colored_pathway_by_elements('path:cth00780',@element_id_list,@fg_list,@bg_list);
print "abc";
print $url2;
exit;


$url="http://soap.genome.jp/tmp/color_pathway.133132987017933/cth00780.png";
$path="/local/workspace01/zhangju/kegg_develop";
chdir $path;
@args=("wget",$url);
system(@args)==0 or die "system @args failed: $?";
exit;
$wsdl = 'http://soap.genome.jp/KEGG.wsdl';

$serv = SOAP::Lite->service($wsdl);
@element_id_list=[1,3,5,7,50];
@fg_list  = [ '#ff0000', '#0000ff', '#ff0000', '#0000ff', '#0000ff'];
@bg_list  = [ '#ffff00', '#ffff00', '#ffcc00', '#ffcc00', '#ffcc00'];
$url2=$serv->color_pathway_by_elements('path:cth00780',@element_id_list,@fg_list,@bg_list);
print "abc";
print $url2;
exit;
$url="http://www.kegg.jp/kegg-bin/show_pathway?cth00780";
$content = get($url) or die 'Unable to get page';
open CF, ">", "/local/workspace01/zhangju/path00780.source";
if(defined $content){
	print CF $content;
}
close(CF);
print "content-type: text/html \n\n"; #The header


$organism="cth"; # clostridium thermocellum ATCC 27405
$int_gene=$serv->get_number_of_genes_by_organism($organism);
@genes=$serv->get_genes_by_organism($organism,1, $int_gene+2);
#%enzymes_by_gene=(); # hash of arrays - multiple enzymes by each gene
@pathways=[];
shift @pathways;
print "abc\n";

%locus_enzymes=(); # hash of arrayes locus->enzymes
%locus_maps=(); # hash of arrayes locus->maps equivalent enzyme->maps
%element_id=(); # hash of hash map->object->element_id
%locus_dblink=();
$filepath="/local/workspace01/zhangju/kegg_develop/test2.txt";
open EF, ">", $filepath or die $!;
$locus_file="/local/workspace01/zhangju/kegg_develop/test1.txt";
#open LF, ">", $locus_file;
for ($i=0;$i<$int_gene;$i++){
	print $i."\n";
	print $genes[0][$i]."\n";
	@enzymes=$serv->get_enzymes_by_gene($genes[0][$i]);
	$j=0;
	if($enzymes[0][0] ne ""){
		$locus_enzymes{$genes[0][$i]}=[$enzymes[0]];
	}
#	@maps=[];
#	shift @maps;
	while($enzymes[0][$j] ne ""){
		print $enzymes[0][$j];
		@paths=$serv->get_pathways_by_enzymes([$enzymes[0][$j]]);
		$k=0;
		while($paths[0][$k] ne ""){
			$path=$paths[0][$k];
			$path =~ s/:map/:cth/g;
			if (not (grep {$path eq $_} @pathways)){
				push @pathways, $path;
#				push @maps, $path;
				print $path."\n";
				@elements=$serv->get_elements_by_pathway($path);
				$m=0;
				print EF "==========\n".$path."\n===========\n";
				while($elements[0][$m] ne ""){
					$path_name=$elements[0][$m]{'names'}[0];
					$path_name =~ s/:map/:cth/g;
					$element_id{$path}{$path_name}=$elements[0][$m]{'element_id'};
					print EF "$path_name"."\t"."$elements[0][$m]{'element_id'}"."\n";
					$m++;
				}
			}
			$k++;
		}
		$j++;	
	}

}
close(EF);
exit;
=pod
	print "maps";
	if(scalar(@maps)>0){
		$locus_maps{$genes[0][$i]}=[@maps];
	}


	$url="http://www.genome.jp/dbget-bin/get_linkdb?-t+8+$genes[0][$i]";
	$content = get $url;
  	if (defined $content){
		@texts=split(/\n/,$content);
		foreach(@texts){
			if ($_ =~ "www.ncbi"){
				$locus_dblink{$genes[0][$i]}=(split(/;/,$_))[1];
#				print LF "$genes[0][$i]}.(split(/;/,$_))[1]\n";
				break;
			}
		}
	}
}
$url=$serv->get_html_of_colored_pathway_by_objects('path:cth00780',['cth:Cthe_0021'],['blue'],['orange']);
@elements=$serv->get_elements_by_pathway('path:cth00780');
@element_id_list=[1,3,5,7];
@fg_list  = [ '#ff0000', '#0000ff', '#ff0000', '#0000ff' ];
@bg_list  = [ '#ffff00', '#ffff00', '#ffcc00', '#ffcc00' ];
$url2=$serv->get_html_of_colored_pathway_by_elements('path:cth00780',@element_id_list,@fg_list,@bg_list);

print "content-type: text/html \n\n"; #The header

$filepath = "/local/workspace01/zhangju/color_pathway.html";
if (not (-e $filepath)){
	open FILE,">", $filepath or die $!;
	close(FILE);
}
open HTML, ">", $filepath or die $!;
printf HTML "<html>\n"; 
printf HTML "<head>\n"; 
printf HTML "<title>My Home Page</title>"; 
printf HTML "</head>\n"; 
printf HTML "<body>\n"; 
printf HTML "<p align=\'center\'>Here we have an HTML page with a colored kegg path<br/>";
printf HTML "<a href=".$url2.">colored kegg path</a>";
printf HTML "</p>";
printf HTML "</body>\n"; 
printf HTML "</html>\n";
close (HTML);
exit;
$filepath = "/local/workspace01/zhangju/kegg_develop/color_pathway.html";
if (not (-e $filepath)){
	open FILE,">", $filepath or die $!;
	close(FILE);
}
open HTML, ">", $filepath or die $!;
printf HTML "<html>\n"; 
printf HTML "<head>\n"; 
printf HTML "<title>My Home Page</title>"; 
printf HTML "</head>\n"; 
printf HTML "<body>\n"; 
printf HTML "<p align=\'center\'>Here we have an HTML page with a colored kegg path<br/>";
printf HTML "<a href=".$IMG_gene.">colored kegg path</a>";
printf HTML "</p>";
printf HTML "</body>\n"; 
printf HTML "</html>\n";
close (HTML);
@elements=$serv->get_elements_by_pathway('path:cth00780');
@element_id_list=[1,3,5,7];
@fg_list  = [ '#ff0000', '#0000ff', '#ff0000', '#0000ff' ];
@bg_list  = [ '#ffff00', '#ffff00', '#ffcc00', '#ffcc00' ];
#$url2=$serv->get_html_of_colored_pathway_by_elements('path:cth00780',@element_id_list,@fg_list,@bg_list);
@command=('start',$url);
system(@command);

$IMG_gene="https://img.jgi.doe.gov/cgi-bin/er/main.cgi?section=GeneDetail&page=geneDetail&gene_oid=2511082802";

$ua = LWP::UserAgent->new;
$req = HTTP::Request->new(GET => $IMG_gene);
$req->authorization_basic('RSparlingLab', '!!MPNG2011');
print $ua->request($req)->as_string;
=cut
