« EnsEMBL/External/ExternalFeatureAdaptor.pm
↑ Summary
EnsEMBL/DBSQL/ProteinAlignFeatureAdaptor.pm »
| 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::Map::DBSQL::MarkerFeatureAdaptor | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | This object is responisble for all database interaction involving marker | ||
| 40 | features including the fetching and storing of marker features. | ||
| 41 | |||
| 42 | The bulk of this objects' methods are inherited from | ||
| 43 | Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor | ||
| 44 | |||
| 45 | =head1 METHODS | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | package Bio::EnsEMBL::Map::DBSQL::MarkerFeatureAdaptor; | ||
| 50 | $Bio::EnsEMBL::Map::DBSQL::MarkerFeatureAdaptor::VERSION = '114.0.0_50'; # TRIAL | ||
| 51 | $Bio::EnsEMBL::Map::DBSQL::MarkerFeatureAdaptor::VERSION = '114.0.050'; | ||
| 52 | use strict; | ||
| 53 | |||
| 54 | use Bio::EnsEMBL::Map::MarkerFeature; | ||
| 55 | use Bio::EnsEMBL::Map::Marker; | ||
| 56 | use Bio::EnsEMBL::Map::MarkerSynonym; | ||
| 57 | use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor; | ||
| 58 | |||
| 59 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 60 | |||
| 61 | use vars qw(@ISA); | ||
| 62 | |||
| 63 | @ISA = qw(Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor); | ||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | =head2 fetch_all_by_Marker | ||
| 68 | |||
| 69 | Arg [1] : Bio::EnsEMBL::Map::Marker | ||
| 70 | Example : @ms = @{$marker_feature_adaptor->fetch_by_Marker($mrkr)}; | ||
| 71 | Description: Retrieves a list of MarkerFeatures for a given marker | ||
| 72 | Returntype : listref of Bio::EnsEMBL::MarkerFeatures | ||
| 73 | Exceptions : none | ||
| 74 | Caller : general | ||
| 75 | Status : stable | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub fetch_all_by_Marker { | ||
| 80 | my $self = shift; | ||
| 81 | my $marker = shift; | ||
| 82 | |||
| 83 | my $constraint = 'm.marker_id = ' . $marker->dbID; | ||
| 84 | |||
| 85 | return $self->generic_fetch($constraint, @_); | ||
| 86 | } | ||
| 87 | |||
| 88 | =head2 fetch_all_by_Slice_and_MarkerName | ||
| 89 | |||
| 90 | Arg [1] : Bio::EnsEMBL::Slice $slice | ||
| 91 | Arg [2] : string marker name | ||
| 92 | Example : @ms = @{$marker_feature_adaptor->fetch_all_by_Slice_and_MarkerName($slice, $name)}; | ||
| 93 | Description: Retrieves a list of MarkerFeatures for a given marker name | ||
| 94 | Returntype : listref of Bio::EnsEMBL::Map::MarkerFeatures | ||
| 95 | Exceptions : none | ||
| 96 | Caller : general | ||
| 97 | Status : stable | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub fetch_all_by_Slice_and_MarkerName { | ||
| 102 | my ($self, $slice, $name) = @_; | ||
| 103 | return unless $slice && $name; | ||
| 104 | |||
| 105 | my $constraint = 'ms.name = "' . $name . '"'; | ||
| 106 | my $results = $self->fetch_all_by_Slice_constraint($slice, $constraint); | ||
| 107 | return $results; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | =head2 fetch_all_by_Slice_and_priority | ||
| 112 | |||
| 113 | Arg [1] : Bio::EnsEMBL::Slice $slice | ||
| 114 | Arg [2] : (optional) int $priority | ||
| 115 | Arg [3] : (optional) int $map_weight | ||
| 116 | Arg [3] : (optional) string $logic_name | ||
| 117 | Example : @feats = @{$mfa->fetch_all_by_Slice_and_priority($slice,80,2)}; | ||
| 118 | Description: Retrieves all marker features above a specified threshold | ||
| 119 | priority which overlap the provided slice, below a | ||
| 120 | a specified map_weight. | ||
| 121 | Returntype : listref of Bio::EnsEMBL::Map::MarkerFeatures in slice coords | ||
| 122 | Exceptions : none | ||
| 123 | Caller : general | ||
| 124 | Status : stable | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | sub fetch_all_by_Slice_and_priority { | ||
| 129 | my ($self, $slice, $priority, $map_weight, @args) = @_; | ||
| 130 | |||
| 131 | my $constraint = ''; | ||
| 132 | if(defined $priority) { | ||
| 133 | $constraint = "m.priority > $priority"; | ||
| 134 | } | ||
| 135 | |||
| 136 | if(defined $map_weight) { | ||
| 137 | if($constraint) { | ||
| 138 | $constraint .= " AND mf.map_weight < $map_weight"; | ||
| 139 | } else { | ||
| 140 | $constraint = "mf.map_weight < $map_weight"; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | return $self->fetch_all_by_Slice_constraint($slice, $constraint, @args); | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | sub _columns { | ||
| 150 | my $self = shift; | ||
| 151 | |||
| 152 | return ('mf.marker_feature_id', 'mf.marker_id', | ||
| 153 | 'mf.seq_region_id', 'mf.seq_region_start', 'mf.seq_region_end', | ||
| 154 | 'mf.analysis_id', 'mf.map_weight', | ||
| 155 | 'm.left_primer', 'm.right_primer', 'm.min_primer_dist', | ||
| 156 | 'm.max_primer_dist', 'm.priority', 'm.type', 'ms.marker_synonym_id', | ||
| 157 | 'ms.name', 'ms.source'); | ||
| 158 | } | ||
| 159 | |||
| 160 | sub _tables { | ||
| 161 | my $self = shift; | ||
| 162 | |||
| 163 | return (['marker_feature', 'mf'], #primary table | ||
| 164 | ['marker', 'm'], | ||
| 165 | ['marker_synonym', 'ms']); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub _left_join { | ||
| 169 | my $self = shift; | ||
| 170 | |||
| 171 | return ( [ 'marker_synonym', | ||
| 172 | 'm.display_marker_synonym_id = ms.marker_synonym_id' ] ); | ||
| 173 | } | ||
| 174 | |||
| 175 | sub _default_where_clause { | ||
| 176 | my $self = shift; | ||
| 177 | |||
| 178 | return ('mf.marker_id = m.marker_id'); | ||
| 179 | } | ||
| 180 | |||
| 181 | sub _objs_from_sth { | ||
| 182 | my ($self, $sth, $mapper, $dest_slice) = @_; | ||
| 183 | |||
| 184 | my ($marker_feature_id, $marker_id, | ||
| 185 | $seq_region_id, $seq_region_start, $seq_region_end, | ||
| 186 | $analysis_id, $map_weight, | ||
| 187 | $left_primer, $right_primer, $min_primer_dist, $max_primer_dist, | ||
| 188 | $priority, $type, $ms_id, $ms_name, $ms_source); | ||
| 189 | |||
| 190 | #warning: ordering depends on _columns function implementation | ||
| 191 | $sth->bind_columns(\$marker_feature_id, \$marker_id, | ||
| 192 | \$seq_region_id, \$seq_region_start, \$seq_region_end, | ||
| 193 | \$analysis_id, \$map_weight, | ||
| 194 | \$left_primer, \$right_primer, \$min_primer_dist, \$max_primer_dist, | ||
| 195 | \$priority, \$type, \$ms_id, \$ms_name, \$ms_source); | ||
| 196 | |||
| 197 | my @out = (); | ||
| 198 | |||
| 199 | my %marker_cache; | ||
| 200 | my %slice_hash; | ||
| 201 | # my %sr_name_hash; | ||
| 202 | my %sr_cs_hash; | ||
| 203 | my %analysis_cache; | ||
| 204 | my $marker_adp = $self->db->get_MarkerAdaptor; | ||
| 205 | my $sa = $self->db->get_SliceAdaptor; | ||
| 206 | my $analysis_adp = $self->db->get_AnalysisAdaptor; | ||
| 207 | |||
| 208 | my $asm_cs; | ||
| 209 | my $cmp_cs; | ||
| 210 | my $asm_cs_vers; | ||
| 211 | my $asm_cs_name; | ||
| 212 | my $cmp_cs_vers; | ||
| 213 | my $cmp_cs_name; | ||
| 214 | if($mapper) { | ||
| 215 | $asm_cs = $mapper->assembled_CoordSystem(); | ||
| 216 | $cmp_cs = $mapper->component_CoordSystem(); | ||
| 217 | $asm_cs_name = $asm_cs->name(); | ||
| 218 | $asm_cs_vers = $asm_cs->version(); | ||
| 219 | $cmp_cs_name = $cmp_cs->name(); | ||
| 220 | $cmp_cs_vers = $cmp_cs->version(); | ||
| 221 | } | ||
| 222 | |||
| 223 | my $dest_slice_start; | ||
| 224 | my $dest_slice_end; | ||
| 225 | my $dest_slice_strand; | ||
| 226 | my $dest_slice_length; | ||
| 227 | if($dest_slice) { | ||
| 228 | $dest_slice_start = $dest_slice->start(); | ||
| 229 | $dest_slice_end = $dest_slice->end(); | ||
| 230 | $dest_slice_strand = $dest_slice->strand(); | ||
| 231 | $dest_slice_length = $dest_slice->length(); | ||
| 232 | } | ||
| 233 | |||
| 234 | FEATURE: while($sth->fetch) { | ||
| 235 | #create a new marker unless this one has been seen already | ||
| 236 | my $marker; | ||
| 237 | if(!($marker = $marker_cache{$marker_id})) { | ||
| 238 | #create a new marker synonym for the display synonym (if defined) | ||
| 239 | my $ms; | ||
| 240 | if($ms_id) { | ||
| 241 | $ms = Bio::EnsEMBL::Map::MarkerSynonym->new | ||
| 242 | ($ms_id, $ms_source, $ms_name); | ||
| 243 | } | ||
| 244 | |||
| 245 | #create a new marker | ||
| 246 | $marker = Bio::EnsEMBL::Map::Marker->new | ||
| 247 | ($marker_id, $marker_adp, | ||
| 248 | $left_primer, $right_primer, $min_primer_dist, $max_primer_dist, | ||
| 249 | $priority, $type, $ms); | ||
| 250 | $marker_cache{$marker_id} = $marker; | ||
| 251 | } | ||
| 252 | |||
| 253 | #get the slice object | ||
| 254 | my $slice = $slice_hash{$seq_region_id}; | ||
| 255 | |||
| 256 | if(!$slice) { | ||
| 257 | $slice = $sa->fetch_by_seq_region_id($seq_region_id); | ||
| 258 | $slice_hash{$seq_region_id} = $slice; | ||
| 259 | # $sr_name_hash{$seq_region_id} = $slice->seq_region_name(); | ||
| 260 | $sr_cs_hash{$seq_region_id} = $slice->coord_system(); | ||
| 261 | } | ||
| 262 | |||
| 263 | #retrieve analysis | ||
| 264 | my $analysis; | ||
| 265 | unless($analysis = $analysis_cache{$analysis_id}) { | ||
| 266 | $analysis = $analysis_adp->fetch_by_dbID($analysis_id); | ||
| 267 | $analysis_cache{$analysis_id} = $analysis; | ||
| 268 | } | ||
| 269 | |||
| 270 | # | ||
| 271 | # remap the feature coordinates to another coord system | ||
| 272 | # if a mapper was provided | ||
| 273 | # | ||
| 274 | if($mapper) { | ||
| 275 | # my $sr_name = $sr_name_hash{$seq_region_id}; | ||
| 276 | my $sr_cs = $sr_cs_hash{$seq_region_id}; | ||
| 277 | |||
| 278 | ($seq_region_id,$seq_region_start,$seq_region_end) = | ||
| 279 | $mapper->fastmap($slice->seq_region_name(), $seq_region_start, $seq_region_end, 0, $sr_cs); | ||
| 280 | |||
| 281 | #skip features that map to gaps or coord system boundaries | ||
| 282 | next FEATURE if(!defined($seq_region_id)); | ||
| 283 | |||
| 284 | #get a slice in the coord system we just mapped to | ||
| 285 | $slice = $slice_hash{"$seq_region_id"} ||= | ||
| 286 | $sa->fetch_by_seq_region_id($seq_region_id); | ||
| 287 | } | ||
| 288 | |||
| 289 | # | ||
| 290 | # If a destination slice was provided convert the coords | ||
| 291 | # If the dest_slice starts at 1 and is foward strand, nothing needs doing | ||
| 292 | # | ||
| 293 | if($dest_slice) { | ||
| 294 | my $seq_region_len = $dest_slice->seq_region_length(); | ||
| 295 | |||
| 296 | if ($dest_slice_strand == 1) { # Positive strand | ||
| 297 | |||
| 298 | $seq_region_start = $seq_region_start - $dest_slice_start + 1; | ||
| 299 | $seq_region_end = $seq_region_end - $dest_slice_start + 1; | ||
| 300 | |||
| 301 | if ($dest_slice->is_circular()) { | ||
| 302 | # Handle cicular chromosomes. | ||
| 303 | |||
| 304 | if ($seq_region_start > $seq_region_end) { | ||
| 305 | # Looking at a feature overlapping the chromsome origin. | ||
| 306 | |||
| 307 | if ($seq_region_end > $dest_slice_start) { | ||
| 308 | |||
| 309 | # Looking at the region in the beginning of the | ||
| 310 | # chromosome. | ||
| 311 | $seq_region_start -= $seq_region_len; | ||
| 312 | } | ||
| 313 | |||
| 314 | if ($seq_region_end < 0) { | ||
| 315 | $seq_region_end += $seq_region_len; | ||
| 316 | } | ||
| 317 | |||
| 318 | } else { | ||
| 319 | |||
| 320 | if ( $dest_slice_start > $dest_slice_end | ||
| 321 | && $seq_region_end < 0) { | ||
| 322 | # Looking at the region overlapping the chromosome | ||
| 323 | # origin and a feature which is at the beginning of the | ||
| 324 | # chromosome. | ||
| 325 | $seq_region_start += $seq_region_len; | ||
| 326 | $seq_region_end += $seq_region_len; | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | } ## end if ($dest_slice->is_circular...) | ||
| 331 | |||
| 332 | } else { # Negative strand | ||
| 333 | |||
| 334 | my $start = $dest_slice_end - $seq_region_end + 1; | ||
| 335 | my $end = $dest_slice_end - $seq_region_start + 1; | ||
| 336 | |||
| 337 | if ($dest_slice->is_circular()) { | ||
| 338 | |||
| 339 | if ($dest_slice_start > $dest_slice_end) { | ||
| 340 | # slice spans origin or replication | ||
| 341 | |||
| 342 | if ($seq_region_start >= $dest_slice_start) { | ||
| 343 | $end += $seq_region_len; | ||
| 344 | $start += $seq_region_len | ||
| 345 | if $seq_region_end > $dest_slice_start; | ||
| 346 | |||
| 347 | } elsif ($seq_region_start <= $dest_slice_end) { | ||
| 348 | # do nothing | ||
| 349 | } elsif ($seq_region_end >= $dest_slice_start) { | ||
| 350 | $start += $seq_region_len; | ||
| 351 | $end += $seq_region_len; | ||
| 352 | |||
| 353 | } elsif ($seq_region_end <= $dest_slice_end) { | ||
| 354 | |||
| 355 | $end += $seq_region_len | ||
| 356 | if $end < 0; | ||
| 357 | |||
| 358 | } elsif ($seq_region_start > $seq_region_end) { | ||
| 359 | |||
| 360 | $end += $seq_region_len; | ||
| 361 | |||
| 362 | } else { | ||
| 363 | |||
| 364 | } | ||
| 365 | |||
| 366 | } else { | ||
| 367 | |||
| 368 | if ($seq_region_start <= $dest_slice_end and $seq_region_end >= $dest_slice_start) { | ||
| 369 | # do nothing | ||
| 370 | } elsif ($seq_region_start > $seq_region_end) { | ||
| 371 | if ($seq_region_start <= $dest_slice_end) { | ||
| 372 | |||
| 373 | $start -= $seq_region_len; | ||
| 374 | |||
| 375 | } elsif ($seq_region_end >= $dest_slice_start) { | ||
| 376 | $end += $seq_region_len; | ||
| 377 | |||
| 378 | } else { | ||
| 379 | |||
| 380 | } | ||
| 381 | } | ||
| 382 | } | ||
| 383 | |||
| 384 | } | ||
| 385 | |||
| 386 | $seq_region_start = $start; | ||
| 387 | $seq_region_end = $end; | ||
| 388 | |||
| 389 | } ## end else [ if ($dest_slice_strand...)] | ||
| 390 | |||
| 391 | # throw away features off the end of the requested slice | ||
| 392 | if($seq_region_end < 1 || $seq_region_start > $dest_slice_length) { | ||
| 393 | next FEATURE; | ||
| 394 | } | ||
| 395 | |||
| 396 | $slice = $dest_slice; | ||
| 397 | } | ||
| 398 | |||
| 399 | #now create a new marker_feature using the marker | ||
| 400 | push @out, Bio::EnsEMBL::Map::MarkerFeature->new | ||
| 401 | ($marker_feature_id, $self, | ||
| 402 | $seq_region_start, $seq_region_end, $slice, | ||
| 403 | $analysis, $marker_id, $map_weight, $marker); | ||
| 404 | } | ||
| 405 | |||
| 406 | return \@out; | ||
| 407 | } | ||
| 408 | |||
| 409 | |||
| 410 | |||
| 411 | |||
| 412 | =head2 store | ||
| 413 | |||
| 414 | Arg [1] : Bio::EnsEMBL::Map::MarkerFeature | ||
| 415 | Example : $marker_feature_adaptor->store(@marker_features); | ||
| 416 | Description: Stores a list of marker features in this database. | ||
| 417 | The dbID and adaptor of each marker will be set on successful | ||
| 418 | storing. | ||
| 419 | Returntype : none | ||
| 420 | Exceptions : thrown if not all data needed for storing is populated in the | ||
| 421 | marker features | ||
| 422 | Caller : general | ||
| 423 | Status : stable | ||
| 424 | |||
| 425 | =cut | ||
| 426 | |||
| 427 | sub store { | ||
| 428 | my ($self, @mfs) = @_; | ||
| 429 | |||
| 430 | foreach my $mf (@mfs) { | ||
| 431 | |||
| 432 | # | ||
| 433 | # Sanity checking! | ||
| 434 | # | ||
| 435 | if(!ref($mf) || !$mf->isa('Bio::EnsEMBL::Map::MarkerFeature')) { | ||
| 436 | $self->throw("Incorrect argument [$mf] to store. Expected " . | ||
| 437 | 'Bio::EnsEMBL::Map::MarkerFeature'); | ||
| 438 | } | ||
| 439 | |||
| 440 | #don't store this feature if it has already been stored | ||
| 441 | if($mf->is_stored($self->db())) { | ||
| 442 | warning('MarkerFeature ['.$mf->dbID.'] is already stored in this DB.'); | ||
| 443 | next; | ||
| 444 | } | ||
| 445 | |||
| 446 | # Get/test the marker | ||
| 447 | my $marker = $mf->marker; | ||
| 448 | if(!$marker || !ref($marker) || | ||
| 449 | !$marker->isa('Bio::EnsEMBL::Map::Marker')) { | ||
| 450 | throw('Cannot store MarkerFeature without an associated Marker'); | ||
| 451 | } | ||
| 452 | |||
| 453 | #store the marker if it has not been stored yet | ||
| 454 | if(!$marker->is_stored($self->db())) { | ||
| 455 | my $marker_adaptor = $self->db->get_adaptor('Marker'); | ||
| 456 | $marker_adaptor->store($marker); | ||
| 457 | } | ||
| 458 | my $marker_id = $marker->dbID || | ||
| 459 | throw('Associated Marker must have dbID to store MarkerFeature'); | ||
| 460 | |||
| 461 | # Get/test the analysis | ||
| 462 | my $analysis = $mf->analysis; | ||
| 463 | if(!$analysis || !ref($analysis) || | ||
| 464 | !$analysis->isa('Bio::EnsEMBL::Analysis')) { | ||
| 465 | throw('Cannot store MarkerFeature without an associated Analysis'); | ||
| 466 | } | ||
| 467 | |||
| 468 | #store the analysis if it has not been stored yet | ||
| 469 | if(!$analysis->is_stored($self->db())) { | ||
| 470 | my $analysis_adaptor = $self->db->get_adaptor('Analysis'); | ||
| 471 | $analysis_adaptor->store($mf->analysis()); | ||
| 472 | } | ||
| 473 | my $analysis_id = $analysis->dbID || | ||
| 474 | throw('Associated Analysis must have dbID to store MarkerFeature'); | ||
| 475 | |||
| 476 | # Store the marker feature itself | ||
| 477 | my $original = $mf; | ||
| 478 | my $seq_region_id; | ||
| 479 | ($mf, $seq_region_id) = $self->_pre_store($mf); | ||
| 480 | |||
| 481 | my $sth = | ||
| 482 | $self->prepare("INSERT INTO marker_feature (marker_id, | ||
| 483 | seq_region_id, seq_region_start, seq_region_end, | ||
| 484 | analysis_id, map_weight) | ||
| 485 | VALUES (?, ?, ?, ?, ?, ?)"); | ||
| 486 | $sth->execute($marker_id, | ||
| 487 | $seq_region_id, $mf->start, $mf->end, | ||
| 488 | $analysis_id, $mf->map_weight || 0); | ||
| 489 | |||
| 490 | my $dbID = $self->last_insert_id('marker_feature_id', undef, 'marker_feature'); | ||
| 491 | |||
| 492 | $original->dbID($dbID); | ||
| 493 | $original->adaptor($self); | ||
| 494 | } | ||
| 495 | } | ||
| 496 | |||
| 497 | |||
| 498 | 1; |