-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSubexonInfo.cpp
1214 lines (1082 loc) · 35.3 KB
/
SubexonInfo.cpp
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
// report the total depth for the segment
// Format: ./a.out alignments.bam splice_site
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include "alignments.hpp"
#include "blocks.hpp"
#include "stats.hpp"
#define ABS(x) ((x)<0?-(x):(x))
char usage[] = "./subexon-info alignment.bam intron.splice [options]\n"
"options:\n"
"\t--minDepth INT: the minimum coverage depth considered as part of a subexon (default: 2)\n"
"\t--noStats: do not compute the statistical scores (default: not used)\n" ;
char buffer[4096] ;
int gMinDepth ;
bool CompSplitSite( struct _splitSite a, struct _splitSite b )
{
if ( a.chrId < b.chrId )
return true ;
else if ( a.chrId > b.chrId )
return false ;
else if ( a.pos != b.pos )
return a.pos < b.pos ;
else if ( a.type != b.type )
return a.type < b.type ; // We want the start of exons comes first, since we are scanning the genome from left to right,
// so we can terminate the extension early and create a single-base subexon later.
else
return a.oppositePos < b.oppositePos ;
}
bool CompBlocksByAvgDepth( struct _block a, struct _block b )
{
double avgA = a.depthSum / (double)( a.end - a.start + 1 ) ;
double avgB = b.depthSum / (double)( b.end - b.start + 1 ) ;
return avgA < avgB ;
}
bool CompBlocksByRatio( struct _block a, struct _block b )
{
return a.ratio < b.ratio ;
}
int CompDouble( const void *p1, const void *p2 )
{
double a = *(double *)p1 ;
double b = *(double *)p2 ;
if ( a > b )
return 1 ;
else if ( a < b )
return -1 ;
else
return 0 ;
}
// Clean up the split sites;
void FilterAndSortSplitSites( std::vector<struct _splitSite> &sites )
{
std::sort( sites.begin(), sites.end(), CompSplitSite ) ;
int i, j, k, l ;
int size = sites.size() ;
for ( i = 0 ; i < size ; )
{
for ( j = i + 1 ; j < size ; ++j )
if ( sites[j].chrId != sites[i].chrId || sites[j].pos != sites[i].pos || sites[j].type != sites[i].type )
break ;
int maxSupport = 0 ;
for ( k = i ; k < j ; ++k )
if ( sites[k].support > maxSupport )
maxSupport = sites[k].support ;
int strandCnt[2] = {0, 0} ;
char strand = sites[i].strand ;
for ( k = i ; k < j ; ++k )
{
if ( sites[k].strand == '-' )
strandCnt[0] += sites[k].support ;
else if ( sites[k].strand == '+' )
strandCnt[1] += sites[k].support ;
}
if ( strandCnt[0] > strandCnt[1] )
strand = '-' ;
else if ( strandCnt[1] > strandCnt[0] )
strand = '+' ;
bool allOneExceptMax = false ;
if ( maxSupport >= 20 )
{
allOneExceptMax = true ;
for ( k = i ; k < j ; ++k )
{
if ( sites[k].support == maxSupport )
continue ;
if ( sites[k].support > 1 )
{
allOneExceptMax = false ;
break ;
}
}
}
for ( k = i ; k < j ; ++k )
{
if ( ( sites[k].support < 0.01 * maxSupport && sites[k].support <= 3 )
|| sites[k].support < 0.001 * maxSupport || sites[k].strand != strand // The introns from the different strand are filtered.
|| ( sites[k].support < 0.02 * maxSupport && sites[k].mismatchSum >= 2 * sites[k].support )
|| ( allOneExceptMax && sites[k].support == 1 )
|| ( sites[k].support <= 2 && sites[k].mismatchSum >= 2 * sites[k].support )
|| ( maxSupport >= 2 && sites[k].support == 1 && ( ABS( sites[k].oppositePos - sites[k].pos - 1 ) >= 10000 || sites[k].mismatchSum != 0 ) ) )
{
for ( l = i - 1 ; l >= 0 && sites[l].chrId == sites[i].chrId && sites[l].pos >= sites[k].oppositePos ; --l )
{
if ( sites[l].pos == sites[k].oppositePos && sites[l].oppositePos == sites[k].pos )
{
sites[l].support = -1 ;
sites[k].support = -1 ;
break ;
}
}
for ( l = j ; l < size && sites[l].chrId == sites[i].chrId && sites[l].pos <= sites[k].oppositePos ; ++l )
{
if ( sites[l].pos == sites[k].oppositePos && sites[l].oppositePos == sites[k].pos )
{
sites[l].support = -1 ;
sites[k].support = -1 ;
break ;
}
}
}
/*else if ( sites[k].support <= 1 && sites[k].oppositePos - sites[k].pos + 1 >= 30000 )
{
for ( l = j ; l < size && sites[l].chrId == sites[i].chrId && sites[l].pos <= sites[k].oppositePos ; ++l )
{
if ( sites[l].pos == sites[k].oppositePos && sites[l].oppositePos == sites[k].pos )
{
if ( l - j >= 20 )
{
sites[l].support = -1 ;
sites[k].support = -1 ;
break ;
}
}
}
}*/
}
i = j ;
}
k = 0 ;
for ( i = 0 ; i < size ; ++i )
{
if ( sites[i].support > 0 )
{
sites[k] = sites[i] ;
if ( sites[k].strand == '?' )
sites[k].strand = '.' ;
++k ;
}
}
sites.resize( k ) ;
}
// Remove the same sites.
void KeepUniqSplitSites( std::vector< struct _splitSite> &sites )
{
int i, j ;
int size = sites.size() ;
int k = 0 ;
for ( i = 0 ; i < size ; )
{
for ( j = i + 1 ; j < size ; ++j )
if ( sites[j].chrId != sites[i].chrId || sites[j].pos != sites[i].pos || sites[j].type != sites[i].type )
break ;
sites[k] = sites[i] ;
++k ;
/*else
{
if ( sites[i].type != sites[k-1].type )
{
printf( "%d\n", sites[i].pos ) ;
}
}*/
i = j ;
}
sites.resize( k ) ;
// For the sites that corresponds to the start of an exon, we remove the adjust to it and
/*for ( i = 1 ; i < size ; ++i )
{
if ( sites[i].pos == sites[i - 1].pos && sites[i - 1].type == 2 )
{
if ( sites[i].type == 1 )
++sites[i].pos ;
}
}*/
}
// Filter split sites that are extremely close to each other but on different strand.
void FilterNearSplitSites( std::vector< struct _splitSite> &sites )
{
int i, j ;
int size = sites.size() ;
int k = 0 ;
for ( i = 0 ; i < size - 1 ; ++i )
{
if ( sites[i].support < 0 || sites[i].type != sites[i + 1].type || sites[i].chrId != sites[i + 1].chrId )
continue ;
if ( sites[i + 1].pos - sites[i].pos <= 7 &&
( sites[i + 1].strand != sites[i].strand ||
sites[i].strand == '?' ) )
{
int tag = i ;
if ( sites[i + 1].support < sites[i].support )
tag = i + 1 ;
sites[tag].support = -1 ;
int direction ;
if ( sites[tag].oppositePos < sites[tag].pos )
direction = -1 ;
else
direction = 1 ;
for ( j = tag ; j >= 0 && j < size ; j += direction )
if ( sites[j].pos == sites[tag].oppositePos && sites[j].oppositePos == sites[tag].pos )
{
sites[j].support = -1 ;
break ;
}
}
}
for ( i = 0 ; i < size ; ++i )
{
if ( sites[i].support > 0 )
{
sites[k] = sites[i] ;
++k ;
}
}
sites.resize( k ) ;
}
void FilterRepeatSplitSites( std::vector<struct _splitSite> &sites )
{
int i, j ;
int size = sites.size() ;
int k = 0 ;
for ( i = 0 ; i < size ; )
{
for ( j = i + 1 ; j < size ; ++j )
{
if ( sites[j].pos != sites[i].pos || sites[j].type != sites[i].type || sites[i].chrId != sites[j].chrId )
break ;
}
int max = -1 ;
int maxtag = 0 ;
for ( k = i ; k < j ; ++k )
{
if ( sites[k].uniqSupport > max )
{
max = sites[k].uniqSupport ;
maxtag = k ;
}
else if ( sites[k].uniqSupport == max && sites[k].support > sites[maxtag].support )
{
maxtag = k ;
}
}
if ( max > -1 )
{
if ( sites[maxtag].uniqSupport > sites[maxtag].support * 0.1 )
{
for ( k = i ; k < j ; ++k )
if ( sites[k].uniqSupport < 0.05 * sites[k].support )
{
sites[k].support = -1 ;
int direction ;
if ( sites[k].oppositePos < sites[k].pos )
direction = -1 ;
else
direction = 1 ;
int l ;
for ( l = k ; l >= 0 && l < size ; l += direction )
if ( sites[l].pos == sites[k].oppositePos && sites[l].oppositePos == sites[k].pos )
{
sites[l].support = -1 ;
break ;
}
}
}
else
{
for ( k = i ; k < j ; ++k )
{
if ( sites[k].support <= 10 )
{
sites[k].support = -1 ;
int direction ;
if ( sites[k].oppositePos < sites[k].pos )
direction = -1 ;
else
direction = 1 ;
int l ;
for ( l = k ; l >= 0 && l < size ; l += direction )
if ( sites[l].pos == sites[k].oppositePos && sites[l].oppositePos == sites[k].pos )
{
sites[l].support = -1 ;
break ;
}
}
}
}
}
i = j ;
}
k = 0 ;
for ( i = 0 ; i < size ; ++i )
{
if ( sites[i].support > 0 )
{
sites[k] = sites[i] ;
++k ;
}
}
sites.resize( k ) ;
}
// When k=1, the gamma distribution becomes exponential distribution, and can be optimized analytically..
// Maximize: sum_i z_i( log( 1/theta e^{-x_i / theta} )
/*double ThetaOfExponentialDistribution( double *x, double *z, int n )
{
double sumZ = 0;
double sumZX = 0 ;
for ( i = 0 ; i < n ; ++i )
{
sumZ += z[i] ;
sumZX += z[i] * x[i] ;
}
}*/
// for boundK, if it is positive, it represent the upper bound. If it is negative, -boundK will be the lower bound for k.
// if boundK==0, there is no extra bound.
// The same logic for boundProduct, which bounds k*theta
void GradientDescentGammaDistribution( double &k, double &theta, double initK, double initTheta, double lowerBoundK, double upperBoundK,
double lowerBoundMean, double upperBoundMean, double *x, double *z, int n )
{
int i ;
k = initK ;
theta = initTheta ;
double c = 0.5 ;
int iterCnt = 1 ;
double sumZ = 0 ;
double sumZX = 0 ;
double sumZLogX = 0 ;
double Hessian[2][2] ; // 0 for k, 1 for theta
double inverseHessian[2][2] ;
int tmp ;
double positiveKRecord = -5, positiveThetaRecord = -5 ; // record the value of k, theta when theta, k becomes non-positive.
for ( i = 0 ; i < n ; ++i )
{
sumZ += z[i] ;
sumZX += z[i] * x[i] ;
sumZLogX += z[i] * log( x[i] ) ;
}
while ( 1 )
{
double gradK = 0 ;
double gradTheta = 0 ;
double prevK = k ;
double prevTheta = theta ;
double digammaK = digammal( k ) ;
gradK = sumZ * ( -log( theta ) - digammaK ) + sumZLogX ;
gradTheta = -sumZ * ( k / theta ) + sumZX / ( theta * theta ) ;
Hessian[0][0] = -sumZ * trigamma( k, &tmp ) ;
Hessian[0][1] = -sumZ / theta ; // \partial l / ( \partial k \partial theta)
Hessian[1][0] = -sumZ / theta ;
Hessian[1][1] = sumZ * k / ( theta * theta ) - 2 * sumZX / ( theta * theta * theta ) ;
double det = Hessian[0][0] * Hessian[1][1] - Hessian[0][1] * Hessian[1][0] ;
/*printf( "%s iter %d:\n", __func__, iterCnt ) ;
printf( "%lf %lf %lf %lf\n", sumZ, k, theta, sumZX ) ;
printf( "%lf %lf %lf\n", gradK, gradTheta, det ) ;
printf( "%lf %lf %lf %lf\n", Hessian[0][0], Hessian[0][1], Hessian[1][0], Hessian[1][1] ) ;*/
if ( det <= 1e-4 && det >=-1e-4 )
{
k = k + c / iterCnt * gradK ;
theta = theta + c / iterCnt * gradTheta ;
}
else
{
inverseHessian[0][0] = Hessian[1][1] / det ;
inverseHessian[0][1] = -Hessian[0][1] / det ;
inverseHessian[1][0] = -Hessian[1][0] / det ;
inverseHessian[1][1] = Hessian[0][0] / det ;
//printf( "%lf %lf %lf %lf: %lf\n=====\n", inverseHessian[0][0], inverseHessian[0][1], inverseHessian[1][0], inverseHessian[1][1],
// Hessian[1][0] * inverseHessian[0][1] + Hessian[1][1] * inverseHessian[1][1] ) ;
double step = 0.5 ;
k = k - step * ( inverseHessian[0][0] * gradK + inverseHessian[0][1] * gradTheta ) ;
theta = theta - step * ( inverseHessian[1][0] * gradK + inverseHessian[1][1] * gradTheta ) ;
bool flag = false ;
if ( k <= 1e-6 )
{
step = ( prevK - 1e-6 ) / ( inverseHessian[0][0] * gradK + inverseHessian[0][1] * gradTheta ) ;
if ( ABS( theta - positiveThetaRecord ) < 1e-5 )
flag = true ;
positiveThetaRecord = theta ;
}
if ( theta <= 1e-6 )
{
double tmp = ( prevTheta - 1e-6 ) / ( inverseHessian[1][0] * gradK + inverseHessian[1][1] * gradTheta ) ;
if ( tmp < step )
step = tmp ;
if ( ABS( k - positiveKRecord ) < 1e-5 )
flag = true ;
positiveKRecord = k ;
}
if ( step != 0.5 )
{
k = prevK - step * ( inverseHessian[0][0] * gradK + inverseHessian[0][1] * gradTheta ) ;
theta = prevTheta - step * ( inverseHessian[1][0] * gradK + inverseHessian[1][1] * gradTheta ) ;
}
/*if ( flag )
{
k = prevK ;
theta = prevTheta ;
break ;
}*/
}
if ( upperBoundK > 0 && k > upperBoundK )
k = upperBoundK ;
else if ( lowerBoundK > 0 && k < lowerBoundK )
k = lowerBoundK ;
if ( upperBoundMean > 0 && k * theta > upperBoundMean )
{
theta = upperBoundMean / k ;
}
else if ( lowerBoundMean > 0 && k * theta < lowerBoundMean )
{
theta = lowerBoundMean / k ;
}
if ( k <= 1e-6 )
{
k = 1e-6 ;
}
if ( theta <= 1e-6 )
{
theta = 1e-6 ;
}
double diff = ABS( prevK - k ) + ABS( prevTheta - theta ) ;
if ( diff < 1e-5 )
break ;
++iterCnt ;
//if ( det <= 1e-4 && det >=-1e-4 && k >= 5000 ) //&& diff < 1 )
if ( k >= 10000 ) //&& diff < 1 )
{
k = prevK ;
theta = prevTheta ;
break ;
}
if ( iterCnt == 1000 )
break ;
}
}
double MixtureGammaEM( double *x, int n, double &pi, double *k, double *theta, int tries, double meanBound[2], int iter = 1000 )
{
int i ;
if ( n <= 0 )
return 0 ;
double *z = new double[n] ; // the expectation that it assigned to model 0.
double *oneMinusZ = new double[n] ;
int t = 0 ;
double history[5] = {-1, -1, -1, -1, -1} ;
double maxX = -1 ;
double sumX = 0 ;
for ( i = 0 ; i < n ; ++i )
{
sumX += x[i] ;
if ( x[i] > maxX )
maxX = x[i] ;
}
if ( maxX > meanBound[1] && meanBound[1] >= 0 )
maxX = meanBound[1] ;
/*if ( meanBound[1] == -1 )
{
// The EM for coverage
maxX = 10.0 ;
}*/
while ( 1 )
{
double npi, nk[2], ntheta[2] ;
double sum = 0 ;
for ( i = 0 ; i < n ; ++i )
{
//double lf0 = -k[0] * log( theta[0] ) + ( k[0] - 1 ) * log( cov[i]) - cov[i] / theta[0] - lgamma( k[0] );
//double lf1 = -k[1] * log( theta[1] ) + ( k[1] - 1 ) * log( cov[i]) - cov[i] / theta[1] - lgamma( k[1] );
//z[i] = exp( lf0 + log( pi ) ) / ( exp( lf0 + log( pi ) ) + exp( lf1 + log( 1 - pi ) ) ) ;
if ( pi != 0 )
z[i] = MixtureGammaAssignment( x[i], pi, k, theta ) ;
else
z[i] = 0 ;
/*if ( isnan( z[i] ) )
{
printf( "nan: %lf %lf %lf %lf\n", x[i], pi, k, theta ) ;
}*/
oneMinusZ[i] = 1 - z[i] ;
sum += z[i] ;
}
// compute new pi.
npi = sum / n ;
// Use gradient descent to compute new k and theta.
if ( 1 ) //pi > 0 )
{
double bound ;
if ( meanBound[1] != -1 ) // the EM for ratio
{
bound = ( theta[1] * k[1] > 1 ) ? 1 : ( theta[1] * k[1] ) / ( 1 + tries );
GradientDescentGammaDistribution( nk[0], ntheta[0], k[0], theta[0], k[1], -1, -1, bound, x, z, n ) ; // It seems setting an upper bound 1 for k[0] is not a good idea.
}
else
{
bound = ( theta[1] * k[1] > 1 ) ? 1 : ( theta[1] * k[1] ) / ( 1 + tries ) ;
GradientDescentGammaDistribution( nk[0], ntheta[0], k[0], theta[0], k[1], -1, meanBound[0], bound, x, z, n ) ; // It seems setting an upper bound 1 for k[0] is not a good idea.
}
GradientDescentGammaDistribution( nk[1], ntheta[1], k[1], theta[1], -1, k[0], theta[0] * k[0], maxX, x, oneMinusZ, n ) ;
}
else
{
GradientDescentGammaDistribution( nk[1], ntheta[1], k[1], theta[1], 0, 0, 0, 0, x, oneMinusZ, n ) ;
}
double diff ;
if ( isnan( npi ) || isnan( nk[0] ) || isnan( nk[1] ) || isnan( ntheta[0] ) || isnan( ntheta[1] ) )
{
delete[] z ;
delete[] oneMinusZ ;
return -1 ;
}
diff = ABS( nk[0] - k[0] ) + ABS( nk[1] - k[1] )
+ ABS( ntheta[0] - theta[0] ) + ABS( ntheta[1] - theta[1] ) ; // pi is fully determined by these 4 parameters.
if ( diff < 1e-4 )
break ;
diff = ABS( nk[0] - history[1] ) + ABS( nk[1] - history[2] )
+ ABS( ntheta[0] - history[3] ) + ABS( ntheta[1] - history[4] ) ; // pi is fully determined by these 4 parameters.
if ( diff < 1e-4 )
break ;
history[0] = pi ;
history[1] = k[0] ;
history[2] = k[1] ;
history[3] = theta[0] ;
history[4] = theta[1] ;
pi = npi ;
k[0] = nk[0] ;
k[1] = nk[1] ;
theta[0] = ntheta[0] ;
theta[1] = ntheta[1] ;
/*double logLikelihood = 0 ;
for ( i = 0 ; i < n ; ++i )
logLikelihood += log( pi * exp( LogGammaDensity( x[i], k[0], theta[0]) ) +
(1 - pi ) * exp( LogGammaDensity( x[i], k[1], theta[1] ) ) ) ;*/
//printf( "%d: %lf %lf %lf %lf %lf\n", t, pi, k[0], theta[0], k[1], theta[1] ) ;
++t ;
if ( iter != -1 && t >= iter )
break ;
}
delete[] z ;
delete[] oneMinusZ ;
return 0 ;
}
bool IsParametersTheSame( double *k, double *theta )
{
if ( ABS( k[0] - k[1] ) < 1e-2 && ABS( theta[0] - theta[1] ) < 1e-2 )
return true ;
return false ;
}
int RatioAndCovEM( double *covRatio, double *cov, int n, double &piRatio, double kRatio[2],
double thetaRatio[2], double &piCov, double kCov[2], double thetaCov[2] )
{
int i ;
piRatio = 0.6 ; // mixture coefficient for model 0 and 1
kRatio[0] = 0.9 ;
kRatio[1] = 0.45 ;
thetaRatio[0] = 0.05 ;
thetaRatio[1] = 1 ;
double meanBound[2] = {-1, 1} ; // [0] is for the lower bound of the noise model, [1] is for the upper bound of the true model
/*double *filteredCovRatio = new double[n] ;// ignore the ratio that is greater than 5.
int m = 0 ;
for ( i = 0 ; i < n ; ++i )
if ( covRatio[i] < 1.0 )
{
filteredCovRatio[m] = covRatio[i] ;
++m ;
}*/
srand( 17 ) ;
int maxTries = 10 ;
int t = 0 ;
double *buffer = new double[n] ;
for ( i = 0 ; i < n ; ++i )
buffer[i] = covRatio[i] ;
qsort( buffer, n, sizeof( double ), CompDouble ) ;
//covRatio = buffer ;
while ( 1 )
{
//printf( "EM\n" ) ;
MixtureGammaEM( covRatio, n, piRatio, kRatio, thetaRatio, t, meanBound ) ;
//printf( "%lf %lf %lf %lf %lf\n", piRatio, kRatio[0], kRatio[1], thetaRatio[0], thetaRatio[1] ) ;
if ( piRatio > 0.999 || piRatio < 0.001 || IsParametersTheSame( kRatio, thetaRatio ) )
{
++t ;
if ( t > maxTries )
break ;
piRatio = 0.6 ;
kRatio[0] += ( ( rand() * 0.5 - RAND_MAX ) / (double)RAND_MAX * 0.1 ) ;
if ( kRatio[0] <= 0 )
kRatio[0] = 0.9 ;
kRatio[1] += ( ( rand() * 0.5 - RAND_MAX ) / (double)RAND_MAX * 0.1 ) ;
if ( kRatio[1] <= 0 )
kRatio[1] = 0.45 ;
thetaRatio[0] += ( ( rand() * 0.5 - RAND_MAX ) / (double)RAND_MAX * 0.1 ) ;
if ( thetaRatio[0] <= 0 )
thetaRatio[0] = 0.05 ;
thetaRatio[1] += ( ( rand() * 0.5 - RAND_MAX ) / (double)RAND_MAX * 0.1 ) ;
if ( thetaRatio[1] <= 0 )
thetaRatio[1] = 1 ;
if ( kRatio[0] < kRatio[1] )
{
if ( rand() & 1 )
kRatio[0] = kRatio[1] ;
else
kRatio[1] = kRatio[0] ;
}
if ( kRatio[0] * thetaRatio[0] > kRatio[1] * thetaRatio[1] )
{
thetaRatio[0] = kRatio[1] * thetaRatio[1] / kRatio[0] ;
}
//printf( "%lf %lf %lf %lf %lf\n", piRatio, kRatio[0], kRatio[1], thetaRatio[0], thetaRatio[1] ) ;
continue ;
}
break ;
}
//delete[] filteredCovRatio ;
if ( t > maxTries && piRatio > 0.999 )
{
/*piRatio = 0.6 ; // mixture coefficient for model 0 and 1
kRatio[0] = 0.9 ;
kRatio[1] = 0.45 ;
thetaRatio[0] = 0.05 ;
thetaRatio[1] = 1 ;*/
piRatio = 0.999 ;
}
if ( IsParametersTheSame( kRatio, thetaRatio ) || piRatio <= 1e-3 )
piRatio = 1e-3 ;
piCov = piRatio ; // mixture coefficient for model 0 and 1
kCov[0] = 0.9 ;
kCov[1] = 0.45 ;
thetaCov[0] = 3 ;
thetaCov[1] = 12 ;
// only do one iteration of EM, so that pi does not change?
// But it seems it still better to run full EM.
meanBound[0] = 1.01 ;
meanBound[1] = -1 ;
//printf( "for coverage:\n" ) ;
//piCov = 0.001000 ;
MixtureGammaEM( cov, n, piCov, kCov, thetaCov, 0, meanBound ) ;
//printf( "for coverage done\n" ) ;
piCov = piRatio ;
delete []buffer ;
return 0 ;
}
double GetPValue( double x, double *k, double *theta )
{
int fault ;
double p ;
p = 1 - gammad( x / theta[0], k[0], &fault ) ;
return p ;
}
// if x's value is less than the average of (k0-1)*theta0, then we force x=(k0-1)*theta0,
// the mode of the model 0. Of course, it does not affect when k0<=1 already.
double MixtureGammaAssignmentAdjust( double x, double pi, double* k, double *theta )
{
if ( x < ( k[0] - 1 ) * theta[0] )
{
x = ( k[0] - 1 ) * theta[0] ;
}
return MixtureGammaAssignment( x, pi, k, theta ) ;
}
// Transform the cov number for better fitting
double TransformCov( double c )
{
double ret ;
// original it is c-1.
// Use -2 instead of -1 is that many region covered to 1 reads will be filtered when
// build the subexons.
//
//ret = sqrt( c ) - 1 ;
if ( c <= 2 + 1e-6 )
ret = 1e-6 ;
else
ret = c - 2 ;
return ret ;
//return log( c ) / log( 2.0 ) ;
}
int main( int argc, char *argv[] )
{
int i, j ;
bool noStats = false ;
if ( argc < 3 )
{
fprintf( stderr, usage ) ;
exit( 1 ) ;
}
gMinDepth = 2 ;
for ( i = 3 ; i < argc ; ++i )
{
if ( !strcmp( argv[i], "--noStats" ) )
{
noStats = true ;
continue ;
}
else if ( !strcmp( argv[i], "--minDepth" ) )
{
gMinDepth = atoi( argv[i + 1] ) ;
++i ;
continue ;
}
else
{
fprintf( stderr, "Unknown argument: %s\n", argv[i] ) ;
return 0 ;
}
}
Alignments alignments ;
alignments.Open( argv[1] ) ;
std::vector<struct _splitSite> splitSites ; // only compromised the
std::vector<struct _splitSite> allSplitSites ;
// read in the splice site
FILE *fp ;
fp = fopen( argv[2], "r" ) ;
char chrom[50] ;
int64_t start, end ;
int support ;
char strand[3] ;
int uniqSupport, secondarySupport, uniqEditDistance, secondaryEditDistance ;
while ( fscanf( fp, "%s %" PRId64 " %" PRId64 " %d %s %d %d %d %d", chrom, &start, &end, &support, strand,
&uniqSupport, &secondarySupport, &uniqEditDistance, &secondaryEditDistance ) != EOF )
{
if ( support <= 0 )
continue ;
//if ( !( uniqSupport >= 1
// || secondarySupport > 10 ) )
//if ( uniqSupport <= 0.01 * ( uniqSupport + secondarySupport ) || ( uniqSupport == 0 && secondarySupport < 20 ) )
//if ( uniqSupport == 0 && secondarySupport <= 10 )
// continue ;
int chrId = alignments.GetChromIdFromName( chrom ) ;
struct _splitSite ss ;
--start ;
--end ;
ss.pos = start ;
ss.chrId = chrId ;
ss.type = 2 ;
ss.oppositePos = end ;
ss.strand = strand[0] ;
ss.support = support ;
ss.uniqSupport = uniqSupport ;
ss.mismatchSum = uniqEditDistance + secondaryEditDistance ;
splitSites.push_back( ss ) ;
ss.pos = end ;
ss.type = 1 ;
ss.oppositePos = start ;
ss.strand = strand[0] ;
ss.support = support ;
ss.uniqSupport = uniqSupport ;
ss.mismatchSum = uniqEditDistance + secondaryEditDistance ;
splitSites.push_back( ss ) ;
}
fclose( fp ) ;
//printf( "ss:%d\n", splitSites.size() ) ;
//printf( "ss:%d\n", splitSites.size() ) ;
alignments.GetGeneralInfo( true ) ;
// Build the blocks
Blocks regions ;
alignments.Rewind() ;
regions.BuildExonBlocks( alignments ) ;
//printf( "%d\n", regions.exonBlocks.size() ) ;
FilterAndSortSplitSites( splitSites ) ;
FilterNearSplitSites( splitSites ) ;
FilterRepeatSplitSites( splitSites ) ;
regions.FilterSplitSitesInRegions( splitSites ) ;
regions.FilterGeneMergeSplitSites( splitSites ) ;
allSplitSites = splitSites ;
KeepUniqSplitSites( splitSites ) ;
//for ( i = 0 ; i < splitSites.size() ; ++i )
// printf( "%d %d\n", splitSites[i].pos + 1, splitSites[i].oppositePos + 1 ) ;
// Split the blocks using split site
regions.SplitBlocks( alignments, splitSites ) ;
//printf( "%d\n", regions.exonBlocks.size() ) ;
/*for ( i = 0 ; i < regions.exonBlocks.size() ; ++i )
{
struct _block &e = regions.exonBlocks[i] ;
printf( "%s %" PRId64 " %" PRId64 " %d %d\n", alignments.GetChromName( e.chrId ), e.start + 1, e.end + 1, e.leftType, e.rightType ) ;
}
return 0 ;*/
// Recompute the coverage for each block.
alignments.Rewind() ;
//printf( "Before computeDepth: %d\n", regions.exonBlocks.size() ) ;
regions.ComputeDepth( alignments ) ;
//printf( "After computeDepth: %d\n", regions.exonBlocks.size() ) ;
// Merge blocks that may have a hollow coverage by accident.
regions.MergeNearBlocks() ;
//printf( "After merge: %d\n", regions.exonBlocks.size() ) ;
// Put the intron informations
regions.AddIntronInformation( allSplitSites, alignments ) ;
//printf( "After add information.\n" ) ;
// Compute the average ratio against the left and right connected subexons.
regions.ComputeRatios() ;
//printf( "After compute ratios.\n" ) ;
//printf( "Finish building regions.\n" ) ;
if ( noStats )
{
// just output the subexons.
if ( realpath( argv[1], buffer ) == NULL )
{
strcpy( buffer, argv[1] ) ;
}
printf( "#%s\n", buffer ) ;
printf( "#fitted_ir_parameter_ratio: pi: -1 k0: -1 theta0: -1 k1: -1 theta1: -1\n" ) ;
printf( "#fitted_ir_parameter_cov: pi: -1 k0: -1 theta0: -1 k1: -1 theta1: -1\n" ) ;
int blockCnt = regions.exonBlocks.size() ;
for ( int i = 0 ; i < blockCnt ; ++i )
{
struct _block &e = regions.exonBlocks[i] ;
double avgDepth = (double)e.depthSum / ( e.end - e.start + 1 ) ;
printf( "%s %" PRId64 " %" PRId64 " %d %d %lf -1 -1 -1 -1 ", alignments.GetChromName( e.chrId ), e.start + 1, e.end + 1, e.leftType, e.rightType, avgDepth ) ;
int prevCnt = e.prevCnt ;
if ( i > 0 && e.start == regions.exonBlocks[i - 1].end + 1 &&
e.leftType == regions.exonBlocks[i - 1].rightType )
{
printf( "%d ", prevCnt + 1 ) ;
for ( j = 0 ; j < prevCnt ; ++j )
printf( "%" PRId64 " ", regions.exonBlocks[ e.prev[j] ].end + 1 ) ;
printf( "%" PRId64 " ", regions.exonBlocks[i - 1].end + 1 ) ;
}
else
{
printf( "%d ", prevCnt ) ;
for ( j = 0 ; j < prevCnt ; ++j )
printf( "%" PRId64 " ", regions.exonBlocks[ e.prev[j] ].end + 1 ) ;
}
int nextCnt = e.nextCnt ;
if ( i < blockCnt - 1 && e.end == regions.exonBlocks[i + 1].start - 1 &&
e.rightType == regions.exonBlocks[i + 1].leftType )
{
printf( "%d %" PRId64 " ", nextCnt + 1, regions.exonBlocks[i + 1].start + 1 ) ;
}
else
printf( "%d ", nextCnt ) ;
for ( j = 0 ; j < nextCnt ; ++j )
printf( "%" PRId64 " ", regions.exonBlocks[ e.next[j] ].start + 1 ) ;
printf( "\n" ) ;
}
return 0 ;
}
// Extract the blocks for different events.
int blockCnt = regions.exonBlocks.size() ;
std::vector<struct _block> irBlocks ; // The regions corresponds to intron retention events.
double *leftClassifier = new double[ blockCnt ] ;
double *rightClassifier = new double[ blockCnt ] ;
std::vector<struct _block> overhangBlocks ; //blocks like (...[ or ]...)
std::vector<struct _block> islandBlocks ; // blocks like (....)
for ( i = 0 ; i < blockCnt ; ++i )
{
int ltype = regions.exonBlocks[i].leftType ;
int rtype = regions.exonBlocks[i].rightType ;
leftClassifier[i] = -1 ;
rightClassifier[i] = -1 ;
//double avgDepth = (double)regions.exonBlocks[i].depthSum / ( regions.exonBlocks[i].end - regions.exonBlocks[i].start + 1 ) ;
if ( ltype == 2 && rtype == 1 )
{
// candidate intron retention.
// Note that when I compute the ratio, it is already made sure that the avgDepth>1.
double ratio = regions.PickLeftAndRightRatio( regions.exonBlocks[i] ) ;
//printf( "%lf %lf\n", regions.exonBlocks[i].leftRatio, regions.exonBlocks[i].rightRatio ) ;
if ( ratio > 0 )
{
regions.exonBlocks[i].ratio = ratio ;
irBlocks.push_back( regions.exonBlocks[i] ) ;
irBlocks[ irBlocks.size() - 1 ].contigId = i ;
}
}
else if ( ( ltype == 0 && rtype == 1 ) || ( ltype == 2 && rtype == 0 ) )
{
// subexons like (...[ or ]...)
double ratio ;
if ( ltype == 0 )
{
ratio = regions.exonBlocks[i].rightRatio ;
}
else if ( ltype == 2 )
{
ratio = regions.exonBlocks[i].leftRatio ;
}
if ( ratio > 0 )
{
regions.exonBlocks[i].ratio = ratio ;
overhangBlocks.push_back( regions.exonBlocks[i] ) ;
overhangBlocks[ overhangBlocks.size() - 1].contigId = i ;
}
}
else if ( ltype == 0 && rtype == 0 )
{
islandBlocks.push_back( regions.exonBlocks[i] ) ;
islandBlocks[ islandBlocks.size() - 1].contigId = i ;