SplicingEventAdaptor.pm 16.2 KB
Newer Older
1 2
=head1 LICENSE

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

=cut

=head1 NAME

Bio::EnsEMBL::DBSQL::SlicingEventAdaptor - Database adaptor for the retrieval and
storage of SplicingEvent objects

=head1 SYNOPSIS

  use Bio::EnsEMBL::Registry;

  Bio::EnsEMBL::Registry->load_registry_from_db(
    -host => 'ensembldb.ensembl.org',
    -user => 'anonymous',
  );

  $se_adaptor =
    Bio::EnsEMBL::Registry->get_adaptor( "human", "core", "SplicingEvent" );

  $se = $se_adaptor->fetch_by_dbID(12);

  $slice_adaptor =
    Bio::EnsEMBL::Registry->get_adaptor( "human", "core", "slice" );

  $slice =
    $slice_adaptor->fetch_by_region( 'chromosome', '1', 1, 1000000 );

  @ase = @{ $se_adaptor->fetch_all_by_Slice($slice) };

=head1 DESCRIPTION

This is a database aware adaptor for the retrieval and storage ofSlicingEvents
objects.

=head1 METHODS

=cut
package Bio::EnsEMBL::DBSQL::SplicingEventAdaptor;

use strict;

use Bio::EnsEMBL::Utils::Exception qw( deprecate throw warning );
use Bio::EnsEMBL::DBSQL::SliceAdaptor;
use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
use Bio::EnsEMBL::DBSQL::DBAdaptor;
use Bio::EnsEMBL::SplicingEvent;

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


=head2 list_dbIDs

  Example    : @gene_ids = @{$gene_adaptor->list_dbIDs()};
  Description: Gets an array of internal ids for all genes in the current db
  Arg[1]     : <optional> int. not 0 for the ids to be sorted by the seq_region.
  Returntype : Listref of Ints
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut

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

  return $self->_list_dbIDs("splicing_event", undef, $ordered);
}


=head2 fetch_all_by_Slice

  Arg [1]    : Bio::EnsEMBL::Slice $slice
               The slice to fetch genes on.
  Arg [2]    : type of Transcript event
  Arg [3]    : (optional) boolean $load_features
104 105
               If true, transcript will be loaded immediately rather
               than lazy loaded later.
106 107
=cut

108 109
sub fetch_all_by_Slice {
  my ( $self, $slice, $type, $load_features ) = @_;
110

111
  my $constraint = '';
112

113 114 115 116
  if ( defined($type) ) {
    $constraint .= sprintf( " AND at.code = %s",
                            $self->dbc()->db_handle()->quote($type) );
  }
117

118 119
  my $tes =
    $self->SUPER::fetch_all_by_Slice_constraint( $slice, $constraint );
120

121 122
  # Is there any use in having a splice event without the pairs and
  # features??
123

124
  if ( !$load_features || scalar( @{$tes} ) < 2 ) {
125 126 127
    return $tes;
  }

128 129
  # Load pairs and features.
  foreach my $te ( @{$tes} ) {
130 131 132 133 134
    $te->get_all_Features();
    $te->get_all_Pairs();
  }

  return $tes;
135 136
} ## end sub fetch_all_by_Slice

137

138 139
sub fetch_all_by_Gene {
  my ( $self, $gene ) = @_;
140

141 142 143 144 145 146 147 148 149 150 151 152
  my $sth = $self->dbc->prepare(
    q(
SELECT  se.splicing_event_id,
        se.seq_region_id,
        se.seq_region_start,
        se.seq_region_end,
        se.seq_region_strand,
        se.name,
        at.code
FROM    splicing_event se
  JOIN  attrib_type at USING (attrib_type_id)
WHERE   se.gene_id =) . $gene->dbID() );
153 154

  $sth->execute();
155 156 157 158 159 160 161 162 163

  my ( $splicing_event_id, $seq_region_id, $seq_region_start,
       $seq_region_end, $seq_region_strand, $name, $type );

  $sth->bind_columns(
               \( $splicing_event_id, $seq_region_id, $seq_region_start,
                  $seq_region_end, $seq_region_strand, $name,
                  $type ) );

164 165
  my @splicing_events;

166 167 168 169 170 171 172 173
  my $sa = $self->db()->get_SliceAdaptor();

  while ( $sth->fetch() ) {
    my $slice =
      $sa->fetch_by_seq_region_id( $seq_region_id,
                                   $seq_region_start,
                                   $seq_region_end,
                                   $seq_region_strand );
174 175

    push( @splicing_events,
176 177 178 179 180 181 182 183 184 185
          $self->_create_feature_fast( 'Bio::EnsEMBL::SplicingEvent', {
                                         'start'  => $seq_region_start,
                                         'end'    => $seq_region_end,
                                         'strand' => $seq_region_strand,
                                         'adaptor' => $self,
                                         'slice'   => $slice,
                                         'dbID' => $splicing_event_id,
                                         'name' => $name,
                                         'gene_id' => $gene->dbID(),
                                         'type'    => $type } ) );
186 187
  }

188
  foreach my $te (@splicing_events) {
189 190 191
    $te->get_all_Features();
    $te->get_all_Pairs();
  }
192

193
  return \@splicing_events;
194
} ## end sub fetch_all_by_Gene
195

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
sub fetch_all_by_Exon {
  my ( $self, $exon ) = @_;

  my $sth = $self->dbc()->prepare(
    q(
SELECT DISTINCT splicing_event_id
FROM    splicing_event_feature
WHERE   exon_id =) . $exon->dbID() );

  $sth->execute();

  my $se_id;
  $sth->bind_col( 1, \$se_id );

  my @list;
  while ( $sth->fetch() ) {
    push( @list, $se_id );
213
  }
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

  $sth = $self->dbc->prepare(
    q(
SELECT  se.splicing_event_id,
        se.seq_region_id,
        se.seq_region_start,
        se.seq_region_end,
        se.seq_region_strand,
        se.name,
        at.code,
        se.gene_id
FROM    splicing_event se
  JOIN  attrib_type at USING (attrib_type_id)
WHERE   se.splicing_event_id in ) . '(' . join( ',', @list ) . ')' );

229
  $sth->execute();
230 231 232 233 234 235 236 237 238

  my ( $splicing_event_id, $seq_region_id, $seq_region_start,
       $seq_region_end, $seq_region_strand, $name, $type, $gene_id );

  $sth->bind_columns(
               \( $splicing_event_id, $seq_region_id, $seq_region_start,
                  $seq_region_end, $seq_region_strand, $name,
                  $type,           $gene_id ) );

239 240 241 242
  my @splicing_events;

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

243 244 245 246 247 248 249
  while ( $sth->fetch ) {
    my $slice =
      $sa->fetch_by_seq_region_id( $seq_region_id,
                                   $seq_region_start,
                                   $seq_region_end,
                                   $seq_region_strand );

250
    push( @splicing_events,
251 252 253 254 255 256 257 258 259 260
          $self->_create_feature_fast( 'Bio::EnsEMBL::SplicingEvent', {
                                         'start'  => $seq_region_start,
                                         'end'    => $seq_region_end,
                                         'strand' => $seq_region_strand,
                                         'adaptor' => $self,
                                         'slice'   => $slice,
                                         'dbID' => $splicing_event_id,
                                         'name' => $name,
                                         'gene_id' => $gene_id,
                                         'type'    => $type } ) );
261 262
  }

263
  foreach my $te (@splicing_events) {
264 265 266
    $te->get_all_Features();
    $te->get_all_Pairs();
  }
267

268
  return \@splicing_events;
269 270 271 272
} ## end sub fetch_all_by_Exon

sub fetch_all_by_Transcript {
  my ( $self, $transcript ) = @_;
273

274 275 276 277 278 279 280 281 282 283 284 285 286 287
  my $sth = $self->dbc->prepare(
    q(
SELECT DISTINCT splicing_event_id
FROM    splicing_event_feature
WHERE   transcript_id =) . $transcript->dbID() );

  $sth->execute();

  my $se_id;
  $sth->bind_col( 1, \$se_id );

  my @list;
  while ( $sth->fetch() ) {
    push( @list, $se_id );
288
  }
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

  $sth = $self->dbc->prepare(
    q(
SELECT  se.splicing_event_id,
        se.seq_region_id,
        se.seq_region_start,
        se.seq_region_end,
        se.seq_region_strand,
        se.name,
        at.code,
        se.gene_id
FROM    splicing_event se
  JOIN  attrib_type at USING (attrib_type_id)
WHERE   se.splicing_event_id in ) . '(' . join( ',', @list ) . ')' );

304
  $sth->execute();
305 306 307 308 309 310 311 312 313

  my ( $splicing_event_id, $seq_region_id, $seq_region_start,
       $seq_region_end, $seq_region_strand, $name, $type, $gene_id );

  $sth->bind_columns(
               \( $splicing_event_id, $seq_region_id, $seq_region_start,
                  $seq_region_end, $seq_region_strand, $name,
                  $type,           $gene_id ) );

314 315
  my @splicing_events;

316 317 318 319 320 321 322 323
  my $sa = $self->db()->get_SliceAdaptor();

  while ( $sth->fetch() ) {
    my $slice =
      $sa->fetch_by_seq_region_id( $seq_region_id,
                                   $seq_region_start,
                                   $seq_region_end,
                                   $seq_region_strand );
324 325

    push( @splicing_events,
326 327 328 329 330 331 332 333 334 335
          $self->_create_feature_fast( 'Bio::EnsEMBL::SplicingEvent', {
                                         'start'  => $seq_region_start,
                                         'end'    => $seq_region_end,
                                         'strand' => $seq_region_strand,
                                         'adaptor' => $self,
                                         'slice'   => $slice,
                                         'dbID' => $splicing_event_id,
                                         'name' => $name,
                                         'gene_id' => $gene_id,
                                         'type'    => $type } ) );
336 337
  }

338
  foreach my $te (@splicing_events) {
339 340 341
    $te->get_all_Features();
    $te->get_all_Pairs();
  }
342

343
  return \@splicing_events;
344
} ## end sub fetch_all_by_Transcript
345 346 347 348 349 350 351 352 353 354 355

# _tables
#  Arg [1]    : none
#  Description: PROTECTED implementation of superclass abstract method.
#               Returns the names, aliases of the tables to use for queries.
#  Returntype : list of listrefs of strings
#  Exceptions : none
#  Caller     : internal
#  Status     : At Risk

sub _tables {
356
  return ( [ 'splicing_event', 'se' ], [ 'attrib_type', 'at' ] );
357 358 359 360 361 362 363 364 365 366 367 368 369
}

# _columns
#  Arg [1]    : none
#  Example    : none
#  Description: PROTECTED implementation of superclass abstract method.
#               Returns a list of columns to use for queries.
#  Returntype : list of strings
#  Exceptions : none
#  Caller     : internal
#  Status     : At Risk

sub _columns {
370 371 372 373 374
  return ( 'se.splicing_event_id', 'se.seq_region_id',
           'se.seq_region_start',  'se.seq_region_end',
           'se.seq_region_strand', 'se.name',
           'se.gene_id',           'at.code' );
}
375

376 377
sub _left_join {
  return ( [ 'attrib_type', 'at.attrib_type_id = se.attrib_type_id' ] );
378 379 380
}

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

383 384 385 386 387 388 389
  my ( $splicing_event_id, $seq_region_id, $seq_region_start,
       $seq_region_end, $seq_region_strand, $name, $gene_id, $type );

  $sth->bind_columns(
               \( $splicing_event_id, $seq_region_id, $seq_region_start,
                  $seq_region_end, $seq_region_strand, $name,
                  $gene_id,        $type ) );
390

391
  my $sa = $self->db()->get_SliceAdaptor();
392 393 394 395 396 397 398 399 400 401 402 403

  my @splicing_events;
  my %slice_hash;
  my %sr_name_hash;
  my %sr_cs_hash;

  my $asm_cs;
  my $cmp_cs;
  my $asm_cs_vers;
  my $asm_cs_name;
  my $cmp_cs_vers;
  my $cmp_cs_name;
404 405 406 407

  if ( defined($mapper) ) {
    $asm_cs      = $mapper->assembled_CoordSystem();
    $cmp_cs      = $mapper->component_CoordSystem();
408 409 410 411 412 413 414 415 416 417 418 419 420 421
    $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;
  my $dest_slice_cs;
  my $dest_slice_sr_name;
  my $dest_slice_sr_id;
  my $asma;
422 423 424 425 426 427 428

  if ( defined($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();
    $dest_slice_cs      = $dest_slice->coord_system();
429
    $dest_slice_sr_name = $dest_slice->seq_region_name();
430 431
    $dest_slice_sr_id   = $dest_slice->get_seq_region_id();
    $asma               = $self->db->get_AssemblyMapperAdaptor();
432 433
  }

434 435 436
FEATURE:
  while ( $sth->fetch() ) {
    # Need to get the internal_seq_region, if present.
437 438
    $seq_region_id = $self->get_seq_region_id_internal($seq_region_id);

439
    my $slice       = $slice_hash{ "ID:" . $seq_region_id };
440 441
    my $dest_mapper = $mapper;

442
    if ( !$slice ) {
443
      $slice = $sa->fetch_by_seq_region_id($seq_region_id);
444 445 446
      $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();
447 448
    }

449 450 451 452 453 454 455 456 457 458
    # Obtain a mapper if none was defined, but a dest_seq_region was.
    if (   !defined($dest_mapper)
         && defined($dest_slice)
         && !$dest_slice_cs->equals( $slice->coord_system ) )
    {
      $dest_mapper =
        $asma->fetch_by_CoordSystems( $dest_slice_cs,
                                      $slice->coord_system );
      $asm_cs      = $dest_mapper->assembled_CoordSystem();
      $cmp_cs      = $dest_mapper->component_CoordSystem();
459 460 461 462 463 464 465 466
      $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 $sr_name = $sr_name_hash{$seq_region_id};
    my $sr_cs   = $sr_cs_hash{$seq_region_id};
467 468 469 470

    # Remap the feature coordinates to another coord system if a mapper
    # was provided.
    if ( defined($dest_mapper) ) {
471 472 473 474 475 476 477 478 479 480 481 482 483

      if (defined $dest_slice && $dest_mapper->isa('Bio::EnsEMBL::ChainedAssemblyMapper')  ) {
	    ( $seq_region_id,  $seq_region_start,
	      $seq_region_end, $seq_region_strand )
		=
		$dest_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 )
		= $dest_mapper->fastmap( $sr_name, $seq_region_start,
484 485
                                 $seq_region_end, $seq_region_strand,
                                 $sr_cs );
486
      }
487 488 489 490 491 492 493

      # Skip features that map to gaps or coord system boundaries.
      if ( !defined($seq_region_id) ) { next FEATURE }

      # Get a slice in the coord system we just mapped to.
      $slice = $slice_hash{ "ID:" . $seq_region_id } ||=
        $sa->fetch_by_seq_region_id($seq_region_id);
494 495
    }

496 497 498 499 500 501 502 503 504 505 506 507 508
    # If a destination slice was provided convert the coords.  If the
    # dest_slice starts at 1 and is foward strand, nothing needs doing.
    if ( defined($dest_slice) ) {
      if ( $dest_slice_start != 1 || $dest_slice_strand != 1 ) {
        if ( $dest_slice_strand == 1 ) {
          $seq_region_start = $seq_region_start - $dest_slice_start + 1;
          $seq_region_end   = $seq_region_end - $dest_slice_start + 1;
        } else {
          my $tmp_seq_region_start = $seq_region_start;
          $seq_region_start = $dest_slice_end - $seq_region_end + 1;
          $seq_region_end = $dest_slice_end - $tmp_seq_region_start + 1;
          $seq_region_strand = -$seq_region_strand;
        }
509 510
      }

511 512 513 514 515 516
      # Throw away features off the end of the requested slice.
      if (    $seq_region_end < 1
           || $seq_region_start > $dest_slice_length
           || ( $dest_slice_sr_id != $seq_region_id ) )
      {
        next FEATURE;
517 518 519 520 521 522 523 524
      }

      $slice = $dest_slice;
    }

    # Finally, create the new splicing_event.
    push( @splicing_events,
          $self->_create_feature_fast( 'Bio::EnsEMBL::SplicingEvent', {
525 526 527 528 529 530 531 532 533 534 535
                                         'start'  => $seq_region_start,
                                         'end'    => $seq_region_end,
                                         'strand' => $seq_region_strand,
                                         'adaptor' => $self,
                                         'slice'   => $slice,
                                         'dbID' => $splicing_event_id,
                                         'name' => $name,
                                         'gene_id' => $gene_id,
                                         'type'    => $type } ) );

  } ## end while ( $sth->fetch() )
536 537

  return \@splicing_events;
538
} ## end sub _objs_from_sth
539 540 541


1;