forked from grubbybio/pRNASeqTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GSTAr.pl
executable file
·1590 lines (1303 loc) · 46.9 KB
/
GSTAr.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 Getopt::Std;
my $version_number = "1.0";
my $help = help_message($version_number);
# if there are no arguments, return the help message and quit
unless($ARGV[0]) {
die "$help";
}
# declare global variables
our($opt_q,$opt_r,$opt_h,$opt_v,$opt_t,$opt_a);
# Initialize option defaults
$opt_r = 0.65;
# get options
getopts('r:hvqta');
# If user wants help or version, give it, and die
if($opt_h) {
die "$help";
}
if($opt_v) {
die "GSTAr.pl version $version_number\n";
}
# ensure transcripts and queries files are readable
my $transcripts_file = pop @ARGV;
unless(-r $transcripts_file) {
die "FATAL: transcripts.fasta file was not readable\n\n$help";
}
my $queries_file = pop @ARGV;
unless(-r $queries_file) {
die "FATAL: queries.fasta file was not readable\n\n$help";
}
# Start speaking to user, unless quiet mode is on
unless($opt_q) {
print STDERR "\nGSTAr version $version_number\n";
print STDERR `date`;
print STDERR "\nDependency checks:\n";
}
# Check dependencies
my $RNAplex_check = check_RNAplex();
unless($RNAplex_check) {
die "FAIL\n\n$help";
}
unless($opt_q) {
print STDERR "\n";
}
# Validate options
unless(($opt_r > 0) and ($opt_r <= 1)) {
die "FATAL: option r must be greater than 0 and less than or equal to 1\n\n$help";
}
# Hash the transcriptome into memory
my %txome = hash_transcriptome($transcripts_file);
# Begin work, one query at a time
unless($opt_q) {
print STDERR "\n";
}
# Print headers for output
print "\# GSTAr version $version_number\n";
print "\# ";
print `date`;
print "\# Queries: $queries_file\n";
print "\# Transcripts: $transcripts_file\n";
print "\# Minimum Free Energy Ratio cutoff (option -r): $opt_r\n";
print "\# Sorted by: ";
if($opt_a) {
print "AllenScore\n";
} else {
print "MFEratio\n";
}
print "\# Output Format: ";
if($opt_t) {
print "Tabular\n";
# print a header
print "Query\tTranscript\tTStart\tTStop\tTSlice\tMFEperfect\tMFEsite\tMFEratio\tAllenScore\tPaired\tUnpaired\tStructure\tSequence\n";
} else {
print "Pretty\n";
}
(open(QUERIES, "$queries_file")) || die "FATAL: Failed to open queries file $queries_file\n\n$help";
while (<QUERIES>) {
my $header = $_;
my $full_qseq = uc <QUERIES>;
# remove newlines and the > from header
$header =~ s/\r//g;
$header =~ s/\n//g;
$header =~ s/>//g;
$full_qseq =~ s/\r//g;
$full_qseq =~ s/\n//g;
# Report to user
unless($opt_q) {
print STDERR "Working on query $header ...";
}
# Crude cleanup and validation of sequence
$full_qseq =~ s/U/T/g; ## U's to T's if present
unless($full_qseq =~ /^[ATGC]+$/) {
## not suppressed by quiet mode, because it is a warning
print STDERR "\n\tWARNING: query sequence $header has non AUTGC characters. It is not being analyzed\!\n";
next;
}
if((length $full_qseq) > 26) {
print STDERR "\n\tWARNING: query sequence $header is too large. The limit is 26nts. It is not being analyzed\!\n";
next;
}
if((length $full_qseq) < 15) {
print STDERR "\n\tWARNING: query sequence $header is too small. It must be larger than 14 nts. It is not being analyzed\!\n";
next;
}
$full_qseq = uc $full_qseq;
$full_qseq =~ s/T/U/g;
# Write the query fasta file and ge the tx lengths in the process.
my $q_file = write_q(\$full_qseq,\%txome); ## The file name is GSTAr_temp.fasta
# Call the master sub-routine for RNAplex analyses
my @first_pass = RNAplex_analysis(\$full_qseq,\$header,\%txome, \$q_file);
# Sort, and in the process eliminate redundant alignments based on slice site within the query set
my @winners = sort_first_pass(@first_pass);
# Output
unless($opt_q) {
my $final_count = scalar @winners;
if($final_count) {
print STDERR " Found $final_count valid alignments\n";
} else {
print STDERR " NO valid alignments found\n";
}
}
if($opt_t) {
tabular_output(@winners);
} else {
pretty_output(@winners);
}
}
close QUERIES;
# remove temp file
system "rm -f GSTAr_temp.fasta";
###########################
# Here be sub-routines
sub help_message {
my($version) = @_;
my $message = "GSTAr.pl : Generic Small RNA-Transcriptome Aligner
Usage: GSTAr.pl \[options\] queries.fasta transcripts.fasta
Version: $version
Options:
-h Print help message and quit
-v Print version and quit
-q Quiet mode .. no log/progress information to STDERR
-t Tabular output format ... More suitable for automated parsing
-a Sort by Allen et al. score instead of the default MFEratio
-r [float >0..1] Minimum Free Energy Ratio cutoff. Default: 0.65
Dependencies (must be in PATH):
RNAplex (from Vienna RNA Package)
Documentation: perldoc GSTAr.pl
";
return $message;
}
sub check_RNAplex {
unless($opt_q) {
print STDERR "\tRNAplex: ";
}
(open(RD, "RNAplex --version 2>&1 |")) || return 0;
my $one = <RD>;
close RD;
if($one =~ /^RNAplex \d/) {
unless($opt_q) {
print STDERR "PASS $one";
}
return 1;
} else {
return 0;
}
}
sub RNAplex_analysis {
my($qseq,$query_name,$txome,$qfile) = @_; ## passed by reference
# paranoia
my $u_qseq = uc $$qseq;
$u_qseq =~ s/T/U/g;
# get the perfect MFE
my $perfect_MFE = get_perfect_MFE($u_qseq);
unless($perfect_MFE) {
die "ABORT: Failed to get perfect MFE in sub-routine RNAplex_analysis for query sequence $u_qseq\n";
}
# Interaction length is length query + 10.
my $int_length = (length $u_qseq) + 10;
# spacing left on default zero .. return maximal number of sites regardles of spacing. Redundant sites will be sorted out later
# option -e ..
my $option_e = sprintf("%.2f",($opt_r * $perfect_MFE));
# Call it
(open(PLEX, "RNAplex -f 2 -e $option_e -z $int_length < $$qfile |")) || die "ABORT: Failed to open RNAplex main job in sub-routine RNAplex_analysis for query $$query_name\n";
my $plex_brax;
my $plex_mfe;
my @local_pos = ();
my $mfe_ratio;
my $padded_brax;
my @tx_pos = ();
my $tx_ungapped_seq;
my $ungapped;
my @to_be_gapped;
my @gapped = ();
my @out = ();
my $outstring;
my $tx;
my $pline;
while (<PLEX>) {
# test
#print STDERR "Working on plex line $_";
chomp;
$pline = $_;
if($pline =~ /^>/) {
$pline =~ s/>//g;
unless($pline eq "query") {
$tx = $pline;
}
} elsif ($pline =~ /^[\.\(]+\&/) {
## TEST
#print STDERR "\tworking on bracketer pline $pline\n";
## all other data.
## ensure that the perfect_MFE has been set
unless($perfect_MFE) {
die "ABORT: Failed to set MFE of perfect match in sub-routine RNAplex_analysis\n";
}
# check mfe ratio. If it fails op_r, cease analysis of this alignment
$plex_mfe = get_plex_MFE($pline);
#print STDERR "\tplex_mfe: $plex_mfe\n";
$mfe_ratio = $plex_mfe / $perfect_MFE;
#print STDERR "\tmfe_ratio is $mfe_ratio ";
if($mfe_ratio > 1) {
$mfe_ratio = 1;
}
if($mfe_ratio < $opt_r) {
next; ## enclosing loop is the <PLEX> loop. So this goes to next alignment
}
#print STDERR " proceeding\n";
# get the plex_brax
$plex_brax = get_plex_brax($pline);
#print STDERR "\tplex_brax is $plex_brax\n";
# If you are here, then the alignment qualifies for reporting.
# Process the brax
# First, pad with dots so that the length of the query is represented
@local_pos = get_local_pos($pline);
$padded_brax = pad_plex_brax(\$plex_brax,\@local_pos,\$u_qseq);
# After pad_plex_brax above, the @local_pos array will be modified (if needed) to reflect the local
# coordinates within the transcript.
# Ensure that the adjusted sequence location is still valid. If not, abort analysis of this alignment
if(($local_pos[0] < 1) or
((length $$txome{$tx}) < $local_pos[1])) {
next;
}
$tx_ungapped_seq = substr($$txome{$tx}, ($local_pos[0] - 1), ($local_pos[1] - $local_pos[0] + 1));
# Ensure that this sequence is all AUCG
unless($tx_ungapped_seq =~ /^[AUCG]+$/) {
next;
}
# Generate the initial, ungapped sequence string
$ungapped = "$tx_ungapped_seq" . "&" . "$u_qseq";
# Process the alignments to remove trailing nts on the transcript
@to_be_gapped = no_trailing(\$padded_brax,\$ungapped,\@local_pos);
# Gap-ify both the nt and structure alignment strings
@gapped = gapify($to_be_gapped[0],$to_be_gapped[1]);
# Quality control
my $qc = quality_control(@gapped);
unless($qc) {
print STDERR "\nWARNING: Skipping an alignment for $$query_name at transcript $tx $local_pos[0] to $local_pos[1] because of a failed alignment parse\n\t@gapped\n";
next;
}
# Begin to build output string
$outstring = "$$query_name"; ## reset .. [0] Query name
$outstring .= "\t$tx"; ## [1] Transcript name
$outstring .= "\t$local_pos[0]"; ## [2] Transcript start position
$outstring .= "\t$local_pos[1]"; ## [3] Transcript stop position
# Determine transcript position corresponding to position 10 of the query .. the 'slicing site'
my $slice_site = compute_slice_site(\@gapped,\@local_pos);
$outstring .= "\t$slice_site"; ## [4] Transcript slice position (pos 10 of query)
$outstring .= "\t$perfect_MFE"; ## [5] MFE of perfect match
$outstring .= "\t$plex_mfe"; ## [6] MFE of this alignment
$outstring .= "\t$mfe_ratio"; ## [7] MFE Ratio of this alignment to perfect match
# Calculate the Allen et al. score
my $allen = allen_score(@gapped);
$outstring .= "\t$allen"; ## [8] Allen et al. score
# Assess paired, unpaired, 5' SS, 3' SS, SIL, AIL, and Bulges.
my @struc = assess_pairing($gapped[0],$local_pos[0],$u_qseq);
$outstring .= "\t$struc[0]"; ## [9] Paired string
$outstring .= "\t$struc[1]"; ## [10] Annotated unpaired string
# Add on the alignments themselves
$outstring .= "\t$gapped[0]"; ## [11] Bracket alignment
$outstring .= "\t$gapped[1]"; ## [12] Sequence alignment
# test
#print "$outstring\n";
#my @txb = get_al_array($gapped[0],0);
#my @qxb = get_al_array($gapped[0],1);
#my @txs = get_al_array($gapped[1],0);
#my @qxs = get_al_array($gapped[1],1);
#my $tb_pretty = join('',@txb);
#my $ts_pretty = join('',@txs);
#my $qb_pretty = reverse(join('',@qxb));
#my $qs_pretty = reverse(join('',@qxs));
#print "$ts_pretty\n$tb_pretty\n$qb_pretty\n$qs_pretty\n";
# test
#print "$outstring\n";
push(@out,$outstring);
}
}
close PLEX;
return @out;
}
sub hash_transcriptome {
my($tx_file) = @_;
unless($opt_q) {
print STDERR "\nHashing the transcriptome data...";
}
my %hash = ();
(open(TX, "$tx_file")) || die "ABORT: Failed to open transcripts_file $tx_file in sub-routine hash_transcriptome\n";
my $key;
my $seq;
my $line;
while (<TX>) {
chomp;
if($_ =~ /^>(\S+)$/) {
if($seq) {
$hash{$key} = $seq;
}
$seq = '';
$key = $1;
} elsif ($_ =~ /^>/) {
die "ABORT: Invalid transcript name $_ \n\tTranscript names must contain no whitespace! Please edit your transcriptome file\n";
} else {
$line = uc $_; ## force uppercase
$line =~ s/T/U/g; ## RNA-i-fy
$seq .= $line;
}
}
close TX;
# enter last one
$hash{$key} = $seq;
unless($opt_q) {
print STDERR " Done\n";
}
return %hash;
}
sub get_plex_brax {
my($line) = @_;
my $brax;
if($line =~ /^([\(\.\)\&]+)\s/) {
$brax = $1;
}
if($brax) {
return $brax;
} else {
die "ABORT: Failed to parse out RNAplex structure from input line $line in sub-routine get_plex_brax\n";
}
}
sub get_plex_MFE {
my($line) = @_;
my $mfe;
if($line =~ /\(([\s\-\d\.]+)\)/){
$mfe = $1;
}
if($mfe) {
return $mfe;
} else {
#print STDERR "\nWARNING: Failed to parse out MFE from input line $line in sub-routine get_plex_MFE .. reported as 0\n";
return 0;
}
}
sub get_local_pos {
my($line) = @_;
my @local_pos = ();
if($line =~ /(\d+),(\d+).*:.*(\d+),(\d+)/) {
push(@local_pos,$1);
push(@local_pos,$2);
push(@local_pos,$3);
push(@local_pos,$4);
}
if(@local_pos) {
return @local_pos;
} else {
die "ABORT: Failed to parse out local coordinates from input line $line in sub-routine get_local_pos\n";
}
}
sub pad_plex_brax {
my($inbrax,$pos,$qseq) = @_; # passed by reference .. scalar, array, scalar
# test
#print "\nInput: $$inbrax @$pos\n";
my $q_len = length $$qseq;
my $t_part;
my $q_part;
if($$inbrax =~ /^([\(\.]+)\&([\)\.]+)$/) {
$t_part = $1;
$q_part = $2;
} else {
die "ABORT: Failed to split the incoming brax in sub-routine pad_plex_brax\n";
}
my $i;
my $left_added = 0; ## left of query, right of transcript
for($i = $$pos[2]; $i > 1; --$i) {
$q_part = "." . "$q_part";
$t_part = "$t_part" . ".";
++$left_added;
}
my $right_added = 0; ## right of query, left of transcript
for($i = $$pos[3]; $i < $q_len; ++$i) {
$q_part = "$q_part" . ".";
$t_part = "." . "$t_part";
++$right_added;
}
# modify the local position array
$$pos[0] -= $right_added;
$$pos[1] += $left_added;
$$pos[2] -= $left_added;
$$pos[3] += $right_added;
# Ensure against rare cases when the transcript does not fully extend to the limits of the query, likely because
# the orignial longer transcript sequence did not fully capture the site.
my $t_5_dots = 0;
my $t_3_dots = 0;
my $q_5_dots = 0;
my $q_3_dots = 0;
if($t_part =~ /^\.+/) {
$t_5_dots = length $&;
}
if($t_part =~ /\.+$/) {
$t_3_dots = length $&;
}
if($q_part =~ /^\.+/) {
$q_5_dots = length $&;
}
if($q_part =~ /\.+$/) {
$q_3_dots = length $&;
}
# Add dots to t_5 if needed
for($i = ($q_3_dots - $t_5_dots); $i > 0; --$i) {
$t_part = "." . "$t_part";
--$$pos[0];
}
# Add dots to t_3 if needed
for($i = ($q_5_dots - $t_3_dots); $i > 0; --$i) {
$t_part .= ".";
++$$pos[1];
}
# rejoin
my $outbrax = "$t_part" . "&" . "$q_part";
return $outbrax;
}
sub get_tx_final {
my($lpos,$old_tx) = @_; ## passed by reference .. array and scalar
my $old_start;
my $tx_key;
if($$old_tx =~ /^(\S+):(\d+)-\d+$/) {
$tx_key = $1;
$old_start = $2;
} else {
die "ABORT: Failed to parse transcript location string $$old_tx in sub-routine get_tx_final\n";
}
my @out = ();
$out[0] = $tx_key;
$out[1] = $old_start + $$lpos[0] - 1;
$out[2] = $old_start + $$lpos[1] - 1;
return @out;
}
sub gapify {
my($inbrax,$inseq) = @_;
# test
#print "\ngapify inbrax: $inbrax inseq: $inseq\n";
#exit;
my @out = ();
my %left_right = get_left_right($inbrax);
my $last_left = 0;
my $last_right = (length $inbrax) + 1;
my @inbrax_chars = split ('',$inbrax);
my @inseq_chars = split ('', $inseq);
my $left_delta;
my $right_delta;
my $delta;
my $index;
my $outbrax_left;
my $outbrax_right;
my $outseq_left;
my $outseq_right;
my $x;
for (my $i = 1; $i <= (length $inbrax); ++$i) {
if(exists($left_right{$i})) {
$left_delta = $i - $last_left + 1;
$right_delta = $last_right - $left_right{$i} + 1;
$delta = $left_delta - $right_delta;
# add trailing characters, should be all dots
if($left_delta) {
for($x = $last_left; $x < ($i - 1); ++$x) {
$outbrax_left .= "$inbrax_chars[$x]";
$outseq_left .= "$inseq_chars[$x]";
}
}
if($right_delta) {
for($x = ($last_right - 2); $x >= $left_right{$i}; --$x) {
if($outbrax_right) {
$outbrax_right = "$inbrax_chars[$x]" . "$outbrax_right";
$outseq_right = "$inseq_chars[$x]" . "$outseq_right";
} else {
$outbrax_right = "$inbrax_chars[$x]";
$outseq_right = "$inseq_chars[$x]";
}
}
}
# add gaps if need be
if($delta > 0) {
# gap(s) added on the right side
for($x = $delta; $x > 0; --$x) {
if($outbrax_right) {
$outbrax_right = "-" . "$outbrax_right";
$outseq_right = "-" . "$outseq_right";
} else {
$outbrax_right = "-";
$outseq_right = "-";
}
}
} elsif ($delta < 0) {
# gap(s) added on the left side
for($x = $delta; $x < 0; ++$x) {
$outbrax_left .= "-";
$outseq_left .= "-";
}
}
# Add the current pair
$outbrax_left .= "$inbrax_chars[($i - 1)]";
$outseq_left .= "$inseq_chars[($i - 1)]";
if($outbrax_right) {
$outbrax_right = "$inbrax_chars[($left_right{$i} - 1)]" . "$outbrax_right";
$outseq_right = "$inseq_chars[($left_right{$i} - 1)]" . "$outseq_right";
} else {
$outbrax_right = "$inbrax_chars[($left_right{$i} - 1)]";
$outseq_right = "$inseq_chars[($left_right{$i} - 1)]" ;
}
# reset
$last_left = $i;
$last_right = $left_right{$i};
} elsif ($inbrax_chars[($i - 1)] eq "\&") {
# Have reach the middle. Just report any unreported dots since the last pair
$left_delta = $i - $last_left + 1;
$right_delta = $last_right - ($i + 1);
# add trailing characters, should be all dots
if($left_delta) {
for($x = $last_left; $x < ($i - 1); ++$x) {
$outbrax_left .= "$inbrax_chars[$x]";
$outseq_left .= "$inseq_chars[$x]";
}
}
if($right_delta) {
for($x = ($last_right - 2); $x >= $i; --$x) {
$outbrax_right = "$inbrax_chars[$x]" . "$outbrax_right";
$outseq_right = "$inseq_chars[$x]" . "$outseq_right";
}
}
last;
}
}
$out[0] = "$outbrax_left" . "\&" . "$outbrax_right";
$out[1] = "$outseq_left" . "\&" . "$outseq_right";
# TEST
#print "\nINPUT:\n", "\t$inbrax\n", "\t$inseq\n";
#print "OUTPUT:\n", "\t$out[0]\n", "\t$out[1]\n";
#my $rev_right_brax = reverse $outbrax_right;
#my $rev_right_seq = reverse $outseq_right;
#print "$outseq_left\n$outbrax_left\n$rev_right_brax\n$rev_right_seq\n";
return @out;
}
sub get_left_right {
my($brax) = @_;
my %hash = ();
my @chars = split('',$brax);
## Note that the & is included in the above array
my $i = 0;
my @lefts = ();
my $left;
foreach my $char (@chars) {
++$i;
if($char eq "\(") {
push(@lefts, $i);
} elsif ($char eq "\)") {
$left = pop @lefts;
$hash{$left} = $i;
}
}
return %hash;
}
sub no_trailing {
my($brax,$ungapped,$pos) = @_; ## passed by reference .. scalar, scalar, array
my @out = ();
my @bs = split("\&",$$brax);
my @ss = split("\&",$$ungapped);
my $n_left_end_dots = 0;
my $n_right_end_dots = 0;
my $n_left_mid_dots = 0;
my $n_right_mid_dots = 0;
my $n_end_trim = 0;
my $n_mid_trim = 0;
## test
#print "\nno_trailing INPUT $$brax $$ungapped @$pos\n";
if($bs[0] =~ /^\.+/) {
$n_left_end_dots += (length $&);
}
if($bs[0] =~ /\.+$/) {
$n_left_mid_dots += (length $&);
}
if($bs[1] =~ /^\.+/) {
$n_right_mid_dots += (length $&);
}
if($bs[1] =~ /\.+$/) {
$n_right_end_dots += (length $&);
}
if($n_left_end_dots > $n_right_end_dots) {
$n_end_trim = $n_left_end_dots - $n_right_end_dots;
}
if($n_left_mid_dots > $n_right_mid_dots) {
$n_mid_trim = $n_left_mid_dots - $n_right_mid_dots;
}
my $junk;
my @bleft = split ('', $bs[0]);
my @sleft = split ('', $ss[0]);
my $i;
for($i = 0; $i < $n_end_trim; ++$i) {
$junk = shift @bleft;
$junk = shift @sleft;
++$$pos[0];
}
for($i = 0; $i < $n_mid_trim; ++$i) {
$junk = pop @bleft;
$junk = pop @sleft;
--$$pos[1];
}
my $new_brax_left = join('',@bleft);
my $new_seq_left = join('',@sleft);
$out[0] = "$new_brax_left" . "\&" . "$bs[1]";
$out[1] = "$new_seq_left" . "\&" . "$ss[1]";
## test
#print "no_trailing OUTPUT $out[0] $out[1] @$pos\n";
#exit;
return @out;
}
sub compute_slice_site {
my($gapped,$local_pos) = @_; ## passed by reference. Two arrays.
my $q_seq;
my $t_seq;
if($$gapped[0] =~ /^(\S+)\&(\S+)$/) {
$t_seq = $1;
$q_seq = $2;
} else {
die "ABORT: In sub-routine compute_slice_site, failed to parse alignment $$gapped[0]\n";
}
my @q_char = split ('', $q_seq);
my @t_char = split ('', $t_seq);
my $real_t_pos = $$local_pos[1] + 1;
my $real_q_pos = 0;
my $tch;
foreach my $qch (@q_char) {
$tch = pop @t_char;
unless($tch eq "-") {
--$real_t_pos;
}
unless($qch eq "-") {
++$real_q_pos;
}
if($real_q_pos == 10) {
return $real_t_pos;
}
}
# Should not get here
die "ABORT: Failure in sub-routine compute_slice_site\n";
}
sub allen_score {
my(@al) = @_;
my $score = 0;
my @tbrax = get_al_array($al[0],0);
my @qbrax = get_al_array($al[0],1);
my @tseq = get_al_array($al[1],0);
my @qseq = get_al_array($al[1],1);
my $q_pos = 0;
my $ts;
my $qb;
my $tb;
my $i = -1;
#print "INPUT: @al\n";
foreach my $qs (@qseq) {
++$i;
unless($qs eq "-") {
++$q_pos;
}
$qb = $qbrax[$i];
$ts = pop @tseq;
$tb = pop @tbrax;
#print "q_pos: $q_pos ts $ts qs $qs tb $tb qb $qb\n";
# trap error
#unless($tb) {
#print "\nError trap in sub-routine allen_acore INPUT: @al\n";
#print "\ttbrax: @tbrax\n";
#print "\tqbrax: @qbrax\n";
#print "\ttseq: @tseq\n";
#print "\tqseq: @qseq\n";
#exit;
#}
if(($tb eq ".") or ($qb eq ".")) { ## acounts for bulges, assymmetric internal loops as well
$score += 1;
#print "\tpenalty 1\n";
if(($q_pos >= 2) and ($q_pos <= 12)) {
#print "\tpenalty doubled\n";
$score += 1;
}
} elsif ((($ts eq "G") and ($qs eq "U") and ($tb eq "\(") and ($qb eq "\)")) or
(($ts eq "U") and ($qs eq "G") and ($tb eq "\(") and ($qb eq "\)"))) {
$score += 0.5;
#print "\tGU wobble penalty 0.5\n";
if(($q_pos >= 2) and ($q_pos <= 12)) {
#print "\tGU woblle penalty doubled\n";
$score += 0.5;
}
}
}
#print "final score: $score\n";
#exit;
return $score;
}
sub get_al_array {
my($string,$index) = @_;
my @two = split ("\&", $string);
my $chunk = $two[$index];
my @out = split ('', $chunk);
return @out;
}
sub assess_pairing {
my($brax,$tx_start,$u_qseq) = @_;
my @out = ();
$out[0] = check_pairs($brax,$tx_start,$u_qseq);
my $unpaired = check_unpaired($brax,$tx_start,$u_qseq);
if($unpaired) {
$out[1] = annotate_unpaired($unpaired,$tx_start,$u_qseq);
} else {
$out[1] = "NA"; ## All are paired
}
# test
#print "sub assess_pairing INPUT $brax $tx_start $u_qseq\n";
#print "\tpairs: $out[0]\n";
#print "\tUNpairs: $out[1]\n";
return @out;
}
sub check_pairs {
my($brax,$tx_start,$u_qseq) = @_;
my $out;
my @tbrax = get_al_array($brax,0);
my @qbrax = reverse(get_al_array($brax,1));
my $i = -1;
my $t_pos = $tx_start - 1;
my $q_pos = (length $u_qseq) + 1;
my $last_t_start;
my $last_t_paired;
my $last_q_start;
my $last_q_paired;
my $q;
my $output;
foreach my $t (@tbrax) {
++$i;
$q = $qbrax[$i];
# check for closure
if(($t ne "\(") or ($q ne "\)")) {
if($last_t_start) {
if($output) {
$output = "$last_q_paired" . "-" . "$last_q_start" . "," . "$last_t_paired" . "-" . "$last_t_start" . "\;" . "$output";
} else {
$output = "$last_q_paired" . "-" . "$last_q_start" . "," . "$last_t_paired" . "-" . "$last_t_start";
}
}
# reset
$last_t_start = '';
$last_t_paired = '';
$last_q_start = '';
$last_q_paired = '';
}
# Update positions
unless($t eq "-") {
++$t_pos;
}
unless($q eq "-") {
--$q_pos;
}
# If the current position is a pair, track it
if(($t eq "\(") and ($q eq "\)")) {
$last_t_paired = $t_pos;
$last_q_paired = $q_pos;
unless($last_t_start) {
$last_t_start = $t_pos;
}
unless($last_q_start) {
$last_q_start = $q_pos;
}
}
}
# check for closure at end
if($last_t_start) {
if($output) {
$output = "$last_q_paired" . "-" . "$last_q_start" . "," . "$last_t_paired" . "-" . "$last_t_start" . "\;" . "$output";
} else {
$output = "$last_q_paired" . "-" . "$last_q_start" . "," . "$last_t_paired" . "-" . "$last_t_start";
}
}
# reset
$last_t_start = '';
$last_t_paired = '';
$last_q_start = '';
$last_q_paired = '';
return $output;
}
sub check_unpaired {
my($brax,$tx_start,$u_qseq) = @_;
my $out;
my @tbrax = get_al_array($brax,0);
my @qbrax = reverse(get_al_array($brax,1));
my $i = -1;
my $t_pos = $tx_start - 1;
my $q_pos = (length $u_qseq) + 1;
my $last_t_start;
my $last_t_unpaired;
my $last_q_start;
my $last_q_unpaired;
my $q;
my $output;
foreach my $t (@tbrax) {
++$i;
$q = $qbrax[$i];
# check for closure
if(($t eq "\(") and ($q eq "\)")) {
# test
#print "Hello world\n";
if($last_t_start) {
# test
#print "In reporting loop 1\n";
if($output) {
$output = "$last_q_unpaired" . "-" . "$last_q_start" . "," . "$last_t_unpaired" . "-" . "$last_t_start" . "\;" . "$output";
} else {
$output = "$last_q_unpaired" . "-" . "$last_q_start" . "," . "$last_t_unpaired" . "-" . "$last_t_start";
}
}
# reset
$last_t_start = '';
$last_t_unpaired = '';