SliceAdaptor.pm 41.2 KB
Newer Older
1 2

#
Simon Potter's avatar
pod  
Simon Potter committed
3
# Ensembl module for Bio::EnsEMBL::DBSQL::SliceAdaptor
4 5 6 7 8 9 10 11 12 13 14
#
# Cared for by Ewan Birney <ensembl-dev@ebi.ac.uk>
#
# Copyright Ewan Birney
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

15 16
Bio::EnsEMBL::DBSQL::SliceAdaptor - A database aware adaptor responsible for
the creation of Slice objects.
17 18 19

=head1 SYNOPSIS

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  my $db = Bio::EnsEMBL::DBSQL::DBAdaptor->new(...);

  my $slice_adaptor = $db->get_SliceAdaptor();

  #get a slice on the entire chromosome X
  my $chr_slice = $slice_adaptor->fetch_by_region('chromosome','X');

  #get a slice for each clone in the database
  foreach my $cln_slice (@{$slice_adaptor->fetch_all('clone')}) {
    #do something with clone
  }

  #get a slice which is part of NT_004321
  my $spctg_slice = $slice_adaptor->fetch_by_region('supercontig','NT_004321',
                                                    200_000, 600_000);
35 36 37 38


=head1 DESCRIPTION

39 40 41
This module is responsible for fetching Slices representing genomic regions
from a database.  Details on how slices can be used are in the
Bio::EnsEMBL::Slice module.
42 43 44 45 46

=head1 AUTHOR - Ewan Birney

This modules is part of the Ensembl project http://www.ensembl.org

47
=head1 CONTACT
48

49
Email ensembl-dev@ebi.ac.uk
50

51
=head1 METHODS
52 53 54 55

=cut


56
package Bio::EnsEMBL::DBSQL::SliceAdaptor;
57 58 59
use vars qw(@ISA);
use strict;

60

61 62
use Bio::EnsEMBL::DBSQL::BaseAdaptor;
use Bio::EnsEMBL::Slice;
63
use Bio::EnsEMBL::DBSQL::DBAdaptor;
64
use Bio::EnsEMBL::Mapper;
65

66 67
use Bio::EnsEMBL::Utils::Exception qw(throw deprecate warning);
use Bio::EnsEMBL::Utils::Cache; #CPAN LRU cache
68

69
@ISA = ('Bio::EnsEMBL::DBSQL::BaseAdaptor');
70

71
my $SEQ_REGION_CACHE_SIZE = 100;
72

73 74
sub new {
  my $caller = shift;
75

76
  my $class = ref($caller) || $caller;
77

78
  my $self = $class->SUPER::new(@_);
79

80 81
  my %name_cache;
  my %id_cache;
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  tie(%name_cache, 'Bio::EnsEMBL::Utils::Cache', $SEQ_REGION_CACHE_SIZE);
  tie(%id_cache, 'Bio::EnsEMBL::Utils::Cache', $SEQ_REGION_CACHE_SIZE);

  $self->{'_name_cache'} = \%name_cache;
  $self->{'_id_cache'} = \%id_cache;

  return $self;
}


=head2 fetch_by_region

  Arg [1]    : string $coord_system_name
               The name of the coordinate system of the slice to be created
               This may be a name of an acutal coordinate system or an alias
               to a coordinate system.  Valid aliases are 'seqlevel' or
               'toplevel'.
  Arg [2]    : string $seq_region_name
               The name of the sequence region that the slice will be
               created on
  Arg [3]    : int $start (optional, default = 1)
               The start of the slice on the sequence region
  Arg [4]    : int $end (optional, default = seq_region length)
               The end of the slice on the sequence region
  Arg [5]    : int $strand (optional, default = 1)
               The orientation of the slice on the sequence region
  Arg [6]    : string $version (optional, default = default version)
               The version of the coordinate system to use (e.g. NCBI33)
  Example    : $slice = $slice_adaptor->fetch_by_region('chromosome');
112 113 114 115
  Description: Creates a slice object on the given chromosome and coordinates.
  Returntype : Bio::EnsEMBL::Slice
  Exceptions : none
  Caller     : general
116 117 118

=cut

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
sub fetch_by_region {
  my ($self, $coord_system_name, $seq_region_name,
      $start, $end, $strand, $version) = @_;

  throw('seq_region_name argument is required') if(!$seq_region_name);

  my $csa = $self->db()->get_CoordSystemAdaptor();
  my $coord_system = $csa->fetch_by_name($coord_system_name,$version);

  #check the cache so we only go to the db if necessary
  my $name_cache = $self->{'_name_cache'};
  my $key = lc(join(':',$seq_region_name,
                    $coord_system->name(),
                    $coord_system->version));

  my $length;

  if(exists($name_cache->{$key})) {
    $length = $name_cache->{$key}->[1];
  } else {
    my $sth = $self->prepare("SELECT seq_region_id, length " .
                             "FROM seq_region " .
                             "WHERE name = ? AND coord_system_id = ?");

    #force seq_region_name cast to string so mysql cannot treat as int
    $sth->execute("$seq_region_name", $coord_system->dbID());

    if($sth->rows() != 1) {
      $version ||= '';
      $seq_region_name ||= '';
      $coord_system_name ||= '';
      throw("Cannot create slice on non-existant or ambigous seq_region:\n" .
            "  coord_system=[$coord_system_name],\n" .
            "  name=[$seq_region_name],\n" .
            "  version=[$version]");
154 155
    }

156 157 158
    my $id;
    ($id, $length) = $sth->fetchrow_array();
    $sth->finish();
159

160 161 162 163
    #cache results to speed up future queries
    $name_cache->{$key} = [$id,$length];
    $self->{'_id_cache'}->{$id} = [$seq_region_name, $length, $coord_system];
  }
164

165 166 167 168 169 170 171 172 173 174 175 176 177 178
  $start = 1 if(!defined($start));
  $strand = 1 if(!defined($strand));
  $end = $length if(!defined($end));

  if($end < $start) {
    throw('start [$start] must be less than or equal to end [$end]');
  }

  return Bio::EnsEMBL::Slice->new(-COORD_SYSTEM    => $coord_system,
                                  -SEQ_REGION_NAME => $seq_region_name,
                                  -START           => $start,
                                  -END             => $end,
                                  -STRAND          => $strand,
                                  -ADAPTOR         => $self);
179 180 181 182
}



183
=head2 fetch_by_name
184

Graham McVicker's avatar
Graham McVicker committed
185
  Arg [1]    : string $name
186 187 188 189 190 191 192 193 194
  Example    : $name  = 'chromosome:NCBI34:X:1000000:2000000:1';
               $slice = $slice_adaptor->fetch_by_name($name);
               $slice2 = $slice_adaptor->fetch_by_name($slice3->name());
  Description: Fetches a slice using a slice name (i.e. the value returned by
               the Slice::name method).  This is useful if you wish to 
               store a unique identifier for a slice in a file or database or
               pass a slice over a network.
               Slice::name allows you to serialise/marshall a slice and this
               method allows you to deserialise/unmarshal it.
Graham McVicker's avatar
Graham McVicker committed
195
  Returntype : Bio::EnsEMBL::Slice
196 197
  Exceptions : throw if incorrent arg provided
  Caller     : Pipeline
198 199 200

=cut

201 202 203
sub fetch_by_name {
  my $self = shift;
  my $name = shift;
Graham McVicker's avatar
Graham McVicker committed
204

205 206
  if(!$name) {
    throw("name argument is required");
Graham McVicker's avatar
Graham McVicker committed
207
  }
208 209 210 211 212 213 214 215 216 217 218 219 220

  my @array = split(/:/,$name);

  if(@array != 6) {
    throw("Malformed slice name [$name].  Format is " .
        "coord_system:version:start:end:strand");
  }

  my ($cs_name, $cs_version, $seq_region, $start, $end, $strand) = @array;


  return $self->fetch_by_region($cs_name,$seq_region, $start,
                               $end, $strand, $cs_version);
Graham McVicker's avatar
Graham McVicker committed
221
}
Graham McVicker's avatar
Graham McVicker committed
222 223 224



225 226 227 228 229 230 231 232
=head2 fetch_by_seq_region_id

  Arg [1]    : string $seq_region_id
               The internal identifier of the seq_region to create this slice
               on
  Example    : $slice = $slice_adaptor->fetch_by_seq_region_id(34413);
  Description: Creates a slice object of an entire seq_region using the
               seq_region internal identifier to resolve the seq_region.
Graham McVicker's avatar
Graham McVicker committed
233 234 235
  Returntype : Bio::EnsEMBL::Slice
  Exceptions : none
  Caller     : general
Graham McVicker's avatar
Graham McVicker committed
236 237 238

=cut

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 270 271 272 273 274 275 276 277
sub fetch_by_seq_region_id {
  my ($self, $seq_region_id) = @_;

  my $id_cache = $self->{'_id_cache'};

  my ($name, $length, $cs);

  if(exists $id_cache->{$seq_region_id}) {
    ($name, $length, $cs) = @{$id_cache->{$seq_region_id}};
  } else {
    my $sth = $self->prepare("SELECT name, length, coord_system_id " .
                             "FROM seq_region " .
                             "WHERE seq_region_id = ?");

    $sth->execute($seq_region_id);

    if($sth->rows() != 1) {
      throw("Cannot create slice on non-existant or ambigous seq_region:" .
            "  seq_region_id=[$seq_region_id],\n");
    }

    my $cs_id;
    ($name, $length, $cs_id) = $sth->fetchrow_array();
    $sth->finish();

    $cs = $self->db->get_CoordSystemAdaptor->fetch_by_dbID($cs_id);

    #cache results to speed up repeated queries
    $id_cache->{$seq_region_id} = [$name, $length, $cs];
    my $key = lc(join(':', $name, $cs->name, $cs->version));
    $self->{'_name_cache'}->{$key} = [$seq_region_id, $length];
  }

  return Bio::EnsEMBL::Slice->new(-COORD_SYSTEM    => $cs,
                                  -SEQ_REGION_NAME => $name,
                                  -START           => 1,
                                  -END             => $length,
                                  -STRAND          => 1,
                                  -ADAPTOR         => $self);
Arne Stabenau's avatar
Arne Stabenau committed
278 279
}

Graham McVicker's avatar
Graham McVicker committed
280

281 282

=head2 get_seq_region_id
Graham McVicker's avatar
Graham McVicker committed
283

Arne Stabenau's avatar
Arne Stabenau committed
284
  Arg [1]    : Bio::EnsEMBL::Slice $slice
285 286 287 288 289 290 291 292 293 294
               The slice to fetch a seq_region_id for
  Example    : $srid = $slice_adaptor->get_seq_region_id($slice);
  Description: Retrieves the seq_region id (in this database) given a slice
               Seq region ids are not stored on the slices themselves
               because they are intended to be somewhat database independant
               and seq_region_ids vary accross databases.
  Returntype : int
  Exceptions : throw if the seq_region of the slice is not in the db
               throw if incorrect arg provided
  Caller     : BaseFeatureAdaptor
Arne Stabenau's avatar
Arne Stabenau committed
295 296

=cut
Graham McVicker's avatar
Graham McVicker committed
297

298 299 300
sub get_seq_region_id {
  my $self = shift;
  my $slice = shift;
Graham McVicker's avatar
Graham McVicker committed
301

302 303 304 305 306 307 308 309 310
  if(!$slice || !ref($slice) || !$slice->isa('Bio::EnsEMBL::Slice')) {
    throw('Slice argument is required');
  }

  my $cs_name = $slice->coord_system->name();
  my $cs_version = $slice->coord_system->version();
  my $seq_region_name = $slice->seq_region_name();

  my $key = lc(join(':', $seq_region_name,$cs_name,$cs_version));
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 342 343
  my $name_cache = $self->{'_name_cache'};

  if(exists($name_cache->{$key})) {
    return $name_cache->{$key}->[0];
  }

  my $csa = $self->db()->get_CoordSystemAdaptor();
  my $coord_system = $csa->fetch_by_name($cs_name,$cs_version);

  my $sth = $self->prepare("SELECT seq_region_id, length " .
                           "FROM seq_region " .
                           "WHERE name = ? AND coord_system_id = ?");

  #force seq_region_name cast to string so mysql cannot treat as int
  $sth->execute("$seq_region_name", $coord_system->dbID());

  if($sth->rows() != 1) {
    throw("Non existant or ambigous seq_region:\n" .
          "  coord_system=[$cs_name],\n" .
          "  name=[$seq_region_name],\n" .
          "  version=[$cs_version]");
  }

  my($seq_region_id, $length) = $sth->fetchrow_array();
  $sth->finish();

  #cache information for future requests
  $name_cache->{$key} = [$seq_region_id, $length];
  $self->{'_id_cache'}->{$seq_region_id} =
    [$seq_region_name, $length, $coord_system];

  return $seq_region_id;
Arne Stabenau's avatar
Arne Stabenau committed
344
}
345 346


347
=head2 get_seq_region_attribs
Web Admin's avatar
Web Admin committed
348

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
  Arg [1]    : Bio::EnsEMBL::Slice $slice
               The slice to fetch attributes for
  Example    : %attribs = %{$slice_adaptor->get_seq_region_attribs($slice)};
               ($htg_phase) = @{$attribs->{'htg_phase'} || []};
               @synonyms    = @{$attribs->{'synonym'} || []};
  Description: Retrieves a reference to a hash containing attrib code values
               and listref value keys.
  Returntype : hashref
  Exceptions : throw if the seq_region of the slice is not in the db
               throw if incorrect arg provided
  Caller     : Bio::EnsEMBL::Slice

=cut

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

  if(!ref($slice) || !$slice->isa('Bio::EnsEMBL::Slice')) {
    throw('Slice argument is required');
  }

  my $srid = $self->get_seq_region_id($slice);
Web Admin's avatar
Web Admin committed
372

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
  if(!$srid) {
    throw('Slice is not on a seq_region stored in this database.');
  }

  $self->{'_attribs_cache'} ||= {};
  if($self->{'_attribs_cache'}->{$srid}) {
    return $self->{'_attribs_cache'}->{$srid};
  }

  my $sth = $self->prepare('SELECT at.code, sra.value ' .
                           'FROM   seq_region_attrib sra, attrib_type at ' .
                           'WHERE  sra.seq_region_id = ? ' .
                           'AND    at.attrib_type_id = sra.attrib_type_id');

  $sth->execute($srid);

  my($code, $attrib);
  $sth->bind_columns(\$code, \$attrib);

  my %attrib_hash;
  while($sth->fetch()) {
    $attrib_hash{$code} ||= [];
    push @{$attrib_hash{$code}}, $attrib;
  }

  $sth->finish();
  $self->{'_attribs_cache'} = \%attrib_hash;
  return \%attrib_hash;
}


=head2 fetch_all

  Arg [1]    : string $coord_system_name
               The name of the coordinate system to retrieve slices of.
               This may be a name of an acutal coordinate system or an alias
               to a coordinate system.  Valid aliases are 'seqlevel' or
               'toplevel'.
  Arg [2]    : string $coord_system_version (optional)
               The version of the coordinate system to retrieve slices of
  Arg [3]    : int $max_length (optional)
               The maximum length of slices to be returned.  Seq_regions which
               are larger than $max_length will be split into multiple slices.
               If this argument is not provided then slices will not be 
               split.  If this argument is less than 1 a warning will occur and
               the slices will not be split.
  Arg [4]    : int $overlap (optional)
               The amount that the returned slices should overlap. By default
               this value is 0.  If no max_length value is provided but an 
               overlap which is not zero is provided this argument will be
               ignored and a warning will occur. 
  Example    : @chromos = @{$slice_adaptor->fetch_all('chromosome','NCBI33')};
               @contigs = @{$slice_adaptor->fetch_all('contig')};

               #create chunks of size 100k with 10k overlap
               @fixed_chunks = @{$slice_adaptor->fetch_all('chromosome', undef,
                                                           1e5, 1e4);

  Description: Retrieves slices of all seq_regions for a given coordinate
               system.  This is analagous to the methods fetch_all which were
               formerly on the ChromosomeAdaptor, RawContigAdaptor and
               CloneAdaptor classes.  Slices fetched span the entire
               seq_regions and are on the forward strand.
  Returntype : listref of Bio::EnsEMBL::Slices
  Exceptions : throw if invalid coord system is provided
               throw if max_length < 1 is provided
               throw if overlap < 0 is provided
               throw if overlap is provided but max_length is not 
               throw if overlap is greater than max_length
  Caller     : general
Web Admin's avatar
Web Admin committed
443 444 445

=cut

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
sub fetch_all {
  my $self = shift;
  my $cs_name = shift;
  my $cs_version = shift || '';

  my ($max_length, $overlap) = @_;

  #
  # sanity checking of the overlap / maxlength arguments
  #
  if(defined($max_length) && $max_length < 1) {
    throw("Invalid max length [$max_length] provided.");
  }

  $overlap ||= 0;
  if($overlap != 0) {
    if(!defined($max_length)) {
      throw("Cannot define overlap without defining valid max_length.");
Web Admin's avatar
Web Admin committed
464
    }
465 466 467 468 469 470 471
    if($overlap < 0) {
      throw("Cannot define overlap that is less than 0.");
    }
    if($max_length <= $overlap) {
      throw("Overlap must be less than max_length.");
    } 
  }
Web Admin's avatar
Web Admin committed
472

473 474 475 476 477 478 479 480 481 482 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
  #
  # verify existance of requested coord system and get its id
  #
  my $csa = $self->db->get_CoordSystemAdaptor();
  my $cs = $csa->fetch_by_name($cs_name, $cs_version);

  
  #
  # Retrieve the seq_region from the database
  #
  my $sth = $self->prepare('SELECT seq_region_id, name, length ' .
                           'FROM   seq_region ' .
                           'WHERE  coord_system_id =?');

  $sth->execute($cs->dbID);

  my ($seq_region_id, $name, $length);
  $sth->bind_columns(\$seq_region_id, \$name, \$length);

  my $name_cache = $self->{'_name_cache'};
  my $id_cache   = $self->{'_id_cache'};

  my $cs_key = lc($cs->name().':'.$cs_version);

  my @out;
  while($sth->fetch()) {
    #cache values for future reference
    my $key = lc($name) . ':'. $cs_key;
    $name_cache->{$key} = [$seq_region_id, $length];
    $id_cache->{$seq_region_id} = [$name, $length, $cs];

    #
    # split the seq regions into appropriately sized chunks
    #
    my $start = 1;
    my $end;
    my $multiple;
    my $number;

    if($max_length && ($length > $overlap)) {
      #No seq region may be longer than max_length but we want to make
      #them all similar size so that the last one isn't much shorter.
      #Divide the seq_region into the largest equal pieces that are shorter
      #than max_length

      #calculate number of slices to create
      $number = ($length-$overlap) / ($max_length-$overlap);
      $number = int($number + 1.0); #round up to int (ceiling) 

      #calculate length of created slices
      $multiple = $length / $number;
      $multiple   = int($multiple); #round down to int (floor)
    } else {
      #just one slice of the whole seq_region
      $number = 1;
      $multiple = $length;
    }

    my $i;
    for(my $i=0; $i < $number; $i++) {
      $end = $start + $multiple + $overlap;

      #any remainder gets added to the last slice of the seq_region
      $end = $length if($i == $number-1);

      push @out, Bio::EnsEMBL::Slice->new(-START           => $start,
                                          -END             => $end,
                                          -STRAND          => 1,
                                          -SEQ_REGION_NAME => $name,
                                          -COORD_SYSTEM    => $cs,
                                          -ADAPTOR         => $self);
      $start += $multiple;
545
    }
546
  }
547

548
  return \@out;
Web Admin's avatar
Web Admin committed
549 550 551
}


552
=head2 fetch_all_non_redundant
Graham McVicker's avatar
Graham McVicker committed
553

554 555 556 557 558 559
  Arg [1]    : none
  Example    : @all = @{$slice_adaptor->fetch_all_non_redundant()};
  Description: Retrieves all non-redundant slices, i.e. those which have
               the attribute 'nonredundant' set
  Returntype : listref of Bio::EnsEMBL::Slices
  Exceptions : none
Graham McVicker's avatar
Graham McVicker committed
560
  Caller     : general
561 562 563

=cut

564
sub fetch_all_non_redundant {
565

566
  my $self = shift;
567

568 569 570 571 572 573
  my $sth = $self->prepare("SELECT s.name, s.length, c.coord_system_id " .
			   "FROM seq_region s, coord_system c, seq_region_attrib sra, attrib_type at " .
			   "WHERE s.coord_system_id=c.coord_system_id " .
			   "AND at.code='nonredundant' " .
			   "AND at.attrib_type_id=sra.attrib_type_id " .
			   "AND sra.seq_region_id=s.seq_region_id");
574

575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
  $sth->execute();

  my ($name, $length, $cs_id);
  $sth->bind_columns(\$name, \$length, \$cs_id);

  # Slice expects a CoordSystem object
  my $cs_adaptor = $self->db->get_CoordSystemAdaptor();


  my @out;
  while($sth->fetch()) {
    my $cs = $cs_adaptor->fetch_by_dbID($cs_id);
    push @out, Bio::EnsEMBL::Slice->new(-START  => 1,
                                        -END    => $length,
                                        -STRAND => 1,
                                        -SEQ_REGION_NAME => $name,
                                        -COORD_SYSTEM => $cs,
                                        -ADAPTOR => $self);
  }

  return \@out;

}


sub deleteObj {
  my $self = shift;

  $self->SUPER::deleteObj;
604

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
  $self->{'_id_cache'} = {};
  $self->{'_name_cache'} = {};
  $self->{'_exc_cache'} = {};
}



=head2 fetch_by_chr_band

 Title   : fetch_by_chr_band
 Usage   :
 Function: create a Slice representing a series of bands
 Example :
 Returns :
 Args    : the band name

=cut

sub fetch_by_chr_band {
  my ($self,$chr,$band) = @_;

  my $chr_slice = $self->fetch_by_region('chromosome', $chr);

  my $seq_region_id = $self->get_seq_region_id($chr_slice);

  my $sth = $self->db->prepare
        ("select min(k.chr_start), max(k.chr_end) " .
         "from karyotype as k " .
         "where k.seq_region_id = ? and k.band like ?");

  $sth->execute( $seq_region_id, "$band%" );
  my ( $slice_start, $slice_end) = $sth->fetchrow_array;

  if(defined $slice_start) {
    return $self->fetch_by_region('chromosome',$chr,$slice_start,$slice_end);
  }

  throw("Band not recognised in database");
643 644 645 646
}



Graham McVicker's avatar
Graham McVicker committed
647
=head2 fetch_by_transcript_stable_id
648

Graham McVicker's avatar
Graham McVicker committed
649 650 651 652 653 654 655 656 657
  Arg [1]    : string $transcriptid
               The stable id of the transcript around which the slice is 
               desired
  Arg [2]    : (optional) int $size
               The length of the flanking regions the slice should encompass 
               on either side of the transcript (0 by default)
  Example    : $slc = $sa->fetch_by_transcript_stable_id('ENST00000302930',10);
  Description: Creates a slice around the region of the specified transcript. 
               If a context size is given, the slice is extended by that 
658 659
               number of basepairs on either side of the
               transcript.
Graham McVicker's avatar
Graham McVicker committed
660
  Returntype : Bio::EnsEMBL::Slice
661
  Exceptions : Thrown if the transcript is not in the database.
Graham McVicker's avatar
Graham McVicker committed
662
  Caller     : general
663 664 665

=cut

Graham McVicker's avatar
Graham McVicker committed
666
sub fetch_by_transcript_stable_id{
667 668
  my ($self,$transcriptid,$size) = @_;

669 670
  throw('Transcript argument is required.') if(!$transcriptid);

671
  my $ta = $self->db->get_TranscriptAdaptor;
672
  my $transcript = $ta->fetch_by_stable_id($transcriptid);
673

674 675 676 677
  throw("Transcript [$transcriptid] does not exist in DB.") if(!$transcript);

  return $self->fetch_by_Feature($transcript, $size);
}
678

Graham McVicker's avatar
Graham McVicker committed
679 680


Graham McVicker's avatar
Graham McVicker committed
681 682
=head2 fetch_by_transcript_id

Graham McVicker's avatar
Graham McVicker committed
683 684 685 686 687 688 689
  Arg [1]    : int $transcriptid
               The unique database identifier of the transcript around which 
               the slice is desired
  Arg [2]    : (optional) int $size
               The length of the flanking regions the slice should encompass 
               on either side of the transcript (0 by default)
  Example    : $slc = $sa->fetch_by_transcript_id(24, 1000);
690 691 692 693
  Description: Creates a slice around the region of the specified transcript.
               If a context size is given, the slice is extended by that
               number of basepairs on either side of the
               transcript.
Graham McVicker's avatar
Graham McVicker committed
694
  Returntype : Bio::EnsEMBL::Slice
695 696
  Exceptions : throw on incorrect args
               throw if transcript is not in database
Graham McVicker's avatar
Graham McVicker committed
697
  Caller     : general
698 699 700

=cut

Graham McVicker's avatar
Graham McVicker committed
701
sub fetch_by_transcript_id {
702
  my ($self,$transcriptid,$size) = @_;
Graham McVicker's avatar
Graham McVicker committed
703

704
  throw('Transcript id argument is required.') if(!$transcriptid);
Graham McVicker's avatar
Graham McVicker committed
705

706 707 708 709 710 711
  my $transcript_adaptor = $self->db()->get_TranscriptAdaptor();
  my $transcript = $transcript_adaptor->fetch_by_dbID($transcriptid);

  throw("Transcript [$transcriptid] does not exist in DB.") if(!$transcript);

  return $self->fetch_by_Feature($transcript, $size);
712 713
}

714 715


Graham McVicker's avatar
Graham McVicker committed
716
=head2 fetch_by_gene_stable_id
717

Graham McVicker's avatar
Graham McVicker committed
718
  Arg [1]    : string $geneid
719
               The stable id of the gene around which the slice is
Graham McVicker's avatar
Graham McVicker committed
720 721
               desired
  Arg [2]    : (optional) int $size
Graham McVicker's avatar
Graham McVicker committed
722
               The length of the flanking regions the slice should encompass
Graham McVicker's avatar
Graham McVicker committed
723 724
               on either side of the gene (0 by default)
  Example    : $slc = $sa->fetch_by_transcript_stable_id('ENSG00000012123',10);
Graham McVicker's avatar
Graham McVicker committed
725 726 727
  Description: Creates a slice around the region of the specified gene.
               If a context size is given, the slice is extended by that
               number of basepairs on either side of the gene.
728
               The slice will be created in the genes native coordinate system.
Graham McVicker's avatar
Graham McVicker committed
729
  Returntype : Bio::EnsEMBL::Slice
730 731
  Exceptions : throw on incorrect args
               throw if transcript does not exist
Graham McVicker's avatar
Graham McVicker committed
732
  Caller     : general
733 734 735

=cut

736 737
sub fetch_by_gene_stable_id {
  my ($self,$geneid,$size) = @_;
738

739
  throw('Gene argument is required.') if(!$geneid);
740

741 742
  my $gene_adaptor = $self->db->get_GeneAdaptor();
  my $gene = $gene_adaptor->fetch_by_stable_id($geneid);
743

744
  throw("Gene [$geneid] does not exist in DB.") if(!$gene);
745

746
  return $self->fetch_by_Feature($gene, $size);
747 748 749
}


750

751
=head2 fetch_by_Feature
Graham McVicker's avatar
Graham McVicker committed
752

753 754 755 756 757 758 759 760 761 762
  Arg [1]    : Bio::EnsEMBL::Feature $feat
               The feature to fetch the slice around
  Arg [2]    : int size (optional)
               The desired number of flanking basepairs around the feature.
  Example    : $slice = $slice_adaptor->fetch_by_Feature($feat, 100);
  Description: Retrieves a slice around a specific feature.  All this really
               does is return a resized version of the slice that the feature
               is already on. Note that slices returned from this method
               are always on the forward strand of the seq_region regardless of
               the strandedness of the feature passed in.
Graham McVicker's avatar
Graham McVicker committed
763
  Returntype : Bio::EnsEMBL::Slice
764 765 766 767
  Exceptions : throw if the feature does not have an attached slice
               throw if feature argument is not provided
  Caller     : fetch_by_gene_stable_id, fetch_by_transcript_stable_id,
               fetch_by_gene_id, fetch_by_transcript_id
Graham McVicker's avatar
Graham McVicker committed
768 769 770

=cut

771 772
sub fetch_by_Feature{
  my ($self, $feature, $size) = @_;
Graham McVicker's avatar
Graham McVicker committed
773

774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
  $size ||= 0;

  if(!ref($feature) || !$feature->isa('Bio::EnsEMBL::Feature')) {
    throw('Feature argument expected.');
  }

  my $slice = $feature->slice();
  if(!$slice || !$slice->isa('Bio::EnsEMBL::Slice')) {
    throw('Feature must be attached to a valid slice.');
  }

  my $fstart = $feature->start();
  my $fend   = $feature->end();
  if(!defined($fstart) || !defined($fend)) {
    throw('Feature must have defined start and end.');
  }

  #convert the feature slice coordinates to seq_region coordinates
  my $slice_start  = $slice->start();
  my $slice_end    = $slice->end();
  my $slice_strand = $slice->strand();
  if($slice_start != 1 || $slice_strand != 1) {
    if($slice_strand == 1) {
      $fstart = $fstart + $slice_start - 1;
      $fend   = $fend   + $slice_start - 1;
    } else {
      my $tmp_start = $fstart;
      $fstart = $slice_end - $fend      + 1;
      $fend   = $slice_end - $tmp_start + 1;
    }
  }

  #return a new slice covering the region of the feature
  return Bio::EnsEMBL::Slice->new(-seq_region_name => $slice->seq_region_name,
                                  -coord_system    => $slice->coord_system,
                                  -start           => $fstart - $size,
                                  -end             => $fend + $size,
                                  -strand          => 1,
                                  -adaptor         => $self);
}



=head2 fetch_by_misc_feature_attribute

  Arg [1]    : string $attribute_type
               The code of the attribute type
  Arg [2]    : (optional) string $attribute_value
               The value of the attribute to fetch by
  Arg [3]    : (optional) int $size
               The amount of flanking region around the misc feature desired.
  Example    : $slice = $sa->fetch_by_misc_feature_attribute('superctg',
                                                             'NT_030871');
               $slice = $sa->fetch_by_misc_feature_attribute('synonym',
                                                             'AL00012311',
                                                             $flanking);
  Description: Fetches a slice around a MiscFeature with a particular
               attribute type and value. If no value is specified then
               the feature with the particular attribute is used.
               If no size is specified then 0 is used.
  Returntype : Bio::EnsEMBL::Slice
  Exceptions : Throw if no feature with the specified attribute type and value
               exists in the database
               Warning if multiple features with the specified attribute type
               and value exist in the database.
  Caller     : webcode

=cut

sub fetch_by_misc_feature_attribute {
  my ($self, $attrib_type_code, $attrib_value, $size) = @_;
Graham McVicker's avatar
Graham McVicker committed
845

846
  my $mfa = $self->db()->get_MiscFeatureAdaptor();
Web Admin's avatar
Web Admin committed
847

848 849
  my $feats = $mfa->fetch_all_by_attribute_type_value($attrib_type_code,
                                                   $attrib_value);
Web Admin's avatar
Web Admin committed
850

851 852 853 854
  if(@$feats == 0) {
    throw("MiscFeature with $attrib_type_code=$attrib_value does " .
          "not exist in DB.");
  }
Graham McVicker's avatar
Graham McVicker committed
855

856 857 858 859
  if(@$feats > 1) {
    warning("MiscFeature with $attrib_type_code=$attrib_value is " .
            "ambiguous - using first one found.");
  }
860

861
  my ($feat) = @$feats;
862

863
  return $self->fetch_by_Feature($feat, $size);
Graham McVicker's avatar
Graham McVicker committed
864 865
}

Graham McVicker's avatar
Graham McVicker committed
866

867
=head2 fetch_normalized_slice_projection
Graham McVicker's avatar
Graham McVicker committed
868

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
  Arg [1]    : Bio::EnsEMBL::Slice $slice
  Example    :  ( optional )
  Description: gives back a project style result. The returned slices 
               represent the areas to which there are symlinks for the 
               given slice. start, end show which area on given slice is 
               symlinked
  Returntype : [[start,end,$slice][]]
  Exceptions : none
  Caller     : BaseFeatureAdaptor

=cut


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

  my $slice_seq_region_id = $self->get_seq_region_id( $slice );

  my $result = $self->{'asm_exc_cache'}->{$slice_seq_region_id};

  if(!defined($result)) {
    my $sql = "
      SELECT seq_region_id, seq_region_start, seq_region_end,
             exc_type, exc_seq_region_id, exc_seq_region_start,
             exc_seq_region_end
        FROM assembly_exception
       WHERE seq_region_id = ?";

    my $sth = $self->prepare( $sql );
    $sth->execute( $slice_seq_region_id );
    $result = $sth->fetchall_arrayref();
    $self->{'asm_exc_cache'}->{$slice_seq_region_id} = $result;
  }

  my (@haps, @pars);

  foreach my $row (@$result) {
    my ( $seq_region_id, $seq_region_start, $seq_region_end,
         $exc_type, $exc_seq_region_id, $exc_seq_region_start,
         $exc_seq_region_end ) = @$row;

    # need overlapping PAR and all HAPs if any
    if( $exc_type eq "PAR" ) {
      if( $seq_region_start <= $slice->end() && 
          $seq_region_end >= $slice->start() ) {
        push( @pars, [ $seq_region_start, $seq_region_end, $exc_seq_region_id,
                       $exc_seq_region_start, $exc_seq_region_end ] );
      }
    } else {
      push( @haps, [ $seq_region_start, $seq_region_end, $exc_seq_region_id,
                     $exc_seq_region_start, $exc_seq_region_end ] );
    }
  }

  if(!@pars && !@haps) {
    #just return this slice, there were no haps or pars
    return  [[1,$slice->length, $slice]];
  }

  my @syms;
  if( @haps > 1 ) {
    my @sort_haps = sort { $a->[1] <=> $b->[1] } @haps;
    throw( "More than one HAP region not supported yet" );
  } elsif( @haps == 1 ) {
    my $hap = $haps[0];

    my $seq_reg_slice = $self->fetch_by_seq_region_id($slice_seq_region_id);
    my $exc_slice = $self->fetch_by_seq_region_id( $hap->[2] );

    #
    # lengths of haplotype and reference in db may be different
    # we want to use the maximum possible length for the mapping
    # between the two systems
    #
    my $len1 = $seq_reg_slice->length();
    my $len2 = $exc_slice->length();
    my $max_len = ($len1 > $len2) ? $len1 : $len2;

    #the inserted region can differ in length, but mapped sections
    #need to be same lengths
    my $diff = $hap->[4] - $hap->[1];

    # we want the region of the haplotype INVERTED
    push( @syms, [ 1, $hap->[0]-1, $hap->[2], 1, $hap->[3] - 1 ] );
    push( @syms, [ $hap->[1]+1, $max_len - $diff, 
                   $hap->[2], $hap->[4] + 1, $max_len ] );   
  }

  # for now haps and pars should not be both there, but in theory we 
  # could handle it here by cleverly merging the pars into the existing syms,
  # for now just:
  push( @syms, @pars );

  my $mapper = Bio::EnsEMBL::Mapper->new( "sym", "org" );
  for my $sym ( @syms ) {
    $mapper->add_map_coordinates( $slice_seq_region_id, $sym->[0], $sym->[1],
                                  1, $sym->[2], $sym->[3], $sym->[4] );
  }
968

969 970 971
  my @linked = $mapper->map_coordinates( $slice_seq_region_id,
                                         $slice->start(), $slice->end(),
                                         $slice->strand(), "sym" );
972

973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
  # gaps are regions where there is no mapping to another region
  my $rel_start = 1;

  #if there was only one coord and it is a gap, we know it is just the
  #same slice with no overlapping symlinks
  if(@linked == 1 && $linked[0]->isa('Bio::EnsEMBL::Mapper::Gap')) {
    return [[1,$slice->length, $slice]];
  }

  my @out;
  for my $coord ( @linked ) {
    if( $coord->isa( "Bio::EnsEMBL::Mapper::Gap" )) {
      my $exc_slice = Bio::EnsEMBL::Slice->new
        (-START        => $coord->start(),
         -END          => $coord->end(),
         -STRAND       => $slice->strand(),
         -COORD_SYSTEM => $slice->coord_system(),
         -ADAPTOR      => $self,
         -SEQ_REGION_NAME => $slice->seq_region_name);
      push( @out, [ $rel_start, $coord->length()+$rel_start-1,
                        $exc_slice ] );
    } else {
      my $exc_slice = $self->fetch_by_seq_region_id( $coord->id() );
      my $exc2_slice = Bio::EnsEMBL::Slice->new
        (
         -START  => $coord->start(),
         -END    => $coord->end(),
         -STRAND => $coord->strand(),
         -SEQ_REGION_NAME => $exc_slice->seq_region_name(),
         -COORD_SYSTEM => $exc_slice->coord_system(),
         -ADAPTOR => $self
        );
	
      push( @out, [ $rel_start, $coord->length() + $rel_start - 1,
                    $exc2_slice ] );
    }
    $rel_start += $coord->length();
  }

  return \@out;
}




=head2 store

  Arg [1]    : Bio::EnsEMBL::Slice $slice
  Arg [2]    : (optional) $seqref reference to a string
               The sequence associated with the slice to be stored.
  Example    : $slice = Bio::EnsEMBL::Slice->new(...);
               $seq_region_id = $slice_adaptor->store($slice, \$sequence);
  Description: This stores a slice as a sequence region in the database
               and returns the seq region id. The passed in slice must
               start at 1, and must have a valid seq_region name and coordinate
               system. The attached coordinate system must already be stored in
               the database.  The sequence region is assumed to start at 1 and
               to have a length equalling the length of the slice.
               If the slice coordinate system is the sequence level coordinate
               system then the seqref argument must also be passed.  If the
               slice coordinate system is NOT a sequence level coordinate
               system then the sequence argument cannot be passed.
  Returntype : int 
  Exceptions : throw if slice has no coord system.
               throw if slice coord system is not already stored.
               throw if slice coord system is seqlevel and no sequence is 
                     provided.
               throw if slice coord system is not seqlevel and sequence is
                     provided.
               throw if slice does not start at 1
               throw if sequence is provided and the sequence length does not
                     match the slice length.
               throw if the SQL insert fails (e.g. on duplicate seq region)
               throw if slice argument is not passed
  Caller     : database loading scripts

=cut



sub store {
  my $self = shift;
  my $slice = shift;
  my $seqref = shift;

  #
  # Get all of the sanity checks out of the way before storing anything
  #

  if(!ref($slice) || !$slice->isa('Bio::EnsEMBL::Slice')) {
    throw('Slice argument is required');
  }

  my $cs = $slice->coord_system();
  throw("Slice must have attached CoordSystem.") if(!$cs);

  my $db = $self->db();
  if(!$cs->is_stored($db)) {
    throw("Slice CoordSystem must already be stored in DB.") 
  }

  if($slice->start != 1 || $slice->strand != 1) {
    throw("Slice must have start==1 and strand==1.");
  }

  my $sr_len = $slice->length();
  my $sr_name  = $slice->seq_region_name();

  if(!$sr_name) {
    throw("Slice must have valid seq region name.");
  }

  if($cs->is_sequence_level()) {
    if(!$seqref) {
      throw("Must provide sequence for sequence level coord system.");
    }
    if(ref($seqref) ne 'SCALAR') {
      throw("Sequence must be a scalar reference.");
    }
    my $seq_len = length($$seqref);

    if($seq_len != $sr_len) {
      throw("Sequence length ($seq_len) must match slice length ($sr_len).");
    }
  } else {
    if($seqref) {
      throw("Cannot provide sequence for non-sequence level seq regions.");
    }
  }

  #store the seq_region

  my $sth = $db->prepare("INSERT INTO seq_region " .
                         "SET    name = ?, " .
                         "       length = ?, " .
                         "       coord_system_id = ?" );

  $sth->execute($sr_name, $sr_len, $cs->dbID());

  my $seq_region_id = $sth->{'mysql_insertid'};

  if(!$seq_region_id) {
    throw("Database seq_region insertion failed.");
  }

  if($cs->is_sequence_level()) {
    #store sequence if it was provided
    my $seq_adaptor = $db->get_SequenceAdaptor();
    $seq_adaptor->store($seq_region_id, $$seqref);
  }

  return $seq_region_id;
}



#####################################
# sub DEPRECATED METHODs
#####################################

=head2 fetch_by_mapfrag

 Function: DEPRECATED use fetch_by_misc_feature_attribute('synonym',$mapfrag)
1136 1137 1138 1139

=cut

sub fetch_by_mapfrag{
1140
   my ($self,$mymapfrag,$flag,$size) = @_;
1141
   deprecate('Use fetch_by_misc_feature_attribute instead');
1142
   $flag ||= 'fixed-width'; # alt.. 'context'
1143 1144 1145
   $size ||= $flag eq 'fixed-width' ? 100000 : 0;
   return $self->fetch_by_misc_feature_attribute('synonym',$mymapfrag,$size);
}
1146

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164


=head2 fetch_by_chr_start_end

  Description: DEPRECATED use fetch_by_region instead

=cut

sub fetch_by_chr_start_end {
  my ($self,$chr,$start,$end) = @_;
  deprecate('Use fetch_by_region() instead');

  #assume that by chromosome the user actually meant top-level coord
  #system since this is the old behaviour of this deprecated method
  my $csa = $self->db->get_CoordSystemAdaptor();
  my $cs = $csa->fetch_top_level();

  return $self->fetch_by_region($cs->name,$chr,$start,$end,1,$cs->version);
1165 1166 1167 1168
}



1169
=head2 fetch_by_contig_name
Graham McVicker's avatar
Graham McVicker committed
1170

1171 1172
  Description: Deprecated. Use fetch_by_region(), Slice::project(),
               Slice::expand() instead
Graham McVicker's avatar
Graham McVicker committed
1173

1174
=cut
Graham McVicker's avatar
Graham McVicker committed
1175

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
sub fetch_by_contig_name {
  my ($self, $name, $size) = @_;

  deprecate('Use fetch_by_region(), Slice::project() and Slice::expand().');

  #previously wanted chromosomal slice on a given contig.  Assume this means
  #a top-level slice on a given seq_region in the seq_level coord system
  my $csa = $self->db()->get_CoordSystemAdaptor();
  my $top_level = $csa->fetch_top_level();
  my $seq_level = $csa->fetch_sequence_level();

  my $seq_lvl_slice = $self->fetch_by_region($seq_level->name(), $name);

  my @projection = @{$seq_lvl_slice->project($top_level->name(),
                                             $top_level->version())};
  if(@projection == 0) {
    warning("contig $name is not used in ".$top_level->name().' assembly.');
    return undef;
  }

  if(@projection > 1) {
    warning("$name is mapped to multiple locations in " . $top_level->name());
  }

  return $projection[0]->[2]->expand($size, $size);
}


=head2 fetch_by_clone_accession

  Description: DEPRECATED.  Use fetch_by_region, Slice::project, Slice::expand
               instead.
Graham McVicker's avatar
Graham McVicker committed
1208 1209 1210

=cut

1211 1212
sub fetch_by_clone_accession{
  my ($self,$name,$size) = @_;
1213

1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
  deprecate('Use fetch_by_region(), Slice::project() and Slice::expand().');

  my $csa = $self->db()->get_CoordSystemAdaptor();
  my $top_level = $csa->fetch_top_level();
  my $clone_cs = $csa->fetch_by_name('clone');

  if(!$clone_cs) {
    warning('Clone coordinate system does not exist for this species');
    return undef;
  }

  #this unfortunately needs a version on the end to work
  if(! ($name =~ /\./)) {
    my $sth = $self->prepare("SELECT sr.name " .
                             "FROM   seq_region sr, coord_system cs " .
                             "WHERE  cs.name = 'clone' " .
                             "AND    cs.coord_system_id = sr.coord_system_id ".
                             "AND    sr.name LIKE '$name.%'");
    $sth->execute();
    if(!$sth->rows()) {
      $sth->finish();
      throw("Clone $name not found in database");
    }

    ($name) = $sth->fetchrow_array();

    $sth->finish();
  }
Graham McVicker's avatar
Graham McVicker committed
1242

1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
  my $clone = $self->fetch_by_region($clone_cs->name(), $name);
  my @projection = @{$clone->project($top_level->name(),
                                     $top_level->version())};
  if(@projection == 0) {
    warning("clone $name is not used in " . $top_level->name() . ' assembly.');
    return undef;
  }

  if(@projection > 1) {
    warning("$name is mapped to multiple locations in " . $top_level->name());
  }

  return $projection[0]->[2]->expand($size, $size);
Graham McVicker's avatar
Graham McVicker committed
1256 1257 1258
}


1259
=head2 fetch_by_supercontig_name
Graham McVicker's avatar
Graham McVicker committed
1260

1261 1262
  Description: DEPRECATED. Use fetch_by_region(), Slice::project() and
               Slice::expand() instead
Graham McVicker's avatar
Graham McVicker committed
1263 1264 1265

=cut

1266 1267 1268 1269
sub fetch_by_supercontig_name {
  my ($self,$name, $size) = @_;

  deprecate('Use fetch_by_region(), Slice::project() and Slice::expand().');
Graham McVicker's avatar
Graham McVicker committed
1270

1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
  my $csa = $self->db()->get_CoordSystemAdaptor();
  my $top_level = $csa->fetch_top_level();
  my $sc_level = $csa->fetch_by_name('supercontig');

  if(!$sc_level) {
    warning('No supercontig coordinate system exists for this species.');
    return undef;
  }

  my $sc_slice = $self->fetch_by_region($sc_level->name(),$name);

  my @projection = @{$sc_slice->project($top_level->name(),
                                        $top_level->version())};
  if(@projection == 0) {
    warning("Supercontig $name is not used in " . $top_level->name() .
         ' assembly.');
    return undef;
  }

  if(@projection > 1) {
    warning("$name is mapped to multiple locations in " . $top_level->name());
  }

  return $projection[0]->[2]->expand($size, $size);
1295
}
Graham McVicker's avatar
Graham McVicker committed
1296 1297 1298 1299




1300
=head2 list_overlapping_supercontigs
Graham McVicker's avatar
Graham McVicker committed
1301

1302
  Description: DEPRECATED use Slice::project instead
Graham McVicker's avatar
Graham McVicker committed
1303

1304
=cut
Graham McVicker's avatar
Graham McVicker committed
1305

1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
sub list_overlapping_supercontigs {
   my ($self,$slice) = @_;

   deprecate('Use Slice::project() instead.');

   my $csa = $self->db()->get_CoordSystemAdaptor();
   my $top_level = $csa->fetch_top_level();
   my $sc_level = $csa->fetch_by_name('supercontig');

   if(!$sc_level) {
     warning('No supercontig coordinate system exists for this species.');
     return undef;
   }

   my @out;
   foreach my $seg (@{$slice->project($sc_level->name(), $sc_level->version)}){
     push @out, $seg->[2]->seq_region_name();
   }

   return \@out;
}



=head2 fetch_by_chr_name

  Description: DEPRECATED. Use fetch by region instead

=cut

sub fetch_by_chr_name{
   my ($self,$chr_name) = @_;
   deprecate('Use fetch_by_region() instead.');

   return $self->fetch_by_region('toplevel',$chr_name);
}


1;