-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEval.pm
3494 lines (3318 loc) · 85 KB
/
Eval.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
use strict;
use GTF;
package Eval;
######################################################################
#
# Definition of statistics and their data structures
#
######################################################################
# top level evaluate data structures
sub get_level_list{
return qw (Gene
Transcript
Exon
Nuc
Signal);
}
sub get_list_struct{
my @level_list = get_level_list();
my @gene_list = get_gene_type_list();
my @tx_list = get_tx_type_list();
my @exon_list = get_exon_type_list();
my @nuc_list = get_nuc_type_list();
my @signal_list = get_signal_type_list();
my @gene_sub_list = get_gene_stat_list();
my @tx_sub_list = get_tx_stat_list();
my @exon_sub_list = get_exon_stat_list();
my @nuc_sub_list = get_nuc_stat_list();
my @signal_sub_list = get_signal_stat_list();
my %struct = (Levels => \@level_list,
Gene => {Type => \@gene_list,
Stat => \@gene_sub_list},
Transcript => {Type => \@tx_list,
Stat => \@tx_sub_list},
Exon => {Type => \@exon_list,
Stat => \@exon_sub_list},
Nuc => {Type => \@nuc_list,
Stat => \@nuc_sub_list},
Signal => {Type => \@signal_list,
Stat => \@signal_sub_list});
return %struct;
}
sub get_general_list_struct{
my @level_list = get_level_list();
my @gene_list = get_gene_type_list();
my @tx_list = get_tx_type_list();
my @exon_list = get_exon_type_list();
my @nuc_list = get_nuc_type_list();
my @signal_list = get_signal_type_list();
my @gene_sub_list = get_gene_general_stat_list();
my @tx_sub_list = get_tx_general_stat_list();
my @exon_sub_list = get_exon_general_stat_list();
my @nuc_sub_list = get_nuc_general_stat_list();
my @signal_sub_list = get_signal_general_stat_list();
my %struct = (Levels => \@level_list,
Gene => {Type => \@gene_list,
Stat => \@gene_sub_list},
Transcript => {Type => \@tx_list,
Stat => \@tx_sub_list},
Exon => {Type => \@exon_list,
Stat => \@exon_sub_list},
Nuc => {Type => \@nuc_list,
Stat => \@nuc_sub_list},
Signal => {Type => \@signal_list,
Stat => \@signal_sub_list});
return %struct;
}
sub get_stats_struct{
my %gene = _get_gene_type_struct();
my %tx = _get_tx_type_struct();
my %exon = _get_exon_type_struct();
my %nuc = _get_nuc_type_struct();
my %signal = _get_signal_type_struct();
return (Gene => \%gene,
Exon => \%exon,
Transcript => \%tx,
Nuc => \%nuc,
Signal => \%signal);
}
# gene level evaluate data structures
sub get_gene_type_list{
return qw (All);
}
sub get_gene_stat_list{
my @list;
my @genstat = get_gene_general_stat_list();
foreach my $stat (@genstat){
push @list, $stat;
}
my @add = get_gene_stat_type_list();
my @substat = get_gene_substat_list();
foreach my $add (@add){
foreach my $substat (@substat){
push @list, $add."_".$substat;
}
}
return @list;
}
sub get_gene_general_stat_list{
return qw(Count
Ann_Count
Total_Transcripts
Transcripts_Per);
}
sub get_gene_stat_type_list{
return qw(Consistent
Exact
Full_Exact
Genomic_Overlap
CDS_Overlap
All_Introns
Exact_UTR5
Exact_UTR3
Exact_Intron
Exact_Exon
Start_Codon
Stop_Codon
Start_Stop);
}
sub get_gene_substat_list{
return qw(Pred_Count
Ann_Count
Specificity
Sensitivity);
}
sub _get_gene_type_hash{
my @list = get_gene_type_list();
my %gene;
foreach my $type (@list){
$gene{$type} = 0;
}
return %gene;
}
sub _get_gene_stat_hash{
my @list = get_gene_stat_list();
my %gene;
foreach my $type (@list){
$gene{$type} = 0;
}
return %gene;
}
sub _get_gene_type_struct{
my @list = get_gene_type_list();
my %gene;
foreach my $type (@list){
my %substruct = _get_gene_stat_hash();
$gene{$type} = \%substruct;
}
return %gene;
}
# tx level evaluate data structures
sub get_tx_type_list{
return qw (All
Complete
Incomplete
Single_Exon);
}
sub get_tx_stat_list{
my @list;
my @genstat = get_tx_general_stat_list();
foreach my $stat (@genstat){
push @list, $stat;
}
my @add = get_tx_stat_type_list();
my @substat = get_tx_substat_list();
foreach my $add (@add){
foreach my $substat (@substat){
push @list, $add."_".$substat;
}
}
return @list;
}
sub get_tx_general_stat_list{
return qw(Count
Ann_Count
Average_Length
Median_Length
Total_Length
Average_Coding_Length
Median_Coding_Length
Total_Coding_Length
Average_Score
Total_Score
Ave_Exons_Per
Med_Exons_Per
Total_Exons);
}
sub get_tx_stat_type_list{
return get_gene_stat_type_list();
}
sub get_tx_substat_list{
return qw(Pred_Count
Ann_Count
Specificity
Sensitivity);
}
sub _get_tx_type_hash{
my @list = get_tx_type_list();
my %tx;
foreach my $type (@list){
$tx{$type} = 0;
}
return %tx;
}
sub _get_tx_stat_hash{
my @list = get_tx_stat_list();
my %tx;
foreach my $type (@list){
$tx{$type} = 0;
}
return %tx;
}
sub _get_tx_type_struct{
my @list = get_tx_type_list();
my %tx;
foreach my $type (@list){
my %substruct = _get_tx_stat_hash();
$tx{$type} = \%substruct;
}
return %tx;
}
# exon level evaluate data structures
sub get_exon_type_list{
return qw(All
Initial
Internal
Terminal
Single
UTR3
UTR5
Intron
InframeOptional
FrameshiftOptional);
}
sub get_exon_stat_list{
my @list;
my @genstat = get_exon_general_stat_list();
foreach my $stat (@genstat){
push @list, $stat;
}
my @add = get_exon_stat_type_list();
my @substat = get_exon_substat_list();
foreach my $add (@add){
foreach my $substat (@substat){
push @list, $add."_".$substat;
}
}
return @list;
}
sub get_exon_general_stat_list{
return qw(Count
Ann_Count
Average_Length
Median_Length
Total_Length
Average_Score
Total_Score);
}
sub get_exon_stat_type_list{
return qw(Correct
Overlap
Overlap_80p
Splice_5
Splice_3);
}
sub get_exon_substat_list{
return qw(Pred_Count
Ann_Count
Specificity
Sensitivity);
}
sub _get_exon_type_hash{
my @list = get_exon_type_list();
my %exon;
foreach my $type (@list){
$exon{$type} = 0;
}
return %exon;
}
sub _get_exon_stat_hash{
my @list = get_exon_stat_list();
my %exon;
foreach my $type (@list){
$exon{$type} = 0;
}
return %exon;
}
sub _get_exon_type_struct{
my @list = get_exon_type_list();
my %exon;
foreach my $type (@list){
my %substruct = _get_exon_stat_hash();
$exon{$type} = \%substruct;
}
return %exon;
}
# nuc level evaluate data structures
sub get_nuc_type_list{
return get_exon_type_list();
}
sub get_nuc_stat_list{
my @list;
my @genstat = get_nuc_general_stat_list();
foreach my $stat (@genstat){
push @list, $stat;
}
my @add = get_nuc_stat_type_list();
my @substat = get_nuc_substat_list();
foreach my $add (@add){
foreach my $substat (@substat){
push @list, $add."_".$substat;
}
}
return @list;
}
sub get_nuc_general_stat_list{
return qw(Count
Ann_Count);
}
sub get_nuc_stat_type_list{
return qw (Correct);
}
sub get_nuc_substat_list{
return qw(Pred_Count
Ann_Count
Specificity
Sensitivity);
}
sub _get_nuc_type_hash{
my @list = get_nuc_type_list();
my %nuc;
foreach my $type (@list){
$nuc{$type} = 0;
}
return %nuc;
}
sub _get_nuc_stat_hash{
my @list = get_nuc_stat_list();
my %nuc;
foreach my $type (@list){
$nuc{$type} = 0;
}
return %nuc;
}
sub _get_nuc_type_struct{
my @list = get_nuc_type_list();
my %nuc;
foreach my $type (@list){
my %substruct = _get_nuc_stat_hash();
$nuc{$type} = \%substruct;
}
return %nuc;
}
# signal level evaluate data structures
sub get_signal_type_list{
return qw (Splice_Acceptor
Splice_Donor
Start_Codon
Stop_Codon);
}
sub get_signal_stat_list{
my @list;
my @genstat = get_signal_general_stat_list();
foreach my $stat (@genstat){
push @list, $stat;
}
my @add = get_signal_stat_type_list();
my @substat = get_signal_substat_list();
foreach my $add (@add){
foreach my $substat (@substat){
push @list, $add."_".$substat;
}
}
return @list;
}
sub get_signal_general_stat_list{
return qw(Count
Ann_Count);
}
sub get_signal_stat_type_list{
return qw (Correct);
}
sub get_signal_substat_list{
return qw(Pred_Count
Ann_Count
Specificity
Sensitivity);
}
sub _get_signal_type_hash{
my @list = get_signal_type_list();
my %signal;
foreach my $type (@list){
$signal{$type} = 0;
}
return %signal;
}
sub _get_signal_stat_hash{
my @list = get_signal_stat_list();
my %signal;
foreach my $type (@list){
$signal{$type} = 0;
}
return %signal;
}
sub _get_signal_type_struct{
my @list = get_signal_type_list();
my %signal;
foreach my $type (@list){
my %substruct = _get_signal_stat_hash();
$signal{$type} = \%substruct;
}
return %signal;
}
######################################################################
#
# evalutate($ann,$preds,$ann_name,$pred_names,$verbose)
# Compares one set of annotation gtfs to one or more sets of
# prediction gtfs.
#
# Parameters:
# $ann - reference to set(array) of annotation gtfs
# $preds - reference to array of sets(arrays) of prediction gtfs
# $verbose - boolean turns verbose mode on/off
#
######################################################################
sub evaluate{
my ($ann,$preds,$verbose) = @_;
unless(defined($verbose)){
$verbose = 0;
}
if($verbose){
print STDERR "Starting Evaluation Calculations\n";
}
my $start_time = time;
my @a_genes;
my @p_genes;
my $a_data;
my @p_data;
foreach my $ann_gtf (@$ann){
my $genes = $ann_gtf->genes;
push @a_genes, $genes;
}
$a_data = _get_stats(\@a_genes);
_calculate_stats($a_data);
foreach my $pred (@$preds){
my $pred_genes;
foreach my $pred_gtf (@$pred){
my $genes = $pred_gtf->genes;
push @$pred_genes, $genes;
}
push @p_genes, $pred_genes;
my %stats_struct = get_stats_struct();
push @p_data, \%stats_struct;
}
for(my $p = 0;$p <= $#p_genes;$p++){
for(my $i = 0;$i <= $#a_genes;$i++){
compare_gene_lists($a_genes[$i],$p_genes[$p][$i],$p_data[$p]);
}
_calculate_stats($p_data[$p]);
}
my $end_time = time;
my $total_time = $end_time - $start_time + 1;
if($verbose){
print_time($total_time);
}
return ($a_data,@p_data);
}
# functions to initialize the data
sub _get_gene_tag{
my %tag;
my @types = get_gene_type_list();
foreach my $type (@types){
$tag{$type} = 0;
}
my @stats = get_gene_stat_type_list();
foreach my $stat (@stats){
$tag{$stat} = 0;
}
return \%tag;
}
sub _get_tx_tag{
my %tag;
my @types = get_tx_type_list();
foreach my $type (@types){
$tag{$type} = 0;
}
my @stats = get_tx_stat_type_list();
foreach my $stat (@stats){
$tag{$stat} = 0;
}
return \%tag;
}
sub _get_exon_tag{
my %tag;
my @types = get_exon_type_list();
foreach my $type (@types){
$tag{$type} = 0;
}
my @stats = get_exon_stat_type_list();
foreach my $stat (@stats){
$tag{$stat} = 0;
}
return \%tag;
}
sub _init_gene_tag{
my ($gene) = @_;
my $gene_tag = _get_gene_tag();
$$gene_tag{All} = 1;
$gene->set_tag($gene_tag);
foreach my $tx (@{$gene->transcripts}){
_init_tx_tag($tx);
}
}
sub _init_tx_tag{
my ($tx) = @_;
my $tx_tag = _get_tx_tag();
$$tx_tag{All} = 1;
my $starts = $tx->start_codons;
my $stops = $tx->stop_codons;
my $cds = $tx->cds;
if(($#$starts >= 0) && ($#$stops >= 0)){
$$tx_tag{Complete} = 1;
if($#$cds == 0){
$$tx_tag{Single_Exon} = 1;
}
else{
$$tx_tag{Single_Exon} = 0;
}
}
else{
$$tx_tag{Incomplete} = 1;
$$tx_tag{Single_Exon} = 0;
}
$tx->set_tag($tx_tag);
foreach my $cds (@{$tx->cds}){
_init_exon_tag($cds);
}
foreach my $utr (@{$tx->utr3}){
_init_utr3_tag($utr);
}
foreach my $utr (@{$tx->utr5}){
_init_utr5_tag($utr);
}
foreach my $intron (@{$tx->introns}){
_init_intron_tag($intron);
}
}
sub _init_exon_tag{
my ($cds) = @_;
my $cds_tag = _get_exon_tag;
$$cds_tag{All} = 1;
my $type = $cds->subtype;
$$cds_tag{$type} = 1;
my $ase = $cds->ase; # rpz add a tag for the alternative splicing
$$cds_tag{$ase} = 1 if($ase); # event if one exists. Exons are put in
$cds->set_tag($cds_tag); # both buckets (subtype and ase).
}
sub _init_utr5_tag{
my ($utr) = @_;
my $u_tag = _get_exon_tag;
$$u_tag{UTR5} = 1;
$utr->set_tag($u_tag);
}
sub _init_utr3_tag{
my ($utr) = @_;
my $u_tag = _get_exon_tag;
$$u_tag{UTR3} = 1;
$utr->set_tag($u_tag);
}
sub _init_intron_tag{
my ($intron) = @_;
my $i_tag = _get_exon_tag;
$$i_tag{Intron} = 1;
$intron->set_tag($i_tag);
}
# free the memory
sub _clear_gene_tag{
my ($gene) = @_;
$gene->set_tag();
foreach my $tx (@{$gene->transcripts}){
_clear_tx_tag($tx);
}
}
sub _clear_tx_tag{
my ($tx) = @_;
$tx->set_tag();
foreach my $cds (@{$tx->cds}){
_clear_exon_tag($cds);
}
foreach my $utr (@{$tx->utr5}){
_clear_exon_tag($utr);
}
foreach my $utr (@{$tx->utr3}){
_clear_exon_tag($utr);
}
foreach my $intron (@{$tx->introns}){
_clear_exon_tag($intron);
}
}
sub _clear_exon_tag{
my ($exon) = @_;
$exon->set_tag();
}
# functions to collect the data from the tags
sub _collect_gene_stats{
my ($genes,$data) = @_;
my @all_txs;
foreach my $gene (@$genes){
my $info = $gene->tag;
my $txs = $gene->transcripts;
my @types = get_gene_type_list;
my @stats = get_gene_stat_type_list;
foreach my $type (@types){
if($$info{$type}){
$$data{Gene}{$type}{Count}++;
$$data{Gene}{$type}{Total_Transcripts} += $#$txs + 1;
foreach my $stat (@stats){
if($$info{$stat}){
$$data{Gene}{$type}{$stat."_Pred_Count"}++;
}
}
}
}
push @all_txs, @$txs;
}
@all_txs = sort {$a->start <=> $b->start || $a->stop <=> $b->stop} @all_txs;
_collect_tx_stats(\@all_txs,$data);
}
sub _collect_ann_gene_stats{
my ($genes,$data) = @_;
my @all_txs;
foreach my $gene (@$genes){
my $info = $gene->tag;
my $txs = $gene->transcripts;
my @types = get_gene_type_list;
my @stats = get_gene_stat_type_list;
foreach my $type (@types){
if($$info{$type}){
$$data{Gene}{$type}{Ann_Count}++;
foreach my $stat (@stats){
if($$info{$stat}){
$$data{Gene}{$type}{$stat."_Ann_Count"}++;
}
}
}
}
push @all_txs, @$txs;
}
@all_txs = sort {$a->start <=> $b->start || $a->stop <=> $b->stop} @all_txs;
_collect_ann_tx_stats(\@all_txs,$data);
}
sub _collect_tx_stats{
my ($txs,$data) = @_;
my @all_cds;
my @all_introns;
my @all_utr5;
my @all_utr3;
foreach my $tx (@$txs){
my $info = $tx->tag;
my $cds = $tx->cds;
my $introns = $tx->introns;
my $len = $tx->length;
my $clen = $tx->coding_length;
my $score = $tx->score;
my @types = get_tx_type_list;
my @stats = get_tx_stat_type_list;
my $starts = $tx->start_codons;
my $stops = $tx->stop_codons;
if($#$starts >= 0){
$$data{Signal}{Start_Codon}{Count}++;
if($$info{Start_Codon}){
$$data{Signal}{Start_Codon}{Correct_Pred_Count}++;
}
}
if($#$stops >= 0){
$$data{Signal}{Stop_Codon}{Count}++;
if($$info{Stop_Codon}){
$$data{Signal}{Stop_Codon}{Correct_Pred_Count}++;
}
}
foreach my $type (@types){
if($$info{$type}){
$$data{Transcript}{$type}{Count}++;
$$data{Transcript}{$type}{Total_Length} += $len;
push @{$$data{Transcript}{$type}{Length_Array}}, $len;
$$data{Transcript}{$type}{Total_Coding_Length} += $clen;
push @{$$data{Transcript}{$type}{Coding_Length_Array}}, $clen;
$$data{Transcript}{$type}{Total_Exons} += $#$cds + 1;
push @{$$data{Transcript}{$type}{Exons_Per_Array}}, $#$cds + 1;
$$data{Transcript}{$type}{Total_Score} += $score;
foreach my $stat (@stats){
if($$info{$stat}){
$$data{Transcript}{$type}{$stat."_Pred_Count"}++;
}
}
}
}
push @all_cds, @$cds;
push @all_introns, @$introns;
push @all_utr5, @{$tx->utr5};
push @all_utr3, @{$tx->utr3};
}
@all_cds = sort {$a->start <=> $b->start || $a->stop <=> $b->stop} @all_cds;
@all_utr5 = sort {$a->start <=> $b->start || $a->stop <=> $b->stop} @all_utr5;
@all_utr3 = sort {$a->start <=> $b->start || $a->stop <=> $b->stop} @all_utr3;
@all_introns = sort {$a->start <=> $b->start || $a->stop <=> $b->stop} @all_introns;
_collect_exon_stats(\@all_cds,$data);
_collect_exon_stats(\@all_utr5,$data);
_collect_exon_stats(\@all_utr3,$data);
_collect_exon_stats(\@all_introns,$data);
}
sub _collect_ann_tx_stats{
my ($txs,$data) = @_;
my @all_cds;
my @all_introns;
my @all_utr5;
my @all_utr3;
foreach my $tx (@$txs){
my $info = $tx->tag;
my $cds = $tx->cds;
my $introns = $tx->introns;
my $len = $tx->length;
my $clen = $tx->coding_length;
my $score = $tx->score;
my @types = get_tx_type_list;
my @stats = get_tx_stat_type_list;
my $starts = $tx->start_codons;
my $stops = $tx->stop_codons;
if($#$starts >= 0){
$$data{Signal}{Start_Codon}{Ann_Count}++;
if($$info{Start_Codon}){
$$data{Signal}{Start_Codon}{Correct_Ann_Count}++;
}
}
if($#$stops >= 0){
$$data{Signal}{Stop_Codon}{Ann_Count}++;
if($$info{Stop_Codon}){
$$data{Signal}{Stop_Codon}{Correct_Ann_Count}++;
}
}
foreach my $type (@types){
if($$info{$type}){
$$data{Transcript}{$type}{Ann_Count}++;
foreach my $stat (@stats){
if($$info{$stat}){
$$data{Transcript}{$type}{$stat."_Ann_Count"}++;
}
}
}
}
push @all_cds, @$cds;
push @all_utr5, @{$tx->utr5};
push @all_utr3, @{$tx->utr3};
push @all_introns, @$introns;
}
@all_cds = sort {$a->start <=> $b->start || $a->stop <=> $b->stop} @all_cds;
@all_utr5 = sort {$a->start <=> $b->start || $a->stop <=> $b->stop} @all_utr5;
@all_utr3 = sort {$a->start <=> $b->start || $a->stop <=> $b->stop} @all_utr3;
@all_introns = sort {$a->start <=> $b->start || $a->stop <=> $b->stop} @all_introns;
_collect_ann_exon_stats(\@all_cds,$data);
_collect_ann_exon_stats(\@all_utr5,$data);
_collect_ann_exon_stats(\@all_utr3,$data);
_collect_ann_exon_stats(\@all_introns,$data);
}
sub _collect_exon_stats{
my ($exons,$data) = @_;
my @types = get_exon_type_list;
my @stats = get_exon_stat_type_list;
my @skip;
my %all_nucs;
my %match_nucs;
foreach my $type (@types){
$all_nucs{$type} = [];
$match_nucs{$type} = [];
}
for(my $i = 0;$i <= $#$exons;$i++){
my $old_info = $$exons[$i]->tag;
foreach my $type (@types){
if($$old_info{$type}){
push @{$all_nucs{$type}}, [$$exons[$i]->start,$$exons[$i]->stop];
if(defined($$old_info{Correct_Nucs})){
push @{$match_nucs{$type}}, @{$$old_info{Correct_Nucs}};
}
}
}
if($$exons[$i]->equals($$exons[$i+1])){
my $new_info = $$exons[$i+1]->tag;
foreach my $key (keys %$old_info){
unless($key eq "Correct_Nucs"){
if($$old_info{$key}){
$$new_info{$key} = 1;
}
}
}
$skip[$i] = 1;
}
else{
$skip[$i] = 0;
}
}
foreach my $type (@types){
$match_nucs{$type} = [sort {$$a[0] <=> $$b[0] || $$a[1] <=> $$b[1]} @{$match_nucs{$type}}];
$all_nucs{$type} = [sort {$$a[0] <=> $$b[0] || $$a[1] <=> $$b[1]} @{$all_nucs{$type}}];
my $all_count = 0;
my $match_count = 0;
my $last_all = -1;
my $last_match = -1;
foreach my $n (@{$all_nucs{$type}}){
if($$n[1] > $last_all){
$all_count += $$n[1] - max($$n[0],$last_all+1) + 1;
$last_all = $$n[1];
}
}
foreach my $n (@{$match_nucs{$type}}){
if($$n[1] > $last_match){
$match_count += $$n[1] - max($$n[0],$last_match+1) + 1;
$last_match = $$n[1];
}
}
$$data{Nuc}{$type}{Count} += $all_count;
$$data{Nuc}{$type}{Correct_Pred_Count} += $match_count;
}
for(my $i = 0;$i <= $#$exons;$i++){
my $exon = $$exons[$i];
if($skip[$i]){
next;
}
my $info = $exon->tag;
my $len = $exon->length;
my $score = $exon->score;
my $cnucs = 0;
foreach my $interval (@{$$info{Correct_Nucs}}){
$cnucs += $$interval[1] - $$interval[0] + 1;
}
if($$info{Initial} || $$info{Internal}){
$$data{Signal}{Splice_Donor}{Count}++;
if($$info{Splice_3}){
$$data{Signal}{Splice_Donor}{Correct_Pred_Count}++;
}
}
if($$info{Terminal} || $$info{Internal}){
$$data{Signal}{Splice_Acceptor}{Count}++;
if($$info{Splice_5}){
$$data{Signal}{Splice_Acceptor}{Correct_Pred_Count}++;
}
}
foreach my $type (@types){
if($$info{$type}){
$$data{Exon}{$type}{Count}++;
$$data{Exon}{$type}{Total_Length} += $len;
push @{$$data{Exon}{$type}{Length_Array}}, $len;
$$data{Exon}{$type}{Total_Score} += $score;
foreach my $stat (@stats){
if($$info{$stat}){
$$data{Exon}{$type}{$stat."_Pred_Count"}++;
}
}
}
}
}
}
sub _collect_ann_exon_stats{
my ($exons,$data) = @_;
my @types = get_exon_type_list;
my @stats = get_exon_stat_type_list;
my @skip;
my %all_nucs;
my %match_nucs;
foreach my $type (@types){
$all_nucs{$type} = [];
$match_nucs{$type} = [];
}
for(my $i = 0;$i <= $#$exons;$i++){
my $old_info = $$exons[$i]->tag;
foreach my $type (@types){
if($$old_info{$type}){
push @{$all_nucs{$type}}, [$$exons[$i]->start,$$exons[$i]->stop];
if(defined($$old_info{Correct_Nucs})){
push @{$match_nucs{$type}}, @{$$old_info{Correct_Nucs}};
}
}
}
if($$exons[$i]->equals($$exons[$i+1])){
my $new_info = $$exons[$i+1]->tag;
foreach my $key (keys %$old_info){
unless($key eq "Correct_Nucs"){
if($$old_info{$key}){
$$new_info{$key} = 1;
}
}
}
$skip[$i] = 1;
}
else{
$skip[$i] = 0;
}
}
foreach my $type (@types){
$match_nucs{$type} = [sort {$$a[0] <=> $$b[0] || $$a[1] <=> $$b[1]} @{$match_nucs{$type}}];
$all_nucs{$type} = [sort {$$a[0] <=> $$b[0] || $$a[1] <=> $$b[1]} @{$all_nucs{$type}}];
my $all_count = 0;
my $match_count = 0;
my $last_all = -1;
my $last_match = -1;
foreach my $n (@{$all_nucs{$type}}){
if($$n[1] > $last_all){
$all_count += $$n[1] - max($$n[0],$last_all+1) + 1;
$last_all = $$n[1];
}
}
foreach my $n (@{$match_nucs{$type}}){
if($$n[1] > $last_match){
$match_count += $$n[1] - max($$n[0],$last_match+1) + 1;
$last_match = $$n[1];
}
}
$$data{Nuc}{$type}{Ann_Count} += $all_count;
$$data{Nuc}{$type}{Correct_Ann_Count} += $match_count;
}
for(my $i = 0;$i <= $#$exons;$i++){
my $exon = $$exons[$i];
if($skip[$i]){
next;
}
my $info = $exon->tag;
my $len = $exon->length;
my $score = $exon->score;
my $cnucs = 0;
foreach my $interval (@{$$info{Correct_Nucs}}){
$cnucs += $$interval[1] - $$interval[0] + 1;
}