-
Notifications
You must be signed in to change notification settings - Fork 2
/
GTF.pm
4280 lines (4071 loc) · 125 KB
/
GTF.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
# -*- Mode: Perl; tab-width: 8; perl-indent-level: 2; indent-tabs-mode: nil -*-
use strict;
###############################################################################
# GTF
###############################################################################
# The GTF object parses and stores all data from a gtf file.
package GTF;
use Carp;
# GTF::new(hash)
# This is the contructor for GTF objects. It takes one required argument
# which is a hash containing the following optional fields:
# gtf_filename: The filename of the gtf file to be loaded. If no filename
# is given it just creates an empty object.
# seq_filename: The filename of the sequence corresponding to this gtf file.
# If given additional validity checks are made, additional stats
# are kept and the transcript can be output.
# tx_out_fh: The filehandle to output the genes transcripts to. Only works
# If seq_filename is given.
# conseq_filename: The filename of the conservation sequence corresponding to
# this gtf file. If given additional stats are kept.
# warning_fh: The filehandle to output gtf format warnings to. If none
# is given warnings are disregarded.
sub new {
my ($info) = @_;
my $gtf = bless {Genes => [], Transcripts => [], Exons => [], CDS => [],
Inter => [], Inter_CNS => [], Filename => "", Sequence => "",
Comments => "", Modified => 0};
$gtf->{Total_Conseq} = {0 => -1,
1 => -1,
2 => -1};
$gtf->{Total_Seq} = {A => -1,
C => -1,
G => -1,
T => -1,
N => -1};
if(defined($info->{seq_filename})){
if(-e $info->{seq_filename}){
$gtf->{Sequence} = $info->{seq_filename};
}
else{
$gtf->{Sequence} = 0;
print STDERR "GTF: $info->{seq_filename} does not exist.\n";
}
}
else{
$gtf->{Sequence} = 0;
}
if(defined($info->{strict})){
$gtf->{Strict} = $info->{strict};
}
else{
$gtf->{Strict} = 0;
}
if(defined($info->{tx_out_fh})){
if(defined($gtf->{Sequence})){
$gtf->{Tx} = $info->{tx_out_fh};
}
else{
$gtf->{Tx} = 0;
print STDERR "GTF: Cannot ouput transcripts without the sequence.\n";
}
}
else{
$gtf->{Tx} = 0;
}
if(defined($info->{conseq_filename})){
if(-e $info->{conseq_filename}){
$gtf->{Conseq} = $info->{conseq_filename};
}
else{
$info->{Conseq} = 0;
print STDERR "GTF: $info->{conseq_filename} does not exist.\n";
}
}
else{
$info->{Conseq} = 0;
}
if((defined($info->{inframe_stops})) && ($info->{inframe_stops})){
$gtf->{Inframe_Stops} = 1;
}
else{
$gtf->{Inframe_Stops} = 0;
}
if((defined($info->{fix_gtf})) &&
($info->{fix_gtf} == 1)){
$gtf->{Fix_GTF} = 1;
}
else{
$gtf->{Fix_GTF} = 0;
}
if(defined($info->{warning_skips})){
$gtf->{Warning_Skips} = $info->{warning_skips};
}
else{
$gtf->{Warning_Skips} = [];
}
if(defined($info->{bad_list})){
$gtf->{Bad_List} = $info->{bad_list};
}
else{
$gtf->{Bad_List} = [];
}
if(defined($info->{detailed_error_count})){
$gtf->{Detailed_Error_Count} = $info->{detailed_error_count};
}
if(defined($info->{mark_ase})) {
$gtf->{Mark_ASE} = $info->{mark_ase};
}
else {
$gtf->{Mark_ASE} = 0;
}
if(defined($info->{gtf_filename})){
if(-e $info->{gtf_filename}){
$gtf->{Filename} = $info->{gtf_filename};
if((defined($info->{no_check})) && ($info->{no_check})){
$gtf->_load_no_check($info->{gtf_filename});
}
else{
$gtf->_parse_file($info->{warning_fh});
}
if($gtf->{Mark_ASE}) {
$gtf->mark_ase;
}
}
else{
print STDERR "GTF: $info->{gtf_filename} does not exist.\n";
}
}
else{
$gtf->{Filename} = "";
}
return $gtf;
}
# GTF::_update
# This function resorts all lists. It is called when a list is requested.
# It does nothing unless the $gtf->{Modified} value is 1. Sets
# $gene->{Modified} to 0;
sub _update{
my ($gtf) = @_;
unless($gtf->{Modified}){
return;
}
$gtf->{Modified} = 0;
my $genes = $gtf->{Genes};
my $txs = $gtf->{Transcripts};
my $cds = $gtf->{CDS};
my $inter = $gtf->{Inter};
$genes = [sort {$a->start <=> $b->start} @$genes];
$txs = [sort {$a->start <=> $b->start} @$txs];
$cds = [sort {$a->start <=> $b->start} @$cds];
$inter = [sort {$a->start <=> $b->start} @$inter];
$gtf->{Genes} = $genes;
$gtf->{Transcripts} = $txs;
$gtf->{CDS} = $cds;
$gtf->{Inter} = $inter;
}
# GTF::genes()
# This function returns an array of refernces to GTF::Gene objects.
# The array contains one reference for each gene in the gtf file.
# The array is sorted by the start position of each gene.
sub genes{
my ($gtf) = @_;
$gtf->_update;
return $gtf->{Genes};
}
sub transcripts{
my ($gtf) = @_;
$gtf->_update;
return $gtf->{Transcripts};
}
# GTF::add_gene(gene_ref)
# This function take a reference to a GTF::Gene object and adds it to the
# list of genes stored by this object.
sub add_gene{
my ($gtf,$gene) = @_;
my $genes = $gtf->{Genes};
my $txs = $gtf->{Transcripts};
my $cds = $gtf->{CDS};
push @$genes, $gene;
push @$txs, @{$gene->transcripts};
push @$cds, @{$gene->cds};
$gtf->{Modified} = 1;
}
# GTF::add_feature(feature_ref,seqname,source,strand,id)
# This function will take a reference to an intergenic feature (such as
# inter or inter_CNS) and add it to the appropriate feature list.
# Pass a sequence name, the strand ("+" is OK for intergenic)
# and an ID to identify the feature.
# Note that the feature must be of the types inter or inter_CNS.
sub add_feature{
my ($gtf,$feature,$seqname,$source,$strand,$id) = @_;
die "Only inter or inter_CNS features may be directly added to a GTF.\n"
if($feature->type ne "inter" && $feature->type ne "inter_CNS");
my $gene = GTF::Gene::new($id,$seqname,$source,$strand);
my $tx = GTF::Transcript::new($id);
$gene->add_transcript($tx);
$tx->add_feature($feature);
my $features;
if($feature->type eq "inter") { $features = $gtf->{Inter} }
elsif($feature->type eq "inter_CNS") { $features = $gtf->{Inter_CNS} }
push @$features, $feature;
$gtf->{Modified} = 1;
}
# GTF::set_genes(gene_list)
# This function take a reference to a list of GTF::Gene objects and adds each to the
# list of genes stored by this object.
sub set_genes{
my ($gtf,$genes) = @_;
$gtf->{Genes} = $genes;
my @txs;
my @cds;
foreach my $gene (@$genes){
push @txs, @{$gene->transcripts};
push @cds, @{$gene->cds};
}
$gtf->{Transcripts} = \@txs;
$gtf->{CDS} = \@cds;
$gtf->{Modified} = 1;
}
# GTF::remove_gene(gene_id)
# This function takes a gene_id and removes the gene with that gene_id
# from the list of genes stored by this object, if a gene with that gene_id
# can be found. Otherwise does nothing. Returns true if a gene was removed
# and false if no gene with that gene_id was found.
sub remove_gene{
my ($gtf,$gene_id) = @_;
my $genes = $gtf->{Genes};
my $removed = 0;
my @new_genes;
my @new_txs;
my @new_cds;
foreach my $gene (@$genes){
if($gene->gene_id eq $gene_id){
$removed++;
}
else{
push @new_genes, $gene;
push @new_txs, @{$gene->transcripts};
push @new_cds, @{$gene->cds};
}
}
if($removed){
$gtf->{Genes} = \@new_genes;
$gtf->{Transcripts} = \@new_txs;
$gtf->{CDS} = \@new_cds;
}
return $removed;
}
# GTF::remove_feature(feature)
# This function will remove a feature from the list of features.
# Note the feature must be of type inter or inter_CNS.
sub remove_feature {
my($gtf,$feature) = @_;
my $removed = 0;
my $feature_key;
if($feature->type eq "inter") {
$feature_key = "Inter";
}
elsif($feature->type eq "inter_CNS") {
$feature_key = "Inter_CNS";
}
my @new_features;
foreach my $stored_feature (@{$gtf->{$feature_key}}) {
if($stored_feature == $feature) {
$removed++;
}
else {
push @new_features, $stored_feature;
}
}
if($removed) {
$gtf->{$feature_key} = \@new_features;
}
return $removed;
}
# GTF::set_filename(filename)
# Sets the filename to be returned by the filename function
sub set_filename{
my ($gtf,$filename) = @_;
$gtf->{Filename} = $filename;
}
# GTF::offset(ammount)
# This function will offset all genes in this gene by the given ammount
sub offset{
my ($gtf,$offset) = @_;
unless(defined($offset)){
print STDERR "Undefined value passed to GTF::offset.\n";
return;
}
my $genes = $gtf->{Genes};
foreach my $gene (@$genes){
$gene->offset($offset);
}
$gtf->{Modified} = 1;
}
# GTF::reverse_complement(seq_length)
# This function takes the length of the sequence this gtf file was based on and
# and reverse complements everything in the file. So the positive strand becomes
# the negative strand and the files starts at seq_length and moves downward to 0.
sub reverse_complement{
my ($gtf, $seq_length) = @_;
unless(defined($seq_length)){
print STDERR
"Undefined value for seq_length passed to GTF::reverse_complement.\n";
}
my $genes = $gtf->{Genes};
foreach my $gene(@$genes){
$gene->reverse_complement($seq_length);
}
$gtf->{Modified} = 1;
}
# GTF::cds()
# This function returns an array of refernces to GTF::Feature objects.
# The array contains one refernece to each CDS feature in the gtf file.
# The array is sorted by the <start> of each CDS.
sub cds{
my ($gtf) = @_;
$gtf->_update;
return $gtf->{CDS};
}
sub inter {
my ($gtf) = @_;
$gtf->_update;
return $gtf->{Inter};
}
sub inter_cns {
my ($gtf) = @_;
$gtf->_update;
return $gtf->{Inter_CNS};
}
# GTF::mark_ase()
# If you are looking for alternative splicing events in your GTF file,
# call this function to marke them. This function is separate from the
# _update methods in order to keep the running time low.
sub mark_ase {
my ($gtf) = @_;
#set the alternative splicing event flags for each feature
#currently, only optional coding exons
# The main idea is to check all pairs of transcripts in a gene,
# and find any incident where there are two exons that have the same
# end coord and two exons which have the same start coord
# and that an exon in only one of the transcripts lies between them.
# It is accomplished by sorting all the exons in both transcripts
# and checking for a supercasette, i.e.
#
# ===|||||||||===||||||||======||||||||||====
# ==||||||||||=================||||||||||||==
# <--------------->
# supercassette
#
# Note that it doesn't matter if the opposite ends of the exon are equal.
# rpz
my @genes = @{$gtf->genes};
for my $gene(@{$gtf->genes}) {
my @txs = (@{$gene->transcripts});
if(scalar(@txs) > 1) {
for (my $i = 0; $i < scalar(@txs)-1; $i++) {
next if(scalar(@{$txs[$i]->cds}) == 0);
for (my $j = $i+1; $j < scalar(@txs); $j++) {
next if(scalar(@{$txs[$j]->cds}) == 0);
my @ocds = sort {$a->start <=> $b->start || $a->stop <=> $b->stop}
(@{$txs[$i]->cds}, @{$txs[$j]->cds});
for(my $k = 4; $k < scalar(@ocds); $k++) {
if( $ocds[$k-4]->stop == $ocds[$k-3]->stop &&
$ocds[$k-1]->start == $ocds[$k]->start) {
if($ocds[$k-2]->length%3 == 0) {
$ocds[$k-2]->set_ase("InframeOptional");
}
else {
$ocds[$k-2]->set_ase("FrameshiftOptional");
}
}
}
}
}
}
}
}
# GTF::infer_exons()
# Skips infering if Exons already exist
# Output is undefined if features overlap, except for start_codon
# Currently knows about utr5, start,stop,cds, utr3
sub infer_exons {
my ($gtf) = @_;
$gtf->_update;
for my $tx (@{$gtf->transcripts}) {
next if (@{$tx->exons});
if ($tx->strand eq '+') {
for ( @{$tx->utr5}, @{$tx->cds}, @{$tx->stop_codons}, @{$tx->utr3} ) {
if (!@{$tx->exons} || ${$tx->exons}[-1]->stop + 1 < $_->start ) {
$tx->add_feature( GTF::Feature::new('exon', $_->start, $_->stop, 0, 0) );
} elsif ( ${$tx->exons}[-1]->stop + 1 eq $_->start ) {
${$tx->exons}[-1]->set_stop($_->stop);
}
}
} else {
for ( @{$tx->utr3}, @{$tx->stop_codons}, @{$tx->cds}, @{$tx->utr5} ) {
if (!@{$tx->exons} || ${$tx->exons}[-1]->stop + 1 < $_->start ) {
$tx->add_feature(GTF::Feature::new('exon', $_->start, $_->stop, 0, 0));
} elsif ( ${$tx->exons}[-1]->stop + 1 eq $_->start ) {
${$tx->exons}[-1]->set_stop($_->stop);
}
}
}
}
}
# GTF::remove_exons()
# remove exons from all transcripts
sub remove_exons {
my ($gtf) = @_;
$gtf->_update;
$_->{Exons} = [] for (@{$gtf->transcripts});
}
# GTF::filename()
# This function returns the filename of the gtf file.
sub filename {shift->{Filename}}
# GTF::comments()
# This function returns all full line commentsfrom the gtf file.
sub comments {shift->{Comments}};
# GTF::conservation_count()
# Returns a hash containing counts for each conservation character (0,1,2)
sub conservation_count {shift->{Total_Conseq}};
# GTF::sequence_count()
# Returns a hash containing counts for each sequence character (A,C,T,G,N,X),
# Where X is all other characters
sub sequence_count {shift->{Total_Seq}};
# GTF::output_gtf_file([file_handle]);
# This function moves through each gene in the gtf file and outputs
# all data in valid gtf2 format. If takes and optional file_handle
# as the only argument to which it outputs the data. If no argument is
# given it outputs the data to stdout.
sub output_gtf_file{
my ($gtf,$out_handle) = @_;
$gtf->_update;
unless(defined $out_handle){
$out_handle = \*STDOUT;
}
if($gtf->comments){
foreach my $comment (@{$gtf->comments}){
print $out_handle "$comment\n";
}
}
for my $gene (@{$gtf->genes}) { $gene->_update }
my @top_level_features = sort {$a->start <=> $b->start}
(@{$gtf->genes}, @{$gtf->inter_cns}, @{$gtf->inter});
for my $feature (@top_level_features) {
$feature->output_gtf($out_handle);
}
}
# GTF::output_gff_file([file_handle]);
# This function moves through each gene in the gtf file and outputs
# all data in valid GFF format. If takes and optional file_handle
# as the only argument to which it outputs the data. If no argument is
# given it outputs the data to stdout.
sub output_gff_file{
my ($gtf,$out_handle) = @_;
$gtf->_update;
unless(defined $out_handle){
$out_handle = \*STDOUT;
}
if($gtf->genes){
foreach my $gene (@{$gtf->genes}){
$gene->output_gff($out_handle);
}
}
}
# GTF::_parse_file()
# This function is used internally by the GTF constructor GTF::new
# to read, parse, and store the GTF file;
sub _parse_file{
my ($gtf,$warnings) = @_;
unless(defined($warnings)){
if(open(WARN, ">/dev/null")){
$warnings = \*WARN;
}
else{
die "Could not open /dev/null for write\n";
}
}
my $fix_gtf = $gtf->{Fix_GTF};
my $strict = $gtf->{Strict};
#open file
open(GTF, "<$gtf->{Filename}")
or die "Could not open $gtf->{Filename}.\n";
my (%GeneObj,%TxObj);
my (@all_genes,@all_txs,@all_cds,@all_inter,@all_cns);
my $all_line_comments = [];
#prepare error counts
my $max_error_count = 5;
if(defined($gtf->{Detailed_Error_Count})){
$max_error_count = $gtf->{Detailed_Error_Count};
}
my @error_msgs = $gtf->_get_error_messages();
my @errors;
my @bad_list;
my $no_warn = $gtf->{Warning_Skips};
for(my $i = 0;$i <= $#error_msgs;$i++){
if($$no_warn[$i]){
$errors[$i] = $max_error_count;
}
else{
$errors[$i] = 0;
}
}
#read the file
my $line_num = 0;
while(my $in_line = <GTF>){
$line_num++;
chomp $in_line;
if($in_line !~ /\S/){
next;
}
if($in_line =~ /^\s*\#/){
push @$all_line_comments, $in_line;
next;
}
else{
#remove leading whitespace and get comments
my $comments = "";
while($in_line =~ /^(.*)\#(.*)$/){
$in_line = $1;
$comments = $2.$comments;
}
if($in_line =~ /^\s+(.*)$/){
$in_line = $1;
}
my @data = split /\s+/,$in_line;
#verify line is correct length
if($#data < 8){
if($errors[0] < $max_error_count){
print $warnings
"Not enough fields on line $line_num.\n";
}
$errors[0]++;
next;
}
#check for correct whitespace
my $field = 1;
while(($field < 8)&&($in_line =~ /\S+(\s+)/g)){
unless($1 =~ /^\t$/){
if($errors[1] < $max_error_count){
print $warnings
"Incorrect type of whitespace between fields ",
"$field and ",$field + 1, " on line $line_num. ",
"Should be tab.\n";
}
$errors[1]++;
}
$field++;
}
#verify correct <feature> field
$data[2] = lc($data[2]);
unless(($data[2] eq "cds") || ($data[2] eq "exon")
|| ($data[2] eq "5utr") || ($data[2] eq "3utr")
|| ($data[2] eq "start_codon")
|| ($data[2] eq "stop_codon")
|| ($data[2] eq "sec")
|| ($data[2] eq "intron_cns")
|| ($data[2] eq "inter_cns")
|| ($data[2] eq "inter")) {
if($errors[2] < $max_error_count){
print $warnings
"Incorrect value for <feature> field, \"$data[2]\", ",
"on line $line_num.\n";
}
$errors[2]++;
}
if (($data[2] eq "cds") || ($data[2] eq "5utr")
|| ($data[2] eq "3utr") || ($data[2] eq "sec")) {
$data[2] = uc($data[2]);
}
if ($data[2] eq "inter_cns") { $data[2] = "inter_CNS" }
if ($data[2] eq "intron_cns") { $data[2] = "intron_CNS" }
#verify correct <start> format
if($data[3] !~ /^\d*$/){
if($errors[3] < $max_error_count){
print $warnings
"Non-numerical value for <start> field, \"$data[3]\"",
", on line $line_num.\n";
}
$errors[3]++;
}
#verify correct <stop> format
if($data[4] !~ /^\d*$/){
if($errors[4] < $max_error_count){
print $warnings
"Non-numerical value for <stop> field, \"$data[4]\"",
", on line $line_num.\n";
}
$errors[4]++;
}
#verify start is < stop
if($data[3] > $data[4]){
if($errors[38] < $max_error_count){
print $warnings
"Start field is greater than stop field on line $line_num.\n";
}
$errors[38]++;
my $start = $data[3];
$data[3] = $data[4];
$data[4] = $start;
}
#verify correct <score> format
if($data[5] !~ /^-?\d*\.?\d*$/){ # should check for float
if($data[5] ne '.'){
if($errors[5] < $max_error_count){
print $warnings
"Value for <score> field, \"$data[5]\", is ",
"neither numerical nor \".\" on line $line_num\n";
}
$errors[5]++;
}
}
if(!$strict && $data[5] eq '.'){
$data[5] = 0;
}
#verify correct <strand> field
unless(($data[6] eq "+")||($data[6] eq "-")||
($data[6] eq ".")){
if($errors[6] < $max_error_count){
print $warnings
"Value of <strand> field is \"$data[6]\" on line ",
"$line_num. Should be \"+\", \"-\", or \".\".\n";
}
$errors[6]++;
if(!$strict){
if($data[6] eq "1"){
$data[6] = "+";
}
elsif($data[6] eq "-1"){
$data[6] = "-";
}
elsif($data[6] eq "0"){
$data[6] = ".";
}
}
}
#verify correct <frame> field
unless(($data[7] eq "0")||($data[7] eq "1")||
($data[7] eq "2")||($data[7] eq ".")){
if($errors[7] < $max_error_count){
print $warnings
"Value of <frame> field is \"$data[7]\" on line ",
"$line_num. Should be \"0\", \"1\", \"2\", or \".\"",
".\n";
}
$errors[7]++;
}
#prepare <attributes> and <comment> fields
my $attribute_string = "";
if($in_line =~ /^\S+\t\S+\t\S+\t\S+\t\S+\t\S+\t\S+\t\S+\t(.*)$/){
$attribute_string = "$1 ";
}
#check for equals
if($attribute_string =~ / = /){
if($errors[30] < $max_error_count){
print $warnings
"Illegal \'=\' character in <attributes> field on ",
"line $line_num\n";
}
$errors[30]++;
$attribute_string =~ s/=/ /g;
}
# check for tab character.
if($attribute_string =~ /\t/){
if($errors[9] < $max_error_count){
print $warnings
"Illegal tab character in <attributes> field on ",
"line $line_num.\n";
}
$errors[9]++;
$attribute_string =~ s/\t/ /g;
}
# make sure it has semicolon terminator after each attribute
# otherwise fake it.
unless($attribute_string =~ /;/){
if($errors[10] < $max_error_count){
print $warnings
"<attributes> field conatins no semicolons",
" terminating each attribute on line $line_num.\n";
}
$errors[10]++;
my @att = split /\s+/, $attribute_string;
$attribute_string = "";
my $att_count = $#att - (($#att+1) % 2);
for(my $i = 0;$i <= $att_count;$i+=2){
$attribute_string .= $att[$i]." ".$att[$i+1]."; ";
}
}
# parse <attributes> field
my $attributes = {};
while($attribute_string =~ /(\S+)(\s+)(\S+)(\s*)/g){
my $name = $1;
my $space1 = $2;
my $value = $3;
my $space2 = $4;
if($value =~ /^\"(\S*)\";$/){
$value = $1;
}
else{
if($value =~ /^(\S+);$/){
$value = $1;
}
else{
if($errors[10] < $max_error_count){
$value =~ /(.)$/;
print $warnings
"Bad terminator, \"$1\", after ",
"<attributes> name-value pair, $name $value, on ",
"line $line_num. Should be \";\".\n";
}
$errors[10]++;
}
if($value =~ /^\"(\S+)\"$/){
$value = $1;
}
else{
if($errors[11] < $max_error_count){
print $warnings
"<attributes> field missing quotes around",
" values on line $line_num.\n";
}
$errors[11]++;
}
}
$attributes->{$name} = $value;
unless($space1 =~ /^ $/){
if($errors[12] < $max_error_count){
print $warnings
"Bad separator, $space1, between <attributes> ",
"name-value pair, $name $value, on line ",
"$line_num.\n";
}
$errors[12]++;
}
unless($space2 =~ /^ $/){
if($errors[12] < $max_error_count){
print $warnings
"Bad separator, \"$space2\", between <attributes>",
" name-value pairs on line $line_num. Should be",
" \" \".\n$name $value\n";
}
$errors[12]++;
}
}
# check for gene_id and transcript_id attributes
my $gid = "missing";
if(defined($attributes->{gene_id})){
$gid = $attributes->{gene_id};
}
else{
if($errors[13] < $max_error_count){
print $warnings
"gene_id not set in <attributes> field on line ",
"$line_num.\n";
}
$errors[13]++;
$attributes->{gene_id} = "missing";
}
my $tid = "missing";
if(defined($attributes->{transcript_id})){
$tid = $attributes->{transcript_id};
}
else{
if($errors[14] < $max_error_count){
print $warnings
"transcript_id not set in <attributes> field on line ",
"$line_num.\n";
}
$errors[14]++;
$attributes->{transcript_id} = "missing";
}
#create objects
my $feature =
GTF::Feature::new($data[2], $data[3], $data[4], $data[5], $data[7]);
if($feature->type eq "CDS"){
push @all_cds, $feature;
}
elsif($feature->type eq "inter") {
push @all_inter, $feature;
}
elsif($feature->type eq "inter_CNS") {
push @all_cns, $feature;
}
my $tx;
my $gene;
unless(defined($TxObj{$tid}) && length($tid)){
$tx = GTF::Transcript::new($tid);
$TxObj{$tid} = $tx;
unless($feature->type eq "inter" || $feature->type eq "inter_CNS") {
push @all_txs, $tx;
}
unless(defined($GeneObj{$gid}) && length($gid)){
$gene = GTF::Gene::new($gid,$data[0],$data[1],$data[6]);
$GeneObj{$gid} = $gene;
unless($feature->type eq "inter" || $feature->type eq "inter_CNS") {
push @all_genes, $gene;
}
}
$gene = $GeneObj{$gid};
$gene->add_transcript($tx);
}
else{
$gene = $GeneObj{$gid};
}
$tx = $TxObj{$tid};
#check that strand value is consistent with the strand of the tx
unless($data[6] eq $tx->strand){
if($errors[20] < $max_error_count){
print $warnings
"Inconsistent <strand> value across gene_id = ".
$tx->gene_id.".\n";
}
$errors[20]++;
if (_check_errors_against_badlist($gtf,20)) {
push @bad_list, $tx->id;
}
if($strict){
die;
}
#last;???
}
$tx->add_feature($feature);
}
}
close(GTF);
#check each gene object
foreach my $tx (@all_txs){
my $exons = $tx->exons;
my $cds = $tx->cds;
my $utr3 = $tx->utr3;
my $utr5 = $tx->utr5;
my $sec = $tx->sec;
my $inter = $tx->inter;
my $inter_cns = $tx->inter_cns;
if($#$exons >= 0 && $#$utr3 == -1 && $#$utr5 == -1){
$tx->create_utr_objects_from_exons;
}
my $starts = $tx->start_codons;
my $stops = $tx->stop_codons;
my $strand = $tx->strand;
#make sure this tx contains at least one CDS, inter_CNS, or inter feature
if($#$cds == -1 && $#$inter_cns == -1 && $#$inter == -1){
if($errors[15] < $max_error_count){
print $warnings
"Transcript ".$tx->id." contains no CDS, inter or inter_CNS features.\n";
}
$errors[15]++;
if (_check_errors_against_badlist($gtf,15)) {
push @bad_list, $tx->id;
}
next;
}
#verify start/stop codon exists and has length 3
my $start_codon_start = -1;
my $start_codon_stop = -1;
if($#$starts == -1){
if($errors[17] < $max_error_count){
print $warnings
"Missing start_codon for transcript \"".$tx->id."\".\n";
}
$errors[17]++;
if (_check_errors_against_badlist($gtf,17)) {
push @bad_list, $tx->id;
}
}
else{
my $length = 0;
foreach my $start (@$starts){
$length += $start->stop - $start->start + 1;
if(($start_codon_start < 0) || ($start_codon_start > $start->start)){
$start_codon_start = $start->start;
}
if(($start_codon_stop < 0) || ($start_codon_stop < $start->stop)){
$start_codon_stop = $start->stop;
}
}
if($length != 3){
if($errors[16] < $max_error_count){
print $warnings
"Start Codon length is not three in transcript \"".$tx->id."\".\n";
}
$errors[16]++;
if (_check_errors_against_badlist($gtf,16)) {
push @bad_list, $tx->id;
}
}
}
my $stop_codon_start = -1;
my $stop_codon_stop = -1;
if($#$stops == -1){
if($errors[19] < $max_error_count){
print $warnings
"Missing stop_codon for transcript \"".$tx->id."\".\n";
}
$errors[19]++;
if (_check_errors_against_badlist($gtf,19)) {
push @bad_list, $tx->id;
}
}
else{
my $length = 0;
foreach my $stop (@$stops){
$length += $stop->stop - $stop->start + 1;
if(($stop_codon_start < 0) || ($stop_codon_start > $stop->stop)){
$stop_codon_start = $stop->start;
}
if(($stop_codon_stop < 0) || ($stop_codon_stop < $stop->stop)){
$stop_codon_stop = $stop->stop;
}
}
if($length != 3){
if($errors[18] < $max_error_count){
print $warnings
"Stop Codon length is not three in gene \"".$tx->id."\".\n";
}
$errors[18]++;
if (_check_errors_against_badlist($gtf,18)) {
push @bad_list, $tx->id;
}
}
}
#check that no cds or utr features overlap each other
# also counts introns that are too short
# introns shorter than 4 bp are in all likelihood
# biologically impossible (at least 2 bp for each splice signal)
my $p_exons;
my $overlap = 0;
my $zero_intron_count = 0;
my $short_intron_count = 0;
foreach $p_exons ($utr5, $starts, $cds, $stops, $utr3)
{
my $last_stop;
if ($#$p_exons > 0)
{
$last_stop = $$p_exons[0]->stop;
}
for(my $i = 1;$i <= $#$p_exons;$i++){
my $intron_length = $$p_exons[$i]->start - $last_stop - 1;
if($intron_length < 0){
$overlap++;
}
elsif($intron_length < 4){
$zero_intron_count++;
}
elsif($intron_length < 20){
$short_intron_count++;
}
$last_stop = $$p_exons[$i]->stop;
}
}
# check CDS - UTR junctions for overlaps
# here, introns with length 0 are allowed because some
# exons could be in part UTR and in part CDS
if ($tx->strand eq "+")
{
my $ilen;