EnsEMBL/Utils/TranscriptSelector.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 212 CC 20 SCAR 39.3
CC
20
Cov
0%
select_canonical_transcript_for_Gene
90.0
encode_transcript
56.0
check_Ens_trans_against_CCDS
30.0
CRAP 50.8
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::Utils::TranscriptSelector - Finds canonical transcripts
34  
35 =head1 SYNOPSIS
36  
37 my $selector = Bio::EnsEMBL::Utils::TranscriptSelector->new($ccds_dba);
38 my $canonical_transcript = $selector->select_canonical_transcript_for_Gene($gene);
39  
40 =head1 DESCRIPTION
41  
42 The decision process for choosing a canonical transcript of a given Gene is
43 an involved process. This package converts transcript attributes into
44 numeric values, sorts the values and returns the favourite transcript.
45
46 The canonical order of precedence is as follows:
47 longest translation of transcript present in CCDS that is reference sequence
48 longest translation of protein-coding transcript
49 longest translation of transcript marked nonsense-mediated-decay
50 longest translation of any other transcript (premature stop codon translations have an effective length of 0)
51 longest non-coding transcript
52 first stable ID in alphabetical order
53
54 The last condition is to give consistent behaviour when everything is else is equal.
55 It selects the "older" stable ID, thus preventing new IDs supplanting old ones that
56 remain correct.
57 =cut
58  
59 package Bio::EnsEMBL::Utils::TranscriptSelector;
60 $Bio::EnsEMBL::Utils::TranscriptSelector::VERSION = '114.0.0';
61 use strict;
62 use warnings;
63 use Bio::EnsEMBL::Utils::Exception;
64  
65 =head2 new
66  
67 Arg [1] : Optional - CCDS database adaptor - needed for species with CCDS only
68 Arg [2] : Optional - Boolean verbose flag. Turn on to fill your logs
69 Description: Constructor
70 Returntype : Bio::EnsEMBL::Utils::TranscriptSelector
71  
72 =cut
73  
74 sub new {
75 my $class = shift;
76 my $self = {
77 'ccds_dba' => shift,
78 'verbose' => shift,
79 };
80 bless $self, $class;
81 if (not defined ($self->{'ccds_dba'}) ) { warning ("Running without CCDS DB");}
82 return $self;
83 }
84  
85 =head2 select_canonical_transcript_for_Gene
86  
87 Arg 1 : Bio::EnsEMBL::Gene
88 Example : $canonical_transcript = $selector->select_canonical_transcript_for_Gene
89 Description: Sorts the Transcripts of this Gene into order of suitability,
90 and returns the favourite Transcript.
91 Returntype : Bio::EnsEMBL::Transcript
92 Exceptions :
93  
94 =cut
95  
96 sub select_canonical_transcript_for_Gene {
97 my $self = shift;
98 my $gene = shift;
99  
100 if (!$gene) {throw ('Cannot select canonical transcript without a gene.');}
101  
102 my $transcript_array = $gene->get_all_Transcripts;
103 my @transcripts;
104 if ($transcript_array) {
105 @transcripts = @$transcript_array;
106 } else {
107 warning('No transcripts attached to gene '.$gene->stable_id);
108 return;
109 }
110 my @encoded; # array of encoded transcripts
111  
112 # if we have a single transcript return it by default
113 if (scalar (@transcripts) == 1) {
114 print 'Choosing the only existing transcript: ' . $transcripts[0]->stable_id . "\n" if ($self->{'verbose'});
115 return $transcripts[0];
116 }
117
118 foreach my $transcript (@transcripts) {
119 my $encoded_transcript = $self->encode_transcript($transcript);
120 push(@encoded, $encoded_transcript );
121 if ($self->{'verbose'}) {
122 printf "%s encoded to: [%s,%s,%s,%s,%s,%s,%s]\n",$transcript->stable_id, @$encoded_transcript;
123 }
124 }
125  
126 my $sorted_ids = $self->sort_into_canonical_order(\@encoded);
127 if ($self->{'verbose'}) {
128 print "Sorted order: \n";
129 foreach my $dbID (@$sorted_ids) {
130 print $dbID."\n";
131 }
132 }
133 my $canonical_dbID = $sorted_ids->[0];
134
135 foreach my $transcript (@transcripts) {
136 if ($transcript->dbID == $canonical_dbID) {
137 if ($self->{'verbose'}) {print 'Chosen transcript: '.$transcript->stable_id."\n";}
138 return $transcript;
139 }
140 }
141 throw ('Run out of transcripts without finding selected canonical dbID.')
142 }
143  
144  
145 # Constants for doing numerical sorting of transcripts based upon which ones we prefer. Lowest is best.
146 my %source_priority = ('ccds' => 1,
147 'merged' => 2,
148 'other' => 3);
149 my %biotype_priority = ('protein_coding' => 1,
150 'nonsense_mediated_decay' => 2,
151 'non_stop_decay' => 2,
152 'polymorphic_pseudogene' => 2,
153 'protein_coding_LoF' => 2,
154 'other' => 3,
155 );
156  
157 =head2 encode_transcript
158  
159 Arg 1 : Transcript
160 Description: Converts a transcript into a list of encoded values for sorting
161 Priorities are defined immediately above
162 Unimportant biotypes and sources are classed as 'other'
163 Returntype : Listref of encoded attributes
164 =cut
165  
166 sub encode_transcript {
167 my $self = shift;
168 my $transcript = shift;
169  
170 my $type;
171 if ( $self->{'ccds_dba'} && $transcript->slice->is_reference()
172 && $self->check_Ens_trans_against_CCDS($transcript) ) {
173 $type = 'ccds';
174 } elsif ($transcript->analysis->logic_name eq 'ensembl_havana_transcript') {
175 $type = 'merged';
176 } else {
177 $type = 'other';
178 }
179
180 my $translation = $transcript->translate;
181 my $translation_length = 0;
182 if ($translation) {
183 $translation_length = $translation->length;
184 # Translations containing premature stops are undesirable.
185 if ($translation->seq =~ /\*/) {$translation_length = 0;}
186 }
187 # Zero-length/non-existent translations are ok. We sort by coding and non-coding first
188 my $translates = 0;
189 if ($translation_length != 0) {$translates = 1;}
190
191 my $transcript_length = $transcript->length;
192
193 my $biotype;
194 if ( $transcript->biotype() ne 'protein_coding'
195 && $transcript->biotype() ne 'nonsense_mediated_decay'
196 && $transcript->biotype() ne 'non_stop_decay'
197 && $transcript->biotype() ne 'polymorphic_pseudogene'
198 && $transcript->biotype() ne 'protein_coding_LoF' ) {
199 $biotype = 'other';
200 } else { $biotype = $transcript->biotype(); }
201  
202 return [$transcript->dbID,
203 $translates,
204 $source_priority{$type},
205 $biotype_priority{$biotype},
206 $translation_length,
207 $transcript_length,
208 $transcript->stable_id];
209 }
210  
211  
212 =head2 sort_into_canonical_order
213  
214 Arg 1 : 2D array reference of numerically encoded values
215 0 1 2 3 4 5 6
216 ( [transcript dbID, translates, source , biotype, translation length, transcript length, stable ID],
217 ...
218 )
219 Description: see Schwartzian transform for method in the following madness:
220 sort the 6-column array by the last 5 columns, then map the first elements
221 into a list of dbIDs, now in canonical order.
222 Returntype : Listref of ensembl dbIDs
223 Caller : select_canonical_transcript_for_Gene
224 =cut
225
226 sub sort_into_canonical_order {
227 my $self = shift;
228 my $encoded_list_ref = shift;
229
230 my @sorted_ids = map { $_->[0] }
231 sort {
232 # [0] contains ID
233 $b->[1] <=> $a->[1] || # translates
234 $a->[2] <=> $b->[2] || # source
235 $a->[3] <=> $b->[3] || # biotype
236 $b->[4] <=> $a->[4] || # translation length (largest is best, $a and $b reversed)
237 $b->[5] <=> $a->[5] || # transcript length "
238 $a->[6] cmp $b->[6] # stable id. All other things being equal, 'lowest' transcript ID wins
239 } @{$encoded_list_ref};
240
241 return \@sorted_ids;
242 }
243  
244 =head2 check_Ens_trans_against_CCDS
245  
246 Arg 1 : Transcript
247 Description: Attempts to find a matching transcript in CCDS by comparing Exon
248 composition. Returns true if one is found, or silently ends.
249 Returntype : Boolean
250 Caller : encode_transcript
251 =cut
252  
253 sub check_Ens_trans_against_CCDS {
254 my ( $self ,$transcript ) = @_;
255
256 my @translateable_exons = @{ $transcript->get_all_translateable_Exons };
257
258 my $seq_region_name = $transcript->slice->seq_region_name;
259 my $seq_region_start = $transcript->seq_region_start;
260 my $seq_region_end = $transcript->seq_region_end;
261 my $ccds_dba = $self->{'ccds_dba'};
262
263 my $ext_slice = $ccds_dba->get_SliceAdaptor->fetch_by_region(
264 'toplevel',
265 $seq_region_name,
266 $seq_region_start,
267 $seq_region_end );
268  
269 EXT_GENE: foreach my $ext_gene ( @{ $ext_slice->get_all_Genes } ) {
270 EXT_TRANS: foreach my $ext_trans ( @{ $ext_gene->get_all_Transcripts } ) {
271 my @ext_exons = @{ $ext_trans->get_all_Exons };
272
273 if ( scalar(@translateable_exons) == scalar(@ext_exons) ) {
274 for ( my $i = 0 ; $i < scalar(@translateable_exons) ; $i++ ) {
275 if ( $translateable_exons[$i]->coding_region_start($transcript)
276 != $ext_exons[$i]->seq_region_start
277 || $translateable_exons[$i]->strand
278 != $ext_exons[$i]->strand
279 || $translateable_exons[$i]->coding_region_end($transcript)
280 != $ext_exons[$i]->seq_region_end
281 ) {
282 next EXT_TRANS;
283 }
284 }
285 print "Ensembl transcript "
286 . $transcript->display_id
287 . " found match "
288 . $ext_gene->display_id
289 . " in CCDS DB.\n" if $self->{'verbose'};
290 if ($ext_gene->stable_id !~ /^CCDS/) {
291 throw (sprintf("Database does not appear to contain CCDS IDs. Possible configuration problem with CCDS source. Found ID %s", $ext_gene->stable_id()));
292 }
293 return 1;
294 }
295 } # end foreach EXT_TRANS
296 } # end foreach EXT_GENE
297 } ## end sub check_Ens_trans_against_CCDS
298  
299 1;