| 1 | =head1 LICENSE | ||
| 2 | |||
| 3 | See the NOTICE file distributed with this work for additional information | ||
| 4 | regarding copyright ownership. | ||
| 5 | |||
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 7 | you may not use this file except in compliance with the License. | ||
| 8 | You may obtain a copy of the License at | ||
| 9 | |||
| 10 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | |||
| 12 | Unless required by applicable law or agreed to in writing, software | ||
| 13 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 15 | See the License for the specific language governing permissions and | ||
| 16 | limitations under the License. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | |||
| 21 | =head1 CONTACT | ||
| 22 | |||
| 23 | Please email comments or questions to the public Ensembl | ||
| 24 | developers list at <http://lists.ensembl.org/mailman/listinfo/dev>. | ||
| 25 | |||
| 26 | Questions may also be sent to the Ensembl help desk at | ||
| 27 | <http://www.ensembl.org/Help/Contact>. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | Bio::EnsEMBL::DBSQL::MergedAdaptor | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | # load all available adaptors of the given type for the species | ||
| 38 | my $merged_adaptor = Bio::EnsEMBL::DBSQL::MergedAdaptor->new( | ||
| 39 | -species => "human", | ||
| 40 | -type => "gene" | ||
| 41 | ); | ||
| 42 | |||
| 43 | # only load adaptors from the given groups of the given type for the species | ||
| 44 | my $merged_adaptor = Bio::EnsEMBL::DBSQL::MergedAdaptor->new( | ||
| 45 | -species => "human", | ||
| 46 | -type => "gene", | ||
| 47 | -groups => ['core','otherfeatures'] | ||
| 48 | ); | ||
| 49 | |||
| 50 | =head1 DESCRIPTION | ||
| 51 | |||
| 52 | The MergedAdaptor object is merely a list of adaptors. AUTOLOAD is used | ||
| 53 | to call a subroutine on each adaptor and merge the results. This object structure | ||
| 54 | allows you to treat a set of adaptors as a logical single entity. The end result | ||
| 55 | is that disparate database source data sets are accessible through a single | ||
| 56 | adaptor call. | ||
| 57 | |||
| 58 | This code will convert single object return calls into ArrayRef returning calls | ||
| 59 | and so is only safe to use with the C<fetch_all_XXX> or C<get_all_XXX> methods. | ||
| 60 | |||
| 61 | =head1 METHODS | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | |||
| 66 | package Bio::EnsEMBL::DBSQL::MergedAdaptor; | ||
| 67 | $Bio::EnsEMBL::DBSQL::MergedAdaptor::VERSION = '114.0.0'; | ||
| 68 | use strict; | ||
| 69 | use warnings; | ||
| 70 | |||
| 71 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 72 | use Bio::EnsEMBL::Utils::Argument qw(rearrange); | ||
| 73 | use Bio::EnsEMBL::Utils::Scalar qw(wrap_array assert_ref); | ||
| 74 | require Bio::EnsEMBL::Registry; | ||
| 75 | my $registry = "Bio::EnsEMBL::Registry"; | ||
| 76 | |||
| 77 | |||
| 78 | =head2 new | ||
| 79 | |||
| 80 | Arg [SPECIES]: String species name to get adaptors for | ||
| 81 | Arg [TYPE] : String type to get adaptors for | ||
| 82 | Arg [GROUPS] : (optional) ArrayRef of groups to load | ||
| 83 | Example : my $adaptor = Bio::EnsEMBL::DBSQL::MergedAdaptor->new( | ||
| 84 | -species=> 'human', -type =>'Population', -groups => ['Sanger','Ensembl']); | ||
| 85 | my $alL_adaptor = Bio::EnsEMBL::DBSQL::MergedAdaptor->new( | ||
| 86 | -species=> 'human', -type =>'Population'); | ||
| 87 | Description: Creates a new MergedAdaptor. | ||
| 88 | Returntype : Bio::EnsEMBL::DBSQL::MergedAdaptor | ||
| 89 | Exceptions : throws if species or type not specified | ||
| 90 | Caller : general | ||
| 91 | Status : At Risk | ||
| 92 | : Under development | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub new { | ||
| 97 | my ($class, @args) = @_; | ||
| 98 | $class = ref($class) || $class; | ||
| 99 | my $self = bless({}, $class); | ||
| 100 | my ($species, $type, $groups) = rearrange([qw(SPECIES TYPE GROUPS)], @args); | ||
| 101 | |||
| 102 | if(!defined($species)|| !defined($type)){ | ||
| 103 | throw "-SPECIES and -TYPE must be specified"; | ||
| 104 | } | ||
| 105 | $self->_populate_adaptors($species, $type, $groups); | ||
| 106 | return $self; | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 _populate_adaptors | ||
| 110 | |||
| 111 | Arg [1] : String species name to get adaptors for | ||
| 112 | Arg [2] : String type to get adaptors for | ||
| 113 | Arg [3] : (optional) ArrayRef of groups to load | ||
| 114 | Description : Auto-populates the current MergedAdaptor with the | ||
| 115 | adaptors linked to this species, type and optional set of groups | ||
| 116 | Caller : general | ||
| 117 | Status : At Risk | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | sub _populate_adaptors { | ||
| 122 | my ($self, $species, $type, $groups) = @_; | ||
| 123 | if($groups) { | ||
| 124 | assert_ref($groups, 'ARRAY', '-GROUPS'); | ||
| 125 | my @adaptors = map { $registry->get_adaptor($species, $_, $type) } @{$groups}; | ||
| 126 | $self->add_list(@adaptors); | ||
| 127 | } | ||
| 128 | else { | ||
| 129 | my $adaptors = $registry->get_all_adaptors(-SPECIES => $species, -TYPE => $type); | ||
| 130 | $self->add_list(@{$adaptors}); | ||
| 131 | } | ||
| 132 | return; | ||
| 133 | } | ||
| 134 | |||
| 135 | =head2 add_list | ||
| 136 | |||
| 137 | Arg [n] : Adaptors to add into this instance | ||
| 138 | Description : Adds the given adaptors to the internal adaptor list | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub add_list { | ||
| 143 | my ($self, @adaptors) = @_; | ||
| 144 | push(@{$self->{adaptors}}, @adaptors); | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | =head2 add_list | ||
| 149 | |||
| 150 | Arg [1] : Adaptor to add into this instance | ||
| 151 | Description : Adds the given adaptor to the internal adaptor list. For | ||
| 152 | multiple adaptor addition use C<add_list()>. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub add_adaptor { | ||
| 157 | my ($self, $adaptor) = @_; | ||
| 158 | $self->add_list($adaptor); | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | |||
| 162 | =head2 can | ||
| 163 | |||
| 164 | Arg [1] : String method name to be called | ||
| 165 | Description : Implementation of UNIVERSAL::can(). We loop through the | ||
| 166 | available adaptors and return true if any will respond | ||
| 167 | to the given method name | ||
| 168 | Returntype : Boolean indicating if any delegating object will respond to this method | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | sub can { | ||
| 173 | my ($self, $method) = @_; | ||
| 174 | foreach my $adaptor (@{$self->{adaptors}}) { | ||
| 175 | return 1 if $adaptor->can($method); | ||
| 176 | } | ||
| 177 | return 0; | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | =head2 isa | ||
| 182 | |||
| 183 | Arg [1] : String method name to be called | ||
| 184 | Description : Implementation of UNIVERSAL::isa(). We loop through the | ||
| 185 | available adaptors and return true if any inherited from | ||
| 186 | the given class | ||
| 187 | Returntype : Boolean indicating if any delegating object inherits from the given class | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | sub isa { | ||
| 192 | my ($self, $isa) = @_; | ||
| 193 | foreach my $adaptor (@{$self->{adaptors}}) { | ||
| 194 | return 1 if $adaptor->isa($isa); | ||
| 195 | } | ||
| 196 | return 0; | ||
| 197 | } | ||
| 198 | |||
| 199 | use vars '$AUTOLOAD'; | ||
| 200 | |||
| 201 | =head2 AUTOLOAD | ||
| 202 | |||
| 203 | Description : Internal override of AUTLOAD. The code will detect the requested | ||
| 204 | method, loop through all available adaptors and will | ||
| 205 | Returntype : Boolean indicating if any delegating object inherits from the given class | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub AUTOLOAD { | ||
| 210 | my ($self, @args) = @_; | ||
| 211 | my @return; | ||
| 212 | |||
| 213 | #Detect required method | ||
| 214 | $AUTOLOAD =~ /^.*::(\w+)+$/ ; | ||
| 215 | my $sub = $1; | ||
| 216 | |||
| 217 | foreach my $adaptor (@{$self->{adaptors}}) { | ||
| 218 | my @local_return; | ||
| 219 | if(my $method_ref = $adaptor->can($sub)) { | ||
| 220 | my $ref = $method_ref->($adaptor, @args); | ||
| 221 | push(@return, @{wrap_array($ref)}); | ||
| 222 | } | ||
| 223 | else{ | ||
| 224 | warning("In Merged Adaptor $adaptor cannot call sub $sub"); | ||
| 225 | } | ||
| 226 | } | ||
| 227 | return \@return; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub DESTROY { | ||
| 231 | |||
| 232 | } | ||
| 233 | |||
| 234 | 1; |