[Bio] / FigWebServices / hope_tools.cgi Repository:
ViewVC logotype

View of /FigWebServices/hope_tools.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (download) (annotate)
Wed Oct 15 21:28:28 2008 UTC (11 years, 1 month ago) by dejongh
Branch: MAIN
CVS Tags: mgrast_dev_08112011, rast_rel_2009_05_18, mgrast_dev_08022011, rast_rel_2014_0912, myrast_rel40, mgrast_dev_05262011, rast_rel_2008_12_18, mgrast_dev_04082011, rast_rel_2010_0928, mgrast_version_3_2, mgrast_dev_12152011, mgrast_dev_06072011, rast_rel_2009_0925, rast_rel_2010_0526, rast_rel_2014_0729, mgrast_dev_02212011, rast_rel_2010_1206, mgrast_release_3_0, mgrast_dev_03252011, rast_rel_2010_0118, mgrast_rel_2008_1110_v2, rast_rel_2009_02_05, rast_rel_2011_0119, mgrast_release_3_0_4, mgrast_release_3_0_2, mgrast_release_3_0_3, mgrast_release_3_0_1, mgrast_dev_03312011, mgrast_release_3_1_2, mgrast_release_3_1_1, mgrast_release_3_1_0, mgrast_dev_04132011, mgrast_dev_04012011, rast_rel_2009_07_09, rast_rel_2010_0827, mgrast_rel_2008_1110, myrast_33, rast_rel_2011_0928, rast_rel_2008_10_29, mgrast_dev_04052011, mgrast_dev_02222011, rast_rel_2009_03_26, mgrast_dev_10262011, rast_rel_2008_11_24, HEAD
Changes since 1.3: +9 -9 lines
efficiency improvements

#!/usr/bin/env /Users/fig/FIGdisk/env/mac/bin/perl
BEGIN {
    @INC = qw(
              /Volumes/Bay3/FIGdisk.v5/dist/releases/dejongh/mac/lib
              /Volumes/Bay3/FIGdisk.v5/dist/releases/dejongh/mac/lib/FigKernelPackages
              /Users/fig/FIGdisk/dist/dejongh/mac/lib
              /Users/fig/FIGdisk/dist/dejongh/mac/lib/FigKernelPackages
              /Users/fig/FIGdisk/env/mac/lib/perl5/5.8.7/darwin-2level
              /Users/fig/FIGdisk/env/mac/lib/perl5/5.8.7
              /Users/fig/FIGdisk/env/mac/lib/perl5/site_perl/5.8.7/darwin-2level
              /Users/fig/FIGdisk/env/mac/lib/perl5/site_perl/5.8.7
              /Users/fig/FIGdisk/env/mac/lib/perl5/site_perl
              .
              /Users/fig/FIGdisk/config
	      
	     );
  }
use CGI;
use strict;
use integer;
use FIG;
use FIG_Config;
use CGI;
use HTML;
use CGI::Carp qw(fatalsToBrowser);
use model;
use Switch; 

our $fig = new FIG;
my $cgi = new CGI;
my $html = [];
my($p, $v);


#-----------------------------------------------------------------------
#  Initial tools page
#-----------------------------------------------------------------------
my $user = $cgi->param("user");
my %params = $cgi->Vars;
my $param;

push @$html, "<TITLE>The SEED: Subsystem Tools</TITLE>\n";
push (@$html , "<table width=\"100%\" cellspacing=2 ><tr><td bgcolor=\"lightblue\"><font size=\"+3\" >",
      $cgi->start_form(-method => 'post' , -action => 'hope_tools.cgi',-name => 'tool select'), 
      $cgi->submit(-name => 'start_page', -label =>"Home"),
      $cgi->submit(-name => 'find_kegg_id', -label =>"Find KEGG Compound"),
      $cgi->submit(-name => 'find_kegg_reaction', -label =>"Find KEGG Reaction"), 
      $cgi->submit(-name => 'EC_search', -label =>"EC Number Search"),
      $cgi->submit(-name => 'create_maps_for_org', -label => "Reactions not in Subsystems"),
      $cgi->submit(-name => 'update_kegg', -label => "Update KEGG"),
      $cgi->submit(-name => 'update_hope', -label => "Update Hope Info"),
      $cgi->hidden(-name => 'user', $user),
      "</font></table>\n", $cgi->end_form);

if($cgi -> param("kegg_name_id_org") eq "1 Select\tan\torganism")
  {
    delete $params{"kegg_name_id_org"};
  }
if($cgi -> param("kegg_name_id_org_ec") eq "1 Select\tan\torganism")
  {
    delete $params{"kegg_name_id_org_ec"};
  }
if(defined $params{"user"})
  {
    delete($params{"user"});
  }
if(defined $params{"start_page"})
  {
    delete($params{"start_page"});
  }
foreach (keys %params)
  {
    if($params{$_} ne "")
      {
	$param = $_;
      }
  }

switch ($param){
  case "update_hope" {update_hope();}
    case "find_kegg_id" {find_kegg_id();}
      case "kegg_id" {find_kegg_id();}
	case "update_kegg" {update_kegg();}
	  case "find_kegg_reaction" {find_kegg_reaction();}
	    case "kegg_reaction" {find_kegg_reaction();}
	      case "EC_search" {EC_search();}
		case "EC_number" {EC_search();}
		  case "create_maps_for_org" {create_maps_for_org();}
		    case "kegg_name_id_org"{create_maps_for_org();}
		      case "kegg_name_id_org_ec"{miss_react_by_subsystem();}
			case "kegg_name"{create_the_kegg_dir();}
			    else  {Start_Page();}
}

&HTML::show_page($cgi,$html, 1);
exit;

sub Start_Page {
  my $user = $cgi->param("user");
  
  push (@$html , "<pre><center><h1>Welcome to Scenario Tools</h1></center>");
  push (@$html , "<h3>User Login</h3>");
  push (@$html ,
	$cgi->start_form(-action => "hope_tools.cgi"),
        " Enter User: ",
        $cgi->textfield(-name => "user", -size => 20));
  
  if($user){
    push (@$html , "<b><br> Logged in as: $user</b>");
  }
  
  #push(@$html, "<hr>");
  my $temp = `find $FIG_Config::var/Models -name Scenarios 2>&1`;
  my @files = split "\n", $temp;
  my @ids;
  
  foreach my $file (@files)
    {
      if ($file =~ /Models\/(.*)\//)
	{
	  if ($1 ne "All")
	    {
	      push (@ids, $1);
	    }
	}
    }
  
  my @orgs;
  foreach my $org (@ids)
    {
      my $gs = $fig->genus_species($org);
      if ($fig->genome_domain($org) ne "Environmental Sample")
	{
	  push @orgs, "$gs\t($org)";
	}
      else 
	{
	  push @orgs, "$gs\t($org)";
	}
    }
  
  foreach my $org (@orgs)
    {
      if($org =~ /\((.*)\)/)
	{
	  my $file = "$FIG_Config::var/Models/$1/Curation/kegg_id";
	  open(FILE, $file) or next;
	  if(<FILE> =~ /([a-z][a-z][a-z])/)
	    {
	      $org .= "\t$1";
	    }
	}
    }
  
  push(@orgs,"1 Select\tan\torganism");
  
  
  push (@$html , "<br><h3>Find KEGG Compound</h3>",
	" KEGG Compound ID: ",
	$cgi->textfield(-name => "kegg_id", -size => 20),
	"<br> Search inputs/outputs of subsystems scenarios by KEGG compound ID");
  
  push (@$html , "<br><h3>OR<br><br>Find KEGG Reaction</h3>",
	" KEGG Reaction ID: ",
	$cgi->textfield(-name => "kegg_reaction", -size => 20),
	"<br> Search for KEGG Reaction ID",
	"<br><h3>OR<br><br>EC Number Search</h3>",
	" EC Number: ",
	$cgi->textfield(-name => "EC_number", -size => 20),
	"<br> Search subsystems for KEGG EC numbers<br><br>",
	"<h3>OR<br><br> Reactions not in Subsystems(by maps) </h3>",
	$cgi->popup_menu( -name  => 'kegg_name_id_org', -values => [sort map {$_} @orgs], -default => "1 Select\tan\torganism"),
	"<br> Returns maps of reactions that are not in subsystems yet<br><br>",
	"<h3>OR<br><br> Reactions not in Subsystems (by subsystem)</h3>",
	$cgi->popup_menu( -name  => 'kegg_name_id_org_ec', -values => [sort map {$_} @orgs], -default => "1 Select\tan\torganism"),
	"<br> Returns missing reactions sorted by subsystems<br><br>",
	$cgi->submit(-name => 'start_page', -label =>"Submit"),
	"</pre>",
	$cgi->end_form); 
}


sub EC_search {
  my $EC, my $temp, my $line, my $role;
  my @data, my @functionalRole;
  my $user = $cgi->param("user");
  
  push (@$html ,
        $cgi->start_form(-action => "hope_tools.cgi"),
        "EC Number: ",
        $cgi->textfield(-name => "EC_number", -size => 20),
	$cgi->hidden(-name => 'user', $user),
        $cgi->submit(-name => 'EC_search', -label =>"Submit"),
        $cgi->end_form);
  
  if($cgi->param("EC_number")){
    $EC = $cgi->param("EC_number");
    $EC =~ s/\./\\./g;
    $temp = `find $FIG_Config::data/Subsystems -maxdepth 2 -name spreadsheet -exec grep $EC {} \\\; -print`;		
    $temp .= `find $FIG_Config::data/Subsystems -maxdepth 2 -name hope_kegg_info -exec grep $EC {} \\\; -print`;
    @data = split( /\n/, $temp);
    
    if(@data){
      
      foreach $line (@data){
	if($line =~ /$EC\D/){	
	  push (@functionalRole , "$line");
	}
	if($line =~ /Subsystems\/(.*)\//){
	  if(@functionalRole){
	    push(@$html , "<br><a href=\"javascript:void(0)\"onclick=\"window.open('subsys.cgi?user=$user&ssa_name=$1&request=show_ssa&check=1&sort=&show_clusters=&show_minus1=','$1 Curation','height=640,width=800,scrollbars=yes,toolbar=yes,status=yes,resizable=yes')\"><b>$1</b></a><br>");
	    foreach $role (@functionalRole){
	      push(@$html , "$role<br>") 
	    }
	    @functionalRole = ();
	  }
	}
      }
    }
    
    else{
      $EC = $cgi->param("EC_number");
      push (@$html , "<b><br>No results found for: $EC</b>");
    }
  }
  
}

sub find_kegg_reaction {
  my $reaction, my $temp, my $line, my $role;
  my @data, my @functionalRole;
  my $user = $cgi->param("user");
  
  push (@$html ,
        $cgi->start_form(-action => "hope_tools.cgi"),
        "KEGG reaction id: ",
        $cgi->textfield(-name => "kegg_reaction", -size => 20),
	$cgi->hidden(-name => 'user', $user),
        $cgi->submit(-name => 'find_kegg_reaction', -label =>"Submit"),
        $cgi->end_form);
  
  if($cgi->param("kegg_reaction")){
    $reaction = $cgi->param("kegg_reaction");
    $temp = `find $FIG_Config::data/Subsystems -maxdepth 2 -name hope_reactions -exec grep $reaction {} \\\; -print`;
    $temp .= `find $FIG_Config::data/Subsystems -maxdepth 2 -name hope_kegg_info -exec grep $reaction {} \\\; -print`;
    @data = split( /\n/, $temp);
    
    if(@data){
      
      foreach $line (@data){
	if($line =~ /$reaction/){	
	  push (@functionalRole , "$line");
	}
	if($line =~ /Subsystems\/(.*)\//){
	  if(@functionalRole){
	    push(@$html , "<br><a href=\"javascript:void(0)\"onclick=\"window.open('subsys.cgi?user=$user&ssa_name=$1&request=show_ssa&check=1&sort=&show_clusters=&show_minus1=','$1 Curation','height=640,width=800,scrollbars=yes,toolbar=yes,status=yes,resizable=yes')\"><b>$1</b></a><br>");
	    foreach $role (@functionalRole){
	      push(@$html , "$role<br>") 
	    }
	    @functionalRole = ();
	  }
	}
      }
    }
    
    else{
      $reaction = $cgi->param("kegg_reaction");
      push (@$html , "<b><br>No results found for: $reaction</b>");
    }
  }
  
}

sub find_kegg_id {
  
  my %input, my %output;
  my $subsys, my $temp, my $line, my $name, my $count, my $key;
  my @data, my @kegg_info, my @inputs, my @outputs;
  my $user = $cgi->param("user");
  
  #push(@$html , $cgi->param("find_kegg_id"));
  push (@$html ,
        $cgi->start_form(-action => "hope_tools.cgi"),
        "KEGG compound ID: ",
        $cgi->textfield(-name => "kegg_id", -size => 20),
	$cgi->hidden(-name => 'user', $user),
        $cgi->submit(-name => 'find_kegg_id', -label =>"Submit"),
        $cgi->end_form);
  
  if($cgi->param("kegg_id")){
    #push(@$html , $cgi->param("kegg_id"));
  }
  
  my $kegg_id = $cgi->param("kegg_id");
  
  $temp = `find $FIG_Config::data/Subsystems -maxdepth 2 -name hope_kegg_info -exec grep -l $kegg_id  {} \\\;`; 
  @data = split( /\n/, $temp);
  
  if(@data){
    foreach $line (@data)
      {
	open (CURRENT, "$line") or print STDERR "Can't open $line\n";
	@kegg_info = <CURRENT>; # load the entire file into successive array elements
	
	if ($line =~ /Subsystems\/(.*)\//){
	  $subsys = "$1";
	}
	
	my @inputs;
	my @outputs;
	
	$count = 0; 
	foreach(@kegg_info){
	  $name = $_ if $count == 0;
	  
	  if(/$kegg_id/) {
	    if($count == 1){
	      $input{$subsys} = \@inputs;
	      push(@inputs, $name);
	    }
	    
	    if($count == 2){
	      $output{$subsys} = \@outputs;
	      push(@outputs, $name);
	    }
	  }
	  $count++;
	  $count %= 6;
	}
      }
    
    
    my $inputs;
    push(@$html , "<h3>Inputs:</h3>");
    for my $key (keys %input){
      push(@$html , "<a href=\"javascript:void(0)\"onclick=\"window.open('subsys.cgi?user=$user&ssa_name=$key&request=show_ssa&check=1&sort=&show_clusters=&show_minus1=','$key Curation','height=640,width=800,scrollbars=yes,toolbar=yes,status=yes,resizable=yes')\"><b>$key</b></a><br>");
      foreach $inputs (@{$input{$key}}){
	foreach ($inputs){
	  push(@$html , "$_<br>");
	}
      }
    }
    
    my $outputs;
    push(@$html , "<br><h3>Outputs:</h3>");
    for my $key (keys %output){
      push(@$html , "<a href=\"javascript:void(0)\"onclick=\"window.open('subsys.cgi?user=$user&ssa_name=$key&request=show_ssa&check=1&sort=&show_clusters=&show_minus1=','$key Curation','height=640,width=800,scrollbars=yes,toolbar=yes,status=yes,resizable=yes')\"><b>$key</b></a><br>");
      foreach $outputs (@{$output{$key}}){
	foreach ($outputs){
	  push(@$html , "$_<br>");
	}
      }
    }
  }
  else {
    if($kegg_id) {
      push (@$html , "<br><b>No results found for: $kegg_id</b>");
    }
  }
}

sub create_maps_for_org {
  my $kegg_org = $cgi->param("kegg_name");
  my $genome_id = $cgi ->param("genome_id");

  if($cgi->param("kegg_name") and  $cgi ->param("genome_id"))
    {
      my $gs = $fig->genus_species($genome_id);
      push(@$html, "<h1> $gs\t$genome_id</h1>");
    }
  if ($cgi->param("kegg_name_id_org") ne "1 Select\tan\torganism")
    {
      my @name_id_org = split "\t", $cgi->param("kegg_name_id_org");
      if(@name_id_org[2])
	{
	  $genome_id = $name_id_org[1];
	  $kegg_org = $name_id_org[2];
	  push (@$html, "<h1>$name_id_org[0]\t$genome_id</h1>");
	}
      else
	{
	  add_kegg_org();
	}
    }
  
  if($genome_id and $kegg_org) 
    {
      my @files = `find $FIG_Config::data/Subsystems -maxdepth 2 -name spreadsheet`;
      
      my %ec_to_sub;
      
      foreach my $file (@files){
	my ($ss);
	open (SUB, "<$file") or die "can't open file";
	$/= "//";
	my @lines = split "\n", <SUB>;
	
	if($file =~ /Subsystems\/(.*)\//){
	  $ss = $1; 
	}
	
	foreach my $l (@lines){
	  my $ec;
	  if($l =~ /\(EC\s+(\S+\.\S+\.\S+\.\S+)\)/){
	    $ec = $1;
	  }
	  $ec_to_sub{$ec}->{$ss}=1;
	}
	close SUB;
      }
      
      open (ECFILE, "$FIG_Config::data/KEGG/reaction");
      my (%reaction_to_EC, %all_reactions_to_EC);
      $/ = "\n///\n";
      while (<ECFILE>)
	{
	  chomp;
	  my $reaction;
	  if($_ =~ /ENTRY\s+(R\d+)\s+/)
	    {
	      $reaction = $1;
	    }
	  my $enzyme;
	  if($_ =~ /ENZYME\s+(\S+\.\S+\.\S+\.\S+)\s*/)
	    {
	      $enzyme = $1;
	    }
	  if($enzyme)
	    {
	      $all_reactions_to_EC{$reaction} = $enzyme;
	    }
	}
      close ECFILE;
      
      open(SUP, "<$FIG_Config::data/Global/Models/hope_supersets.txt");
      
      my @subsystems;
      $/ = "\n";
      while(<SUP>){
	chomp;
	my($junk, $ss) = split "\t", $_;
	push(@subsystems, $ss);
      }
      
      close SUP;
      
      my %map_to_subsystems;
      foreach my $ss (@subsystems){
	open(SS,"<$FIG_Config::data/Subsystems/$ss/hope_kegg_info") or next;
	while(<SS>){
	  chomp;
	  if($_=~/(\d+)/)
	    {
	      $map_to_subsystems{$1} -> {$ss} = 1;
	    }
	}
      }
                 
      my $dir = "$FIG_Config::kgml_dir/$kegg_org/";
      my @xmlfiles;
      opendir(BIN, $dir) or next;
      while( defined (my $file = readdir BIN) ) 
	{
	  if($file =~ /\.xml$/)
	    {
	      push(@xmlfiles,$dir.$file) if -T "$dir/$file";
	    }
	}
      closedir(BIN);
      
      my (%mapping, %map_path_name);
      
      foreach my $file (@xmlfiles)
	{
	  my $path_id;
	  
	  if ($file =~ /$kegg_org(\d\d\d\d\d)/)
	    {
	      $path_id = $1;
	    }
	  my @title = `grep "title=" $file`;
	  
	  foreach my $title (@title)
	    {
	      chomp $title;
	      if($title =~ /title=\"(.*)\"/)
		{
		  $map_path_name{$path_id}= "$1"; 
		}
	    }
	  my @lines = `grep "type=\\"gene\\"" $file | grep reaction`;
	  
	  foreach my $line (@lines)
	    {
	      chomp $line;
	      
	      if ($line =~ /name=\"(.*)\" type.*reaction=\"(.*)\"/)
		{
		  my $genelist = $1;
		  my $reactionlist = $2;
		  my @genes = split " ", $genelist;
		  my @reactions = split " ", $reactionlist;
		  foreach my $reaction (@reactions)
		    {
		      $reaction =~ s/rn://g;
		      map { $mapping{$_}->{$reaction} = $path_id } @genes;
		    }
		}
	    }
	}
      
      #Creates a hash whose keys are all of the possible reactions.
      my (%all_reaction_map, $keys, $total_reactions);
      foreach my $gene (keys %mapping)
	{
	  my (@reaction);
	  my %reaction_hash = %{$mapping{$gene}};
	  @reaction = sort keys %reaction_hash;
	  foreach (@reaction)
	    {
	      $all_reaction_map{$_} = $reaction_hash{$_};
	      $reaction_to_EC{$_} = $all_reactions_to_EC{$_};
	      $total_reactions += 1;
	    }
	}
      
      #Get's an array of all of the reactions covered by scenarios.
      my $fig = new FIG;
      my $superset_file = "$FIG_Config::global/Models/hope_supersets.txt";
      
      #load scenario information
      model::load_superset_file($superset_file);
      my ($all_rxn,$rxn_to_sub,$rxn_to_prod) = model::load_supersets("All");
      my $reactions = join " ", sort keys %$all_rxn;
      my @scenario_reactions= split " ", $reactions;
      
      #Removes the reactions that are already covered by scenarios.
      foreach (@scenario_reactions)
	{
	  delete $all_reaction_map{$_};
	  delete $reaction_to_EC{$_};
	}
      
      my %EC_to_Reactions;
      while (my ($key, $value) = each (%reaction_to_EC))
	{
	  $EC_to_Reactions{$value}->{$key} = 1;
	}
      
      my (%Path_Unacc_React, %number_of_reactions, %num_react_to_map);
      while (my ($key, $value) = each (%all_reaction_map))
	{
	  $Path_Unacc_React{$value}.= "+$key";
	  $number_of_reactions{$value}+= 1;
	}
      my $number_reactions_left;
      $number_reactions_left = keys(%reaction_to_EC);
      while (my ($key, $value) = each (%number_of_reactions))
	{
	  $num_react_to_map{$value}.= "$key ";
	}
      
      push @$html, "<b>There are currently this many reactions not yet in subsystems: $number_reactions_left</b><br>";
      my $dec_done =  $number_reactions_left * 1000 /$total_reactions;
      my $dec_ver = 1000 - $dec_done;
      my $total_percentage = $dec_ver/10;
      push @$html, "<b>The organism has approximately $total_percentage% of its reactions in subsystems</b><br>";
      
      my $linkindex = 0;
      my $mappedindex = 0;
      foreach my $key (sort {$b <=> $a} keys %num_react_to_map)
	{
	  my @sortedkeys = split " ", $num_react_to_map{$key};

	  foreach my $links (@sortedkeys)
	    {
	      $linkindex++;
	      push (@$html, "<br><a href=\"\#tips$linkindex\"> $map_path_name{$links} ($key Reactions)</a>");
	    }
	}
      foreach my $key (sort {$b <=> $a} keys %num_react_to_map)
	{
	  my @sortedkeys = split " ", $num_react_to_map{$key};
	  foreach my $map (@sortedkeys)
	    {
	      $mappedindex++;
	      push(@$html, "<br><br><a href=\"javascript:void(0)\"onclick=\"window.open('http://www.genome.jp/dbget-bin/show_pathway?rn$map$Path_Unacc_React{$map}','$&','height=640,width=800,scrollbars=yes,toolbar=yes,status=yes,resizable=yes')\"><font size=+2><b>$map</b></a>\n</font>");
	      push(@$html, "<a name=\"tips$mappedindex\"><font size=+2> <b>$map_path_name{$map}  ($key reactions)</b> </font></a>");
	      push (@$html, "<a href=\"\#top\"><b> Back To Top </b> </a>");
	      push(@$html, "<br><br> <b> Possible subsystems for the missing reactions based on EC #'s</b> <br>");
	      
	      my @reactions = sort split m"\+", $Path_Unacc_React{$map};
	      my %number_ss;
	      my %unique_ss;
	      my %react_for_EC;
	      my %ss_to_EC;
	      foreach my $reaction (@reactions)
		{
		  if ($reaction)
		    {
		      my $EC = $reaction_to_EC{$reaction};
		      if ($EC)
			{
			  if ($ec_to_sub{$EC})
			    {
			      my %ss = %{$ec_to_sub{$EC}};
			      if (%ss)
				{
				  foreach my $ss (sort keys %ss)
				    {
				      $unique_ss{$ss}= 1;
				      $ss_to_EC{$ss} -> {$EC} = 1;
				      $react_for_EC{$EC} -> {$reaction} = 1;
				      $number_ss{$ss} += 1;
				    }
				}
			    }
			}
		    }
		}
	      my %num_each_ss;
	      while (my ($key, $value) = each (%number_ss))
		{
		  $num_each_ss{$value}.= "$key ";
		}
	      foreach my $key (sort {$b <=> $a} keys %num_each_ss)
		{
		  my @sortedkeys = split " ", $num_each_ss{$key};
		  foreach my $ss (@sortedkeys)
		    {
		      push(@$html , "<br><br>  <a href=\"javascript:void(0)\"onclick=\"window.open('subsys.cgi?user=$user&ssa_name=$ss&request=show_ssa&check=1&sort=&show_clusters=&show_minus1=','$&','height=640,width=800,scrollbars=yes,toolbar=yes,status=yes,resizable=yes')\"><b>$ss ($number_ss{$ss} reactions matched based on E.C. #'s)</b></a><br>");
		      
		      my @ECs = sort (keys %{$ss_to_EC{$ss}});
		      foreach my $EC (@ECs)
			{
			  push (@$html, "<br>");
			  my $tempss= $ss;
			  $tempss =~ s/\(/\\(/g;
			  $tempss =~ s/\)/\\)/g;
			  my $dir = "$FIG_Config::data/Subsystems/".$tempss."/spreadsheet";
			  my $temp = `grep $EC $dir`;
			  my @ECinfo = split "\n", $temp;
			  if ($ECinfo[0] =~ /$EC\D/)
			    {
			      push (@$html, $ECinfo[0]);
			    }
			  else
			    {
			      push (@$html, "(EC $EC)");
			    }
			  my @reacts = sort (keys %{$react_for_EC{$EC}});
			  foreach my $react (@reacts)
			    {
			      push (@$html, " ");
			      push (@$html, "<a href=\"javascript:void(0)\"onclick=\"window.open('http://www.genome.ad.jp/dbget-bin/www_bget?rn+$react','$&','height=640,width=800,scrollbars=yes,toolbar=yes,status=yes,resizable=yes')\">$react</a>");
			      push (@$html, " ");
			    }
			  
			}
		    }
		}
	      push(@$html, "<br><br><b> Subsystems that use the map in their scenarios </b>");
	      if (defined $map_to_subsystems{$map})
	      {
		  my %sshash = %{$map_to_subsystems{$map}};
		  foreach my $ss (keys %sshash)
		    {
		      push(@$html , "<pre> <a href=\"javascript:void(0)\"onclick=\"window.open('subsys.cgi?user=$user&ssa_name=$ss&request=show_ssa&check=1&sort=&show_clusters=&show_minus1=','$&','height=640,width=800,scrollbars=yes,toolbar=yes,status=yes,resizable=yes')\"><b>$ss</b></a> </pre>");
		    }
		}
	    }
	}
    }
  else
    {
      #push(@$html, "<hr>");
      my $temp = `find $FIG_Config::var/Models -name Scenarios 2>&1`;
      my @files = split "\n", $temp;
      my @ids;
      
      foreach my $file (@files)
	{
	  if ($file =~ /Models\/(.*)\//)
	    {
	      if ($1 ne "All")
		{
		  push (@ids, $1);
		}
	    }
	}
      
      my @orgs;
      foreach my $org (@ids)
	{
	  my $gs = $fig->genus_species($org);
	  if ($fig->genome_domain($org) ne "Environmental Sample")
	    {
	      push @orgs, "$gs\t($org)";
	    }
	  else 
	    {
	      push @orgs, "$gs\t($org)";
	    }
	}
      
      foreach my $org (@orgs)
	{
	  if($org =~ /\((.*)\)/)
	    {
	      my $file = "$FIG_Config::var/Models/$1/Curation/kegg_id";
	      open(FILE, $file) or next;
	      if(<FILE> =~ /([a-z][a-z][a-z])/)
		{
		  $org .= "\t$1";
		}
	    }
	}
      
      push(@orgs,"1 Select\tan\torganism");
                
      push (@$html ,  "<h3>Reactions not in subsystems</h3>",
	    "<b>Pick an organism</b>",
	    $cgi->start_form(-action => "hope_tools.cgi"),
	    $cgi->popup_menu( -name  => 'kegg_name_id_org', -values => [sort map {$_} @orgs], -default => "1 Select\tan\torganism"),
	    "<br> Returns maps of reactions that are not in subsystems yet<br><br>",
	    $cgi->submit(-name => 'start_page', -label =>"Submit"),
	    "</pre>",
	    $cgi->end_form); 
    }
}

sub update_kegg{
  my $user = $cgi->param("user");
  
  push (@$html , $cgi->start_form(-action => "hope_tools.cgi"),
	$cgi->hidden(-name => 'user', $user),
	$cgi->end_form);
  
  open (BODY, "<$FIG_Config::fig_disk/dist/releases/dejongh/update_body");
  
  while(<BODY>){
    if($user){
      $_ =~ s/USERID/$user/g;
    }
    else{
      $_ =~ s/USERID//g;
    }
    push (@$html, $_);
  }
  
  close BODY;  
}

sub update_hope{
  my $user = $cgi->param("user");
  
  push (@$html , $cgi->start_form(-action => "hope_tools.cgi"),
	$cgi->hidden(-name => 'user', $user),
	$cgi->end_form);
  
  open (BODY, "<$FIG_Config::fig_disk/dist/releases/dejongh/update_hope");
  
  while(<BODY>){
    if($user){
      $_ =~ s/USERID/$user/g;
    }
    else{
      $_ =~ s/USERID//g;
    }
    push (@$html, $_);
  }
  
  close BODY;  
}

sub add_kegg_org{
  my $user = $cgi->param("user");
  my $genome_id;
  if($cgi->param("kegg_name_id_org") ne "1 Select\tan\torganism")
    {
      my @name_id_org = split "\t", $cgi->param("kegg_name_id_org");
      $genome_id = $name_id_org[1];
      $genome_id =~ s/\(//g;
      $genome_id =~ s/\)//g;
    }
  if($cgi->param("kegg_name_id_org_ec") ne "1 Select\tan\torganism")
    {
      my @name_id_org = split "\t", $cgi->param("kegg_name_id_org_ec");
      $genome_id = $name_id_org[1];
      $genome_id =~ s/\(//g;
      $genome_id =~ s/\)//g;
    }
  
  (my $link_id, my $nothing) = split(/\./,  $genome_id);
  if ($link_id)
    {
      push (@$html , "The three letter identifier is not defined, look in the link to find it and submit it.");
      push (@$html , "<br> Look for something like \"gn:eco\". The letters \"eco\" would be the kegg name for this. <br>");
      push (@$html , "<br><a href=\"javascript:void(0)\"onclick=\"window.open('http://www.genome.jp/dbget-bin/www_bfind_sub?mode=bfind&max_hit=1000&dbkey=kegg&keywords=$link_id','height=640,width=800,scrollbars=yes,toolbar=yes,status=yes,resizable=yes')\">KEGG ID LINK</a><br><br>");
      push (@$html ,
	    $cgi->start_form(-action => "hope_tools.cgi"),
	    "KEGG NAME: ",
	    $cgi->textfield(-name => "kegg_name", -size => 20),
	    $cgi->hidden(-name => 'user', $user),
	    "<br>Genome ID:",
	    $cgi->textfield(-name => 'genome_id',-default=> $genome_id),
	    $cgi->submit(-name => 'create_the_kegg_dir', -label =>"Submit"),
	    $cgi->end_form);
    }
}

sub create_the_kegg_dir { 
  my $kegg_name = $cgi->param("kegg_name");
  my $genome_id = $cgi ->param("genome_id");
  
  my $file = "$FIG_Config::var/Models/$genome_id/Curation/kegg_id";
  
  #check if model directory exists
  my $path = "/Users/fig/FIGdisk/FIG/var/Models/$genome_id/";
  my $tmp = `find $path 2>&1`;
  
  if($tmp =~ /No such file or directory/){
    print STDERR "creating directory $path\n";
    mkdir($path);
  }
  
  #check if curation directory exists for a particular model
  #by implication if curation directory neither will necessary files within curation directory
  $path .= "Curation";
  $tmp = `find $path 2>&1`;
  
  if($tmp =~ /No such file or directory/){
    print STDERR "creating directory $path\n";
    mkdir($path);
  }
  unless($tmp =~ /kegg_id/s)
    {
      open(A, "+>>$path/kegg_id") or warn "unable to create $path/kegg_id";
      print A "$kegg_name";
      close A;
    }
  create_maps_for_org();
}


sub miss_react_by_subsystem {
  my $kegg_org = $cgi->param("kegg_name");
  my $genome_id = $cgi ->param("genome_id");
  
  if($cgi->param("kegg_name") and  $cgi ->param("genome_id"))
    {
      my $gs = $fig->genus_species($genome_id);
      push(@$html, "<h1> $gs\t$genome_id</h1>");
    }
  my @name_id_org;
  if ($cgi->param("kegg_name_id_org_ec") ne "1 Select\tan\torganism")
    {
      @name_id_org = split "\t", $cgi->param("kegg_name_id_org_ec");
      if(@name_id_org[2])
	{
	  $genome_id = $name_id_org[1];
	  $kegg_org = $name_id_org[2];
	  push (@$html, "<h1>$name_id_org[0]\t$genome_id</h1>");
	  $genome_id =~ s/\(//g;
	  $genome_id =~ s/\)//g;
	}
      else
	{
	  add_kegg_org();
	}
    }
  
  if($genome_id and $kegg_org) 
    {

      my @files = `find $FIG_Config::data/Subsystems -maxdepth 2 -name reactions`;
      
      my (%all_reactions_to_sub, %sub_to_reactions, %reactions_to_sub);
      
      foreach my $file (@files){
	my ($ss);
	open (SUB, "<$file") or die "can't open file";
	$/= "//";
	my @lines = split "\n", <SUB>;
	
	if($file =~ /Subsystems\/(.*)\//){
	  $ss = $1; 
	}
	
	foreach my $l (@lines)
	  {
	    my $react;
	    if($l =~ /(R\d+)/)
	      {
		$react = $1;
	      }
	    $sub_to_reactions{$ss}->{$react}=1;
	    $all_reactions_to_sub{$react}->{$ss}=1;
	  }
	close SUB;
      }
      
      open (ECFILE, "$FIG_Config::data/KEGG/reaction");
      my (%reaction_to_EC, %all_reactions_to_EC);
      $/ = "\n///\n";
      while (<ECFILE>)
	{
	  chomp;
	  my $reaction;
	  if($_ =~ /ENTRY\s+(R\d+)\s+/)
	    {
	      $reaction = $1;
	    }
	  my $enzyme;
	  if($_ =~ /ENZYME\s+(\S+\.\S+\.\S+\.\S+)\s*/)
	    {
	      $enzyme = $1;
	    }
	  if($enzyme)
	    {
	      $all_reactions_to_EC{$reaction} = $enzyme;
	    }
	}
      close ECFILE;


      
                 
      open(SUP, "<$FIG_Config::data/Global/Models/hope_supersets.txt");
      
      my @subsystems;
      $/ = "\n";
      while(<SUP>){
	chomp;
	my($junk, $ss) = split "\t", $_;
	push(@subsystems, $ss);
      }
      
      close SUP;
      
      my (%subsystems_to_map, %map_to_subsystems);
      foreach my $ss (@subsystems){
	open(SS,"<$FIG_Config::data/Subsystems/$ss/hope_kegg_info") or next;
	while(<SS>){
	  chomp;
	  if($_=~/^(\d+)/)
	    {
	      $subsystems_to_map{$ss} -> {$1} = 1;
	      $map_to_subsystems{$1} -> {$ss} = 1;
	    }
	}
      }
                 
      my $dir = "$FIG_Config::kgml_dir/$kegg_org/";
      my @xmlfiles;
      opendir(BIN, $dir) or next;
      while( defined (my $file = readdir BIN) ) 
	{
	  if($file =~ /\.xml$/)
	    {
	      push(@xmlfiles,$dir.$file) if -T "$dir/$file";
	    }
	}
      closedir(BIN);
      
      my (%mapping, %map_path_name);
      
      foreach my $file (@xmlfiles)
	{
	  my $path_id;
	  
	  if ($file =~ /$kegg_org(\d\d\d\d\d)/)
	    {
	      $path_id = $1;
	    }
	  my @title = `grep "title=" $file`;
	  
	  foreach my $title (@title)
	    {
	      chomp $title;
	      if($title =~ /title=\"(.*)\"/)
		{
		  $map_path_name{$path_id}= "$1"; 
		}
	    }
	  my @lines = `grep "type=\\"gene\\"" $file | grep reaction`;
	  
	  foreach my $line (@lines)
	    {
	      chomp $line;
	      
	      if ($line =~ /name=\"(.*)\" type.*reaction=\"(.*)\"/)
		{
		  my $genelist = $1;
		  my $reactionlist = $2;
		  my @genes = split " ", $genelist;
		  my @reactions = split " ", $reactionlist;
		  foreach my $reaction (@reactions)
		    {
		      $reaction =~ s/rn://g;
		      map {$mapping{$_}->{$reaction} = $path_id } @genes;
		    }
		}
	    }
	}
      
      #Creates a hash whose keys are all of the possible reactions.
      my (%all_reaction_map, $keys, $total_reactions);
      foreach my $gene (keys %mapping)
	{
	  my (@reaction);
	  my %reaction_hash = %{$mapping{$gene}};
	  @reaction = sort keys %reaction_hash;
	  foreach (@reaction)
	    {
	      $all_reaction_map{$_} = $reaction_hash{$_};
	      #$reaction_to_EC{$_} = $all_reactions_to_EC{$_};
	      if ($all_reactions_to_sub{$_})
		{
		  $reactions_to_sub{$_} = $all_reactions_to_sub{$_};
		}
	      $total_reactions += 1;
	    }
	}
      
      #Get's an array of all of the reactions covered by scenarios.
      my $fig = new FIG;
      my $superset_file = "$FIG_Config::global/Models/hope_supersets.txt";
      
      #load scenario information
      model::load_superset_file($superset_file);
      my ($all_rxn,$rxn_to_sub,$rxn_to_prod) = model::load_supersets("$genome_id");
      my $reactions = join " ", sort keys %$all_rxn;
      my @scenario_reactions= split " ", $reactions;
      
      #Removes the reactions that are already covered by scenarios.
      foreach (@scenario_reactions)
	{
	  delete $all_reaction_map{$_};
	  delete $reactions_to_sub{$_};
	}
      
      my (%sub_to_reactions,%number_of_reactions, %num_react_to_sub);
      while (my ($key, $value) = each (%reactions_to_sub))
	{
	  my @subsystems = sort (keys %{$value});
	  foreach my $sub (@subsystems)
	    {
	      $sub_to_reactions{$sub}->{$key} = 1;
	      $number_of_reactions{$sub}+= 1;
	    }
	}
      my $react_not_in_ss_with_known_ss = keys(%reactions_to_sub);
      my $number_reactions_left;
      $number_reactions_left = keys(%all_reaction_map);
      while (my ($key, $value) = each (%number_of_reactions))
	{
	  $num_react_to_sub{$value}.= "$key ";
	}
      
      push @$html, "<b>There are currently this many reactions not yet in subsystems: $number_reactions_left</b><br>";
      push @$html, "<b>There are currently this many reactions missing for $name_id_org[0] that have known Hope Scenarios, that are not yet in subsystems for $name_id_org[0]: $react_not_in_ss_with_known_ss</b> <br>";
      my $dec_done =  $number_reactions_left * 1000 /$total_reactions;
      my $dec_ver = 1000 - $dec_done;
      my $total_percentage = $dec_ver/10;
      push @$html, "<b>The organism has approximately $total_percentage% of its reactions in subsystems</b><br>";
      
      my $linkindex = 0;
      my $mappedindex = 0;
      foreach my $key (sort {$b <=> $a} keys %num_react_to_sub)
	{
	  my @subsystems = split " ", $num_react_to_sub{$key};

	  foreach my $links (@subsystems)
	    {
	      $linkindex++;
	      push (@$html, "<br><a href=\"\#tips$linkindex\">  $links ($key Reactions)</a>");
	    }
	}
      foreach my $key (sort {$b <=> $a} keys %num_react_to_sub)
	{
	  my @sortedkeys = split " ", $num_react_to_sub{$key};
	  foreach my $subsystem (@sortedkeys)
	    {
	      $mappedindex++;
	      push(@$html, "<br><br><a name=\"tips$mappedindex\"><font size=+2><b>$subsystem ($key reactions)</b></a></font>", 
		   "<a href=\"\#top\"><b> Back To Top </b> </a><br>");
	      push(@$html, "  <a href=\"javascript:void(0)\"onclick=\"window.open('subsys.cgi?user=$user&ssa_name=$subsystem&request=show_ssa&check=1&sort=&show_clusters=&show_minus1=','$&','height=640,width=800,scrollbars=yes,toolbar=yes,status=yes,resizable=yes')\"><b>$subsystem ($key reactions matched)</b></a><br>");
	      
	      
	      my @reactions = sort (keys%{$sub_to_reactions{$subsystem}});
	      my $reactlink;
	      push (@$html, "<br><b>Reactions missing from the subsystem</b><br>");
	      foreach my $react (@reactions)
		{
		  $reactlink .= "+$react";
		  push (@$html, " ");
		  push (@$html, "<a href=\"javascript:void(0)\"onclick=\"window.open('http://www.genome.ad.jp/dbget-bin/www_bget?rn+$react','$&','height=640,width=800,scrollbars=yes,toolbar=yes,status=yes,resizable=yes')\">$react</a>");
		  push (@$html, " ");
		}
	      push(@$html, "<br><br><b> Maps that use the given subsystem <b>");
	      my @maps = sort(keys%{$subsystems_to_map{$subsystem}});
	      foreach my $map (@maps)
		{
		  push (@$html, "<br><a href=\"javascript:void(0)\"onclick=\"window.open('http://www.genome.jp/dbget-bin/show_pathway?rn$map$reactlink','$&','height=640,width=800,scrollbars=yes,toolbar=yes,status=yes,resizable=yes')\"><b>$map $map_path_name{$map}</b></a>\n");
		}
	    }
	}
    }
  else
    {
      #push(@$html, "<hr>");
      my $temp = `find $FIG_Config::var/Models -name Scenarios 2>&1`;
      my @files = split "\n", $temp;
      my @ids;
      
      foreach my $file (@files)
	{
	  if ($file =~ /Models\/(.*)\//)
	    {
	      if ($1 ne "All")
		{
		  push (@ids, $1);
		}
	    }
	}
      
      my @orgs;
      foreach my $org (@ids)
	{
	  my $gs = $fig->genus_species($org);
	  if ($fig->genome_domain($org) ne "Environmental Sample")
	    {
	      push @orgs, "$gs\t($org)";
	    }
	  else 
	    {
	      push @orgs, "$gs\t($org)";
	    }
	}
      
      foreach my $org (@orgs)
	{
	  if($org =~ /\((.*)\)/)
	    {
	      my $file = "$FIG_Config::var/Models/$1/Curation/kegg_id";
	      open(FILE, $file) or next;
	      if(<FILE> =~ /([a-z][a-z][a-z])/)
		{
		  $org .= "\t$1";
		}
	    }
	}
      
      push(@orgs,"1 Select\tan\torganism");
                
      push (@$html ,  "<h3>Reactions not in subsystems</h3>",
	    "<b>Pick an organism</b>",
	    $cgi->start_form(-action => "hope_tools.cgi"),
	    $cgi->popup_menu( -name  => 'kegg_name_id_org_ec', -values => [sort map {$_} @orgs], -default => "1 Select\tan\torganism"),
	    "<br> Returns maps of reactions that are not in subsystems yet<br><br>",
	    $cgi->submit(-name => 'start_page', -label =>"Submit"),
	    "</pre>",
	    $cgi->end_form); 
    }
}


  
      

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3