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

3 4 5
use strict;
use warnings;

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


15
use Getopt::Long qw(:config pass_through no_auto_abbrev);
16 17
use Pod::Usage;

18
use Bio::EnsEMBL::Hive;
19
use Bio::EnsEMBL::Hive::DBSQL::DBAdaptor;
20
use Bio::EnsEMBL::Hive::Utils ('script_usage', 'load_file_or_module');
21 22
use Bio::EnsEMBL::Hive::Utils::Graph;

Leo Gordon's avatar
Leo Gordon committed
23
main();
24 25


26
sub main {
27

Leo Gordon's avatar
Leo Gordon committed
28
    my $self = {};
29

30 31
    GetOptions(
            # connection parameters
32 33
        'url=s'                 => \$self->{'url'},
        'reg_conf|reg_file=s'   => \$self->{'reg_conf'},
34
        'reg_type=s'            => \$self->{'reg_type'},
35 36
        '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
37

38 39
        'pipeconfig|pc=s'       => \$self->{'pipeconfig'},

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

43
        'h|help'                => \$self->{'help'},
44
    );
45

46
    if($self->{'help'}) {
47 48 49
        pod2usage({-exitvalue => 0, -verbose => 2});
    }

50
    if(! $self->{'output'}) {
51 52 53
        pod2usage({
            -message => 'ERROR: No -output flag given',
            -exitvalue => 1,
54
            -verbose => 2
55 56
        });
    }
57
  
58 59 60
    if(!$self->{'format'}) {
        if($self->{'output'}=~/\.(\w+)$/) {
            $self->{'format'} = $1;
61
        } else {
Leo Gordon's avatar
Leo Gordon committed
62
            die "Format was not set and could not guess from ".$self->{'output'}.". Please use either way to select it.\n";
63
        }
Leo Gordon's avatar
Leo Gordon committed
64
    }
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    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'},
        );

        Bio::EnsEMBL::Hive->load_collections_from_dba( $self->{'dba'} );
    }

    if($self->{'pipeconfig'}) {
        my $pipeconfig_package_name = load_file_or_module( $self->{'pipeconfig'} );

        my $pipeconfig_object = $pipeconfig_package_name->new();
82
        $pipeconfig_object->process_options( 0 );
83 84 85 86

        $pipeconfig_object->add_objects_from_config();
    }

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

Leo Gordon's avatar
Leo Gordon committed
90
    my $call = 'as_'.$self->{'format'};
91

92
    eval {$graphviz->$call($self->{'output'});};
93 94 95
    if($@) {
        warn $@;
        pod2usage({
96
            -message => 'Error detected. Check '.$self->{'format'}.' is a valid format. Use a format name as supported by graphviz',
97
            -exitvalue => 1,
98
            -verbose => 2
99 100
        });
    }
101 102 103
}

__END__
104

105 106 107 108
=pod

=head1 NAME

109
    generate_graph.pl
110 111 112

=head1 SYNOPSIS

113
    ./generate_graph.pl -url mysql://user:pass@server:port/dbname -output OUTPUT_LOC [-help]
114 115 116

=head1 DESCRIPTION

117 118 119 120 121
    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.
122 123 124

=head1 OPTIONS

125
B<--url>
126

127
    url defining where hive database is located
128

129
B<--reg_conf>
130

131
    path to a Registry configuration file
132

133
B<--reg_alias>
134

135
    species/alias name for the Hive DBAdaptor
136

137
B<--output>
138

139 140
    Location of the file to write to.
    The file extension (.png , .jpeg , .dot , .gif , .ps) will define the output format.
141

142
=head1 EXTERNAL DEPENDENCIES
143

144
    GraphViz
145

146
=head1 LICENSE
147

148
    Copyright [1999-2014] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
149

150 151
    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
152

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

155 156 157
    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.
158

159
=head1 CONTACT
160

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

Leo Gordon's avatar
Leo Gordon committed
163
=cut
164