EnsEMBL/DBSQL/TranscriptAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 748 Branchbran 0.0% 0 / 244 Conditioncond 0.0% 0 / 99 Subroutinesub 0.0% 0 / 46 Podpod 0.0% 0 / 37 totaltotal 0.0% 0 / 1174 CC 123 SCAR 59.0
CC
123
Cov
0%
store
930.0
_objs_from_sth
930.0
fetch_all_by_Slice
210.0
CRAP 366.9
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::DBSQL::TranscriptAdaptor - An adaptor which performs database
34 interaction relating to the storage and retrieval of Transcripts
35  
36 =head1 SYNOPSIS
37  
38 use Bio::EnsEMBL::Registry;
39  
40 Bio::EnsEMBL::Registry->load_registry_from_db(
41 -host => 'ensembldb.ensembl.org',
42 -user => 'anonymous'
43 );
44  
45 $transcript_adaptor =
46 Bio::EnsEMBL::Registry->get_adaptor( 'Human', 'Core',
47 'Transcript' );
48  
49 $transcript = $transcript_adaptor->fetch_by_dbID(1234);
50  
51 $transcript =
52 $transcript_adaptor->fetch_by_stable_id('ENST00000201961');
53  
54 $slice =
55 $slice_adaptor->fetch_by_region( 'Chromosome', '3', 1, 1000000 );
56 @transcripts = @{ $transcript_adaptor->fetch_all_by_Slice($slice) };
57  
58 ($transcript) =
59 @{ $transcript_adaptor->fetch_all_by_external_name('NP_065811.1') };
60  
61 =head1 DESCRIPTION
62  
63 This adaptor provides a means to retrieve and store information related
64 to Transcripts. Primarily this involves the retrieval or storage of
65 Bio::EnsEMBL::Transcript objects from a database.
66  
67 See Bio::EnsEMBL::Transcript for details of the Transcript class.
68  
69 =cut
70  
71 package Bio::EnsEMBL::DBSQL::TranscriptAdaptor;
72 $Bio::EnsEMBL::DBSQL::TranscriptAdaptor::VERSION = '114.0.0_50'; # TRIAL
73 $Bio::EnsEMBL::DBSQL::TranscriptAdaptor::VERSION = '114.0.050';
74 use strict;
75  
76 use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
77 use Bio::EnsEMBL::Gene;
78 use Bio::EnsEMBL::Exon;
79 use Bio::EnsEMBL::Transcript;
80 use Bio::EnsEMBL::Translation;
81 use Bio::EnsEMBL::Utils::Exception qw( throw warning );
82 use Bio::EnsEMBL::Utils::Scalar qw( assert_ref );
83  
84 use vars qw(@ISA);
85 @ISA = qw( Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor );
86  
87  
88 # _tables
89 #
90 # Description: PROTECTED implementation of superclass abstract method.
91 # Returns the names, aliases of the tables to use for queries.
92 # Returntype : list of listrefs of strings
93 # Exceptions : none
94 # Caller : internal
95 # Status : Stable
96  
97 sub _tables {
98 return (
99 [ 'transcript', 't' ],
100 [ 'xref', 'x' ],
101 [ 'external_db', 'exdb' ] );
102 }
103  
104  
105 #_columns
106 #
107 # Description: PROTECTED implementation of superclass abstract method.
108 # Returns a list of columns to use for queries.
109 # Returntype : list of strings
110 # Exceptions : none
111 # Caller : internal
112 # Status : Stable
113  
114 sub _columns {
115 my ($self) = @_;
116  
117 my $created_date =
118 $self->db()->dbc()->from_date_to_seconds("created_date");
119 my $modified_date =
120 $self->db()->dbc()->from_date_to_seconds("modified_date");
121  
122 my @columns =
123 (
124 't.transcript_id', 't.seq_region_id',
125 't.seq_region_start', 't.seq_region_end',
126 't.seq_region_strand', 't.analysis_id',
127 't.gene_id', 't.is_current',
128 't.stable_id', 't.version',
129 $created_date, $modified_date,
130 't.description', 't.biotype',
131 'exdb.db_name',
132 'exdb.status', 'exdb.db_display_name',
133 'x.xref_id', 'x.display_label',
134 'x.dbprimary_acc', 'x.version',
135 'x.description', 'x.info_type',
136 'x.info_text', 'exdb.db_release'
137 );
138  
139 $self->schema_version > 74 and push @columns, 't.source';
140  
141 return @columns;
142 }
143  
144 sub _left_join {
145 return (
146 [ 'xref', "x.xref_id = t.display_xref_id" ],
147 [ 'external_db', "exdb.external_db_id = x.external_db_id" ]
148 );
149 }
150  
151  
152 =head2 fetch_by_stable_id
153  
154 Arg [1] : String $stable_id
155 The stable id of the transcript to retrieve
156 Example : my $tr = $tr_adaptor->fetch_by_stable_id('ENST00000309301');
157 Description: Retrieves a transcript via its stable id.
158 Returntype : Bio::EnsEMBL::Transcript
159 Exceptions : none
160 Caller : general
161 Status : Stable
162  
163 =cut
164  
165 sub fetch_by_stable_id {
166 my ($self, $stable_id) = @_;
167  
168 my $constraint = "t.stable_id = ? AND t.is_current = 1";
169  
170 $self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
171  
172 my ($transcript) = @{ $self->generic_fetch($constraint) };
173  
174 # If we didn't get anything back, desperately try to see if there's
175 # a version number in the stable_id
176 if(!defined($transcript) && (my $vindex = rindex($stable_id, '.'))) {
177 $transcript = $self->fetch_by_stable_id_version(substr($stable_id,0,$vindex),
178 substr($stable_id,$vindex+1));
179 }
180  
181 return $transcript;
182 }
183  
184  
185 =head2 fetch_by_stable_id_version
186  
187 Arg [1] : String $id
188 The stable ID of the transcript to retrieve
189 Arg [2] : Integer $version
190 The version of the stable_id to retrieve
191 Example : $tr = $tr_adaptor->fetch_by_stable_id('ENST00000309301', 3);
192 Description: Retrieves a transcript object from the database via its
193 stable id and version.
194 The transcript will be retrieved in its native coordinate system (i.e.
195 in the coordinate system it is stored in the database). It may
196 be converted to a different coordinate system through a call to
197 transform() or transfer(). If the transcript is not found
198 undef is returned instead.
199 Returntype : Bio::EnsEMBL::Transcript or undef
200 Exceptions : if we cant get the transcript in given coord system
201 Caller : general
202 Status : Stable
203  
204 =cut
205  
206 sub fetch_by_stable_id_version {
207 my ($self, $stable_id, $version) = @_;
208  
209 # Enforce that version be numeric
210 return unless($version =~ /^\d+$/);
211  
212 my $constraint = "t.stable_id = ? AND t.version = ? AND t.is_current = 1";
213 $self->bind_param_generic_fetch($stable_id, SQL_VARCHAR);
214 $self->bind_param_generic_fetch($version, SQL_INTEGER);
215 my ($transcript) = @{$self->generic_fetch($constraint)};
216  
217 return $transcript;
218 }
219  
220 sub fetch_all {
221 my ($self) = @_;
222  
223 my $constraint = 't.biotype != "LRG_gene" and t.is_current = 1';
224 my @trans = @{ $self->generic_fetch($constraint) };
225 return \@trans ;
226 }
227  
228 =head2 fetch_all_versions_by_stable_id
229  
230 Arg [1] : String $stable_id
231 The stable ID of the transcript to retrieve
232 Example : my $tr = $tr_adaptor->fetch_all_version_by_stable_id
233 ('ENST00000309301');
234 Description : Similar to fetch_by_stable_id, but retrieves all versions of a
235 transcript stored in the database.
236 Returntype : listref of Bio::EnsEMBL::Transcript objects
237 Exceptions : if we cant get the gene in given coord system
238 Caller : general
239 Status : At Risk
240  
241 =cut
242  
243 sub fetch_all_versions_by_stable_id {
244 my ($self, $stable_id) = @_;
245  
246 my $constraint = "t.stable_id = ?";
247  
248 $self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
249  
250 return $self->generic_fetch($constraint);
251 }
252  
253  
254 =head2 fetch_by_rnaproduct_id
255  
256 Arg [1] : Int $id
257 The internal identifier of the RNAProduct whose transcript
258 is to be retrieved
259 Example : my $tr = $tr_adaptor->fetch_by_rnaproduct_id($rnap->dbID);
260 Description: Given the internal identifier of a RNAProduct this method
261 retrieves the transcript associated with that RNAProduct.
262 If the transcript cannot be found undef is returned instead.
263 Returntype : Bio::EnsEMBL::Transcript or undef
264 Exceptions : none
265 Caller : general
266 Status : Stable
267  
268 =cut
269  
270 sub fetch_by_rnaproduct_id {
271 my ($self, $p_dbID) = @_;
272  
273 throw("dbID argument is required") unless defined($p_dbID);
274  
275 my $sth = $self->prepare(
276 "SELECT transcript_id FROM rnaproduct WHERE rnaproduct_id = ?"
277 );
278 $sth->bind_param(1, $p_dbID, SQL_INTEGER);
279 $sth->execute();
280  
281 my ($dbID) = $sth->fetchrow_array();
282 $sth->finish();
283  
284 if ($dbID) {
285 return $self->fetch_by_dbID($dbID);
286 }
287  
288 return;
289 }
290  
291  
292 =head2 fetch_by_translation_stable_id
293  
294 Arg [1] : String $transl_stable_id
295 The stable identifier of the translation of the transcript to
296 retrieve
297 Example : my $tr = $tr_adaptor->fetch_by_translation_stable_id
298 ('ENSP00000311007');
299 Description: Retrieves a Transcript object using the stable identifier of
300 its translation.
301 Returntype : Bio::EnsEMBL::Transcript or undef
302 Exceptions : none
303 Caller : general
304 Status : Stable
305  
306 =cut
307  
308 sub fetch_by_translation_stable_id {
309 my ($self, $transl_stable_id ) = @_;
310  
311 my $sth = $self->prepare(qq(
312 SELECT t.transcript_id
313 FROM translation tl,
314 transcript t
315 WHERE tl.stable_id = ?
316 AND tl.transcript_id = t.transcript_id
317 AND t.is_current = 1
318 ));
319  
320 $sth->bind_param(1, $transl_stable_id, SQL_VARCHAR);
321 $sth->execute();
322  
323 my ($id) = $sth->fetchrow_array;
324 $sth->finish;
325 if ($id){
326 return $self->fetch_by_dbID($id);
327 } elsif(my $vindex = rindex($transl_stable_id, '.')) {
328 return $self->fetch_by_translation_stable_id_version(substr($transl_stable_id,0,$vindex),
329 substr($transl_stable_id,$vindex+1));
330 } else {
331 return undef;
332 }
333 }
334  
335 =head2 fetch_by_translation_stable_id_version
336  
337 Arg [1] : String $transl_stable_id
338 The stable identifier of the translation of the transcript to
339 retrieve
340 Arg [2] : Integer $version
341 The version of the translation of the transcript to retrieve
342 Example : my $tr = $tr_adaptor->fetch_by_translation_stable_id_version
343 ('ENSP00000311007', 2);
344 Description: Retrieves a Transcript object using the stable identifier and
345 version of its translation.
346 Returntype : Bio::EnsEMBL::Transcript or undef
347 Exceptions : none
348 Caller : general
349 Status : Stable
350  
351 =cut
352  
353 sub fetch_by_translation_stable_id_version {
354 my ($self, $transl_stable_id, $transl_version ) = @_;
355  
356 # Enforce that version be numeric
357 return unless($transl_version =~ /^\d+$/);
358  
359 my $sth = $self->prepare(qq(
360 SELECT t.transcript_id
361 FROM translation tl,
362 transcript t
363 WHERE tl.stable_id = ?
364 AND tl.version = ?
365 AND tl.transcript_id = t.transcript_id
366 AND t.is_current = 1
367 ));
368  
369 $sth->bind_param(1, $transl_stable_id, SQL_VARCHAR);
370 $sth->bind_param(2, $transl_version, SQL_INTEGER);
371 $sth->execute();
372  
373 my ($id) = $sth->fetchrow_array;
374 $sth->finish;
375 if ($id){
376 return $self->fetch_by_dbID($id);
377 } else {
378 return undef;
379 }
380 }
381  
382 =head2 fetch_by_translation_id
383  
384 Arg [1] : Int $id
385 The internal identifier of the translation whose transcript
386 is to be retrieved
387 Example : my $tr = $tr_adaptor->fetch_by_translation_id($transl->dbID);
388 Description: Given the internal identifier of a translation this method
389 retrieves the transcript associated with that translation.
390 If the transcript cannot be found undef is returned instead.
391 Returntype : Bio::EnsEMBL::Transcript or undef
392 Exceptions : none
393 Caller : general
394 Status : Stable
395  
396 =cut
397  
398 sub fetch_by_translation_id {
399 my ( $self, $p_dbID ) = @_;
400  
401 if ( !defined($p_dbID) ) {
402 throw("dbID argument is required");
403 }
404  
405 my $sth =
406 $self->prepare( "SELECT transcript_id "
407 . "FROM translation "
408 . "WHERE translation_id = ?" );
409  
410 $sth->bind_param( 1, $p_dbID, SQL_INTEGER );
411 $sth->execute();
412  
413 my ($dbID) = $sth->fetchrow_array();
414 $sth->finish();
415  
416 if ($dbID) {
417 return $self->fetch_by_dbID($dbID);
418 }
419  
420 return undef;
421 }
422  
423 =head2 fetch_all_by_Gene
424  
425 Arg [1] : Bio::EnsEMBL::Gene $gene
426 The gene to fetch transcripts of
427 Example : my $gene = $gene_adaptor->fetch_by_stable_id('ENSG0000123');
428 my @transcripts = { $tr_adaptor->fetch_all_by_Gene($gene) };
429 Description: Retrieves Transcript objects for given gene. Puts Genes slice
430 in each Transcript.
431 Returntype : Listref of Bio::EnsEMBL::Transcript objects
432 Exceptions : none
433 Caller : Gene->get_all_Transcripts()
434 Status : Stable
435  
436 =cut
437  
438 sub fetch_all_by_Gene {
439 my ( $self, $gene ) = @_;
440  
441 my $constraint = "t.gene_id = " . $gene->dbID();
442  
443 # Use the fetch_all_by_Slice_constraint method because it handles the
444 # difficult Haps/PARs and coordinate remapping.
445  
446 # Get a slice that entirely overlaps the gene. This is because we
447 # want all transcripts to be retrieved, not just ones overlapping
448 # the slice the gene is on (the gene may only partially overlap the
449 # slice). For speed reasons, only use a different slice if necessary
450 # though.
451  
452 my $gslice = $gene->slice();
453  
454 if ( !defined($gslice) ) {
455 throw("Gene must have attached slice to retrieve transcripts.");
456 }
457  
458 my $slice;
459  
460 if ( $gene->start() < 1 || $gene->end() > $gslice->length() ) {
461 if ( $gslice->is_circular() ) {
462 $slice = $gslice;
463 } else {
464 $slice = $self->db->get_SliceAdaptor->fetch_by_Feature($gene);
465 }
466 } else {
467 $slice = $gslice;
468 }
469  
470 my $transcripts =
471 $self->fetch_all_by_Slice_constraint( $slice, $constraint );
472  
473 if ( $slice != $gslice ) {
474 my @out;
475 foreach my $tr ( @{$transcripts} ) {
476 push( @out, $tr->transfer($gslice) );
477 }
478 $transcripts = \@out;
479 }
480  
481 my $canonical_t = $gene->canonical_transcript();
482  
483 foreach my $t ( @{$transcripts} ) {
484 if ( $t->equals($canonical_t) ) {
485 $t->is_canonical(1);
486 last;
487 }
488 }
489  
490 return $transcripts;
491 } ## end sub fetch_all_by_Gene
492  
493  
494 =head2 fetch_all_by_Slice
495  
496 Arg [1] : Bio::EnsEMBL::Slice $slice
497 The slice to fetch transcripts on
498 Arg [2] : (optional) Boolean $load_exons
499 If true, exons will be loaded immediately rather than
500 lazy loaded later
501 Arg [3] : (optional) String $logic_name
502 The logic name of the type of features to obtain
503 ARG [4] : (optional) String $constraint
504 An extra contraint.
505 Example : my @transcripts = @{ $tr_adaptor->fetch_all_by_Slice($slice) };
506 Description: Overrides superclass method to optionally load exons
507 immediately rather than lazy-loading them later. This
508 is more efficient when there are a lot of transcripts whose
509 exons are going to be used.
510 Returntype : Listref of Bio::EnsEMBL::Transcript objects
511 Exceptions : thrown if exon cannot be placed on transcript slice
512 Caller : Slice::get_all_Transcripts
513 Status : Stable
514  
515 =cut
516  
517 sub fetch_all_by_Slice {
518 my ( $self, $slice, $load_exons, $logic_name, $constraint, $source, $biotype ) = @_;
519  
520 if (defined $constraint and $constraint ne '') {
521 $constraint .= ' AND t.is_current = 1';
522 } else {
523 $constraint .= 't.is_current = 1';
524 }
525 if (defined($source)) {
526 $constraint .= " and t.source = '$source'";
527 }
528 if (defined($biotype)) {
529 my $inline_variables = 1;
530 $constraint .= " and ".$self->generate_in_constraint($biotype, 't.biotype', SQL_VARCHAR, $inline_variables);
531 }
532  
533 my $transcripts = $self->SUPER::fetch_all_by_Slice_constraint( $slice, $constraint, $logic_name);
534  
535 # if there are 0 transcripts still do lazy-loading
536 if ( !$load_exons || @$transcripts < 1 ) {
537 return $transcripts;
538 }
539  
540 # preload all of the exons now, instead of lazy loading later
541 # faster than 1 query per transcript
542  
543 # first check if the exons are already preloaded
544 # @todo FIXME: Should test all exons.
545 if ( exists( $transcripts->[0]->{'_trans_exon_array'} ) ) {
546 return $transcripts;
547 }
548  
549 # get extent of region spanned by transcripts
550 my ($min_start, $max_end);
551 my $ext_slice;
552  
553 unless ($slice->is_circular()) {
554 foreach my $t (@$transcripts) {
555 if (!defined($min_start) || $t->seq_region_start() < $min_start) {
556 $min_start = $t->seq_region_start();
557 }
558 if (!defined($max_end) || $t->seq_region_end() > $max_end) {
559 $max_end = $t->seq_region_end();
560 }
561 }
562  
563 if ($min_start >= $slice->start() && $max_end <= $slice->end()) {
564 $ext_slice = $slice;
565 } else {
566 my $sa = $self->db()->get_SliceAdaptor();
567 $ext_slice = $sa->fetch_by_region($slice->coord_system->name(), $slice->seq_region_name(), $min_start, $max_end, $slice->strand(), $slice->coord_system->version());
568 }
569  
570 } else {
571 # feature might be crossing the origin of replication (i.e. seq_region_start > seq_region_end)
572 # the computation of min_start|end based on seq_region_start|end is not safe
573 # use feature start/end relative to the slice instead
574 my ($min_start_feature, $max_end_feature);
575 foreach my $t (@$transcripts) {
576 if (!defined($min_start) || ($t->start >= 0 && $t->start() < $min_start)) {
577 $min_start = $t->start();
578 $min_start_feature = $t;
579 }
580 if (!defined($max_end) || ($t->end() >= 0 && $t->end() > $max_end)) {
581 $max_end = $t->end();
582 $max_end_feature = $t;
583 }
584 }
585
586 # now we can reassign min_start|end to seq_region_start|end of
587 # the feature which spans the largest region
588 $min_start = $min_start_feature->seq_region_start();
589 $max_end = $max_end_feature->seq_region_end();
590  
591 my $sa = $self->db()->get_SliceAdaptor();
592 $ext_slice =
593 $sa->fetch_by_region($slice->coord_system->name(),
594 $slice->seq_region_name(),
595 $min_start,
596 $max_end,
597 $slice->strand(),
598 $slice->coord_system->version());
599 }
600  
601  
602  
603 # associate exon identifiers with transcripts
604  
605 my %tr_hash = map { $_->dbID => $_ } @{$transcripts};
606  
607 my $tr_id_str = join( ',', keys(%tr_hash) );
608  
609 my $sth =
610 $self->prepare( "SELECT `transcript_id`, `exon_id`, `rank` "
611 . "FROM exon_transcript "
612 . "WHERE transcript_id IN ($tr_id_str)" );
613  
614 $sth->execute();
615  
616 my ( $tr_id, $ex_id, $rank );
617 $sth->bind_columns( \( $tr_id, $ex_id, $rank ) );
618  
619 my %ex_tr_hash;
620  
621 while ( $sth->fetch() ) {
622 $ex_tr_hash{$ex_id} ||= [];
623 push( @{ $ex_tr_hash{$ex_id} }, [ $tr_hash{$tr_id}, $rank ] );
624 }
625  
626 my $ea = $self->db()->get_ExonAdaptor();
627 my $exons = $ea->fetch_all_by_Slice_constraint(
628 $ext_slice,
629 sprintf( "e.exon_id IN (%s)",
630 join( ',', sort { $a <=> $b } keys(%ex_tr_hash) ) ) );
631  
632 # move exons onto transcript slice, and add them to transcripts
633 foreach my $ex ( @{$exons} ) {
634 my $new_ex;
635 if ( $slice != $ext_slice ) {
636 $new_ex = $ex->transfer($slice);
637 if ( !defined($new_ex) ) {
638 throw("Unexpected. "
639 . "Exon could not be transfered onto Transcript slice." );
640 }
641 } else {
642 $new_ex = $ex;
643 }
644  
645 foreach my $row ( @{ $ex_tr_hash{ $new_ex->dbID() } } ) {
646 my ( $tr, $rank ) = @{$row};
647 $tr->add_Exon( $new_ex, $rank );
648 }
649 }
650  
651 my $tla = $self->db()->get_TranslationAdaptor();
652  
653 # load all of the translations at once
654 $tla->fetch_all_by_Transcript_list($transcripts);
655  
656 return $transcripts;
657 } ## end sub fetch_all_by_Slice
658  
659  
660 =head2 fetch_all_by_external_name
661  
662 Arg [1] : String $external_name
663 An external identifier of the transcript to be obtained
664 Arg [2] : (optional) String $external_db_name
665 The name of the external database from which the
666 identifier originates.
667 Arg [3] : Boolean override. Force SQL regex matching for users
668 who really do want to find all 'NM%'
669 Example : my @transcripts =
670 @{ $tr_adaptor->fetch_all_by_external_name( 'NP_065811.1') };
671 my @more_transcripts =
672 @{$tr_adaptor->fetch_all_by_external_name( 'NP_0658__._')};
673 Description: Retrieves all transcripts which are associated with
674 an external identifier such as a GO term, Swissprot
675 identifer, etc. Usually there will only be a single
676 transcript returned in the list reference, but not
677 always. Transcripts are returned in their native
678 coordinate system, i.e. the coordinate system in which
679 they are stored in the database. If they are required
680 in another coordinate system the Transcript::transfer or
681 Transcript::transform method can be used to convert them.
682 If no transcripts with the external identifier are found,
683 a reference to an empty list is returned.
684 SQL wildcards % and _ are supported in the $external_name
685 but their use is somewhat restricted for performance reasons.
686 Users that really do want % and _ in the first three characters
687 should use argument 3 to prevent optimisations
688 Returntype : listref of Bio::EnsEMBL::Transcript
689 Exceptions : none
690 Caller : general
691 Status : Stable
692  
693 =cut
694  
695 sub fetch_all_by_external_name {
696 my ( $self, $external_name, $external_db_name, $override) = @_;
697  
698 my $entryAdaptor = $self->db->get_DBEntryAdaptor();
699  
700 my @ids =
701 $entryAdaptor->list_transcript_ids_by_extids( $external_name,
702 $external_db_name, $override );
703  
704 my @features = @{ $self->fetch_all_by_dbID_list( \@ids ) };
705 my @reference = grep { $_->slice()->is_reference() } @features;
706 my @non_reference = grep { ! $_->slice()->is_reference() } @features;
707 return [ @reference, @non_reference ];
708 }
709  
710 =head2 fetch_all_by_GOTerm
711  
712 Arg [1] : Bio::EnsEMBL::OntologyTerm
713 The GO term for which transcripts should be fetched.
714  
715 Example: @transcripts = @{
716 $transcript_adaptor->fetch_all_by_GOTerm(
717 $go_adaptor->fetch_by_accession('GO:0030326') ) };
718  
719 Description : Retrieves a list of transcripts that are
720 associated with the given GO term, or with any of
721 its descendent GO terms. The transcripts returned
722 are in their native coordinate system, i.e. in
723 the coordinate system in which they are stored
724 in the database. If another coordinate system
725 is required then the Transcript::transfer or
726 Transcript::transform method can be used.
727  
728 Return type : listref of Bio::EnsEMBL::Transcript
729 Exceptions : Throws of argument is not a GO term
730 Caller : general
731 Status : Stable
732  
733 =cut
734  
735 sub fetch_all_by_GOTerm {
736 my ( $self, $term ) = @_;
737  
738 assert_ref( $term, 'Bio::EnsEMBL::OntologyTerm' );
739 if ( $term->ontology() ne 'GO' ) {
740 throw('Argument is not a GO term');
741 }
742  
743 my $entryAdaptor = $self->db->get_DBEntryAdaptor();
744  
745 my %unique_dbIDs;
746 foreach my $accession ( map { $_->accession() }
747 ( $term, @{ $term->descendants() } ) )
748 {
749 my @ids =
750 $entryAdaptor->list_transcript_ids_by_extids( $accession, 'GO' );
751 foreach my $dbID (@ids) { $unique_dbIDs{$dbID} = 1 }
752 }
753  
754 my @result = @{
755 $self->fetch_all_by_dbID_list(
756 [ sort { $a <=> $b } keys(%unique_dbIDs) ]
757 ) };
758  
759 return \@result;
760 } ## end sub fetch_all_by_GOTerm
761  
762 =head2 fetch_all_by_GOTerm_accession
763  
764 Arg [1] : String
765 The GO term accession for which genes should be
766 fetched.
767  
768 Example :
769  
770 @genes =
771 @{ $gene_adaptor->fetch_all_by_GOTerm_accession(
772 'GO:0030326') };
773  
774 Description : Retrieves a list of genes that are associated with
775 the given GO term, or with any of its descendent
776 GO terms. The genes returned are in their native
777 coordinate system, i.e. in the coordinate system
778 in which they are stored in the database. If
779 another coordinate system is required then the
780 Gene::transfer or Gene::transform method can be
781 used.
782  
783 Return type : listref of Bio::EnsEMBL::Gene
784 Exceptions : Throws of argument is not a GO term accession
785 Caller : general
786 Status : Stable
787  
788 =cut
789  
790 sub fetch_all_by_GOTerm_accession {
791 my ( $self, $accession ) = @_;
792  
793 if ( $accession !~ /^GO:/ ) {
794 throw('Argument is not a GO term accession');
795 }
796  
797 my $goAdaptor =
798 Bio::EnsEMBL::Registry->get_adaptor( 'Multi', 'Ontology',
799 'OntologyTerm' );
800  
801 my $term = $goAdaptor->fetch_by_accession($accession);
802  
803 return $self->fetch_all_by_GOTerm($term);
804 }
805  
806 =head2 fetch_by_display_label
807  
808 Arg [1] : String $label - display label of transcript to fetch
809 Example : my $tr = $tr_adaptor->fetch_by_display_label("BRCA2");
810 Description: Returns the transcript which has the given display label or
811 undef if there is none. If there are more than 1, only the first
812 is reported.
813 Returntype : Bio::EnsEMBL::Transcript
814 Exceptions : none
815 Caller : general
816 Status : Stable
817  
818 =cut
819  
820 sub fetch_by_display_label {
821 my $self = shift;
822 my $label = shift;
823  
824 my $constraint = "x.display_label = ? AND t.is_current = 1";
825  
826 $self->bind_param_generic_fetch($label,SQL_VARCHAR);
827  
828 my ($transcript) = @{ $self->generic_fetch($constraint) };
829  
830 return $transcript;
831 }
832  
833  
834 =head2 fetch_all_by_exon_stable_id
835  
836 Arg [1] : String $stable_id
837 The stable id of an exon in a transcript
838 Example : my $tr = $tr_adaptor->fetch_all_by_exon_stable_id
839 ('ENSE00000309301');
840 Description: Retrieves a list of transcripts via an exon stable id.
841 Returntype : Listref of Bio::EnsEMBL::Transcript objects
842 Exceptions : none
843 Caller : general
844 Status : Stable
845  
846 =cut
847  
848 sub fetch_all_by_exon_stable_id {
849 my ($self, $stable_id) = @_;
850  
851 my @trans ;
852  
853 my $sth = $self->prepare(qq(
854 SELECT t.transcript_id
855 FROM exon_transcript et, exon e, transcript t
856 WHERE e.exon_id = et.exon_id
857 AND et.transcript_id = t.transcript_id
858 AND e.stable_id = ?
859 AND t.is_current = 1
860 ));
861
862 $sth->bind_param(1, $stable_id, SQL_VARCHAR);
863 $sth->execute();
864  
865 while( my $id = $sth->fetchrow_array ) {
866 my $transcript = $self->fetch_by_dbID($id);
867 push(@trans, $transcript) if $transcript;
868 }
869  
870 if (!@trans) {
871 return undef;
872 }
873  
874 return \@trans;
875 }
876  
877 =head2 fetch_all_by_source
878  
879 Arg [1] : String $source
880 listref of $sources
881 The source of the transcript to retrieve. You can have as an argument a reference
882 to a list of sources
883 Example : $transcripts = $transcript_adaptor->fetch_all_by_source('havana');
884 $transcripts = $transcript_adaptor->fetch_all_by_source(['ensembl', 'vega']);
885 Description: Retrieves an array reference of transcript objects from the database via its source or sources.
886 The transcript will be retrieved in its native coordinate system (i.e.
887 in the coordinate system it is stored in the database). It may
888 be converted to a different coordinate system through a call to
889 transform() or transfer(). If the gene or exon is not found
890 undef is returned instead.
891 Returntype : listref of Bio::EnsEMBL::Transcript
892 Exceptions : if we cant get the gene in given coord system
893 Caller : general
894 Status : Stable
895  
896 =cut
897  
898 sub fetch_all_by_source {
899 my ($self, $source) = @_;
900 my @transcripts = @{$self->generic_fetch($self->source_constraint($source))};
901 return \@transcripts;
902 }
903  
904 =head2 source_constraint
905  
906 Arg [1] : String $source
907 listref of $sources
908 The source of the transcript to retrieve. You can have as an argument a reference
909 to a list of sources
910 Description: Used internally to generate a SQL constraint to restrict a transcript query by source
911 Returntype : String
912 Exceptions : If source is not supplied
913 Caller : general
914 Status : Stable
915  
916 =cut
917  
918 sub source_constraint {
919 my ($self, $sources, $inline_variables) = @_;
920 my $constraint = "t.is_current = 1";
921 my $in_statement = $self->generate_in_constraint($sources, 't.source', SQL_VARCHAR, $inline_variables);
922 $constraint .= " and $in_statement";
923 return $constraint;
924 }
925  
926 =head2 count_all_by_source
927  
928 Arg [1] : String $source
929 listref of $source
930 The source of the transcript to retrieve. You can have as an argument a reference
931 to a list of sources
932 Example : $cnt = $transcript_adaptor->count_all_by_source('ensembl');
933 $cnt = $transcript_adaptor->count_all_by_source(['havana', 'vega']);
934 Description : Retrieves count of transcript objects from the database via its source or sources.
935 Returntype : integer
936 Caller : general
937 Status : Stable
938  
939 =cut
940  
941 sub count_all_by_source {
942 my ($self, $source) = @_;
943 return $self->generic_count($self->source_constraint($source));
944 }
945  
946 =head2 count_all_by_Slice
947  
948 Arg [1] : Bio::EnsEMBL::Slice $slice
949 The slice to count transcripts on.
950 Arg [2] : (optional) biotype(s) string or arrayref of strings
951 the biotype of the features to count.
952 Arg [1] : (optional) string $source
953 the source name of the features to count.
954 Example : $cnt = $transcript_adaptor->count_all_by_Slice();
955 Description: Method to count transcripts on a given slice, filtering by biotype and source
956 Returntype : integer
957 Exceptions : thrown if exon cannot be placed on transcript slice
958 Status : Stable
959 Caller : general
960 =cut
961  
962 sub count_all_by_Slice {
963 my ($self, $slice, $biotype, $source) = @_;
964  
965 my $constraint = 't.is_current = 1';
966 if (defined($source)) {
967 $constraint .= " and t.source = '$source'";
968 }
969 if (defined($biotype)) {
970 $constraint .= " and " . $self->biotype_constraint($biotype);
971 }
972  
973 return $self->count_by_Slice_constraint($slice, $constraint);
974 }
975  
976 =head2 fetch_all_by_biotype
977  
978 Arg [1] : String $biotype
979 listref of $biotypes
980 The biotype of the transcript to retrieve. You can have as an argument a reference
981 to a list of biotypes
982 Example : $gene = $transcript_adaptor->fetch_all_by_biotype('protein_coding');
983 $gene = $transcript_adaptor->fetch_all_by_biotypes(['protein_coding', 'sRNA', 'miRNA']);
984 Description: Retrieves an array reference of transcript objects from the database via its biotype or biotypes.
985 The transcript will be retrieved in its native coordinate system (i.e.
986 in the coordinate system it is stored in the database). It may
987 be converted to a different coordinate system through a call to
988 transform() or transfer(). If the gene or exon is not found
989 undef is returned instead.
990 Returntype : listref of Bio::EnsEMBL::Transcript
991 Exceptions : if we cant get the gene in given coord system
992 Caller : general
993 Status : Stable
994  
995 =cut
996  
997 sub fetch_all_by_biotype {
998 my ($self, $biotype) = @_;
999 my @transcripts = @{$self->generic_fetch($self->biotype_constraint($biotype))};
1000 return \@transcripts;
1001 }
1002  
1003 =head2 biotype_constraint
1004  
1005 Arg [1] : String $biotypes
1006 listref of $biotypes
1007 The biotype of the transcript to retrieve. You can have as an argument a reference
1008 to a list of biotypes
1009 Description: Used internally to generate a SQL constraint to restrict a transcript query by biotype
1010 Returntype : String
1011 Exceptions : If biotype is not supplied
1012 Caller : general
1013 Status : Stable
1014  
1015 =cut
1016  
1017 sub biotype_constraint {
1018 my ($self, $biotypes, $inline_variables) = @_;
1019 my $constraint = "t.is_current = 1";
1020 my $in_statement = $self->generate_in_constraint($biotypes, 't.biotype', SQL_VARCHAR, $inline_variables);
1021 $constraint .= " and $in_statement";
1022 return $constraint;
1023 }
1024  
1025 =head2 count_all_by_biotype
1026  
1027 Arg [1] : String $biotype
1028 listref of $biotypes
1029 The biotype of the transcript to retrieve. You can have as an argument a reference
1030 to a list of biotypes
1031 Example : $cnt = $transcript_adaptor->count_all_by_biotype('protein_coding');
1032 $cnt = $transcript_adaptor->count_all_by_biotypes(['protein_coding', 'sRNA', 'miRNA']);
1033 Description : Retrieves count of transcript objects from the database via its biotype or biotypes.
1034 Returntype : integer
1035 Caller : general
1036 Status : Stable
1037  
1038 =cut
1039  
1040 sub count_all_by_biotype {
1041 my ($self, $biotype) = @_;
1042 return $self->generic_count($self->biotype_constraint($biotype));
1043 }
1044  
1045 =head2 store
1046  
1047 Arg [1] : Bio::EnsEMBL::Transcript $transcript
1048 The transcript to be written to the database
1049 Arg [2] : Int $gene_dbID
1050 The identifier of the gene that this transcript is associated
1051 with
1052 Arg [3] : DEPRECATED (optional) Int $analysis_id
1053 The analysis_id to use when storing this gene. This is for
1054 backward compatibility only and used to fall back to the gene
1055 analysis_id if no analysis object is attached to the transcript
1056 (which you should do for new code).
1057 Arg [4] : prevent coordinate recalculation if you are persisting
1058 transcripts with this gene
1059 Example : $transID = $tr_adaptor->store($transcript, $gene->dbID);
1060 Description: Stores a transcript in the database and returns the new
1061 internal identifier for the stored transcript.
1062 Returntype : Int
1063 Exceptions : none
1064 Caller : general
1065 Status : Stable
1066  
1067 =cut
1068  
1069 sub store {
1070 my ( $self, $transcript, $gene_dbID, $analysis_id, $skip_recalculating_coordinates ) = @_;
1071  
1072 if ( !ref($transcript)
1073 || !$transcript->isa('Bio::EnsEMBL::Transcript') )
1074 {
1075 throw("$transcript is not a EnsEMBL transcript - not storing");
1076 }
1077  
1078 my $db = $self->db();
1079  
1080 if ( $transcript->is_stored($db) ) {
1081 return $transcript->dbID();
1082 }
1083  
1084 # Force lazy-loading of exons and ensure coords are correct.
1085 # If we have been told not to do this then skip doing this
1086 # and we assume the user knows what they are doing. You have been
1087 # warned
1088 if(! $skip_recalculating_coordinates) {
1089 $transcript->recalculate_coordinates();
1090 }
1091  
1092 my $is_current = ( defined( $transcript->is_current() )
1093 ? $transcript->is_current()
1094 : 1 );
1095  
1096 # store analysis
1097 my $analysis = $transcript->analysis();
1098 my $new_analysis_id;
1099  
1100 if ($analysis) {
1101 if ( $analysis->is_stored($db) ) {
1102 $new_analysis_id = $analysis->dbID;
1103 } else {
1104 $new_analysis_id = $db->get_AnalysisAdaptor->store($analysis);
1105 }
1106 } else {
1107 throw("Need an analysis_id to store the Transcript.");
1108 }
1109  
1110 #
1111 # Store exons - this needs to be done before the possible transfer
1112 # of the transcript to another slice (in _prestore()). Transfering
1113 # results in copies being made of the exons and we need to preserve
1114 # the object identity of the exons so that they are not stored twice
1115 # by different transcripts.
1116 #
1117 my $exons = $transcript->get_all_Exons();
1118 my $exonAdaptor = $db->get_ExonAdaptor();
1119 foreach my $exon ( @{$exons} ) {
1120 $exonAdaptor->store($exon);
1121 }
1122  
1123 my $original_translation = $transcript->translation();
1124 my $original = $transcript;
1125 my $seq_region_id;
1126 ( $transcript, $seq_region_id ) = $self->_pre_store($transcript);
1127  
1128 # First store the transcript without a display xref. The display xref
1129 # needs to be set after xrefs are stored which needs to happen after
1130 # transcript is stored.
1131  
1132 #
1133 # Store transcript
1134 #
1135  
1136 # my $store_transcript_sql =
1137 # sprintf "INSERT INTO transcript SET gene_id = ?, analysis_id = ?, seq_region_id = ?, seq_region_start = ?, seq_region_end = ?, seq_region_strand = ?,%s biotype = ?, description = ?, is_current = ?, canonical_translation_id = ?", ($self->schema_version > 74)?" source = ?,":'';
1138  
1139 my @columns = qw(
1140 gene_id
1141 analysis_id
1142 seq_region_id
1143 seq_region_start
1144 seq_region_end
1145 seq_region_strand
1146 );
1147  
1148 push @columns, 'source' if ($self->schema_version > 74);
1149  
1150 push @columns, qw(
1151 biotype
1152 description
1153 is_current
1154 canonical_translation_id
1155 );
1156  
1157 my @canned_columns;
1158 my @canned_values;
1159  
1160 if ( defined( $transcript->stable_id() ) ) {
1161 push @columns, 'stable_id', 'version';
1162  
1163 my $created = $self->db->dbc->from_seconds_to_date($transcript->created_date());
1164 my $modified = $self->db->dbc->from_seconds_to_date($transcript->modified_date());
1165  
1166 if ($created) {
1167 push @canned_columns, 'created_date';
1168 push @canned_values, $created;
1169 }
1170 if ($modified) {
1171 push @canned_columns, 'modified_date';
1172 push @canned_values, $modified;
1173 }
1174
1175 }
1176  
1177 my $columns = join(', ', @columns, @canned_columns);
1178 my $values = join(', ', ('?') x @columns, @canned_values);
1179 my $store_transcript_sql = qq(
1180 INSERT INTO transcript ( $columns ) VALUES ( $values )
1181 );
1182  
1183 my $tst = $self->prepare($store_transcript_sql);
1184 my $i = 0;
1185 $tst->bind_param( ++$i, $gene_dbID, SQL_INTEGER );
1186 $tst->bind_param( ++$i, $new_analysis_id, SQL_INTEGER );
1187 $tst->bind_param( ++$i, $seq_region_id, SQL_INTEGER );
1188 $tst->bind_param( ++$i, $transcript->start(), SQL_INTEGER );
1189 $tst->bind_param( ++$i, $transcript->end(), SQL_INTEGER );
1190 $tst->bind_param( ++$i, $transcript->strand(), SQL_TINYINT );
1191  
1192 $self->schema_version > 74 and
1193 $tst->bind_param( ++$i, $transcript->source(), SQL_VARCHAR );
1194  
1195 $tst->bind_param( ++$i, $transcript->get_Biotype->name, SQL_VARCHAR );
1196 $tst->bind_param( ++$i, $transcript->description(), SQL_LONGVARCHAR );
1197 $tst->bind_param( ++$i, $is_current, SQL_TINYINT );
1198  
1199 # If the transcript has a translation, this is updated later:
1200 $tst->bind_param( ++$i, undef, SQL_INTEGER );
1201  
1202 if ( defined( $transcript->stable_id() ) ) {
1203  
1204 $tst->bind_param( ++$i, $transcript->stable_id(), SQL_VARCHAR );
1205 $tst->bind_param( ++$i, $transcript->version(), SQL_INTEGER );
1206 }
1207  
1208 $tst->execute();
1209 $tst->finish();
1210  
1211 my $transc_dbID = $self->last_insert_id('transcript_id', undef, 'transcript');
1212  
1213 #
1214 # Store translation
1215 #
1216  
1217 my $alt_translations =
1218 $transcript->get_all_alternative_translations();
1219 my $translation = $transcript->translation();
1220  
1221 if ( defined($translation) ) {
1222 # Make sure that the start and end exon are set correctly.
1223 my $start_exon = $translation->start_Exon();
1224 my $end_exon = $translation->end_Exon();
1225  
1226 if ( !defined($start_exon) ) {
1227 throw("Translation does not define a start exon.");
1228 }
1229  
1230 if ( !defined($end_exon) ) {
1231 throw("Translation does not defined an end exon.");
1232 }
1233  
1234 # If the dbID is not set, this means the exon must have been a
1235 # different object in memory than the the exons of the transcript.
1236 # Try to find the matching exon in all of the exons we just stored.
1237 if ( !defined( $start_exon->dbID() ) ) {
1238 my $key = $start_exon->hashkey();
1239 ($start_exon) = grep { $_->hashkey() eq $key } @$exons;
1240  
1241 if ( defined($start_exon) ) {
1242 $translation->start_Exon($start_exon);
1243 } else {
1244 throw( "Translation's start_Exon does not appear "
1245 . "to be one of the exons in "
1246 . "its associated Transcript" );
1247 }
1248 }
1249  
1250 if ( !defined( $end_exon->dbID() ) ) {
1251 my $key = $end_exon->hashkey();
1252 ($end_exon) = grep { $_->hashkey() eq $key } @$exons;
1253  
1254 if ( defined($end_exon) ) {
1255 $translation->end_Exon($end_exon);
1256 } else {
1257 throw( "Translation's end_Exon does not appear "
1258 . "to be one of the exons in "
1259 . "its associated Transcript." );
1260 }
1261 }
1262  
1263 my $old_dbid = $translation->dbID();
1264 $db->get_TranslationAdaptor()->store( $translation, $transc_dbID );
1265  
1266 # Need to update the canonical_translation_id for this transcript.
1267  
1268 my $sth = $self->prepare(
1269 q(
1270 UPDATE transcript
1271 SET canonical_translation_id = ?
1272 WHERE transcript_id = ?)
1273 );
1274  
1275 $sth->bind_param( 1, $translation->dbID(), SQL_INTEGER );
1276 $sth->bind_param( 2, $transc_dbID, SQL_INTEGER );
1277  
1278 $sth->execute();
1279  
1280 # Set values of the original translation, we may have copied it when
1281 # we transformed the transcript.
1282 $original_translation->dbID( $translation->dbID() );
1283 $original_translation->adaptor( $translation->adaptor() );
1284 } ## end if ( defined($translation...))
1285  
1286 #
1287 # Store the alternative translations, if there are any.
1288 #
1289  
1290 if ( defined($alt_translations)
1291 && scalar( @{$alt_translations} ) > 0 )
1292 {
1293 foreach my $alt_translation ( @{$alt_translations} ) {
1294 my $start_exon = $alt_translation->start_Exon();
1295 my $end_exon = $alt_translation->end_Exon();
1296  
1297 if ( !defined($start_exon) ) {
1298 throw("Translation does not define a start exon.");
1299 } elsif ( !defined($end_exon) ) {
1300 throw("Translation does not defined an end exon.");
1301 }
1302  
1303 if ( !defined( $start_exon->dbID() ) ) {
1304 my $key = $start_exon->hashkey();
1305 ($start_exon) = grep { $_->hashkey() eq $key } @{$exons};
1306  
1307 if ( defined($start_exon) ) {
1308 $alt_translation->start_Exon($start_exon);
1309 } else {
1310 throw( "Translation's start_Exon does not appear "
1311 . "to be one of the exon in"
1312 . "its associated Transcript" );
1313 }
1314 }
1315 if ( !defined( $end_exon->dbID() ) ) {
1316 my $key = $end_exon->hashkey();
1317 ($end_exon) = grep { $_->hashkey() eq $key } @$exons;
1318  
1319 if ( defined($end_exon) ) {
1320 $alt_translation->end_Exon($end_exon);
1321 } else {
1322 throw( "Translation's end_Exon does not appear "
1323 . "to be one of the exons in "
1324 . "its associated Transcript." );
1325 }
1326 }
1327  
1328 $db->get_TranslationAdaptor()
1329 ->store( $alt_translation, $transc_dbID );
1330 } ## end foreach my $alt_translation...
1331 } ## end if ( defined($alt_translations...))
1332  
1333 #
1334 # Store the xrefs/object xref mapping.
1335 #
1336 my $dbEntryAdaptor = $db->get_DBEntryAdaptor();
1337  
1338 foreach my $dbe ( @{ $transcript->get_all_DBEntries() } ) {
1339 $dbEntryAdaptor->store( $dbe, $transc_dbID, "Transcript", 1 );
1340 }
1341  
1342 #
1343 # Update transcript to point to display xref if it is set.
1344 #
1345 if ( my $dxref = $transcript->display_xref() ) {
1346 my $dxref_id;
1347  
1348 if ( $dxref->is_stored($db) ) {
1349 $dxref_id = $dxref->dbID();
1350 } else {
1351 $dxref_id = $dbEntryAdaptor->exists($dxref);
1352 }
1353  
1354 if ( defined($dxref_id) ) {
1355 my $sth =
1356 $self->prepare( "UPDATE transcript "
1357 . "SET display_xref_id = ? "
1358 . "WHERE transcript_id = ?" );
1359 $sth->bind_param( 1, $dxref_id, SQL_INTEGER );
1360 $sth->bind_param( 2, $transc_dbID, SQL_INTEGER );
1361 $sth->execute();
1362 $dxref->dbID($dxref_id);
1363 $dxref->adaptor($dbEntryAdaptor);
1364 $sth->finish();
1365 } else {
1366 warning(sprintf(
1367 "Display_xref %s:%s is not stored in database.\n"
1368 . "Not storing relationship to this transcript.",
1369 $dxref->dbname(), $dxref->display_id() ) );
1370 $dxref->dbID(undef);
1371 $dxref->adaptor(undef);
1372 }
1373 } ## end if ( my $dxref = $transcript...)
1374  
1375 #
1376 # Link transcript to exons in exon_transcript table
1377 #
1378 my $etst = $self->prepare(
1379 "INSERT INTO exon_transcript (`exon_id`,`transcript_id`,`rank`) "
1380 . "VALUES (?,?,?)" );
1381 my $rank = 1;
1382 foreach my $exon ( @{ $transcript->get_all_Exons } ) {
1383 $etst->bind_param( 1, $exon->dbID, SQL_INTEGER );
1384 $etst->bind_param( 2, $transc_dbID, SQL_INTEGER );
1385 $etst->bind_param( 3, $rank, SQL_INTEGER );
1386 $etst->execute();
1387 $rank++;
1388 }
1389  
1390 $etst->finish();
1391  
1392 # Now the supporting evidence
1393 my $tsf_adaptor = $db->get_TranscriptSupportingFeatureAdaptor();
1394 $tsf_adaptor->store( $transc_dbID,
1395 $transcript->get_all_supporting_features() );
1396  
1397 # store transcript attributes if there are any
1398 my $attr_adaptor = $db->get_AttributeAdaptor();
1399  
1400 $attr_adaptor->store_on_Transcript( $transc_dbID,
1401 $transcript->get_all_Attributes() );
1402  
1403 # Check if transcript is canonical
1404 if ($transcript->is_canonical()) {
1405 my $gene_adaptor = $self->db()->get_GeneAdaptor();
1406 my $gene = $gene_adaptor->fetch_by_dbID($gene_dbID);
1407 $transcript->dbID($transc_dbID);
1408 $gene->canonical_transcript($transcript);
1409 $gene_adaptor->update($gene);
1410 }
1411  
1412 # store the IntronSupportingEvidence features
1413 my $ise_adaptor = $db->get_IntronSupportingEvidenceAdaptor();
1414 my $intron_supporting_evidence = $transcript->get_all_IntronSupportingEvidence();
1415 foreach my $ise (@{$intron_supporting_evidence}) {
1416 $ise_adaptor->store($ise);
1417 $ise_adaptor->store_transcript_linkage($ise, $transcript, $transc_dbID);
1418 }
1419  
1420 # Update the original transcript object - not the transfered copy that
1421 # we might have created.
1422 $original->dbID($transc_dbID);
1423 $original->adaptor($self);
1424  
1425 return $transc_dbID;
1426 } ## end sub store
1427  
1428  
1429 =head2 get_Interpro_by_transid
1430  
1431 Arg [1] : String $trans_stable_id
1432 The stable if of the transcript to obtain
1433 Example : @i = $tr_adaptor->get_Interpro_by_transid($trans->stable_id());
1434 Description: Gets interpro accession numbers by transcript stable id.
1435 A hack really - we should have a much more structured
1436 system than this.
1437 Returntype : listref of strings (Interpro_acc:description)
1438 Exceptions : none
1439 Caller : domainview? , GeneView
1440 Status : Stable
1441  
1442 =cut
1443  
1444 sub get_Interpro_by_transid {
1445 my ($self,$trans_stable_id) = @_;
1446  
1447 my $straight_join = $self->_can_straight_join ? 'STRAIGHT_JOIN' : '';
1448 my $sth = $self->prepare(qq(
1449 SELECT ${straight_join} i.interpro_ac, x.description
1450 FROM transcript t,
1451 translation tl,
1452 protein_feature pf,
1453 interpro i,
1454 xref x
1455 WHERE t.stable_id = ?
1456 AND tl.transcript_id = t.transcript_id
1457 AND tl.translation_id = pf.translation_id
1458 AND i.id = pf.hit_name
1459 AND i.interpro_ac = x.dbprimary_acc
1460 AND t.is_current = 1
1461 ));
1462  
1463 $sth->bind_param(1, $trans_stable_id, SQL_VARCHAR);
1464 $sth->execute();
1465  
1466 my @out;
1467 my %h;
1468 while( (my $arr = $sth->fetchrow_arrayref()) ) {
1469 if( $h{$arr->[0]} ) { next; }
1470 $h{$arr->[0]}=1;
1471 my $string = $arr->[0] .":".$arr->[1];
1472 push(@out,$string);
1473 }
1474  
1475 return \@out;
1476 }
1477  
1478 =head2 is_Transcript_canonical()
1479  
1480 Arg [1] : Bio::EnsEMBL::Transcript $transcript
1481 The transcript to query with
1482 Example : $tr_adaptor->is_Transcript_canonical($transcript);
1483 Description : Returns a boolean if the given transcript is considered
1484 canonical with respect to a gene
1485 Returntype : Boolean
1486 Exceptions : None
1487 Caller : Bio::EnsEMBL::Transcript
1488 Status : Beta
1489
1490  
1491 =cut
1492  
1493 sub is_Transcript_canonical {
1494 my ($self, $transcript) = @_;
1495 return $self->dbc()->sql_helper()->execute_single_result(
1496 -SQL => 'select count(*) from gene where canonical_transcript_id =?',
1497 -PARAMS => [$transcript->dbID()]
1498 );
1499 }
1500  
1501  
1502 =head2 remove
1503  
1504 Arg [1] : Bio::EnsEMBL::Transcript $transcript
1505 The transcript to remove from the database
1506 Arg [2] : Boolean, update Gene coordinates after removal. WARNING: this does not alter any other copies of the
1507 gene currently in memory. Other copies will retain their original coordinates. Either refetch them
1508 or go directly through Gene->remove_Transcript first, then remove the Transcript here.
1509 Example : $tr_adaptor->remove($transcript);
1510 Description: Removes a transcript completely from the database, and all
1511 associated information.
1512 This method is usually called by the GeneAdaptor::remove method
1513 because this method will not preform the removal of genes
1514 which are associated with this transcript. Do not call this
1515 method directly unless you know there are no genes associated
1516 with the transcript!
1517 Returntype : none
1518 Exceptions : throw on incorrect arguments
1519 warning if transcript is not in this database
1520 Caller : GeneAdaptor::remove
1521 Status : Stable
1522  
1523 =cut
1524  
1525 sub remove {
1526 my $self = shift;
1527 my $transcript = shift;
1528 my $update = shift;
1529 if(!ref($transcript) || !$transcript->isa('Bio::EnsEMBL::Transcript')) {
1530 throw("Bio::EnsEMBL::Transcript argument expected");
1531 }
1532  
1533 # sanity check: make sure nobody tries to slip past a prediction transcript
1534 # which inherits from transcript but actually uses different tables
1535 if($transcript->isa('Bio::EnsEMBL::PredictionTranscript')) {
1536 throw("TranscriptAdaptor can only remove Transcripts " .
1537 "not PredictionTranscripts");
1538 }
1539  
1540 if ( !$transcript->is_stored($self->db()) ) {
1541 warning("Cannot remove transcript ". $transcript->dbID .". Is not stored ".
1542 "in this database.");
1543 return;
1544 }
1545  
1546 # remove the supporting features of this transcript
1547  
1548 my $prot_adp = $self->db->get_ProteinAlignFeatureAdaptor;
1549 my $dna_adp = $self->db->get_DnaAlignFeatureAdaptor;
1550  
1551 my $sfsth = $self->prepare("SELECT feature_type, feature_id " .
1552 "FROM transcript_supporting_feature " .
1553 "WHERE transcript_id = ?");
1554  
1555 $sfsth->bind_param(1, $transcript->dbID, SQL_INTEGER);
1556 $sfsth->execute();
1557  
1558 # statements to check for shared align_features
1559 my $sth1 = $self->prepare("SELECT count(*) FROM supporting_feature " .
1560 "WHERE feature_type = ? AND feature_id = ?");
1561 my $sth2 = $self->prepare("SELECT count(*) " .
1562 "FROM transcript_supporting_feature " .
1563 "WHERE feature_type = ? AND feature_id = ?");
1564  
1565 SUPPORTING_FEATURE:
1566 while(my ($type, $feature_id) = $sfsth->fetchrow()){
1567
1568 # only remove align_feature if this is the last reference to it
1569 $sth1->bind_param(1, $type, SQL_VARCHAR);
1570 $sth1->bind_param(2, $feature_id, SQL_INTEGER);
1571 $sth1->execute;
1572 $sth2->bind_param(1, $type, SQL_VARCHAR);
1573 $sth2->bind_param(2, $feature_id, SQL_INTEGER);
1574 $sth2->execute;
1575 my ($count1) = $sth1->fetchrow;
1576 my ($count2) = $sth2->fetchrow;
1577 if ($count1 + $count2 > 1) {
1578 #warn "transcript: shared feature, not removing $type|$feature_id\n";
1579 next SUPPORTING_FEATURE;
1580 }
1581
1582 #warn "transcript: removing $type|$feature_id\n";
1583
1584 if($type eq 'protein_align_feature'){
1585 my $f = $prot_adp->fetch_by_dbID($feature_id);
1586 $prot_adp->remove($f);
1587 }
1588 elsif($type eq 'dna_align_feature'){
1589 my $f = $dna_adp->fetch_by_dbID($feature_id);
1590 $dna_adp->remove($f);
1591 }
1592 else {
1593 warning("Unknown supporting feature type $type. Not removing feature.");
1594 }
1595 }
1596 $sfsth->finish();
1597 $sth1->finish();
1598 $sth2->finish();
1599  
1600 # delete the association to supporting features
1601  
1602 $sfsth = $self->prepare("DELETE FROM transcript_supporting_feature WHERE transcript_id = ?");
1603 $sfsth->bind_param(1, $transcript->dbID, SQL_INTEGER);
1604 $sfsth->execute();
1605 $sfsth->finish();
1606
1607 # delete the associated IntronSupportingEvidence and if the ISE had no more
1608 # linked transcripts remove it
1609 my $ise_adaptor = $self->db->get_IntronSupportingEvidenceAdaptor();
1610 foreach my $ise (@{$transcript->get_all_IntronSupportingEvidence()}) {
1611 $ise_adaptor->remove_transcript_linkage($ise, $transcript);
1612 if(! $ise->has_linked_transcripts()) {
1613 $ise_adaptor->remove($ise);
1614 }
1615 }
1616  
1617 # remove all xref linkages to this transcript
1618  
1619 my $dbeAdaptor = $self->db->get_DBEntryAdaptor();
1620 foreach my $dbe (@{$transcript->get_all_DBEntries}) {
1621 $dbeAdaptor->remove_from_object($dbe, $transcript, 'Transcript');
1622 }
1623  
1624 # remove the attributes associated with this transcript
1625 my $attrib_adp = $self->db->get_AttributeAdaptor;
1626 $attrib_adp->remove_from_Transcript($transcript);
1627  
1628 # remove the translation associated with this transcript
1629  
1630 my $translationAdaptor = $self->db->get_TranslationAdaptor();
1631 if( defined($transcript->translation()) ) {
1632 $translationAdaptor->remove( $transcript->translation );
1633 }
1634  
1635 # remove exon associations to this transcript
1636  
1637 my $exonAdaptor = $self->db->get_ExonAdaptor();
1638 foreach my $exon ( @{$transcript->get_all_Exons()} ) {
1639 # get the number of transcript references to this exon
1640 # only remove the exon if this is the last transcript to
1641 # reference it
1642  
1643 my $sth = $self->prepare( "SELECT count(*)
1644 FROM exon_transcript
1645 WHERE exon_id = ?" );
1646 $sth->bind_param(1, $exon->dbID, SQL_INTEGER);
1647 $sth->execute();
1648 my ($count) = $sth->fetchrow_array();
1649 $sth->finish();
1650  
1651 if($count == 1){
1652 $exonAdaptor->remove( $exon );
1653 }
1654 }
1655  
1656 my $sth = $self->prepare( "DELETE FROM exon_transcript
1657 WHERE transcript_id = ?" );
1658 $sth->bind_param(1, $transcript->dbID, SQL_INTEGER);
1659 $sth->execute();
1660 $sth->finish();
1661  
1662 my $gene = $transcript->get_Gene;
1663  
1664 $sth = $self->prepare( "DELETE FROM transcript
1665 WHERE transcript_id = ?" );
1666 $sth->bind_param(1, $transcript->dbID, SQL_INTEGER);
1667 $sth->execute();
1668 $sth->finish();
1669  
1670 if ($update) {
1671 $gene->remove_Transcript($transcript);
1672 }
1673  
1674 $transcript->dbID(undef);
1675 $transcript->adaptor(undef);
1676  
1677 return;
1678 }
1679  
1680  
1681 =head2 update
1682  
1683 Arg [1] : Bio::EnsEMBL::Transcript $transcript
1684 The transcript to update
1685 Example : $tr_adaptor->update($transcript);
1686 Description: Updates a transcript in the database.
1687 Returntype : None
1688 Exceptions : thrown if the $transcript is not a Bio::EnsEMBL::Transcript.
1689 warn if the method is called on a transcript that does not exist
1690 in the database.
1691 Should warn if trying to update the number of attached exons, but
1692 this is a far more complex process and is not yet implemented.
1693 Caller : general
1694 Status : Stable
1695  
1696 =cut
1697  
1698 sub update {
1699 my ( $self, $transcript ) = @_;
1700  
1701 if ( !defined($transcript)
1702 || !ref($transcript)
1703 || !$transcript->isa('Bio::EnsEMBL::Transcript') )
1704 {
1705 throw("Must update a transcript object, not a $transcript");
1706 }
1707  
1708 my $update_transcript_sql =
1709 sprintf "UPDATE transcript SET stable_id = ?, analysis_id = ?, display_xref_id = ?, description = ?,%s biotype = ?, is_current = ?, canonical_translation_id = ?, version = ? WHERE transcript_id = ?", ($self->schema_version > 74)?" source = ?,":'';
1710  
1711 my $display_xref = $transcript->display_xref();
1712 my $display_xref_id;
1713  
1714 if ( defined($display_xref) && $display_xref->dbID() ) {
1715 $display_xref_id = $display_xref->dbID();
1716 } else {
1717 $display_xref_id = undef;
1718 }
1719  
1720 my $sth = $self->prepare($update_transcript_sql);
1721 my $i = 0;
1722 $sth->bind_param( ++$i, $transcript->stable_id(), SQL_VARCHAR );
1723 $sth->bind_param( ++$i, $transcript->analysis()->dbID(), SQL_INTEGER );
1724 $sth->bind_param( ++$i, $display_xref_id, SQL_INTEGER );
1725 $sth->bind_param( ++$i, $transcript->description(), SQL_LONGVARCHAR );
1726  
1727 $self->schema_version > 74 and
1728 $sth->bind_param( ++$i, $transcript->source(), SQL_VARCHAR );
1729  
1730 $sth->bind_param( ++$i, $transcript->get_Biotype->name, SQL_VARCHAR );
1731 $sth->bind_param( ++$i, $transcript->is_current(), SQL_TINYINT );
1732 $sth->bind_param( ++$i, (
1733 defined( $transcript->translation() )
1734 ? $transcript->translation()->dbID()
1735 : undef ),
1736 SQL_INTEGER );
1737 $sth->bind_param( ++$i, $transcript->version(), SQL_INTEGER );
1738 $sth->bind_param( ++$i, $transcript->dbID(), SQL_INTEGER );
1739  
1740 $sth->execute();
1741  
1742 # Check if transcript is canonical
1743 if ($transcript->is_canonical()) {
1744 my $gene = $transcript->get_Gene();
1745 my $gene_adaptor = $self->db()->get_GeneAdaptor();
1746 $gene->canonical_transcript($transcript);
1747 $gene_adaptor->update($gene);
1748 }
1749  
1750 } ## end sub update
1751  
1752  
1753 =head2 list_dbIDs
1754  
1755 Example : @transcript_ids = @{ $t_adaptor->list_dbIDs };
1756 Description: Gets a list of internal ids for all transcripts in the db.
1757 Arg[1] : <optional> int. not 0 for the ids to be sorted by the seq_region. Returntype : Listref of Ints
1758 Exceptions : none
1759 Caller : general
1760 Status : Stable
1761  
1762 =cut
1763  
1764 sub list_dbIDs {
1765 my ($self, $ordered) = @_;
1766  
1767 return $self->_list_dbIDs("transcript",undef, $ordered);
1768 }
1769  
1770  
1771 =head2 list_stable_ids
1772  
1773 Example : @stable_trans_ids = @{ $transcript_adaptor->list_stable_ids };
1774 Description: Gets a list of stable ids for all transcripts in the current
1775 database.
1776 Returntype : Listref of Strings
1777 Exceptions : none
1778 Caller : general
1779 Status : Stable
1780  
1781 =cut
1782  
1783 sub list_stable_ids {
1784 my ($self) = @_;
1785  
1786 return $self->_list_dbIDs("transcript", "stable_id");
1787 }
1788  
1789  
1790 #_objs_from_sth
1791  
1792 # Arg [1] : StatementHandle $sth
1793 # Arg [2] : Bio::EnsEMBL::AssemblyMapper $mapper
1794 # Arg [3] : Bio::EnsEMBL::Slice $dest_slice
1795 # Description: PROTECTED implementation of abstract superclass method.
1796 # Responsible for the creation of Transcripts.
1797 # Returntype : Listref of Bio::EnsEMBL::Transcripts in target coord system
1798 # Exceptions : none
1799 # Caller : internal
1800 # Status : Stable
1801  
1802 sub _objs_from_sth {
1803 my ($self, $sth, $mapper, $dest_slice) = @_;
1804  
1805 #
1806 # This code is ugly because an attempt has been made to remove as many
1807 # function calls as possible for speed purposes. Thus many caches and
1808 # a fair bit of gymnastics is used.
1809 #
1810  
1811 my $sa = $self->db()->get_SliceAdaptor();
1812 my $aa = $self->db()->get_AnalysisAdaptor();
1813 my $dbEntryAdaptor = $self->db()->get_DBEntryAdaptor();
1814  
1815 my @transcripts;
1816 my %analysis_hash;
1817 my %slice_hash;
1818 my %sr_name_hash;
1819 my %sr_cs_hash;
1820  
1821 my (
1822 $transcript_id, $seq_region_id, $seq_region_start,
1823 $seq_region_end, $seq_region_strand, $analysis_id,
1824 $gene_id, $is_current, $stable_id,
1825 $version, $created_date, $modified_date,
1826 $description, $biotype,
1827 $external_db, $external_status, $external_db_name,
1828 $display_xref_id, $xref_display_label, $xref_primary_acc,
1829 $xref_version, $xref_description, $xref_info_type,
1830 $xref_info_text, $external_release, $source
1831 );
1832  
1833 if ($self->schema_version() > 74) {
1834 $sth->bind_columns(
1835 \(
1836 $transcript_id, $seq_region_id, $seq_region_start,
1837 $seq_region_end, $seq_region_strand, $analysis_id,
1838 $gene_id, $is_current, $stable_id,
1839 $version, $created_date, $modified_date,
1840 $description, $biotype,
1841 $external_db, $external_status, $external_db_name,
1842 $display_xref_id, $xref_display_label, $xref_primary_acc,
1843 $xref_version, $xref_description, $xref_info_type,
1844 $xref_info_text, $external_release, $source
1845 ) );
1846 } else {
1847 $sth->bind_columns(
1848 \(
1849 $transcript_id, $seq_region_id, $seq_region_start,
1850 $seq_region_end, $seq_region_strand, $analysis_id,
1851 $gene_id, $is_current, $stable_id,
1852 $version, $created_date, $modified_date,
1853 $description, $biotype,
1854 $external_db, $external_status, $external_db_name,
1855 $display_xref_id, $xref_display_label, $xref_primary_acc,
1856 $xref_version, $xref_description, $xref_info_type,
1857 $xref_info_text, $external_release
1858 ) );
1859 }
1860  
1861 my $dest_slice_start;
1862 my $dest_slice_end;
1863 my $dest_slice_strand;
1864 my $dest_slice_length;
1865 my $dest_slice_cs;
1866 my $dest_slice_sr_name;
1867 my $dest_slice_sr_id;
1868 my $asma;
1869  
1870 if ($dest_slice) {
1871 $dest_slice_start = $dest_slice->start();
1872 $dest_slice_end = $dest_slice->end();
1873 $dest_slice_strand = $dest_slice->strand();
1874 $dest_slice_length = $dest_slice->length();
1875 $dest_slice_cs = $dest_slice->coord_system();
1876 $dest_slice_sr_name = $dest_slice->seq_region_name();
1877 $dest_slice_sr_id = $dest_slice->get_seq_region_id();
1878 $asma = $self->db->get_AssemblyMapperAdaptor();
1879 }
1880  
1881 FEATURE: while($sth->fetch()) {
1882  
1883 #get the analysis object
1884 my $analysis = $analysis_hash{$analysis_id} ||= $aa->fetch_by_dbID($analysis_id);
1885 $analysis_hash{$analysis_id} = $analysis;
1886  
1887 #need to get the internal_seq_region, if present
1888 $seq_region_id = $self->get_seq_region_id_internal($seq_region_id);
1889 my $slice = $slice_hash{"ID:".$seq_region_id};
1890  
1891 if (!$slice) {
1892 $slice = $sa->fetch_by_seq_region_id($seq_region_id);
1893 $slice_hash{"ID:".$seq_region_id} = $slice;
1894 $sr_name_hash{$seq_region_id} = $slice->seq_region_name();
1895 $sr_cs_hash{$seq_region_id} = $slice->coord_system();
1896 }
1897  
1898 #obtain a mapper if none was defined, but a dest_seq_region was
1899 if(!$mapper && $dest_slice && !$dest_slice_cs->equals($slice->coord_system)) {
1900 $mapper = $asma->fetch_by_CoordSystems($dest_slice_cs, $slice->coord_system);
1901 }
1902  
1903 my $sr_name = $sr_name_hash{$seq_region_id};
1904 my $sr_cs = $sr_cs_hash{$seq_region_id};
1905  
1906 #
1907 # remap the feature coordinates to another coord system
1908 # if a mapper was provided
1909 #
1910  
1911 if ($mapper) {
1912  
1913 if (defined $dest_slice && $mapper->isa('Bio::EnsEMBL::ChainedAssemblyMapper') ) {
1914 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
1915 $mapper->map($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs, 1, $dest_slice);
1916  
1917 } else {
1918 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
1919 $mapper->fastmap($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs);
1920 }
1921  
1922 #skip features that map to gaps or coord system boundaries
1923 next FEATURE if (!defined($seq_region_id));
1924  
1925 #get a slice in the coord system we just mapped to
1926 $slice = $slice_hash{"ID:".$seq_region_id} ||= $sa->fetch_by_seq_region_id($seq_region_id);
1927 }
1928  
1929 #
1930 # If a destination slice was provided convert the coords.
1931 #
1932 if (defined($dest_slice)) {
1933 my $seq_region_len = $dest_slice->seq_region_length();
1934  
1935 if ( $dest_slice_strand == 1 ) {
1936 $seq_region_start = $seq_region_start - $dest_slice_start + 1;
1937 $seq_region_end = $seq_region_end - $dest_slice_start + 1;
1938  
1939 if ( $dest_slice->is_circular ) {
1940 # Handle circular chromosomes.
1941  
1942 if ( $seq_region_start > $seq_region_end ) {
1943 # Looking at a feature overlapping the chromosome origin.
1944  
1945 if ( $seq_region_end > $dest_slice_start ) {
1946 # Looking at the region in the beginning of the chromosome
1947 $seq_region_start -= $seq_region_len;
1948 }
1949 if ( $seq_region_end < 0 ) {
1950 $seq_region_end += $seq_region_len;
1951 }
1952 } else {
1953 if ($dest_slice_start > $dest_slice_end && $seq_region_end < 0) {
1954 # Looking at the region overlapping the chromosome
1955 # origin and a feature which is at the beginning of the
1956 # chromosome.
1957 $seq_region_start += $seq_region_len;
1958 $seq_region_end += $seq_region_len;
1959 }
1960 }
1961 }
1962 } else {
1963  
1964 my $start = $dest_slice_end - $seq_region_end + 1;
1965 my $end = $dest_slice_end - $seq_region_start + 1;
1966  
1967 if ($dest_slice->is_circular()) {
1968  
1969 if ($dest_slice_start > $dest_slice_end) {
1970 # slice spans origin or replication
1971  
1972 if ($seq_region_start >= $dest_slice_start) {
1973 $end += $seq_region_len;
1974 $start += $seq_region_len if $seq_region_end > $dest_slice_start;
1975  
1976 } elsif ($seq_region_start <= $dest_slice_end) {
1977 # do nothing
1978 } elsif ($seq_region_end >= $dest_slice_start) {
1979 $start += $seq_region_len;
1980 $end += $seq_region_len;
1981  
1982 } elsif ($seq_region_end <= $dest_slice_end) {
1983 $end += $seq_region_len if $end < 0;
1984  
1985 } elsif ($seq_region_start > $seq_region_end) {
1986 $end += $seq_region_len;
1987 }
1988  
1989 } else {
1990  
1991 if ($seq_region_start <= $dest_slice_end and $seq_region_end >= $dest_slice_start) {
1992 # do nothing
1993 } elsif ($seq_region_start > $seq_region_end) {
1994 if ($seq_region_start <= $dest_slice_end) {
1995 $start -= $seq_region_len;
1996 } elsif ($seq_region_end >= $dest_slice_start) {
1997 $end += $seq_region_len;
1998 }
1999 }
2000 }
2001 }
2002  
2003 $seq_region_start = $start;
2004 $seq_region_end = $end;
2005 $seq_region_strand *= -1;
2006  
2007 } ## end else [ if ( $dest_slice_strand...)]
2008  
2009 # Throw away features off the end of the requested slice or on
2010 # different seq_region.
2011 if ($seq_region_end < 1
2012 || $seq_region_start > $dest_slice_length
2013 || ($dest_slice_sr_id != $seq_region_id)) {
2014 next FEATURE;
2015 }
2016 $slice = $dest_slice;
2017 }
2018  
2019 my $display_xref;
2020  
2021 if ($display_xref_id) {
2022 $display_xref = Bio::EnsEMBL::DBEntry->new_fast( {
2023 'dbID' => $display_xref_id,
2024 'adaptor' => $dbEntryAdaptor,
2025 'display_id' => $xref_display_label,
2026 'primary_id' => $xref_primary_acc,
2027 'version' => $xref_version,
2028 'description' => $xref_description,
2029 'release' => $external_release,
2030 'dbname' => $external_db,
2031 'db_display_name' => $external_db_name,
2032 'info_type' => $xref_info_type,
2033 'info_text' => $xref_info_text
2034 });
2035 $display_xref->status($external_status);
2036 }
2037  
2038 # Finally, create the new Transcript.
2039 my $params =
2040 {
2041 'analysis' => $analysis,
2042 'biotype' => $biotype,
2043 'start' => $seq_region_start,
2044 'end' => $seq_region_end,
2045 'strand' => $seq_region_strand,
2046 'adaptor' => $self,
2047 'slice' => $slice,
2048 'dbID' => $transcript_id,
2049 'stable_id' => $stable_id,
2050 'version' => $version,
2051 'created_date' => $created_date || undef,
2052 'modified_date' => $modified_date || undef,
2053 'description' => $description,
2054 'external_name' => $xref_display_label,
2055  
2056 'external_status' => $external_status,
2057 'external_display_name' => $external_db_name,
2058 'external_db' => $external_db,
2059 'display_xref' => $display_xref,
2060 'is_current' => $is_current,
2061 'edits_enabled' => 1
2062 };
2063
2064 $self->schema_version > 74 and $params->{'source'} = $source;
2065 push( @transcripts,
2066 $self->_create_feature_fast(
2067 'Bio::EnsEMBL::Transcript',$params) );
2068  
2069 }
2070  
2071 return \@transcripts;
2072 }
2073  
2074  
2075 =head2 fetch_all_by_exon_supporting_evidence
2076  
2077 Arg [1] : String $hit_name
2078 Name of supporting feature
2079 Arg [2] : String $feature_type
2080 one of "dna_align_feature" or "protein_align_feature"
2081 Arg [3] : (optional) Bio::Ensembl::Analysis
2082 Example : $tr = $tr_adaptor->fetch_all_by_exon_supporting_evidence
2083 ('XYZ', 'dna_align_feature');
2084 Description: Gets all the transcripts with exons which have a specified hit
2085 on a particular type of feature. Optionally filter by analysis.
2086 Returntype : Listref of Bio::EnsEMBL::Transcript objects
2087 Exceptions : If feature_type is not of correct type.
2088 Caller : general
2089 Status : Stable
2090  
2091 =cut
2092  
2093 sub fetch_all_by_exon_supporting_evidence {
2094 my ($self, $hit_name, $feature_type, $analysis) = @_;
2095  
2096 if($feature_type !~ /(dna)|(protein)_align_feature/) {
2097 throw("feature type must be dna_align_feature or protein_align_feature");
2098 }
2099  
2100 my $anal_from = "";
2101 $anal_from = ", analysis a " if ($analysis);
2102 my $anal_where = "";
2103 $anal_where = "AND a.analysis_id = f.analysis_id AND a.analysis_id=? "
2104 if ($analysis);
2105  
2106 my $sql = qq(
2107 SELECT DISTINCT(t.transcript_id)
2108 FROM transcript t,
2109 exon_transcript et,
2110 supporting_feature sf,
2111 $feature_type f
2112 $anal_from
2113 WHERE t.transcript_id = et.transcript_id
2114 AND t.is_current = 1
2115 AND et.exon_id = sf.exon_id
2116 AND sf.feature_id = f.${feature_type}_id
2117 AND sf.feature_type = ?
2118 AND f.hit_name=?
2119 $anal_where
2120 );
2121  
2122 my $sth = $self->prepare($sql);
2123  
2124 $sth->bind_param(1, $feature_type, SQL_VARCHAR);
2125 $sth->bind_param(2, $hit_name, SQL_VARCHAR);
2126 $sth->bind_param(3, $analysis->dbID(), SQL_INTEGER) if ($analysis);
2127  
2128 $sth->execute();
2129  
2130 my @transcripts;
2131  
2132 while( my $id = $sth->fetchrow_array ) {
2133 my $transcript = $self->fetch_by_dbID( $id );
2134 push(@transcripts, $transcript) if $transcript;
2135 }
2136  
2137 return \@transcripts;
2138 }
2139  
2140  
2141 =head2 fetch_all_by_transcript_supporting_evidence
2142  
2143 Arg [1] : String $hit_name
2144 Name of supporting feature
2145 Arg [2] : String $feature_type
2146 one of "dna_align_feature" or "protein_align_feature"
2147 Arg [3] : (optional) Bio::Ensembl::Analysis
2148 Example : $transcripts = $transcript_adaptor->fetch_all_by_transcript_supporting_evidence('XYZ', 'dna_align_feature');
2149 Description: Gets all the transcripts with evidence from a specified hit_name on a particular type of feature, stored in the
2150 transcript_supporting_feature table. Optionally filter by analysis. For hits stored in the supporting_feature
2151 table (linked to exons) use fetch_all_by_exon_supporting_evidence instead.
2152 Returntype : Listref of Bio::EnsEMBL::Transcript objects
2153 Exceptions : If feature_type is not of correct type.
2154 Caller : general
2155 Status : Stable
2156  
2157 =cut
2158  
2159 sub fetch_all_by_transcript_supporting_evidence {
2160
2161 my ($self, $hit_name, $feature_type, $analysis) = @_;
2162  
2163 if($feature_type !~ /(dna)|(protein)_align_feature/) {
2164 throw("feature type must be dna_align_feature or protein_align_feature");
2165 }
2166  
2167 my $anal_from = "";
2168 $anal_from = ", analysis a " if ($analysis);
2169 my $anal_where = "";
2170 $anal_where = "AND a.analysis_id = f.analysis_id AND a.analysis_id=? "
2171 if ($analysis);
2172  
2173 my $sql = qq(
2174 SELECT DISTINCT(t.transcript_id)
2175 FROM transcript t,
2176 transcript_supporting_feature sf,
2177 $feature_type f
2178 $anal_from
2179 WHERE t.transcript_id = sf.transcript_id
2180 AND t.is_current = 1
2181 AND sf.feature_id = f.${feature_type}_id
2182 AND sf.feature_type = ?
2183 AND f.hit_name=?
2184 $anal_where
2185 );
2186  
2187 my $sth = $self->prepare($sql);
2188  
2189 $sth->bind_param(1, $feature_type, SQL_VARCHAR);
2190 $sth->bind_param(2, $hit_name, SQL_VARCHAR);
2191 $sth->bind_param(3, $analysis->dbID(), SQL_INTEGER) if ($analysis);
2192  
2193 $sth->execute();
2194  
2195 my @transcripts;
2196  
2197 while( my $id = $sth->fetchrow_array ) {
2198 my $transcript = $self->fetch_by_dbID( $id );
2199 push(@transcripts, $transcript) if $transcript;
2200 }
2201  
2202 return \@transcripts;
2203 }
2204  
2205 sub _final_clause {
2206 return ' ORDER BY t.transcript_id'
2207 }
2208  
2209 sub update_canonical_attribute {
2210 my ($self, $transcript_id, $old_transcript_id) = @_;
2211  
2212 # Get canonical attribute id
2213 my $db = $self->db();
2214 my $attr_adaptor = $db->get_AttributeAdaptor();
2215 my $canonical_attrib_id = @{$attr_adaptor->fetch_by_code('is_canonical')}[0];
2216 throw("No attrib_type_id found for 'is_canonical' attribute in attrib_type table.") if (!defined($canonical_attrib_id));
2217  
2218 # Check if new canonical transcript attribute exists
2219 my $sth = $self->prepare("SELECT value FROM transcript_attrib WHERE transcript_id=? AND attrib_type_id=?");
2220 $sth->execute($transcript_id, $canonical_attrib_id);
2221 if (my ($exists) = $sth->fetchrow_array()) {
2222 $sth->finish();
2223  
2224 $sth = $self->prepare("UPDATE transcript_attrib SET value=? WHERE transcript_id=? AND attrib_type_id=?");
2225 $sth->execute('1', $transcript_id, $canonical_attrib_id);
2226 } else {
2227 $sth->finish();
2228  
2229 $sth = $self->prepare("INSERT INTO transcript_attrib (transcript_id, attrib_type_id, value) values(?,?,?)");
2230 $sth->execute($transcript_id, $canonical_attrib_id, '1');
2231 }
2232 $sth->finish();
2233  
2234 # Delete old canonical transcript attribute
2235 if (defined($old_transcript_id) && $old_transcript_id ne $transcript_id) {
2236 $sth = $self->prepare("DELETE FROM transcript_attrib WHERE transcript_id=? AND attrib_type_id=?");
2237 $sth->execute($old_transcript_id, $canonical_attrib_id);
2238 $sth->finish();
2239 }
2240 }
2241  
2242 1;