generate_graph.pl 3.79 KB
Newer Older
Andy Yates's avatar
Andy Yates committed
1 2
#!/usr/bin/env perl

3 4 5 6 7 8 9 10 11 12 13 14 15 16
package Script;

use strict;
use warnings;

use Getopt::Long;
use Pod::Usage;

use Bio::EnsEMBL::Hive::URLFactory;
use Bio::EnsEMBL::Hive::DBSQL::DBAdaptor;
use Bio::EnsEMBL::Hive::Utils::Graph;

my $self = bless({}, __PACKAGE__);

17
$self->main();
18

19
sub main {
20 21 22 23 24
    my ($self) = @_;

    $self->_options();
    $self->_process_options();
    $self->_write_graph();
25 26 27
}

sub _options {
28 29 30 31 32 33 34 35 36 37 38 39 40
    my ($self) = @_;
    GetOptions(
            # connection parameters
        'reg_conf|regfile=s'    => \$self->{'reg_conf'},
        'reg_alias|regname=s'   => \$self->{'reg_alias'},
        'url=s'                 => \$self->{url},

        'f|format=s'            => \$self->{format},
        'o|output=s'            => \$self->{output},

        'h|help'                => \$self->{help},
        'm|man'                 => \$self->{man},
    );
41 42 43
}

sub _process_options {
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    my ($self) = @_;

    #Check for help
    if($self->{help}) {
        pod2usage({-exitvalue => 0, -verbose => 1});
    }
    if($self->{man}) {
        pod2usage({-exitvalue => 0, -verbose => 2});
    }

    #Check for DB
    if($self->{'reg_conf'} and $self->{'reg_alias'}) {
        Bio::EnsEMBL::Registry->load_all($self->{'reg_conf'});
        $self->{dba} = Bio::EnsEMBL::Registry->get_DBAdaptor($self->{'reg_alias'}, 'hive');
    } elsif($self->{url}) {
        $self->{dba} = Bio::EnsEMBL::Hive::URLFactory->fetch($self->{url}) || die("Unable to connect to $self->{url}\n");
    } else {
        pod2usage({
            -message => 'ERROR: Connection parameters (url or reg_conf+reg_alias) need to be specified',
            -exitvalue => 1,
            -verbose => 1
        });
    }
67
  
68 69 70 71 72 73 74
    if(! $self->{output}) {
        pod2usage({
            -message => 'ERROR: No -output flag given',
            -exitvalue => 1,
            -verbose => 1
        });
    }
75
  
76 77 78 79 80 81
    if(!$self->{format}) {
        if($self->{output}=~/\.(\w+)$/) {
            $self->{format} = $1;
        } else {
            die "Format was not set and could not guess from ".$self->output().". Please use either way to select it.\n";
        }
Leo Gordon's avatar
Leo Gordon committed
82
    }
83 84 85
}

sub _write_graph {
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    my ($self) = @_;

    my $graph = Bio::EnsEMBL::Hive::Utils::Graph->new( $self->{dba} );
    my $graphviz = $graph->build();

    my $call = q{as_}.$self->{format};

    eval {$graphviz->$call($self->{output});};
    if($@) {
        warn $@;
        pod2usage({
            -message => 'Error detected. Check '.$self->{format}.' is a valid format. Use a format name as supported by graphviz',
            -exitvalue => 1,
            -verbose => 1
        });
    }
102 103 104
}

__END__
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
=pod

=head1 NAME

generate_graph.pl

=head1 SYNOPSIS

  ./generate_graph.pl -url mysql://user:pass@server:port/dbname -output OUTPUT_LOC [-format FORMAT ] [-help | -man]

=head1 DESCRIPTION

This program will generate a graphical representation of your hive pipeline. 
This includes visalising the flow of data from the different analyses, blocking
rules & table writers. The graph is also coloured to indicate the stage 
121 122
an analysis is at. The colours & fonts used can be configured via
hive_config.json configuration file.
123 124 125 126 127 128 129

=head1 OPTIONS

=over 8

=item B<--format>

130
    The format of the file output. See FORMATS for more information
131 132 133

=item B<--output>

134
    Location of the file to write to. 
135

136
=item B<-reg_conf>
137

138
    path to a Registry configuration file
139

140
=item B<-reg_alias>
141

142
    species/alias name for the Hive DBAdaptor
143 144 145

=item B<-url> 

146
    url defining where hive database is located
147 148 149 150 151

=back

=head1 FORMATS

152 153 154
    The script supports the same output formats as GraphViz & the accompanying
    Perl module do. However here are a list of common output formats you may
    want to specify (png is the default).
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

=over 8

=item png

=item jpeg

=item dot

=item gif

=item ps

=item ps2

=back

=head1 REQUIREMENTS

=over 8

=item GraphViz

=back

Leo Gordon's avatar
Leo Gordon committed
180
=cut