GraphViz.pm 3.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
=pod 

=head1 NAME

    Bio::EnsEMBL::Hive::Utils::GraphViz

=head1 DESCRIPTION

    An extension of GraphViz that supports nested clusters

=head1 EXTERNAL DEPENDENCIES

    GraphViz

=head1 LICENSE

17
    Copyright [1999-2014] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License
    is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License.

=head1 CONTACT

    Please contact ehive-users@ebi.ac.uk mailing list with questions/suggestions.

=cut
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50


package Bio::EnsEMBL::Hive::Utils::GraphViz;

use strict;
use warnings;
use base ('GraphViz');


sub subgraphs {
    my $self = shift @_;
    if(@_) {
        $self->{_subgraphs} = shift @_;
    }
    return $self->{_subgraphs};
}


51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
sub colour_scheme {
    my $self = shift @_;
    if(@_) {
        $self->{_colour_scheme} = shift @_;
    }
    return $self->{_colour_scheme};
}


sub colour_offset {
    my $self = shift @_;
    if(@_) {
        $self->{_colour_offset} = shift @_;
    }
    return $self->{_colour_offset};
}


69 70 71 72 73 74
sub get_top_clusters {
    my $self = shift @_;

    my $subgraphs = $self->subgraphs();

    my %set = ();
75 76 77
    foreach my $potential_top_cluster (values %$subgraphs) {
        if( $potential_top_cluster and !$subgraphs->{ $potential_top_cluster } ) {  # if it's a valid node not mentioned in the keys, it is a top cluster
            $set{$potential_top_cluster}++;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        }
    }
    return [ keys %set ];
}


sub get_nodes_that_point_at {
    my ($self, $node) = @_;

    my $subgraphs = $self->subgraphs();
    my %set = ();
    while( my ($key,$value) = each %$subgraphs) {
        if($value and ($value eq $node)) {
            $set{$key}++;
        }
    }
    return [ keys %set ];
}


98
sub display_subgraph {
99 100
    my ($self, $cluster_name, $depth) = @_;

101 102
    my $colour_scheme   = $self->colour_scheme();
    my $colour_offset   = $self->colour_offset();
103 104 105 106 107 108

    my $prefix = "\t" x $depth;

    my $text = '';

    $text .= $prefix . "subgraph cluster_${cluster_name} {\n";
109
#     $text .= $prefix . "\tlabel=\"$cluster_name\";\n";
110
    $text .= $prefix . "\tcolorscheme=$colour_scheme;\n";
111
    $text .= $prefix . "\tstyle=filled;\n";
112
    $text .= $prefix . "\tcolor=".($depth+$colour_offset).";\n";
113 114 115 116 117

    foreach my $node_name ( @{ $self->get_nodes_that_point_at( $cluster_name ) } ) {

        $text .= $prefix . "\t${node_name};\n";
        if( @{ $self->get_nodes_that_point_at( $node_name ) } ) {
118
            $text .= $self->display_subgraph( $node_name, $depth+1 );
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        }
    }
    $text .= $prefix . "}\n";

    return $text;
}


sub _as_debug {
    my $self = shift @_;

    my $text = $self->SUPER::_as_debug;

    $text=~s/^}$//m;

    foreach my $node_name ( @{ $self->get_top_clusters() } ) {
135
        $text .= $self->display_subgraph( $node_name, 1);
136 137 138
    }
    $text .= "}\n";

139 140 141 142 143 144
        # GraphViz.pm thinks 'record' is the only shape that allows HTML-like labels,
        # but newer versions of dot allow more freedom, so we patch dot input after generation:
        #
    $text=~s/^(\s+table_.*)"record"/$1"tab"/mg;
    $text=~s/^(\s+analysis_.*)"record"/$1"Mrecord"/mg;

145
        # uncomment the following line to see the final input to dot
146
#    print $text;
147 148 149 150 151 152

    return $text;
}

1;