| 1 |
# $Id: Dotty.pm,v 1.1 2005/02/11 05:44:56 cmungall Exp $ |
| 2 |
|
| 3 |
package GO::IO::Dotty; |
| 4 |
|
| 5 |
=head1 NAME |
| 6 |
|
| 7 |
GO::IO::Dotty; |
| 8 |
|
| 9 |
=head1 SYNOPSIS |
| 10 |
|
| 11 |
Utility functions for producing Dotty diagrams from GO graphs |
| 12 |
|
| 13 |
Contributed to the go-database list by Philip Lord |
| 14 |
|
| 15 |
=head1 REQUIREMENTS |
| 16 |
|
| 17 |
http://www.research.att.com/sw/tools/graphviz/ |
| 18 |
http://search.cpan.org/search?dist=GraphViz |
| 19 |
|
| 20 |
=cut |
| 21 |
|
| 22 |
use strict; |
| 23 |
use Exporter; |
| 24 |
use GraphViz; |
| 25 |
my @ISA=qw{Exporter}; |
| 26 |
|
| 27 |
my %labellednodes; |
| 28 |
|
| 29 |
sub graphviz_to_dotty{ |
| 30 |
my $graphviz = shift; |
| 31 |
|
| 32 |
open( DOTTY, "|dotty -" ); |
| 33 |
print DOTTY $graphviz->as_text; |
| 34 |
close( DOTTY ); |
| 35 |
} |
| 36 |
|
| 37 |
sub go_graph_to_graphviz { |
| 38 |
my $graph = shift; |
| 39 |
my %gopts = %{shift || {}}; |
| 40 |
my %opts = %{shift || {}}; |
| 41 |
my $it = $graph->create_iterator; |
| 42 |
my %relationships; |
| 43 |
my %labellednodes; |
| 44 |
|
| 45 |
my $graphviz = GraphViz->new(%gopts); |
| 46 |
|
| 47 |
while (my $ni = $it->next_node_instance ){ |
| 48 |
my $term = $ni->term; |
| 49 |
my $rel = $ni->parent_rel; |
| 50 |
my $parents = $graph->get_parent_terms($term->acc); |
| 51 |
_graphviz_label( $term, $graph, |
| 52 |
\%labellednodes, $graphviz, \%opts ); |
| 53 |
|
| 54 |
foreach my $p (@$parents){ |
| 55 |
_graphviz_label( $p, $graph->is_focus_node( $p ), |
| 56 |
\%labellednodes, $graphviz, \%opts ); |
| 57 |
|
| 58 |
my $identifier = $p->acc . " " . $term->acc; |
| 59 |
unless( $relationships{ $identifier } ){ |
| 60 |
## for some reason graphviz assumes that a number is only a |
| 61 |
## label, and not a node name which is irritating. |
| 62 |
my $node = "acc" . $term->acc; |
| 63 |
my $pnode = "acc" . $p->acc; |
| 64 |
my $apph = $term->apph; |
| 65 |
$graphviz->add_edge($node=>$pnode, label=>$rel->type); |
| 66 |
$relationships{ $identifier } = 1; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if ($opts{selected_assocs}) { |
| 71 |
my @prs = map {$_->gene_product } @{$term->selected_association_list || []}; |
| 72 |
my $node = "acc" . $term->acc; |
| 73 |
foreach my $pr (@prs) { |
| 74 |
my $nid = $pr->xref->as_str; |
| 75 |
unless($labellednodes{$nid}) { |
| 76 |
$graphviz->add_node($nid, label=>$pr->symbol, |
| 77 |
color=>'red'); |
| 78 |
$labellednodes{$nid} = 1; |
| 79 |
} |
| 80 |
$graphviz->add_edge( $nid=>$node, label=>"annotated_to"); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
return $graphviz; |
| 85 |
} |
| 86 |
|
| 87 |
sub _graphviz_label{ |
| 88 |
my $term = shift; |
| 89 |
my $graph = shift; |
| 90 |
my $labellednodes = shift; |
| 91 |
my $graphviz = shift; |
| 92 |
my %opts = %{ shift || {}}; |
| 93 |
my $acc = $term->acc; |
| 94 |
my $node = "acc" . $term->acc; |
| 95 |
|
| 96 |
unless( $labellednodes->{$acc} ){ |
| 97 |
if ($opts{sub}) { |
| 98 |
$graphviz->add_node($opts{sub}->($node, $term)); |
| 99 |
} |
| 100 |
else { |
| 101 |
$graphviz->add_node |
| 102 |
( $node, |
| 103 |
label=>$term->name . " " . $term->acc , |
| 104 |
URL=>%opts->{'base_url'}.$term->acc, |
| 105 |
fontname=>'Courier' |
| 106 |
); |
| 107 |
} |
| 108 |
$labellednodes->{$acc} = 1; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
sub label_nodes_with_colour{ |
| 113 |
my $graphviz = shift; |
| 114 |
my $terms = shift; |
| 115 |
my $colour = shift; |
| 116 |
|
| 117 |
foreach my $term (@$terms){ |
| 118 |
my $node = "acc" . $term->public_acc; |
| 119 |
$graphviz->add_node |
| 120 |
( $node, |
| 121 |
style=>"filled", color=>$colour , |
| 122 |
fontname=>'Courier'); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
# support US spelling |
| 127 |
*label_nodes_with_color = \&label_nodes_with_colour; |
| 128 |
|
| 129 |
1; |