| 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::MiscFeatureAdaptor | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | $mfa = $database_adaptor->get_MiscFeatureAdaptor(); | ||
| 38 | |||
| 39 | # retrieve a misc feature by its dbID | ||
| 40 | my $misc_feat = $mfa->fetch_by_dbID(1234); | ||
| 41 | |||
| 42 | # retrieve all misc features in a given region | ||
| 43 | my @misc_feats = @{ $mfa->fetch_all_by_Slice($slice) }; | ||
| 44 | |||
| 45 | # retrieve all misc features in a given region with a given set code | ||
| 46 | my @misc_clones = | ||
| 47 | @{ $mfa->fetch_all_by_Slice_and_set_code('cloneset') }; | ||
| 48 | |||
| 49 | # store some misc features in the database | ||
| 50 | $mfa->store(@misc_features); | ||
| 51 | |||
| 52 | =head1 DESCRIPTION | ||
| 53 | |||
| 54 | This is an adaptor for the retrieval and storage of MiscFeatures. | ||
| 55 | Misc Features are extremely generic features that can be added with | ||
| 56 | minimal effort to the database. Currently misc features are used to | ||
| 57 | describe the locations of clone sets and tiling path information, | ||
| 58 | but arbitrary features can be stored. Misc features are grouped | ||
| 59 | into sets and can be fetched according to their grouping using the | ||
| 60 | fetch_all_by_Slice_and_set_code and fetch_all_by_set_code methods. | ||
| 61 | MiscFeatures may belong to more than one set. | ||
| 62 | |||
| 63 | =head1 METHODS | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | package Bio::EnsEMBL::DBSQL::MiscFeatureAdaptor; | ||
| 68 | $Bio::EnsEMBL::DBSQL::MiscFeatureAdaptor::VERSION = '114.0.0_50'; # TRIAL | ||
| 69 | $Bio::EnsEMBL::DBSQL::MiscFeatureAdaptor::VERSION = '114.0.050'; | ||
| 70 | use strict; | ||
| 71 | use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor; | ||
| 72 | use Bio::EnsEMBL::MiscFeature; | ||
| 73 | use Bio::EnsEMBL::Attribute; | ||
| 74 | use Bio::EnsEMBL::MiscSet; | ||
| 75 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 76 | |||
| 77 | use vars qw(@ISA); | ||
| 78 | |||
| 79 | @ISA = qw(Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor); | ||
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | =head2 fetch_all_by_Slice_and_set_code | ||
| 84 | |||
| 85 | Arg [1] : Bio::EnsEMBL::Slice $slice | ||
| 86 | A slice representing the region to fetch from | ||
| 87 | Arg [2...] : string $set_code | ||
| 88 | The code of the set to retrieve features from | ||
| 89 | Example : @feats = @{$mfa->fetch_all_by_Slice_and_set_code('cloneset')}; | ||
| 90 | Description: Retrieves a set of MiscFeatures which have a particular set code | ||
| 91 | and which lie in a particular region. All features with the | ||
| 92 | provide set code and which overlap the given slice are returned. | ||
| 93 | Returntype : listref of Bio::EnsEMBL::MiscFeatures | ||
| 94 | Exceptions : throw if set_code is not provided | ||
| 95 | warning if no set for provided set code exists | ||
| 96 | Caller : general | ||
| 97 | Status : Stable | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub fetch_all_by_Slice_and_set_code { | ||
| 102 | my $self = shift; | ||
| 103 | my $slice = shift; | ||
| 104 | |||
| 105 | throw('Set code argument is required.') unless @_; | ||
| 106 | |||
| 107 | my $msa = $self->db->get_MiscSetAdaptor(); | ||
| 108 | my @sets = (); | ||
| 109 | my $max_len = 0; | ||
| 110 | foreach my $set_code (@_) { | ||
| 111 | my $set = $msa->fetch_by_code($set_code); | ||
| 112 | if($set) { | ||
| 113 | $max_len = $set->longest_feature if $set->longest_feature > $max_len; | ||
| 114 | push @sets, $set->dbID; | ||
| 115 | } else { | ||
| 116 | warning("No misc_set with code [$set_code] exists"); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | my $constraint; | ||
| 120 | if( @sets > 1 ) { | ||
| 121 | $constraint = " mfms.misc_set_id in ( @{[join ',',@sets]} ) "; | ||
| 122 | } elsif( @sets == 1 ) { | ||
| 123 | $constraint = " mfms.misc_set_id = $sets[0] "; | ||
| 124 | } else { | ||
| 125 | return []; | ||
| 126 | } | ||
| 127 | |||
| 128 | $self->_max_feature_length($max_len); | ||
| 129 | |||
| 130 | my $results = $self->fetch_all_by_Slice_constraint($slice, $constraint); | ||
| 131 | |||
| 132 | $self->_max_feature_length(undef); | ||
| 133 | |||
| 134 | return $results; | ||
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | =head2 fetch_all_by_attribute_type_value | ||
| 140 | |||
| 141 | Arg [1] : string $attrib_type_code | ||
| 142 | The code of the attribute type to fetch features for | ||
| 143 | Arg [2] : (optional) string $attrib_value | ||
| 144 | The value of the attribute to fetch features for | ||
| 145 | Example : | ||
| 146 | #get all misc features that have an embl accession | ||
| 147 | @feats = @{$mfa->fetch_all_by_attrib_type_value('embl_acc')}; | ||
| 148 | #get the misc feature with synonym 'AL014121' | ||
| 149 | ($feat)=@{$mfa->fetch_all_by_attrib_type_value('synonym','AL014121'); | ||
| 150 | Description: Retrieves MiscFeatures which have a particular attribute. | ||
| 151 | If the attribute value argument is also provided only | ||
| 152 | features which have the attribute AND a particular value | ||
| 153 | are returned. The features are returned in their native | ||
| 154 | coordinate system (i.e. the coordinate system that they | ||
| 155 | are stored in). | ||
| 156 | Returntype : listref of Bio::EnsEMBL::MiscFeatures | ||
| 157 | Exceptions : throw if attrib_type code arg is not provided | ||
| 158 | Caller : general | ||
| 159 | Status : Stable | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | sub fetch_all_by_attribute_type_value { | ||
| 164 | my $self = shift; | ||
| 165 | my $attrib_type_code = shift; | ||
| 166 | my $attrib_value = shift; | ||
| 167 | |||
| 168 | throw("Attrib type code argument is required.") | ||
| 169 | if ( !$attrib_type_code ); | ||
| 170 | |||
| 171 | # Need to do 2 queries so that all of the ids come back with the | ||
| 172 | # features. The problem with adding attrib constraints to filter the | ||
| 173 | # misc_features which come back is that not all of the attributes will | ||
| 174 | # come back | ||
| 175 | |||
| 176 | my $sql = qq( | ||
| 177 | SELECT DISTINCT | ||
| 178 | ma.misc_feature_id | ||
| 179 | FROM misc_attrib ma, | ||
| 180 | attrib_type at, | ||
| 181 | misc_feature mf, | ||
| 182 | seq_region sr, | ||
| 183 | coord_system cs | ||
| 184 | WHERE ma.attrib_type_id = at.attrib_type_id | ||
| 185 | AND at.code = ? | ||
| 186 | AND ma.misc_feature_id = mf.misc_feature_id | ||
| 187 | AND mf.seq_region_id = sr.seq_region_id | ||
| 188 | AND sr.coord_system_id = cs.coord_system_id | ||
| 189 | AND cs.species_id = ?); | ||
| 190 | |||
| 191 | if ($attrib_value) { | ||
| 192 | $sql .= " AND ma.value = ?"; | ||
| 193 | } | ||
| 194 | |||
| 195 | my $sth = $self->prepare($sql); | ||
| 196 | |||
| 197 | $sth->bind_param( 1, $attrib_type_code, SQL_VARCHAR ); | ||
| 198 | $sth->bind_param( 2, $self->species_id(), SQL_INTEGER ); | ||
| 199 | if ($attrib_value) { | ||
| 200 | $sth->bind_param( 3, $attrib_value, SQL_VARCHAR ); | ||
| 201 | } | ||
| 202 | |||
| 203 | $sth->execute(); | ||
| 204 | |||
| 205 | my @ids = map { $_->[0] } @{ $sth->fetchall_arrayref() }; | ||
| 206 | |||
| 207 | $sth->finish(); | ||
| 208 | |||
| 209 | # Construct constraints from the list of ids. Split ids into groups | ||
| 210 | # of 1000 to ensure that the query is not too big. | ||
| 211 | my @constraints; | ||
| 212 | while (@ids) { | ||
| 213 | my @subset = splice( @ids, 0, 1000 ); | ||
| 214 | if ( @subset == 1 ) { | ||
| 215 | push @constraints, "mf.misc_feature_id = $subset[0]"; | ||
| 216 | } else { | ||
| 217 | my $id_str = join( ',', @subset ); | ||
| 218 | push @constraints, "mf.misc_feature_id in ($id_str)"; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | my @results; | ||
| 223 | foreach my $constraint (@constraints) { | ||
| 224 | push @results, @{ $self->generic_fetch($constraint) }; | ||
| 225 | } | ||
| 226 | |||
| 227 | return \@results; | ||
| 228 | } ## end sub fetch_all_by_attribute_type_value | ||
| 229 | |||
| 230 | |||
| 231 | =head2 fetch_by_attribute_set_value | ||
| 232 | |||
| 233 | Arg [1] : string $attrib_type_code | ||
| 234 | The code of the attribute type to fetch features for | ||
| 235 | Arg [2] : (optional) string $attrib_value | ||
| 236 | The value of the attribute to fetch features for | ||
| 237 | Arg [3] : (optional) string $misc_set | ||
| 238 | The name of the set to which the feature belongs | ||
| 239 | Example : | ||
| 240 | $feat = $mfa->fetch_by_attribute_set_value('clone', 'RP11-411G9', 'tilepath'); | ||
| 241 | # Get the clone belonging to the tilepath | ||
| 242 | Description: Retrieves MiscFeatures which have a particular attribute. | ||
| 243 | If the attribute value argument is also provided only | ||
| 244 | features which have the attribute AND a particular value | ||
| 245 | are returned. The features are returned in their native | ||
| 246 | coordinate system (i.e. the coordinate system that they | ||
| 247 | are stored in). | ||
| 248 | Returntype : listref of Bio::EnsEMBL::MiscFeatures | ||
| 249 | Exceptions : throw if attrib_type code arg is not provided | ||
| 250 | Caller : general | ||
| 251 | Status : Stable | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | sub fetch_by_attribute_set_value { | ||
| 256 | my $self = shift; | ||
| 257 | my $attrib_type_code = shift; | ||
| 258 | my $attrib_value = shift; | ||
| 259 | my $misc_set = shift; | ||
| 260 | |||
| 261 | throw("Attrib type code argument is required.") | ||
| 262 | if ( !$attrib_type_code ); | ||
| 263 | |||
| 264 | # Need to do 2 queries so that all of the ids come back with the | ||
| 265 | # features. The problem with adding attrib constraints to filter the | ||
| 266 | # misc_features which come back is that not all of the attributes will | ||
| 267 | # come back | ||
| 268 | |||
| 269 | my $sql = qq( | ||
| 270 | SELECT DISTINCT | ||
| 271 | ma.misc_feature_id | ||
| 272 | FROM misc_attrib ma, | ||
| 273 | attrib_type at, | ||
| 274 | misc_feature mf, | ||
| 275 | misc_feature_misc_set mfs, | ||
| 276 | misc_set ms, | ||
| 277 | seq_region sr, | ||
| 278 | coord_system cs | ||
| 279 | WHERE ma.attrib_type_id = at.attrib_type_id | ||
| 280 | AND at.code = ? | ||
| 281 | AND ma.misc_feature_id = mf.misc_feature_id | ||
| 282 | AND mf.misc_feature_id = mfs.misc_feature_id | ||
| 283 | AND mfs.misc_set_id = ms.misc_set_id | ||
| 284 | AND mf.seq_region_id = sr.seq_region_id | ||
| 285 | AND sr.coord_system_id = cs.coord_system_id | ||
| 286 | AND ma.value = ? | ||
| 287 | AND ms.code = ? | ||
| 288 | AND cs.species_id = ?); | ||
| 289 | |||
| 290 | my $sth = $self->prepare($sql); | ||
| 291 | |||
| 292 | $sth->bind_param( 1, $attrib_type_code, SQL_VARCHAR ); | ||
| 293 | $sth->bind_param( 2, $attrib_value, SQL_VARCHAR ); | ||
| 294 | $sth->bind_param( 3, $misc_set, SQL_VARCHAR ); | ||
| 295 | $sth->bind_param( 4, $self->species_id(), SQL_INTEGER ); | ||
| 296 | |||
| 297 | $sth->execute(); | ||
| 298 | |||
| 299 | my ($id) = $sth->fetchrow_array(); | ||
| 300 | |||
| 301 | if (!$id) { | ||
| 302 | return; | ||
| 303 | } | ||
| 304 | |||
| 305 | $sth->finish(); | ||
| 306 | |||
| 307 | my $constraint = "mf.misc_feature_id = $id"; | ||
| 308 | |||
| 309 | my ($result) = @{$self->generic_fetch($constraint)}; | ||
| 310 | |||
| 311 | return $result; | ||
| 312 | } ## end sub fetch_by_attribute_set_value | ||
| 313 | |||
| 314 | |||
| 315 | #_tables | ||
| 316 | # | ||
| 317 | # Arg [1] : none | ||
| 318 | # Example : none | ||
| 319 | # Description: PROTECTED Implementation of abstract superclass method to | ||
| 320 | # provide the name of the tables to query | ||
| 321 | # Returntype : string | ||
| 322 | # Exceptions : none | ||
| 323 | # Caller : internal | ||
| 324 | |||
| 325 | |||
| 326 | sub _tables { | ||
| 327 | my $self = shift; | ||
| 328 | |||
| 329 | return (['misc_feature', 'mf'], | ||
| 330 | ['misc_feature_misc_set', 'mfms'], | ||
| 331 | ['misc_attrib', 'ma'], | ||
| 332 | ['attrib_type', 'at']); | ||
| 333 | } | ||
| 334 | |||
| 335 | |||
| 336 | #_columns | ||
| 337 | |||
| 338 | # Arg [1] : none | ||
| 339 | # Example : none | ||
| 340 | # Description: PROTECTED Implementation of abstract superclass method to | ||
| 341 | # provide the name of the columns to query | ||
| 342 | # Returntype : list of strings | ||
| 343 | # Exceptions : none | ||
| 344 | # Caller : internal | ||
| 345 | |||
| 346 | sub _columns { | ||
| 347 | my $self = shift; | ||
| 348 | |||
| 349 | #warning _objs_from_sth implementation depends on ordering | ||
| 350 | return qw (mf.misc_feature_id | ||
| 351 | mf.seq_region_id | ||
| 352 | mf.seq_region_start | ||
| 353 | mf.seq_region_end | ||
| 354 | mf.seq_region_strand | ||
| 355 | ma.value | ||
| 356 | at.code | ||
| 357 | mfms.misc_set_id | ||
| 358 | at.name | ||
| 359 | at.description); | ||
| 360 | } | ||
| 361 | |||
| 362 | |||
| 363 | |||
| 364 | # _default_where_clause | ||
| 365 | |||
| 366 | # Arg [1] : none | ||
| 367 | # Example : none | ||
| 368 | # Description: Overrides superclass method to provide an additional | ||
| 369 | # table joining constraint before the SQL query is performed. | ||
| 370 | # Returntype : string | ||
| 371 | # Exceptions : none | ||
| 372 | # Caller : generic_fetch | ||
| 373 | |||
| 374 | sub _default_where_clause { | ||
| 375 | my $self = shift; | ||
| 376 | |||
| 377 | return ''; | ||
| 378 | } | ||
| 379 | |||
| 380 | |||
| 381 | sub _left_join { | ||
| 382 | my $self = shift; | ||
| 383 | |||
| 384 | return( | ||
| 385 | ['misc_feature_misc_set','mf.misc_feature_id = mfms.misc_feature_id'], | ||
| 386 | ['misc_attrib', 'mf.misc_feature_id = ma.misc_feature_id'], | ||
| 387 | ['attrib_type','ma.attrib_type_id = at.attrib_type_id']); | ||
| 388 | } | ||
| 389 | |||
| 390 | |||
| 391 | sub _final_clause { | ||
| 392 | my $self = shift; | ||
| 393 | |||
| 394 | return " ORDER BY mf.misc_feature_id"; | ||
| 395 | } | ||
| 396 | |||
| 397 | |||
| 398 | # _objs_from_sth | ||
| 399 | |||
| 400 | # Arg [1] : StatementHandle $sth | ||
| 401 | # Example : none | ||
| 402 | # Description: PROTECTED implementation of abstract superclass method. | ||
| 403 | # responsible for the creation of MiscFeatures from a | ||
| 404 | # hashref generated from an SQL query | ||
| 405 | # Returntype : listref of Bio::EnsEMBL::MiscFeatures | ||
| 406 | # Exceptions : none | ||
| 407 | # Caller : internal | ||
| 408 | |||
| 409 | sub _objs_from_sth { | ||
| 410 | my ($self, $sth, $mapper, $dest_slice) = @_; | ||
| 411 | |||
| 412 | # | ||
| 413 | # This code is ugly because an attempt has been made to remove as many | ||
| 414 | # function calls as possible for speed purposes. Thus many caches and | ||
| 415 | # a fair bit of gymnastics is used. | ||
| 416 | # | ||
| 417 | |||
| 418 | my $sa = $self->db()->get_SliceAdaptor(); | ||
| 419 | my $msa = $self->db()->get_MiscSetAdaptor(); | ||
| 420 | |||
| 421 | my @features; | ||
| 422 | my %ms_hash; | ||
| 423 | my %slice_hash; | ||
| 424 | my %sr_name_hash; | ||
| 425 | my %sr_cs_hash; | ||
| 426 | |||
| 427 | my( | ||
| 428 | $misc_feature_id, $seq_region_id, $seq_region_start, | ||
| 429 | $seq_region_end, $seq_region_strand, | ||
| 430 | $attrib_value, $attrib_type_code, $misc_set_id, | ||
| 431 | $attrib_type_name, $attrib_type_description ); | ||
| 432 | |||
| 433 | $sth->bind_columns( \$misc_feature_id, \$seq_region_id, \$seq_region_start, | ||
| 434 | \$seq_region_end, \$seq_region_strand, | ||
| 435 | \$attrib_value, \$attrib_type_code,\$misc_set_id, | ||
| 436 | \$attrib_type_name, \$attrib_type_description ); | ||
| 437 | |||
| 438 | my $dest_slice_start; | ||
| 439 | my $dest_slice_end; | ||
| 440 | my $dest_slice_strand; | ||
| 441 | my $dest_slice_length; | ||
| 442 | my $dest_slice_cs; | ||
| 443 | my $dest_slice_sr_name; | ||
| 444 | my $dest_slice_sr_id; | ||
| 445 | my $asma; | ||
| 446 | |||
| 447 | if ($dest_slice) { | ||
| 448 | $dest_slice_start = $dest_slice->start(); | ||
| 449 | $dest_slice_end = $dest_slice->end(); | ||
| 450 | $dest_slice_strand = $dest_slice->strand(); | ||
| 451 | $dest_slice_length = $dest_slice->length(); | ||
| 452 | $dest_slice_cs = $dest_slice->coord_system(); | ||
| 453 | $dest_slice_sr_name = $dest_slice->seq_region_name(); | ||
| 454 | $dest_slice_sr_id = $dest_slice->get_seq_region_id(); | ||
| 455 | $asma = $self->db->get_AssemblyMapperAdaptor(); | ||
| 456 | } | ||
| 457 | |||
| 458 | my $current = -1; | ||
| 459 | my $throw_away = -1; | ||
| 460 | my $feat; | ||
| 461 | my $feat_misc_sets; | ||
| 462 | my $feat_attribs; | ||
| 463 | my $seen_attribs; | ||
| 464 | |||
| 465 | FEATURE: while($sth->fetch()) { | ||
| 466 | #if this feature is not being used, skip all rows related to it | ||
| 467 | next if($throw_away == $misc_feature_id); | ||
| 468 | |||
| 469 | if ($current == $misc_feature_id) { | ||
| 470 | #still working on building up attributes and sets for current feature | ||
| 471 | |||
| 472 | #if there is a misc_set, add it to the current feature | ||
| 473 | if ($misc_set_id) { | ||
| 474 | my $misc_set = $ms_hash{$misc_set_id} ||= | ||
| 475 | $msa->fetch_by_dbID($misc_set_id); | ||
| 476 | if ( ! exists $feat_misc_sets->{$misc_set->{'code'}} ) { | ||
| 477 | $feat->add_MiscSet( $misc_set ); | ||
| 478 | $feat_misc_sets->{$misc_set->{'code'}} = $misc_set; | ||
| 479 | } | ||
| 480 | } | ||
| 481 | |||
| 482 | #if there is a new attribute add it to the current feature | ||
| 483 | if ($attrib_value && $attrib_type_code && !$seen_attribs->{"$attrib_type_code:$attrib_value"}) { | ||
| 484 | my $attrib = Bio::EnsEMBL::Attribute->new | ||
| 485 | ( -CODE => $attrib_type_code, | ||
| 486 | -NAME => $attrib_type_name, | ||
| 487 | -DESC => $attrib_type_description, | ||
| 488 | -VALUE => $attrib_value | ||
| 489 | ); | ||
| 490 | |||
| 491 | |||
| 492 | $feat_attribs ||= []; | ||
| 493 | push @$feat_attribs, $attrib; | ||
| 494 | $seen_attribs->{"$attrib_type_code:$attrib_value"} = 1; | ||
| 495 | } | ||
| 496 | |||
| 497 | } else { | ||
| 498 | if ($feat) { | ||
| 499 | #start working on a new feature, discard references to last one | ||
| 500 | $feat = {}; | ||
| 501 | $feat_attribs = []; | ||
| 502 | $feat_misc_sets = {}; | ||
| 503 | $seen_attribs = {}; | ||
| 504 | } | ||
| 505 | |||
| 506 | $current = $misc_feature_id; | ||
| 507 | #need to get the internal_seq_region, if present | ||
| 508 | $seq_region_id = $self->get_seq_region_id_internal($seq_region_id); | ||
| 509 | my $slice = $slice_hash{"ID:".$seq_region_id}; | ||
| 510 | |||
| 511 | if (!$slice) { | ||
| 512 | $slice = $sa->fetch_by_seq_region_id($seq_region_id); | ||
| 513 | $slice_hash{"ID:".$seq_region_id} = $slice; | ||
| 514 | $sr_name_hash{$seq_region_id} = $slice->seq_region_name(); | ||
| 515 | $sr_cs_hash{$seq_region_id} = $slice->coord_system(); | ||
| 516 | } | ||
| 517 | |||
| 518 | if(!$mapper && $dest_slice && !$dest_slice_cs->equals($slice->coord_system)) { | ||
| 519 | $mapper = $asma->fetch_by_CoordSystems($dest_slice_cs, $slice->coord_system); | ||
| 520 | } | ||
| 521 | |||
| 522 | my $sr_name = $sr_name_hash{$seq_region_id}; | ||
| 523 | my $sr_cs = $sr_cs_hash{$seq_region_id}; | ||
| 524 | |||
| 525 | # | ||
| 526 | # remap the feature coordinates to another coord system | ||
| 527 | # if a mapper was provided | ||
| 528 | # | ||
| 529 | if ($mapper) { | ||
| 530 | |||
| 531 | if (defined $dest_slice && $mapper->isa('Bio::EnsEMBL::ChainedAssemblyMapper') ) { | ||
| 532 | ($seq_region_id, $seq_region_start,$seq_region_end, $seq_region_strand) = | ||
| 533 | $mapper->map($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs, 1, $dest_slice); | ||
| 534 | |||
| 535 | } else { | ||
| 536 | ($seq_region_id, $seq_region_start,$seq_region_end, $seq_region_strand) = | ||
| 537 | $mapper->fastmap($sr_name, $seq_region_start, $seq_region_end,$seq_region_strand, $sr_cs); | ||
| 538 | } | ||
| 539 | |||
| 540 | #skip features that map to gaps or coord system boundaries | ||
| 541 | if(!defined($seq_region_id)) { | ||
| 542 | $throw_away = $misc_feature_id; | ||
| 543 | next FEATURE; | ||
| 544 | } | ||
| 545 | |||
| 546 | #get a slice in the coord system we just mapped to | ||
| 547 | $slice = $slice_hash{"ID:".$seq_region_id} ||= $sa->fetch_by_seq_region_id($seq_region_id); | ||
| 548 | } | ||
| 549 | |||
| 550 | # | ||
| 551 | # If a destination slice was provided convert the coords | ||
| 552 | # | ||
| 553 | if ($dest_slice) { | ||
| 554 | my $seq_region_len = $dest_slice->seq_region_length(); | ||
| 555 | |||
| 556 | if ($dest_slice_strand == 1) { # Positive strand | ||
| 557 | $seq_region_start = $seq_region_start - $dest_slice_start + 1; | ||
| 558 | $seq_region_end = $seq_region_end - $dest_slice_start + 1; | ||
| 559 | |||
| 560 | if ($dest_slice->is_circular()) { | ||
| 561 | # Handle cicular chromosomes. | ||
| 562 | |||
| 563 | if ($seq_region_start > $seq_region_end) { | ||
| 564 | # Looking at a feature overlapping the chromsome origin. | ||
| 565 | |||
| 566 | if ($seq_region_end > $dest_slice_start) { | ||
| 567 | # Looking at the region in the beginning of the chromosome. | ||
| 568 | $seq_region_start -= $seq_region_len; | ||
| 569 | } | ||
| 570 | if ($seq_region_end < 0) { | ||
| 571 | $seq_region_end += $seq_region_len; | ||
| 572 | } | ||
| 573 | } else { | ||
| 574 | if ($dest_slice_start > $dest_slice_end && $seq_region_end < 0) { | ||
| 575 | # Looking at the region overlapping the chromosome | ||
| 576 | # origin and a feature which is at the beginning of the | ||
| 577 | # chromosome. | ||
| 578 | $seq_region_start += $seq_region_len; | ||
| 579 | $seq_region_end += $seq_region_len; | ||
| 580 | } | ||
| 581 | } | ||
| 582 | } ## end if ($dest_slice->is_circular...) | ||
| 583 | } else { # Negative strand | ||
| 584 | |||
| 585 | my $start = $dest_slice_end - $seq_region_end + 1; | ||
| 586 | my $end = $dest_slice_end - $seq_region_start + 1; | ||
| 587 | |||
| 588 | if ($dest_slice->is_circular()) { | ||
| 589 | |||
| 590 | if ($dest_slice_start > $dest_slice_end) { | ||
| 591 | # slice spans origin or replication | ||
| 592 | |||
| 593 | if ($seq_region_start >= $dest_slice_start) { | ||
| 594 | $end += $seq_region_len; | ||
| 595 | $start += $seq_region_len if $seq_region_end > $dest_slice_start; | ||
| 596 | |||
| 597 | } elsif ($seq_region_start <= $dest_slice_end) { | ||
| 598 | # do nothing | ||
| 599 | } elsif ($seq_region_end >= $dest_slice_start) { | ||
| 600 | $start += $seq_region_len; | ||
| 601 | $end += $seq_region_len; | ||
| 602 | |||
| 603 | } elsif ($seq_region_end <= $dest_slice_end) { | ||
| 604 | $end += $seq_region_len if $end < 0; | ||
| 605 | |||
| 606 | } elsif ($seq_region_start > $seq_region_end) { | ||
| 607 | $end += $seq_region_len; | ||
| 608 | } | ||
| 609 | } else { | ||
| 610 | |||
| 611 | if ($seq_region_start <= $dest_slice_end and $seq_region_end >= $dest_slice_start) { | ||
| 612 | # do nothing | ||
| 613 | } elsif ($seq_region_start > $seq_region_end) { | ||
| 614 | if ($seq_region_start <= $dest_slice_end) { | ||
| 615 | $start -= $seq_region_len; | ||
| 616 | } elsif ($seq_region_end >= $dest_slice_start) { | ||
| 617 | $end += $seq_region_len; | ||
| 618 | } | ||
| 619 | } | ||
| 620 | } | ||
| 621 | } | ||
| 622 | |||
| 623 | $seq_region_start = $start; | ||
| 624 | $seq_region_end = $end; | ||
| 625 | $seq_region_strand *= -1; | ||
| 626 | |||
| 627 | } ## end else [ if ($dest_slice_strand...)] | ||
| 628 | |||
| 629 | #throw away features off the end of the requested slice | ||
| 630 | if ($seq_region_end < 1 || $seq_region_start > $dest_slice_length || | ||
| 631 | $dest_slice_sr_id != $seq_region_id) { | ||
| 632 | #flag this feature as one to throw away | ||
| 633 | $throw_away = $misc_feature_id; | ||
| 634 | next FEATURE; | ||
| 635 | } | ||
| 636 | |||
| 637 | $slice = $dest_slice; | ||
| 638 | } | ||
| 639 | |||
| 640 | |||
| 641 | if ($attrib_value && $attrib_type_code) { | ||
| 642 | my $attrib = Bio::EnsEMBL::Attribute->new | ||
| 643 | ( -CODE => $attrib_type_code, | ||
| 644 | -NAME => $attrib_type_name, | ||
| 645 | -DESC => $attrib_type_description, | ||
| 646 | -VALUE => $attrib_value | ||
| 647 | ); | ||
| 648 | $feat_attribs = [$attrib]; | ||
| 649 | $seen_attribs->{"$attrib_type_code:$attrib_value"} = 1; | ||
| 650 | } | ||
| 651 | |||
| 652 | $feat = | ||
| 653 | $self->_create_feature_fast( 'Bio::EnsEMBL::MiscFeature', { | ||
| 654 | 'start' => $seq_region_start, | ||
| 655 | 'end' => $seq_region_end, | ||
| 656 | 'strand' => $seq_region_strand, | ||
| 657 | 'slice' => $slice, | ||
| 658 | 'adaptor' => $self, | ||
| 659 | 'dbID' => $misc_feature_id, | ||
| 660 | 'attributes' => $feat_attribs ||= [] | ||
| 661 | } ); | ||
| 662 | |||
| 663 | push @features, $feat; | ||
| 664 | |||
| 665 | if ($misc_set_id) { | ||
| 666 | #get the misc_set object | ||
| 667 | my $misc_set = $ms_hash{$misc_set_id} ||= | ||
| 668 | $msa->fetch_by_dbID($misc_set_id); | ||
| 669 | if ( ! exists $feat_misc_sets->{$misc_set->{'code'}} ) { | ||
| 670 | $feat->add_MiscSet( $misc_set ); | ||
| 671 | $feat_misc_sets->{$misc_set->{'code'}} = $misc_set; | ||
| 672 | } | ||
| 673 | } | ||
| 674 | } | ||
| 675 | } | ||
| 676 | |||
| 677 | return \@features; | ||
| 678 | } | ||
| 679 | |||
| 680 | |||
| 681 | |||
| 682 | =head2 list_dbIDs | ||
| 683 | |||
| 684 | Arg [1] : none | ||
| 685 | Example : @feature_ids = @{$misc_feature_adaptor->list_dbIDs()}; | ||
| 686 | Description: Gets an array of internal ids for all misc_features in the | ||
| 687 | current db | ||
| 688 | Arg[1] : <optional> int. not 0 for the ids to be sorted by the seq_region. | ||
| 689 | Returntype : list of ints | ||
| 690 | Exceptions : none | ||
| 691 | Caller : ? | ||
| 692 | Status : Stable | ||
| 693 | |||
| 694 | =cut | ||
| 695 | |||
| 696 | sub list_dbIDs { | ||
| 697 | my ($self,$ordered) = @_; | ||
| 698 | |||
| 699 | return $self->_list_dbIDs("misc_feature",undef,$ordered); | ||
| 700 | } | ||
| 701 | |||
| 702 | |||
| 703 | =head2 store | ||
| 704 | |||
| 705 | Arg [1] : list of Bio::EnsEMBL::MiscFeatures @misc_features | ||
| 706 | Example : $misc_feature_adaptor->store(@misc_features); | ||
| 707 | Description: Stores a list of MiscFeatures in this database. The stored | ||
| 708 | features will have their | ||
| 709 | Returntype : none | ||
| 710 | Exceptions : throw on invalid arguments | ||
| 711 | warning if misc feature is already stored in this database | ||
| 712 | throw if start/end/strand attribs are not valid | ||
| 713 | Caller : general | ||
| 714 | Status : Stable | ||
| 715 | |||
| 716 | =cut | ||
| 717 | |||
| 718 | sub store { | ||
| 719 | my $self = shift; | ||
| 720 | my @misc_features = @_; | ||
| 721 | |||
| 722 | my $db = $self->db(); | ||
| 723 | |||
| 724 | my $feature_sth = $self->prepare( | ||
| 725 | q{INSERT INTO misc_feature ( | ||
| 726 | seq_region_id, | ||
| 727 | seq_region_start, | ||
| 728 | seq_region_end, | ||
| 729 | seq_region_strand | ||
| 730 | ) VALUES (?, ?, ?, ?) | ||
| 731 | }); | ||
| 732 | |||
| 733 | my $insert_ignore = $self->insert_ignore_clause(); | ||
| 734 | my $feature_set_sth = $self->prepare( | ||
| 735 | qq{${insert_ignore} INTO misc_feature_misc_set ( | ||
| 736 | misc_feature_id, | ||
| 737 | misc_set_id | ||
| 738 | ) VALUES (?, ?) | ||
| 739 | }); | ||
| 740 | |||
| 741 | my $msa = $db->get_MiscSetAdaptor(); | ||
| 742 | my $aa = $db->get_AttributeAdaptor(); | ||
| 743 | |||
| 744 | FEATURE: | ||
| 745 | foreach my $mf (@misc_features) { | ||
| 746 | if(!ref($mf) || !$mf->isa('Bio::EnsEMBL::MiscFeature')) { | ||
| 747 | throw("List of MiscFeature arguments expeceted"); | ||
| 748 | } | ||
| 749 | |||
| 750 | if($mf->is_stored($db)) { | ||
| 751 | warning("MiscFeature [" .$mf->dbID."] is already stored in database."); | ||
| 752 | next FEATURE; | ||
| 753 | } | ||
| 754 | |||
| 755 | # do some checking of the start/end and convert to seq_region coords | ||
| 756 | my $original = $mf; | ||
| 757 | my $seq_region_id; | ||
| 758 | ($mf, $seq_region_id) = $self->_pre_store($mf); | ||
| 759 | |||
| 760 | # store the actual MiscFeature | ||
| 761 | $feature_sth->bind_param(1,$seq_region_id,SQL_INTEGER); | ||
| 762 | $feature_sth->bind_param(2,$mf->start,SQL_INTEGER); | ||
| 763 | $feature_sth->bind_param(3,$mf->end,SQL_INTEGER); | ||
| 764 | $feature_sth->bind_param(4,$mf->strand,SQL_TINYINT); | ||
| 765 | $feature_sth->execute(); | ||
| 766 | |||
| 767 | my $dbID = $self->last_insert_id('misc_feature_id', undef, 'misc_feature'); | ||
| 768 | |||
| 769 | $mf->dbID($dbID); | ||
| 770 | $mf->adaptor($self); | ||
| 771 | |||
| 772 | # store all the attributes | ||
| 773 | my $attribs = $mf->get_all_Attributes(); | ||
| 774 | $aa->store_on_MiscFeature($mf, $attribs); | ||
| 775 | |||
| 776 | # store all the sets that have not been stored yet | ||
| 777 | my $sets = $mf->get_all_MiscSets(); | ||
| 778 | foreach my $set (@$sets) { | ||
| 779 | $msa->store($set) if(!$set->is_stored($db)); | ||
| 780 | |||
| 781 | # update the misc_feat_misc_set table to store the set relationship | ||
| 782 | $feature_set_sth->bind_param(1,$dbID,SQL_INTEGER); | ||
| 783 | $feature_set_sth->bind_param(2,$set->dbID,SQL_INTEGER); | ||
| 784 | |||
| 785 | $feature_set_sth->execute(); | ||
| 786 | } | ||
| 787 | } | ||
| 788 | |||
| 789 | return; | ||
| 790 | } | ||
| 791 | |||
| 792 | 1; | ||
| 793 | |||
| 794 | |||
| 795 | |||
| 796 | |||
| 797 |