Extensions.pm 7.73 KB
Newer Older
Jessica Severin's avatar
Jessica Severin committed
1
2
3
4
5
6
7
8
#
# You may distribute this module under the same terms as perl itself
#
# POD documentation - main docs before the code

=pod 

=head1 NAME
Jessica Severin's avatar
Jessica Severin committed
9

10
  Bio::EnsEMBL::Hive::Extensions
Jessica Severin's avatar
Jessica Severin committed
11

Jessica Severin's avatar
Jessica Severin committed
12
13
14
=cut

=head1 SYNOPSIS
Jessica Severin's avatar
Jessica Severin committed
15

16
  Object categories to extend the functionality of existing classes
Jessica Severin's avatar
Jessica Severin committed
17

Jessica Severin's avatar
Jessica Severin committed
18
19
20
=cut

=head1 DESCRIPTION
Jessica Severin's avatar
Jessica Severin committed
21

Jessica Severin's avatar
Jessica Severin committed
22
23
24
=cut

=head1 CONTACT
Jessica Severin's avatar
Jessica Severin committed
25

26
27
  Contact Jessica Severin on EnsEMBL::Hive implemetation/design detail: jessica@ebi.ac.uk
  Contact Ewan Birney on EnsEMBL in general: birney@sanger.ac.uk
Jessica Severin's avatar
Jessica Severin committed
28

Jessica Severin's avatar
Jessica Severin committed
29
30
31
=cut

=head1 APPENDIX
Jessica Severin's avatar
Jessica Severin committed
32

33
34
  The rest of the documentation details each of the object methods. 
  Internal methods are usually preceded with a _
Jessica Severin's avatar
Jessica Severin committed
35

Jessica Severin's avatar
Jessica Severin committed
36
37
38
39
=cut

use strict;

Jessica Severin's avatar
Jessica Severin committed
40
use Bio::EnsEMBL::Utils::Exception;
Jessica Severin's avatar
Jessica Severin committed
41
use Bio::EnsEMBL::Analysis;
42
use Bio::EnsEMBL::DBSQL::DBConnection;
Jessica Severin's avatar
Jessica Severin committed
43
use Bio::EnsEMBL::DBSQL::AnalysisAdaptor;
44
use Bio::EnsEMBL::Pipeline::RunnableDB;
45
#use Bio::EnsEMBL::Analysis::RunnableDB;
Jessica Severin's avatar
Jessica Severin committed
46

47

Jessica Severin's avatar
Jessica Severin committed
48
49
=head2 Bio::EnsEMBL::Analysis::runnableDB

50
51
52
53
54
55
  Arg [1]    : none
  Example    : $runnable_db = $analysis->runnableDB;
  Description: from the $analysis->module construct a runnableDB object
  Returntype : Bio::EnsEMBL::Pipeline::RunnableDB
  Exceptions : none
  Caller     : general
Jessica Severin's avatar
Jessica Severin committed
56

57
=cut
Jessica Severin's avatar
Jessica Severin committed
58

Jessica Severin's avatar
Jessica Severin committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
sub Bio::EnsEMBL::Analysis::runnableDB
{
  my $self = shift;  #self is an Analysis object

  return undef unless($self);
  die("self must be a [Bio::EnsEMBL::Analysis] not a [$self]")
    unless($self->isa('Bio::EnsEMBL::Analysis'));

  my $runnable;
  if($self->module =~ "Bio::") { $runnable = $self->module; }
  else { $runnable = "Bio::EnsEMBL::Pipeline::RunnableDB::".$self->module; }
  (my $file = $runnable) =~ s/::/\//g;
  require "$file.pm";
  print STDERR "creating runnable ".$file."\n" if($self->{'verbose'});

Jessica Severin's avatar
Jessica Severin committed
74
75
76
77
78
79
  #make copy of analysis ($self) to pass into the runnableDB
  #to insulate the infrastructure from any modification the runnableDB may
  #do to the analysis object
  my $copy_self = new Bio::EnsEMBL::Analysis;
  %$copy_self = %$self;
  
Jessica Severin's avatar
Jessica Severin committed
80
81
82
83
84
85
86
87
88
89
90
  $runnable =~ s/\//::/g;
  my $runobj = "$runnable"->new(-db       => $self->adaptor->db,
                                -input_id => '1',
                                -analysis => $self,
                                );
  print STDERR "Instantiated ".$runnable." runnabledb\n" if($self->{'verbose'});

  return $runobj
}


Jessica Severin's avatar
Jessica Severin committed
91
92
=head2 Bio::EnsEMBL::DBSQL::DBConnection::url

93
94
95
96
97
  Arg [1]    : none
  Example    : $url = $dbc->url;
  Description: Constructs a URL string for this database connection. Follows
               the format defined for FTP urls and adopted by
               
Jessica Severin's avatar
Jessica Severin committed
98
  Returntype : string of format  mysql://<user>:<pass>@<host>:<port>/<dbname>
99
100
  Exceptions : none
  Caller     : general
Jessica Severin's avatar
Jessica Severin committed
101

102
=cut
Jessica Severin's avatar
Jessica Severin committed
103

104
105
106
sub Bio::EnsEMBL::DBSQL::DBConnection::url
{
  my $self = shift;
107
108
109
110
111
112
113
114
115
  return undef unless($self->host and $self->port and $self->dbname);
  my $url = "mysql://";
  if($self->username) {
    $url .= $self->username;
    $url .= ":".$self->password if($self->password);
    $url .= "@";
  }
  $url .= $self->host .":". $self->port ."/" . $self->dbname;
  return $url;
116
117
118
}


Jessica Severin's avatar
Jessica Severin committed
119
120
=head2 Bio::EnsEMBL::Analysis::url

121
  Arg [1]    : none
Jessica Severin's avatar
Jessica Severin committed
122
  Example    : $url = $dbc->url;
123
124
125
  Description: Constructs a URL string for this database connection
               Follows the general URL rules.
  Returntype : string of format
Jessica Severin's avatar
Jessica Severin committed
126
               mysql://<user>:<pass>@<host>:<port>/<dbname>/analysis?logic_name=<name>
127
128
  Exceptions : none
  Caller     : general
Jessica Severin's avatar
Jessica Severin committed
129

130
=cut
Jessica Severin's avatar
Jessica Severin committed
131

132
133
134
135
136
137
sub Bio::EnsEMBL::Analysis::url
{
  my $self = shift;
  my $url;

  return undef unless($self->adaptor);
138
  $url = $self->adaptor->db->dbc->url;
139
  $url .= "/analysis?logic_name=" . $self->logic_name;
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  return $url;  
}


sub Bio::EnsEMBL::DBSQL::AnalysisAdaptor::fetch_by_url_query
{
  my $self = shift;
  my $query = shift;

  return undef unless($query);
  #print("Bio::EnsEMBL::DBSQL::AnalysisAdaptor::fetch_by_url_query : $query\n");

  if((my $p=index($query, "=")) != -1) {
    my $type = substr($query,0, $p);
    my $value = substr($query,$p+1,length($query));

    if($type eq 'logic_name') {
      return $self->fetch_by_logic_name($value);
    }
    if($type eq 'dbID') {
      return $self->fetch_by_dbID($value);
    }
  }
  return undef;
164
165
}

166

Jessica Severin's avatar
Jessica Severin committed
167
168
169
170
171
172
173
174
175
176
177
178
179
=head2 Bio::EnsEMBL::Analysis::stats

  Arg [1]    : none
  Example    : $stats = $analysis->stats;
  Description: returns the AnalysisStats object associated with this Analysis
               object.  Does not cache, but pull from database by using the
               Analysis objects adaptor->db.
  Returntype : Bio::EnsEMBL::Hive::AnalysisStats object
  Exceptions : none
  Caller     : general

=cut

180
181
182
183
sub Bio::EnsEMBL::Analysis::stats
{
  my $self = shift;
  my $stats = undef;
Jessica Severin's avatar
Jessica Severin committed
184
185
186
187

  #not cached internally since I want it to always be in sync with the database
  #otherwise the user application would need to be aware of the sync state and send
  #explicit 'sync' calls.
Jessica Severin's avatar
Jessica Severin committed
188
  $stats = $self->adaptor->db->get_AnalysisStatsAdaptor->fetch_by_analysis_id($self->dbID);
189
  return $stats;
190
}
Jessica Severin's avatar
Jessica Severin committed
191

192
193
194
195
#######################################
# extensions to
# Bio::EnsEMBL::Pipeline::RunnableDB
#######################################
196
197
198
199
200
201
202

sub Bio::EnsEMBL::Pipeline::RunnableDB::reset_job
{
  my $self = shift;
  return 1;
}

Jessica Severin's avatar
Jessica Severin committed
203
204
205
206
207
208
209
210
211
212
213
=head2 Bio::EnsEMBL::Pipeline::RunnableDB::global_cleanup

  Arg [1]    : none
  Description: method which user RunnableDB can override if it needs to clean up
               any 'global within worker run time' files or data.
  Returntype : 1
  Exceptions : none
  Caller     : Bio::EnsEMBL::Hive::Worker

=cut

214
215
216
217
218
219
sub Bio::EnsEMBL::Pipeline::RunnableDB::global_cleanup
{
  my $self = shift;
  return 1;
}

Jessica Severin's avatar
Jessica Severin committed
220
221
222
223
224
225
226
227
228
229
230
231
=head2 Bio::EnsEMBL::Pipeline::RunnableDB::branch_code

  Arg [1]       : none
  Description   : method which user RunnableDB can override if it needs to return
                  a specific branch code.  Used by the dataflow rules to determine which
                  job to create/run next
  Returntype    : int (default 1)
  Exceptions    : none
  Caller        : Bio::EnsEMBL::Hive::Worker

=cut

232
233
234
235
sub Bio::EnsEMBL::Pipeline::RunnableDB::branch_code
{
  my $self = shift;
  $self->{'_branch_code'} = shift if(@_);
236
  $self->{'_branch_code'}=1 unless($self->{'_branch_code'});
237
238
239
  return $self->{'_branch_code'};
}

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
sub Bio::EnsEMBL::Pipeline::RunnableDB::analysis_job_id
{
  my $self = shift;
  $self->{'_analysis_job_id'} = shift if(@_);
  $self->{'_analysis_job_id'}=0 unless($self->{'_analysis_job_id'});
  return $self->{'_analysis_job_id'};
}

sub Bio::EnsEMBL::Pipeline::RunnableDB::debug {
  my $self = shift;
  $self->{'_debug'} = shift if(@_);
  $self->{'_debug'}=0 unless(defined($self->{'_debug'}));  
  return $self->{'_debug'};
}

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#######################################
# extensions to
# Bio::EnsEMBL::Analysis::RunnableDB
#######################################

sub Bio::EnsEMBL::Analysis::RunnableDB::reset_job
{
  my $self = shift;
  return 1;
}

sub Bio::EnsEMBL::Analysis::RunnableDB::global_cleanup
{
  my $self = shift;
  return 1;
}

sub Bio::EnsEMBL::Analysis::RunnableDB::branch_code
{
  my $self = shift;
  $self->{'_branch_code'} = shift if(@_);
276
  $self->{'_branch_code'}=1 unless($self->{'_branch_code'});
277
278
279
  return $self->{'_branch_code'};
}

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
sub Bio::EnsEMBL::Analysis::RunnableDB::analysis_job_id
{
  my $self = shift;
  $self->{'_analysis_job_id'} = shift if(@_);
  $self->{'_analysis_job_id'}=0 unless($self->{'_analysis_job_id'});
  return $self->{'_analysis_job_id'};
}

sub Bio::EnsEMBL::Analysis::RunnableDB::debug {
  my $self = shift;
  $self->{'_debug'} = shift if(@_);
  $self->{'_debug'}=0 unless(defined($self->{'_debug'}));  
  return $self->{'_debug'};
}

295
296
297
298
#######################################
# top level functions
#######################################

299
sub main::encode_hash
300
301
302
303
304
305
{
  my $hash_ref = shift;

  return "" unless($hash_ref);

  my $hash_string = "{";
306
  my @keys = sort(keys %{$hash_ref});
307
308
309
310
311
312
313
314
  foreach my $key (@keys) {
    $hash_string .= "'$key'=>'" . $hash_ref->{$key} . "',";
  }
  $hash_string .= "}";

  return $hash_string;
}

Jessica Severin's avatar
Jessica Severin committed
315
316
1;