#!/usr/local/bin/perl5 -s
require 5.000;

# Program: MP_terms.pl
#
# Original Author: Mary Dolan
#
# Purpose: Given Mammalian Phenotype ontology in OBO format
#    generate list of MP ids and terms
#
# Usage: perl MP_terms.pl
#

open (GO_TERMS, ">MP_terms.txt");

#########read in OBO terms
&parse_obo(%nodes);

##	

foreach $query_node (keys %nodes) {

##	capture term
	$line = $nodes{$query_node}->{"name"};
print GO_TERMS "$query_node\t$line\n";
}

close (GO_TERMS) || die "GO_TERMS file was not closed\n";
exit (0);

############
sub parse_obo()
{
    %nodes = ();

    open (OBO, "MPheno_OBO.ontology") ||print "ontology open error";
    while (<OBO>) {

    if (/\[Term\]/) {
    #new term
	&get_node_info($node_id, $node_name);
	$term = {
		"name" => $node_name,
		};

	$nodes{$node_id} = $term;
	} #end term test
     } #end read loop

     close (OBO);
}

############
sub get_node_info()
{
#clear info
    $node_id = "";
    $node_name = "";
    $node_url = "";
    @parent_nodes = ();

    while (<OBO>) {
    chomp;
    $term_line = $_;

    #node_id
	if ($term_line =~ /^id: ([A-Z]+:\d+)/) {
	    $node_id = $1;
	    $browser_url = "http://www.informatics.jax.org/searches/GO.cgi?id=" . $node_id;
		$node_id_file = $node_id;
		$node_id_file =~ s/\:/\_/;
	    $graph_url = $node_id_file . "\.svg";
	    }

    #node name
	if ($term_line =~ /^name: (.*)/) {
	    $node_name = $1;
	    }
	    
    #end of Term
	if ($term_line eq "") {
	    return();
	    }
    } #end read node
} #end get_node_info routine


__END__