-
Notifications
You must be signed in to change notification settings - Fork 1
/
FindRegions.cpp
2869 lines (2584 loc) · 81.3 KB
/
FindRegions.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
/**
Find a region with alternative splice event based on the depth(coverage) file and the junc file generated from Tophat.
*/
#include "FindRegions.h"
#define MAX_CHROM 100
#define ISLAND_COVER 10
#define ALL_CLEAN 0
extern int READS_LENGTH ;
extern int FRAG_LENGTH, FRAG_STD ;
extern bool VERBOSE ;
int BIN_SIZE = 50 ;
int BIN_RANGE = 10000 ;
int BIN_USE = 30 ; // The number of bins on one side of the interval that we will use
double IR_ALPHA = 6.0 ;
#define MAX_CURRENT_ISLANDS 10000
#define BIN_ALPHA 6.0
struct _chrom
{
char name[10] ;
//int end ;
} ;
FILE *fpDepth, *fpSplice, *fpDepthForGroup ;
struct _splice *splices, *tmpSplices ;
int scnt ;
// A gene with single exon. It is determined in the IsInExon function.
int singleExon[MAX_EXON][2], tmpSingleExon[MAX_EXON][2] ;
int seCnt = 0, tmpSeCnt = 0 ;
double noiseDepth, noiseSqDepth ;
int noiseCnt ; // the single exons regarded as noise. Computed in IsSingleExon
struct _chrom chroms[MAX_CHROM] ;
int chromCnt ;
int curChromInd ;
struct _readFile fpReads, fpReadsForGroup ;
struct _read cutReads[MAX_READ] ;
int cutReadCnt = 0 ;
int prevReadRegion[2] = { -1000, -1000 };
// The average coverage for the islands.
struct _islandCover
{
double avgCover[MAX_CURRENT_ISLANDS] ;
int pos[MAX_CURRENT_ISLANDS] ;
int used ;
} ;
struct _islandCover prevIslandCover, // The islands before and in the interval.
futureIslandCover ;
double islandDepth[MAX_LENGTH] ;
bool IsInExon( char *chrom, int spliceInd, bool isFuture ) ;
// The variables holding the exons of evidences.
struct _exon *evidenceExons ;
int eviExonCnt ;
// Some additional splice information used for finding regions.
struct _spliceInfo
{
int geneId ;
int regionId ;
// The coverage from this splice to the next splice
double depthSum ;
double depthSqSum ;
int depthCnt ;
double avgSoftDepth[2] ;
double softDepthSum[2] ;
int soft[2] ;
double threshold ; // The threshold decide whether a noise or a signal for the affiliated soft
int possibleIR ; // if the splice is ] [, is it a possible intron retention? 0-no, 1-yes, 2-might be a forced IR
} ;
struct _spliceInfo *spliceInfo ;
extern int CompInt( const void *p1, const void *p2 ) ;
extern int CompDouble( const void *p1, const void *p2 ) ;
// Get the median of the n numbers in array a.
double GetMedian( double *a, int n )
{
qsort( a, n, sizeof( a[0] ), CompDouble ) ;
return a[n / 2] ;
}
// Test whether there is a paired-end read spanning the cut junction site
bool NoPairReadsSpan( char *chrom, int cutStart, int cutEnd, int start, int end, bool isFuture, double CUT_THRESHOLD )
{
if ( cutEnd - cutStart > FRAG_LENGTH + 2 * FRAG_STD - 2 * READS_LENGTH )
return true ;
if ( isFuture )
return true ;
int rstart, rend ;
int i, k, offset[2] = {0,0} ;
bool flag ;
/*if ( start == 124839485 + 1 && end == 124839978 - 1 )
{
printf( "### Yoooo %d %d\n", cutStart, cutEnd ) ;
exit( 1 ) ;
}*/
rstart = cutStart - FRAG_LENGTH ;
if ( rstart < start )
{
rstart = start ;
offset[0] = MERGE_DISTANCE - 1 ;
}
rend = cutEnd + FRAG_LENGTH ;
if ( rend > end )
{
rend = end ;
offset[1] = MERGE_DISTANCE - 1 ;
}
//printf( "### %d %d %d\n", rstart, rend, prevReadRegion[1] ) ;
cutReadCnt = ExtractReads( fpReads, chrom, rstart,
rend, cutReads, prevReadRegion ) ;
int support = 0 ;
flag = false ;
for ( i = 0 ; i < cutReadCnt ; ++i )
{
k = cutReads[i].mateInd ;
if ( flag && cutReads[i].start == cutReads[i - 1].start )
continue ;
flag = false ;
if ( k == -1 || k < i )
continue ;
if ( cutReads[i].end > rstart + offset[0] && cutReads[k].start + offset[1] < rend &&
cutReads[i].end < cutStart && cutReads[k].start > cutEnd &&
cutReads[i].end < rend && cutReads[k].start > rstart &&cutReads[k].end - cutReads[i].start + 1 <= FRAG_LENGTH + 2 * FRAG_STD )
{
//if ( start == 124839486 && end == 124839977 )
// printf( "###### hi %d %d %d %d, %d %d,(%d,%d) (%d,%d)\n", cutStart, cutEnd, rstart, rend, i, k, cutReads[i].start, cutReads[i].end, cutReads[k].start, cutReads[k].end ) ;
++support ;
flag = true ;
if ( log( (double)support ) / log( LOG_BASE ) >= CUT_THRESHOLD )
return false ;
}
}
return true ;
}
void GetFutureIslands( char *chrom, int spliceInd )
{
fpos_t fpDepthPos, fpReadsPos ;
int soft[2] ;
int start = splices[ spliceInd ].pos ;
int tmpPrevReadRegion[2] = { prevReadRegion[0], prevReadRegion[1] };
fgetpos( fpDepth, &fpDepthPos ) ;
//fgetpos( fpDepth, &fpReadsPos ) ;
futureIslandCover.used = 0 ;
while ( spliceInd < scnt - 1 )
{
IsInExon( chrom, spliceInd, true ) ;
if ( futureIslandCover.used >= BIN_USE || splices[ spliceInd ].pos - start >= BIN_RANGE )
break ;
++spliceInd ;
}
prevReadRegion[0] = tmpPrevReadRegion[0] ;
prevReadRegion[1] = tmpPrevReadRegion[1] ;
fsetpos( fpDepth, &fpDepthPos ) ;
//fsetpos( fpDepth, &fpReadsPos ) ;
}
// According to the bins to decide the bin_alpha
// type - 0: noise covered all the intron. 1: noise creates alternative TSS or TES. 2: Single exon gene
double GetBinAlpha( int l, int r, int type, int pos = -1 )
{
//return 0 ;
//printf( "%d %d %d\n", l, r, pos ) ;
//return 2.0 ;
if ( type == 0 )
{
//if ( l == BIN_USE || r == BIN_USE )
// return 3.0 ;
//return 2.0 ;
return IR_ALPHA ;
}
else if ( type == 1 )
{
//if ( l == BIN_USE || r == BIN_USE )
// return 4.0 ;
if ( l >= 0.9 * BIN_USE || r >= 0.9 * BIN_USE )
return 4.0 ;
//if ( l >= 0.75 * BIN_USE || r >= 0.75 * BIN_USE )
// return 4.0 ;
if ( l >= 0.5 * BIN_USE || r >= 0.5 * BIN_USE )
return 3.0 ;
//if ( l >= 0.25 * BIN_USE || r >= 0.25 * BIN_USE )
return 2.0 ;
}
else if ( type == 2 )
{
if ( l == BIN_USE || r == BIN_USE )
return 6.0 ;
if ( l >= 0.9 * BIN_USE || r >= 0.9 * BIN_USE )
return 5.0 ;
if ( l >= 0.75 * BIN_USE || r >= 0.75 * BIN_USE )
return 4.0 ;
if ( l >= 0.5 * BIN_USE || r >= 0.5 * BIN_USE )
return 3.0 ;
//if ( l >= 0.25 * BIN_USE || r >= 0.25 * BIN_USE )
return 2.0 ;
//return 0 ;
}
//if ( l >= 3 || r >= 3 )
// return 2.0 ;
return 0 ;
}
// Test whether an island can become a single exon.
bool IsSingleExon( int from, int to, const struct _islandCover &prevIslandCover, const struct _islandCover &currIslandCover,
const struct _islandCover &futureIslandCover )
{
//if ( to - from + 1 < READS_LENGTH )
// return false ;
//return true ;
int i, j, k ;
int a, b ;
for ( i = 0 ; i < currIslandCover.used ; ++i )
{
if ( currIslandCover.pos[i] >= from )
break ;
}
a = i - 1 ;
for ( ; i < currIslandCover.used ; ++i )
{
if ( currIslandCover.pos[i] > to )
break ;
}
b = i ;
int l = 0, r = 0 ;
double islandCoverMean, islandCoverVar ;
double islandSum, islandSqSum ;
double avgCover ;
//printf( "%lf %d\n", islandSum, islandCnt ) ;
//printf( "%d\n", start - 1 ) ;
islandSum = 0 ;
islandSqSum = 0 ;
for ( i = a ; i >= 0 ; --i )
{
if ( l >= BIN_USE || currIslandCover.pos[i] < from - BIN_RANGE )
break ;
++l ;
islandSum += currIslandCover.avgCover[i] ;
islandSqSum += currIslandCover.avgCover[i] * currIslandCover.avgCover[i] ;
//printf( "island: %lf\n", islandCover->avgCover[i] ) ;
}
for ( i = prevIslandCover.used - 1 ; i >= 0 ; --i )
{
if ( l >= BIN_USE || prevIslandCover.pos[i] < from - BIN_RANGE )
break ;
++l ;
islandSum += prevIslandCover.avgCover[i] ;
islandSqSum += prevIslandCover.avgCover[i] * prevIslandCover.avgCover[i];
//printf( "island: %lf\n", islandCover->avgCover[i] ) ;
}
r = 0 ;
for ( i = b ; i < currIslandCover.used ; ++i )
{
if ( r >= BIN_USE || currIslandCover.pos[i] > to + BIN_RANGE )
break ;
++r ;
islandSum += currIslandCover.avgCover[i] ;
islandSqSum += currIslandCover.avgCover[i] * currIslandCover.avgCover[i] ;
//printf( "island: %lf\n", islandCover->avgCover[i] ) ;
}
for ( i = 0 ; i < futureIslandCover.used ; ++i )
{
if ( r >= BIN_USE || futureIslandCover.pos[i] > to + BIN_RANGE )
break ;
++r ;
islandSum += futureIslandCover.avgCover[i] ;
islandSqSum += futureIslandCover.avgCover[i] * futureIslandCover.avgCover[i] ;
//printf( "island: %lf\n", islandCover->avgCover[i] ) ;
}
if ( l + r > 4 )
{
islandCoverMean = islandSum / (double)( l + r ) ;
islandCoverVar = sqrt( islandSqSum / (double)(l + r) - islandCoverMean * islandCoverMean ) ;
}
else
{
islandCoverMean = 10 ;
islandCoverVar = 0 ;
}
avgCover = 0 ;
for ( i = a + 1 ; i < b ; ++i )
{
avgCover += currIslandCover.avgCover[i] ;
}
if ( b == a )
{
return false ;
}
avgCover /= ( b - a ) ; // Not accurate because of the last bin.
//printf( "(%d %d): %lf %lf %lf\n", from, to, avgCover, islandCoverMean, islandCoverVar * GetBinAlpha( l, r, 2 ) ) ;
if ( avgCover > islandCoverMean + islandCoverVar * GetBinAlpha( l, r, 2 ) )
{
//printf( "hi %d %d\n", from, to ) ;
return true ;
}
else
{
for ( i = a + 1 ; i < b ; ++i )
{
noiseDepth += currIslandCover.avgCover[i] ;
noiseSqDepth += currIslandCover.avgCover[i] * currIslandCover.avgCover[i] ;
++noiseCnt ;
}
return false ;
}
}
/**
Test wether a interval [start, end] is covered by some exons. Note: This interval is inside of a pair of splice sites.
If it can be cut, then it is possible to look like [ )...( ] the positions of )...( is returned in soft[2].
spliceInd is the left splice indices for that interval.
*/
bool IsInExon( char *chrom, int spliceInd, bool isFuture )
{
int i, j, k, tmp ;
char inChrom[50] ;
int pos = -1, idepth ;
int soft[2] = {-1, -1} ;
double depthSum = 0 ;
const int nextSpliceInd = spliceInd + 1 ;
int start, end ;
if ( spliceInd < 0 )
start = 0 ;
else
start = splices[ spliceInd ].pos + 1 ;
if ( nextSpliceInd >= scnt )
end = INF ;
else
end = splices[ nextSpliceInd ].pos - 1 ;
if ( end < start && !isFuture )
return true ;
static char newChrom[50] = "" ;
static int newPos, newDepth ;
double depth ;
int prevPos = start - 1 ;
int cutStart = -1, cutEnd ;
int binFill = 0, binPos ;
double binCover = 0 ;
int seStart, seEnd ; // Note: this start and end means the start and end of an exon.
bool first = true ;
bool covered ;
bool cut = false ;
int fret = 1 ;
struct _islandCover currIslandCover ;
currIslandCover.used = 0 ;
// Record the information of the islands in this interval.
double currCover = 0 ;
double leftIntervalCover = 0, rightIntervalCover = 0 ;
double leftIntervalAdjust = 0, rightIntervalAdjust = 0 ; // The total coverage around the splice junction, which is likely to be the mismatch.
bool differentStrands ;
tmpSeCnt = 0 ;
// Some constant
// When to ignore a gap
int CUT_MERGE ;
if ( spliceInd >= 0 && nextSpliceInd < scnt )
CUT_MERGE = ( splices[spliceInd].strand == splices[nextSpliceInd].strand &&
splices[spliceInd].type == 1 && splices[nextSpliceInd].type == 0 &&
splices[nextSpliceInd].pos - splices[spliceInd].pos < LONG_EXON ) ? 50 : 15 ;
else
CUT_MERGE = 15 ;
// When to regard this depth is actually empty.
double CUT_THRESHOLD ;
if ( spliceInd >= 0 && nextSpliceInd < scnt &&
splices[spliceInd].type == 0 && splices[nextSpliceInd].type == 1 )
{
CUT_THRESHOLD = CUT_THRESHOLD = log( 5.0 ) / log( LOG_BASE ) ;
}
else
CUT_THRESHOLD = 1 ;
//if ( !isFuture )
// printf( "IsInExon: %s %d %d\n", chrom, start, end ) ;
/*if ( end < start + CUT_MERGE )
{
if ( spliceInd >= 0 )
{
spliceInfo[ spliceInd ].depthSum = 0 ;
spliceInfo[ spliceInd ].possibleIR = false ;
}
return true ;
}*/
if ( !isFuture && nextSpliceInd < scnt )
{
GetFutureIslands( chrom, nextSpliceInd ) ;
}
if ( spliceInd >= 0 )
differentStrands = ( splices[ spliceInd ].strand != splices[ nextSpliceInd ].strand &&
splices[ spliceInd ].type != splices[ nextSpliceInd ].type ) ;
else
differentStrands = false ;
soft[0] = soft[1] = seStart = seEnd = -1 ;
//seCnt = 0 ;
if ( isFuture )
first = false ;
while ( 1 )
{
if ( fret == EOF )
break ;
if ( newChrom[0] && !strcmp( newChrom, chrom ) && nextSpliceInd == 0 && !isFuture)
{
strcpy( inChrom, newChrom ) ;
pos = newPos ;
idepth = newDepth ;
newChrom[0] = '\0' ;
}
else
{
if ( newChrom[0] && strcmp( newChrom, chrom ) && !isFuture )
{
// This case happens when the remaining splice sites of the chromosome is filter in depth
// or a chromosome with no splice sites.
int chromId = GetChromIdFromName( chrom ) ;
int newChromId = GetChromIdFromName( newChrom ) ;
if ( chromId < newChromId )
{
// the remaining splice sites in this chromsome is filtered in depth file.
strcpy( inChrom, chrom ) ;
idepth = 100000 ;
pos = end + 1 ;
}
else //if (chromId > newChromId)
{
// there is a chromosome with no splice sites
char prevChrom[50] ;
strcpy( prevChrom, newChrom ) ;
newChrom[0] = '\0' ;
while ( 1 )
{
fret = fscanf( fpDepth, "%s %d %d", inChrom, &pos, &idepth ) ;
if ( fret == EOF )
break ;
if ( !strcmp( prevChrom, inChrom ) )
continue ;
else
{
int inChromId = GetChromIdFromName( inChrom ) ;
if ( chromId < inChromId )
{
strcpy( inChrom, chrom ) ;
idepth = 100000 ;
pos = end + 1 ;
break ;
}
else if ( chromId == inChromId )
break ;
else // chromId > inChromId
{
strcpy( prevChrom, inChrom ) ;
continue ;
}
}
}
}
}
else
{
fret = fscanf( fpDepth, "%s %d %d", inChrom, &pos, &idepth ) ;
}
}
if ( fret == EOF )
{
pos = end + 1 ;
idepth = 100000 ;
}
/*if ( fret != EOF && first && strcmp( inChrom, chrom ) )
continue ;
if ( fret != EOF && ( !first || isFuture ) && strcmp( inChrom, chrom ) )
{
if ( !isFuture )
{
strcpy( newChrom, inChrom ) ;
newPos = pos ;
newDepth = idepth ;
}
idepth = 100000 ;
pos = end + 1 ;
//break ;
}*/
if ( fret != EOF && strcmp( inChrom, chrom ) )
{
if ( !isFuture )
{
// Let the logic at the beginning in this while(1) to decide whether to stop or keep reading.
// It might happen that the first depth position we get is from next chromosome,
// e.g. the first chromosome is filtered in depth file.
strcpy( newChrom, inChrom ) ;
newPos = pos ;
newDepth = idepth ;
continue ;
}
else
{
idepth = 100000 ;
pos = end + 1 ;
}
}
//if ( !isFuture )
// printf( "FindRegions: %s %d %d\n", inChrom, pos, idepth ) ;
if ( idepth )
depth = log( (double)idepth ) / log( LOG_BASE ) ;
else
depth = 0 ;
if ( pos < start )
continue ;
else if ( pos > end && ( nextSpliceInd < scnt || depth >= CUT_THRESHOLD ) ) // Now pos should be as far as the splice site.
{
if ( prevPos < pos - 1 && cutStart == -1 )
cutStart = prevPos + 1 ;
if ( cutStart != -1 )
{
// There may be a gap before the end.
cutEnd = pos - 1 ;
if ( cutEnd - cutStart + 1 > CUT_MERGE && ( differentStrands || NoPairReadsSpan( chrom, cutStart, cutEnd, start, end, isFuture, CUT_THRESHOLD ) ) )
{
if ( soft[0] == -1 )
{
soft[0] = cutStart - 1 ;
if ( cutStart != start )
{
leftIntervalCover = ( currCover - leftIntervalAdjust ) /
(double)( cutStart - start - 3 * MERGE_DISTANCE + 1 ) ;
if ( spliceInd >= 0 )
spliceInfo[ spliceInd ].softDepthSum[1] = currCover - leftIntervalAdjust ;
}
}
soft[1] = cutEnd + 1 ;
if ( seStart != -1 )
{
if ( !isFuture )
{
tmpSingleExon[ tmpSeCnt ][0] = seStart ;
tmpSingleExon[ tmpSeCnt ][1] = cutStart - 1 ;
++tmpSeCnt ;
}
for ( i = seStart ; i + BIN_SIZE < cutStart - 1 &&
currIslandCover.used < MAX_CURRENT_ISLANDS ; i += BIN_SIZE )
{
binCover = 0 ;
for ( j = i ; j < i + BIN_SIZE ; ++j )
binCover += islandDepth[j - seStart] ;
currIslandCover.avgCover[ currIslandCover.used ] = binCover / (double)BIN_SIZE ;
//currIslandCover.avgCover[ currIslandCover.used ] = GetMedian( &islandDepth[i - seStart], BIN_SIZE ) ;
currIslandCover.pos[ currIslandCover.used ] = i ;
++currIslandCover.used ;
}
if ( cutStart - 1 - i >= BIN_SIZE / 2 && currIslandCover.used < MAX_CURRENT_ISLANDS )
{
binCover = 0 ;
for ( j = i ; j < cutStart - 1 ; ++j )
binCover += islandDepth[j - seStart] ;
currIslandCover.avgCover[ currIslandCover.used ] = binCover / (double)BIN_SIZE ;
//currIslandCover.avgCover[ currIslandCover.used ] = GetMedian( &islandDepth[i - seStart], cutStart - 1 - i ) ;
currIslandCover.pos[ currIslandCover.used ] = i ;
++currIslandCover.used ;
}
}
cut = true ;
currCover = 0 ;
binCover = 0 ;
}
}
rightIntervalCover= ( currCover - rightIntervalAdjust )/ (double)( end - soft[1] + 1 - 3 * MERGE_DISTANCE + 1 ) ;
//if ( soft[1] == -1 && rightIntervalCover > 0 && nextSpliceInd == 0 )
// soft[1] = start ;
if ( nextSpliceInd < scnt )
spliceInfo[ nextSpliceInd ].softDepthSum[0] = currCover - rightIntervalAdjust ;
break ;
}
if ( pos > prevPos + 1 )
{
//return false ;
if ( cutStart == -1 )
cutStart = prevPos + 1 ;
cutEnd = pos - 1 ;
//cut = true ;
}
if ( pos < start - 1 + 3 * MERGE_DISTANCE )
leftIntervalAdjust += depth ;
if ( pos > end + 1 - 3 * MERGE_DISTANCE )
rightIntervalAdjust += depth ;
depthSum += depth ;
if ( spliceInd >= 0 && nextSpliceInd < scnt &&
splices[ spliceInd ].type == 0 && splices[ nextSpliceInd ].type == 1 &&
( pos < start - 1 + 3 * MERGE_DISTANCE || pos > end + 1 - 3 * MERGE_DISTANCE ) )
depthSum -= depth ;
if ( depth < CUT_THRESHOLD && ( evidenceExons == NULL //|| ( spliceInd >= 0 && nextSpliceInd < scnt && splices[spliceInd].type == 0 && splices[nextSpliceInd].type == 1 )
|| idepth == 0 || !IsPosInEvidenceExon( pos, evidenceExons, eviExonCnt ) ) )
{
if ( cutStart == -1 )
{
//if ( !isFuture ) printf( "cutstart %d %d\n", pos, idepth ) ;
cutStart = pos ;
}
cutEnd = pos ;
//cut = true ;
}
else
{
if ( first && nextSpliceInd == 0 )
{
seStart = pos ;
soft[1] = pos ;
//printf( "%d\n", cutStart ) ;
}
else if ( cutStart != -1 && cutEnd - cutStart + 1 > CUT_MERGE && ( differentStrands || NoPairReadsSpan( chrom, cutStart, cutEnd, start, end, isFuture, CUT_THRESHOLD ) ) )
{
//printf( "### %d %d %d\n", cutStart, cutEnd, differentStrands ) ;
// The two soft boundary should be in the same exon.
if ( soft[0] == -1 )
{
soft[0] = cutStart - 1 ;
if ( cutStart != start )
{
leftIntervalCover = ( currCover - leftIntervalAdjust ) / (double)( cutStart - start ) ;
if ( spliceInd >= 0 )
spliceInfo[ spliceInd ].softDepthSum[1] = currCover - leftIntervalAdjust ;
}
}
soft[1] = cutEnd + 1 ;
if ( seStart != -1 )
{
if ( !isFuture )
{
tmpSingleExon[ tmpSeCnt ][0] = seStart ;
tmpSingleExon[ tmpSeCnt ][1] = cutStart - 1 ;
++tmpSeCnt ;
}
for ( i = seStart ; i + BIN_SIZE < cutStart - 1 && currIslandCover.used < MAX_CURRENT_ISLANDS ; i += BIN_SIZE )
{
binCover = 0 ;
for ( j = i ; j < i + BIN_SIZE ; ++j )
binCover += islandDepth[j - seStart] ;
currIslandCover.avgCover[ currIslandCover.used ] = binCover / (double)BIN_SIZE ;
//currIslandCover.avgCover[ currIslandCover.used ] = GetMedian( &islandDepth[i - seStart], BIN_SIZE ) ;
currIslandCover.pos[ currIslandCover.used ] = i ;
++currIslandCover.used ;
}
if ( cutStart - 1 - i >= BIN_SIZE / 2 && currIslandCover.used < MAX_CURRENT_ISLANDS )
{
binCover = 0 ;
for ( j = i ; j < cutStart - 1 ; ++j )
binCover += islandDepth[j - seStart] ;
currIslandCover.avgCover[ currIslandCover.used ] = binCover / (double)BIN_SIZE ;
//currIslandCover.avgCover[ currIslandCover.used ] = GetMedian( &islandDepth[i - seStart], cutStart - 1 - i ) ;
currIslandCover.pos[ currIslandCover.used ] = i ;
++currIslandCover.used ;
}
//printf( "island %d %d %d %d\n", start, currIslandCover.used, pos - binFill + 1, binFill ) ;
binFill = 0 ;
}
seStart = cutEnd + 1 ;
cut = true ;
currCover = 0 ;
}
cutStart = -1 ;
if ( depth >= CUT_THRESHOLD )
currCover += depth ;
}
//if ( seStart != -1 && pos - seStart < MAX_LENGTH )
//{
tmp = start ;
if ( seStart != -1 )
tmp = seStart ;
if ( ( seStart != -1 && pos - tmp < MAX_LENGTH ) ||
( !cut && spliceInd >= 0 && splices[spliceInd].type == 1 && splices[nextSpliceInd ].type == 0 &&
pos - tmp < MAX_LENGTH ) ) // Either it is in a island or a intron-retention portion.
{
for ( i = ( prevPos + 1 - tmp > 0 ? prevPos + 1 : tmp ) ; i < pos ; ++i )
islandDepth[i - tmp] = 0 ;
//printf( "%d %llf\n", pos - tmp, depth ) ;
islandDepth[pos - tmp] = depth ;
}
//}
first = false ;
prevPos = pos ;
}
//soft[1] = cutEnd + 1 ;
//if ( soft[1] == -1 && nextSpliceInd == 0 && )
if ( soft[0] < start - 1 + 3 * MERGE_DISTANCE )
soft[0] = -1 ;
if ( soft[1] > end + 1 - 3 * MERGE_DISTANCE )
soft[1] = -1 ;
if ( spliceInd >= 0 && !isFuture )
{
spliceInfo[ spliceInd ].soft[1] = soft[0] ;
spliceInfo[ spliceInd ].avgSoftDepth[1] = leftIntervalCover ;
}
if ( nextSpliceInd < scnt && !isFuture )
{
spliceInfo[ nextSpliceInd ].soft[0] = soft[1] ;
spliceInfo[ nextSpliceInd ].avgSoftDepth[0] = rightIntervalCover ;
}
//if ( soft[0] != -1 )
// printf( "left: %d %lf\n", start, leftIntervalCover ) ;
//if ( soft[1] != -1 )
// printf( "right: %d %lf\n", end, rightIntervalCover ) ;
/*if ( cut == true )
{
printf( "### %d %d %d %d\n", start, end, soft[0], soft[1] ) ;
}*/
if ( spliceInd >= 0 && !isFuture )
{
//if ( !isFuture && start == 155166029 )
// printf( "hi %d %d\n", start, end ) ;
spliceInfo[ spliceInd ].depthSum = depthSum ;
spliceInfo[ spliceInd ].possibleIR = 0 ;
if ( !cut && spliceInd >= 0 && nextSpliceInd < scnt &&
splices[ spliceInd ].type == 0 && splices[nextSpliceInd].type == 1 && end - start + 1 > 6 * MERGE_DISTANCE - 2 )
{
spliceInfo[ spliceInd ].possibleIR = 1 ;
cut = true ;
}
else if ( !cut && spliceInd >= 0 && nextSpliceInd < scnt &&
splices[ spliceInd ].type == 0 && splices[nextSpliceInd].type == 1 && end - start + 1 <= 6 * MERGE_DISTANCE - 2 )
{
spliceInfo[ spliceInd ].depthSum = currCover ; //( leftIntervalAdjust + rightIntervalAdjust ) / 2 ;
spliceInfo[ spliceInd ].possibleIR = 1 ;
cut = true ;
}
if ( spliceInfo[ spliceInd ].possibleIR == 1 )
{
// Test whether the coverage of the intron is much higher than the splice junction
if ( 1 ) //splices[ spliceInd ].otherInd == nextSpliceInd )
{
int len ;
double avg ;
if ( splices[ nextSpliceInd ].pos - splices[ spliceInd ].pos - 1 > 6 * MERGE_DISTANCE - 2 )
len = splices[ nextSpliceInd ].pos - splices[ spliceInd ].pos - 1 - ( 6 * MERGE_DISTANCE - 2 ) ;
else
len = splices[ nextSpliceInd ].pos - splices[ spliceInd ].pos - 1 ;
avg = spliceInfo[ spliceInd ].depthSum / (double)len ;
if ( pow( LOG_BASE, avg ) > 4 * ( splices[ spliceInd ].support + splices[ nextSpliceInd ].support ) )
//&& splices[ spliceInd ].otherInd == nextSpliceInd )
{
//printf( "%d: %d %d: %d %d: %d\n", end - start - 1, splices[ spliceInd ].support, splices[ nextSpliceInd ].support,
// splices[ spliceInd ].pos, splices[ nextSpliceInd ].pos, spliceInd ) ;
//cut = false ;
//spliceInfo[ spliceInd ].possibleIR = 0 ;
spliceInfo[ spliceInd ].possibleIR = 2 ;
}
}
// Test whether this is actaully from a 3', 5' UTR.
if ( 0 ) //end - start + 1 >= 3 * BIN_SIZE && end - start + 1 < MAX_LENGTH )
{
bool flag = true ;
// Test increasing from left to right _-^
double sum, firstSum, prevSum ;
sum = 0 ;
for ( i = end ; i > end - BIN_SIZE ; --i )
{
sum += islandDepth[i - start] ;
}
firstSum = sum ;
prevSum = sum ;
for ( ; i >= start + BIN_SIZE - 1 ; i -= BIN_SIZE )
{
sum = 0 ;
for ( j = i ; j > i - BIN_SIZE ; --j )
sum += islandDepth[j - start] ;
if ( sum >= prevSum )
{
flag = false ;
break ;
}
prevSum = sum ;
}
if ( flag && sum / BIN_SIZE <= firstSum / BIN_SIZE - 8 )
{
cut = true ;
spliceInfo[ spliceInd ].possibleIR = 0 ;
sum = 0 ;
for ( i = ( start + end ) / 2 ; i < end - 3 * MERGE_DISTANCE ; ++i )
{
sum += islandDepth[i - start] ;
}
//printf( "Success: %d\n", splices[ nextSpliceInd ].pos ) ;
spliceInfo[ nextSpliceInd ].soft[0] = ( start + end ) / 2 ;
spliceInfo[ nextSpliceInd ].avgSoftDepth[0] = sum / ( i - (start + end) / 2) ;
}
// Test decreasing from right to left ^-_
flag = true ;
sum = 0 ;
for ( i = start ; i < start + BIN_SIZE ; ++i )
{
sum += islandDepth[i - start] ;
}
firstSum = sum ;
prevSum = sum ;
for ( ; i <= end - BIN_SIZE + 1 ; i += BIN_SIZE )
{
sum = 0 ;
for ( j = i ; j < i + BIN_SIZE ; ++j )
sum += islandDepth[j - start] ;
if ( sum >= prevSum )
{
flag = false ;
break ;
}
prevSum = sum ;
}
if ( flag && sum / BIN_SIZE <= firstSum / BIN_SIZE - 8 )
{
cut = true ;
spliceInfo[ spliceInd ].possibleIR = 0 ;
sum = 0 ;
for ( i = start + 3 * MERGE_DISTANCE ; i <= ( start + end ) / 2 ; ++i )
{
sum += islandDepth[i - start] ;
}
spliceInfo[spliceInd].soft[1] = ( start + end ) / 2 ;
spliceInfo[spliceInd].avgSoftDepth[1] = sum / ( i - start - 3 * MERGE_DISTANCE) ;
}
}
}
}
if ( !isFuture )
{
// Get the single exons
for ( i = 0 ; i < tmpSeCnt && !isFuture ; ++i )
{
if ( IsSingleExon( tmpSingleExon[i][0], tmpSingleExon[i][1], prevIslandCover, currIslandCover, futureIslandCover ) )
{
//printf( "## %d %d %d %d\n", start, end, tmpSingleExon[i][0], tmpSingleExon[i][1] ) ;
singleExon[ seCnt ][0] = tmpSingleExon[i][0] ;
singleExon[ seCnt ][1] = tmpSingleExon[i][1] ;
++seCnt ;
}
}
// Update the prevIsland by currIsland
/*for ( i = 0 ; i < prevIslandCover.used ; ++i )
printf( "#%d ", prevIslandCover.pos[i] ) ;
printf( "\n" ) ;
for ( i = 0 ; i < currIslandCover.used ; ++i )
printf( "%d ", currIslandCover.pos[i] ) ;
printf( "\n" ) ;*/
if ( currIslandCover.used >= BIN_USE )
{
for ( k = 0, i = currIslandCover.used - BIN_USE ; i < currIslandCover.used ; ++i, ++k )
{
prevIslandCover.avgCover[k] = currIslandCover.avgCover[i] ;
prevIslandCover.pos[k] = currIslandCover.pos[i] ;
}
prevIslandCover.used = k ;
}
else
{
int tmp = BIN_USE - currIslandCover.used ;
for ( k = 0, i = ( prevIslandCover.used - tmp < 0 ? 0 : prevIslandCover.used - tmp ) ; i < prevIslandCover.used ; ++i, ++k )
{
prevIslandCover.avgCover[k] = prevIslandCover.avgCover[i] ;
prevIslandCover.pos[k] = prevIslandCover.pos[i] ;
}
for ( i = 0 ; i < currIslandCover.used ; ++i, ++k )
{
prevIslandCover.avgCover[k] = currIslandCover.avgCover[i] ;
prevIslandCover.pos[k] = currIslandCover.pos[i] ;
}
prevIslandCover.used = k ;
}
}
else
{
// Update the futureIsland by currIsland
for ( i = 0, k = futureIslandCover.used ; i < currIslandCover.used ; ++i, ++k )
{
if ( k >= BIN_USE )
break ;
futureIslandCover.avgCover[k] = currIslandCover.avgCover[i] ;
futureIslandCover.pos[k] = currIslandCover.pos[i] ;
}
futureIslandCover.used = k ;
}
/*if ( start == 31246180 )
{
printf( "%d: %d %d\n", cut, start, end ) ;
exit( 1 ) ;
}*/
#if ALL_CLEAN
if ( spliceInd >= 0 && splices[spliceInd].type == 0 && splices[nextSpliceInd].type == 1 )
{
soft[0] = soft[1] = -1 ;
cut = true ;
}
#endif
return !cut ;
}
// Decide the regions from splice startInd to splice endInd
double DetermineRegions( char *chrom, int startInd, int endInd )
{
int i, j, k ;
int idepth ;
double depth ;
double avgDepth, stdevDepth ;
char inChrom[50] ;
int pos ;
double depthSum = 0 ;
int intronLen = 0 ;
const double alpha = IR_ALPHA ;
double noiseThreshold = 0 ;
double threshold ; // TODO: different threshold for intron retention and 3'5' start site
int exonLen = 0 ;
double exonDepthSum = 0, exonAvgDepth ;
const double exonAlpha = 1.5 ;
int start, end ;
start = splices[ startInd ].pos ;
end = splices[ endInd ].pos ;
// Determine the possibleIR=2 case
for ( i = startInd ; i < endInd ; ++i )