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

3 4 5 6 7
package Script;

use strict;
use warnings;

8 9 10 11
    # Finding out own path in order to reference own components (including own modules):
use Cwd            ();
use File::Basename ();
BEGIN {
12
    $ENV{'EHIVE_ROOT_DIR'} ||= File::Basename::dirname( File::Basename::dirname( Cwd::realpath($0) ) );
13
    unshift @INC, $ENV{'EHIVE_ROOT_DIR'}.'/modules';
14 15 16
}


17 18 19 20 21 22 23 24
use Getopt::Long;
use Pod::Usage;

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

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

25
$self->main();
26

27
sub main {
28 29 30 31 32
    my ($self) = @_;

    $self->_options();
    $self->_process_options();
    $self->_write_graph();
33 34 35
}

sub _options {
36 37 38
    my ($self) = @_;
    GetOptions(
            # connection parameters
39 40
        'url=s'                 => \$self->{'url'},
        'reg_conf|reg_file=s'   => \$self->{'reg_conf'},
41
        'reg_type=s'            => \$self->{'reg_type'},
42 43
        'reg_alias|reg_name=s'  => \$self->{'reg_alias'},
        'nosqlvc=i'             => \$self->{'nosqlvc'},     # using "=i" instead of "!" for consistency with scripts where it is a propagated option
44

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

48
        'h|help'                => \$self->{'help'},
49
    );
50 51 52
}

sub _process_options {
53 54 55
    my ($self) = @_;

    #Check for help
56
    if($self->{'help'}) {
57 58 59
        pod2usage({-exitvalue => 0, -verbose => 2});
    }

60 61 62 63 64 65 66 67
    if($self->{'url'} or $self->{'reg_alias'}) {
        $self->{'dba'} = Bio::EnsEMBL::Hive::DBSQL::DBAdaptor->new(
            -url                            => $self->{'url'},
            -reg_conf                       => $self->{'reg_conf'},
            -reg_type                       => $self->{'reg_type'},
            -reg_alias                      => $self->{'reg_alias'},
            -no_sql_schema_version_check    => $self->{'nosqlvc'},
        );
68 69 70 71
    } else {
        pod2usage({
            -message => 'ERROR: Connection parameters (url or reg_conf+reg_alias) need to be specified',
            -exitvalue => 1,
72
            -verbose => 2
73 74
        });
    }
75
  
76
    if(! $self->{'output'}) {
77 78 79
        pod2usage({
            -message => 'ERROR: No -output flag given',
            -exitvalue => 1,
80
            -verbose => 2
81 82
        });
    }
83
  
84 85 86
    if(!$self->{'format'}) {
        if($self->{'output'}=~/\.(\w+)$/) {
            $self->{'format'} = $1;
87 88 89
        } 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
90
    }
91 92 93
}

sub _write_graph {
94 95
    my ($self) = @_;

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

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

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

__END__
113

114 115 116 117
=pod

=head1 NAME

118
    generate_graph.pl
119 120 121

=head1 SYNOPSIS

122
    ./generate_graph.pl -url mysql://user:pass@server:port/dbname -output OUTPUT_LOC [-help]
123 124 125

=head1 DESCRIPTION

126 127 128 129 130
    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
    an analysis is at. The colours & fonts used can be configured via
    hive_config.json configuration file.
131 132 133

=head1 OPTIONS

134
B<--url>
135

136
    url defining where hive database is located
137

138
B<--reg_conf>
139

140
    path to a Registry configuration file
141

142
B<--reg_alias>
143

144
    species/alias name for the Hive DBAdaptor
145

146
B<--output>
147

148 149
    Location of the file to write to.
    The file extension (.png , .jpeg , .dot , .gif , .ps) will define the output format.
150

151
=head1 EXTERNAL DEPENDENCIES
152

153
    GraphViz
154

155
=head1 LICENSE
156

157
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Matthieu Muffato's avatar
Matthieu Muffato committed
158
    Copyright [2016-2018] EMBL-European Bioinformatics Institute
159

160 161
    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
162

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

165 166 167
    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.
168

169
=head1 CONTACT
170

171
    Please subscribe to the Hive mailing list:  http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users  to discuss Hive-related questions or to be notified of our updates
172

Leo Gordon's avatar
Leo Gordon committed
173
=cut
174