Extensions.pm 6.76 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
=head1 SYNOPSIS
Jessica Severin's avatar
Jessica Severin committed
13

14
  Object categories to extend the functionality of existing classes
Jessica Severin's avatar
Jessica Severin committed
15

Jessica Severin's avatar
Jessica Severin committed
16
=head1 DESCRIPTION
Jessica Severin's avatar
Jessica Severin committed
17

Jessica Severin's avatar
Jessica Severin committed
18
=head1 CONTACT
Jessica Severin's avatar
Jessica Severin committed
19

20
  Please contact ehive-users@ebi.ac.uk mailing list with questions/suggestions.
Jessica Severin's avatar
Jessica Severin committed
21
22

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

24
25
  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
26

Jessica Severin's avatar
Jessica Severin committed
27
28
=cut

29

Jessica Severin's avatar
Jessica Severin committed
30
31
use strict;

Jessica Severin's avatar
Jessica Severin committed
32
use Bio::EnsEMBL::Utils::Exception;
Jessica Severin's avatar
Jessica Severin committed
33
use Bio::EnsEMBL::Analysis;
34
use Bio::EnsEMBL::DBSQL::DBConnection;
35
use Bio::EnsEMBL::Hive::URLFactory;
36
#use Bio::EnsEMBL::Pipeline::RunnableDB;
37
#use Bio::EnsEMBL::Analysis::RunnableDB;
Jessica Severin's avatar
Jessica Severin committed
38

39

40
=head2 Bio::EnsEMBL::Analysis::process
Jessica Severin's avatar
Jessica Severin committed
41

42
  Arg [1]    : none
43
44
45
  Example    : $process = $analysis->process;
  Description: from the $analysis->module construct a Process object
  Returntype : Bio::EnsEMBL::Hive::Process subclass 
46
47
  Exceptions : none
  Caller     : general
Jessica Severin's avatar
Jessica Severin committed
48

49
=cut
Jessica Severin's avatar
Jessica Severin committed
50

51
52
sub Bio::EnsEMBL::Analysis::process {
    my $self = shift;  #self is an Analysis object
Jessica Severin's avatar
Jessica Severin committed
53
54
55
56
57

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

58
59
60
  throw("analysis ". $self->logic_name . " has an undefined analysis.module")
    unless($self->module);

61
62
63
64
  my $process_class;
  if($self->module =~ /::/) { $process_class = $self->module; }
  else { $process_class = "Bio::EnsEMBL::Pipeline::RunnableDB::".$self->module; }
  (my $file = $process_class) =~ s/::/\//g;
Jessica Severin's avatar
Jessica Severin committed
65
66
67
  require "$file.pm";
  print STDERR "creating runnable ".$file."\n" if($self->{'verbose'});

68
69
70
  $process_class =~ s/\//::/g;
  my $runobj = "$process_class"->new(
                                -db       => $self->adaptor->db,
Jessica Severin's avatar
Jessica Severin committed
71
72
73
                                -input_id => '1',
                                -analysis => $self,
                                );
74
  print STDERR "Instantiated ". $process_class. " runnabledb\n" if($self->{'verbose'});
Jessica Severin's avatar
Jessica Severin committed
75
76
77
78
79

  return $runobj
}


Jessica Severin's avatar
Jessica Severin committed
80
81
=head2 Bio::EnsEMBL::DBSQL::DBConnection::url

82
83
84
85
86
  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
87
  Returntype : string of format  mysql://<user>:<pass>@<host>:<port>/<dbname>
88
89
  Exceptions : none
  Caller     : general
Jessica Severin's avatar
Jessica Severin committed
90

91
=cut
Jessica Severin's avatar
Jessica Severin committed
92

93
94
95
sub Bio::EnsEMBL::DBSQL::DBConnection::url
{
  my $self = shift;
96
97
98
99
100
101
102
103
104
  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;
105
106
107
}


Jessica Severin's avatar
Jessica Severin committed
108
109
=head2 Bio::EnsEMBL::Analysis::url

110
  Arg [1]    : none
Jessica Severin's avatar
Jessica Severin committed
111
  Example    : $url = $dbc->url;
112
113
114
  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
115
               mysql://<user>:<pass>@<host>:<port>/<dbname>/analysis?logic_name=<name>
116
117
  Exceptions : none
  Caller     : general
Jessica Severin's avatar
Jessica Severin committed
118

119
=cut
Jessica Severin's avatar
Jessica Severin committed
120

121
122
123
124
125
126
sub Bio::EnsEMBL::Analysis::url
{
  my $self = shift;
  my $url;

  return undef unless($self->adaptor);
127
  $url = $self->adaptor->db->dbc->url;
128
  $url .= "/analysis?logic_name=" . $self->logic_name;
129
130
131
132
  return $url;  
}


Jessica Severin's avatar
Jessica Severin committed
133
134
135
136
137
138
139
140
141
142
143
144
145
=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

146
147
148
149
sub Bio::EnsEMBL::Analysis::stats
{
  my $self = shift;
  my $stats = undef;
Jessica Severin's avatar
Jessica Severin committed
150
151
152
153

  #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
154
  $stats = $self->adaptor->db->get_AnalysisStatsAdaptor->fetch_by_analysis_id($self->dbID);
155
  return $stats;
156
}
Jessica Severin's avatar
Jessica Severin committed
157

Jan-Hinnerk Vogel's avatar
 
Jan-Hinnerk Vogel committed
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
=head2 Bio::EnsEMBL::Analysis::data

  Arg [1]    : none
  Example    : $stats = $analysis->data;
  Description: returns the analysis data associated with this Analysis
               object. The data is stored in the analysis_data table. 
               Does not cache, but pull from database by using the
               Analysis objects adaptor->db.
  Returntype : String 
  Exceptions : none
  Caller     : general

=cut

sub Bio::EnsEMBL::Analysis::data
{
  my $self = shift;
  my $data = "";

  my $analysis_data_id = eval($self->parameters)->{'analysis_data_id'};  
  unless ( $analysis_data_id) {  
    warning( " analysis_data_id undefined for analysis " .$self->logic_name. " in analysis_data table.") ; 
  }else {  
    $data  = $self->adaptor->db->get_AnalysisDataAdaptor->fetch_by_dbID($analysis_data_id) ; 
  }
  return $data;
}


187
188
189
190
#######################################
# extensions to
# Bio::EnsEMBL::Pipeline::RunnableDB
#######################################
191

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

199
200
201
202
203
#######################################
# extensions to
# Bio::EnsEMBL::Analysis::RunnableDB
#######################################

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

Jan-Hinnerk Vogel's avatar
 
Jan-Hinnerk Vogel committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
sub Bio::EnsEMBL::Analysis::RunnableDB::analyze_tables {
  my $self = shift;  

  my $starttime = time();

  my $gdb = $self->{'comparaDBA'}->get_GenomeDBAdaptor;   

  foreach my $genome_db ( @{$gdb->fetch_all} ) { 
    my $gdb_id = $genome_db->dbID;
    my $species_name = lc($genome_db->name);
    $species_name =~ s/\ /\_/g;
    my $tbl_name = "peptide_align_feature"."_"."$species_name"."_"."$gdb_id";
    # Re-enable the keys before starting the queries
    my $sql = "ALTER TABLE $tbl_name ENABLE KEYS";
  
    print("$sql\n") if ($self->debug);
    my $sth = $self->dbc->prepare($sql);
    $sth->execute();
    $sql = "ANALYZE TABLE $tbl_name";
    $sth = $self->dbc->prepare($sql);
    $sth->execute();
    printf("  %1.3f secs to ANALYZE TABLE\n", (time()-$starttime));
  }
}
 





241
242
243
244
#######################################
# top level functions
#######################################

245
sub main::encode_hash
246
247
248
249
250
251
{
  my $hash_ref = shift;

  return "" unless($hash_ref);

  my $hash_string = "{";
252
  my @keys = sort(keys %{$hash_ref});
253
  foreach my $key (@keys) {
254
255
256
    if(defined($hash_ref->{$key})) {
      $hash_string .= "'$key'=>'" . $hash_ref->{$key} . "',";
    }
257
258
259
260
261
262
  }
  $hash_string .= "}";

  return $hash_string;
}

Jessica Severin's avatar
Jessica Severin committed
263
264
1;