-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gene_obj.pm
executable file
·5572 lines (3709 loc) · 143 KB
/
Gene_obj.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
package main;
our $DEBUG;
package Gene_obj;
use strict;
use Nuc_translator;
#use Gene_ontology;
use Longest_orf;
use Storable qw (store retrieve freeze thaw dclone);
use warnings;
use Data::Dumper;
use Carp qw (croak cluck confess);
use URI::Escape;
=head1 NAME
package Gene_obj
=cut
=head1 DESCRIPTION
Gene_obj(s) encapsulate the elements of both gene structure and gene function. The gene structure is stored in a hierarchical fashion as follows:
Gene =========================================================
Exon ========= ========= ========= ========
CDS ====== ========= ======
where a Gene is a container for Exon(s), and each Exon is a container for a CDS, and an Exon can contain a single CDS component. An Exon lacking a CDS exon is an untranslated exon or UTR exon. The region of an Exon which extends beyond the CDS is also considered a UTR.
There are several ways to instantiate gene objects. A simple example is described:
Exon and CDS component coordinates can be assigned as hashes.
ie.
my %mrna = ( 100 => 200,
300 => 500 );
my %CDS = ( 150=>200,
300=>450);
my $sequence = "GACTACATTTAATAGGGCCC"; #string representing the genomic sequence
my $gene = new Gene_obj();
$gene->{com_name} = "hypothetical protein";
$gene->populate_gene_obj(\%CDS, \%mRNA, \$sequence);
print $gene->toString();
Alternatively, the individual components of genes (Exons and CDSs) can be instantiated separately and used to build the Gene from the ground up (See packages mRNA_exon_obj and CDS_exon_obj following this Gene_obj documentation).
my $cds_exon = new CDS_exon_obj (150, 200);
my $mRNA_exon = new mRNA_exon_obj (100, 200);
$mRNA_exon->set_CDS_exon_obj($cds_exon);
my $gene_obj = new Gene_obj ();
$gene_obj->{gene_name} = "hypothetical gene";
$gene_obj->{com_name} = "hypothetical protein";
$gene_obj->add_mRNA_exon_obj($mRNA_exon);
$gene_obj->refine_gene_object();
$gene_obj->create_all_sequence_types (\$sequence); #ref to genomic sequence string.
print $gene_obj->toString();
The API below describes useful functions for navigating and manipulating the Gene object along with all of its attributes.
=cut
=over 4
=item new()
B<Description:> Constructor for Gene_obj
B<Parameters:> none
B<Returns:> $gene_obj
The Gene_obj contains several attributes which can be manipulated directly (or by get/set methods if they exist). These attributes include:
asmbl_id # identifier for the genomic contig for which this gene is anchored.
TU_feat_name #feat_names are TIGR temporary identifiers.
Model_feat_name # temp TIGR identifier for gene models
locus #identifier for a gene (TU) ie. T2P3.5
pub_locus #another identifier for a gene (TU) ie. At2g00010
model_pub_locus #identifier for a gene model (model) ie. At2g00010.1
model_locus #analagous to locus, but for model rather than gene (TU)
alt_locus #alternative locus
gene_name # name for gene
com_name # name for gene product
comment #internal comment
pub_comment #comment related to gene
ec_num # enzyme commission number
gene_sym # gene symbol
is_5prime_partial # 0|1 missing start codon.
is_3prime_partial # 0|1 missing stop codon.
is_pseudogene # 0|1
curated_com_name # 0|1
curated_gene_structure # 0|1
## Other attributes set internally Access-only, do not set directly.
gene_length # length of gene span (int).
mid_pt # holds midpoint of gene-span
strand # [+-]
protein_seq # holds protein sequence
protein_seq_length
CDS_sequence #holds CDS sequence (translated to protein); based on CDS_exon coordinates
CDS_seq_length
cDNA_sequence #holds cDNA sequence; based on mRNA exon coordinates.
cDNA_seq_length
gene_sequence #holds unspliced transcript
gene_sequence_length #length of unspliced transcript
gene_type # "protein-coding", #default type for gene object. Could be changed to "rRNA|snoRNA|snRNA|tRNA" to accommodate other gene or feature types.
num_additional_isoforms # int
=back
=cut
sub new {
shift;
my $self = { asmbl_id => 0, #genomic contig ID
locus => undef, #text
pub_locus => undef, #text ie. At2g00010
model_pub_locus =>undef, #text ie. At2g00010.1
model_locus => undef, #text ie. F12G15.1
alt_locus => undef, #text
gene_name => undef, #text
com_name => undef, #text
comment => undef,
curated_com_name => 0,
curated_gene_structure => 0,
pub_comment => undef, #text
ec_num => undef, #text (enzyme commission number)
gene_type => "protein-coding", #default type for gene object. Could be changed to "rRNA|snoRNA|snRNA|tRNA" to accomodate other gene or feature types.
gene_sym => undef, #text (gene symbol)
mRNA_coords => 0, #assigned to anonymous hash of end5->end3 relative to the parent sequence
CDS_coords => 0, #assigned to anonymous hash of end5->end3 relative to the parent sequence
mRNA_exon_objs => 0, # holds arrayref to mRNA_obj, retrieve only thru method: get_exons()
num_exons => 0, # number of exons in this gene_obj
model_span => [], # holds array ref to (end5,end3) for CDS range of gene.
gene_span => [], # holds array ref to (end5,end3) for mRNA range of gene.
gene_length => 0, # length of gene span (int).
mid_pt => 0, # holds midpoint of gene-span
strand => 0, # [+-]
gi => undef, #text
prot_acc => undef, #text
is_pseudogene => 0, # toggle indicating pseudogene if 1.
is_5prime_partial => 0, #boolean indicating missing 5' part of gene.
is_3prime_partial => 0, #boolean
protein_seq => undef, # holds protein sequence
protein_seq_length => 0,
CDS_sequence => undef, #holds CDS sequence (translated to protein); based on CDS_exon coordinates
CDS_seq_length => 0,
cDNA_sequence => undef, #holds cDNA sequence; based on mRNA exon coordinates.
cDNA_seq_length => 0,
gene_sequence => undef, #holds unspliced transcript
gene_sequence_length => 0, #length of unspliced transcript
TU_feat_name => undef, #feat_names are TIGR temporary identifiers.
Model_feat_name =>undef,
classification => 'annotated_genes', #type of seq_element.
gene_synonyms => [], #list of synonymous model feat_names
GeneOntology=>[], #list of Gene_ontology assignment objects. ...see GeneOntology.pm
## Additional functional attributes:
secondary_gene_names => [],
secondary_product_names => [],
secondary_gene_symbols => [],
secondary_ec_numbers =>[],
## Alternative splicing support.
num_additional_isoforms => 0, # number of additional isoforms stored in additonal_isoform list below
additional_isoforms => [] # stores list of Gene_objs corresponding to the additional isoforms.
};
bless($self);
return ($self);
}
=over 4
=item erase_gene_structure()
B<Description:> Removes the structural components of a gene (ie. exons, CDSs, coordinate spans, any corresponding sequences)
B<Parameters:> none
B<Returns:> none
=back
=cut
## erase gene structure
sub erase_gene_structure {
my $self = shift;
$self->{mRNA_exon_objs} = 0;
$self->{num_exons} = 0;
$self->{model_span} = [];
$self->{gene_span} = [];
$self->{gene_length} = 0;
$self->{strand} = 0;
$self->{protein_seq} = 0;
$self->{CDS_sequence} = 0;
$self->{CDS_seq_length} = 0;
$self->{cDNA_sequence} = 0;
$self->{cDNA_seq_length} = 0;
}
=over 4
=item clone_gene()
B<Description:> Clones this Gene_obj by copying attributes from this Gene to a new gene. Does NOT do a deep clone for all attributes. See dclone() for a more rigorous cloning method. This method is safer because all references are not cloned, only the critical ones.
B<Parameters:> none
B<Returns:> new Gene_obj
=back
=cut
## all objects are cloned. References to data only are not.
sub clone_gene {
my $self = shift;
my $clone = new Gene_obj();
## Copy over the non-ref attribute values.
foreach my $key (keys %$self) {
my $value = $self->{$key};
if (defined $value) {
## Not copying over refs.
if (ref $value) {
next;
}
## Not copying over attributes of length > 200, such as protein/nucleotide sequences
my $length = length($value);
if ($length > 200) { next;}
}
# passed tests above, copying attribute.
$clone->{$key} = $value;
}
## copy over the gene synonyms.
my @gene_syns = @{$self->{gene_synonyms}};
$clone->{gene_synonyms} = \@gene_syns;
## copy the GO assignments:
my @GO_assignments = $self->get_gene_ontology_objs();
if (@GO_assignments) {
foreach my $go_assignment (@GO_assignments) {
my $go_clone = dclone($go_assignment);
$clone->add_gene_ontology_objs($go_clone);
}
}
## copy gene structure.
my @exons = $self->get_exons();
foreach my $exon (@exons) {
$clone->add_mRNA_exon_obj($exon->clone_exon());
}
foreach my $isoform ($self->get_additional_isoforms()) {
my $isoform_clone = $isoform->clone_gene();
$clone->add_isoform($isoform_clone);
}
$clone->refine_gene_object();
return ($clone);
}
=over 4
=item deep_clone()
B<Description:> Provides a deep clone of a gene_obj. Only references supported in Gene_obj documentation are supported. Those added in a rogue way are undef()d
B<Parameters:> none
B<Returns:> $gene_obj
uses the Storable dclone() function to deep clone the Gene_obj
=back
=cut
;
## all objects are cloned. References to data only are not.
sub deep_clone {
my $self = shift;
my $clone = dclone($self);
my %supported_refs = (model_span => 1,
gene_span => 1,
gene_synonyms => 1,
Gene_ontology => 1,
additional_isoforms=>1,
mRNA_exon_objs => 1);
foreach my $gene_obj ($clone, $clone->get_additional_isoforms()) {
my @keys = keys %$gene_obj;
foreach my $key (@keys) {
my $value = $gene_obj->{$key};
if (ref $value && !$supported_refs{$key}) {
$gene_obj->{$key} = undef;
}
}
}
return ($clone);
}
=over 4
=item populate_gene_obj()
B<Description:> Given CDS and mRNA coordinates stored in hash form, a gene object is populated with mRNA and CDS exons. This is one available way to populate a newly instantiated Gene_obj.
B<Parameters:> $cds_hash_ref, $mRNA_hash_ref, <$seq_ref>
$mRNA_hash_ref is a reference to a hash holding the end5 => end3 coordinates of the Exons
$cds_hash_ref same as mRNA_has_ref except holds the CDS end5 => end3 coordinates.
$seq_ref is a reference to a string containing the genomic sequence. This is an optional parameter.
B<Returns:> none
=back
=cut
;
## Do several things at once: assign CDS and mRNA coordinates, and build gene sequences.
## The \$seq_ref is optional in case you want to create the sequence types.
sub populate_gene_obj {
my ($self, $cds_ref, $mRNA_ref, $seq_ref) = @_;
$self->set_CDS_coords ($cds_ref);
$self->set_mRNA_coords ($mRNA_ref);
$self->refine_gene_object();
if (ref $seq_ref) {
$self->create_all_sequence_types($seq_ref);
}
## reinitialize the hashrefs:
$self->{mRNA_coords} = 0;
$self->{CDS_coords} = 0;
}
# alias above
sub populate_gene_object {
my $self = shift;
$self->populate_gene_obj(@_);
}
####
sub populate_gene_object_via_CDS_coords {
my $self = shift;
my @coordsets = @_;
foreach my $coordset (@coordsets) {
my ($end5, $end3) = @$coordset;
my $mrna_exon_obj = mRNA_exon_obj->new($end5, $end3);
my $cds_obj = CDS_exon_obj->new($end5, $end3);
$mrna_exon_obj->{CDS_exon_obj} = $cds_obj;
$self->add_mRNA_exon_obj($mrna_exon_obj);
}
$self->refine_gene_object();
return;
}
sub build_gene_obj_exons_n_cds_range {
my $self = shift;
my ($exons_aref, $cds_lend, $cds_rend, $orient) = @_;
my @exon_coords;
foreach my $exon_aref (@$exons_aref) {
my ($exon_lend, $exon_rend) = sort {$a<=>$b} @$exon_aref;
push (@exon_coords, [$exon_lend, $exon_rend] );
}
@exon_coords = sort {$a->[0]<=>$b->[0]} @exon_coords;
unless ($orient =~ /^[\+\-]$/) {
confess "Error, orient not [+-] ";
}
## build the CDS coordinates.
my @cds_range;
if ($cds_lend > 0 && $cds_rend > 0) {
($cds_lend, $cds_rend) = sort {$a<=>$b} ($cds_lend, $cds_rend);
foreach my $exon_coords_aref (@exon_coords) {
my ($exon_lend, $exon_rend) = @$exon_coords_aref;
if ($exon_rend >= $cds_lend && $exon_lend <= $cds_rend) {
## got overlap
my $cds_exon_lend = ($cds_lend < $exon_lend) ? $exon_lend : $cds_lend;
my $cds_exon_rend = ($cds_rend > $exon_rend) ? $exon_rend : $cds_rend;
push (@cds_range, [$cds_exon_lend, $cds_exon_rend]);
}
}
unless (@cds_range) {
confess "Error, no CDS exon coords built based on exon overlap";
}
}
## all coordinate sets are ordered left to right.
# build the coordinates href
my %exon_coords;
my %cds_coords;
foreach my $exon_coords_aref (@exon_coords) {
my ($exon_lend, $exon_rend) = @$exon_coords_aref;
my ($exon_end5, $exon_end3) = ($orient eq '+') ? ($exon_lend, $exon_rend) : ($exon_rend, $exon_lend);
$exon_coords{$exon_end5} = $exon_end3;
}
foreach my $cds_coords_aref (@cds_range) {
my ($cds_lend, $cds_rend) = @$cds_coords_aref;
my ($cds_end5, $cds_end3) = ($orient eq '+') ? ($cds_lend, $cds_rend) : ($cds_rend, $cds_lend);
$cds_coords{$cds_end5} = $cds_end3;
}
# print Dumper (\%cds_coords) . Dumper (\%exon_coords);
$self->populate_gene_obj(\%cds_coords, \%exon_coords);
return ($self);
}
####
sub join_adjacent_exons {
my $self = shift;
my @exons = $self->get_exons();
my $strand = $self->get_orientation();
my $first_exon = shift @exons;
my @new_exons = ($first_exon);
while (@exons) {
my $prev_exon = $new_exons[$#new_exons];
my ($prev_end5, $prev_end3) = $prev_exon->get_coords();
my $next_exon = shift @exons;
my ($next_end5, $next_end3) = $next_exon->get_coords();
if ( ($strand eq '+' && $prev_end3 == $next_end5 - 1) # adjacent
||
($strand eq '-' && $prev_end3 == $next_end5 + 1) ) {
$prev_exon->merge_exon($next_exon);
}
else {
push (@new_exons, $next_exon);
}
}
$self->{mRNA_exon_objs} = [@new_exons];
$self->refine_gene_object();
return;
}
=over 4
=item AAToNucleotideCoords()
B<Description:> Converts an amino acid -based coordinate to a genomic sequence -based coordinate.
B<Parameters:> $aa_coord
B<Returns:> $genomic_coord
undef is returned if the aa_coord could not be converted.
=back
=cut
;
sub AAToNucleotideCoords{
my($self) = shift;
my($aacoord) = shift;
my($debug) = shift;
my($PCDS_coords) = {};
my($A2NMapping) = {};
my($currAA) = 1;
my $strand = $self->{strand};
my @exons = $self->get_exons();
my($cds_count)=0;
my($translated_bp)=-1;
my($lastcarryover)=0;
my($end_bp);
foreach my $exon (sort {
if($strand eq "+"){
$a->{end5}<=>$b->{end5};
}
else{
$b->{end5}<=>$a->{end5};
}
} @exons) {
my $cds = $exon->get_CDS_obj();
if ($cds) {
my @cds_coords = $cds->get_CDS_end5_end3();
my($bpspread) = abs($cds_coords[0]-$cds_coords[1]);
$bpspread+=$lastcarryover;
my($nextAA) = int($bpspread/3); # last complete AA in CDS
$lastcarryover = $bpspread%3;
$PCDS_coords->{$currAA} = $currAA+$nextAA-1;
if($strand eq "+"){
$A2NMapping->{$currAA} = $cds_coords[0]<$cds_coords[1]?$cds_coords[0]:$cds_coords[1];
}
else{
$A2NMapping->{$currAA} = $cds_coords[0]<$cds_coords[1]?$cds_coords[1]:$cds_coords[0];
}
print "DEBUG: $strand $cds_count AA range ($currAA - $PCDS_coords->{$currAA}) nucleotide start($A2NMapping->{$currAA})\n" if($debug);
$currAA = $currAA+$nextAA;
$cds_count++;
if($strand eq "+"){
$end_bp = $cds_coords[0]<$cds_coords[1]?$cds_coords[1]:$cds_coords[0];
}
else{
$end_bp = $cds_coords[0]<$cds_coords[1]?$cds_coords[0]:$cds_coords[1];
}
}
}
# PCDS_coords key/value are start/stop aa counts for each cds;
# A2NMapping stores cds AA start key to cds nucleotide start
$cds_count=0;
foreach my $PCDS_end5 (sort {
$a<=>$b;
}(keys %$PCDS_coords)) {
my($PCDS_end3) = $PCDS_coords->{$PCDS_end5};
if($aacoord>=$PCDS_end5 && $aacoord<=$PCDS_end3){
my($nucleotide_start) = $A2NMapping->{$PCDS_end5};
my($aa_offset) = $aacoord - $PCDS_end5;
my($nucleotide_offset) = $aa_offset*3;
print "DEBUG: CDS offset $aa_offset AA $nucleotide_offset bp\n" if($debug);
if($strand eq "+"){
$translated_bp = $nucleotide_start+$nucleotide_offset;
}
else{
$translated_bp = $nucleotide_start-$nucleotide_offset;
}
print "DEBUG: Mapping $aacoord to $translated_bp in cds $cds_count\n" if($debug);
print "DEBUG: CDS $PCDS_end5 - $PCDS_end3 nucleotide start $A2NMapping->{$PCDS_end5}, nuc offset $nucleotide_offset\n" if($debug);
}
$cds_count++;
}
#}
if($translated_bp == -1){
$translated_bp = undef;
print STDERR "Unable to translate AA coordinate: $aacoord. Off end. Using undef\n" if($debug);
}
return $translated_bp;
}
## private method, used by populate_gene_obj()
# sets CDS_coords instance member to a hash reference of CDS coordinates. $hash{end5} = end3
sub set_CDS_coords {
my $self = shift;
my $hash_ref = shift;
if (ref ($hash_ref) eq 'HASH') {
$self->{CDS_coords} = $hash_ref;
} else {
print STDERR "Cannot set CDS_coords, must have hash reference\n";
}
}
=over 4
=item get_gene_span()
B<Description:> Retrieves the coordinates which span the length of the gene along the genomic sequence.
B<Parameters:> none
B<Returns:> (end5, end3)
These coordinates represent the minimal and maximal exonic coordinates of the gene. Orientation can be inferred by the relative values of end5 and end3.
=back
=cut
;
## All return gene end5, end3 ###
sub get_gene_span {
my $self = shift;
return (@{$self->{gene_span}});
}
## private
sub get_seq_span {
my $self = shift;
return ($self->get_gene_span());
}
=over 4
=item get_coords()
B<Description:> See get_gene_span()
B<Parameters:> none
B<Returns:> (end5, end3)
=back
=cut
sub get_coords {
my $self = shift;
return ($self->get_gene_span());
}
=over 4
=item get_model_span()
B<Description:> Retrieves the coordinates spanned by the protein-coding region of the gene along the genomic sequence.
B<Parameters:> none
B<Returns:> (end5, end3)
These coordinates are determined by the min and max of the CDS components of the gene.
=back
=cut
sub get_model_span {
my $self = shift;
return (@{$self->{model_span}});
}
sub get_CDS_span { # preferred
my $self = shift;
return($self->get_model_span());
}
=over 4
=item get_transcript_span()
B<Description:> Retrieves the coordinates spanned by the exonic regions of the gene along the genomic sequence.
B<Parameters:> none
B<Returns:> (lend, rend)
These coordinates are determined by the min and max of the CDS components of the gene.
=back
=cut
sub get_transcript_span {
my $self = shift;
my @coords;
my @exons = $self->get_exons();
foreach my $exon (@exons) {
push (@coords, $exon->get_coords());
}
@coords = sort {$a<=>$b} @coords;
my $lend = shift @coords;
my $rend = pop @coords;
return($lend, $rend);
}
sub is_pseudogene {
my $self = shift;
return ($self->{is_pseudogene});
}
sub set_pseudogene {
my $self = shift;
my $pseudogene_val = shift;
unless ($pseudogene_val =~ /[01]/) {
confess "Error, can set pseudogene to zero or one only.\n";
}
foreach my $gene ($self, $self->get_additional_isoforms()) {
$gene->{is_pseudogene} = $pseudogene_val;
}
return;
}
#private
# sets mRNA_coords instance member to a hash reference of CDS coordinates. $hash{end5} = end3
sub set_mRNA_coords {
my $self = shift;
my $hash_ref = shift;
if (ref ($hash_ref) eq 'HASH') {
$self->{mRNA_coords} = $hash_ref;
} else {
print STDERR "Cannot set CDS_coords, must have hash reference\n";
}
}
=over 4
=item refine_gene_object()
B<Description:> This method performs some data management operations and should be called at any time modifications have been made to the gene structure (ie. exons added or modified, model isoforms added, etc). It performs the following orientations:
-Sets (or resets) gene span and model span coordinates, strand orientation, gene length, mid-point.
B<Parameters:> none
B<Returns:> none
=back
=cut
## Once mRNA_coords and CDS_coords have been assigned, this will populate the remaining elements in the gene object.
sub refine_gene_object {
my ($self) = shift;
#check to see if mRNA_coords field is populated. If not, initialize.
if ($self->{mRNA_coords} == 0) {
$self->{mRNA_coords} = {};
}
my ($CDS_coords, $mRNA_coords) = ($self->{CDS_coords}, $self->{mRNA_coords});
unless ($CDS_coords && $mRNA_coords) {
#maybe created exon objects already
if ($self->{mRNA_exon_objs}) {
$self->trivial_refinement();
}
return;
}
# intialize mRNA_exon_objs to array ref.
$self->{mRNA_exon_objs} = [];
#retrieve coordinate data.
my %mRNA = %$mRNA_coords;
my %CDS = %$CDS_coords;
my @mRNAcoords = keys %mRNA;
my @CDScoords = keys %CDS;
my (%new_mRNA, %new_CDS);
## if correlation between mRNA exons and CDS exons, then map CDS's to mRNA's, otherwise, replicate CDSs as mRNAs
if ($#mRNAcoords >= $#CDScoords) {
foreach my $mRNA_end5 (keys %mRNA) {
my $mRNA_end3 = $mRNA{$mRNA_end5};
#find overlapping cds exon to mRNA exon
#easy to compare if in same orientation for all comparisons
my ($m1, $m2) = ($mRNA_end5 < $mRNA_end3) ? ($mRNA_end5, $mRNA_end3) : ($mRNA_end3, $mRNA_end5);
#create mRNA_exon_obj
my $mRNA_exon_obj = mRNA_exon_obj->new ($mRNA_end5, $mRNA_end3);
$new_mRNA{$mRNA_end5} = $mRNA_end3;
foreach my $CDS_end5 (keys %CDS) {
my $CDS_end3 = $CDS{$CDS_end5};
my ($c1, $c2) = ($CDS_end5 < $CDS_end3) ? ($CDS_end5, $CDS_end3) : ($CDS_end3, $CDS_end5);
## do overlap comparison; CDS must be contained within mRNA exon
if ( ($c1 >= $m1) && ($c2 <= $m2)) {
# found the contained CDS
$mRNA_exon_obj->{CDS_exon_obj} = CDS_exon_obj->new ($CDS_end5, $CDS_end3);
$new_CDS{$CDS_end5} = $CDS_end3;
last;
}
}
$self->add_mRNA_exon_obj($mRNA_exon_obj);
}
} else { # remap CDSs to mRNAS
print STDERR "ERROR: mRNA exons < CDS exons. Copying all CDS exons into mRNA exons. \n\n";
foreach my $CDS_end5 (keys %CDS) {
my $CDS_end3 = $CDS{$CDS_end5};
my $mRNA_exon_obj = mRNA_exon_obj->new ($CDS_end5, $CDS_end3);
$mRNA_exon_obj->{CDS_exon_obj} = CDS_exon_obj->new ($CDS_end5, $CDS_end3);
$self->add_mRNA_exon_obj($mRNA_exon_obj);
$new_mRNA{$CDS_end5} = $CDS_end3;
$new_CDS{$CDS_end5} = $CDS_end3;
}
}
$self->trivial_refinement();
## assign orientation to all children exon and CDS components.
my $strand = $self->get_orientation();
foreach my $exon ($self->get_exons()) {
$exon->{strand} = $strand;
if (my $cds = $exon->get_CDS_exon_obj()) {
$cds->{strand} = $strand;
}
}
return;
}
## alias
sub refine_gene_obj {
my $self = shift;
$self->refine_gene_object();
}
=over 4
=item get_exons()
B<Description:>Retrieves a list of exons belonging to this Gene_obj
B<Parameters:> none
B<Returns:> @exons
@exons is an ordered list of mRNA_exon_obj; the first exon of the list corresponds to the first exon of the spliced gene.
=back
=cut
;
sub get_exons {
my ($self) = shift;
if ($self->{mRNA_exon_objs} != 0) {
my @exons = (@{$self->{mRNA_exon_objs}});
@exons = sort {$a->{end5}<=>$b->{end5}} @exons;
if ($self->{strand} eq '-') {
@exons = reverse (@exons);
}
return (@exons);
} else {
my @x = ();
return (@x); #empty array
}
}
## private
sub get_segments {
my $self = shift;
return ($self->get_exons());
}
=over 4
=item number_of_exons()
B<Description:> Provides the number of exons contained by the Gene
B<Parameters:> none
B<Returns:> int
=back
=cut
sub number_of_exons {
my $self = shift;
my $exon_number = $#{$self->{mRNA_exon_objs}} + 1;
return ($exon_number);
}
=over 4
=item get_intron_coordinates()
B<Description:> Provides an ordered list of intron coordinates
B<Parameters:> none
B<Returns:> ( [end5,end3], ....)
A list of arrayRefs are returned providing the coordinates of introns, ordered from first intron to last intron within the gene.
=back
=cut
;
sub get_intron_coordinates {
my $gene_obj = shift;
my $strand = $gene_obj->get_orientation();
my @exons = $gene_obj->get_exons();
## exon list should already be sorted.
my @introns = ();