-
Notifications
You must be signed in to change notification settings - Fork 12
/
ACF_trans_splice_step1.pl
763 lines (737 loc) · 31.2 KB
/
ACF_trans_splice_step1.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
#!/usr/bin/perl -w
use strict;
# check splicing sites for 2-segment-diff_chr_strand
die "Usage: $0 \"CB_splice_folder\" \"unmap.parsed.tmp\" \"genome_location\" \"output basename\" \"\(optional\)cutoff\" \"\(optional\) extend N bases\" \"\(optional\)min_AS\" \"\(optional\)maxJump\" \"\(DEBUG set to 1\)\"" if (@ARGV < 4);
my $DIR=$ARGV[0]; # /home/CB_splice/
my $anno=$ARGV[1]; # unmap.parsed.tmp
my $genome=$ARGV[2]; # /data/iGenome/mouse/Ensembl/NCBIM37/Sequence/Chromosomes/
my $fileout=$ARGV[3];
my $cutoff=0.9;
if (scalar(@ARGV) > 4) {$cutoff=$ARGV[4];}
my $Extend=15; # 15nt by default. as 3' splice strength need 23nt, 20nt from intron and 3 from exon.
if (scalar(@ARGV) > 5) {$Extend=$ARGV[5];}
my $MAS=0;
if (scalar(@ARGV) > 6) {$MAS=$ARGV[6];}
my $maxJump=1000000;
if (scalar(@ARGV) > 7) {$maxJump=$ARGV[7];}
my $debug=0;
if (scalar(@ARGV) > 8) {$debug=$ARGV[8];}
die "Usage: $0 \"CB_splice_folder\" \"unmap.parsed.tmp\" \"genome_location\" \"output basename\" \"\(optional\)cutoff\" \"\(optional\) extend N bases\" \"\(optional\)min_AS\" \"\(optional\)maxJump\" \"\(DEBUG set to 1\)\"" if (scalar(@ARGV) > 9);
my %uniq;
my %READ;
my %genome;
open IN,$anno;
open OUT,">".$fileout;
open OUT1,">".$fileout.".0"; # splice-motif found trust hit CB prediction
open OUT2,">".$fileout.".1"; # closure == 0, trust best CB prediction
open OUT3,">".$fileout.".01"; # combined files of the above 2
open OUTerr,">".$fileout."_withN";
open OUTp,">".$fileout.".S1";
while(<IN>) {
chomp;
# modified to read from "unmap.parsed.tmp" directly, since the trans-splicing overhand could contain more low complexity sequences and hense fail to pass the MAS critera
# the MAS can be re-enact here
my @a0=split("\t",$_);
# newid-1__1 75 11,-118352428,36S39M,48,0 9,+20365667,37S38M,48,0
if (scalar(@a0) ne 4) { next; }
my $Len=$a0[1];
my @a2=split(/,/,$a0[2]);
my @a3=split(/,/,$a0[3]);
my $strand1=substr($a2[1],0,1);
my $strand2=substr($a3[1],0,1);
my $pos1=substr($a2[1],1);
my $pos2=substr($a3[1],1);
my $multi="";
if (($a2[0] ne $a3[0]) or ($strand1 ne $strand2) or (abs($pos1 - $pos2) > $maxJump)) {
if (($a2[3] + $a3[3]) >= (2 * $MAS)) {
my $info=$a0[2]."\t".$a0[3];
# process $info
my %anno;
my %Chr;
my @p=split("\t",$info);
for(@p) {
my @tmp=split(/\,/,$_);
my $strandy="+";
if ($tmp[1]=~m/^-/){
$strandy="-";
$tmp[1]=~s/^\-//;
}
else {
$tmp[1]=~s/^\+//;
}
my @CIGAR_op=($tmp[2]=~m/[MSID]/g);
my @CIGAR_va=($tmp[2]=~m/\d+/g);
my $start=0;
my $length=0;
my $Nr=scalar(@CIGAR_op);
if ($strandy eq "+") {
for(my $i=0; $i<$Nr; $i++){
if ($CIGAR_op[$i] eq "M") {$length+=$CIGAR_va[$i]; }
elsif ($CIGAR_op[$i] eq "D") {$length+=$CIGAR_va[$i]; }
elsif ($CIGAR_op[$i] eq "I") {$length-=$CIGAR_va[$i]; }
elsif ($CIGAR_op[$i] eq "S") {
if (($start > 0) or ($length > 0)) {last;}
$start=$CIGAR_va[$i];
}
}
}
else {
for(my $i=$Nr-1; $i>=0; $i--){
if ($CIGAR_op[$i] eq "M") {$length+=$CIGAR_va[$i];}
elsif ($CIGAR_op[$i] eq "D") {$length+=$CIGAR_va[$i]; }
elsif ($CIGAR_op[$i] eq "I") {$length-=$CIGAR_va[$i]; }
elsif ($CIGAR_op[$i] eq "S") {
if (($start > 0) or ($length > 0)) {last;}
$start=$CIGAR_va[$i];
}
}
}
if (exists $anno{$tmp[0]}{$start}) {}
else {
$Chr{$tmp[0]}++;
$anno{$tmp[0]}{$start}=join("\t",$length,$tmp[1],($tmp[1]+$length),$strandy);
}
}
# pick the most-hit chromosome
my $chromo="";
foreach my $id(sort{$Chr{$b} <=> $Chr{$a}} keys %Chr) {
if ($Chr{$id} > 1) {$chromo=$id; last;}
}
# each part hit once
if ($chromo eq "") {
my $result=$Len;
my $cov1=0;
my $cov2=0;
my @Cov;
for(my $i=0; $i<$Len; $i++) { $Cov[$i]=0; }
foreach my $id(sort keys %anno) {
foreach my $pos (sort{$a <=> $b} keys %{$anno{$id}}) {
$result=$result."\t".$id."\t".$pos."\t".$anno{$id}{$pos};
my @t=split("\t",$anno{$id}{$pos});
for(my $i=0; $i<$t[0]; $i++) {$Cov[$i+$pos]=1;}
}
}
my $first=-1;
my $last=-1;
for(my $i=0; $i<$Len; $i++) {
if ($Cov[$i] eq 1) {
$cov1++;
if ($first eq -1) {$first = $i;}
$last=$i;
}
}
$cov2=$last - $first + 1;
if ($cov1 > 0) { $multi=$a0[0]."\t".$cov2."\t".$cov1."\t".$result; }
} else {
# hit on the same chromosome
# to-be added
}
}
}
if ($multi eq "") { next; }
my @a=split("\t",$multi);
# remove the above if start from "unmap.parsed.multi"
# and enable the following line:
# my @a=split("\t",$_);
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# in_len out_len len chr seq_s len pos_s pos_e str chr seq_s len pos_s pos_e strand
# newid-231525/1__69 151 151 151 3 0 115 144260022 144260137 - X 113 38 108011492 108011530 -
# newid-1__1 75 75 75 11 0 39 118352428 118352467 - 9 37 38 20365667 20365705 +
if(($a[1] eq $a[2]) and ($a[1] > $cutoff*$a[3]) and (scalar(@a) eq 16)){
#print OUTp join("\t",@a),"\n";
$a[8]=$a[8]-1;
$a[14]=$a[14]-1;
if ($a[4]=~m/^chromosome/i) {$a[4]=~s/chromosome//i;}
if ($a[4]=~m/^chr/i) {$a[4]=~s/chr//i;}
if ($a[10]=~m/^chromosome/i) {$a[10]=~s/chromosome//i;}
if ($a[10]=~m/^chr/i) {$a[10]=~s/chr//i;}
if ($a[5] < $a[11]) {
$uniq{$a[4]}++;
$uniq{$a[10]}++;
$READ{$a[0]}=join("\t",@a);
print OUTp $READ{$a[0]},"\n";
#$uniq{$a[4]}{$a[7]}{$a[8]}{$a[0]}=join("\t",1,$a[5],$a[6],$a[9]);
#$uniq{$a[10]}{$a[13]}{$a[14]}{$a[0]}=join("\t",2,$a[11],$a[12],$a[15]);
}
else {
$uniq{$a[4]}++;
$uniq{$a[10]}++;
#$READ{$a[0]}=join("\t",@a);
$READ{$a[0]}=join("\t",$a[0],$a[1],$a[2],$a[3],$a[10],$a[11],$a[12],$a[13],$a[14],$a[15],$a[4],$a[5],$a[6],$a[7],$a[8],$a[9]);
print OUTp $READ{$a[0]},"\n";
#$uniq{$a[4]}{$a[7]}{$a[8]}{$a[0]}=join("\t",2,$a[5],$a[6],$a[9]);
#$uniq{$a[10]}{$a[13]}{$a[14]}{$a[0]}=join("\t",1,$a[11],$a[12],$a[15]);
}
#$READ{$a[0]}=join("\t",@a);
}
}
close IN;
##### calculate 5p splice strength #####
my $tmpfile1=$DIR."/me2x5";
#my %me2x5 = &makescorematrix("~/CB_splice/me2x5");
my %me2x5 = &makescorematrix($tmpfile1);
my $tmpfile2=$DIR."/splicemodels/splice5sequences";
#my %seq = &makesequencematrix("/home/CB_splice/splicemodels/splice5sequences");
my %seq = &makesequencematrix($tmpfile2);
my %bgd;
$bgd{'A'} = 0.27;
$bgd{'C'} = 0.23;
$bgd{'G'} = 0.23;
$bgd{'T'} = 0.27;
sub makesequencematrix{
my $file = shift;
my %matrix;my $n=0;
open(SCOREF, $file) || die "Can't open $file!\n";
while(<SCOREF>) {
chomp;
$_=~ s/\s//;
$matrix{$_} = $n;
$n++;
}
close(SCOREF);
return %matrix;
}
sub makescorematrix{
my $file = shift;
my %matrix;my $n=0;
open(SCOREF, $file) || die "Can't open $file!\n";
while(<SCOREF>) {
chomp;
$_=~ s/\s//;
$matrix{$n} = $_;
$n++;
}
close(SCOREF);
return %matrix;
}
sub getrest5{
my $seq = shift;
my @seqa = split(//,uc($seq));
return $seqa[0].$seqa[1].$seqa[2].$seqa[5].$seqa[6].$seqa[7].$seqa[8];
}
sub scoreconsensus5{
my $seq = shift;
my @seqa = split(//,uc($seq));
my %bgd;
$bgd{'A'} = 0.27; $bgd{'C'} = 0.23; $bgd{'G'} = 0.23; $bgd{'T'} = 0.27;
my %cons1;
$cons1{'A'} = 0.004; $cons1{'C'} = 0.0032; $cons1{'G'} = 0.9896; $cons1{'T'} = 0.0032;
my %cons2;
$cons2{'A'} = 0.0034; $cons2{'C'} = 0.0039; $cons2{'G'} = 0.0042; $cons2{'T'} = 0.9884;
my $addscore = $cons1{$seqa[3]}*$cons2{$seqa[4]}/($bgd{$seqa[3]}*$bgd{$seqa[4]});
return $addscore;
}
sub log2{
my ($val) = @_;
return log($val)/log(2);
}
##### calculate 5p splice strength #####
##### calculate 3p splice strength #####
sub hashseq{
#returns hash of sequence in base 4
# &hashseq('CAGAAGT') returns 4619
my $seq = shift;
$seq = uc($seq);
$seq =~ tr/ACGT/0123/;
my @seqa = split(//,$seq);
my $sum = 0;
my $len = length($seq);
my @four = (1,4,16,64,256,1024,4096,16384);
my $i=0;
while ($i<$len) {
$sum+= $seqa[$i] * $four[$len - $i -1] ;
$i++;
}
return $sum;
}
my @metables = &makemaxentscores;
sub makemaxentscores{
my $dir = $DIR."/splicemodels/";
my @list = ('me2x3acc1','me2x3acc2','me2x3acc3','me2x3acc4','me2x3acc5','me2x3acc6','me2x3acc7','me2x3acc8','me2x3acc9');
my @metables;
my $num = 0 ;
foreach my $file (@list) {
my $n = 0;
open (SCOREF,"<".$dir.$file) || die "Can't open $file!\n";
while(<SCOREF>) {
chomp;
$_=~ s/\s//;
$metables[$num]{$n} = $_;
$n++;
}
close(SCOREF);
#print STDERR $file."\t".$num."\t".$n."\n";
$num++;
}
return @metables;
}
sub makewmmscores{
my $dir = $DIR."/splicemodels/";
my @list = ('me1s0acc1','me1s0acc2','me1s0acc3','me1s0acc4','me1s0acc5','me1s0acc6','me1s0acc7','me1s0acc8','me1s0acc9');
my @metables;
my $num = 0 ;
foreach my $file (@list) {
my $n = 0;
open (SCOREF,"<".$dir.$file) || die "Can't open $file!\n";
while(<SCOREF>) {
chomp;
$_=~ s/\s//;
$metables[$num]{$n} = $_;
$n++;
}
close(SCOREF);
#print STDERR $file."\t".$num."\t".$n."\n";
$num++;
}
return @metables;
}
sub makemmscores{
my $dir = $DIR."/splicemodels/";
my @list = ('me2s0acc1','me2s0acc2','me2s0acc3','me2s0acc4','me2s0acc5','me2s0acc6','me2s0acc7','me2s0acc8','me2s0acc9');
my @metables;
my $num = 0 ;
foreach my $file (@list) {
my $n = 0;
open (SCOREF,"<".$dir.$file) || die "Can't open $file!\n";
while(<SCOREF>) {
chomp;
$_=~ s/\s//;
$metables[$num]{$n} = $_;
$n++;
}
close(SCOREF);
#print STDERR $file."\t".$num."\t".$n."\n";
$num++;
}
return @metables;
}
sub maxentscore{
my $seq = shift;
my $table_ref = shift;
my @metables = @$table_ref;
my @sc;
$sc[0] = $metables[0]{&hashseq(substr($seq,0,7))};
$sc[1] = $metables[1]{&hashseq(substr($seq,7,7))};
$sc[2] = $metables[2]{&hashseq(substr($seq,14,7))};
$sc[3] = $metables[3]{&hashseq(substr($seq,4,7))};
$sc[4] = $metables[4]{&hashseq(substr($seq,11,7))};
$sc[5] = $metables[5]{&hashseq(substr($seq,4,3))};
$sc[6] = $metables[6]{&hashseq(substr($seq,7,4))};
$sc[7] = $metables[7]{&hashseq(substr($seq,11,3))};
$sc[8] = $metables[8]{&hashseq(substr($seq,14,4))};
my $finalscore = $sc[0] * $sc[1] * $sc[2] * $sc[3] * $sc[4] / ($sc[5] * $sc[6] * $sc[7] * $sc[8]);
return $finalscore;
}
sub getrest3{
my $seq = shift;
my $seq_noconsensus = substr($seq,0,18).substr($seq,20,3);
return $seq_noconsensus;
}
sub scoreconsensus3{
my $seq = shift;
my @seqa = split(//,uc($seq));
my %bgd;
$bgd{'A'} = 0.27; $bgd{'C'} = 0.23; $bgd{'G'} = 0.23; $bgd{'T'} = 0.27;
my %cons1;
$cons1{'A'} = 0.9903; $cons1{'C'} = 0.0032; $cons1{'G'} = 0.0034; $cons1{'T'} = 0.0030;
my %cons2;
$cons2{'A'} = 0.0027; $cons2{'C'} = 0.0037; $cons2{'G'} = 0.9905; $cons2{'T'} = 0.0030;
my $addscore = $cons1{$seqa[18]} * $cons2{$seqa[19]}/ ($bgd{$seqa[18]} * $bgd{$seqa[19]});
return $addscore;
}
##### calculate 3p splice strength #####
sub sanity {
my $seq = shift;
my @seqa = split(//,uc($seq));
my $Nr=scalar(@seqa);
my $flag=0;
for(my $i=0; $i<$Nr; $i++) {
if ($seqa[$i] !~m/[ATCG]/) {$flag++;}
}
return $flag;
}
my %SMotif;
$SMotif{"GTAG"}=3;
$SMotif{"GCAG"}=2;
$SMotif{"ATAC"}=1;
$SMotif{"CTAC"}=-3;
$SMotif{"CTGC"}=-2;
$SMotif{"GTAT"}=-1;
my %reported;
my $f=0;
foreach my $chr (sort keys %uniq) {
#if (length($chr) > 3) {next}
if ($f eq 1) {print "\t=======\tyes\n";}
elsif ($f eq -1) {print "\t=======\tno\n";}
print "searching chromosome : ".$chr.".fa";
$f=-1;
if (exists $reported{$chr}) {next;}
open (IN1, $genome."/".$chr.".fa") or next;
<IN1>;
my $SEQ;
$f=1;
$reported{$chr}=1;
while(<IN1>) {chomp; $SEQ=$SEQ.$_;}
$genome{$chr}=$SEQ;
close IN1;
}
if ($f eq 1) {print "\t=======\tyes\n";}
elsif ($f eq -1) {print "\t=======\tno\n";}
if ($f eq -1) {
foreach my $chr (sort keys %uniq) {
#if (length($chr) > 3) {next}
if ($f eq 1) {print "\t=======\tyes\n";}
elsif ($f eq -1) {print "\t=======\tno\n";}
print "searching chromosome : chr".$chr.".fa";
$f=-1;
if (exists $reported{$chr}) {next;}
open (IN1, $genome."/chr".$chr.".fa") or next;
<IN1>;
my $SEQ;
$f=1;
$reported{$chr}=1;
while(<IN1>) {chomp; $SEQ=$SEQ.$_;}
$genome{$chr}=$SEQ;
close IN1;
}
if ($f eq 1) {print "\t=======\tyes\n";}
elsif ($f eq -1) {print "\t=======\tno\n";}
}
if ($f eq -1) {
foreach my $chr (sort keys %uniq) {
#if (length($chr) > 3) {next}
if ($f eq 1) {print "\t=======\tyes\n";}
elsif ($f eq -1) {print "\t=======\tno\n";}
print "searching chromosome : chromosome".$chr.".fa";
$f=-1;
if (exists $reported{$chr}) {next;}
open (IN1, $genome."/chromosome".$chr.".fa") or next;
<IN1>;
my $SEQ;
$f=1;
$reported{$chr}=1;
while(<IN1>) {chomp; $SEQ=$SEQ.$_;}
$genome{$chr}=$SEQ;
close IN1;
}
if ($f eq 1) {print "\t=======\tyes\n";}
elsif ($f eq -1) {print "\t=======\tno\n";}
}
foreach my $id (keys %READ) {
my @a=split("\t",$READ{$id});
if ($debug eq -1) { print $READ{$id},"\n"; }
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# in_len out_len len chr seq_s len pos_s pos_e str chr seq_s len pos_s pos_e strand
# newid-1__1 75 75 75 11 0 39 118352428 118352466 - 9 37 38 20365667 20365704 +
# 11 split exon 118352430 118352807 protein_coding + MLL ENSG00000118058___19___67
# 9 split exon 20365667 20365742 protein_coding - MLLT3 ENSG00000171843___16___30
# 11 split exon 118353137 118353210 protein_coding + MLL ENSG00000118058___22___67
# newid-2__1 75 75 75 9 0 41 20365706 20365746 + 11 37 38 118353173 118353210 -
# $left_seq => 5' splice site
# $right_seq => 3' splice site
if ($a[9] eq "-") {
my $overlap=$a[5]+$a[6]-$a[11];
my $start=$a[7];
my $end=0;
my $chr1=$a[4];
my $chr2=$a[10];
my $left_seq="";
my $left_max=-99999;
my $left_pos=0;
my $left_flag=0;
my $right_seq="";
my $right_max=-99999;
my $right_pos=0;
my $right_flag=0;
my $SMscoren=-99999;
# R-m+ [9876543AG|0123456789] R-m+ [9876543210|GU3456789] pos = 0 on exon
if ($a[15] eq "-") {
$end=$a[14];
$left_seq=substr($genome{$chr2},($end-2*$Extend-1),(4*$Extend + 1));
if ($overlap > $Extend) { print OUTerr join("\t",@a),"\tlarge_overlap\t",$right_seq,"\t",$left_seq,"\t",$overlap,"\n"; next; }
if (length($left_seq) ne (4*$Extend + 1)) {print OUTerr join("\t",$chr2,$end,"right_404"),"\t",join("\t",@a),"\n"; next;}
$right_seq=substr($genome{$chr1},($start-2*$Extend-1),(4*$Extend + 1));
if (length($right_seq) ne (4*$Extend + 1)) {print OUTerr join("\t",$chr1,$start,"left_404"),"\t",join("\t",@a),"\n"; next;}
}
# R-m+ [9876543AG|0123456789] R+m- [9876543AC|0123456789] pos = 0 on exon
else {
$end=$a[13];
$left_seq=substr($genome{$chr2},($end-2*$Extend-1),(4*$Extend + 1));
if ($overlap > $Extend) { print OUTerr join("\t",@a),"\tlarge_overlap\t",$right_seq,"\t",$left_seq,"\t",$overlap,"\n"; next; }
if (length($left_seq) ne (4*$Extend + 1)) {print OUTerr join("\t",$chr2,$end,"right_404"),"\t",join("\t",@a),"\n"; next;}
my $tmp1=$left_seq;
$tmp1=~tr/[atcgATCG]/[TAGCTAGC]/;
my $tmp2=scalar reverse $tmp1;
$left_seq=$tmp2;
$right_seq=substr($genome{$chr1},($start-2*$Extend-1),(4*$Extend + 1));
if (length($right_seq) ne (4*$Extend + 1)) {print OUTerr join("\t",$chr1,$start,"left_404"),"\t",join("\t",@a),"\n"; next;}
}
{
# sanity check for non-[ATCG] nt
if ((sanity($left_seq) > 0) or (sanity($right_seq) > 0)) { print OUTerr join("\t",@a),"\tsanity_fail\t",$left_seq,"\t",$right_seq,"\t",$overlap,"\n"; next;}
if ((length($left_seq) ne (4*$Extend+1)) or (length($right_seq) ne (4*$Extend+1))) { print OUTerr join("\t",@a),"\tlength_fail\t",$left_seq,"\t",$right_seq,"\t",$overlap,"\n"; next;}
# check canonical splice-motif
my $SMS=0; # score of motif
my $PMS=0; # position of best motif
if ($debug eq 1){print join("\t",@a),"\n",$left_seq,"\t",$right_seq,"\n";}
for(my $k=0; $k<=$overlap; $k++) {
my $ml="";
my $mr="";
$ml=substr($left_seq,2*$Extend+1-$k,2);
$mr=substr($right_seq,2*$Extend-2+$overlap-$k,2);
if (($ml ne "") and ($mr ne "")) {
my $motif=$ml.$mr;
if ($debug eq 1){print $k,"\t",$motif,"\n";}
if ((exists $SMotif{$motif}) and ($SMotif{$motif} > 0)) {
my $str1="";
$str1=uc substr($left_seq,(2*$Extend+1-$k-3),9);
my $str2="";
$str2=uc substr($right_seq,(2*$Extend-2+$overlap-$k-20+2),23);
if ((length($str1) eq 9) and (length($str2) eq 23)) {
my $left_maxt=sprintf("%.2f",&log2(&scoreconsensus5($str1)*$me2x5{$seq{&getrest5($str1)}}));
my $right_maxt=sprintf("%.2f", &log2(&scoreconsensus3($str2)*&maxentscore(&getrest3($str2),\@metables)));
my $sumt=sprintf("%.2f",($left_maxt + $right_maxt));
if ($SMscoren < $sumt) { $SMscoren=$sumt; $SMS=$SMotif{$motif}; $PMS=$k; }
}
}
#if ((exists $SMotif{$motif}) and ($SMotif{$motif} > 0)) { if (abs($SMotif{$motif}) > abs($SMS) ){ $SMS=$SMotif{$motif}; $PMS=$k; } }
}
}
if ($debug eq 1){print "\n",$SMS,"\t",$PMS,"\n";}
if ($SMS > 0) {
# found junction on the plus strand
my $str1="";
$str1=uc substr($left_seq,(2*$Extend+1-$PMS-3),9);
my $str2="";
$str2=uc substr($right_seq,(2*$Extend-2+$overlap-$PMS-20+2),23);
if ($debug eq 1) { print "str1 = $str1\nstr2 = $str2\n"; }
if ((length($str1) eq 9) and (length($str2) eq 23)) {
$left_max=sprintf("%.2f",&log2(&scoreconsensus5($str1)*$me2x5{$seq{&getrest5($str1)}}));
$right_max=sprintf("%.2f", &log2(&scoreconsensus3($str2)*&maxentscore(&getrest3($str2),\@metables)));
my $sum=sprintf("%.2f",($left_max + $right_max));
$a[9]=$a[9]."\t".$left_seq."\t".$left_max."\t".$PMS."\t"."+5";
$a[15]=$a[15]."\t".$right_seq."\t".$right_max."\t".($overlap-$PMS)."\t"."+3";
$a[15]=$a[15]."\t".$sum."\t".$SMS."\t".$PMS;
print OUT join("\t",@a),"\n";
my @a=split("\t",$READ{$id});
$a[11]=$a[11]+$PMS;
$a[12]=$a[12]-$PMS;
$a[6]=$a[6]-($overlap-$PMS);
$a[7]=$a[7]+($overlap-$PMS);
if ($a[15] eq "-") { $a[14]=$a[14]-$PMS;}
else { $a[13]=$a[13]+$PMS; }
$a[15]=$a[15]."\t".$sum."\t".sprintf("%.2f",$left_max)."\t".sprintf("%.2f",$right_max)."\t"."+"."\t".$overlap."\t".$PMS."\t".($overlap-$PMS)."\t".$SMS."\t".$PMS;
print OUT1 join("\t",@a),"\n";
print OUT3 join("\t",@a),"\n";
next;
}
}
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# in_len out_len len chr seq_s len pos_s pos_e str chr seq_s len pos_s pos_e strand
# newid-32171/1__3 151 151 151 13 0 70 94111709 94111778 - 13 67 84 94125952 94126035 -
else {
$SMscoren=-99999;
$left_max=0;
$left_pos=0;
$right_max=0;
$right_pos=0;
for(my $k=0; $k<=$overlap; $k++) {
my $str1="";
$str1=uc substr($left_seq,(2*$Extend+1-$k-3),9);
my $str2="";
$str2=uc substr($right_seq,(2*$Extend-2+$overlap-$k-20+2),23);
if ((length($str1) eq 9) and (length($str2) eq 23)) {
my $left_maxt=sprintf("%.2f",&log2(&scoreconsensus5($str1)*$me2x5{$seq{&getrest5($str1)}}));
my $right_maxt=sprintf("%.2f", &log2(&scoreconsensus3($str2)*&maxentscore(&getrest3($str2),\@metables)));
my $sumt=sprintf("%.2f",($left_maxt + $right_maxt));
if ($SMscoren < $sumt) {
$SMscoren=$sumt;
$PMS=$k;
$left_pos=$k;
$left_max=$left_maxt;
$right_pos=$overlap-$k;
$right_max=$right_maxt;
$left_flag=+5;
$right_flag=+3;
}
}
}
if ($debug eq 1){print $SMscoren,"\t",$PMS,"\t",($overlap-$PMS),"\t",$overlap,"\n";}
$a[9]=$a[9]."\t".$left_seq."\t".$left_max."\t".$PMS."\t"."+5";
$a[15]=$a[15]."\t".$right_seq."\t".$right_max."\t".($overlap-$PMS)."\t"."+3";
$a[15]=$a[15]."\t".$SMscoren."\t".$SMS."\t".$PMS;
print OUT join("\t",@a),"\n";
my @a=split("\t",$READ{$id});
$a[11]=$a[11]+$PMS;
$a[12]=$a[12]-$PMS;
$a[6]=$a[6]-($overlap-$PMS);
$a[7]=$a[7]+($overlap-$PMS);
if ($a[15] eq "-") { $a[14]=$a[14]-$PMS;}
else { $a[13]=$a[13]+$PMS; }
$a[15]=$a[15]."\t".$SMscoren."\t".sprintf("%.2f",$left_max)."\t".sprintf("%.2f",$right_max)."\t"."+"."\t".$overlap."\t".$PMS."\t".($overlap-$PMS)."\t".$SMS."\t".$PMS;
print OUT2 join("\t",@a),"\n";
print OUT3 join("\t",@a),"\n";
}
}
}
else {
my $overlap=$a[5]+$a[6]-$a[11];
my $start=$a[8];
my $end=0;
my $chr1=$a[4];
my $chr2=$a[10];
my $left_seq="";
my $left_max=-99999;
my $left_pos=0;
my $left_flag=0;
my $right_seq="";
my $right_max=-99999;
my $right_pos=0;
my $right_flag=0;
# R+m- [9876543210|CT2345678] R+m- [9876543AC|0123456789] pos = 0 on exon
if ($a[15] eq "+") {
$end=$a[13];
$left_seq=substr($genome{$chr2},($end-2*$Extend-1),(4*$Extend + 1));
if (length($left_seq) ne (4*$Extend + 1)) {print OUTerr join("\t",$chr1,$start,"left_404"),"\t",join("\t",@a),"\n"; next;}
$right_seq=substr($genome{$chr1},($start-2*$Extend-1),(4*$Extend + 1));
if (length($right_seq) ne (4*$Extend + 1)) {print OUTerr join("\t",$chr2,$end,"right_404"),"\t",join("\t",@a),"\n"; next;}
if ($overlap > $Extend) { print OUTerr join("\t",@a),"\tlarge_overlap\t",$left_seq,"\t",$right_seq,"\t",$overlap,"\n"; next; }
my $tmp1=$left_seq;
$tmp1=~tr/[atcgATCG]/[TAGCTAGC]/;
my $tmp2=scalar reverse $tmp1;
$left_seq=$tmp2;
$tmp1=$right_seq;
$tmp1=~tr/[atcgATCG]/[TAGCTAGC]/;
$tmp2=scalar reverse $tmp1;
$right_seq=$tmp2;
}
# R+m- [9876543210|CT2345678] R-m+ [9876543210|GU3456789] pos = 0 on exon
else {
$end=$a[14];
$left_seq=substr($genome{$chr2},($end-2*$Extend-1),(4*$Extend + 1));
if (length($left_seq) ne (4*$Extend + 1)) {print OUTerr join("\t",$chr1,$start,"left_404"),"\t",join("\t",@a),"\n"; next;}
$right_seq=substr($genome{$chr1},($start-2*$Extend-1),(4*$Extend + 1));
if (length($right_seq) ne (4*$Extend + 1)) {print OUTerr join("\t",$chr2,$end,"right_404"),"\t",join("\t",@a),"\n"; next;}
if ($overlap > $Extend) { print OUTerr join("\t",@a),"\tlarge_overlap\t",$left_seq,"\t",$right_seq,"\t",$overlap,"\n"; next; }
my $tmp1=$right_seq;
$tmp1=~tr/[atcgATCG]/[TAGCTAGC]/;
my $tmp2=scalar reverse $tmp1;
$right_seq=$tmp2;
}
{
# sanity check for non-[ATCG] nt
if ((sanity($left_seq) > 0) or (sanity($right_seq) > 0)) { print OUTerr join("\t",@a),"\tsanity_fail\t",$left_seq,"\t",$right_seq,"\t",$overlap,"\n"; next;}
if ((length($left_seq) ne (4*$Extend+1)) or (length($right_seq) ne (4*$Extend+1))) { print OUTerr join("\t",@a),"\tlength_fail\t",$left_seq,"\t",$right_seq,"\t",$overlap,"\n"; next;}
# check canonical splice-motif
my $SMS=0; # score of motif
my $PMS=0; # position of best motif
my $SMscoren=-99999;
if ($debug eq 1){print join("\t",@a),"\n",$left_seq,"\t",$right_seq,"\n";}
for(my $k=0; $k<=$overlap; $k++) {
my $ml="";
my $mr="";
$ml=substr($left_seq,2*$Extend+1-$k,2);
$mr=substr($right_seq,2*$Extend-2+$overlap-$k,2);
if (($ml ne "") and ($mr ne "")) {
my $motif=$ml.$mr;
if ($debug eq 1){print $k,"\t",$motif,"\n";}
if ((exists $SMotif{$motif}) and ($SMotif{$motif} > 0)) {
my $str1="";
$str1=uc substr($left_seq,(2*$Extend+1-$k-3),9);
my $str2="";
$str2=uc substr($right_seq,(2*$Extend-2+$overlap-$k-20+2),23);
if ((length($str1) eq 9) and (length($str2) eq 23)) {
my $left_maxt=sprintf("%.2f",&log2(&scoreconsensus5($str1)*$me2x5{$seq{&getrest5($str1)}}));
my $right_maxt=sprintf("%.2f", &log2(&scoreconsensus3($str2)*&maxentscore(&getrest3($str2),\@metables)));
my $sumt=sprintf("%.2f",($left_maxt + $right_maxt));
if ($SMscoren < $sumt) { $SMscoren=$sumt; $SMS=$SMotif{$motif}; $PMS=$k; }
}
}
#if ((exists $SMotif{$motif}) and ($SMotif{$motif} < 0)) { if (abs($SMotif{$motif}) > abs($SMS) ){ $SMS=$SMotif{$motif}; $PMS=$k; } }
}
}
if ($debug eq 1){print "\n",$SMS,"\t",$PMS,"\n";}
if ($SMS > 0) {
my $str1="";
$str1=uc substr($left_seq,(2*$Extend+1-$PMS-3),9);
my $str2="";
$str2=uc substr($right_seq,(2*$Extend-2+$overlap-$PMS-20+2),23);
if ($debug eq 1) { print "str1 = $str1\nstr2 = $str2\n"; }
if ((length($str1) eq 9) and (length($str2) eq 23)) {
$left_max=sprintf("%.2f",&log2(&scoreconsensus5($str1)*$me2x5{$seq{&getrest5($str1)}}));
$right_max=sprintf("%.2f", &log2(&scoreconsensus3($str2)*&maxentscore(&getrest3($str2),\@metables)));
my $sum=sprintf("%.2f",($left_max + $right_max));
$a[9]=$a[9]."\t".$left_seq."\t".$left_max."\t".$PMS."\t"."-5";
$a[15]=$a[15]."\t".$right_seq."\t".$right_max."\t".($overlap-$PMS)."\t"."-3";
$a[15]=$a[15]."\t".$sum."\t".$SMS."\t".$PMS;
print OUT join("\t",@a),"\n";
my @a=split("\t",$READ{$id});
$a[6]=$a[6]-($overlap-$PMS);
$a[8]=$a[8]-($overlap-$PMS);
$a[11]=$a[11]+$PMS;
$a[12]=$a[12]-$PMS;
if ($a[15] eq "-") { $a[14]=$a[14]-$PMS; }
else { $a[13]=$a[13]+$PMS; }
$a[15]=$a[15]."\t".$sum."\t".sprintf("%.2f",$left_max)."\t".sprintf("%.2f",$right_max)."\t"."-"."\t".$overlap."\t".$PMS."\t".($overlap-$PMS)."\t".$SMS."\t".$PMS;
print OUT1 join("\t",@a),"\n";
print OUT3 join("\t",@a),"\n";
next;
}
}
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# in_len out_len len chr seq_s len pos_s pos_e str chr seq_s len pos_s pos_e strand
# newid-16829/1__5 151 151 151 1 0 75 157809618 157809692 + 1 71 80 157800282 157800361 +
# newid-16829/1__5 151 151 151 1 0 73 157809618 157809690 + 1 73 78 157800284 157800361 + -9411 14.50 6.04 8.46 - 4 2 2 -3 2
else {
$SMscoren=-99999;
$left_max=0;
$left_pos=0;
$right_max=0;
$right_pos=0;
for(my $k=0; $k<=$overlap; $k++) {
my $str1="";
$str1=uc substr($left_seq,(2*$Extend+1-$PMS-3),9);
my $str2="";
$str2=uc substr($right_seq,(2*$Extend-2+$overlap-$PMS-20+2),23);
if ((length($str1) eq 9) and (length($str2) eq 23)) {
my $left_maxt=sprintf("%.2f",&log2(&scoreconsensus5($str1)*$me2x5{$seq{&getrest5($str1)}}));
my $right_maxt=sprintf("%.2f", &log2(&scoreconsensus3($str2)*&maxentscore(&getrest3($str2),\@metables)));
my $sumt=sprintf("%.2f",($left_maxt + $right_maxt));
if ($SMscoren < $sumt) {
$SMscoren=$sumt;
$PMS=$k;
$left_pos=$k;
$left_max=$left_maxt;
$right_pos=$overlap-$k;
$right_max=$right_maxt;
$left_flag=-5;
$right_flag=-3;
}
}
}
if ($debug eq 1){print $SMscoren,"\t",$PMS,"\t",($overlap-$PMS),"\t",$overlap,"\n";}
$a[9]=$a[9]."\t".$left_seq."\t".$left_max."\t".$PMS."\t"."-5";
$a[15]=$a[15]."\t".$right_seq."\t".$right_max."\t".($overlap-$PMS)."\t"."-3";
$a[15]=$a[15]."\t".$SMscoren."\t".$SMS."\t".$PMS;
print OUT join("\t",@a),"\n";
my @a=split("\t",$READ{$id});
$a[6]=$a[6]-($overlap-$PMS);
$a[8]=$a[8]-($overlap-$PMS);
$a[11]=$a[11]+$PMS;
$a[12]=$a[12]-$PMS;
if ($a[15] eq "-") { $a[14]=$a[14]-$PMS; }
else { $a[13]=$a[13]+$PMS; }
$a[15]=$a[15]."\t".$SMscoren."\t".sprintf("%.2f",$left_max)."\t".sprintf("%.2f",$right_max)."\t"."-"."\t".$overlap."\t".$PMS."\t".($overlap-$PMS)."\t".$SMS."\t".$PMS;
print OUT2 join("\t",@a),"\n";
print OUT3 join("\t",@a),"\n";
}
}
}
}
close IN;
close OUT;
close OUT1;
close OUT2;
close OUT3;
close OUTerr;
close OUTp;