#!/usr/bin/perl

#use strict;
#use warnings;
use diagnostics;

# Program: refgenomes_graph.pl
#
# Original Author: Mary Dolan
#
# Purpose: Given an ontology in OBO format 
#    and N annotation files in GO standard format
#    and a list of ortholog N-tuples
#    generate comparative annotation graphs
#    index page links to html page containing svg image for each ortholog
#
# Requirements:	Annotation files are tab-delimited (15 columns)
# 		ortholog file is N-column, tab-delimited 
#		  (for each member, id that matches annotation file and symbol)
#
# Usage: perl refgenomes_graphs.pl > index.html
#

$time_date = localtime(time);

#########read in OMIM disease-gene lookup table: disorder, id, gene, id
%disease_index = ();
%omim_index = ();
%map_gene2id = ();
%map_id2gene = ();
#%map_disorder2id = ();
#%map_id2disorder = ();
%organism_map = ();

@omim = ();
$omim_count = 0;
open (OMIM, "refgenome_list.txt") ||print "refgenome_list open error";
while (<OMIM>) {
    if (/^(.*)\t(.*)\t(.*)\t(.*)$/) {
	$omim_count ++;
	$disorder = $1;
	$d_omim_index = lc(substr($disorder,0,1));
	$gene = $2;
	$g_omim_index = lc(substr($gene,0,1));
	$omim_id = $4;
	$location = $3;
	$map_gene2id{$gene} = $omim_id;
	$map_id2gene{$omim_id} = $gene;
#	$map_disorder2id{$disorder} = $disease_id;
#	$map_id2disorder{$disease_id} = $disorder;

#print "$omim_count, $disorder, $d_omim_index, $gene, $omim_id, $g_omim_index, $location\n";
	$omim[ $omim_count ] = [ $disorder, $gene, $omim_id, $location ];
	push @{$disease_index{$d_omim_index}}, $disorder;
	push @{$omim_index{$g_omim_index}}, $gene;
	}
}

#########read in orthologs
%ortho_map = ();
@organism_list = ("Human", "Mouse", "Rat", "Worm", "Fly", "Dicty", "Chicken", "Zfin", "Tair", "Yeast", "Pombe", "Ecoli");
	
open (ORTHO, "refgenome_orthologs.txt") ||print "refgenome_orthologs open error";
while (<ORTHO>) {
#OMIM_gene_id	human	mouse	rat	worm	fly	dicty	chicken	zfin	tair	yeast	pombe	ecoli
    # $1 = OMIM_gene_id
    # $2 = human
    # $3 = mouse
    # $4 = rat
    # $5 = worm
    # $6 = fly
    # $7 = dicty
    # $8 = chicken
    # $9 = zfin
    # $10 = tair
    # $11 = yeast
    # $12 = pombe
    # $13 = ecoli
    if (/^(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)$/) {
	$omim_gene_id = $1;
	$human_id = $2;
	$mouse_id = $3;
	$rat_id = $4;
	$worm_id = $5;
	$fly_id = $6;
	$dicty_id = $7;
	$chicken_id = $8;
	$zfin_id = $9;
	$tair_id = $10;
	$yeast_id = $11;
	$pombe_id = $12;
	$ecoli_id = $13;
#print "$omim_gene_id, $human_id, $mouse_id, $rat_id, $worm_id, $fly_id, $dicty_id, $chicken_id, $zfin_id, $tair_id, $yeast_id, $pombe_id, $ecoli_id\n";
$orthologs{$omim_gene_id} = [ $omim_gene_id, $human_id, $mouse_id, $rat_id, $worm_id, $fly_id, $dicty_id, $chicken_id, $zfin_id, $tair_id, $yeast_id, $pombe_id, $ecoli_id ];
	
	
	if ($human_id ne "") {
	push @ {$ortho_map{"Human"}{$human_id}}, $omim_gene_id;
	push @ {$organism_map{$omim_gene_id}}, "Human";
	}
	if ($mouse_id ne "") {
	push @ {$ortho_map{"Mouse"}{$mouse_id}}, $omim_gene_id;
	push @ {$organism_map{$omim_gene_id}}, "Mouse";
	}
	if ($rat_id ne "") {
	push @ {$ortho_map{"Rat"}{$rat_id}}, $omim_gene_id;
	push @ {$organism_map{$omim_gene_id}}, "Rat";
	}
	if ($worm_id ne "") {
	push @ {$ortho_map{"Worm"}{$worm_id}}, $omim_gene_id;
	push @ {$organism_map{$omim_gene_id}}, "Worm";
	}
	if ($fly_id ne "") {
	push @ {$ortho_map{"Fly"}{$fly_id}}, $omim_gene_id;
	push @ {$organism_map{$omim_gene_id}}, "Fly";
	}
	if ($dicty_id ne "") {
	push @ {$ortho_map{"Dicty"}{$dicty_id}}, $omim_gene_id;
	push @ {$organism_map{$omim_gene_id}}, "Dicty";
	}
	if ($chicken_id ne "") {
	push @ {$ortho_map{"Chicken"}{$chicken_id}}, $omim_gene_id;
	push @ {$organism_map{$omim_gene_id}}, "Chicken";
	}
	if ($zfin_id ne "") {
	push @ {$ortho_map{"Zfin"}{$zfin_id}}, $omim_gene_id;
	push @ {$organism_map{$omim_gene_id}}, "Zfin";
	}
	if ($tair_id ne "") {
	push @ {$ortho_map{"Tair"}{$tair_id}}, $omim_gene_id;
	push @ {$organism_map{$omim_gene_id}}, "Tair";
	}
	if ($yeast_id ne "") {
	push @ {$ortho_map{"Yeast"}{$yeast_id}}, $omim_gene_id;
	push @ {$organism_map{$omim_gene_id}}, "Yeast";
	}
	if ($pombe_id ne "") {
	push @ {$ortho_map{"Pombe"}{$pombe_id}}, $omim_gene_id;
	push @ {$organism_map{$omim_gene_id}}, "Pombe";
	}
	if ($ecoli_id ne "") {
	push @ {$ortho_map{"Ecoli"}{$ecoli_id}}, $omim_gene_id;
	push @ {$organism_map{$omim_gene_id}}, "Ecoli";
	}
	#for $in_list (@ {$organism_map{$omim_gene_id}}) {
	#print "$in_list\n"
	#}
    }
}

#########select distinct ids for each omim group, each organism

	foreach $organism (@organism_list) {
	   @organism_ids = keys %{$ortho_map{$organism}};
	foreach $organism_id (@organism_ids) {
	   @omim_ids = @{$ortho_map{$organism}{$organism_id}};
		# select unique objects
		%seen = ();
		foreach $item (@omim_ids) {
    		   $seen{$item}++;
		   }#id loop
		@omim_ids = keys %seen;
	   	@{$ortho_map{$organism}{$organism_id}} = @omim_ids;
	}#organism_id loop
	}#organism loop


#########read in OBO terms
&parse_obo(%nodes);
#print "OBO parsed<BR>";
######### nodes (GO ids) to ancestors
	foreach $query_node (keys %nodes) {
	@ancestor_nodes = ();
	&find_ancestors($query_node);
	}

    #associate with descendant nodes as ancestorss
    foreach $node_id (keys %nodes) {
	@ancestor_nodes = @ { $nodes{$node_id}->{"ancestors"} };

	#register as descendant with each ancestor node
	foreach $ancestor_node (@ancestor_nodes) {
		$descendant_node =  $node_id;
		@descendant_nodes = @ { $nodes{$ancestor_node}->{"descendants"} };
		push @descendant_nodes, $descendant_node;
		$nodes{$ancestor_node}->{"descendants"} = [ @descendant_nodes ];
	}
    }
    
#open (GOSLIM, "goslim_generic.obo") ||print "generic_goslim open error";
#while (<GOSLIM>) {
#	if (/^id: ([A-Z]+:\d+)/) {
#	push @slim_nodes, $1;
#	}
#}

#########start annotation section

#read in human annotations
%human_annotations = &read_GOannotations("Human", "gene_association.goa_human");
#print "human GO parsed<BR>";

#read in mouse annotations
%mouse_annotations = &read_GOannotations("Mouse", "gene_association.mgi");
#print "mouse GO parsed<BR>";

#read in rat annotations
%rat_annotations = &read_GOannotations("Rat", "gene_association.rgd");
#print "rat GO parsed<BR>";

#read in fly annotations
%fly_annotations = &read_GOannotations("Fly", "gene_association.fb");
#print "fly GO parsed<BR>";

#read in chicken annotations
%chicken_annotations = &read_GOannotations("Chicken", "gene_association.goa_chicken");
#print "chicken GO parsed<BR>";

#read in zfin annotations
%zfin_annotations = &read_GOannotations("Zfin", "gene_association.zfin");
#print "zfin GO parsed<BR>";

#read in dicty annotations
%dicty_annotations = &read_GOannotations("Dicty", "gene_association.ddb");
#print "dicty GO parsed<BR>";

#read in worm annotations
%worm_annotations = &read_GOannotations("Worm", "gene_association.wb");
#print "worm GO parsed<BR>";

#read in tair annotations
%tair_annotations = &read_GOannotations("Tair", "gene_association.tair");
#print "tair GO parsed<BR>";

#read in yeast annotations
%yeast_annotations = &read_GOannotations("Yeast", "gene_association.sgd");
#print "yeastGO parsed<BR>";

#read in pombe annotations
%pombe_annotations = &read_GOannotations("Pombe", "gene_association.GeneDB_Spombe");
#print "pombe GO parsed<BR>";

#read in ecoli annotations
#%ecoli_annotations = &read_GOannotations("Ecoli", "gene_association.Ecoli");
#print "ecoli GO parsed<BR>";

#########end annotation section

#########set graph properties
#	&graph_properties(); 

    %color = (
        "Human" => "tomato",
        "Mouse" => "lightblue",
        "Rat" => "gold",
        "Fly" => "darkturquoise",
        "Chicken" => "orchid",
        "Zfin" => "cornflowerblue",
        "Dicty" => "darkorange",
        "Worm" => "goldenrod",
        "Tair" => "yellowgreen",
        "Yeast" => "cyan",
        "Pombe" => "blueviolet",
        "Ecoli" => "palegreen",
        "Multiple" => "lightgray",
	);

    %edge_color = (
        "is_a" => "green",
        "part_of" => "purple",
    );

$lineLimit = 10;

#loop through ortholog sets
    foreach $key (keys %orthologs) {
	@ortholog_set = @ { $orthologs{$key} } ;

$omim_id = $key;
$human_id = $ortholog_set[1];
$mouse_id = $ortholog_set[2];
$rat_id = $ortholog_set[3];
$fly_id = $ortholog_set[4];
$chicken_id = $ortholog_set[5];
$zfin_id = $ortholog_set[6];
$dicty_id = $ortholog_set[7];
$worm_id = $ortholog_set[8];
$tair_id = $ortholog_set[9];
$yeast_id = $ortholog_set[10];
$pombe_id = $ortholog_set[11];
$ecoli_id = $ortholog_set[12];
$gene = $map_id2gene{$key};
@split_genes = split /,/,$gene;
$primary_gene = $split_genes[0];

#########generate dot file for graph
##	clear annotation info
%node_printed = ();
foreach $query_node (keys %nodes) {
	$nodes{$query_node}->{"annotation"} = "simple";
}

$omim_list = &print_dot($omim_id);
$annotation_list{$omim_id} = $omim_list;
#print "in main routine: $omim_id, $primary_gene, $annotation_list{$omim_id}<BR>\n";

# now that the DOT file is created ...
`dot -Tpng temp.dot -o $omim_id.png`;	# create the png image

#########generate svg file from dot file
my @svgimg = `dot -Tsvg temp.dot`;	# create the svg

#########fix font-size parameter value in svg file for firefox
open FIXSVG, ">$omim_id.svg";
for my $svgline (@svgimg) {
	$svgline =~ s/font-size:10.00/font-size:10px/;
	print FIXSVG $svgline;
    }

##########setup the web page
open WEBPAGE, ">$omim_id.html";
print WEBPAGE <<HTML;
<HTML>
    <HEAD>
	<TITLE>Compare GO annotations for $primary_gene and its Reference Genomes orthologs</TITLE>
    </HEAD>
    <BODY>
	<H3>Compare GO annotations for $primary_gene and its orthologs</H3>
    A <a href="#Slim">summary/comparison table of GO categories</a> is provided below.<br>
    A <a href="#Annotations">table of the annotations</a> represented in this image is provided below.
<ul>
<li>Annotations are indicated by colored nodes: 
<span class="style1" style="background-color:#FF6347">human</span>, 
<span class="style1" style="background-color:#C0D9D9">mouse</span>, 
<span class="style1" style="background-color:#FFD700">rat</span>, 
<span class="style1" style="background-color:#DAA520">worm</span>, 
<span class="style1" style="background-color:#00CED1">fly</span>, 
<span class="style1" style="background-color:#FF8C00">dicty</span>,
<span class="style1" style="background-color:#DB70DB">chicken</span>, 
<span class="style1" style="background-color:#6495ED">zfin</span>,
<span class="style1" style="background-color:#9ACD32">tair</span>, 
<span class="style1" style="background-color:#00FFFF">yeast</span>, 
<span class="style1" style="background-color:#8A2BE2">pombe</span>,
<span class="style1" style="background-color:#98FB98">ecoli</span>.</li>
<li>Green arrows indicate "is_a"; Purple arrows indicate "part_of"</li>
<li>GO annotations: Mouse from <A HREF="http://informatics.jax.org">MGI</A>; Human  from <A HREF="http://www.ebi.ac.uk/GOA/HUMAN_release.html">GOA</A>; Rat from <A HREF="http://rgd.mcw.edu/">RGD</A>; Chicken from <A HREF="http://www.ebi.ac.uk/GOA/CHICKEN_release.html">GOA</A>; Fly from <A HREF="http://www.flybase.org/">Flybase</A>; Worm from <A HREF="http://www.wormbase.org/">Wormbase</A>; Dicty from <A HREF="http://dictybase.org/">Dictybase</A>; Yeast from <A HREF="http://db.yeastgenome.org/">SGD</A>; Zfin from <A HREF="http://zfin.org/">Zfin</A>; Tair from <A HREF="http://www.arabidopsis.org/">TAIR</A>; Pombe from <A HREF="http://www.genedb.org/">Sanger</A>; Ecoli from <A HREF="http://ecoliwiki.net/">EcoliWiki (coming soon)</A>.
</li>
<li>Organisms with experimental annotations are indicated: IDA, IEP, IGI, IMP, IPI. In addition, ISS annotations are so indicated.</li>
<li>Does not include evidence codes: IC, IEA, IGC, NAS, ND, NR, RCA, TAS. </li>
<li>Graph is <A HREF="$omim_id.svg">also available as SVG</A> (requires <a href="http://www.adobe.com/svg/viewer/install/main.html">viewer plug-in from Adobe</a>)
<li><a href="#EvidenceCodes">Evidence codes</A> are listed at the bottom of the page. </li>
<li>Graph generated $time_date. </li>
</ul>

	<BR>
	<IMG SRC="$omim_id.png" BORDER="1">
HTML

#####print annotation table
	# select unique objects
	%seen = ();
	foreach $item (keys %node_printed) {
	   $seen{$item}++;
	   }#id loop
	@slim_nodes = keys %seen;

&print_slimtable($omim_id);
&print_table($omim_id);
} #end loop through ortholog triples

#########print gene index page
print "<HTML><HEAD><TITLE>Reference Genomes comparison graphs </TITLE></HEAD><BODY>\n";

open (OBO, "gene_ontology.obo") ||print "ontology open error";
print "<B>Gene ontology: \n";
while (<OBO>) {
if (/(date:.*)/) {print $1, " ";}
if (/ version: \$(.*)\$/) {print $1, " ";}
}
close (OBO);
print "<BR>Column entries in the table below indicate that the organism has an ortholog to the OMIM gene.\n";
print "<BR>Asterisks indicate that the ortholog has GO annotation.\n";
print "<BR>\n";

print "Locate OMIM gene symbol using this index<BR>\n";

###
#	sort omim_index alpha then numeric
sub alpha_first()
{
	($a =~ /\d+/ and $b =~ /\d+/) ? $a <=> $b : (($a =~ /\d+/) ? $b cmp $a : ($b =~ /\d+/) ? $b cmp $a :  $a cmp $b)
}
###

for $key (sort alpha_first keys %omim_index) {
    print "<A HREF=\"#$key\">" . uc($key) . "</A> ";
}

print "</B><BR><BR>\n";

for $key (sort alpha_first keys %omim_index) {
    print "<A NAME=\"$key\"><B>" . uc($key) . "</B></A>\n";
    print "<TABLE BORDER=\"1\">\n";
    print "<TR><TH>OMIM Symbol(s)</TH><TH>OMIM_id</TH><TH>ComparisonGraph</TH><TH>Human</TH><TH>Mouse</TH><TH>Rat</TH><TH>Worm</TH><TH>Fly</TH><TH>Dicty</TH><TH>Chicken</TH><TH>Zfin</TH><TH>Tair</TH><TH>Yeast</TH><TH>Pombe</TH><TH>Ecoli</TH></TR>\n";

		# select unique objects
		%seen = ();
		@distinct_omim = @{$omim_index{$key}};
		foreach $item (@distinct_omim) {
    			$seen{$item}++;
			}
		@distinct_omim = keys %seen;

    @sorted = sort @distinct_omim;
    foreach $gene (@sorted) {
	
	@split_genes = split /,/,$gene;
	$primary_gene = $split_genes[0];
	$omim_id = $map_gene2id{$gene};
	
	print "<TR>\n";
	print "<TD>$gene</TD>";
	print "<TD><A HREF=\"http://www.ncbi.nlm.nih.gov/entrez/dispomim.cgi?id=" . $omim_id . "\">$omim_id</A></TD>";
	print "<TD><A HREF=\"$omim_id.html\">Graph: $primary_gene</A></TD>";
	
	
	#indicate ortholog has annotation
	@org_annot = split(/ /,$annotation_list{$omim_id}) ;
	
	#indicate which organisms have orthologs
	for $organism (@organism_list) {
	#indicate which orthologs have annotation
	if ((grep /$organism/, (@org_annot)) > 0) {
	print "<TD>*";
	}
	else {
	print "<TD>";
	}
	#indicate organism has ortholog
	if ((grep /$organism/, (@ {$organism_map{$omim_id}})) > 0) {
	print "$organism";
	}
	else {
	print "X";
	}
	#indicate ortholog has annotation
	if ((grep /$organism/, (@org_annot)) > 0) {
	print "*</TD>";
	}
	else {
	print "</TD>";
	}
	}
	print "</TR>\n";
}
    print "</TABLE>\n";
    print "<BR>\n";
}
print "</BODY></HTML>\n";

exit (0);

############
sub parse_obo
{
    %nodes = ();

    open (OBO, "gene_ontology.obo") ||print "ontology open error";
    while (<OBO>) {

    if (/\[Term\]/) {
    #new term
	&get_node_info($node_id, $node_name, $node_aspect, @parent_nodes, $node_url);
	$term = {
		"name" => $node_name,
		"aspect" => $node_aspect,
		"parents" => [ @parent_nodes ],
		"children" => [ @child_nodes ],
		"ancestors" => [ ],
		"descendents" => [ ],
		"url" => $node_url,
		"annotation" => "simple",
		};

	$nodes{$node_id} = $term;
	} #end term test
     } #end read loop

     close (OBO);

    #associate with child nodes as parents
    foreach $node_id (keys %nodes) {
	@parent_nodes = @ { $nodes{$node_id}->{"parents"} };

	#register as child with each parent node
	foreach $parent (@parent_nodes) {
		$parent_node = $parent->{"id"};
		$parent_type = $parent->{"type"};
		$child =  {
		      "id" => $node_id,
		      "type" => $parent_type,
		      };
		@child_nodes = @ { $nodes{$parent_node}->{"children"} };
		push @child_nodes, $child;
		$nodes{$parent_node}->{"children"} = [ @child_nodes ];
	}
    }
}

############
sub get_node_info
{
#clear info
    $node_id = "";
    $node_name = "";
    $node_aspect = "";
    $node_url = "";
    @parent_nodes = ();

    while (<OBO>) {
    chomp;
    $term_line = $_;

    #node_id
	if ($term_line =~ /^id: ([A-Z]+:\d+)/) {
	    $node_id = $1;
	    #$node_url = "http://www.informatics.jax.org/searches/GO.cgi?id=" . $node_id;
	    $node_url = "http://amigo.geneontology.org/cgi-bin/amigo/go.cgi?view=details&search_constraint=terms&depth=0&query=" . $node_id;
	    }

    #node name
	if ($term_line =~ /^name: (.*)/) {
	    $node_name = $1;
	    }

    #node aspect
	if ($term_line =~ /^namespace: (.*)/) {
	    $node_aspect = $1;
		if ($node_aspect eq "molecular_function") {
		$node_aspect = "Molecular Function";
		}
		if ($node_aspect eq "biological_process") {
		$node_aspect = "Biological Process";
		}
		if ($node_aspect eq "cellular_component") {
		$node_aspect = "Cellular Component";
		}
	    }
    #is_a
	if ($term_line =~ /^is_a: (\w+:\d+)/) {
	    $parent_id = $1;
	    $parent_type = "is_a";
	    $parent = {
		      "id" => $parent_id,
		      "type" => $parent_type,
		      };
	    push @parent_nodes, $parent;
	    }
    #other relationship
	if ($term_line =~ /^relationship: (\S+) ([A-Z]+:\d+)/) {
	    $parent_id = $2;
	    $parent_type = $1;
	    $parent = {
		      "id" => $parent_id,
		      "type" => $parent_type,
		      };
	    push @parent_nodes, $parent;
	    }
    #end of Term
	if ($term_line eq "") {
	    return();
	    }
    } #end read node
} #end get_node_info routine

#############
sub find_ancestors()
{
	#find parents/ancestors
	&find_parents($query_node);

		# select unique objects
		%seen = ();
    		$seen{$query_node}++;
		foreach $item (@ancestor_nodes) {
    			$seen{$item}++;
			}
		@ancestor_nodes = keys %seen;
	$nodes{$query_node}->{"ancestors"} = [ @ancestor_nodes ];
}

#############
sub find_parents()
{
    local $node_id = shift;
    local @parent_nodes = @ { $nodes{$node_id}->{"parents"} };

    #no more parents (stopping condition)
    if (!@parent_nodes) {
	return();
	}
    #
    foreach $parent (@parent_nodes) {
	$parent_id = $parent->{"id"};
	push @ancestor_nodes, $parent_id;
	&find_parents($parent_id);
	}
} #end find_parents routine

#############
sub read_GOannotations
{
	my ($organism) = $_[0] ;
	my ($file_name) = $_[1] ;
#print "$organism, $file_name<BR>";

#read annotations
%no_duplicates = ();

open (ANNOTATIONS, $file_name) ||print "$organism annotations open error";
%organism_annotations = ();

    while (<ANNOTATIONS>) {

    #check for annotation record
    if (/^(.*)\t(.*)\t(.*)\t(.*)\t(GO:\d+)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(taxon:.*)\t(.*)\t(.*)$/) {
    #
	$new_object = $2;
	$new_symbol = $3;
	$new_not = $4;
	$new_id = $5;
	$new_ref = $6;
	$new_evid = $7;

	if (! ($new_not =~ /NOT/) and ! ($new_evid =~ /(IEA|RCA|NR|ND|NAS|IC|IGC|TAS)/)) {

	    $new_annotation =  {
		"organism" => $organism,
		"object" => $new_object,
		"symbol" => $new_symbol,
		"id" => $new_id,
		"qualifier" => $new_not,
		"reference" => $new_ref,
		"evidence" => $new_evid,
		};

$remove_duplicate = $organism . "," . $new_object . "," . $new_id . "," . $new_not . "," . $new_ref . "," . $new_evid;
#print "$remove_duplicate<BR>";

if (! exists $no_duplicates{$remove_duplicate}) {
	# check for ortholog
	if (exists $ortho_map{$organism}{$new_object}) {

	foreach $omim_gene ( @ {$ortho_map{$organism}{$new_object}} ) {
	   push @ { $organism_annotations{$omim_gene} }, $new_annotation;
	   #print "add annotation to $omim_gene: $remove_duplicate<BR>";

	   }#organism id to gene loop
	   
	}#ortholog exists check
	
	$no_duplicates{$remove_duplicate}++;
	} #duplicate annotation check
	
	} #end NOT check
	} #end annotation test
     } #end read loop

	#foreach $omim_gene ( keys %organism_annotations ) {
	#@check_annotations = @ { $organism_annotations{$omim_gene} };
	#foreach $annotation ( @check_annotations ) {
	#$go_id = $annotation->{'id'};
	#print "verify annotations: $omim_gene, $go_id<BR>";
	#}
	#S}

	return (%organism_annotations) ;

close (ANNOTATIONS);
}

#############

sub print_dot
{
open (DOT_FILE, ">temp.dot") ||print "open dot_file error";

#header for dot file
#with root nodes--order: molecular_function, cellular_component, biological_process

my ($gene) = $map_id2gene{$omim_id};
my (@split_genes) = split /,/,$gene;
my ($primary_gene) = $split_genes[0];

print DOT_FILE <<HTML;
strict digraph $primary_gene {
ranksep=0.25;
node [shape=plaintext fontsize=10.0 fontname="Helvetica"];
{ rank = same; "GO:0003674"; "GO:0005575"; "GO:0008150";};

"GO:0003674"  [label=<
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD PORT="top">molecular_function<BR/>$primary_gene</TD></TR>
</TABLE>>
];

"GO:0005575"  [label=<
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD PORT="top">cellular_component<BR/>$primary_gene</TD></TR>
</TABLE>>
];

"GO:0008150"  [label=<
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD PORT="top">biological_process<BR/>$primary_gene</TD></TR>
</TABLE>>
];

HTML

$node_printed{"GO:0003674"} = 1;
$node_printed{"GO:0005575"} = 1;
$node_printed{"GO:0008150"} = 1;

@hs_annotations = ();
@mm_annotations = ();
@rn_annotations = ();
@ce_annotations = ();
@dm_annotations = ();
@dd_annotations = ();
@gg_annotations = ();
@dr_annotations = ();
@at_annotations = ();
@sc_annotations = ();
@sp_annotations = ();
@ec_annotations = ();

@all_annotations = ();

@hs_go_ids = ();
@mm_go_ids = ();
@rn_go_ids = ();
@ce_go_ids = ();
@dm_go_ids = ();
@dd_go_ids = ();
@gg_go_ids = ();
@dr_go_ids = ();
@at_go_ids = ();
@sc_go_ids = ();
@sp_go_ids = ();
@ec_go_ids = ();


my  ($annotation_list) = "";
my  (%exp_list) = ();

#list all annotations for gene
	   
	if (exists $human_annotations{$omim_id}) {
	@hs_annotations = @ { $human_annotations{$omim_id} };
	foreach $annotation (@hs_annotations) {
	   $node_id =  $annotation->{"id"};
	   
	   $check_evidence =  $annotation->{"evidence"};
	   if ($check_evidence =~ /(IDA|IEP|IGI|IMP|IPI)/) {
	   if (! exists $exp_list{$node_id}) {
	   	$exp_list{$node_id} = "Human ";}
	   }
	   
	   push @hs_go_ids, $node_id;
	   push @all_annotations, $node_id;
	   }
	 $annotation_list = "Human ";
	   }

	if (exists $mouse_annotations{$omim_id}) {
	@mm_annotations = @ { $mouse_annotations{$omim_id} };
	foreach $annotation (@mm_annotations) {
	   $node_id =  $annotation->{"id"};
	   
	   $check_evidence =  $annotation->{"evidence"};
	   if ($check_evidence =~ /(IDA|IEP|IGI|IMP|IPI)/) {
	   if (exists $exp_list{$node_id}) {
	   	$exp_list{$node_id} .= "Mouse ";}
	   else {
	   	$exp_list{$node_id} = "Mouse ";}
	   }
	   
	   push @mm_go_ids, $node_id;
	   push @all_annotations, $node_id;
	   }
	 $annotation_list = $annotation_list . "Mouse ";
	   }
	   
	if (exists $rat_annotations{$omim_id}) {
	@rn_annotations = @ { $rat_annotations{$omim_id} };
	foreach $annotation (@rn_annotations) {
	   $node_id =  $annotation->{"id"};
	   
	   $check_evidence =  $annotation->{"evidence"};
	   if ($check_evidence =~ /(IDA|IEP|IGI|IMP|IPI)/) {
	   if (exists $exp_list{$node_id}) {
	   	$exp_list{$node_id} .= "Rat ";}
	   else {
	   	$exp_list{$node_id} = "Rat ";}
	   }
	   
	   push @rn_go_ids, $node_id;
	   push @all_annotations, $node_id;
	   }
	 $annotation_list = $annotation_list . "Rat ";
	   }
	      
	if (exists $worm_annotations{$omim_id}) {
	@ce_annotations = @ { $worm_annotations{$omim_id} };
	foreach $annotation (@ce_annotations) {
	   $node_id =  $annotation->{"id"};
	   
	   $check_evidence =  $annotation->{"evidence"};
	   if ($check_evidence =~ /(IDA|IEP|IGI|IMP|IPI)/) {
	   if (exists $exp_list{$node_id}) {
	   	$exp_list{$node_id} .= "Worm ";}
	   else {
	   	$exp_list{$node_id} = "Worm ";}
	   }
	   
	   push @ce_go_ids, $node_id;
	   push @all_annotations, $node_id;
	   }
	 $annotation_list = $annotation_list . "Worm ";
	   }
	   
	if (exists $fly_annotations{$omim_id}) {
	@dm_annotations = @ { $fly_annotations{$omim_id} };
	foreach $annotation (@dm_annotations) {
	   $node_id =  $annotation->{"id"};
	   
	   $check_evidence =  $annotation->{"evidence"};
	   if ($check_evidence =~ /(IDA|IEP|IGI|IMP|IPI)/) {
	   if (exists $exp_list{$node_id}) {
	   	$exp_list{$node_id} .= "Fly ";}
	   else {
	   	$exp_list{$node_id} = "Fly ";}
	   }
	   
	   push @dm_go_ids, $node_id;
	   push @all_annotations, $node_id;
	   }
	 $annotation_list = $annotation_list . "Fly ";
	   }
	   
	if (exists $dicty_annotations{$omim_id}) {
	@dd_annotations = @ { $dicty_annotations{$omim_id} };
	foreach $annotation (@dd_annotations) {
	   $node_id =  $annotation->{"id"};
	   
	   $check_evidence =  $annotation->{"evidence"};
	   if ($check_evidence =~ /(IDA|IEP|IGI|IMP|IPI)/) {
	   if (exists $exp_list{$node_id}) {
	   	$exp_list{$node_id} .= "Dicty ";}
	   else {
	   	$exp_list{$node_id} = "Dicty ";}
	   }
	   
	   push @dd_go_ids, $node_id;
	   push @all_annotations, $node_id;
	   }
	 $annotation_list = $annotation_list . "Dicty ";
	   }
	   
	if (exists $chicken_annotations{$omim_id}) {
	@gg_annotations = @ { $chicken_annotations{$omim_id} };
	foreach $annotation (@gg_annotations) {
	   $node_id =  $annotation->{"id"};
	   
	   $check_evidence =  $annotation->{"evidence"};
	   if ($check_evidence =~ /(IDA|IEP|IGI|IMP|IPI)/) {
	   if (exists $exp_list{$node_id}) {
	   	$exp_list{$node_id} .= "Chicken ";}
	   else {
	   	$exp_list{$node_id} = "Chicken ";}
	   }
	   
	   push @gg_go_ids, $node_id;
	   push @all_annotations, $node_id;
	   }
	 $annotation_list = $annotation_list . "Chicken ";
	   }

	if (exists $zfin_annotations{$omim_id}) {
	@dr_annotations = @ { $zfin_annotations{$omim_id} };
	foreach $annotation (@dr_annotations) {
	   $node_id =  $annotation->{"id"};
	   
	   $check_evidence =  $annotation->{"evidence"};
	   if ($check_evidence =~ /(IDA|IEP|IGI|IMP|IPI)/) {
	   if (exists $exp_list{$node_id}) {
	   	$exp_list{$node_id} .= "Zfin ";}
	   else {
	   	$exp_list{$node_id} = "Zfin ";}
	   }
	   
	   push @dr_go_ids, $node_id;
	   push @all_annotations, $node_id;
	   }
	 $annotation_list = $annotation_list . "Zfin ";
	   }
	   
	if (exists $tair_annotations{$omim_id}) {
	@at_annotations = @ { $tair_annotations{$omim_id} };
	foreach $annotation (@at_annotations) {
	   $node_id =  $annotation->{"id"};
	   
	   $check_evidence =  $annotation->{"evidence"};
	   if ($check_evidence =~ /(IDA|IEP|IGI|IMP|IPI)/) {
	   if (exists $exp_list{$node_id}) {
	   	$exp_list{$node_id} .= "Tair ";}
	   else {
	   	$exp_list{$node_id} = "Tair ";}
	   }
	   
	   push @at_go_ids, $node_id;
	   push @all_annotations, $node_id;
	   }
	 $annotation_list = $annotation_list . "Tair ";
	   }

	if (exists $yeast_annotations{$omim_id}) {
	@sc_annotations = @ { $yeast_annotations{$omim_id} };
	foreach $annotation (@sc_annotations) {
	   $node_id =  $annotation->{"id"};
	   
	   $check_evidence =  $annotation->{"evidence"};
	   if ($check_evidence =~ /(IDA|IEP|IGI|IMP|IPI)/) {
	   if (exists $exp_list{$node_id}) {
	   	$exp_list{$node_id} .= "Yeast ";}
	   else {
	   	$exp_list{$node_id} = "Yeast ";}
	   }
	   
	   push @sc_go_ids, $node_id;
	   push @all_annotations, $node_id;
	   }
	 $annotation_list = $annotation_list . "Yeast ";
	   }

	if (exists $pombe_annotations{$omim_id}) {
	@sp_annotations = @ { $pombe_annotations{$omim_id} };
	foreach $annotation (@sp_annotations) {
	   $node_id =  $annotation->{"id"};
	   
	   $check_evidence =  $annotation->{"evidence"};
	   if ($check_evidence =~ /(IDA|IEP|IGI|IMP|IPI)/) {
	   if (exists $exp_list{$node_id}) {
	   	$exp_list{$node_id} .= "Pombe ";}
	   else {
	   	$exp_list{$node_id} = "Pombe ";}
	   }
	   
	   push @sp_go_ids, $node_id;
	   push @all_annotations, $node_id;
	   }
	 $annotation_list = $annotation_list . "Pombe ";
	   }

	if (exists $ecoli_annotations{$omim_id}) {
	@ec_annotations = @ { $ecoli_annotations{$omim_id} };
	foreach $annotation (@ec_annotations) {
	   $node_id =  $annotation->{"id"};
	   
	   $check_evidence =  $annotation->{"evidence"};
	   if ($check_evidence =~ /(IDA|IEP|IGI|IMP|IPI)/) {
	   if (exists $exp_list{$node_id}) {
	   	$exp_list{$node_id} .= "Ecoli ";}
	   else {
	   	$exp_list{$node_id} = "Ecoli ";}
	   }
	   
	   push @ec_go_ids, $node_id;
	   push @all_annotations, $node_id;
	   }
	 $annotation_list = $annotation_list . "Ecoli ";
	   }

		# select unique objects
		%seen = ();
		foreach $item (@all_annotations) {
    			$seen{$item}++;
			}
		@all_annotations = keys %seen;

	foreach $node_id (@all_annotations) {
	   $node_list = "";

	#mark node with annotations

	if ((grep /$node_id/, @mm_go_ids) > 0) {
	 $node_list = "Mouse ";
	 }
	if ((grep /$node_id/, @hs_go_ids) > 0) {
	 $node_list = $node_list . "Human ";
	 }
	if ((grep /$node_id/, @rn_go_ids) > 0) {
	 $node_list = $node_list . "Rat ";
	 }
	if ((grep /$node_id/, @ce_go_ids) > 0) {
	 $node_list = $node_list . "Worm ";
	 }
	if ((grep /$node_id/, @dm_go_ids) > 0) {
	 $node_list = $node_list . "Fly ";
	 }
	if ((grep /$node_id/, @dd_go_ids) > 0) {
	 $node_list = $node_list . "Dicty ";
	 }
	if ((grep /$node_id/, @gg_go_ids) > 0) {
	 $node_list = $node_list . "Chicken ";
	 }
	if ((grep /$node_id/, @dr_go_ids) > 0) {
	 $node_list = $node_list . "Zfin ";
	 }
	if ((grep /$node_id/, @at_go_ids) > 0) {
	 $node_list = $node_list . "Tair ";
	 }
	if ((grep /$node_id/, @sc_go_ids) > 0) {
	 $node_list = $node_list . "Yeast ";
	 }
	if ((grep /$node_id/, @sp_go_ids) > 0) {
	 $node_list = $node_list . "Pombe ";
	 }
	if ((grep /$node_id/, @ec_go_ids) > 0) {
	 $node_list = $node_list . "Ecoli ";
	 }
	
&print_node($node_id, $node_list, $exp_list{$node_id});
$node_printed{$node_id} = 1;
}

foreach $node_id (@all_annotations) {
#print parents
&print_parents($node_id);
}

%edges_printed = ();
foreach $node_id (@all_annotations) {
#print parent edges
&print_parent_edges($node_id);
}

#end of dot file
print DOT_FILE "}\n";

close (DOT_FILE);
#print "in print_dot: $omim_id, $primary_gene, $annotation_list<BR>\n";
return $annotation_list;

}	#end print routine

############
sub print_node()
{
	my ($node_id) = $_[0];
	my ($node_list) = $_[1] ;
	my ($exp_list) = $_[2] ;
	 
	$node_name = $nodes{$node_id}->{"name"};
	$wrapped_name = &WrapIt($node_name, $lineLimit);

	chop ($node_list);
	if (exists $color{$node_list}) {
		$node_color = $color{$node_list};
		$org_ev = $node_list;
		if (index($exp_list, $node_list) < 0) {$org_ev .= ": ISS_only";}
print DOT_FILE <<HTML;
"$node_id"  [label=<
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD PORT="top" BGCOLOR="$node_color">$wrapped_name<BR/>($org_ev)</TD></TR>
</TABLE>>
];
HTML
}
	else {
@org_list = split(/ /,$node_list) ;
print DOT_FILE <<HTML;
"$node_id" [label=<
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD PORT="top" BGCOLOR="white">$wrapped_name</TD></TR>
HTML

$n_org = @org_list;
for ($i=0; $i<($n_org-1); $i++) {
$org = $org_list[$i];
$org_ev = $org;
if (index($exp_list, $org) < 0) {$org_ev .= ": ISS_only";}
print DOT_FILE <<HTML;
<TR><TD BGCOLOR="$color{$org}">($org_ev)</TD></TR>
HTML
}

$org = $org_list[$n_org-1];
$org_ev = $org;
if (index($exp_list, $org) < 0) {$org_ev .= ": ISS_only";}
print DOT_FILE "<TR><TD PORT=\"bottom\" BGCOLOR=\"$color{$org}\">($org_ev)</TD></TR>\n</TABLE>>\n];\n";
$nodes{$node_id}->{"annotation"} = "multi";
}

}

#############
sub print_parents
{
    local $node_id = shift;
    if (! $node_printed{$node_id} == 1) {
	 
	$node_name = $nodes{$node_id}->{"name"};
	$wrapped_name = &WrapIt($node_name, $lineLimit);
	
print DOT_FILE <<HTML;
"$node_id" [label=<
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD PORT="top" BGCOLOR="white">$wrapped_name</TD></TR>
</TABLE>>
];
HTML
$node_printed{$node_id} = 1;
	}
	
    local @parent_nodes = @ { $nodes{$node_id}->{"parents"} };
    #no more parents (stopping condition)
    if (!@parent_nodes) {
	return();
	}
    #
    foreach $parent (@parent_nodes) {
	$parent_id = $parent->{"id"};
	&print_parents($parent_id);
	}

} #end print_parents routine

#############
sub print_parent_edges
{
    local $node_id = shift;
    if (! $edges_printed{$node_id} == 1) {
	
    local @parent_nodes = @ { $nodes{$node_id}->{"parents"} };
    #no more parents (stopping condition)
    if (!@parent_nodes) {
	return();
	}
    #
    foreach $parent (@parent_nodes) {
	$parent_id = $parent->{"id"};
	$parent_type = $parent->{"type"};
	$this_edge_color = $edge_color{$parent_type};

	$parent_place = ":top";
    	if ($nodes{$parent_id}->{"annotation"} eq "multi") {$parent_place = ":bottom";}

	print DOT_FILE "\"$parent_id\"$parent_place -> \"$node_id\":top [color=$this_edge_color, dir=back];\n";
	&print_parent_edges($parent_id);
	}
	
	$edges_printed{$node_id} = 1;
	}

} #end print_parent_edges routine


#############

sub print_table
{
print WEBPAGE <<HTML;
<BR><B><a name="Annotations"></a>Full set of experimental (and ISS) annotations for $primary_gene</B><BR>
<TABLE BORDER="1">
<TR>
<TH>Category</TH>
<TH>ID</TH>
<TH>Term</TH>
<TH>DB Object</TH>
<TH>Evidence</TH>
<TH>Organism</TH>
<TH>Reference</TH>
</TR>
HTML

@all_annotations = ();

#list all annotations for gene

	if (exists $human_annotations{$omim_id}) {
	@hs_annotations = @ { $human_annotations{$omim_id} };
	foreach $annotation (@hs_annotations) {
	   push @all_annotations, $annotation;
	   }
	   }
	   
	if (exists $mouse_annotations{$omim_id}) {
	@mm_annotations = @ { $mouse_annotations{$omim_id} };
	foreach $annotation (@mm_annotations) {
	   push @all_annotations, $annotation;
	   }
	   }

	if (exists $rat_annotations{$omim_id}) {
	@rn_annotations = @ { $rat_annotations{$omim_id} };
	foreach $annotation (@rn_annotations) {
	   push @all_annotations, $annotation;
	   }
	   }
	   
	if (exists $worm_annotations{$omim_id}) {
	@ce_annotations = @ { $worm_annotations{$omim_id} };
	foreach $annotation (@ce_annotations) {
	   push @all_annotations, $annotation;
	   }
	   }

	if (exists $fly_annotations{$omim_id}) {
	@dm_annotations = @ { $fly_annotations{$omim_id} };
	foreach $annotation (@dm_annotations) {
	   push @all_annotations, $annotation;
	   }
	   }

	if (exists $dicty_annotations{$omim_id}) {
	@dd_annotations = @ { $dicty_annotations{$omim_id} };
	foreach $annotation (@dd_annotations) {
	   push @all_annotations, $annotation;
	   }
	   }
	   
	if (exists $chicken_annotations{$omim_id}) {
	@gg_annotations = @ { $chicken_annotations{$omim_id} };
	foreach $annotation (@gg_annotations) {
	   push @all_annotations, $annotation;
	   }
	   }

	if (exists $zfin_annotations{$omim_id}) {
	@dr_annotations = @ { $zfin_annotations{$omim_id} };
	foreach $annotation (@dr_annotations) {
	   push @all_annotations, $annotation;
	   }
	   }
	   
	if (exists $tair_annotations{$omim_id}) {
	@at_annotations = @ { $tair_annotations{$omim_id} };
	foreach $annotation (@at_annotations) {
	   push @all_annotations, $annotation;
	   }
	   }

	if (exists $yeast_annotations{$omim_id}) {
	@sc_annotations = @ { $yeast_annotations{$omim_id} };
	foreach $annotation (@sc_annotations) {
	   push @all_annotations, $annotation;
	   }
	   }

	if (exists $pombe_annotations{$omim_id}) {
	@sp_annotations = @ { $pombe_annotations{$omim_id} };
	foreach $annotation (@sp_annotations) {
	   push @all_annotations, $annotation;
	   }
	   }

	if (exists $ecoli_annotations{$omim_id}) {
	@ec_annotations = @ { $ecoli_annotations{$omim_id} };
	foreach $annotation (@ec_annotations) {
	   push @all_annotations, $annotation;
	   }
	   }

#############
#	sort annotations by GO name within aspect
sub bygoname()
{
($nodes{$a->{"id"}}->{"aspect"} eq $nodes{$b->{"id"}}->{"aspect"}) ? lc($nodes{$a->{"id"}}->{"name"}) cmp lc($nodes{$b->{"id"}}->{"name"}) : $nodes{$b->{"id"}}->{"aspect"} cmp $nodes{$a->{"id"}}->{"aspect"}
}
#############
    @sorted = sort bygoname @all_annotations;

	foreach $annotation (@sorted) {

	$organism = $annotation->{"organism"};
	$bgcolor = $color{$organism};
	$ref = $annotation->{"reference"};
	if ($ref =~ /PMID:(\d+)/) {
		$ref = "<A HREF=\"http://www.ncbi.nlm.nih.gov/sites/entrez?cmd=Retrieve&db=PubMed&list_uids=$1&dopt=Abstract\">$ref</A>";
		}
	$object = $annotation->{"object"};
	$symbol = $annotation->{"symbol"};
	$node_id = $annotation->{"id"};
	$term = $nodes{$node_id}->{"name"};
	$evidence = $annotation->{"evidence"};
	$aspect = $nodes{$node_id}->{"aspect"};
	$golink = $nodes{$node_id}->{"url"};
	
	$link = &set_link($organism, $object);

	# add a line to the table
	    print WEBPAGE "\t    <TR>\t\t<TD>$aspect</TD>";
	    print WEBPAGE "<TD><A HREF=\"$golink\">$node_id</A></TD><TD>$term</TD>";
	    print WEBPAGE "<TD><A HREF=\"$link\">$symbol</A></TD>";
	    print WEBPAGE "<TD>$evidence</TD><TD BGCOLOR=$bgcolor>$organism</TD>";
	    print WEBPAGE "<TD>$ref</TD>\n";
	    
	}
# finish the page
print WEBPAGE <<HTML;
	</TABLE><BR><B>
    <a name="EvidenceCodes"></a>Gene Ontology Evidence Code Abbreviations:</B><BR>
    <BR>
    <TABLE>
      <TR>
        <TD>&nbsp;&nbsp;<B>IC</B> Inferred by curator</TD>
      </TR>
      <TR>
        <TD>&nbsp;&nbsp;<B>IDA</B> Inferred from direct assay</TD>
      </TR>
      <TR>
        <TD>&nbsp;&nbsp;<B>IEA</B> Inferred from electronic annotation</TD>
      </TR>
      <TR>
        <TD>&nbsp;&nbsp;<B>IEP</B> Inferred from expression pattern</TD>
      </TR>
      <TR>
        <TD>&nbsp;&nbsp;<B>IGC</B> Inferred from genomic context</TD>
      </TR>
      <TR>
        <TD>&nbsp;&nbsp;<B>IGI</B> Inferred from genetic interaction</TD>
      </TR>
      <TR>
        <TD>&nbsp;&nbsp;<B>IMP</B> Inferred from mutant phenotype</TD>
      </TR>
      <TR>
        <TD>&nbsp;&nbsp;<B>IPI</B> Inferred from physical interaction</TD>
      </TR>
      <TR>
        <TD>&nbsp;&nbsp;<B>ISS</B> Inferred from sequence or structural similarity</TD>
      </TR>
      <TR>
        <TD>&nbsp;&nbsp;<B>NAS</B> Non-traceable Author Statement</TD>
      </TR>
      <TR>
        <TD>&nbsp;&nbsp;<B>ND</B> No biological data available</TD>
      </TR>
      <TR>
        <TD>&nbsp;&nbsp;<B>RCA</B> Reviewed computational analysis</TD>
      </TR>
      <TR>
        <TD>&nbsp;&nbsp;<B>TAS</B> Traceable author statement</TD>
      </TR>
      <TR>
        <TD>&nbsp;&nbsp;<B>NR</B> Not recorded (appears in some legacy annotations)</TD>
      </TR>
    </TABLE></B>
</font>
    </BODY>
</HTML>
HTML
}	#end print table routine
##################

##################
sub print_slimtable
{
print WEBPAGE <<HTML;
<BR><BR><B><a name="Slim"></a>Compare annotations to GO categories for $primary_gene.</B>
<BR>Parentheses indicate inferred annotation only.<BR>
<TABLE BORDER="1">
<TR>
<TH>Category</TH>
<TH>ID</TH>
<TH>Term</TH>
<TH>Human</TH>
<TH>Mouse</TH>
<TH>Rat</TH>
<TH>Worm</TH>
<TH>Fly</TH>
<TH>Dicty</TH>
<TH>Chicken</TH>
<TH>Zfin</TH>
<TH>Tair</TH>
<TH>Yeast</TH>
<TH>Pombe</TH>
<TH>Ecoli</TH>
</TR>
HTML

#list all annotations for gene

	if (exists $human_annotations{$omim_id}) {
	@hs_annotations = @ { $human_annotations{$omim_id} };
	   }
	   
	if (exists $mouse_annotations{$omim_id}) {
	@mm_annotations = @ { $mouse_annotations{$omim_id} };
	   }

	if (exists $rat_annotations{$omim_id}) {
	@rn_annotations = @ { $rat_annotations{$omim_id} };
	   }
	   
	if (exists $worm_annotations{$omim_id}) {
	@ce_annotations = @ { $worm_annotations{$omim_id} };
	   }

	if (exists $fly_annotations{$omim_id}) {
	@dm_annotations = @ { $fly_annotations{$omim_id} };
	   }

	if (exists $dicty_annotations{$omim_id}) {
	@dd_annotations = @ { $dicty_annotations{$omim_id} };
	   }
	   
	if (exists $chicken_annotations{$omim_id}) {
	@gg_annotations = @ { $chicken_annotations{$omim_id} };
	   }

	if (exists $zfin_annotations{$omim_id}) {
	@dr_annotations = @ { $zfin_annotations{$omim_id} }
	   }
	   
	if (exists $tair_annotations{$omim_id}) {
	@at_annotations = @ { $tair_annotations{$omim_id} };
	   }

	if (exists $yeast_annotations{$omim_id}) {
	@sc_annotations = @ { $yeast_annotations{$omim_id} };
	   }

	if (exists $pombe_annotations{$omim_id}) {
	@sp_annotations = @ { $pombe_annotations{$omim_id} };
	   }

	if (exists $ecoli_annotations{$omim_id}) {
	@ec_annotations = @ { $ecoli_annotations{$omim_id} };
	   }

#############
#	sort annotations by GO slim name within aspect
sub byslim()
{
($nodes{$a}->{"aspect"} eq $nodes{$b}->{"aspect"}) ? lc($nodes{$a}->{"name"}) cmp lc($nodes{$b}->{"name"}) : $nodes{$b}->{"aspect"} cmp $nodes{$a}->{"aspect"}
}
#############

    @sorted = sort byslim @slim_nodes;

	foreach $node_id (@sorted) {
	$term = $nodes{$node_id}->{"name"};
	$aspect = $nodes{$node_id}->{"aspect"};
	$golink = $nodes{$node_id}->{"url"};
	@descendant_nodes = @ { $nodes{$node_id}->{"descendants"} };
	$node_list = "";

	#create entry in table
	$no_entry = "<TD>X</TD>";

	
	$this_entry = $no_entry;
	if ((grep /$node_id/, @hs_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#FF6347>Human</TD>";
	 }
	else {
	 foreach $descendant_id (@descendant_nodes) {
	 if ((grep /$descendant_id/, @hs_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#FF6347>(Human)</TD>";
	 }
	 }
	 }
	$node_list = $this_entry;
	 
	$this_entry = $no_entry;
	if ((grep /$node_id/, @mm_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#C0D9D9>Mouse</TD>";
	 }
	 else {
	 foreach $descendant_id (@descendant_nodes) {
	 if ((grep /$descendant_id/, @mm_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#C0D9D9>(Mouse)</TD>";
	 }
	 }
	 }
	$node_list = $node_list . $this_entry;
	 
	$this_entry = $no_entry;
	if ((grep /$node_id/, @rn_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#FFD700>Rat</TD>";
	 }
	 else {
	 foreach $descendant_id (@descendant_nodes) {
	 if ((grep /$descendant_id/, @rn_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#FFD700>(Rat)</TD>";
	 }
	 }
	 }
	$node_list = $node_list . $this_entry;
	 
	$this_entry = $no_entry;
	if ((grep /$node_id/, @ce_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#DAA520>Worm</TD>";
	 }
	 else {
	 foreach $descendant_id (@descendant_nodes) {
	 if ((grep /$descendant_id/, @ce_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#DAA520>(Worm)</TD>";
	 }
	 }
	 }
	$node_list = $node_list . $this_entry;
	 
	$this_entry = $no_entry;
	if ((grep /$node_id/, @dm_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#00CED>Fly</TD>";
	 }
	 else {
	 foreach $descendant_id (@descendant_nodes) {
	 if ((grep /$descendant_id/, @dm_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#00CED>(Fly)</TD>";
	 }
	 }
	 }
	$node_list = $node_list . $this_entry;
	 
	$this_entry = $no_entry;
	if ((grep /$node_id/, @dd_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#FF8C0>Dicty</TD>";
	 }
	 else {
	 foreach $descendant_id (@descendant_nodes) {
	 if ((grep /$descendant_id/, @dd_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#FF8C0>(Dicty)</TD>";
	 }
	 }
	 }
	$node_list = $node_list . $this_entry;
	 
	$this_entry = $no_entry;
	if ((grep /$node_id/, @gg_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#DB70DB>Chicken</TD>";
	 }
	 else {
	 foreach $descendant_id (@descendant_nodes) {
	 if ((grep /$descendant_id/, @gg_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#DB70DB>(Chicken)</TD>";
	 }
	 }
	 }
	$node_list = $node_list . $this_entry;
	 
	$this_entry = $no_entry;
	if ((grep /$node_id/, @dr_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#6495ED>Zfin</TD>";
	 }
	 else {
	 foreach $descendant_id (@descendant_nodes) {
	 if ((grep /$descendant_id/, @dr_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#6495ED>(Zfin)</TD>";
	 }
	 }
	 }
	$node_list = $node_list . $this_entry;
	 
	$this_entry = $no_entry;
	if ((grep /$node_id/, @at_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#9ACD32>Tair</TD>";
	 }
	 else {
	 foreach $descendant_id (@descendant_nodes) {
	 if ((grep /$descendant_id/, @at_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#9ACD32>(Tair)</TD>";
	 }
	 }
	 }
	$node_list = $node_list . $this_entry;

	$this_entry = $no_entry;
	if ((grep /$node_id/, @sc_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#00FFFF>Yeast</TD>";
	 }
	 else {
	 foreach $descendant_id (@descendant_nodes) {
	 if ((grep /$descendant_id/, @sc_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#00FFFF>(Yeast)</TD>";
	 }
	 }
	 }
	$node_list = $node_list . $this_entry;
	 
	$this_entry = $no_entry;
	if ((grep /$node_id/, @sp_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#8A2BE2>Pombe</TD>";
	 }
	 else {
	 foreach $descendant_id (@descendant_nodes) {
	 if ((grep /$descendant_id/, @sp_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#8A2BE2>(Pombe)</TD>";
	 }
	 }
	 }
	$node_list = $node_list . $this_entry;
	 
	$this_entry = $no_entry;
	if ((grep /$node_id/, @ec_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#98FB98>Ecoli</TD>";
	 }
	 else {
	 foreach $descendant_id (@descendant_nodes) {
	 if ((grep /$descendant_id/, @ec_go_ids) > 0) {
	 $this_entry = "<TD BGCOLOR=#98FB98>(Ecoli)</TD>";
	 }
	 }
	 }
	$node_list = $node_list . $this_entry;
	 
	if ($node_list =~ /(Human|Mouse|Rat|Worm|Fly|Dicty|Chicken|Zfin|Tair|Yeast|Pombe|Ecoli)/) {
	 print WEBPAGE "<TR><TD>$aspect</TD><TD><A HREF=\"$golink\">$node_id</A></TD><TD>$term</TD>$node_list</TR>\n";
	}
    
	}
print WEBPAGE <<HTML;
</TABLE><BR>
HTML
}#end print slim table

#############
sub set_link
{
	my ($organism) = $_[0] ;
	my ($object_id) = $_[1] ;
	
	if ($organism eq "Mouse") {
	$link = "http://www.informatics.jax.org/searches/accession_report.cgi?id=" . $object_id;
	}

	elsif ($organism eq "Human") {
	$link = "http://www.ebi.ac.uk/ego/GSearch?query=" . $object_id . "&mode=protein&ontology=all_ont";
	}

	elsif ($organism eq "Rat") {
	$link = "http://rgd.mcw.edu/tools/query/query.cgi?id=" . $object_id;
	}

	elsif ($organism eq "Chicken") {
	$link = "http://www.ebi.ac.uk/ego/GSearch?query=" . $object_id . "&mode=protein&ontology=all_ont";
	}

	elsif ($organism eq "Fly") {
	$link = "http://www.flybase.org/.bin/fbidq.html?" . $object_id;
	}

	elsif ($organism eq "Worm") {
	$link = "http://www.wormbase.org/db/gene/gene?name=" . $object_id . ";class=Gene";
	}

	elsif ($organism eq "Dicty") {
	$link = "http://dictybase.org/db/cgi-bin/gene_page.pl?dictybaseid=" . $object_id;
	}

	elsif ($organism eq "Yeast") {
	$link = "http://db.yeastgenome.org/cgi-bin/locus.pl?locus=" . $object_id;
	}

	elsif ($organism eq "Zfin") {
	$link = "http://zfin.org/cgi-bin/ZFIN_jump?record=" . $object_id;
	}

	elsif ($organism eq "Tair") {
	$link = "http://www.arabidopsis.org/servlets/TairObject?accession=" . $object_id;
	}

	elsif ($organism eq "Pombe") {
	$link = "http://www.genedb.org/genedb/Search?submit=Search+for&name=" . $object_id . "&organism=pombe";
	}

	elsif ($organism eq "Ecoli") {
	$link = "" . $object_id . "";
	}
	
return $link;
}

#############
sub WrapIt
{
	my (@wordlist) = split(/ /,$_[0]) ;
	my ($wraplimit) = $_[1] ;
	my ($currentline) = "" ;
	my ($wrapoutput) = "" ;
	my ($hanging) = 0 ;
		
	while (@wordlist) {
		$currentline = $currentline.shift(@wordlist) ;
		if (length($currentline) > $wraplimit) {
			if (length($wrapoutput) > 0) { 
				$wrapoutput = $wrapoutput."<BR/>".$currentline ;
				$currentline = "";
				$hanging = 0 ;
				next ; 
				}
			else {
				$wrapoutput = $currentline ;  #if it is the first line
				$currentline = "";
				$hanging = 0 ;
				next ;
				} ;
			}
			
		else {
			$currentline = $currentline." " ;
			$hanging = 1 ;
		    } ;
		} ;
		
	if ($hanging) { 
		chop($currentline) ;             #completes the line and removes the trailing space
		if (length($wrapoutput) > 0) {
			$wrapoutput = $wrapoutput."<BR/>".$currentline ; 
			}
		else {
			$wrapoutput = $currentline ;
			} ;
		}
	else { 
		$wrapoutput = $wrapoutput.$currentline ; }
		;
	
	return ($wrapoutput) ;
}


__END__
