AnalysisAdaptor.pm 5.33 KB
Newer Older
1
2
3
4
=pod

=head1 NAME

5
    Bio::EnsEMBL::Hive::DBSQL::AnalysisAdaptor
6
7
8

=head1 SYNOPSIS

9
    $analysis_adaptor = $db_adaptor->get_AnalysisAdaptor;
10

11
    $analysis_adaptor = $analysis_object->adaptor;
12

13
14
=head1 DESCRIPTION

15
16
17
18
19
    Module to encapsulate all db access for persistent class Analysis.
    There should be just one such adaptor per application and database connection.

=head1 LICENSE

20
21
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
    Copyright [2016] EMBL-European Bioinformatics Institute
22
23
24
25
26
27
28
29
30

    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

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

    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.
31
32
33

=head1 CONTACT

34
    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
35
36
37
38
39
40
41

=cut


package Bio::EnsEMBL::Hive::DBSQL::AnalysisAdaptor;

use strict;
42
use warnings;
43
44
use Bio::EnsEMBL::Hive::Analysis;
use Bio::EnsEMBL::Hive::URLFactory;
45
use Bio::EnsEMBL::Hive::Utils ('throw');
46

47
use base ('Bio::EnsEMBL::Hive::DBSQL::ObjectAdaptor');
48

49
50
51
52
53
54
55
56
57
58
59
60
61
62

sub default_table_name {
    return 'analysis_base';
}


sub default_insertion_method {
    return 'INSERT';
}


sub object_class {
    return 'Bio::EnsEMBL::Hive::Analysis';
}
Leo Gordon's avatar
Leo Gordon committed
63

64
65
66
67
68
69
70
71

=head2 fetch_by_logic_name_or_url

    Description: given a URL gets the analysis from URLFactory, otherwise fetches it from the db

=cut

sub fetch_by_logic_name_or_url {
Leo Gordon's avatar
Leo Gordon committed
72
    my $self                = shift @_; # can either be $self or class name
73
74
    my $logic_name_or_url   = shift @_;

75
    if($logic_name_or_url =~ m{^\w*://}) {
Leo Gordon's avatar
Leo Gordon committed
76
        return Bio::EnsEMBL::Hive::URLFactory->fetch($logic_name_or_url, ref($self) && $self->db);
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    } else {
        return $self->fetch_by_logic_name($logic_name_or_url);
    }
}


=head2 fetch_by_url_query

    Description: fetches the analysis either by logic_name or by dbID (either coming from the tail of the URL)

=cut

sub fetch_by_url_query {
    my ($self, $field_name, $field_value) = @_;

    if(!$field_name or !$field_value) {

        return;

    } elsif($field_name eq 'logic_name') {

        return $self->fetch_by_logic_name($field_value);

    } elsif($field_name eq 'dbID') {

        return $self->fetch_by_dbID($field_value);

    }
}


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
=head2 fetch_all_by_pattern

  Arg [1]    : (optional) string $pattern
  Example    : my $first_fifteen_analyses_and_two_more = $analysis_adaptor->fetch_all_by_pattern( '1..15,analysis_X,21' );
  Example    : my $two_open_ranges = $analysis_adaptor->fetch_all_by_pattern( '..7,10..' );
  Example    : my $double_exclusion = $analysis_adaptor->fetch_all_by_pattern( '1..15-3..5+4' );
  Example    : my $blast_related_with_exceptions = $analysis_adaptor->fetch_all_by_pattern( 'blast%-12-%funnel' );
  Description: Fetches an arrayref of non-repeating Analyses objects by interpreting a pattern.
                The pattern can contain individual analyses_ids, individual logic_names,
                open and closed ranges of analysis_ids, wildcard patterns of logic_names,
                merges (+ or ,) and exclusions (-) of the above subsets.
  Exceptions : none
  Caller     : both beekeeper.pl (for scheduling) and runWorker.pl (for specialization)

=cut

124
125
126
127
128
sub fetch_all_by_pattern {
    my ($self, $pattern) = @_;

    my $analyses;

129
130
131
132
    if( not defined($pattern) ) {

        $analyses = $self->fetch_all();

133
    } elsif( $pattern=~/[+\-,]/ ) {     # merging by inclusion/exclusion, and maintaining uniqueness
134

135
136
137
138
139
140
141
        my @syll = split(/([+\-,])/, $pattern);

        my %uniq = map { ($_->dbID => $_) } @{ $self->fetch_all_by_pattern( shift @syll ) };   # initialize with the first syllable

        while(@syll) {
            my $operation   = shift @syll;
            my $subpattern  = shift @syll;
142
143

            foreach my $analysis (@{ $self->fetch_all_by_pattern( $subpattern ) }) {
144
145
146
147
148
149
150
                if($operation eq '-') {
                    delete $uniq{$analysis->dbID};
                } elsif ($operation eq '+' or $operation eq ',') {
                    $uniq{$analysis->dbID} = $analysis;
                } else {
                    throw( "Complex pattern '$pattern' not recognized" );
                }
151
152
153
154
155
            }
        }

        $analyses = [ values %uniq ];

156
    } elsif( $pattern=~/^\d+$/ ) {
157
158
159
160
161
162
163
164
165

        $analyses = $self->fetch_all_by_analysis_id( $pattern );

    } elsif( $pattern=~/^(\d+)\.\.(\d+)$/ ) {

        $analyses = $self->fetch_all( "analysis_id BETWEEN $1 AND $2" );

    } elsif( $pattern=~/^(\d+)\.\.$/ ) {

166
        $analyses = $self->fetch_all( "$1<=analysis_id" );
167
168
169

    } elsif( $pattern=~/^\.\.(\d+)$/ ) {

170
        $analyses = $self->fetch_all( "analysis_id<=$1" );
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

    } elsif( $pattern=~/^\w+$/) {

        $analyses = $self->fetch_all_by_logic_name( $pattern );

    } elsif( $pattern=~/^[\w\%]+$/) {

        $analyses = $self->fetch_all( "logic_name LIKE '$pattern'" );

    } else {

        throw( "Pattern '$pattern' not recognized" );

    }

    return $analyses;
}


190
1;