| 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::External::ExternalFeatureAdaptor | ||
| 34 | |||
| 35 | =head 1 SUMMARY | ||
| 36 | |||
| 37 | Allows features created externally from Ensembl in a single | ||
| 38 | coordinate system to be retrieved in several other (Ensembl-style) | ||
| 39 | coordinate systems. This is intended to be a replacement for the old | ||
| 40 | Bio::EnsEMBL::DB::ExternalFeatureFactoryI interface. | ||
| 41 | |||
| 42 | =head1 SYNOPSIS | ||
| 43 | |||
| 44 | $database_adaptor = new Bio::EnsEMBL::DBSQL::DBAdaptor( | ||
| 45 | -host => 'kaka.sanger.ac.uk', | ||
| 46 | -dbname => 'homo_sapiens_core_9_30', | ||
| 47 | -pass => 'anonymous' | ||
| 48 | ); | ||
| 49 | |||
| 50 | $xf_adaptor = new ExternalFeatureAdaptorSubClass; | ||
| 51 | |||
| 52 | # Connect the Ensembl core database: | ||
| 53 | $xf_adaptor->db($database_adaptor); | ||
| 54 | |||
| 55 | # get some features in vontig coords | ||
| 56 | @feats = | ||
| 57 | @{ $xf_adaptor->fetch_all_by_contig_name('AC000087.2.1.42071') }; | ||
| 58 | |||
| 59 | # get some features in assembly coords | ||
| 60 | @feats = | ||
| 61 | @{ $xf_adaptor->fetch_all_by_chr_start_end( 'X', 100000, 200000 ) }; | ||
| 62 | |||
| 63 | # get some features in clone coords | ||
| 64 | @feats = @{ $xf_adaptor->fetch_all_by_clone_accession('AC000087') }; | ||
| 65 | |||
| 66 | # Add the adaptor to the ensembl core dbadaptor (implicitly sets db | ||
| 67 | # attribute) | ||
| 68 | $database_adaptor->add_ExternalFeatureAdaptor($xf_adaptor); | ||
| 69 | |||
| 70 | # get some features in Slice coords | ||
| 71 | $slice_adaptor = $database_adaptor->get_SliceAdaptor; | ||
| 72 | $slice = | ||
| 73 | $slice_adaptor->fetch_all_by_chr_start_end( 1, 100000, 200000 ); | ||
| 74 | @feats = @{ $xf_adaptor->fetch_all_by_Slice($slice) }; | ||
| 75 | |||
| 76 | # now features can be retrieved directly from Slice | ||
| 77 | @feats = @{ $slice->get_all_ExternalFeatures }; | ||
| 78 | |||
| 79 | =head1 DESCRIPTION | ||
| 80 | |||
| 81 | This class is intended to be used as a method of getting external | ||
| 82 | features into EnsEMBL. To work, this class must be extended and must | ||
| 83 | implement the the coordinate_systems method. As well, the subclass | ||
| 84 | is required to implement a single fetch method so that the external | ||
| 85 | features may be retrieved. By implementing a single fetch_method in a | ||
| 86 | single coordinate system all of the other ExternalFeatureAdaptor fetch | ||
| 87 | methods become available for retrieving the data in several different | ||
| 88 | coordinate systems. | ||
| 89 | |||
| 90 | The coordinate_systems method should return a list of strings indicating | ||
| 91 | which coordinate system(s) have been implemented. If a given string is | ||
| 92 | returned from the coordinate_systems method then the corresponding fetch | ||
| 93 | method must be implemented. The reverse is also true: if a fetch method | ||
| 94 | is implemented then coordinate_systems must return the appropriate | ||
| 95 | string in its list of return values. The following are the valid | ||
| 96 | coordinate system values and the corresponding fetch methods that must | ||
| 97 | be implemented: | ||
| 98 | |||
| 99 | COORD SYSTEM STRING FETCH_METHOD | ||
| 100 | ------------------- ------------ | ||
| 101 | 'ASSEMBLY' fetch_all_by_chr_start_end | ||
| 102 | 'CLONE' fetch_all_by_clone_accession | ||
| 103 | 'CONTIG' fetch_all_by_contig_name | ||
| 104 | 'SUPERCONTIG' fetch_all_by_supercontig_name | ||
| 105 | 'SLICE' fetch_all_by_Slice | ||
| 106 | |||
| 107 | The objects returned by the fetch methods should be EnsEMBL or BioPerl | ||
| 108 | style Feature objects. These objects MUST have start, end and strand | ||
| 109 | methods. | ||
| 110 | |||
| 111 | Before the non-overridden ExternalFeatureAdaptor fetch methods may | ||
| 112 | be called an EnsEMBL core database adaptor must be attached to the | ||
| 113 | ExternalFeatureAdaptor . This database adaptor is required to perform | ||
| 114 | the remappings between various coordinate system. This may be done | ||
| 115 | implicitly by adding the ExternalFeatureAdaptor to the database adaptor | ||
| 116 | through a call to the DBAdaptor add_ExternalFeatureAdaptor method or | ||
| 117 | explicitly by calling the ExternalFeatureAdaptor ensembl_db method. | ||
| 118 | |||
| 119 | =head1 METHODS | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | package Bio::EnsEMBL::External::ExternalFeatureAdaptor; | ||
| 124 | $Bio::EnsEMBL::External::ExternalFeatureAdaptor::VERSION = '114.0.0_50'; # TRIAL | ||
| 125 | $Bio::EnsEMBL::External::ExternalFeatureAdaptor::VERSION = '114.0.050'; | ||
| 126 | use strict; | ||
| 127 | |||
| 128 | use Bio::EnsEMBL::Utils::Exception qw(warning throw); | ||
| 129 | |||
| 130 | |||
| 131 | =head2 new | ||
| 132 | |||
| 133 | Arg [1] : none | ||
| 134 | Example : $xfa = new Bio::EnsEMBL::External::ExternalFeatureAdaptor; | ||
| 135 | Description: Creates a new ExternalFeatureAdaptor object. You may wish to | ||
| 136 | extend this constructor and provide your own set of paremeters. | ||
| 137 | Returntype : Bio::EnsEMBL::External::ExternalFeatureAdaptor | ||
| 138 | Exceptions : none | ||
| 139 | Caller : general | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub new { | ||
| 144 | my $class = shift; | ||
| 145 | |||
| 146 | if(ref $class) { | ||
| 147 | return bless {}, ref $class; | ||
| 148 | } | ||
| 149 | |||
| 150 | return bless {}, $class; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | =head2 ensembl_db | ||
| 156 | |||
| 157 | Arg [1] : (optional) Bio::EnsEMBL::DBSQL::DBAdaptor | ||
| 158 | Example : $external_feature_adaptor->ensembl_db($new_val); | ||
| 159 | Description: none | ||
| 160 | Returntype : Bio::EnsEMBL::DBSQL::DBAdaptor | ||
| 161 | Exceptions : none | ||
| 162 | Caller : internal | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | sub ensembl_db { | ||
| 167 | my ($self, $value) = @_; | ||
| 168 | |||
| 169 | if($value) { | ||
| 170 | $self->{'ensembl_db'} = $value; | ||
| 171 | } | ||
| 172 | |||
| 173 | return $self->{'ensembl_db'}; | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | =head2 coordinate_systems | ||
| 179 | |||
| 180 | Arg [1] : none | ||
| 181 | Example : @implemented_coord_systems = $ext_adaptor->coordinate_systems; | ||
| 182 | Description: ABSTRACT method. Must be implemented by all | ||
| 183 | ExternalFeatureAdaptor subclasses. This method returns a list | ||
| 184 | of coordinate systems which are implemented by the subclass. | ||
| 185 | A minimum of on valid coordinate system must be implemented. | ||
| 186 | Valid coordinate systems are: 'SLICE', 'ASSEMBLY', 'CONTIG', | ||
| 187 | and 'CLONE'. | ||
| 188 | Returntype : list of strings | ||
| 189 | Exceptions : none | ||
| 190 | Caller : internal | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | sub coordinate_systems { | ||
| 195 | my $self = shift; | ||
| 196 | |||
| 197 | throw("abstract method coordinate_systems not implemented\n"); | ||
| 198 | |||
| 199 | return ''; | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | =head2 track_name | ||
| 204 | |||
| 205 | Arg [1] : none | ||
| 206 | Example : $track_name = $xf_adaptor->track_name; | ||
| 207 | Description: Currently this is not really used. In the future it may be | ||
| 208 | possible to have ExternalFeatures automatically displayed by | ||
| 209 | the EnsEMBL web code. By default this method returns | ||
| 210 | 'External features' but you are encouraged to override this | ||
| 211 | method and provide your own meaningful name for the features | ||
| 212 | your adaptor provides. This also allows you to distinguish the | ||
| 213 | type of features retrieved from Slices. See | ||
| 214 | the PODs for Bio::EnsEMBL::Slice::get_all_ExternalFeatures and | ||
| 215 | Bio::EnsEMBL::DBSQL::DBAdaptor::add_ExternalFeatureAdaptor | ||
| 216 | methods. | ||
| 217 | Returntype : string | ||
| 218 | Exceptions : none | ||
| 219 | Caller : Bio::EnsEMBL::DBSQL::DBAdaptor::add_ExternalFeatureAdaptor | ||
| 220 | |||
| 221 | =cut | ||
| 222 | |||
| 223 | sub track_name { | ||
| 224 | my $self = shift; | ||
| 225 | |||
| 226 | return 'External features'; | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | |||
| 231 | =head2 feature_type | ||
| 232 | |||
| 233 | Arg [1] : none | ||
| 234 | Example : $feature_type = $xf_adaptor->track_name | ||
| 235 | Description: Currently this is not used. In the future it may be possible | ||
| 236 | to have ExternalFeatures automatically displayed by the EnsEMBL | ||
| 237 | web code. This method would then be used do determine the | ||
| 238 | type of glyphs used to draw the features which are returned | ||
| 239 | from this external adaptor. | ||
| 240 | Returntype : string | ||
| 241 | Exceptions : none | ||
| 242 | Caller : none | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub feature_type { | ||
| 247 | my $self = shift; | ||
| 248 | |||
| 249 | return qw(SIMPLE); | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | |||
| 254 | =head2 fetch_all_by_Slice | ||
| 255 | |||
| 256 | Arg [1] : Bio::EnsEMBL::Slice $slice | ||
| 257 | Example : @features = @{$ext_adaptor->fetch_all_by_Slice($slice)}; | ||
| 258 | Description: Retrieves all features which lie in the region defined | ||
| 259 | by $slice in slice coordinates. | ||
| 260 | |||
| 261 | If this method is overridden then the coordinate_systems method | ||
| 262 | must return 'SLICE' as one of its values. | ||
| 263 | |||
| 264 | This method will work as is (i.e. without overriding it) | ||
| 265 | providing at least one of the other fetch methods is overridden. | ||
| 266 | Returntype : reference to a list of Bio::SeqFeature objects in the Slice | ||
| 267 | coordinate system | ||
| 268 | Exceptions : Thrown on incorrect input arguments | ||
| 269 | Caller : general, fetch_all_by_chr_start_end | ||
| 270 | |||
| 271 | =cut | ||
| 272 | |||
| 273 | sub fetch_all_by_Slice { | ||
| 274 | my ($self, $slice) = @_; | ||
| 275 | |||
| 276 | unless($slice && ref $slice && $slice->isa('Bio::EnsEMBL::Slice')) { | ||
| 277 | throw("[$slice] is not a Bio::EnsEMBL::Slice"); | ||
| 278 | } | ||
| 279 | |||
| 280 | my $out = []; | ||
| 281 | |||
| 282 | my $csa = $self->ensembl_db->get_CoordSystemAdaptor(); | ||
| 283 | |||
| 284 | my $slice_start = $slice->start; | ||
| 285 | my $slice_end = $slice->end; | ||
| 286 | my $slice_strand = $slice->strand; | ||
| 287 | my $slice_seq_region = $slice->seq_region_name; | ||
| 288 | my $slice_seq_region_id = $slice->get_seq_region_id; | ||
| 289 | my $coord_system = $slice->coord_system; | ||
| 290 | |||
| 291 | if($self->_supported('SLICE')) { | ||
| 292 | throw("ExternalFeatureAdaptor supports SLICE coordinate system" . | ||
| 293 | " but fetch_all_by_Slice not implemented"); | ||
| 294 | } | ||
| 295 | |||
| 296 | my %features; | ||
| 297 | my $from_coord_system; | ||
| 298 | |||
| 299 | my $fetch_method; | ||
| 300 | |||
| 301 | # | ||
| 302 | # Get all of the features from whatever coord system they are computed in | ||
| 303 | # | ||
| 304 | if($self->_supported('CLONE')) { | ||
| 305 | $fetch_method = sub { | ||
| 306 | my $self = shift; | ||
| 307 | my $name = shift; | ||
| 308 | my ($acc, $ver) = split(/\./, $name); | ||
| 309 | $self->fetch_all_by_clone_accession($acc,$ver,@_); | ||
| 310 | }; | ||
| 311 | $from_coord_system = $csa->fetch_by_name('clone'); | ||
| 312 | } elsif($self->_supported('ASSEMBLY')) { | ||
| 313 | $from_coord_system = $csa->fetch_by_name('chromosome'); | ||
| 314 | $fetch_method = $self->can('fetch_all_by_chr_start_end'); | ||
| 315 | } elsif($self->_supported('CONTIG')) { | ||
| 316 | $from_coord_system = $csa->fetch_by_name('contig'); | ||
| 317 | $fetch_method = $self->can('fetch_all_by_contig_name'); | ||
| 318 | } elsif($self->_supported('SUPERCONTIG')) { | ||
| 319 | $from_coord_system = $csa->fetch_by_name('supercontig'); | ||
| 320 | $fetch_method = $self->can('fetch_all_by_supercontig_name'); | ||
| 321 | } else { | ||
| 322 | $self->_no_valid_coord_systems(); | ||
| 323 | } | ||
| 324 | |||
| 325 | if($from_coord_system->equals($coord_system)) { | ||
| 326 | $features{$slice_seq_region} = &$fetch_method($self, $slice_seq_region, | ||
| 327 | $slice_start,$slice_end); | ||
| 328 | } else { | ||
| 329 | foreach my $segment (@{$slice->project($from_coord_system->name, | ||
| 330 | $from_coord_system->version)}) { | ||
| 331 | my ($start,$end,$pslice) = @$segment; | ||
| 332 | $features{$pslice->seq_region_name } ||= []; | ||
| 333 | push @{$features{$pslice->seq_region_name }}, | ||
| 334 | @{&$fetch_method($self, $pslice->seq_region_name, | ||
| 335 | $pslice->start(), | ||
| 336 | $pslice->end())}; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | my @out; | ||
| 341 | |||
| 342 | if(!$coord_system->equals($from_coord_system)) { | ||
| 343 | my $asma = $self->ensembl_db->get_AssemblyMapperAdaptor(); | ||
| 344 | my $mapper = $asma->fetch_by_CoordSystems($from_coord_system, | ||
| 345 | $coord_system); | ||
| 346 | my %slice_cache; | ||
| 347 | my $slice_adaptor = $self->ensembl_db->get_SliceAdaptor(); | ||
| 348 | my $slice_setter; | ||
| 349 | |||
| 350 | #convert the coordinates of each of the features retrieved | ||
| 351 | foreach my $fseq_region (keys %features) { | ||
| 352 | my $feats = $features{$fseq_region}; | ||
| 353 | next if(!$feats); | ||
| 354 | $slice_setter = _guess_slice_setter($feats) if(!$slice_setter); | ||
| 355 | |||
| 356 | foreach my $f (@$feats) { | ||
| 357 | my($sr_id, $start, $end, $strand) = | ||
| 358 | $mapper->fastmap($fseq_region,$f->start,$f->end,$f->strand, | ||
| 359 | $from_coord_system); | ||
| 360 | |||
| 361 | #maps to gap | ||
| 362 | next if(!defined($sr_id)); | ||
| 363 | |||
| 364 | #maps to unexpected seq region, probably error in the externally | ||
| 365 | if($sr_id ne $slice_seq_region_id) { | ||
| 366 | warning("Externally created Feature mapped to [$sr_id] " . | ||
| 367 | "which is not on requested seq_region_id [$slice_seq_region_id]"); | ||
| 368 | next; | ||
| 369 | } | ||
| 370 | |||
| 371 | #update the coordinates of the feature | ||
| 372 | &$slice_setter($f,$slice); | ||
| 373 | $f->start($start); | ||
| 374 | $f->end($end); | ||
| 375 | $f->strand($strand); | ||
| 376 | push @out, $f; | ||
| 377 | } | ||
| 378 | } | ||
| 379 | } else { | ||
| 380 | #we already know the seqregion the featues are on, we just have | ||
| 381 | #to put them on the slice | ||
| 382 | @out = @{$features{$slice_seq_region}}; | ||
| 383 | my $slice_setter = _guess_slice_setter(\@out); | ||
| 384 | |||
| 385 | foreach my $f (@out) { | ||
| 386 | &$slice_setter($f,$slice); | ||
| 387 | } | ||
| 388 | } | ||
| 389 | |||
| 390 | # convert from assembly coords to slice coords | ||
| 391 | # handle the circular slice case | ||
| 392 | my $seq_region_len = $slice->seq_region_length(); | ||
| 393 | foreach my $f (@out) { | ||
| 394 | my($f_start, $f_end, $f_strand); | ||
| 395 | |||
| 396 | if ($slice->strand == 1) { # Positive strand | ||
| 397 | $f_start = $f->start - $slice_start + 1; | ||
| 398 | $f_end = $f->end - $slice_start + 1; | ||
| 399 | $f_strand = $f->strand; | ||
| 400 | |||
| 401 | if ($slice->is_circular()) { # Handle cicular chromosomes | ||
| 402 | if ($f_start > $f_end) { # Looking at a feature overlapping the chromsome origin. | ||
| 403 | if ($f_end > $slice_start) { | ||
| 404 | # Looking at the region in the beginning of the chromosome. | ||
| 405 | $f_start -= $seq_region_len; | ||
| 406 | } | ||
| 407 | |||
| 408 | if ($f_end < 0) { | ||
| 409 | $f_end += $seq_region_len; | ||
| 410 | } | ||
| 411 | } else { | ||
| 412 | if ($slice_start > $slice_end && $f_end < 0) { | ||
| 413 | # Looking at the region overlapping the chromosome origin and | ||
| 414 | # a feature which is at the beginning of the chromosome. | ||
| 415 | $f_start += $seq_region_len; | ||
| 416 | $f_end += $seq_region_len; | ||
| 417 | } | ||
| 418 | } | ||
| 419 | } | ||
| 420 | } else { # Negative strand | ||
| 421 | my ($seq_region_start, $seq_region_end) = ($f->start, $f->end); | ||
| 422 | $f_start = $slice_end - $seq_region_end + 1; | ||
| 423 | $f_end = $slice_end - $seq_region_start + 1; | ||
| 424 | $f_strand = $f->strand * -1; | ||
| 425 | |||
| 426 | if ($slice->is_circular()) { | ||
| 427 | if ($slice_start > $slice_end) { # slice spans origin or replication | ||
| 428 | if ($seq_region_start >= $slice_start) { | ||
| 429 | $f_end += $seq_region_len; | ||
| 430 | $f_start += $seq_region_len | ||
| 431 | if $seq_region_end > $slice_start; | ||
| 432 | |||
| 433 | } elsif ($seq_region_start <= $slice_end) { | ||
| 434 | # do nothing | ||
| 435 | } elsif ($seq_region_end >= $slice_start) { | ||
| 436 | $f_start += $seq_region_len; | ||
| 437 | $f_end += $seq_region_len; | ||
| 438 | } elsif ($seq_region_end <= $slice_end) { | ||
| 439 | $f_end += $seq_region_len | ||
| 440 | if $f_end < 0; | ||
| 441 | } elsif ($seq_region_start > $seq_region_end) { | ||
| 442 | $f_end += $seq_region_len; | ||
| 443 | } else { } | ||
| 444 | } else { | ||
| 445 | if ($seq_region_start <= $slice_end and $seq_region_end >= $slice_start) { | ||
| 446 | # do nothing | ||
| 447 | } elsif ($seq_region_start > $seq_region_end) { | ||
| 448 | if ($seq_region_start <= $slice_end) { | ||
| 449 | $f_start -= $seq_region_len; | ||
| 450 | } elsif ($seq_region_end >= $slice_start) { | ||
| 451 | $f_end += $seq_region_len; | ||
| 452 | } else { } | ||
| 453 | } | ||
| 454 | } | ||
| 455 | } | ||
| 456 | } | ||
| 457 | |||
| 458 | $f->start($f_start); | ||
| 459 | $f->end($f_end); | ||
| 460 | $f->strand($f_strand); | ||
| 461 | } | ||
| 462 | |||
| 463 | return \@out; | ||
| 464 | } | ||
| 465 | |||
| 466 | |||
| 467 | sub _guess_slice_setter { | ||
| 468 | my $features = shift; | ||
| 469 | |||
| 470 | #we do not know what type of features these are. They might | ||
| 471 | #be bioperl features or old ensembl features, hopefully they are new | ||
| 472 | #style features. Try to come up with a setter method for the | ||
| 473 | #slice. | ||
| 474 | |||
| 475 | return undef if(!@$features); | ||
| 476 | |||
| 477 | my ($f) = @$features; | ||
| 478 | |||
| 479 | my $slice_setter; | ||
| 480 | foreach my $method (qw(slice contig attach_seq)) { | ||
| 481 | last if($slice_setter = $f->can($method)); | ||
| 482 | } | ||
| 483 | |||
| 484 | if(!$slice_setter) { | ||
| 485 | if($f->can('seqname')) { | ||
| 486 | $slice_setter = sub { $_[0]->seqname($_[1]->seq_region_name()); }; | ||
| 487 | } else { | ||
| 488 | $slice_setter = sub{} if(!$slice_setter); | ||
| 489 | } | ||
| 490 | } | ||
| 491 | |||
| 492 | return $slice_setter; | ||
| 493 | } | ||
| 494 | |||
| 495 | |||
| 496 | =head2 fetch_all_by_contig_name | ||
| 497 | |||
| 498 | Arg [1] : string $contig_name | ||
| 499 | Arg [2] : int $start (optional) | ||
| 500 | The start of the region on the contig to retrieve features on | ||
| 501 | if not specified the whole of the contig is used. | ||
| 502 | Arg [3] : int $end (optional) | ||
| 503 | The end of the region on the contig to retrieve features on | ||
| 504 | if not specified the whole of the contig is used. | ||
| 505 | Example : @fs = @{$self->fetch_all_by_contig_name('AB00879.1.1.39436')}; | ||
| 506 | Description: Retrieves features on the contig defined by the name | ||
| 507 | $contig_name in contig coordinates. | ||
| 508 | |||
| 509 | If this method is overridden then the coordinate_systems | ||
| 510 | method must return 'CONTIG' as one of its values. | ||
| 511 | |||
| 512 | This method will work as is (i.e. without being overridden) | ||
| 513 | providing at least one other fetch method has | ||
| 514 | been overridden. | ||
| 515 | Returntype : reference to a list of Bio::SeqFeature objects in the contig | ||
| 516 | coordinate system. | ||
| 517 | Exceptions : thrown if the input argument is incorrect | ||
| 518 | thrown if the coordinate_systems method returns the value | ||
| 519 | 'CONTIG' and this method has not been overridden. | ||
| 520 | Caller : general, fetch_all_by_Slice | ||
| 521 | |||
| 522 | =cut | ||
| 523 | |||
| 524 | sub fetch_all_by_contig_name { | ||
| 525 | my ($self, $contig_name, $start, $end) = @_; | ||
| 526 | |||
| 527 | unless($contig_name) { | ||
| 528 | throw("contig_name argument not defined"); | ||
| 529 | } | ||
| 530 | |||
| 531 | if($self->_supported('CONTIG')) { | ||
| 532 | throw("ExternalFeatureAdaptor supports CONTIG coordinate system" . | ||
| 533 | " but fetch_all_by_contig_name is not implemented"); | ||
| 534 | } | ||
| 535 | |||
| 536 | unless($self->ensembl_db) { | ||
| 537 | throw('DB attribute not set. This value must be set for the ' . | ||
| 538 | 'ExternalFeatureAdaptor to function correctly'); | ||
| 539 | } | ||
| 540 | |||
| 541 | my $slice_adaptor = $self->ensembl_db->get_SliceAdaptor(); | ||
| 542 | my $slice = $slice_adaptor->fetch_by_region('contig', $contig_name, | ||
| 543 | $start, $end); | ||
| 544 | return $self->fetch_all_by_Slice($slice); | ||
| 545 | } | ||
| 546 | |||
| 547 | |||
| 548 | |||
| 549 | =head2 fetch_all_by_supercontig_name | ||
| 550 | |||
| 551 | Arg [1] : string $supercontig_name | ||
| 552 | Arg [2] : int $start (optional) | ||
| 553 | The start of the region on the contig to retrieve features on | ||
| 554 | if not specified the whole of the contig is used. | ||
| 555 | Arg [3] : int $end (optional) | ||
| 556 | The end of the region on the contig to retrieve features on | ||
| 557 | if not specified the whole of the contig is used. | ||
| 558 | Example : @fs = @{$self->fetch_all_by_contig_name('NT_004321')}; | ||
| 559 | Description: Retrieves features on the contig defined by the name | ||
| 560 | $supercontigname in supercontig coordinates. | ||
| 561 | |||
| 562 | If this method is overridden then the coordinate_systems | ||
| 563 | method must return 'SUPERCONTIG' as one of its values. | ||
| 564 | |||
| 565 | This method will work as is (i.e. without being overridden) | ||
| 566 | providing at least one other fetch method has | ||
| 567 | been overridden. | ||
| 568 | Returntype : reference to a list of Bio::SeqFeature objects in the contig | ||
| 569 | coordinate system. | ||
| 570 | Exceptions : thrown if the input argument is incorrect | ||
| 571 | thrown if the coordinate_systems method returns the value | ||
| 572 | 'SUPERCONTIG' and this method has not been overridden. | ||
| 573 | Caller : general, fetch_all_by_Slice | ||
| 574 | |||
| 575 | =cut | ||
| 576 | |||
| 577 | |||
| 578 | sub fetch_all_by_supercontig_name { | ||
| 579 | my ($self, $supercontig_name, $start, $end) = @_; | ||
| 580 | |||
| 581 | unless($supercontig_name) { | ||
| 582 | throw("supercontig_name argument not defined"); | ||
| 583 | } | ||
| 584 | |||
| 585 | if($self->_supported('SUPERCONTIG')) { | ||
| 586 | throw("ExternalFeatureAdaptor supports SUPERCONTIG coordinate system" . | ||
| 587 | " but fetch_all_by_supercontig_name is not implemented"); | ||
| 588 | } | ||
| 589 | |||
| 590 | unless($self->ensembl_db) { | ||
| 591 | throw('DB attribute not set. This value must be set for the ' . | ||
| 592 | 'ExternalFeatureAdaptor to function correctly'); | ||
| 593 | } | ||
| 594 | |||
| 595 | my $slice_adaptor = $self->ensembl_db->get_SliceAdaptor(); | ||
| 596 | my $slice = $slice_adaptor->fetch_by_region('supercontig', $supercontig_name, | ||
| 597 | $start, $end); | ||
| 598 | return $self->fetch_all_by_Slice($slice); | ||
| 599 | } | ||
| 600 | |||
| 601 | |||
| 602 | =head2 fetch_all_by_clone_accession | ||
| 603 | |||
| 604 | Arg [1] : string $acc | ||
| 605 | The EMBL accession number of the clone to fetch features from. | ||
| 606 | Arg [2] : (optional) string $ver | ||
| 607 | Arg [3] : (optional) int $start | ||
| 608 | Arg [4] : (optional) int $end | ||
| 609 | |||
| 610 | Example : @fs = @{$self->fetch_all_by_clone_accession('AC000093')}; | ||
| 611 | Description: Retrieves features on the clone defined by the $acc arg in | ||
| 612 | Clone coordinates. | ||
| 613 | |||
| 614 | If this method is overridden then the coordinate_systems method | ||
| 615 | must return 'CLONE' as one of its values. The arguments | ||
| 616 | start, end, version are passed if this method is overridden and | ||
| 617 | can optionally be used to reduce the scope of the query and | ||
| 618 | improve performance. | ||
| 619 | |||
| 620 | This method will work as is - providing at least one other | ||
| 621 | fetch method has been overridden. | ||
| 622 | Returntype : reference to a list of Bio::SeqFeature objects in the Clone | ||
| 623 | coordinate system | ||
| 624 | Exceptions : thrown if the input argument is incorrect | ||
| 625 | thrown if the coordinate system method returns the value 'CLONE' | ||
| 626 | and this method is not overridden. | ||
| 627 | thrown if the coordinate systems method does not return any | ||
| 628 | valid values. | ||
| 629 | Caller : general, fetch_all_by_clone_accession | ||
| 630 | |||
| 631 | =cut | ||
| 632 | |||
| 633 | sub fetch_all_by_clone_accession { | ||
| 634 | my ($self, $acc, $version, $start, $end) = @_; | ||
| 635 | |||
| 636 | unless($acc) { | ||
| 637 | throw("clone accession argument not defined"); | ||
| 638 | } | ||
| 639 | |||
| 640 | if($self->_supported('CLONE')) { | ||
| 641 | throw('ExternalFeatureAdaptor supports CLONE coordinate system ' . | ||
| 642 | 'but does not implement fetch_all_by_clone_accession'); | ||
| 643 | } | ||
| 644 | |||
| 645 | unless($self->ensembl_db) { | ||
| 646 | throw('DB attribute not set. This value must be set for the ' . | ||
| 647 | 'ExternalFeatureAdaptor to function correctly'); | ||
| 648 | } | ||
| 649 | |||
| 650 | if(defined($version)) { | ||
| 651 | $acc = "$acc.$version"; | ||
| 652 | } elsif(!$acc =~ /\./) { | ||
| 653 | $acc = "$acc.1"; | ||
| 654 | } | ||
| 655 | |||
| 656 | my $slice_adaptor = $self->ensembl_db->get_SliceAdaptor; | ||
| 657 | |||
| 658 | my $slice = $slice_adaptor->fetch_by_region('clone', $acc, $start, $end); | ||
| 659 | |||
| 660 | return $self->fetch_all_by_Slice($slice); | ||
| 661 | } | ||
| 662 | |||
| 663 | |||
| 664 | |||
| 665 | =head2 fetch_all_by_chr_start_end | ||
| 666 | |||
| 667 | Arg [1] : string $chr_name | ||
| 668 | The name of the chromosome to retrieve features from | ||
| 669 | Arg [2] : int $start | ||
| 670 | The start coordinate of the chromosomal region to retrieve | ||
| 671 | features from. | ||
| 672 | Arg [3] : int $end | ||
| 673 | The end coordinate of the chromosomal region to retrieve | ||
| 674 | features from. | ||
| 675 | Example : @features | ||
| 676 | Description: Retrieves features on the region defined by the $chr_name, | ||
| 677 | $start, and $end args in assembly (chromosomal) coordinates. | ||
| 678 | |||
| 679 | If this method is overridden then the coordinate_systems method | ||
| 680 | must return 'ASSEMBLY' as one of its values. | ||
| 681 | |||
| 682 | This method will work as is (i.e. without overriding it) | ||
| 683 | providing at least one of the other fetch methods is overridden. | ||
| 684 | Returntype : reference to a list of Bio::SeqFeatures | ||
| 685 | Exceptions : Thrown if the coordinate_systems method returns ASSEMBLY as a | ||
| 686 | value and this method is not overridden. | ||
| 687 | Thrown if any of the input arguments are incorrect | ||
| 688 | Caller : general, fetch_all_by_Slice | ||
| 689 | |||
| 690 | =cut | ||
| 691 | |||
| 692 | sub fetch_all_by_chr_start_end { | ||
| 693 | my ($self, $chr_name, $start, $end) = @_; | ||
| 694 | |||
| 695 | unless($chr_name && defined $start && defined $end && $start < $end) { | ||
| 696 | throw("Incorrect start [$start] end [$end] or chr [$chr_name] arg"); | ||
| 697 | } | ||
| 698 | |||
| 699 | unless($self->ensembl_db) { | ||
| 700 | throw('DB attribute not set. This value must be set for the ' . | ||
| 701 | 'ExternalFeatureAdaptor to function correctly'); | ||
| 702 | } | ||
| 703 | |||
| 704 | my $slice_adaptor = $self->ensembl_db->get_SliceAdaptor(); | ||
| 705 | |||
| 706 | my $slice = $slice_adaptor->fetch_by_region('toplevel', $chr_name, $start, | ||
| 707 | $end); | ||
| 708 | |||
| 709 | return $self->fetch_all_by_Slice($slice); | ||
| 710 | } | ||
| 711 | |||
| 712 | |||
| 713 | =head2 _no_valid_coord_system | ||
| 714 | |||
| 715 | Arg [1] : none | ||
| 716 | Example : none | ||
| 717 | Description: PRIVATE method - throws an error with a descriptive message | ||
| 718 | Returntype : none | ||
| 719 | Exceptions : always thrown | ||
| 720 | Caller : internal | ||
| 721 | |||
| 722 | =cut | ||
| 723 | |||
| 724 | sub _no_valid_coord_system { | ||
| 725 | my $self = shift; | ||
| 726 | |||
| 727 | throw("This ExternalFeatureAdaptor does not support a known " . | ||
| 728 | "coordinate system.\n Valid coordinate systems are: " . | ||
| 729 | "[SLICE,ASSEMBLY,SUPERCONTIG,CONTIG,CLONE].\n This External Adaptor " . | ||
| 730 | "supports: [" . join(', ', $self->coordinate_systems) . "]"); | ||
| 731 | } | ||
| 732 | |||
| 733 | |||
| 734 | |||
| 735 | |||
| 736 | =head2 _supported | ||
| 737 | |||
| 738 | Arg [1] : string $system | ||
| 739 | Example : print "CONTIG system supported" if($self->_supported('CONTIG')); | ||
| 740 | Description: PRIVATE method. Tests if the coordinate system defined by | ||
| 741 | the $system argument is implemented. | ||
| 742 | Returntype : boolean | ||
| 743 | Exceptions : none | ||
| 744 | Caller : internal | ||
| 745 | |||
| 746 | =cut | ||
| 747 | |||
| 748 | sub _supported { | ||
| 749 | my ($self, $system) = @_; | ||
| 750 | |||
| 751 | #construct the hash of supported features if it has not been already | ||
| 752 | unless(exists $self->{_supported}) { | ||
| 753 | $self->{_supported} = {}; | ||
| 754 | foreach my $coord_system ($self->coordinate_systems) { | ||
| 755 | $self->{_supported}->{$coord_system} = 1; | ||
| 756 | } | ||
| 757 | } | ||
| 758 | |||
| 759 | return $self->{_supported}->{$system}; | ||
| 760 | } | ||
| 761 | |||
| 762 | |||
| 763 | |||
| 764 | 1; |