generate_graph.pl 4.39 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
25
26
27
28
  my ($self) = @_;
  $self->_options();
  $self->_process_options();
  $self->_write_graph();
}

sub _options {
  my ($self) = @_;
  GetOptions(
29
30
31
32
33
34
35
36
    'reg_conf|regfile=s'    => \$self->{'reg_conf'},
    'reg_alias|regname=s'   => \$self->{'reg_alias'},
    'url=s'                 => \$self->{url},
    'host|dbhost=s'         => \$self->{db_conf}->{'-host'},
    'port|dbport=i'         => \$self->{db_conf}->{'-port'},
    'user|dbuser=s'         => \$self->{db_conf}->{'-user'},
    'password|dbpass=s'     => \$self->{db_conf}->{'-pass'},
    'database|dbname=s'     => \$self->{db_conf}->{'-dbname'},
37
    
38
39
    'f|format=s'            => \$self->{format},
    'o|output=s'            => \$self->{output},
40

41
42
    'h|help'                => \$self->{help},
    'm|man'                 => \$self->{man},
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  );
}

sub _process_options {
  my ($self) = @_;
  
  #Check for help
  if($self->{help}) {
    pod2usage({-exitvalue => 0, -verbose => 1});
  }
  if($self->{man}) {
    pod2usage({-exitvalue => 0, -verbose => 2});
  }
  
  #Check for DB
58
59
60
  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');
61
62
63
64
65
66
67
  } 
  elsif($self->{url}) {
    $self->{dba} = Bio::EnsEMBL::Hive::URLFactory->fetch($self->{url}) || die("Unable to connect to $self->{url}\n");
  }
  elsif (    $self->{db_conf}->{'-host'}
          && $self->{db_conf}->{'-user'}
          && $self->{db_conf}->{'-dbname'}) { # connect to database specified
68
    $self->{dba} = Bio::EnsEMBL::Hive::DBSQL::DBAdaptor->new( %{$self->{db_conf}} );
69
70
71
  } 
  else {
    pod2usage({
72
      -message => 'ERROR: Connection parameters (reg_conf+reg_alias, url or dbhost+dbuser+dbname) need to be specified',
73
74
75
76
77
78
79
80
81
82
83
84
85
86
      -exitvalue => 1,
      -verbose => 1
    });
  }
  
  if(! $self->{output}) {
    pod2usage({
      -message => 'ERROR: No -output flag given',
      -exitvalue => 1,
      -verbose => 1
    });
  }
  
  if(!$self->{format}) {
Leo Gordon's avatar
Leo Gordon committed
87
88
89
90
91
    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";
    }
92
93
94
95
96
97
  }
}

sub _write_graph {
  my ($self) = @_;
  
98
  my $graph = Bio::EnsEMBL::Hive::Utils::Graph->new( $self->{dba} );
99
  my $graphviz = $graph->build();
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  
  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
    });
  }
}

__END__
=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 
130
131
an analysis is at. The colours & fonts used can be configured via
hive_config.json configuration file.
132
133
134
135
136
137
138
139
140
141
142
143
144

=head1 OPTIONS

=over 8

=item B<--format>

The format of the file output. See FORMATS for more information

=item B<--output>

Location of the file to write to. 

145
=item B<-reg_conf>
146
147
148

path to a Registry configuration file

149
=item B<-reg_alias>
150
151
152
153
154
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

species/alias name for the Hive DBAdaptor

=item B<-url> 

url defining where hive database is located

=item B<-host>

mysql database host <machine>

=item B<-port> 

mysql port number

=item B<-user>

mysql connection user <name>

=item B<-password>

mysql connection password <pass>

=item B<-database>

mysql database <name>

=back

=head1 FORMATS

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).

=over 8

=item png

=item jpeg

=item dot

=item gif

=item ps

=item ps2

=back

=head1 MAINTAINER

Leo Gordon's avatar
Leo Gordon committed
203
$Author: lg4 $
204
205
206

=head1 VERSION

207
$Revision: 1.10 $
208
209
210
211
212
213
214
215
216

=head1 REQUIREMENTS

=over 8

=item GraphViz

=back

Leo Gordon's avatar
Leo Gordon committed
217
=cut