| 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::AssemblyExceptionFeatureAdaptor | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | my $assembly_exception_feature_adaptor = | ||
| 38 | $database_adaptor->get_AssemblyExceptionFeatureAdaptor(); | ||
| 39 | |||
| 40 | @assembly_exception_features = | ||
| 41 | $assembly_exception_feature_adaptor->fetch_all_by_Slice($slice); | ||
| 42 | |||
| 43 | =head1 DESCRIPTION | ||
| 44 | |||
| 45 | Assembly Exception Feature Adaptor - database access for assembly | ||
| 46 | exception features. | ||
| 47 | |||
| 48 | =head1 METHODS | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | package Bio::EnsEMBL::DBSQL::AssemblyExceptionFeatureAdaptor; | ||
| 53 | $Bio::EnsEMBL::DBSQL::AssemblyExceptionFeatureAdaptor::VERSION = '114.0.0_50'; # TRIAL | ||
| 54 | $Bio::EnsEMBL::DBSQL::AssemblyExceptionFeatureAdaptor::VERSION = '114.0.050'; | ||
| 55 | use strict; | ||
| 56 | use warnings; | ||
| 57 | no warnings qw(uninitialized); | ||
| 58 | |||
| 59 | use Bio::EnsEMBL::DBSQL::BaseAdaptor; | ||
| 60 | use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor; | ||
| 61 | use Bio::EnsEMBL::AssemblyExceptionFeature; | ||
| 62 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 63 | use Bio::EnsEMBL::Utils::Cache; | ||
| 64 | |||
| 65 | our @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor); | ||
| 66 | |||
| 67 | # set the number of slices you'd like to cache | ||
| 68 | our $ASSEMBLY_EXCEPTION_FEATURE_CACHE_SIZE = 100; | ||
| 69 | |||
| 70 | =head2 new | ||
| 71 | |||
| 72 | Arg [1] : list of args @args | ||
| 73 | Superclass constructor arguments | ||
| 74 | Example : none | ||
| 75 | Description: Constructor which just initializes internal cache structures | ||
| 76 | Returntype : Bio::EnsEMBL::DBSQL::AssemblyExceptionFeatureAdaptor | ||
| 77 | Exceptions : none | ||
| 78 | Caller : implementing subclass constructors | ||
| 79 | Status : Stable | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub new { | ||
| 84 | my $caller = shift; | ||
| 85 | my $class = ref($caller) || $caller; | ||
| 86 | |||
| 87 | my $self = $class->SUPER::new(@_); | ||
| 88 | |||
| 89 | # initialize an LRU cache for slices | ||
| 90 | my %cache; | ||
| 91 | tie(%cache, 'Bio::EnsEMBL::Utils::Cache', | ||
| 92 | $ASSEMBLY_EXCEPTION_FEATURE_CACHE_SIZE); | ||
| 93 | |||
| 94 | $self->{'_aexc_slice_cache'} = \%cache; | ||
| 95 | |||
| 96 | return $self; | ||
| 97 | } | ||
| 98 | |||
| 99 | =head2 fetch_all | ||
| 100 | |||
| 101 | Arg [1] : none | ||
| 102 | Example : my @axfs = @{$axfa->fetch_all()}; | ||
| 103 | Description: Retrieves all assembly exception features which are in the | ||
| 104 | database and builds internal caches of the features. | ||
| 105 | Returntype : reference to list of Bio::EnsEMBL::AssemblyExceptionFeatures | ||
| 106 | Exceptions : none | ||
| 107 | Caller : fetch_by_dbID, fetch_by_Slice | ||
| 108 | Status : Stable | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub fetch_all { | ||
| 113 | my $self = shift; | ||
| 114 | |||
| 115 | # this is the "global" cache for all assembly exception features in the db | ||
| 116 | if(defined($self->{'_aexc_cache'})) { | ||
| 117 | return $self->{'_aexc_cache'}; | ||
| 118 | } | ||
| 119 | |||
| 120 | my $statement = qq( | ||
| 121 | SELECT ae.assembly_exception_id, | ||
| 122 | ae.seq_region_id, | ||
| 123 | ae.seq_region_start, | ||
| 124 | ae.seq_region_end, | ||
| 125 | ae.exc_type, | ||
| 126 | ae.exc_seq_region_id, | ||
| 127 | ae.exc_seq_region_start, | ||
| 128 | ae.exc_seq_region_end, | ||
| 129 | ae.ori | ||
| 130 | FROM assembly_exception ae, | ||
| 131 | coord_system cs, | ||
| 132 | seq_region sr | ||
| 133 | WHERE cs.species_id = ? | ||
| 134 | AND sr.coord_system_id = cs.coord_system_id | ||
| 135 | AND sr.seq_region_id = ae.seq_region_id); | ||
| 136 | |||
| 137 | my $sth = $self->prepare($statement); | ||
| 138 | |||
| 139 | $sth->bind_param( 1, $self->species_id(), SQL_INTEGER ); | ||
| 140 | |||
| 141 | $sth->execute(); | ||
| 142 | |||
| 143 | my ($ax_id, $sr_id, $sr_start, $sr_end, | ||
| 144 | $x_type, $x_sr_id, $x_sr_start, $x_sr_end, $ori); | ||
| 145 | |||
| 146 | $sth->bind_columns(\$ax_id, \$sr_id, \$sr_start, \$sr_end, | ||
| 147 | \$x_type, \$x_sr_id, \$x_sr_start, \$x_sr_end, \$ori); | ||
| 148 | |||
| 149 | my @features; | ||
| 150 | my $sa = $self->db()->get_SliceAdaptor(); | ||
| 151 | |||
| 152 | $self->{'_aexc_dbID_cache'} = {}; | ||
| 153 | |||
| 154 | while($sth->fetch()) { | ||
| 155 | my $slice = $sa->fetch_by_seq_region_id($sr_id); | ||
| 156 | my $x_slice = $sa->fetch_by_seq_region_id($x_sr_id); | ||
| 157 | |||
| 158 | # each row creates TWO features, each of which has alternate_slice | ||
| 159 | # pointing to the "other" one | ||
| 160 | |||
| 161 | |||
| 162 | my $a = Bio::EnsEMBL::AssemblyExceptionFeature->new | ||
| 163 | ('-dbID' => $ax_id, | ||
| 164 | '-start' => $sr_start, | ||
| 165 | '-end' => $sr_end, | ||
| 166 | '-strand' => 1, | ||
| 167 | '-adaptor' => $self, | ||
| 168 | '-slice' => $slice, | ||
| 169 | '-alternate_slice' => $x_slice->sub_Slice($x_sr_start, $x_sr_end), | ||
| 170 | '-type' => $x_type); | ||
| 171 | |||
| 172 | push @features, $a; | ||
| 173 | $self->{'_aexc_dbID_cache'}->{$ax_id} = $a; | ||
| 174 | |||
| 175 | push @features, Bio::EnsEMBL::AssemblyExceptionFeature->new | ||
| 176 | ('-dbID' => $ax_id, | ||
| 177 | '-start' => $x_sr_start, | ||
| 178 | '-end' => $x_sr_end, | ||
| 179 | '-strand' => 1, | ||
| 180 | '-adaptor' => $self, | ||
| 181 | '-slice' => $x_slice, | ||
| 182 | '-alternate_slice' => $slice->sub_Slice($sr_start, $sr_end), | ||
| 183 | '-type' => "$x_type REF" ); | ||
| 184 | } | ||
| 185 | |||
| 186 | $sth->finish(); | ||
| 187 | |||
| 188 | $self->{'_aexc_cache'} = \@features; | ||
| 189 | |||
| 190 | return \@features; | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | =head2 fetch_by_dbID | ||
| 195 | |||
| 196 | Arg [1] : int $dbID | ||
| 197 | Example : my $axf = $axfa->fetch_by_dbID(3); | ||
| 198 | Description: Retrieves a single assembly exception feature via its internal | ||
| 199 | identifier. Note that this only retrieves one of the two | ||
| 200 | assembly exception features which are represented by a single | ||
| 201 | row in the assembly_exception table. | ||
| 202 | Returntype : Bio::EnsEMBL::AssemblyExceptionFeature | ||
| 203 | Exceptions : none | ||
| 204 | Caller : general | ||
| 205 | Status : Stable | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub fetch_by_dbID { | ||
| 210 | my $self = shift; | ||
| 211 | my $dbID = shift; | ||
| 212 | |||
| 213 | if(!exists($self->{'_aexc_dbID_cache'})) { | ||
| 214 | # force loading of cache | ||
| 215 | $self->fetch_all(); | ||
| 216 | } | ||
| 217 | |||
| 218 | return $self->{'_aexc_dbID_cache'}->{$dbID}; | ||
| 219 | } | ||
| 220 | |||
| 221 | |||
| 222 | =head2 fetch_all_by_Slice | ||
| 223 | |||
| 224 | Arg [1] : Bio::EnsEMBL::Slice $slice | ||
| 225 | Example : my @axfs = @{$axfa->fetch_all_by_Slice($slice)}; | ||
| 226 | Description: Retrieves all assembly exception features which overlap the | ||
| 227 | provided slice. The returned features will be in coordinate | ||
| 228 | system of the slice. | ||
| 229 | Returntype : reference to list of Bio::EnsEMBL::AssemblyException features | ||
| 230 | Exceptions : none | ||
| 231 | Caller : Feature::get_all_alt_locations, general | ||
| 232 | Status : Stable | ||
| 233 | |||
| 234 | =cut | ||
| 235 | |||
| 236 | sub fetch_all_by_Slice { | ||
| 237 | my $self = shift; | ||
| 238 | my $slice = shift; | ||
| 239 | |||
| 240 | my $key= uc($slice->name()); | ||
| 241 | |||
| 242 | # return features from the slice cache if present | ||
| 243 | if(exists($self->{'_aexc_slice_cache'}->{$key})) { | ||
| 244 | return $self->{'_aexc_slice_cache'}->{$key}; | ||
| 245 | } | ||
| 246 | |||
| 247 | my $all_features = $self->fetch_all(); | ||
| 248 | |||
| 249 | my $mcc = $self->db()->get_MetaCoordContainer(); | ||
| 250 | my $css = $mcc->fetch_all_CoordSystems_by_feature_type('assembly_exception'); | ||
| 251 | |||
| 252 | my @features; | ||
| 253 | |||
| 254 | my $ma = $self->db()->get_AssemblyMapperAdaptor(); | ||
| 255 | |||
| 256 | foreach my $cs (@$css) { | ||
| 257 | my $mapper; | ||
| 258 | if($cs->equals($slice->coord_system)) { | ||
| 259 | $mapper = undef; | ||
| 260 | } else { | ||
| 261 | $mapper = $ma->fetch_by_CoordSystems($cs,$slice->coord_system()); | ||
| 262 | } | ||
| 263 | |||
| 264 | push @features, @{ $self->_remap($all_features, $mapper, $slice) }; | ||
| 265 | } | ||
| 266 | |||
| 267 | $self->{'_aexc_slice_cache'}->{$key} = \@features; | ||
| 268 | |||
| 269 | return \@features; | ||
| 270 | } | ||
| 271 | |||
| 272 | |||
| 273 | # | ||
| 274 | # Given a list of features checks if they are in the correct coord system | ||
| 275 | # by looking at the first features slice. If they are not then they are | ||
| 276 | # converted and placed on the slice. | ||
| 277 | # | ||
| 278 | # Note that this is a re-implementation of a method with the same name in | ||
| 279 | # BaseFeatureAdaptor, and in contrast to the latter which maps features in | ||
| 280 | # place, this method returns a remapped copy of each feature. The reason for | ||
| 281 | # this is to get around conflicts with caching. | ||
| 282 | # | ||
| 283 | sub _remap { | ||
| 284 | my ($self, $features, $mapper, $slice) = @_; | ||
| 285 | |||
| 286 | # check if any remapping is actually needed | ||
| 287 | if(@$features && (!$features->[0]->isa('Bio::EnsEMBL::Feature') || | ||
| 288 | $features->[0]->slice == $slice)) { | ||
| 289 | return $features; | ||
| 290 | } | ||
| 291 | # remapping has not been done, we have to do our own conversion from | ||
| 292 | # to slice coords | ||
| 293 | |||
| 294 | my @out; | ||
| 295 | |||
| 296 | my $slice_start = $slice->start(); | ||
| 297 | my $slice_end = $slice->end(); | ||
| 298 | my $slice_strand = $slice->strand(); | ||
| 299 | my $slice_cs = $slice->coord_system(); | ||
| 300 | |||
| 301 | my ($seq_region, $start, $end, $strand, $seq_region_name); | ||
| 302 | |||
| 303 | my $slice_seq_region = $slice->seq_region_name(); | ||
| 304 | |||
| 305 | foreach my $f (@$features) { | ||
| 306 | # since feats were obtained in contig coords, attached seq is a contig | ||
| 307 | my $fslice = $f->slice(); | ||
| 308 | if(!$fslice) { | ||
| 309 | throw("Feature does not have attached slice.\n"); | ||
| 310 | } | ||
| 311 | my $fseq_region = $fslice->seq_region_name(); | ||
| 312 | my $fcs = $fslice->coord_system(); | ||
| 313 | if(!$slice_cs->equals($fcs)) { | ||
| 314 | # slice of feature in different coord system, mapping required | ||
| 315 | ($seq_region, $start, $end, $strand) = | ||
| 316 | $mapper->fastmap($fseq_region,$f->start(),$f->end(),$f->strand(),$fcs); | ||
| 317 | # undefined start means gap | ||
| 318 | next if(!defined $start); | ||
| 319 | |||
| 320 | my $slice_adaptor = $self->db()->get_SliceAdaptor(); | ||
| 321 | $seq_region_name = $slice_adaptor->fetch_by_seq_region_id($seq_region)->seq_region_name; | ||
| 322 | |||
| 323 | } else { | ||
| 324 | $start = $f->start(); | ||
| 325 | $end = $f->end(); | ||
| 326 | $strand = $f->strand(); | ||
| 327 | $seq_region_name = $f->slice->seq_region_name(); | ||
| 328 | } | ||
| 329 | |||
| 330 | # maps to region outside desired area | ||
| 331 | next if ($start > $slice_end) || ($end < $slice_start) || | ||
| 332 | ($slice_seq_region ne $seq_region_name); | ||
| 333 | |||
| 334 | # create new copies of successfully mapped feaatures with shifted start, | ||
| 335 | # end and strand | ||
| 336 | my ($new_start, $new_end); | ||
| 337 | my $seq_region_len = $slice->seq_region_length(); | ||
| 338 | |||
| 339 | if ($slice_strand == 1) { # Positive strand | ||
| 340 | |||
| 341 | $new_start = $start - $slice_start + 1; | ||
| 342 | $new_end = $end - $slice_start + 1; | ||
| 343 | |||
| 344 | if ($slice->is_circular()) { | ||
| 345 | # Handle circular chromosomes. | ||
| 346 | |||
| 347 | if ($new_start > $new_end) { | ||
| 348 | # Looking at a feature overlapping the chromsome origin. | ||
| 349 | if ($new_end > $slice_start) { | ||
| 350 | # Looking at the region in the beginning of the chromosome. | ||
| 351 | $new_start -= $seq_region_len; | ||
| 352 | } | ||
| 353 | if ($new_end < 0) { | ||
| 354 | $new_end += $seq_region_len; | ||
| 355 | } | ||
| 356 | } else { | ||
| 357 | if ( $slice_start > $slice_end && $new_end < 0) { | ||
| 358 | # Looking at the region overlapping the chromosome | ||
| 359 | # origin and a feature which is at the beginning of the | ||
| 360 | # chromosome. | ||
| 361 | $new_start += $seq_region_len; | ||
| 362 | $new_end += $seq_region_len; | ||
| 363 | } | ||
| 364 | } | ||
| 365 | } ## end if ($dest_slice->is_circular...) | ||
| 366 | } else { # Negative strand | ||
| 367 | |||
| 368 | $new_start = $slice_end - $end + 1; | ||
| 369 | $new_end = $slice_end - $start + 1; | ||
| 370 | |||
| 371 | if ($slice->is_circular()) { | ||
| 372 | |||
| 373 | if ($slice_start > $slice_end) { | ||
| 374 | # slice spans origin or replication | ||
| 375 | |||
| 376 | if ($start >= $slice_start) { | ||
| 377 | $new_end += $seq_region_len; | ||
| 378 | $new_start += $seq_region_len if $end > $slice_start; | ||
| 379 | |||
| 380 | } elsif ($start <= $slice_end) { | ||
| 381 | # do nothing | ||
| 382 | } elsif ($end >= $slice_start) { | ||
| 383 | $new_start += $seq_region_len; | ||
| 384 | $new_end += $seq_region_len; | ||
| 385 | |||
| 386 | } elsif ($end <= $slice_end) { | ||
| 387 | $new_end += $seq_region_len if $new_end < 0; | ||
| 388 | } elsif ($start > $end) { | ||
| 389 | $new_end += $seq_region_len; | ||
| 390 | } else { | ||
| 391 | |||
| 392 | } | ||
| 393 | |||
| 394 | } else { | ||
| 395 | |||
| 396 | if ($start <= $slice_end and $end >= $slice_start) { | ||
| 397 | # do nothing | ||
| 398 | } elsif ($start > $end) { | ||
| 399 | if ($start <= $slice_end) { | ||
| 400 | |||
| 401 | $new_start -= $seq_region_len; | ||
| 402 | |||
| 403 | } elsif ($end >= $slice_start) { | ||
| 404 | $new_end += $seq_region_len; | ||
| 405 | } else { | ||
| 406 | |||
| 407 | } | ||
| 408 | } | ||
| 409 | } | ||
| 410 | |||
| 411 | } | ||
| 412 | |||
| 413 | } ## end else [ if ($dest_slice_strand...)] | ||
| 414 | push @out, Bio::EnsEMBL::AssemblyExceptionFeature->new( | ||
| 415 | '-dbID' => $f->dbID, | ||
| 416 | '-start' => $new_start, | ||
| 417 | '-end' => $new_end, | ||
| 418 | '-strand' => $strand * $slice_strand, | ||
| 419 | '-adaptor' => $self, | ||
| 420 | '-slice' => $slice, | ||
| 421 | '-alternate_slice' => $f->alternate_slice, | ||
| 422 | '-type' => $f->type, | ||
| 423 | ); | ||
| 424 | } # end foreach assembly exception | ||
| 425 | |||
| 426 | return \@out; | ||
| 427 | } | ||
| 428 | |||
| 429 | =head2 store | ||
| 430 | |||
| 431 | Arg[1] : Bio::EnsEMBL::AssemblyException $asx | ||
| 432 | Arg[2] : Bio::EnsEMBL::AssemblyException $asx2 | ||
| 433 | |||
| 434 | Example : $asx = Bio::EnsEMBL::AssemblyExceptionFeature->new(...) | ||
| 435 | $asx2 = Bio::EnsEMBL::AssemblyExceptionFeature->new(...) | ||
| 436 | $asx_seq_region_id = $asx_adaptor->store($asx); | ||
| 437 | Description: This stores a assembly exception feature in the | ||
| 438 | assembly_exception table and returns the assembly_exception_id. | ||
| 439 | Needs 2 features: one pointing to the Assembly_exception, and the | ||
| 440 | other pointing to the region in the reference that is being mapped to | ||
| 441 | Will check that start, end and type are defined, and the alternate | ||
| 442 | slice is present as well. | ||
| 443 | ReturnType: int | ||
| 444 | Exceptions: throw if assembly exception not defined (needs start, end, | ||
| 445 | type and alternate_slice) of if $asx not a Bio::EnsEMBL::AssemblyException | ||
| 446 | Caller: general | ||
| 447 | Status: Stable | ||
| 448 | |||
| 449 | =cut | ||
| 450 | |||
| 451 | sub store{ | ||
| 452 | my $self = shift; | ||
| 453 | my $asx = shift; | ||
| 454 | my $asx2 = shift; | ||
| 455 | |||
| 456 | |||
| 457 | if (! $asx->isa('Bio::EnsEMBL::AssemblyExceptionFeature')){ | ||
| 458 | throw("$asx is not a Ensembl assemlby exception -- not stored"); | ||
| 459 | } | ||
| 460 | #if already present, return ID in the database | ||
| 461 | my $db = $self->db(); | ||
| 462 | if ($asx->is_stored($db)){ | ||
| 463 | return $asx->dbID(); | ||
| 464 | } | ||
| 465 | #do some checkings for the object | ||
| 466 | #at the moment, the orientation is always 1 | ||
| 467 | if (! $asx->start || ! $asx->end ){ | ||
| 468 | throw("Assembly exception does not have coordinates"); | ||
| 469 | } | ||
| 470 | if ($asx->type !~ /PAR|HAP|PATCH_NOVEL|PATCH_FIX/){ | ||
| 471 | throw("Only types of assembly exception features valid are PAR, HAP, PATCH_FIX or PATCH_NOVEL"); | ||
| 472 | } | ||
| 473 | if ( !($asx->alternate_slice->isa('Bio::EnsEMBL::Slice')) ){ | ||
| 474 | throw("Alternate slice should be a Bio::EnsEMBL::Slice"); | ||
| 475 | } | ||
| 476 | #now check the other Assembly exception feature, the one pointing to the REF | ||
| 477 | # region | ||
| 478 | if (!$asx2->isa('Bio::EnsEMBL::AssemblyExceptionFeature')){ | ||
| 479 | throw("$asx2 is not a Ensembl assemlby exception -- not stored"); | ||
| 480 | } | ||
| 481 | if (! $asx2->start || ! $asx2->end ){ | ||
| 482 | throw("Assembly exception does not have coordinates"); | ||
| 483 | } | ||
| 484 | if ($asx2->type !~ /HAP REF|PAR REF|PATCH_NOVEL REF|PATCH_FIX REF/){ | ||
| 485 | throw("$asx2 should have type of assembly exception features HAP REF, PAR REF, PATCH_FIX REF or PATCH_NOVEL REF"); | ||
| 486 | } | ||
| 487 | if (! ($asx2->alternate_slice->isa('Bio::EnsEMBL::Slice')) ){ | ||
| 488 | throw("Alternate slice should be a Bio::EnsEMBL::Slice"); | ||
| 489 | } | ||
| 490 | #finally check that both features are pointing to each other slice | ||
| 491 | if ($asx->slice != $asx2->alternate_slice || $asx->alternate_slice != $asx2->slice){ | ||
| 492 | throw("Slice and alternate slice in both features are not pointing to each other"); | ||
| 493 | } | ||
| 494 | #prepare the SQL | ||
| 495 | my $asx_sql = q{ | ||
| 496 | INSERT INTO assembly_exception( seq_region_id, seq_region_start, | ||
| 497 | seq_region_end, | ||
| 498 | exc_type, exc_seq_region_id, | ||
| 499 | exc_seq_region_start, exc_seq_region_end, | ||
| 500 | ori) | ||
| 501 | VALUES (?, ?, ?, ?, ?, ?, ?, 1) | ||
| 502 | }; | ||
| 503 | |||
| 504 | my $asx_st = $self->prepare($asx_sql); | ||
| 505 | my $asx_id = undef; | ||
| 506 | my $asx_seq_region_id; | ||
| 507 | my $asx2_seq_region_id; | ||
| 508 | my $original = $asx; | ||
| 509 | my $original2 = $asx2; | ||
| 510 | #check all feature information | ||
| 511 | ($asx, $asx_seq_region_id) = $self->_pre_store($asx); | ||
| 512 | ($asx2, $asx2_seq_region_id) = $self->_pre_store($asx2); | ||
| 513 | |||
| 514 | #and store it | ||
| 515 | $asx_st->bind_param(1, $asx_seq_region_id, SQL_INTEGER); | ||
| 516 | $asx_st->bind_param(2, $asx->start(), SQL_INTEGER); | ||
| 517 | $asx_st->bind_param(3, $asx->end(), SQL_INTEGER); | ||
| 518 | $asx_st->bind_param(4, $asx->type(), SQL_VARCHAR); | ||
| 519 | $asx_st->bind_param(5, $asx2_seq_region_id, SQL_INTEGER); | ||
| 520 | $asx_st->bind_param(6, $asx2->start(), SQL_INTEGER); | ||
| 521 | $asx_st->bind_param(7, $asx2->end(), SQL_INTEGER); | ||
| 522 | |||
| 523 | $asx_st->execute(); | ||
| 524 | $asx_id = $self->last_insert_id('assembly_exception_id', undef, 'assembly_exception'); | ||
| 525 | |||
| 526 | #finally, update the dbID and adaptor of the asx and asx2 | ||
| 527 | $original->adaptor($self); | ||
| 528 | $original->dbID($asx_id); | ||
| 529 | $original2->adaptor($self); | ||
| 530 | $original2->dbID($asx_id); | ||
| 531 | #and finally update dbID cache with new assembly exception | ||
| 532 | $self->{'_aexc_dbID_cache'}->{$asx_id} = $original; | ||
| 533 | #and update the other caches as well | ||
| 534 | push @{$self->{'_aexc_slice_cache'}->{uc($asx->slice->name)}},$original, $original2; | ||
| 535 | push @{$self->{'_aexc_cache'}}, $original, $original2; | ||
| 536 | |||
| 537 | return $asx_id; | ||
| 538 | } | ||
| 539 | |||
| 540 | # | ||
| 541 | # Helper function containing some common feature storing functionality | ||
| 542 | # | ||
| 543 | # Given a Feature this will return a copy (or the same feature if no changes | ||
| 544 | # to the feature are needed) of the feature which is relative to the start | ||
| 545 | # of the seq_region it is on. The seq_region_id of the seq_region it is on | ||
| 546 | # is also returned. | ||
| 547 | # | ||
| 548 | # This method will also ensure that the database knows which coordinate | ||
| 549 | # systems that this feature is stored in. | ||
| 550 | # Since this adaptor doesn't inherit from BaseFeatureAdaptor, we need to copy | ||
| 551 | # the code | ||
| 552 | # | ||
| 553 | |||
| 554 | sub _pre_store { | ||
| 555 | my $self = shift; | ||
| 556 | my $feature = shift; | ||
| 557 | |||
| 558 | if(!ref($feature) || !$feature->isa('Bio::EnsEMBL::Feature')) { | ||
| 559 | throw('Expected Feature argument.'); | ||
| 560 | } | ||
| 561 | |||
| 562 | $self->_check_start_end_strand($feature->start(),$feature->end(), | ||
| 563 | $feature->strand()); | ||
| 564 | |||
| 565 | |||
| 566 | my $db = $self->db(); | ||
| 567 | |||
| 568 | my $slice_adaptor = $db->get_SliceAdaptor(); | ||
| 569 | my $slice = $feature->slice(); | ||
| 570 | |||
| 571 | if(!ref($slice) || !($slice->isa('Bio::EnsEMBL::Slice') or $slice->isa('Bio::EnsEMBL::LRGSlice')) ) { | ||
| 572 | throw('Feature must be attached to Slice to be stored.'); | ||
| 573 | } | ||
| 574 | |||
| 575 | # make sure feature coords are relative to start of entire seq_region | ||
| 576 | |||
| 577 | if($slice->start != 1 || $slice->strand != 1) { | ||
| 578 | #move feature onto a slice of the entire seq_region | ||
| 579 | $slice = $slice_adaptor->fetch_by_region($slice->coord_system->name(), | ||
| 580 | $slice->seq_region_name(), | ||
| 581 | undef, #start | ||
| 582 | undef, #end | ||
| 583 | undef, #strand | ||
| 584 | $slice->coord_system->version()); | ||
| 585 | |||
| 586 | $feature = $feature->transfer($slice); | ||
| 587 | |||
| 588 | if(!$feature) { | ||
| 589 | throw('Could not transfer Feature to slice of ' . | ||
| 590 | 'entire seq_region prior to storing'); | ||
| 591 | } | ||
| 592 | } | ||
| 593 | |||
| 594 | # Ensure this type of feature is known to be stored in this coord system. | ||
| 595 | my $cs = $slice->coord_system; | ||
| 596 | |||
| 597 | my $mcc = $db->get_MetaCoordContainer(); | ||
| 598 | |||
| 599 | $mcc->add_feature_type($cs, 'assembly_exception', $feature->length); | ||
| 600 | |||
| 601 | my $seq_region_id = $slice_adaptor->get_seq_region_id($slice); | ||
| 602 | |||
| 603 | if(!$seq_region_id) { | ||
| 604 | throw('Feature is associated with seq_region which is not in this DB.'); | ||
| 605 | } | ||
| 606 | |||
| 607 | return ($feature, $seq_region_id); | ||
| 608 | } | ||
| 609 | |||
| 610 | # | ||
| 611 | # helper function used to validate start/end/strand and | ||
| 612 | # hstart/hend/hstrand etc. | ||
| 613 | # | ||
| 614 | sub _check_start_end_strand { | ||
| 615 | my $self = shift; | ||
| 616 | my $start = shift; | ||
| 617 | my $end = shift; | ||
| 618 | my $strand = shift; | ||
| 619 | |||
| 620 | # | ||
| 621 | # Make sure that the start, end, strand are valid | ||
| 622 | # | ||
| 623 | if(int($start) != $start) { | ||
| 624 | throw("Invalid Feature start [$start]. Must be integer."); | ||
| 625 | } | ||
| 626 | if(int($end) != $end) { | ||
| 627 | throw("Invalid Feature end [$end]. Must be integer."); | ||
| 628 | } | ||
| 629 | if(int($strand) != $strand || $strand < -1 || $strand > 1) { | ||
| 630 | throw("Invalid Feature strand [$strand]. Must be -1, 0 or 1."); | ||
| 631 | } | ||
| 632 | if($end < $start) { | ||
| 633 | throw("Invalid Feature start/end [$start/$end]. Start must be less " . | ||
| 634 | "than or equal to end."); | ||
| 635 | } | ||
| 636 | |||
| 637 | return 1; | ||
| 638 | } | ||
| 639 | |||
| 640 | =head2 remove | ||
| 641 | |||
| 642 | Arg [1] : $asx Bio::EnsEMBL::AssemblyFeatureException | ||
| 643 | Example : $asx_adaptor->remove($asx); | ||
| 644 | Description: This removes a assembly exception feature from the database. | ||
| 645 | Returntype : none | ||
| 646 | Exceptions : thrown if $asx arg does not implement dbID(), or if | ||
| 647 | $asx->dbID is not a true value | ||
| 648 | Caller : general | ||
| 649 | Status : Stable | ||
| 650 | |||
| 651 | =cut | ||
| 652 | |||
| 653 | #again, this method is generic in BaseFeatureAdaptor, but since this class | ||
| 654 | #is not inheriting, need to copy&paste | ||
| 655 | sub remove { | ||
| 656 | my ($self, $feature) = @_; | ||
| 657 | |||
| 658 | if(!$feature || !ref($feature) || !$feature->isa('Bio::EnsEMBL::AssemblyExceptionFeature')) { | ||
| 659 | throw('AssemblyExceptionFeature argument is required'); | ||
| 660 | } | ||
| 661 | |||
| 662 | if(!$feature->is_stored($self->db)) { | ||
| 663 | throw("This feature is not stored in this database"); | ||
| 664 | } | ||
| 665 | |||
| 666 | my $asx_id = $feature->dbID(); | ||
| 667 | my $key = uc($feature->slice->name); | ||
| 668 | my $sth = $self->prepare("DELETE FROM assembly_exception WHERE assembly_exception_id = ?"); | ||
| 669 | $sth->bind_param(1,$feature->dbID,SQL_INTEGER); | ||
| 670 | $sth->execute(); | ||
| 671 | |||
| 672 | #and clear cache | ||
| 673 | #and finally update dbID cache | ||
| 674 | delete $self->{'_aexc_dbID_cache'}->{$asx_id}; | ||
| 675 | #and remove from cache feature | ||
| 676 | my @features; | ||
| 677 | foreach my $asx (@{$self->{'_aexc_slice_cache'}->{$key}}){ | ||
| 678 | if ($asx->dbID != $asx_id){ | ||
| 679 | push @features, $asx; | ||
| 680 | } | ||
| 681 | } | ||
| 682 | $self->{'_aexc_slice_cache'}->{$key} = \@features; | ||
| 683 | @features = (); | ||
| 684 | foreach my $asx (@{$self->{'_aexc_cache'}}){ | ||
| 685 | if ($asx->dbID != $asx_id){ | ||
| 686 | push @features, $asx; | ||
| 687 | } | ||
| 688 | } | ||
| 689 | $self->{'_aexc_cache'} = \@features; | ||
| 690 | |||
| 691 | #unset the feature dbID ad adaptor | ||
| 692 | $feature->dbID(undef); | ||
| 693 | $feature->adaptor(undef); | ||
| 694 | |||
| 695 | return; | ||
| 696 | } | ||
| 697 | |||
| 698 | |||
| 699 | 1; |