AnalysisCtrlRuleAdaptor.pm 6.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# Perl module for Bio::EnsEMBL::Hive::DBSQL::AnalysisCtrlRuleAdaptor
#
# Date of creation: 22.03.2004
# Original Creator : Jessica Severin <jessica@ebi.ac.uk>
#
# Copyright EMBL-EBI 2004
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

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

14
15
16
  Bio::EnsEMBL::Hive::DBSQL::AnalysisCtrlRuleAdaptor 

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

18
19
20
21
  $AnalysisCtrlRuleAdaptor = $db_adaptor->get_AnalysisCtrlRuleAdaptor;
  $analysisCtrlRuleAdaptor = $analysisCtrlRuleObj->adaptor;

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

23
24
25
26
  Module to encapsulate all db access for persistent class AnalysisCtrlRule.
  There should be just one per application and database connection.

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

28
29
30
31
  Contact Jessica Severin on implemetation/design detail: jessica@ebi.ac.uk
  Contact Ewan Birney on EnsEMBL in general: birney@sanger.ac.uk

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

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  The rest of the documentation details each of the object methods.
  Internal methods are usually preceded with a _
  
=cut


# Let the code begin...


package Bio::EnsEMBL::Hive::DBSQL::AnalysisCtrlRuleAdaptor;

use strict;
use Carp;
use Bio::EnsEMBL::DBSQL::BaseAdaptor;
use Bio::EnsEMBL::Hive::AnalysisCtrlRule;
48
49
use Bio::EnsEMBL::Utils::Argument;
use Bio::EnsEMBL::Utils::Exception;
50
51
52
53
54

our @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);


=head2 fetch_by_ctrled_analysis_id
Jessica Severin's avatar
Jessica Severin committed
55

56
57
  Arg [1]    : int $id
               the unique database identifier for the feature to be obtained
Jessica Severin's avatar
Jessica Severin committed
58
59
60
61
  Example    : $ctrlRuleArray = $adaptor->fetch_by_ctrled_analysis_id($ctrled_analysis->dbID);
  Description: Returns an array reference of all the AnalysisCtrlRule objects 
               for the specified controled analysis.
  Returntype : listref of Bio::EnsEMBL::Hive::AnalysisCtrlRule objects
62
63
  Exceptions : thrown if $id is not defined
  Caller     : general
Jessica Severin's avatar
Jessica Severin committed
64
  
65
=cut
Jessica Severin's avatar
Jessica Severin committed
66

67
68
69
70
sub fetch_by_ctrled_analysis_id{
  my ($self,$id) = @_;

  unless(defined $id) {
71
    throw("fetch_by_ctrled_analysis_id must have an id");
72
73
74
75
  }

  my $constraint = "r.ctrled_analysis_id = $id";

76
  return $self->_generic_fetch($constraint);
77
78
79
80
}


=head2 fetch_all
Jessica Severin's avatar
Jessica Severin committed
81

82
  Arg        : None
Jessica Severin's avatar
Jessica Severin committed
83
84
85
86
87
88
  Example    : my $all_rules = $ctrlRuleDBA->fetch_all();
  Description: fetches all AnalysisCtrlRule objects from database
  Returntype : array reference of Bio::EnsEMBL::Hive::AnalysisCtrlRule objects
  Exceptions : none
  Caller     : general
  
89
=cut
Jessica Severin's avatar
Jessica Severin committed
90

91
92
93
94
95
96
sub fetch_all {
  my $self = shift;
  return $self->_generic_fetch();
}

=head2 store
Jessica Severin's avatar
Jessica Severin committed
97
98
99
100
101
102
103

  Arg[1]     : Bio::EnsEMBL::Hive::AnalysisCtrlRule object
  Usage      : $self->store( $rule );
  Function   : Stores a rule in db
               Sets adaptor and dbID in AnalysisCtrlRule object
  Returntype : none

104
=cut
Jessica Severin's avatar
Jessica Severin committed
105

106
107
108
109
110
sub store {
  my ( $self, $rule ) = @_;

  #print("\nAnalysisCtrlRuleAdaptor->store()\n");
 
Jessica Severin's avatar
Jessica Severin committed
111
112
  my $sth = $self->prepare(q{INSERT ignore INTO analysis_ctrl_rule 
        (ctrled_analysis_id, condition_analysis_url) VALUES(?,?) });
113
114
115
116
117
118
119
  if($sth->execute($rule->ctrled_analysis_id, $rule->condition_analysis_url)) {
    $sth->finish();
  }
  $rule->adaptor( $self );
}


120
=head2 remove_by_condition_analysis_url
121

122
123
124
  Arg[1]  : string condition_analysis_url
  Usage   : $self->remove_by_condition_analysis_url("ThisAnalysisLogicName");
  Function: removes all the control rules for this condition analysis URL
125
  Returns : -
126

127
=cut
Jessica Severin's avatar
Jessica Severin committed
128

129
130
sub remove_by_condition_analysis_url {
  my ( $self, $condition_analysis_url ) = @_;
131

132
133
  my $sth = $self->prepare("DELETE FROM analysis_ctrl_rule WHERE condition_analysis_url =?");
  $sth->execute($condition_analysis_url);
134
135
}

136

Jessica Severin's avatar
Jessica Severin committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
=head2 create_rule

  Arg[1]      : condition analysis object (Bio::EnsEMBL::Analysis object)
  Arg[2]      : controled analysis object (Bio::EnsEMBL::Analysis object)
  Example     : $dba->get_AnalysisCtrlRuleAdaptor->create_rule($conditionAnalysis, $ctrledAnalysis);
  Description : Creates an AnalysisCtrlRule where the condition analysis must be completely DONE with
                all jobs in order for the controlled analysis to be unblocked and allowed to proceed.
                If an analysis requires multiple conditions, simply create multiple rules and controlled
                analysis will only unblock if ALL conditions are satisified.
  Returntype  : none
  Exceptions  : none
  Caller      : general
  
=cut

152
153
154
155
156
157
158

sub create_rule {
  my ($self, $conditionAnalysis, $ctrledAnalysis) = @_;

  return unless($conditionAnalysis and $ctrledAnalysis);
  
  my $rule = Bio::EnsEMBL::Hive::AnalysisCtrlRule->new();
159
  #must set ctrled_analysis first in order for internal logic to abreviate 'to_url'
160
  $rule->ctrled_analysis($ctrledAnalysis);
161
  $rule->condition_analysis($conditionAnalysis);
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
203
204
205
206
207
208
209
210
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  
  $self->store($rule);
}

############################
#
# INTERNAL METHODS
# (pseudo subclass methods)
#
############################

#internal method used in multiple calls above to build objects from table data

sub _tables {
  my $self = shift;

  return (['analysis_ctrl_rule', 'r']);
}


sub _columns {
  my $self = shift;

  return qw (r.ctrled_analysis_id
             r.condition_analysis_url
            );
}


sub _objs_from_sth {
  my ($self, $sth) = @_;
  my @rules = ();

  my ($ctrled_analysis_id, $condition_analysis_url);
  $sth->bind_columns(\$ctrled_analysis_id, \$condition_analysis_url);

  while ($sth->fetch()) {
    my $rule = Bio::EnsEMBL::Hive::AnalysisCtrlRule->new;
    $rule->adaptor($self);
    $rule->ctrled_analysis_id($ctrled_analysis_id);
    $rule->condition_analysis_url($condition_analysis_url);
    push @rules, $rule;
  }
  return \@rules;
}


sub _default_where_clause {
  my $self = shift;
  return '';
}


sub _final_clause {
  my $self = shift;
  return '';
}

###############################################################################
#
# General access methods that could be moved
# into a superclass
#
###############################################################################

sub _generic_fetch {
  my ($self, $constraint, $join) = @_;

  my @tables = $self->_tables;
  my $columns = join(', ', $self->_columns());

  if ($join) {
    foreach my $single_join (@{$join}) {
      my ($tablename, $condition, $extra_columns) = @{$single_join};
      if ($tablename && $condition) {
        push @tables, $tablename;

        if($constraint) {
          $constraint .= " AND $condition";
        } else {
          $constraint = " $condition";
        }
      }
      if ($extra_columns) {
        $columns .= ", " . join(', ', @{$extra_columns});
      }
    }
  }

  #construct a nice table string like 'table1 t1, table2 t2'
  my $tablenames = join(', ', map({ join(' ', @$_) } @tables));

  my $sql = "SELECT $columns FROM $tablenames";

  my $default_where = $self->_default_where_clause;
  my $final_clause = $self->_final_clause;

  #append a where clause if it was defined
  if($constraint) {
    $sql .= " WHERE $constraint ";
    if($default_where) {
      $sql .= " AND $default_where ";
    }
  } elsif($default_where) {
    $sql .= " WHERE $default_where ";
  }

  #append additional clauses which may have been defined
  $sql .= " $final_clause";

  my $sth = $self->prepare($sql);
  $sth->execute;

#  print STDERR $sql,"\n";

  return $self->_objs_from_sth($sth);
}


1;