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