-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace_recalling.pl
executable file
·2114 lines (1547 loc) · 64.4 KB
/
trace_recalling.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 -w
use strict;
use File::Basename;
use Getopt::Long;
use GTF;
use EGlite;
# hashes I will use later to ambiguate and disambiguate pairs of base calls
my %ambig_hash;
populate_ambig_hash(\%ambig_hash);
my %disambig_hash;
populate_disambig_hash(\%disambig_hash);
my %type_hash;
populate_type_hash(\%type_hash);
my $mode;
GetOptions( "mode=s" => \$mode);
# input files
my ($trace_file, $top_sequence_file) = @ARGV;
my $num_of_arguments = @ARGV;
my $usage = "trace_recalling.pl [--mode=single,<numeric threshold>] <trace file> <genomic sequence file>";
if($num_of_arguments != 2) {
die($usage);
}
my ($trace_base, $trace_dir, $trace_ext) = fileparse($trace_file,'\..*');
my $trace_base_and_ext = $trace_base.$trace_ext;
my $expt_dir = $trace_dir.$trace_base_and_ext."_dir";
if(!(-e $expt_dir)) {
system("mkdir $expt_dir");
}
my $basename = $expt_dir."/".$trace_base.$trace_ext;
if(defined($mode)) {
if($mode =~ /single,([\d\.]+)/) {
my $threshold = $1;
my $sequence_file = $top_sequence_file;
# build neccessary filenames for later
my $poly = $expt_dir."/".$trace_base_and_ext.".poly";
my $ambig_file = $basename.".ambig";
my $recall_file = $basename.".recall";
my $first_align = $basename.".first_align";
my $second_align = $basename.".second_align";
my $reverse_complement_flag;
# get the original poly file and make an internal representation of it
system( "phred -dd $expt_dir $trace_file");
# create poly hash and set initial threshold for calling ambiguity codes
my %poly_hash;
create_poly_hash($poly, \%poly_hash, 1/$threshold);
# generate ambiguity sequence the way original trace recalling does it (no thresholding at all so far)
my $ambig_seq = blindly_generate_ambig_sequence(\%poly_hash, \%ambig_hash);
# dump ambiguity sequence to a file
open(AMBIG, ">$ambig_file");
print AMBIG ">${ambig_file}\n";
print AMBIG $ambig_seq, "\n";
close(AMBIG);
# run est_genome_ambig to generate the first align file
system( "est_genome_ambig -genome $sequence_file -est $ambig_file -mismatch 6 -gap_panalty 6 -match 2 -splice_penalty 20 -intron_penalty 40 -align -minscore 0 > $first_align");
my $first_align_report = get_estgenome_report($first_align);
if($first_align_report->genomeDirection eq "forward" && $first_align_report->estDirection eq "forward") {
$reverse_complement_flag = 0;
}
elsif($first_align_report->genomeDirection eq "forward" && $first_align_report->estDirection eq "reversed") {
$reverse_complement_flag = 1;
# in this case need to reverse complement the sequence to make alignment foward to foward
my $new_sequence_file = $sequence_file."_rc";
system("reverse_complement.pl $sequence_file > $new_sequence_file");
$sequence_file = $new_sequence_file;
system( "est_genome_ambig -mode forward -genome $sequence_file -est $ambig_file -mismatch 6 -gap_panalty 6 -match 2 -splice_penalty 20 -intron_penalty 40 -align -minscore 0 > $first_align");
}
else {
die("with first alignment... doesnt appear to be fwd/fwd or rev/fwd and should be one of those 2!");
}
# parse first alignment into a hash
my %first_align_contents;
create_alignment_hash($first_align, \%first_align_contents);
#first_align_to_trace takes a position in the first alignment and returns the corresponding position in the abmiguity sequence
my %first_align_to_trace;
# trace_to_first_align takes a postion in the ambiguity sequence and returns the corresponding position in the first alignment
my %trace_to_first_align;
# create mapping between the trace (ambiguity sequence) and first alignment...
create_sequence_alignment_map(\%trace_to_first_align, \%first_align_to_trace, \%first_align_contents, $poly_hash{"length"});
# generate the recalled sequence from the first align file
my $recall_seq = blindly_generate_recall_sequence(\%poly_hash, \%first_align_contents, \%trace_to_first_align, \%disambig_hash);
# dump recall sequence out to a file
open(RECALL, ">$recall_file");
print RECALL ">${recall_file}\n";
print RECALL $recall_seq, "\n";
close(RECALL);
system( "est_genome_ambig -genome $sequence_file -est $recall_file -match 1 -mismatch 1 -gap_panalty 2 -splice_penalty 20 -intron_penalty 40 -align -minscore 0 > $second_align");
exit(0);
}
else {
die("Inivalid mode");
}
}
# if you get to this point it means go ahead and run the analysis as in the paper
my @logfile_list;
my $movie_log = $basename.".result_log";
# default here should be up to 20
# the whole program basically takes place in this loop
for(my $thresh = 1; $thresh <= 20; $thresh++) {
print STDERR "working on threshold $thresh\n";
my $sequence_file = $top_sequence_file;
# build neccessary filenames for later
my $poly = $expt_dir."/".$trace_base_and_ext.".poly";
my $ambig_file = $basename.".ambig".".${thresh}";
my $recall_file = $basename.".recall".".${thresh}";
my $first_align = $basename.".first_align".".${thresh}";
my $first_gtf = $basename.".first_gtf".".${thresh}";
my $second_align = $basename.".second_align".".${thresh}";
my $second_gtf = $basename.".second_gtf".".${thresh}";
my $ambig_file2 = $basename.".ambig2".".${thresh}";
my $recall_file2 = $basename.".recall2".".${thresh}";
my $first_align2 = $basename.".first_align2".".${thresh}";
my $first_gtf2 = $basename.".first_gtf2".".${thresh}";
my $second_align2 = $basename.".second_align2".".${thresh}";
my $second_gtf2 = $basename.".second_gtf2".".${thresh}";
my $first_pass_log_file = $basename.".first_log".".${thresh}";
my $log_file = $basename.".iter_log".".${thresh}";
my $acedb_command_line = $basename.".acedb_command".".${thresh}";
my $reverse_complement_flag;
push(@logfile_list, $log_file);
# get the original poly file and make an internal representation of it
system( "phred -dd $expt_dir $trace_file");
# create poly hash and set initial threshold for calling ambiguity codes
my %poly_hash;
create_poly_hash($poly, \%poly_hash, $thresh);
# generate ambiguity sequence the way original trace recalling does it (no thresholding at all so far)
my $ambig_seq = blindly_generate_ambig_sequence(\%poly_hash, \%ambig_hash);
# dump ambiguity sequence to a file
open(AMBIG, ">$ambig_file");
print AMBIG ">${ambig_file}\n";
print AMBIG $ambig_seq, "\n";
close(AMBIG);
# run est_genome_ambig to generate the first align file
system( "est_genome_ambig -genome $sequence_file -est $ambig_file -mismatch 6 -gap_panalty 6 -match 2 -splice_penalty 20 -intron_penalty 40 -align -minscore 0 > $first_align");
my $first_align_report = get_estgenome_report($first_align);
if($first_align_report->genomeDirection eq "forward" && $first_align_report->estDirection eq "forward") {
$reverse_complement_flag = 0;
}
elsif($first_align_report->genomeDirection eq "forward" && $first_align_report->estDirection eq "reversed") {
$reverse_complement_flag = 1;
# in this case need to reverse complement the sequence to make alignment foward to foward
my $new_sequence_file = $sequence_file."_rc";
system("reverse_complement.pl $sequence_file > $new_sequence_file");
$sequence_file = $new_sequence_file;
system( "est_genome_ambig -mode forward -genome $sequence_file -est $ambig_file -mismatch 6 -gap_panalty 6 -match 2 -splice_penalty 20 -intron_penalty 40 -align -minscore 0 > $first_align");
}
else {
die("with first alignment... doesnt appear to be fwd/fwd or rev/fwd and should be one of those 2!");
}
# convert first align file to gtf
system("est2geno_to_gtf.pl $first_align > $first_gtf");
# parse first alignment into a hash
my %first_align_contents;
create_alignment_hash($first_align, \%first_align_contents);
#first_align_to_trace takes a position in the first alignment and returns the corresponding position in the abmiguity sequence
my %first_align_to_trace;
# trace_to_first_align takes a postion in the ambiguity sequence and returns the corresponding position in the first alignment
my %trace_to_first_align;
# create mapping between the trace (ambiguity sequence) and first alignment...
create_sequence_alignment_map(\%trace_to_first_align, \%first_align_to_trace, \%first_align_contents, $poly_hash{"length"});
# generate the recalled sequence from the first align file
my $recall_seq = blindly_generate_recall_sequence(\%poly_hash, \%first_align_contents, \%trace_to_first_align, \%disambig_hash);
# dump recall sequence out to a file
open(RECALL, ">$recall_file");
print RECALL ">${recall_file}\n";
print RECALL $recall_seq, "\n";
close(RECALL);
# run est_genome on the recalled sequence and the genomic sequence
system( "est_genome_ambig -genome $sequence_file -est $recall_file -match 1 -mismatch 1 -gap_panalty 2 -splice_penalty 20 -intron_penalty 40 -align -minscore 0 > $second_align");
my $second_align_report = get_estgenome_report($second_align);
if($second_align_report->genomeDirection ne "forward" || $second_align_report->estDirection ne "forward") {
open(LOG, ">${log_file}");
close(LOG);
next;
}
system("est2geno_to_gtf.pl $second_align > $second_gtf");
open(LOG, ">$first_pass_log_file");
my $first_compare_string = `compare_altsplices_single.pl ${first_gtf} ${second_gtf} --no_both_altsplice`;
if($first_compare_string ne "") {
print LOG "altsplice found --> $first_compare_string\n";
}
else {
print LOG "no altsplice found\n";
}
close(LOG);
# parse second alignment into a has in the same way as the first alignment
my %second_align_contents;
create_alignment_hash($second_align, \%second_align_contents);
my %second_align_to_recall;
my %recall_to_second_align;
create_sequence_alignment_map(\%recall_to_second_align, \%second_align_to_recall, \%second_align_contents, $poly_hash{"length"});
# walk through the poly file and take a look at what happened at each position..
tester(\%poly_hash, \%trace_to_first_align, \%first_align_contents, \%recall_to_second_align, \%second_align_contents, \%type_hash, $thresh);
my($new_ambig, $new_recall) = guided_generate_sequences(\%poly_hash);
# dump new ambiguity sequence to a file
open(AMBIG, ">$ambig_file2");
print AMBIG ">${ambig_file2}\n";
print AMBIG $new_ambig, "\n";
close(AMBIG);
# dump new recall sequence out to a file
open(RECALL, ">$recall_file2");
print RECALL ">${recall_file2}\n";
print RECALL $new_recall, "\n";
close(RECALL);
# run est_genome_ambig to generate the first align file
# using this one
system( "est_genome_ambig -genome $sequence_file -est $ambig_file2 -mismatch 6 -gap_panalty 6 -match 2 -splice_penalty 20 -intron_penalty 40 -align -minscore 0 > $first_align2");
system("est2geno_to_gtf.pl $first_align2 > $first_gtf2");
# run est_genome on the recalled sequence and the genomic sequence... this is the old secondary alignment
system( "est_genome_ambig -genome $sequence_file -est $recall_file2 -match 1 -mismatch 1 -gap_panalty 4 -splice_penalty 20 -intron_penalty 40 -align -minscore 0 > $second_align2");
system("est2geno_to_gtf.pl $second_align2 > $second_gtf2");
# once you have the first and second second pass gtf files check them
# for alternate splices and dump the results into a log file
open(LOG, ">$log_file");
my $compare_string = `compare_altsplices_single.pl ${first_gtf2} ${second_gtf2} --no_both_altsplice`;
if($compare_string ne "") {
print LOG "altsplice found --> $compare_string\n";
}
else {
print LOG "no altsplice found\n";
}
close(LOG);
}
# look at log files and alignments of different thresholds and try and figure out if you really have an alternate splice
analyze_movie(\@logfile_list, $movie_log);
exit(0);
################## SUBROUTINES ##############################
sub make_add_string {
my($gtf_file1, $gtf_file2) = @_;
my $gtf1 = GTF::new({gtf_filename => $gtf_file1});
my $genes1 = $gtf1->transcripts;
my $gtf2 = GTF::new({gtf_filename => $gtf_file2});
my $genes2 = $gtf2->transcripts;
my $max = 0;
my $min = 1000000000000000;
my $length1 = 0;
my $length2 = 0;
foreach my $gene (@$genes1) {
my $cds = $gene->cds;
foreach my $c (@$cds){
$length1 += $c->stop - $c->start;
if($c->stop > $max) {
$max = $c->stop;
}
if($c->start < $min) {
$min = $c->start;
}
}
}
foreach my $gene (@$genes2){
my $cds = $gene->cds;
foreach my $c (@$cds){
$length2 += $c->stop - $c->start;
if($c->stop > $max) {
$max = $c->stop;
}
if($c->start < $min) {
$min = $c->start;
}
}
}
my $compare_string1 = "";
my $compare_string2 = "";
my $add_string = "";
for(my $i=0;$i<$max - $min;$i++) {
$compare_string1 .= "0";
$compare_string2 .= "0";
}
foreach my $gene (@$genes1){
my $cds = $gene->cds;
foreach my $c (@$cds){
my $temp = "";
for(my $i=0;$i<$c->stop - $c->start;$i++) {
$temp .= "1";
}
substr($compare_string1, $c->start - $min, $c->stop - $c->start, $temp);
}
}
foreach my $gene (@$genes2){
my $cds = $gene->cds;
foreach my $c (@$cds){
my $temp = "";
for(my $i=0;$i<$c->stop - $c->start;$i++) {
$temp .= "1";
}
substr($compare_string2, $c->start - $min, $c->stop - $c->start, $temp);
}
}
for(my $i=0;$i<length($compare_string1);$i++) {
my $c1 = substr($compare_string1, $i, 1);
my $c2 = substr($compare_string2, $i, 1);
my $add = $c1 + $c2;
$add_string = $add_string.$add;
}
return($add_string, $max, $min);
}
sub check_exon_overlap {
my($gtf_file, $test_start, $test_end) = @_;
my $gtf = GTF::new({gtf_filename => $gtf_file});
my $genes = $gtf->transcripts;
foreach my $gene (@$genes) {
my $cds = $gene->cds;
foreach my $c (@$cds) {
if($c->start < $test_start && $c->end < $test_start) {
}
elsif($c->start > $test_end && $c->end > $test_end) {
}
else {
return("yes");
}
}
return("no");
}
}
sub build_gtf_filenames {
my($logfile) = @_;
# build the names of the gtf files you need to compare
my $thresh;
if($logfile =~ /.iter_log\.(\d+)$/) {
$thresh = $1;
}
else {
die("improperly formatted logfile name");
}
my $gtf1 = $logfile;
if($gtf1 =~ /\.iter_log\.\d+$/) {
$gtf1 =~ s/iter_log/first_gtf2/;
}
else {
die("improperly formatted logfile name");
}
my $gtf2 = $logfile;
if($gtf2 =~ /\.iter_log\.\d+$/) {
$gtf2 =~ s/iter_log/second_gtf2/;
}
else {
die("improperly formatted logfile name");
}
return($thresh, $gtf1, $gtf2);
}
sub do_5_prime_splice_site {
my ($altsplice_bouandries, $one_past_gtf1, $one_past_gtf2, $representative_threshold, $most_numerous_key) = @_;
my $gtf1_exon1_overlap = check_exon_overlap($one_past_gtf1, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{0}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{2});
my $gtf2_exon1_overlap = check_exon_overlap($one_past_gtf2, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{0}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{2});
my $gtf1_exon2_overlap = check_exon_overlap($one_past_gtf1, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{3}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{4});
my $gtf2_exon2_overlap = check_exon_overlap($one_past_gtf2, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{3}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{4});
if($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "no" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "yes") {
return("do_5_prime_splice_site, exon 1 dissapears");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "yes" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "no") {
return("do_5_prime_splice_site, exon 2 dissapears");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "no" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "no") {
return("do_5_prime_splice_site, both secondary alignment exons dissapear");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "yes" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "yes") {
return("do_5_prime_splice_site, both secondary alignment exons visible");
}
else {
return("do_5_prime_splice_site unexpected case, e-mail author");
}
return("do_5_prime_splice_site, unexpectedly reached end of function e-mail author");
}
sub do_3_prime_splice_site {
my ($altsplice_bouandries, $one_past_gtf1, $one_past_gtf2, $representative_threshold, $most_numerous_key) = @_;
my $gtf1_exon1_overlap = check_exon_overlap($one_past_gtf1, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{0}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{1});
my $gtf2_exon1_overlap = check_exon_overlap($one_past_gtf2, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{0}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{1});
my $gtf1_exon2_overlap = check_exon_overlap($one_past_gtf1, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{2}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{4});
my $gtf2_exon2_overlap = check_exon_overlap($one_past_gtf2, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{2}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{4});
if($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "no" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "yes") {
return("do_3_prime_splice_site, exon 1 dissapears");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "yes" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "no") {
return("do_3_prime_splice_site, exon 2 dissapears");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "no" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "no") {
return("do_3_prime_splice_site, both secondary alignment exons dissapear");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "yes" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "yes") {
return("do_3_prime_splice_site, both secondary alignment exons visible");
}
else {
return("do_3_prime_splice_site unexpected case, e-mail authof");
}
return("do_3_prime_splice_site, unexpectedly reached end of function e-mail author");
}
sub do_retained_intron {
my ($altsplice_bouandries, $one_past_gtf1, $one_past_gtf2, $representative_threshold, $most_numerous_key) = @_;
my $gtf1_exon1_overlap = check_exon_overlap($one_past_gtf1, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{1}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{2});
my $gtf2_exon1_overlap = check_exon_overlap($one_past_gtf2, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{1}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{2});
my $gtf1_exon2_overlap = check_exon_overlap($one_past_gtf1, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{3}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{4});
my $gtf2_exon2_overlap = check_exon_overlap($one_past_gtf2, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{3}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{4});
if($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "no" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "yes") {
return("do_retained_intron, exon 1 dissapears");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "yes" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "no") {
return("do_retained_intron, exon 2 dissapears");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "no" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "no") {
return("do_retained_intron, both secondary alignment exons dissapear");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "yes" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "yes") {
return("do_retained_intron, both secondary alignment exons visible");
}
else {
return("do_retained_intron unexpected case e-mail author");
}
return("do_retained_intron, unexpectedly reached end of function e-mail author");
}
sub do_clean_alternate_exon {
my ($altsplice_bouandries, $one_past_gtf1, $one_past_gtf2, $representative_threshold, $most_numerous_key) = @_;
my $gtf1_exon1_overlap = check_exon_overlap($one_past_gtf1, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{0}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{1});
my $gtf2_exon1_overlap = check_exon_overlap($one_past_gtf2, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{0}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{1});
my $gtf1_exon2_overlap = check_exon_overlap($one_past_gtf1, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{4}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{5});
my $gtf2_exon2_overlap = check_exon_overlap($one_past_gtf2, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{4}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{5});
if($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "no" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "yes") {
return("do_clean_alternate_exon, looks like exon 2 is the first double trace exon past the cassette exon");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "yes" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "no") {
return("do_clean_alternate_exon, looks like exon 1 is the first double trace exon past the cassette exon");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "no" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "no") {
return("do_clean_alternate_exon, both secondary alignment exons dissapear");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "yes" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "yes") {
return("do_clean_alternate_exon, both secondary alignment exons visible");
}
else {
return("do_clean_alternate_exon unexpected case e-mail author");
}
return("do_clean_alternate_exon, unexpectedly reached end of function e-mail author");
}
sub do_alternate_exon {
my ($altsplice_bouandries, $one_past_gtf1, $one_past_gtf2, $representative_threshold, $most_numerous_key) = @_;
my $gtf1_exon1_overlap = check_exon_overlap($one_past_gtf1, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{0}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{2});
my $gtf2_exon1_overlap = check_exon_overlap($one_past_gtf2, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{0}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{2});
my $gtf1_exon2_overlap = check_exon_overlap($one_past_gtf1, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{5}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{7});
my $gtf2_exon2_overlap = check_exon_overlap($one_past_gtf2, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{5}, $altsplice_bouandries->{$representative_threshold}->{$most_numerous_key}->{7});
if($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "no" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "yes") {
return("do_alternate_exon, looks like exon 2 is the first double trace exon past the cassette exon");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "yes" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "no") {
return("do_alternate_exon, looks like exon 1 is the first double trace exon past the cassette exon");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "no" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "no") {
return("do_alternate_exon, both secondary alignment exons dissapear");
}
elsif($gtf1_exon1_overlap eq "yes" &&
$gtf2_exon1_overlap eq "yes" &&
$gtf1_exon2_overlap eq "yes" &&
$gtf2_exon2_overlap eq "yes") {
return("do_alternate_exon, both secondary alignment exons visible");
}
else {
return("do_alternate_exon unexpected case, e-mail author");
}
return("do_alternate_exon, unexpectedly reached end of function, e-mail author");
}
sub analyze_all_altsplices {
my($alt_type_hash, $movie_log, $regex_type) = @_;
# set the regular expression corresponding to the type of alternate
# splice the function was called to handle
my $regex = "";
my $landmark_count = -1;
if($regex_type eq "alternate exon") {
$regex = "(2+)(1*)(0+)(1+)(0+)(1*)(2+)";
$landmark_count = 7;
}
elsif($regex_type eq "clean alternate exon") {
$regex = "(2+)(0+)(1+)(0+)(2+)";
$landmark_count = 5;
}
elsif($regex_type eq "alt 3\' splice site") {
$regex = "(2+)(0+)(1+)(2+)";
$landmark_count = 4;
}
elsif($regex_type eq "alt 5\' splice site") {
$regex = "(2+)(1+)(0+)(2+)";
$landmark_count = 4;
}
elsif($regex_type eq "retained intron") {
$regex = "(0+)(2+)(1+)(2+)(1*)\$|^(1*)(2+)(1+)(2+)(0+)|^(1*)(2+)(1+)(2+)(1*)\$|(0+)(2+)(1+)(2+)(0+)";
$landmark_count = 5;
}
else {
die("analyze_all altsplices: error, no test defined for $regex_type");
}
# open the movie log file
open(OUT, ">>${movie_log}");
my %compare_altsplice_hash;
my %altsplice_bouandries;
# look at each logfile that matches the type of alternate splice you are looking at
foreach my $logfile (@{$alt_type_hash->{$regex_type}}) {
# get the threshold and gtf filenames you will need to use
my($thresh, $gtf1, $gtf2) = build_gtf_filenames($logfile);
# added this to fix a problem that occurs when you next out of the loop you need that is one past the last observed alternate splice
if(!(-e $gtf1) || !(-e $gtf2)) {
print OUT "module report: one of files $gtf1 or $gtf2 needed for analysis doesn't exits... exiting\n";
exit(0);
}
# generate the add string and coordinates that orient the add string to the genomic sequence
my ($add_string, $add_string_max, $add_string_min) = make_add_string($gtf1, $gtf2);
# look for all places in the add string that match the regular
# expression for the alternate splice... hopefully there is
# only one but have implemented code to handle the case where
# there is more than 1
my $hit_count = 0;
while($add_string =~ /$regex/g) {
$hit_count++;
my @landmark_string_array = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20);
# since there are 4 different patterns in the retained intron regex
# now need to account for the fact that you might not match the first
# one, if not you need to shift things down
if($regex_type eq "retained intron") {
if(defined($1)) {
# do nothing... the landmark strings are where they need to be
}
elsif(defined($6)) {
$landmark_string_array[0] = $landmark_string_array[5];
$landmark_string_array[1] = $landmark_string_array[6];
$landmark_string_array[2] = $landmark_string_array[7];
$landmark_string_array[3] = $landmark_string_array[8];
$landmark_string_array[4] = $landmark_string_array[9];
}
elsif(defined($11)) {
$landmark_string_array[0] = $landmark_string_array[10];
$landmark_string_array[1] = $landmark_string_array[11];
$landmark_string_array[2] = $landmark_string_array[12];
$landmark_string_array[3] = $landmark_string_array[13];
$landmark_string_array[4] = $landmark_string_array[14];
}
elsif(defined($16)) {
$landmark_string_array[0] = $landmark_string_array[15];
$landmark_string_array[1] = $landmark_string_array[16];
$landmark_string_array[2] = $landmark_string_array[17];
$landmark_string_array[3] = $landmark_string_array[18];
$landmark_string_array[4] = $landmark_string_array[19];
}
else {
die("error when testing for retained intron, exiting");
}
}
my @landmark_length_array = ();
for(my $i=0;$i<$landmark_count;$i++) {
$landmark_length_array[$i] = length($landmark_string_array[$i]);
}
my $pos = pos($add_string);
for(my $i=0;$i<$landmark_count;$i++) {
$pos -= $landmark_length_array[$i];
}
my @landmark_array = ();
$landmark_array[0] = $add_string_min + $pos;
for(my $i=1;$i<$landmark_count+1;$i++) {
$landmark_array[$i] = $landmark_array[$i-1] + $landmark_length_array[$i-1];
}
my $compare_altsplice_key;
if($regex_type eq "alternate exon") {
$compare_altsplice_key = $landmark_array[1]."_".$landmark_array[2]."_".$landmark_array[3]."_".$landmark_array[4]."_".$landmark_array[5];
}
elsif($regex_type eq "clean alternate exon") {
$compare_altsplice_key = $landmark_array[1]."_".$landmark_array[2]."_".$landmark_array[3]."_".$landmark_array[4];
}
elsif($regex_type eq "alt 3\' splice site") {
$compare_altsplice_key = $landmark_array[1]."_".$landmark_array[2]."_".$landmark_array[3];
}
elsif($regex_type eq "alt 5\' splice site") {
$compare_altsplice_key = $landmark_array[1]."_".$landmark_array[2]."_".$landmark_array[3];
}
elsif($regex_type eq "retained intron") {
$compare_altsplice_key = $landmark_array[1]."_".$landmark_array[2]."_".$landmark_array[3]."_".$landmark_array[4];
}
else {
die("attempt to test for unexpected alternate splice type $regex_type... exiting");
}
push(@{$compare_altsplice_hash{$compare_altsplice_key}}, $logfile);
for(my $i=0;$i<$landmark_count+1;$i++) {
$altsplice_bouandries{$thresh}{$compare_altsplice_key}{$i} = $landmark_array[$i];
}
}
if($hit_count == 0) {
die("should have found alternate splice in $logfile but didnt... exiting, e-mail author");
}
}
my $num_altsplices = keys (%compare_altsplice_hash);
print OUT "saw $num_altsplices different example(s) of this type of alternate splice\n";
my $most_numerous = -1;
my $most_numerous_key = "";
if($num_altsplices == 0) {
return("should have seen at least one altsplice of type $regex_type here but didnt...");
}
if($num_altsplices == 1) {
$most_numerous_key = (keys %compare_altsplice_hash)[0];
$most_numerous = @{$compare_altsplice_hash{$most_numerous_key}};
}
elsif($num_altsplices > 1) {
# only analyze the most numerous form and check to make sure that it
# hits on at least 2 thresholds... otherwise they tend to look junky
# like alignment artifects... will loose some good ones this way but
# will probably loose a lot more crumy ones
foreach my $key (keys %compare_altsplice_hash) {
my $n = @{$compare_altsplice_hash{$key}};
if($n > $most_numerous) {
$most_numerous = $n;
$most_numerous_key = $key;
}
print OUT $key, "\n";
foreach my $thing (@{$compare_altsplice_hash{$key}}) {
print OUT "\t", $thing, "\n";
}
}
# delete the entries corresponding to the less numerous alternate
# splice forms seen and proceed as before with the most numerous one
my @delete_list = ();
foreach my $key (keys %compare_altsplice_hash) {
if($key ne $most_numerous_key) {
push(@delete_list, $key)
}
}
foreach my $key (@delete_list) {
delete $compare_altsplice_hash{$key};
}
}
my $min_thresh = 1000;
my $max_thresh = -1;
foreach my $key (keys %compare_altsplice_hash) {
foreach my $logfile (@{$compare_altsplice_hash{$key}}) {
my $alt_thresh;
if($logfile =~ /.iter_log\.(\d+)/) {
$alt_thresh = $1;
}
else {
die("improperly formatted logfile name:\n\n $logfile\n\n");
}
if($min_thresh > $alt_thresh) {
$min_thresh = $alt_thresh
}
if($max_thresh < $alt_thresh) {
$max_thresh = $alt_thresh;
}
}
print OUT "looks like range on which I see the alternate splice is $min_thresh to $max_thresh...\n";
}
# now have the range of thresholds on which we can see the alternate
# splice... go one past the max thresh and make sure one of the
# flanking exons (the double trace side) overlaps in both TR
# alignments but that other flanking exon (the now masked-by-noise
# single trace side) doesnt.
if($max_thresh == 20) {
return("$regex_type, see alternate splice all the way to threshold of 20... wont be able to see any double trace only aligments!");
}
# get the gtf files for the alignments one past the last alternate
# splice you saw to check for a double trace only alignment
my $thresh_to_test = $max_thresh + 1;
my $one_past_gtf1 = $movie_log;
$one_past_gtf1 =~ s/movie_log/first_gtf2/;
$one_past_gtf1 .= ".${thresh_to_test}";
my $one_past_gtf2 = $one_past_gtf1;
$one_past_gtf2 =~ s/first_gtf2/second_gtf2/;
my $temp_logfile_for_threshold = ${$compare_altsplice_hash{$most_numerous_key}}[0];
my $representative_threshold;
if($temp_logfile_for_threshold =~ /\.iter_log\.(\d+)/) {
$representative_threshold = $1;
}
else {
die("error... e-mail author");
}
if($regex_type eq "alternate exon") {
my $return_string = do_alternate_exon(\%altsplice_bouandries, $one_past_gtf1, $one_past_gtf2, $representative_threshold, $most_numerous_key);
return($return_string);