-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMitoanno.pm
414 lines (380 loc) · 15.7 KB
/
Mitoanno.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/perl
#############################################
##Author: Jiang Li
##email: riverlee2008@gmail.com
##Creat Time: Mon Dec 3 10:51:37 CST 2012
##Vanderbilt Center for Quantitative Sciences
##############################################
package Mitoanno;
use strict;
use warnings;
use Convert;
use FindBin;
#This class handles the annotation for a given position in mitochondria genome
#The mitochnodrial annotation comes from NCBI in genbank format, stored at ./Resoures
#For hg19, http://www.ncbi.nlm.nih.gov/nuccore/NC_001807.4?report=genbank
#for rCRS, http://www.ncbi.nlm.nih.gov/nuccore/251831106
#Constructor
sub new{
my ($class,$var) = @_;
my $self={
_build=>"hg19", #only hg19 and rCRS are accpeted
_pos2gene=>{}, #position refer to its located gene name
_gene2pos=>{}, #gene information, includeing start, end,strand, gene name
_hg19TorCRS=>undef,#an Convert object, which is used to access reference allele for a given position
_omimpathogenicfile=>$FindBin::RealBin."/Resources/OMIMpathogenic.txt", #file contain pathogenic from omim, position are in rCRS
_omimpathogenic=>{} #An hash refernce store information from _omimpathogenicfile
};
if(defined($var) ){
if($var eq 'rCRS' || $var eq 'hg19'){
$self->{_build}=$var;
}else{
warn "Only 'hg19' and 'rCRS' are accepted for mitochondria genome, will use 'hg19' as default\n";
}
}
$self->{_gbfile}=$FindBin::RealBin."/Resources/".$self->{_build}."_annotation.gb";
bless $self,$class;
return $self;
}
sub genes{
my ($self) = @_;
$self->_initial unless (keys %{$self->{_gene2pos}});
return $self->{_gene2pos};
}
sub build{
my ($self,$var) = @_;
if(defined($var)){
if($var eq 'rCRS' || $var eq 'hg19'){
$self->{_build}=$var;
$self->{_gbfile}=$FindBin::RealBin."/Resources/".$self->{_build}."_annotation.gb";
$self->_initial;
}else{
warn "Only 'hg19' and 'rCRS' are accepted for mitochondria genome, will use 'hg19' as default\n";
}
}
return $self->{_build};
}
sub gbfile{
my ($self,$var) = @_;
if (defined($var)){
$self->{_gbfile}=$var;
$self->_initial;
}
return $self->{_gbfile};
}
#Given a position, annotation this position
sub annotation{
my($self,$var,$ref,$alt) = @_;
$self->_initial unless (keys %{$self->{_gene2pos}});
if(!defined($var) || $var!~/^\d+$/ || $var<1 || $var>16571){
warn "Please provide the right genome position of mitochondria\n";
}else{
#return value
my ($gene,$genedetail,$aminochange,$aminochangetype);
if(exists($self->{_pos2gene}->{$var})){
my @g;
my @gd;
my @aminochange;
my @aminochangetype;
foreach my $g (@{$self->{_pos2gene}->{$var}}){
push @g,$g;
my $tmp=$self->{_gene2pos}->{$g}->{_gene_synonym}.":".
$self->{_gene2pos}->{$g}->{_type}.":".
$self->{_gene2pos}->{$g}->{_start}."-".
$self->{_gene2pos}->{$g}->{_end}.":(".
$self->{_gene2pos}->{$g}->{_strand}.")";
push @gd,$tmp;
if(defined($ref) && defined($alt) && $self->{_gene2pos}->{$g}->{_type} eq "mRNA"){
#Get the codon
my $refcodon="";
my $altcodon="";
my $pos1;
my $pos2;
if($self->{_gene2pos}->{$g}->{_strand} eq "+"){
my $mode = ($var-$self->{_gene2pos}->{$g}->{_start})%3;
if($mode==0){
$pos1=$var+1;
$pos2=$var+2;
if($self->{_build} eq "hg19"){
$refcodon=$ref.$self->{_hg19TorCRS}->hg19ref($pos1).$self->{_hg19TorCRS}->hg19ref($pos2);
$altcodon=$alt.$self->{_hg19TorCRS}->hg19ref($pos1).$self->{_hg19TorCRS}->hg19ref($pos2);
}else{
$refcodon=$ref.$self->{_hg19TorCRS}->rCRSref($pos1).$self->{_hg19TorCRS}->rCRSref($pos2);
$altcodon=$alt.$self->{_hg19TorCRS}->rCRSref($pos1).$self->{_hg19TorCRS}->rCRSref($pos2);
}
}elsif($mode ==1){
$pos1=$var-1;
$pos2=$var+1;
if($self->{_build} eq "hg19"){
$refcodon=$self->{_hg19TorCRS}->hg19ref($pos1).$ref.$self->{_hg19TorCRS}->hg19ref($pos2);
$altcodon=$self->{_hg19TorCRS}->hg19ref($pos1).$alt.$self->{_hg19TorCRS}->hg19ref($pos2);
}else{
$refcodon=$self->{_hg19TorCRS}->rCRSref($pos1).$ref.$self->{_hg19TorCRS}->rCRSref($pos2);
$altcodon=$self->{_hg19TorCRS}->rCRSref($pos1).$alt.$self->{_hg19TorCRS}->rCRSref($pos2);
}
}else{
$pos1=$var-2;
$pos2=$var-1;
if($self->{_build} eq "hg19"){
$refcodon=$self->{_hg19TorCRS}->hg19ref($pos1).$self->{_hg19TorCRS}->hg19ref($pos2).$ref;
$altcodon=$self->{_hg19TorCRS}->hg19ref($pos1).$self->{_hg19TorCRS}->hg19ref($pos2).$alt;
}else{
$refcodon=$self->{_hg19TorCRS}->rCRSref($pos1).$self->{_hg19TorCRS}->rCRSref($pos2).$ref;
$altcodon=$self->{_hg19TorCRS}->rCRSref($pos1).$self->{_hg19TorCRS}->rCRSref($pos2).$alt;
}
}
}else{
my $mode = ($self->{_gene2pos}->{$g}->{_end}-$var)%3;
if($mode==0){
$pos1=$var-1;
$pos2=$var-2;
if($self->{_build} eq "hg19"){
$refcodon=$ref.$self->{_hg19TorCRS}->hg19ref($pos1).$self->{_hg19TorCRS}->hg19ref($pos2);
$altcodon=$alt.$self->{_hg19TorCRS}->hg19ref($pos1).$self->{_hg19TorCRS}->hg19ref($pos2);
}else{
$refcodon=$ref.$self->{_hg19TorCRS}->rCRSref($pos1).$self->{_hg19TorCRS}->rCRSref($pos2);
$altcodon=$alt.$self->{_hg19TorCRS}->rCRSref($pos1).$self->{_hg19TorCRS}->rCRSref($pos2);
}
}elsif($mode ==1){
$pos1=$var+1;
$pos2=$var-1;
if($self->{_build} eq "hg19"){
$refcodon=$self->{_hg19TorCRS}->hg19ref($pos1).$ref.$self->{_hg19TorCRS}->hg19ref($pos2);
$altcodon=$self->{_hg19TorCRS}->hg19ref($pos1).$alt.$self->{_hg19TorCRS}->hg19ref($pos2);
}else{
$refcodon=$self->{_hg19TorCRS}->rCRSref($pos1).$ref.$self->{_hg19TorCRS}->rCRSref($pos2);
$altcodon=$self->{_hg19TorCRS}->rCRSref($pos1).$alt.$self->{_hg19TorCRS}->rCRSref($pos2);
}
}else{
$pos1=$var+2;
$pos2=$var+1;
if($self->{_build} eq "hg19"){
$refcodon=$self->{_hg19TorCRS}->hg19ref($pos1).$self->{_hg19TorCRS}->hg19ref($pos2).$ref;
$altcodon=$self->{_hg19TorCRS}->hg19ref($pos1).$self->{_hg19TorCRS}->hg19ref($pos2).$alt;
}else{
$refcodon=$self->{_hg19TorCRS}->rCRSref($pos1).$self->{_hg19TorCRS}->rCRSref($pos2).$ref;
$altcodon=$self->{_hg19TorCRS}->rCRSref($pos1).$self->{_hg19TorCRS}->rCRSref($pos2).$alt;
}
}
$refcodon=~tr/ATCG/TAGC/;
$altcodon=~tr/ATCG/TAGC/;
}
my $refamino=codon2aa($refcodon);
my $altamino=codon2aa($altcodon);
my ($min,undef,$max) = sort {$a<=>$b} ($var,$pos1,$pos2);
push @aminochange,$min."-".$max.":".$refcodon."->".$altcodon.":".$refamino."->".$altamino;
my $changetype="unknown";
if($refamino ne "unknown" && $altamino ne "unknown"){
if($refamino eq $altamino){
$changetype = "synonymous";
}elsif($refamino eq "STOP"){
$changetype = "stoploss";
}elsif($altamino eq "STOP"){
$changetype ="stopgain";
}else{
$changetype="non-synonymous";
}
}
push @aminochangetype,$changetype;
}else{
push @aminochange,"";
push @aminochangetype,"";
}
}
return (join("|",@g),join("|",@gd),join("|",@aminochangetype),join("|",@aminochange));
}else{
return("","","","");
}
}
}
#Give a position, output it's pathogenic information
sub pathogenic{
my ($self,$pos) = @_;
if($self->{_build} eq 'hg19'){
$pos=$self->{_hg19TorCRS}->hg19TorCRS($pos);
if(!defined($pos)){
return("","",""); #variants,diseases,links
}
}
if(exists($self->{_omimpathogenic}->{$pos})){
my @variants;
my @diseases;
my @links;
foreach my $hashref(@{$self->{_omimpathogenic}->{$pos}}){
push @variants,$hashref->{'variant'};
push @diseases,$hashref->{'disease'};
push @links,$hashref->{'link'};
}
return (join("|",@variants),join("|",@diseases),join("|",@links));
}else{
return("","",""); #variants,diseases,links
}
}
#if _pos2gene has not been inititalized, load the gbfile
#For genbank file, we only fetch features of tRNA, rRNA and CDS(mRNA)
sub _initial{
my ($self) = @_;
#remove the previous annotation
$self->{_gene2pos}={};
$self->{_pos2gene}={};
open(IN,$self->{_gbfile}) or die "The genbank annotation is not exists \n";
my $feature="";
my $flag=0;
while(<IN>){
if(/^FEATURES/){
$flag=1;
next;
}
last if(/^ORIGIN/); #The following lines display the sequences
$feature.=$_ if ($flag);
}
close IN;
#Begin parse the $feature;
# print $feature;
my @features;
while($feature=~/^ {5}\S.*\n(^ {21}\S.*\n)*/gm){
push @features,$&;
}
foreach my $f (@features){
if($f=~/^ {5}(\S+).*\n(^ {21}\S.*\n)*/){
if($1 eq "tRNA" || $1 eq "rRNA" || $1 eq "CDS"){
# print $f,"=========\n";
my ($start,$end,$strand,$type,$geneid,$gene,$gene_synonym)=("","","+",$1,"","","");
$type="mRNA" if ($type eq "CDS");
$strand="-" if($f=~/complement\(/);
if($f=~/^ {5}\S.*?(\d+)\.\.(\d+)/){
($start,$end)=($1,$2);
}
if($f=~/\/db_xref="GeneID:(\d+)"\n/){
$geneid=$1;
}
if($f=~/\/gene="(.*?)"\n/){
$gene=$1;
}
if($f=~/\/gene_synonym="(.*?)"\n/){
$gene_synonym=$1;
}
#print join "\t", ($start,$end,$strand,$type,$geneid,$gene,$gene_synonym,"\n");
$self->{_gene2pos}->{$gene}={_start=>$start,
_end=>$end,
_strand=>$strand,
_type=>$type,
_geneid=>$geneid,
_gene_synonym=>$gene_synonym
};
#Some of the gene has overlap,
for(my $i=$start;$i<=$end;$i++){
push @{$self->{_pos2gene}->{$i}},$gene;
}
}
}
}
$self->{_hg19TorCRS}=Convert->new();
#Initial _omimpathogenic hash ref
open(IN,$self->{_omimpathogenicfile}) or die $!;
<IN>; #skip the header
while(<IN>){
s/\r|\n//g;
my ($pos,$pathogenic_variant,$gene,$omim,$disease) = split "\t";
my $link="http://omim.org/entry/";
if ($omim=~/(\d+)\.(\d+)/){
$link.=$1."#".$2;
}
push @{$self->{_omimpathogenic}->{$pos}}, {'variant'=>$pathogenic_variant,
'gene'=>$gene,
'omim'=>$omim,
'disease'=>$disease,
'link'=>$link};
}
}
sub codon2aa {
my($codon) = @_;
$codon = uc $codon;
my(%genetic_code) = (
'TCA' => 'S', # Serine
'TCC' => 'S', # Serine
'TCG' => 'S', # Serine
'TCT' => 'S', # Serine
'TTC' => 'F', # Phenylalanine
'TTT' => 'F', # Phenylalanine
'TTA' => 'L', # Leucine
'TTG' => 'L', # Leucine
'TAC' => 'Y', # Tyrosine
'TAT' => 'Y', # Tyrosine
'TAA' => 'STOP', # Stop
'TAG' => 'STOP', # Stop
'TGC' => 'C', # Cysteine
'TGT' => 'C', # Cysteine
'TGA' => 'STOP', # Stop
'TGG' => 'W', # Tryptophan
'CTA' => 'L', # Leucine
'CTC' => 'L', # Leucine
'CTG' => 'L', # Leucine
'CTT' => 'L', # Leucine
'CCA' => 'P', # Proline
'CCC' => 'P', # Proline
'CCG' => 'P', # Proline
'CCT' => 'P', # Proline
'CAC' => 'H', # Histidine
'CAT' => 'H', # Histidine
'CAA' => 'Q', # Glutamine
'CAG' => 'Q', # Glutamine
'CGA' => 'R', # Arginine
'CGC' => 'R', # Arginine
'CGG' => 'R', # Arginine
'CGT' => 'R', # Arginine
'ATA' => 'I', # Isoleucine
'ATC' => 'I', # Isoleucine
'ATT' => 'I', # Isoleucine
'ATG' => 'M', # Methionine
'ACA' => 'T', # Threonine
'ACC' => 'T', # Threonine
'ACG' => 'T', # Threonine
'ACT' => 'T', # Threonine
'AAC' => 'N', # Asparagine
'AAT' => 'N', # Asparagine
'AAA' => 'K', # Lysine
'AAG' => 'K', # Lysine
'AGC' => 'S', # Serine
'AGT' => 'S', # Serine
'AGA' => 'R', # Arginine
'AGG' => 'R', # Arginine
'GTA' => 'V', # Valine
'GTC' => 'V', # Valine
'GTG' => 'V', # Valine
'GTT' => 'V', # Valine
'GCA' => 'A', # Alanine
'GCC' => 'A', # Alanine
'GCG' => 'A', # Alanine
'GCT' => 'A', # Alanine
'GAC' => 'D', # Aspartic Acid
'GAT' => 'D', # Aspartic Acid
'GAA' => 'E', # Glutamic Acid
'GAG' => 'E', # Glutamic Acid
'GGA' => 'G', # Glycine
'GGC' => 'G', # Glycine
'GGG' => 'G', # Glycine
'GGT' => 'G', # Glycine
);
if(exists $genetic_code{$codon}) {
return $genetic_code{$codon};
}else{
print STDERR "Bad codon \"$codon\"!!\n";
#exit;
return "unknown";
}
}
1;
=test
#Test Code
package test;
use Mitoanno;
my $m=Mitoanno->new();
my $ref="T";
my $alt="C";
foreach my $pos (14670..14700){
print join "\t",($pos,$m->annotation($pos,$ref,$alt));
print "\n";
}
1;
=cut