MiscFeatureAdaptor.pm 20.9 KB
Newer Older
1
=head1 LICENSE
2

3
Copyright [1999-2013] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
4

5 6 7 8 9 10 11 12 13 14 15 16 17
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut
18 19 20 21 22


=head1 CONTACT

  Please email comments or questions to the public Ensembl
Magali Ruffier's avatar
Magali Ruffier committed
23
  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
24 25

  Questions may also be sent to the Ensembl help desk at
Magali Ruffier's avatar
Magali Ruffier committed
26
  <http://www.ensembl.org/Help/Contact>.
27 28

=cut
29 30 31 32 33 34 35

=head1 NAME

Bio::EnsEMBL::DBSQL::MiscFeatureAdaptor

=head1 SYNOPSIS

36
  $mfa = $database_adaptor->get_MiscFeatureAdaptor();
37

38 39
  # retrieve a misc feature by its dbID
  my $misc_feat = $mfa->fetch_by_dbID(1234);
Graham McVicker's avatar
Graham McVicker committed
40

41 42
  # retrieve all misc features in a given region
  my @misc_feats = @{ $mfa->fetch_all_by_Slice($slice) };
43

44 45 46
  # retrieve all misc features in a given region with a given set code
  my @misc_clones =
    @{ $mfa->fetch_all_by_Slice_and_set_code('cloneset') };
47

48 49
  # store some misc features in the database
  $mfa->store(@misc_features);
50 51 52

=head1 DESCRIPTION

53 54 55 56 57 58 59 60
This is an adaptor for the retrieval and storage of MiscFeatures.
Misc Features are extremely generic features that can be added with
minimal effort to the database.  Currently misc features are used to
describe the locations of clone sets and tiling path information,
but arbitrary features can be stored.  Misc features are grouped
into sets and can be fetched according to their grouping using the
fetch_all_by_Slice_and_set_code and fetch_all_by_set_code methods.
MiscFeatures may belong to more than one set.
61

Graham McVicker's avatar
Graham McVicker committed
62
=head1 METHODS
63 64 65 66 67 68 69 70

=cut

package Bio::EnsEMBL::DBSQL::MiscFeatureAdaptor;

use strict;
use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
use Bio::EnsEMBL::MiscFeature;
71
use Bio::EnsEMBL::Attribute;
72 73 74 75 76 77 78 79 80 81 82 83 84
use Bio::EnsEMBL::MiscSet;
use Bio::EnsEMBL::Utils::Exception qw(throw warning);

use vars qw(@ISA);

@ISA = qw(Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor);



=head2 fetch_all_by_Slice_and_set_code

  Arg [1]    : Bio::EnsEMBL::Slice $slice
               A slice representing the region to fetch from
Web Admin's avatar
Web Admin committed
85
  Arg [2...] : string $set_code
86 87 88 89 90 91 92 93 94
               The code of the set to retrieve features from
  Example    : @feats = @{$mfa->fetch_all_by_Slice_and_set_code('cloneset')};
  Description: Retrieves a set of MiscFeatures which have a particular set code
               and which lie in a particular region.  All features with the
               provide set code and which overlap the given slice are returned.
  Returntype : listref of Bio::EnsEMBL::MiscFeatures
  Exceptions : throw if set_code is not provided
               warning if no set for provided set code exists
  Caller     : general
95
  Status     : Stable
96 97 98 99 100 101 102

=cut

sub fetch_all_by_Slice_and_set_code {
  my $self = shift;
  my $slice = shift;

Web Admin's avatar
Web Admin committed
103
  throw('Set code argument is required.') unless @_;
104 105

  my $msa = $self->db->get_MiscSetAdaptor();
Web Admin's avatar
Web Admin committed
106
  my @sets = ();
107
  my $max_len = 0;
Web Admin's avatar
Web Admin committed
108 109
  foreach my $set_code (@_) {
    my $set = $msa->fetch_by_code($set_code);
Web Admin's avatar
Web Admin committed
110 111 112 113 114 115
    if($set) {
      $max_len = $set->longest_feature if $set->longest_feature > $max_len;
      push @sets, $set->dbID;
    } else { 
      warning("No misc_set with code [$set_code] exists");
    }
Web Admin's avatar
Web Admin committed
116 117 118 119 120 121 122
  }
  my $constraint;
  if( @sets > 1 ) {
    $constraint = " mfms.misc_set_id in ( @{[join ',',@sets]} ) ";
  } elsif( @sets == 1 ) {
    $constraint = " mfms.misc_set_id = $sets[0] ";
  } else {
123 124
    return [];
  }
125 126 127 128 129 130 131 132

  $self->_max_feature_length($max_len);

  my $results = $self->fetch_all_by_Slice_constraint($slice, $constraint);

  $self->_max_feature_length(undef);

  return $results;
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
}



=head2 fetch_all_by_attribute_type_value

  Arg [1]    : string $attrib_type_code
               The code of the attribute type to fetch features for
  Arg [2]    : (optional) string $attrib_value
               The value of the attribute to fetch features for
  Example    : 
         #get all misc features that have an embl accession
         @feats = @{$mfa->fetch_all_by_attrib_type_value('embl_acc')};
         #get the misc feature with synonym 'AL014121'
         ($feat)=@{$mfa->fetch_all_by_attrib_type_value('synonym','AL014121');
  Description: Retrieves MiscFeatures which have a particular attribute.
149 150 151 152 153
               If the attribute value argument is also provided only
               features which have the attribute AND a particular value
               are returned.  The features are returned in their native
               coordinate system (i.e. the coordinate system that they
               are stored in).
154 155 156
  Returntype : listref of Bio::EnsEMBL::MiscFeatures
  Exceptions : throw if attrib_type code arg is not provided
  Caller     : general
157
  Status     : Stable
158 159 160 161

=cut

sub fetch_all_by_attribute_type_value {
162
  my $self             = shift;
163
  my $attrib_type_code = shift;
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  my $attrib_value     = shift;

  throw("Attrib type code argument is required.")
    if ( !$attrib_type_code );

  # Need to do 2 queries so that all of the ids come back with the
  # features.  The problem with adding attrib constraints to filter the
  # misc_features which come back is that not all of the attributes will
  # come back

  my $sql = qq(
  SELECT DISTINCT
        ma.misc_feature_id
  FROM  misc_attrib ma,
        attrib_type at,
        misc_feature mf,
        seq_region sr,
        coord_system cs
  WHERE ma.attrib_type_id = at.attrib_type_id
    AND at.code = ?
    AND ma.misc_feature_id = mf.misc_feature_id
    AND mf.seq_region_id = sr.seq_region_id
    AND sr.coord_system_id = cs.coord_system_id
    AND cs.species_id = ?);

  if ($attrib_value) {
190 191 192 193
    $sql .= " AND ma.value = ?";
  }

  my $sth = $self->prepare($sql);
194 195 196 197 198 199 200

  $sth->bind_param( 1, $attrib_type_code,   SQL_VARCHAR );
  $sth->bind_param( 2, $self->species_id(), SQL_INTEGER );
  if ($attrib_value) {
    $sth->bind_param( 3, $attrib_value, SQL_VARCHAR );
  }

201
  $sth->execute();
202

203
  my @ids = map { $_->[0] } @{ $sth->fetchall_arrayref() };
204 205 206

  $sth->finish();

207 208
  # Construct constraints from the list of ids.  Split ids into groups
  # of 1000 to ensure that the query is not too big.
209
  my @constraints;
210 211 212
  while (@ids) {
    my @subset = splice( @ids, 0, 1000 );
    if ( @subset == 1 ) {
213 214
      push @constraints, "mf.misc_feature_id = $subset[0]";
    } else {
215
      my $id_str = join( ',', @subset );
216 217 218 219 220 221
      push @constraints, "mf.misc_feature_id in ($id_str)";
    }
  }

  my @results;
  foreach my $constraint (@constraints) {
222
    push @results, @{ $self->generic_fetch($constraint) };
223 224 225
  }

  return \@results;
226
} ## end sub fetch_all_by_attribute_type_value
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269


#_tables
#
#  Arg [1]    : none
#  Example    : none
#  Description: PROTECTED Implementation of abstract superclass method to
#               provide the name of the tables to query
#  Returntype : string
#  Exceptions : none
#  Caller     : internal


sub _tables {
  my $self = shift;

  return (['misc_feature',          'mf'],
          ['misc_feature_misc_set', 'mfms'],
          ['misc_attrib',           'ma'],
          ['attrib_type',           'at']);
}


#_columns

#  Arg [1]    : none
#  Example    : none
#  Description: PROTECTED Implementation of abstract superclass method to 
#               provide the name of the columns to query 
#  Returntype : list of strings
#  Exceptions : none
#  Caller     : internal

sub _columns {
  my $self = shift;

  #warning _objs_from_sth implementation depends on ordering
  return qw (mf.misc_feature_id
	     mf.seq_region_id
	     mf.seq_region_start
	     mf.seq_region_end
	     mf.seq_region_strand
	     ma.value
270 271 272 273
	     at.code
	     mfms.misc_set_id
	     at.name
	     at.description);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
}



# _default_where_clause

#  Arg [1]    : none
#  Example    : none
#  Description: Overrides superclass method to provide an additional 
#               table joining constraint before the SQL query is performed.
#  Returntype : string
#  Exceptions : none
#  Caller     : generic_fetch

sub _default_where_clause {
  my $self = shift;

  return '';
}


sub _left_join {
  my $self = shift;

  return(
      ['misc_feature_misc_set','mf.misc_feature_id = mfms.misc_feature_id'],
      ['misc_attrib', 'mf.misc_feature_id = ma.misc_feature_id'],
      ['attrib_type','ma.attrib_type_id = at.attrib_type_id']);
}


sub _final_clause {
  my $self = shift;

Web Admin's avatar
Web Admin committed
308
  return " ORDER BY mf.misc_feature_id";
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
}


# _objs_from_sth

#  Arg [1]    : StatementHandle $sth
#  Example    : none
#  Description: PROTECTED implementation of abstract superclass method.
#               responsible for the creation of MiscFeatures from a
#               hashref generated from an SQL query
#  Returntype : listref of Bio::EnsEMBL::MiscFeatures
#  Exceptions : none
#  Caller     : internal

sub _objs_from_sth {
  my ($self, $sth, $mapper, $dest_slice) = @_;

  #
  # This code is ugly because an attempt has been made to remove as many
  # function calls as possible for speed purposes.  Thus many caches and
  # a fair bit of gymnastics is used.
  #

  my $sa = $self->db()->get_SliceAdaptor();
  my $msa = $self->db->get_MiscSetAdaptor();

  my @features;
  my %ms_hash;
  my %slice_hash;
  my %sr_name_hash;
  my %sr_cs_hash;

  my($misc_feature_id, $seq_region_id, $seq_region_start, $seq_region_end,
342 343
     $seq_region_strand, $attrib_value, $attrib_type_code, $misc_set_id,
     $attrib_type_name, $attrib_type_description );
344 345 346

  $sth->bind_columns( \$misc_feature_id, \$seq_region_id, \$seq_region_start,
                      \$seq_region_end, \$seq_region_strand,
347 348
                      \$attrib_value, \$attrib_type_code,\$misc_set_id,
		      \$attrib_type_name, \$attrib_type_description );
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

  my $asm_cs;
  my $cmp_cs;
  my $asm_cs_vers;
  my $asm_cs_name;
  my $cmp_cs_vers;
  my $cmp_cs_name;
  if($mapper) {
    $asm_cs = $mapper->assembled_CoordSystem();
    $cmp_cs = $mapper->component_CoordSystem();
    $asm_cs_name = $asm_cs->name();
    $asm_cs_vers = $asm_cs->version();
    $cmp_cs_name = $cmp_cs->name();
    $cmp_cs_vers = $cmp_cs->version();
  }

  my $dest_slice_start;
  my $dest_slice_end;
  my $dest_slice_strand;
  my $dest_slice_length;
369
  my $dest_slice_sr_name;
370
  my $dest_slice_sr_id;
371 372 373 374 375
  if($dest_slice) {
    $dest_slice_start  = $dest_slice->start();
    $dest_slice_end    = $dest_slice->end();
    $dest_slice_strand = $dest_slice->strand();
    $dest_slice_length = $dest_slice->length();
376
    $dest_slice_sr_name = $dest_slice->seq_region_name();
377
    $dest_slice_sr_id  = $dest_slice->get_seq_region_id();
378 379 380 381 382 383 384 385 386 387 388 389 390
  }

  my $current = -1;
  my $throw_away = -1;
  my $feat;
  my $feat_misc_sets;
  my $feat_attribs;
  my $seen_attribs;

 FEATURE: while($sth->fetch()) {
    #if this feature is not being used, skip all rows related to it
    next if($throw_away == $misc_feature_id);

391
    if ($current == $misc_feature_id) {
392 393 394
      #still working on building up attributes and sets for current feature

      #if there is a misc_set, add it to the current feature
395
      if ($misc_set_id) {
396 397
        my $misc_set = $ms_hash{$misc_set_id} ||=
          $msa->fetch_by_dbID($misc_set_id);
398 399 400 401
        if ( ! exists $feat_misc_sets->{$misc_set->{'code'}} ) {
          $feat->add_MiscSet( $misc_set );
          $feat_misc_sets->{$misc_set->{'code'}} = $misc_set;
        }
402 403 404
      }

      #if there is a new attribute add it to the current feature
405 406 407 408 409 410 411 412
      if ($attrib_value && $attrib_type_code &&
          !$seen_attribs->{"$attrib_type_code:$attrib_value"}) {
        my $attrib = Bio::EnsEMBL::Attribute->new
          ( -CODE => $attrib_type_code,
            -NAME => $attrib_type_name,
            -DESC => $attrib_type_description,
            -VALUE => $attrib_value
          );
413 414 415 416
	
	
        $feat_attribs ||= [];
        push @$feat_attribs, $attrib;
417 418 419 420
        $seen_attribs->{"$attrib_type_code:$attrib_value"} = 1;
      }

    } else {
421
      if ($feat) {
422 423
        #start working on a new feature, discard references to last one
        $feat = {};
424
        $feat_attribs = [];
425 426 427 428 429
        $feat_misc_sets = {};
        $seen_attribs = {};
      }

      $current = $misc_feature_id;
430 431
      #need to get the internal_seq_region, if present
      $seq_region_id = $self->get_seq_region_id_internal($seq_region_id);
432 433
      my $slice = $slice_hash{"ID:".$seq_region_id};

434
      if (!$slice) {
435 436 437 438 439 440
        $slice = $sa->fetch_by_seq_region_id($seq_region_id);
        $slice_hash{"ID:".$seq_region_id} = $slice;
        $sr_name_hash{$seq_region_id} = $slice->seq_region_name();
        $sr_cs_hash{$seq_region_id} = $slice->coord_system();
      }

441 442
      my $sr_name = $sr_name_hash{$seq_region_id};
      my $sr_cs   = $sr_cs_hash{$seq_region_id};
443 444 445 446
      #
      # remap the feature coordinates to another coord system
      # if a mapper was provided
      #
447
      if ($mapper) {
448

449 450 451 452 453 454 455 456 457 458 459

	if (defined $dest_slice && $mapper->isa('Bio::EnsEMBL::ChainedAssemblyMapper')  ) {
	    ( $seq_region_id,  $seq_region_start,$seq_region_end, $seq_region_strand ) =
	       $mapper->map( $sr_name, $seq_region_start, $seq_region_end,
                          $seq_region_strand, $sr_cs, 1, $dest_slice);

	} else {

	    ( $seq_region_id,  $seq_region_start,$seq_region_end, $seq_region_strand ) =
	       $mapper->fastmap( $sr_name, $seq_region_start, $seq_region_end,$seq_region_strand, $sr_cs );
	}
460 461

        #skip features that map to gaps or coord system boundaries
462
        if(!defined($seq_region_id)) {
463 464 465
          $throw_away = $misc_feature_id;
          next FEATURE;
        }
466 467

        #get a slice in the coord system we just mapped to
468 469 470 471 472 473 474 475 476
#        if ($asm_cs == $sr_cs ||
#            ($cmp_cs != $sr_cs && $asm_cs->equals($sr_cs))) {
          $slice = $slice_hash{"ID:".$seq_region_id} ||=
            $sa->fetch_by_seq_region_id($seq_region_id);
#        } else {
#          $slice = $slice_hash{"NAME:$sr_name:$asm_cs_name:$asm_cs_vers"} ||=
#            $sa->fetch_by_region($asm_cs_name, $sr_name, undef, undef, undef,
#                                 $asm_cs_vers);
#        }
477 478 479 480 481
      }

      #
      # If a destination slice was provided convert the coords
      #
482
      if ($dest_slice) {
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
	my $seq_region_len = $dest_slice->seq_region_length();

	if ($dest_slice_strand == 1) { # Positive strand
		
	  $seq_region_start = $seq_region_start - $dest_slice_start + 1;
	  $seq_region_end   = $seq_region_end - $dest_slice_start + 1;

	  if ($dest_slice->is_circular()) {
	    # Handle cicular chromosomes.

	    if ($seq_region_start > $seq_region_end) {
	      # Looking at a feature overlapping the chromsome origin.

	      if ($seq_region_end > $dest_slice_start) {

		# Looking at the region in the beginning of the
		# chromosome.
		$seq_region_start -= $seq_region_len;
	      }

	      if ($seq_region_end < 0) {
		$seq_region_end += $seq_region_len;
	      }

	    } else {

	      if (   $dest_slice_start > $dest_slice_end
		     && $seq_region_end < 0) {
		# Looking at the region overlapping the chromosome
		# origin and a feature which is at the beginning of the
		# chromosome.
		$seq_region_start += $seq_region_len;
		$seq_region_end   += $seq_region_len;
	      }
	    }

	  }		       ## end if ($dest_slice->is_circular...)

	} else {		# Negative strand

	  my $start = $dest_slice_end - $seq_region_end + 1;
	  my $end = $dest_slice_end - $seq_region_start + 1;

	  if ($dest_slice->is_circular()) {

	    if ($dest_slice_start > $dest_slice_end) { 
	      # slice spans origin or replication

	      if ($seq_region_start >= $dest_slice_start) {
		$end += $seq_region_len;
		$start += $seq_region_len 
		  if $seq_region_end > $dest_slice_start;

	      } elsif ($seq_region_start <= $dest_slice_end) {
		# do nothing
	      } elsif ($seq_region_end >= $dest_slice_start) {
		$start += $seq_region_len;
		$end += $seq_region_len;

	      } elsif ($seq_region_end <= $dest_slice_end) {

		$end += $seq_region_len
		  if $end < 0;

	      } elsif ($seq_region_start > $seq_region_end) {
		  
		$end += $seq_region_len;

	      } else {
		  
	      }
      
	    } else {

	      if ($seq_region_start <= $dest_slice_end and $seq_region_end >= $dest_slice_start) {
		# do nothing
	      } elsif ($seq_region_start > $seq_region_end) {
		if ($seq_region_start <= $dest_slice_end) {
	  
		  $start -= $seq_region_len;

		} elsif ($seq_region_end >= $dest_slice_start) {
		  $end += $seq_region_len;

		} else {
		    
		}
	      }
	    }

	  }

	  $seq_region_start = $start;
	  $seq_region_end = $end;
	  $seq_region_strand *= -1;

	}		     ## end else [ if ($dest_slice_strand...)]

581 582
	#throw away features off the end of the requested slice
	if ($seq_region_end < 1 || $seq_region_start > $dest_slice_length ||
583
	   ( $dest_slice_sr_id ne $seq_region_id )) {
584 585 586 587
	  #flag this feature as one to throw away
	  $throw_away = $misc_feature_id;
	  next FEATURE;
	}
588

589 590 591 592
        $slice = $dest_slice;
      }


593 594 595 596 597 598 599
      if ($attrib_value && $attrib_type_code) {
        my $attrib = Bio::EnsEMBL::Attribute->new
          ( -CODE => $attrib_type_code,
            -NAME => $attrib_type_name,
            -DESC => $attrib_type_description,
            -VALUE => $attrib_value
          );
600
        $feat_attribs = [$attrib];
601 602 603
        $seen_attribs->{"$attrib_type_code:$attrib_value"} = 1;
      }

604 605 606 607 608 609 610 611
      $feat =
        $self->_create_feature_fast( 'Bio::EnsEMBL::MiscFeature', {
                                       'start'   => $seq_region_start,
                                       'end'     => $seq_region_end,
                                       'strand'  => $seq_region_strand,
                                       'slice'   => $slice,
                                       'adaptor' => $self,
                                       'dbID'    => $misc_feature_id,
612
                                       'attributes' => $feat_attribs ||= []
613 614
                                     } );

615
      push @features, $feat;
616

617
      if ($misc_set_id) {
618 619 620
        #get the misc_set object
        my $misc_set = $ms_hash{$misc_set_id} ||=
          $msa->fetch_by_dbID($misc_set_id);
621 622 623 624
        if ( ! exists $feat_misc_sets->{$misc_set->{'code'}} ) {
          $feat->add_MiscSet( $misc_set );
          $feat_misc_sets->{$misc_set->{'code'}} = $misc_set;
        }
625
      }
626 627 628 629 630 631 632 633 634 635 636 637
    }
  }

  return \@features;
}



=head2 list_dbIDs

  Arg [1]    : none
  Example    : @feature_ids = @{$misc_feature_adaptor->list_dbIDs()};
Graham McVicker's avatar
Graham McVicker committed
638 639
  Description: Gets an array of internal ids for all misc_features in the 
               current db
640
  Arg[1]     : <optional> int. not 0 for the ids to be sorted by the seq_region.
641 642 643
  Returntype : list of ints
  Exceptions : none
  Caller     : ?
644
  Status     : Stable
645 646 647 648

=cut

sub list_dbIDs {
649
   my ($self,$ordered) = @_;
650

651
   return $self->_list_dbIDs("misc_feature",undef,$ordered);
652 653
}

654

Graham McVicker's avatar
Graham McVicker committed
655 656
=head2 store

Graham McVicker's avatar
Graham McVicker committed
657 658 659 660 661 662 663 664 665
  Arg [1]    : list of Bio::EnsEMBL::MiscFeatures @misc_features
  Example    : $misc_feature_adaptor->store(@misc_features);
  Description: Stores a list of MiscFeatures in this database.  The stored
               features will have their 
  Returntype : none
  Exceptions : throw on invalid arguments
               warning if misc feature is already stored in this database
               throw if start/end/strand attribs are not valid
  Caller     : general
666
  Status     : Stable
Graham McVicker's avatar
Graham McVicker committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684

=cut

sub store {
  my $self = shift;
  my @misc_features = @_;

  my $db = $self->db();

  my $feature_sth = $self->prepare
    ("INSERT INTO misc_feature SET " .
     " seq_region_id    = ?, " .
     " seq_region_start = ?, " .
     " seq_region_end   = ?, " .
     " seq_region_strand = ?");

  my $feature_set_sth = $self->prepare
    ("INSERT IGNORE misc_feature_misc_set SET " .
Graham McVicker's avatar
Graham McVicker committed
685
     " misc_feature_id = ?, " .
Graham McVicker's avatar
Graham McVicker committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
     " misc_set_id = ?");

  my $msa = $db->get_MiscSetAdaptor();
  my $aa  = $db->get_AttributeAdaptor();

 FEATURE:
  foreach my $mf (@misc_features) {
    if(!ref($mf) || !$mf->isa('Bio::EnsEMBL::MiscFeature')) {
      throw("List of MiscFeature arguments expeceted");
    }

    if($mf->is_stored($db)) {
      warning("MiscFeature [" .$mf->dbID."] is already stored in database.");
      next FEATURE;
    }

    # do some checking of the start/end and convert to seq_region coords
    my $original = $mf;
    my $seq_region_id;
Graham McVicker's avatar
Graham McVicker committed
705
    ($mf, $seq_region_id) = $self->_pre_store($mf);
Graham McVicker's avatar
Graham McVicker committed
706 707

    # store the actual MiscFeature
708 709 710 711 712
    $feature_sth->bind_param(1,$seq_region_id,SQL_INTEGER);
    $feature_sth->bind_param(2,$mf->start,SQL_INTEGER);
    $feature_sth->bind_param(3,$mf->end,SQL_INTEGER);
    $feature_sth->bind_param(4,$mf->strand,SQL_TINYINT);
    $feature_sth->execute();
Graham McVicker's avatar
Graham McVicker committed
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

    my $dbID = $feature_sth->{'mysql_insertid'};

    $mf->dbID($dbID);
    $mf->adaptor($self);

    # store all the attributes
    my $attribs = $mf->get_all_Attributes();
    $aa->store_on_MiscFeature($mf, $attribs);

    # store all the sets that have not been stored yet
    my $sets = $mf->get_all_MiscSets();
    foreach my $set (@$sets) {
      $msa->store($set) if(!$set->is_stored($db));

      # update the misc_feat_misc_set table to store the set relationship
729 730 731 732
      $feature_set_sth->bind_param(1,$dbID,SQL_INTEGER);
      $feature_set_sth->bind_param(2,$set->dbID,SQL_INTEGER);

      $feature_set_sth->execute();
Graham McVicker's avatar
Graham McVicker committed
733 734 735 736 737
    }
  }

  return;
}
738

739 740 741 742 743 744
1;