added sol100 and chado cvterm pages to validate_all.t
[sgn.git] / lib / CXGN / Phylo / OrganismTree.pm
bloba03833e2e062ad95e57b35c830211ca270ca4a3c
1 =head1 NAME
3 CXGN::Phylo::OrgnanismTree - an object to handle SGN organism trees
5 =head1 USAGE
7 my $tree = CXGN::Phylo::OrganismTree->new();
8 my $root = $tree->get_root();
9 my $node = $root->add_child();
10 $node->set_name("I'm a child node");
11 $node->set_link("http://solgenomics.net/");
12 my $child_node = $node->add_child();
13 $child_node->set_name("I'm a grand-child node");
14 print $tree->generate_newick();
16 =head1 DESCRIPTION
18 This is a subcass of L<CXGN::Phylo::Tree>
22 =head1 AUTHORS
24 Naama Menda (nm249@cornell.edu)
27 =cut
29 use strict;
32 #use CXGN::Page;
33 #use CXGN::Page::FormattingHelpers qw/tooltipped_text/;
34 use CXGN::DB::DBICFactory;
35 use CXGN::Chado::Organism;
36 use CXGN::Tools::WebImageCache;
37 use CXGN::Phylo::Node;
39 package CXGN::Phylo::OrganismTree;
41 use base qw / CXGN::Phylo::Tree / ;
43 =head2 function new()
45 Synopsis: my $t = CXGN::Phylo::OrganismTree->new($schema)
46 Arguments: $schema object
47 Returns: an instance of a Tree object.
48 Side effects: creates the object and initializes some parameters.
49 Description:
51 =cut
53 sub new {
54 my $class = shift;
55 my $schema = shift || die "NO SCHEMA OBJECT PROVIDED!!\n";
57 my $self = $class->SUPER::new();
59 $self->set_schema($schema);
61 return $self;
66 #######
69 =head2 recursive_children
71 Usage: $self->recursive_children($nodes_hashref, $organism, $node, $is_root)
72 Desc: recursively add child nodes starting from root.
73 Ret: nothing
74 Args: $nodes_hashref (organism_id => CXGN::Chado::Organism), $organism object for your root, $node object for your root, 1 (boolean)
75 Side Effects: sets name, label, link, tooltip for nodes, highlites leaf nodes.
76 Example:
78 =cut
81 sub recursive_children {
82 my $self=shift;
83 my $nodes=shift;
84 my $o= shift; #CXGN::Chado::Organism object
85 my $n = shift; # CXGN::Phylo::Node object
86 my $species_cache=shift;
87 my $is_root=shift;
89 $n->set_name($o->get_species());
90 my $orgkey ="".$o->get_organism_id()."";
91 $n->get_label()->set_link("/chado/organism.pl?organism_id=" . $o->get_organism_id());
92 my $content=$species_cache->get($orgkey);
93 $content=~ s/\?/<br\/>/g;
94 $n->set_tooltip($n->get_name());
95 $n->set_onmouseover("javascript:showPopUp('popup','".$content."','<b>".$o->get_species()."</b>')");
96 $n->set_onmouseout("javascript:hidePopUp('popup');");
97 $n->get_label()->set_name(" ".$o->get_species());
98 $n->get_label()->set_onmouseover("javascript:showPopUp('popup','".$content."','<b>".$o->get_species()."</b>')");
99 $n->get_label()->set_onmouseout("javascript:hidePopUp('popup');");
100 $n->set_species($n->get_name());
101 $n->set_hide_label(0);
102 $n->get_label()->set_hidden(0);
104 my @cl=$n->get_children();
107 my @children = $o->get_direct_children;
108 foreach my $child (@children) {
110 if ( exists( $nodes->{$child->get_organism_id() } ) && defined( $nodes->{$child->get_organism_id()} ) ) {
112 my $new_node=$n->add_child();
113 $self->recursive_children($nodes, $child, $new_node,$species_cache);
116 if ($n->is_leaf() ) { $n->set_hilited(1) ; }
120 =head2 find_recursive_parent
122 Usage: $self->find_recursive_parent($organism, $nodes_hashref)
123 Desc: populate $nodes_hashref (organism_id=> CXGN::Chado::organism) with recursive parent organisms
124 Ret: $nodes_hashref
125 Args: $organism object, $nodes_hashref
126 Side Effects: none
127 Example:
129 =cut
133 sub find_recursive_parent {
134 my $self=shift;
135 my $organism=shift ;
136 my $nodes= shift; # hash ref
138 my $parent = $organism->get_parent;
139 if ($parent) {
140 my $id = $parent->get_organism_id();
142 if (!$nodes->{$id} ) {
143 $nodes->{$id} = $parent ;
144 $self->find_recursive_parent($parent, $nodes);
147 else { return; }
148 return $nodes;
152 =head2 build_tree
154 Usage: $self->build_tree($root_species_name, $species_hashref,$speciesinfo_cache)
155 Desc: builds an organism tree starting from $root with a list of species
156 Ret: a newick representation of the tree
157 Args: $root_species_name, $species_hashref
158 Side Effects: sets tree nodes names and lables, and renders the tree (see L<CXGN::Phylo::Renderer> )
159 calls $tree->generate_newick($root_node, 1)
160 Example:
162 =cut
164 sub build_tree {
166 my $self=shift;
167 my $root= shift; #'Solanaceae';
169 my $species_hash= shift;
170 my $species_cache= shift;
172 my $schema = $self->get_schema();
173 my $root_o = CXGN::Chado::Organism->new_with_species($schema, $root);
174 my $root_o_id = $root_o->get_organism_id();
175 my $organism_link = "/chado/organism.pl?organism_id=";
177 my $nodes=();
180 my $root_node = $self->get_root();#CXGN::Phylo::Node->new();
183 foreach my $s (keys %$species_hash ) {
185 my $o = CXGN::Chado::Organism->new_with_species($schema, $s);
186 if ($o) {
187 my $organism_id = $o->get_organism_id();
188 #if ($organism_id != $species_hash->{$s} ) {
191 $nodes->{$organism_id}=$o;
192 $nodes = $self->find_recursive_parent($o, $nodes);
193 } else {
194 $self->d( "NO ORGANISM FOUND FOR SPECIES $s !!!!!!!!!!!\n\n");
198 $self->recursive_children( $nodes, $nodes->{$root_o_id}, $root_node , $species_cache,1) ;
200 $self->set_show_labels(1);
203 $root_node->set_name($root_o->get_species());
204 $root_node->set_link($organism_link . $root_o_id);
205 $self->set_root($root_node);
207 $self->d( "FOUND organism " . $nodes->{$root_o_id} . " root node: " . $root_node->get_name() . "\n\n");
209 my $newick= $self->generate_newick($root_node, 1);
212 $self->standard_layout();
214 my $renderer = CXGN::Phylo::PNG_tree_renderer->new($self);
216 my $leaf_count= $self->get_leaf_count();
217 my $image_height = $leaf_count*20 > 120 ? $leaf_count*20 : 120 ;
219 $self->get_layout->set_image_height($image_height);
220 $self->get_layout->set_image_width(800);
221 $self->get_layout->set_top_margin(20);
222 $self->set_renderer($renderer);
223 #$tree->get_layout->layout();
224 $self->get_renderer->render();
226 return $newick;
232 ##########
233 return 1##
234 ##########