-
Notifications
You must be signed in to change notification settings - Fork 45
/
gtf2gff3.pl
1560 lines (1313 loc) · 45.4 KB
/
gtf2gff3.pl
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/perl
use strict;
use warnings;
use Getopt::Long;
use Config::Std;
#-----------------------------------------------------------------------------
#----------------------------------- MAIN ------------------------------------
#-----------------------------------------------------------------------------
my $usage = "
Synopsis:
gtf2gff3 --cfg gtf2gff3_MY_CONFIG.cfg gtf_file > gff3_file
gtf2gff3 --help # for a more detailed help message.
Description:
This script will convert GTF formatted files to valid GFF3 formatted
files. It will map the column 3 (\"type\" column) to valid SO, but
because any non standard term may appear in that column in GTF files,
you may edit the config file to provide your own GTF feature to SO
mapping. The script will also build gene models from exons, CDSs and
other features given in the GTF file. It is currently tested on Ensemble
and Twinscan GTF, and it should work on any other files that follow the
same specification. It does not work on GTF from the UCSC table browser
because those files use the same ID for gene and transcript, so it is
impossible to group multiple transcripts to a gene. See the README that
came with the script for more info.
Options:
--cfg Provide the filename for a config file. See the configuration
file provided with this script for format details. Use this
configuration file to modify the behavior of the script. If no config
file is given it looks for ./gtf2gff3.cfg, ~/gtf2gff3.cfg or
/etc/gtf2gff3.cfg in that order.
--help Provide a more detailed help message.
";
my ($help, $cfg_file);
my $opt_success = GetOptions('help' => \$help,
'cfg=s' => \$cfg_file);
die $usage if ! $opt_success;
if ($help) {
print `perldoc $0`;
exit;
}
my $file = shift;
die $usage unless $file;
my $home = $ENV{HOME} || '.'; #Apache has no home, so avoid the error.
my @cfg_files = ($cfg_file,
'./gtf2gff3.cfg',
"$home/gtf2gff3.cfg",
'/etc/gtf2gff3.cfg',
);
@cfg_files = grep {-e $_ if $_} @cfg_files;
$cfg_file = shift @cfg_files;
################################################################################
# LOAD CONFIGURATION OR USE DEFAULTS
################################################################################
read_config $cfg_file => my %config if -e $cfg_file;
our $INPUT_FEATURE_MAP = $config{INPUT_FEATURE_MAP};
$INPUT_FEATURE_MAP ||= {gene => 'gene',
mRNA => 'mRNA',
exon => 'exon',
five_prime_UTR => 'five_prime_utr',
start_codon => 'start_codon',
CDS => 'CDS',
stop_codon => 'stop_codon',
three_prime_UTR => 'three_prime_utr'};
our $GTF_ATTRB_MAP = $config{GTF_ATTRB_MAP};
$GTF_ATTRB_MAP ||= {gene_id => 'gene_id',
gene_name => 'gene_name',
trnsc_id => 'transcript_id',
trnsc_name => 'transcript_name'};
our $GFF3_ATTRB_MAP = $config{GFF3_ATTRB_MAP};
$GFF3_ATTRB_MAP ||= {gene_id => 'gene_id',
gene_name => 'gene_name',
trnsc_id => 'transcript_id',
trnsc_name => 'transcript_name',
id => 'ID',
parent => 'Parent',
name => 'Name'};
our $LIMIT_ATTRB = $config{MISC}{LIMIT_ATTRB} || 1;
our $ATTRB_DELIMITER = qr{$config{MISC}{ATTRB_DELIMITER}} || qr{\s*;\s*};
our $ATTRB_REGEX = qr{$config{MISC}{ATTRB_REGEX}} ||
qr{^\s*(\S+)\s+(\"[^\"]*\")\s*$};
our $START_IN_CDS = $config{MISC}{START_IN_CDS} || 1;
our $STOP_IN_CDS = $config{MISC}{STOP_IN_CDS} || 0;
our $DEFAULT_STRAND = $config{MISC}{DEFAULT_STRAND};
################################################################################
my ($genes, $features) = parse_gtf($file);
$genes = build_genes($genes);
print_gff3($genes, $features);
#-----------------------------------------------------------------------------
#-------------------------------- SUBROUTINES --------------------------------
#-----------------------------------------------------------------------------
sub parse_gtf {
my $file = shift;
open (my $IN, '<', $file) or die "FATAL: Can't open GTF file: $file for reading.\n$!\n";
# %genes will have the following structure:
# $genes->gene_id->trnsc_id->feature_type->feature
# |
# ->other->feature_type->feature
#
my (%genes, %features);
while (<$IN>) {
chomp;
s/^\s+//;
s/\s+$//;
#Handle full line comments, meta-comments and blank lines;
#Grab any comments
my ($comment) = $_ =~ s/(\#.*$)//;
;
my ($seqname, $source, $feature_type, $start, $end, $score,
$strand, $frame, $attrb_text) = split "\t", $_;
my $attributes = parse_attributes($attrb_text);
$feature_type = $INPUT_FEATURE_MAP->{$feature_type};
#Note here that we're mapping between GTF/GFF and GFF3
#nomeclature for the hash keys
my $feature = {seq_id => $seqname,
source => $source,
type => $feature_type,
start => $start,
end => $end,
score => $score,
strand => $strand,
phase => $frame,
attributes => $attributes,
comment => $comment};
#Feature has a gene_id
if (exists $attributes->{$GTF_ATTRB_MAP->{gene_id}}) {
my $gene_id = $attributes->{$GTF_ATTRB_MAP->{gene_id}}[0];
#Feature has a gene ID and transcript ID
if (exists $attributes->{$GTF_ATTRB_MAP->{trnsc_id}}) {
my $trnsc_id =
$attributes->{$GTF_ATTRB_MAP->{trnsc_id}}[0];
push @{$genes{$seqname}{$gene_id}{$trnsc_id}{$feature_type}},
$feature;
}
else {
push @{$genes{$seqname}{$gene_id}{other}{$feature}},
$feature;
}
}
else {
push @{$features{$feature_type}}, $feature;
}
}
return (\%genes, \%features);
}
#-----------------------------------------------------------------------------
sub parse_attributes {
my $attrb_text = shift;
#Skip it if it's undefined or empty
return if ! defined $attrb_text || $attrb_text =~ /^\s+$/;
#Split the attributes
my @attrb_array = split $ATTRB_DELIMITER, $attrb_text;
#Grab key, value pairs
my %attributes;
ATT:
for my $attrb (@attrb_array) {
my ($key, $value);
if ($attrb =~ $ATTRB_REGEX) {
($key, $value) = ($1, $2);
$value =~ s/\"//g if $value !~ /\s/;
}
else {
print STDERR "ERROR: Missing or non-standard attributes: parse_attributes\n";
next ATT;
}
push @{$attributes{$key}}, $value;
}
return \%attributes;
}
#-----------------------------------------------------------------------------
sub build_genes {
my $genes = shift;
my @genes;
for my $seq_id (keys %{$genes}) {
for my $gene_id (keys %{$genes->{$seq_id}}) {
my $gene = build_gene($genes->{$seq_id}{$gene_id});
push @genes, $gene;
}
}
return \@genes;
}
#-----------------------------------------------------------------------------
sub build_gene {
my $trnscs = shift;
my @trnscs;
TRN:
for my $trnsc_id (keys %{$trnscs}) {
if ($trnsc_id eq 'other') {
#Handle non-transcript gene features
print STDERR "ERROR: Non-transcript gene feature " .
"not supported. Please contact the author for " .
"support: build_gene\n";
next TRN;
}
my $features = $trnscs->{$trnsc_id};
my $trnsc = build_trnsc($features);
push @trnscs, $trnsc;
}
my $gene = validate_and_build_gene(\@trnscs);
return $gene;
}
#-----------------------------------------------------------------------------
sub build_trnsc {
my $features = shift;
my $exons = $features->{exon} || [];
my $CDSs = $features->{CDS} || [];
my $start = $features->{start_codon} || [];
my $stop = $features->{stop_codon} || [];
my $five_UTRs = $features->{five_prime_utr} || [];
my $three_UTRs = $features->{three_prime_utr} || [];
#We require at least CDSs or exons
unless (scalar @{$CDSs} || scalar @{$exons}) {
print STDERR "ERROR: Must have at least exons or CDSs to" .
"build a transcript: build_trnsc\n";
return;
}
#Make start codons if they don't exist
$start = process_start($exons, $CDSs, $start, $five_UTRs)
if ! scalar @{$start};
#Make stop codons if they don't exist
$stop = process_stop($exons, $CDSs, $stop, $three_UTRs)
if ! scalar @{$stop};
#Make UTRs, exons and/or CDSs if they don't exist
($five_UTRs,
$exons,
$CDSs,
$three_UTRs) = process_exon_CDS_UTRs($exons, $CDSs,
$start, $stop,
$five_UTRs,
$three_UTRs);
$features = {exon => $exons,
five_prime_UTR => $five_UTRs,
start_codon => $start,
CDS => $CDSs,
stop_codon => $stop,
three_prime_UTR => $three_UTRs};
#Build and validate the transcripts
my $trnsc = validate_and_finish_trnsc($features);
return $trnsc;
}
#-----------------------------------------------------------------------------
sub process_start {
my ($exons, $CDSs, $start, $five_UTRs) = @_;
my ($start_codon_start, $start_codon_end);
my $strand;
#Get the strand for exons or CDSs
if (scalar @{$exons}) {
$strand = strand($exons->[0]->{strand});
}
elsif (scalar @{$CDSs}) {
$strand = strand($CDSs->[0]->{strand});
}
else {
die "FATAL: Need exons or CDSs to build transcripts: " .
"process_start.\n";
}
#Already have start
if (scalar @{$start}) {
return $start;
}
################################################################################
# Don't infer start codons from CDS unless backed by UTR or exon infered UTR
################################################################################
#Build start from CDS
elsif (scalar @{$CDSs} && scalar @{$exons}) {
$CDSs = sort_features($CDSs, $strand);
my $first_CDS = $CDSs->[0];
$exons = sort_features($exons, $strand);
my $first_exon = $exons->[0];
if ($strand == 1) {
if ($first_exon->{start} < $first_CDS->{start}) {
$start_codon_start = $first_CDS->{start};
#Move start codon out of CDS if config says to
if ($START_IN_CDS == 0) {
$start_codon_start -= 3;
}
}
}
elsif ($strand == -1) {
if ($first_exon->{end} > $first_CDS->{end}) {
$start_codon_start = $first_CDS->{end} - 2;
#Move start codon out of CDS if config says to
if ($START_IN_CDS == 0) {
$start_codon_start += 3;
}
}
}
$start_codon_end = $start_codon_start + 2
if defined $start_codon_start;
}
#Here we assume that if you have 5' UTR and CDS that the start codon
#must be at the begining of the first CDS. To be more rigorous we should
#check the coordinates and be sure that the 5' UTR and CDS are contiguous.
elsif (scalar @{$CDSs} && scalar @{$five_UTRs}) {
$CDSs = sort_features($CDSs, $strand);
my $first_CDS = $CDSs->[0];
if ($strand == 1) {
$start_codon_start = $first_CDS->{start};
#Move start codon out of CDS if config says to
if ($START_IN_CDS == 0) {
$start_codon_start -= 3;
}
}
elsif ($strand == -1) {
$start_codon_start = $first_CDS->{end} - 2;
#Move start codon out of CDS if config says to
if ($START_IN_CDS == 0) {
$start_codon_start += 3;
}
}
$start_codon_end = $start_codon_start + 2;
}
#Build start from UTRs - I haven't seen an example of this yet. Maybe I should
#only create a start codon if it falls within an exon or CDS.
elsif (scalar @{$five_UTRs}) {
die "FATAL: Untested code in process_start. Contact the aurthor for support\n";
$five_UTRs = sort_features($five_UTRs, $strand);
my $last_five_UTR = $five_UTRs->[-1];
$start_codon_start = $strand == 1 ? $last_five_UTR->{start} + 1 :
$last_five_UTR->{end} - 3;
$start_codon_end = $start_codon_start + 2;
}
#No CDSs or UTRs - let's pretend it's non-coding
elsif (scalar @{$exons}) {
return [];
}
else {
return []; # We don't really need to die here some annotations have stop_codon, but not start_codon
# die "FATAL: Invalid feature set: process_start\n";
}
return [] if (! defined $start_codon_start ||
! defined $start_codon_end);
$start = [{start => $start_codon_start,
end => $start_codon_end,
type => 'start_codon',
score => '.',
phase => '0'}];
return $start;
}
#-----------------------------------------------------------------------------
sub process_stop {
my ($exons, $CDSs, $stop, $three_UTRs) = @_;
my ($stop_codon_start, $stop_codon_end);
#Get the strand
my $strand;
if (scalar @{$exons}) {
$strand = strand($exons->[0]->{strand});
}
elsif (scalar @{$CDSs}) {
$strand = strand($CDSs->[0]->{strand});
}
else {
die "FATAL: Need exons or CDSs to build transcripts: " .
"process_stop\n";
}
#If we already have a stop then return it.
if (scalar @{$stop}) {
return $stop;
}
################################################################################
# Don't infer stop codons from CDS unless backed by UTR or exon infered UTR
################################################################################
#Build stop from CDSs
elsif (scalar @{$CDSs}) {
$CDSs = sort_features($CDSs, $strand);
my $last_CDS = $CDSs->[-1];
$exons = sort_features($exons, $strand);
my $last_exon = $exons->[0];
if ($strand == 1) {
if ($last_exon->{end} > $last_CDS->{end}) {
$stop_codon_start = $last_CDS->{end} - 2;
#Move stop codon out of CDS is config says to
if ($STOP_IN_CDS == 0) {
$stop_codon_start += 3;
}
}
}
elsif ($strand == -1) {
if ($last_exon->{start} < $last_CDS->{start}) {
$stop_codon_start = $last_CDS->{start};
#Move stop codon out of CDS is config says to
if ($STOP_IN_CDS == 0) {
$stop_codon_start -= 3;
}
}
}
$stop_codon_end = $stop_codon_start + 2
if defined $stop_codon_end;
}
elsif (scalar @{$CDSs} && scalar @{$three_UTRs}) {
$CDSs = sort_features($CDSs, $strand);
my $last_CDS = $CDSs->[-1];
if ($strand == 1) {
$stop_codon_start = $last_CDS->{end} - 2;
#Move stop codon out of CDS is config says to
if ($STOP_IN_CDS == 0) {
$stop_codon_start += 3;
}
}
elsif ($strand == -1) {
$stop_codon_start = $last_CDS->{start};
#Move stop codon out of CDS is config says to
if ($STOP_IN_CDS == 0) {
$stop_codon_start -= 3;
}
}
$stop_codon_end = $stop_codon_start + 2;
}
#Build stop from UTRs
elsif (scalar @{$three_UTRs}) {
die("FATAL: Untested code: process_stop.\n");
my $strand = strand($three_UTRs->[0]);
$three_UTRs = sort_features($three_UTRs, $strand);
my $first_three_UTR = $three_UTRs->[0];
$stop_codon_start = $strand == 1 ? $first_three_UTR->{start} - 3 :
$first_three_UTR->{end} + 1;
$stop_codon_end = $stop_codon_start + 2;
}
elsif (scalar @{$exons}) {
#Treating this as a non-coding transcript
return [];
}
else {
# We don't really need to die here. Some features may have
# start_codon, but not stop_codon
return [];
#die "FATAL: Invalid feature set: process_stop\n";
}
return [] if (! defined $stop_codon_start ||
! defined $stop_codon_end);
$stop = [{start => $stop_codon_start,
end => $stop_codon_end,
type => 'stop_codon',
score => '.',
phase => '0'}];
return $stop;
}
#-----------------------------------------------------------------------------
sub process_exon_CDS_UTRs {
my ($exons, $CDSs, $start, $stop,
$five_UTRs, $three_UTRs) = @_;
#Check what features we already have so we don't rebuild them
my $have_five_UTRs++ if scalar @{$five_UTRs};
my $have_exons++ if scalar @{$exons};
my $have_start++ if scalar @{$start};
my $have_CDSs++ if scalar @{$CDSs};
my $have_stop++ if scalar @{$stop};
my $have_three_UTRs++ if scalar @{$three_UTRs};
#If CDSs already exist make sure that they include start and stop codons
if (scalar @{$CDSs}) {
include_terminal_codons($CDSs, $start, $stop);
}
#If we already have everything, then return
if ($have_exons && $have_CDSs && ($have_five_UTRs || $have_three_UTRs)) {
return ($five_UTRs, $exons, $CDSs, $three_UTRs);
}
#Build CDSs && UTRs
elsif ($have_exons && ($have_start || $have_stop || $have_CDSs ||
$have_three_UTRs || $have_five_UTRs)) {
# Make sure that evaluate exons can handle only a start OR a stop!!!
for my $exon (@{$exons}) {
my ($five_UTR, $CDS, $three_UTR) =
evaluate_exon($exon, $start->[0], $stop->[0]);
push @{$five_UTRs}, $five_UTR
if scalar keys %{$five_UTR} && ! $have_five_UTRs;
push @{$CDSs}, $CDS if scalar keys %{$CDS} && ! $have_CDSs;
push @{$three_UTRs}, $three_UTR
if scalar keys %{$three_UTR} && ! $have_three_UTRs;
}
}
#Build exons
elsif (!$have_exons && $have_CDSs){
$exons = build_exons($five_UTRs, $start, $CDSs, $stop, $three_UTRs);
}
#Treat as non-coding even if we have a start or stop but not both
elsif ($have_exons && ! $have_CDSs && ! $have_five_UTRs &&
! $have_three_UTRs && ! $have_start && ! $have_stop) {
#Treating this as a non_coding transcript
return ($five_UTRs, $exons, $CDSs, $three_UTRs);
}
else {
die "FATAL: Invalid feature set: process_exon_CDS_UTR\n";
}
return ($five_UTRs, $exons, $CDSs, $three_UTRs);
}
#-----------------------------------------------------------------------------
sub include_terminal_codons {
my ($CDSs, $start, $stop) = @_;
my $strand = strand($CDSs->[0]{strand});
$CDSs = sort_features($CDSs, $strand);
my $first_CDS = $CDSs->[0];
my $last_CDS = $CDSs->[-1];
if ($strand == 1) {
if (scalar @{$start}) {
$first_CDS->{start} = $start->[0]{start}
if $start->[0]{end} + 1 == $first_CDS->{start};
}
#START_IN_CDS was considered when start was built, so it could introduce errors
#to consider it again here
# elsif (defined $START_IN_CDS) {
# $first_CDS->{start} -= 3 if ! $START_IN_CDS;
# }
if (scalar @{$stop}) {
$last_CDS->{end} = $stop->[0]{end}
if $stop->[0]{start} - 1 == $last_CDS->{end};
}
#STOP_IN_CDS was considered when stop was built, so it could introduce errors
#to consider it again here
# elsif (defined $STOP_IN_CDS) {
# $last_CDS->{end} += 3 if ! $STOP_IN_CDS;
# }
}
elsif ($strand == -1) {
if (scalar @{$start}) {
$first_CDS->{end} = $start->[0]{end}
if $start->[0]{start} - 1 == $first_CDS->{end};
}
#START_IN_CDS was considered when start was built, so it could introduce errors
#to consider it again here
# elsif (defined $START_IN_CDS) {
# $first_CDS->{end} += 3 if ! $START_IN_CDS;
# }
if (scalar @{$stop}) {
$last_CDS->{start} = $stop->[0]{start}
if $stop->[0]{end} + 1 == $last_CDS->{start};
}
#STOP_IN_CDS was considered when stop was built, so it could introduce errors
#to consider it again here
# elsif (defined $STOP_IN_CDS) {
# $last_CDS->{start} -= 3 if ! $STOP_IN_CDS;
# }
}
}
#-----------------------------------------------------------------------------
sub evaluate_exon {
my ($exon, $start, $stop) = @_;
my $strand = strand($exon);
my %five_UTR;
my %CDS;
my %three_UTR;
#######################################
# Allow success if missing either start or stop
#######################################
if ($strand == 1) {
#Exon is fully 5' UTR
if (defined $start->{start} &&
$exon->{end} <= $start->{start}) {
$five_UTR{start} = $exon->{start};
$five_UTR{end} = $exon->{end};
}
#Exon stradles start codon
elsif (defined $start->{start} &&
$exon->{start} < $start->{start} &&
$exon->{end} > $start->{start} ) {
$five_UTR{start} = $exon->{start};
$five_UTR{end} = $start->{start} - 1;
if (defined $stop->{start}) {
$CDS{start} = $start->{start};
$CDS{end} = $exon->{end};
$CDS{phase} = 0;
}
}
#Exon is fully CDS
elsif (defined $start->{start} &&
defined $stop->{end} &&
$exon->{start} >= $start->{start} &&
$exon->{end} <= $stop->{end} ) {
$CDS{start} = $exon->{start};
$CDS{end} = $exon->{end};
#Set phase if this is first CDS
if ($exon->{start} == $start->{end} + 1 ||
$exon->{start} == $start->{start} ) {
$CDS{phase} = 0;
}
}
#Exon stradles stop codon
elsif (defined $stop->{end} &&
$exon->{start} < $stop->{end} &&
$exon->{end} > $stop->{end} ) {
$three_UTR{start} = $stop->{end} + 1;
$three_UTR{end} = $exon->{end};
if (defined $start->{start}) {
$CDS{start} = $exon->{start};
$CDS{end} = $stop->{end};
}
}
#Exon is fully 3' UTR
elsif (defined $stop->{end} &&
$exon->{start} >= $stop->{end}) {
$three_UTR{start} = $exon->{start};
$three_UTR{end} = $exon->{end};
}
else {
# die "Fatal error in evaluate_exon\n";
}
}
else {
#Exon is fully 5' UTR
if (defined $start->{end} &&
$exon->{start} >= $start->{end}) {
$five_UTR{start} = $exon->{start};
$five_UTR{end} = $exon->{end};
}
#Exon stradles start codon
elsif (defined $start->{end} &&
$exon->{start} < $start->{end} &&
$exon->{end} > $start->{end} ) {
$five_UTR{start} = $start->{end} + 1;
$five_UTR{end} = $exon->{end};
if (defined $stop->{start}) {
$CDS{start} = $exon->{start};
$CDS{end} = $start->{end};
}
$CDS{phase} = 0;
}
#Exon is fully CDS
elsif (defined $start->{end} &&
defined $stop->{start} &&
$exon->{end} <= $start->{end} &&
$exon->{start} >= $stop->{start} ) {
$CDS{start} = $exon->{start};
$CDS{end} = $exon->{end};
#Set phase 0 if this is first CDS
if ($exon->{end} == $start->{start} - 1 ||
$exon->{end} == $start->{end} ) {
$CDS{phase} = 0;
}
}
#Exon stradles stop codon
elsif (defined $stop->{start} &&
$exon->{end} > $stop->{start} &&
$exon->{start} < $stop->{start} ) {
$three_UTR{start} = $exon->{start};
$three_UTR{end} = $stop->{start} - 1;
if (defined $start->{end}) {
$CDS{start} = $stop->{start};
$CDS{end} = $exon->{end};
}
}
#Exon is fully 3' UTR
elsif (defined $stop->{start} &&
$exon->{end} <= $stop->{start}) {
$three_UTR{start} = $exon->{start};
$three_UTR{end} = $exon->{end};
}
else {
# die "Fatal error in evaluate_exon\n";
}
}
return (\%five_UTR, \%CDS, \%three_UTR);
}
#-----------------------------------------------------------------------------
sub build_exons {
my ($five_UTRs, $start, $CDSs, $stop, $three_UTRs) = @_;
my $strand = $CDSs->[0]{strand};
my @exons;
#Make an exon for every 5' UTR
for my $five_UTR (@{$five_UTRs}) {
push @exons, {start => $five_UTR->{start},
end => $five_UTR->{end},
type => 'exon',
score => '.',
strand => $strand,
phase => '.'};
}
#Make and exon for every CDS
$CDSs = sort_features($CDSs);
for my $CDS (@{$CDSs}) {
my $exon = {start => $CDS->{start},
end => $CDS->{end},
type => 'exon',
score => '.',
strand => $strand,
phase => '.'};
#START_IN_CDS and STOP_IN_CDS were already considered when start and stop were
#constructed and CDSs were adjusted in include_terminal_codons
=head1
if ($i == 0 &&
$START_IN_CDS == 0) {
if ($strand == 1) {
$exon->start = $start->[0]{start};
}
else {
$exon->end = $start->[0]{end};
}
}
if ($i == (scalar @{$CDSs} - 1) &&
$STOP_IN_CDS == 0) {
if ($strand == 1) {
$exon->{end} = $stop->[0]{end};
}
else {
$exon->{start} = $stop->[0]{start}
}
}
=cut
push @exons, $exon;
}
#Make an exon for every 3' UTR
for my $three_UTR (@{$three_UTRs}) {
push @exons, {start => $three_UTR->{start},
end => $three_UTR->{end},
type => 'exon',
score => '.',
strand => $strand,
phase => '.'};
}
#Merge any contiguous exons (i.e. from UTR & CDS neighbors)
my $exons = sort_features(\@exons, 1);
my @merged_exons = shift @{$exons};
while (my $exon = shift @{$exons}) {
if ($exon->{start} <= $merged_exons[-1]{end} + 1) {
$merged_exons[-1]{end} = $exon->{end};
}
else {
push @merged_exons, $exon;
}
}
return \@merged_exons;
}
#-----------------------------------------------------------------------------
sub validate_and_finish_trnsc {
my $features = shift;
my ($seq_id, $source, $strand, $gene_id, $gene_name,
$trnsc_id, $trnsc_name);
#Find default parameters from either exons or CDSs
TYPE:
for my $type ( qw|exon CDS| ) {
for my $feature (@{$features->{$type}}) {
$seq_id ||= $feature->{seq_id};
$source ||= $feature->{source};
$strand ||= $feature->{strand};
$gene_id ||=
$feature->{attributes}{$GTF_ATTRB_MAP->{gene_id}};
$gene_name ||=
$feature->{attributes}{$GTF_ATTRB_MAP->{gene_name}};
$trnsc_id ||=
$feature->{attributes}{$GTF_ATTRB_MAP->{trnsc_id}};
$trnsc_name ||=
$feature->{attributes}{$GTF_ATTRB_MAP->{trnsc_name}};
last TYPE if ! grep {! defined $_} ($seq_id, $source,
$strand, $gene_name,
$trnsc_id, $trnsc_name);
}
}
#Flag if we have any coding features (CDS, start_codon, stop_codon)
my $coding_flag;
#Min and max for transcript boundaries
my ($min, $max);
for my $feature_type (keys %{$features}) {
my $count;
#For keeping track of the phase for the next CDS.
my $next_phase = '.';
#Sort the features
$features->{$feature_type} =
sort_features($features->{$feature_type},
strand($strand));
for my $feature (@{$features->{$feature_type}}) {
#min and max to calculate transcript boundaries
$min = ! defined $min ?
$feature->{start} :
$min > $feature->{start} ?
$feature->{start} :
$min;
$max = ! defined $max ?
$feature->{end} :
$max < $feature->{end} ?
$feature->{end} :
$max;
#Set the flag if we see indications of coding features
if (grep {$feature_type eq $_} qw(CDS start_codon
stop_codon)) {
$coding_flag++ if $feature->{start};
}
#Calculate CDS phases.
if ($feature_type eq 'CDS') {
($feature, $next_phase) = CDS_phase($feature, $next_phase);
}
#Set parameters to defaults for all features that
#don't already have them set
if (! defined $feature->{seq_id}) {
$feature->{seq_id} = $seq_id;
}
elsif ($feature->{seq_id} ne $seq_id) {
print STDERR "ERROR: seq_id conflict: " .
"validate_and_finish_trnsc\n";
}
if (! defined $feature->{source}) {
$feature->{source} = $source;
}
elsif ($feature->{source} ne $source) {
print STDERR "ERROR: source conflict: " .
"validate_and_finish_trnsc\n";
}
if (! defined $feature->{type}) {
$feature->{type} = $feature_type;
}
elsif ($feature->{type} ne $feature_type) {
print STDERR "ERROR: type conflict: " .
"validate_and_finish_trnsc\n";
}
if (! defined $feature->{strand}) {
$feature->{strand} = $strand;
}
elsif ($feature->{strand} ne $strand) {
print STDERR "ERROR: strand conflict: " .
"validate_and_finish_trnsc\n";
}
if (! defined $feature->{score}) {
$feature->{score} = '.';
}
if (! defined $feature->{phase}) {
$feature->{phase} = '.';
}
#Set attributes
$feature->{attributes} =
{parent => $trnsc_id,
id => ["$feature_type:" . $trnsc_id->[0] .
":" . ++$count]};
}
}
my $trnsc_type = $coding_flag ? 'mRNA' : 'transcript';
my $attributes = {parent => $gene_id,
parent_name => $gene_name,
id => $trnsc_id,
name => $trnsc_name};
my $trnsc = {seq_id => $seq_id,
source => $source,
type => $trnsc_type,
start => $min,
end => $max,
score => '.',
strand => $strand,
phase => '.',
attributes => $attributes,
features => $features};
return $trnsc;
}
#-----------------------------------------------------------------------------
sub CDS_phase {
my ($feature, $next_phase) = @_;
#If phase isn't already valid assign it
if (! defined $feature->{phase} ||
$feature->{phase} !~ /^0|1|2$/) {
$feature->{phase} = $next_phase;
}
#If phase is valid, calculate the next phase
if ($feature->{phase} =~ /^0|1|2$/) {
my $length = ($feature->{end} -
$feature->{start}) + 1;
# my $hang_3 = $length % 3; # 3' overhang
# my $hang_5 = 3 - $hang_3; # 5' overhang
#
# #The next phase is equal to this phase
# #plus the modulus 3 of the length wrapped
# #at 2.
# $next_phase = $feature->{phase} + $hang_5;
# $next_phase -= 3 if $next_phase > 2;
# This was update 5/24/10 in response to an
# e-mail from Leighton Prichard regarding
# errors in the GFF3 spec. The code above
# calculates the phase correctly, but the
# formula suggested by Leighton is cleaner.
$next_phase = ($feature->{phase} - $length) % 3;
}
return ($feature, $next_phase);
}