-
Notifications
You must be signed in to change notification settings - Fork 1
/
SolveRegion.cpp
3125 lines (2811 loc) · 90.5 KB
/
SolveRegion.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
/**
Given a region and its coverage, splice site information, find the best possible regionExons that can explain the information by Linear Programming method.
*/
#include "SolveRegion.h"
extern int READS_LENGTH ;
extern int FRAG_LENGTH, FRAG_STD ;
double psum[MAX_LENGTH] ;
char buffer[MAX_LENGTH], buffer2[MAX_LENGTH] ;
int OFFSET ;
struct _point inputStart[MAX_POINT], inputEnd[MAX_POINT] ;
struct _spliceIndex spliceIndices[MAX_POINT * MAX_POINT] ;
//struct _point start[MAX_POINT], end[MAX_POINT] ; // The points after merging
struct _point *allStart, *allEnd ;
int allStartCnt, allEndCnt ;
//int point[MAX_POINT] ;
//double interval[MAX_POINT] ;
// The enumerated regionExons.
struct _enumExon
{
int start, end ;
int startInd, endInd ;
int strand ;
bool inEvidence ;
} ;
struct _enumExon regionExons[MAX_POINT * MAX_POINT] ;
bool exonHasSoft[MAX_POINT * MAX_POINT][2] ; // Does a exon have soft boundary. 0-left, 1-right?
//int ecnt = 0 ;
//bool eused[MAX_POINT * MAX_POINT] ;
//int startCover[MAX_POINT][3], endCover[MAX_POINT][3] ;
//int intervalCnt, startCnt, endCnt ;
struct _exonfrag
{
int eid ; // exon id
int length ;
int previd ; // exon frag index of the previous fragment of the same exon.
int type ; // 0-normal, 1-at the end, which can have higher difference between its neighbor.
} ;
//struct _exonfrag exonfrag[MAX_POINT * MAX_POINT] ;
//int efCnt ;
// Data structure used to find the constraint between splice junctions
// TODO: Can be more efficient
//bool graph[MAX_POINT][MAX_POINT] ;
//bool visited[MAX_POINT][2] ;
//bool beyond[MAX_POINT][2][2] ; // Whether one side of the exon is beyond the region. 0-left end, 1-right end. The last "2" is the strand.
//int leftEf[MAX_POINT], rightEf[MAX_POINT] ;
//int leftEfCnt, rightEfCnt ;
//int efid[MAX_POINT][2] ; // The first and last ef index of an exon. 0-left end, 1-right end
// Data structure for traversing "exon path" when testing contraint points.
// Means: is there a path between those two exon?
//int exonPath[MAX_POINT][MAX_POINT] ;
//int exonDist[2][MAX_POINT][MAX_POINT] ; // The minimal and maximal distance between two regionExons. 0-min, 1-max
// Constraints from the mate pair reads.
struct _constraintPoint
{
int startPos, endPos ;
int strand ;
int type ; // 0-must belong to the same exon, 1-they are connected through several regionExons. 2-the Pos is the index of an interval
// 3-the chunk [startPos, endPos] must be from the same exon. 4-reachability between two mate pairs.
int support ; // # of supporting reads.
int otherPos[2] ;
int precise[2] ;
bool valid ;
} ;
struct _constraintPoint constraintPoints[MAX_POINT * MAX_POINT] ;
//int cpCnt = 0 ;
// For returning
//bool bestEused[MAX_POINT * MAX_POINT] ;
//double bestScore ;
//int bestEcnt ;
//int mainStrand ; // What is the main strand in this region
extern FILE *fpPolyA ;
// The attributes for best combination.
struct _bestCombAttr
{
int bestEcnt ;
int bestEvidenceExonCnt ;
int bestLongExonCnt ;
int bestBalancedExonCnt ;
} ;
struct _solveRegionData
{
bool *graph ;
bool *visited ;
bool *beyond ;
struct _exonfrag *exonfrag ;
int *efid ;
int *leftEf, *rightEf ;
struct _enumExon *regionExons ;
bool *eused ;
bool *exonHasSoft ;
int *exonPath ;
int *exonDist ;
struct _constraintPoint *constraintPoints ;
struct _point *start, *end ;
int *point ;
double *interval ;
int *startCover, *endCover ;
struct _spliceIndex *spliceIndices ;
struct _bestCombAttr bestCombAttr ;
} ;
struct _pthreadArgSolveRegion
{
struct _enumExon *regionExons ;
int ecnt ;
bool *exonHasSoft ;
int OFFSET ;
int mainStrand ;
struct _point *start, *end ;
int *startCover, *endCover ;
int *point ;
double *interval ;
int startCnt, endCnt, intervalCnt ;
struct _spliceIndex *spliceIndices ;
int siCnt ;
struct _constraintPoint *constraintPoints ;
int cpCnt ;
struct _exon *allExons ;
int *exonCnt ;
int readCnt ;
} ;
extern pthread_attr_t pthreadAttr ;
extern int NUM_OF_THREADS ;
int currSolveRegionThreadsCnt ;
pthread_mutex_t solveRegionMutex ;
pthread_mutex_t allExonsMutex ;
pthread_cond_t idleSolveRegionCond ;
pthread_cond_t clearSolveRegionCond ;
/**
Test whether s1 and s2 can belong to the same strand
*/
bool IsSameStrand( int s1, int s2 )
{
if ( s1 >= 0 && s2 >= 0 )
return s1 == s2 ;
if ( s1 == -1 || s2 == -1 )
return true ;
if ( ( s1 == -2 && s2 == 1 )
|| ( s1 == 1 && s2 == -2 ) )
return true ;
if ( ( s1 == -3 && s2 == 0 )
|| ( s1 == 0 && s2 == -3 ) )
return true ;
return false ;
}
/**
Suppose a, b are the reads supports the junctions.
Test whether a significantly great b.
*/
bool SignificantGreater( int a, int b )
{
/*if ( a <= b )
return false ;
if ( b <= 20 && a > b + 100 )
return true ;
if ( b <= 50 && a > b + 200 )
return true ;
if ( b <= 100 && a > b + 400 )
return true ;
if ( a > 5 * b )
return true ;*/
if ( a > 100 * b )
return true ;
if ( b <= 100 && a - 6 * sqrt( a ) > b + 6 * sqrt( b ) )
return true ;
return false ;
}
/**
Find the representative of the set that the point is merged to.
*/
int GetMergeFather( struct _point points[], int tag )
{
if ( points[tag].merge != tag )
return points[tag].merge = GetMergeFather( points, points[tag].merge ) ;
return tag ;
}
/**
Recursively determine the exon fragments on the left side and right side of a "complete" splice.
tag: the current exon.
direction: 0-from left to right(right end of an exon), 1-from right to left(left end of an exon)
@return : false if an exon's boundary has its pair beyond the region.
*/
bool FindSpliceExon( int tag, int direction, int efCnt, int ecnt,
int &leftEfCnt, int &rightEfCnt, struct _solveRegionData &data )
{
bool *eused = data.eused ;
bool *graph = data.graph ;
bool *visited = data.visited ;
bool *beyond = data.beyond ;
struct _enumExon *regionExons = data.regionExons ;
int *efid = data.efid ;
int *leftEf = data.leftEf ;
int *rightEf = data.rightEf ;
if ( visited[tag * 2 + direction] || !eused[tag] )
return true ;
visited[tag * 2 + direction] = true ;
int i ;
if ( direction == 0 )
{
if ( regionExons[tag].strand >= 0 && beyond[ tag * 4 + 2 + regionExons[tag].strand ] )
return false ;
leftEf[ leftEfCnt ] = efid[tag * 2 + 1] ;
++leftEfCnt ;
for ( i = tag + 1 ; i < ecnt ; ++i )
{
if ( graph[tag * ecnt + i] )
if ( !FindSpliceExon( i, 1, efCnt, ecnt, leftEfCnt, rightEfCnt, data ) )
return false ;
}
}
else
{
if ( regionExons[tag].strand >= 0 && beyond[ tag * 4 + 2 * 0 + regionExons[tag].strand ] )
return false ;
rightEf[ rightEfCnt ] = efid[tag * 2 + 0] ;
++rightEfCnt ;
for ( i = 0 ; i < tag ; ++i )
if ( graph[i * ecnt + tag] )
if ( !FindSpliceExon( i, 1, efCnt, ecnt, leftEfCnt, rightEfCnt, data ) )
return false ;
}
return true ;
}
double LP( int ecnt, int intervalCnt, int startCnt, int endCnt, struct _solveRegionData &data, bool useExonFrag = true )
{
int i, j, k ;
int p ;
int a, b ;
int miCnt ;
int Ncol ;
int *colno ;
double *row ;
lprec *lp ;
bool right = false ; // right combination?
int spliceExonfrag[2][MAX_POINT] ; // The indices of the exonfrags that spanning the splice.
int spliceSupport[2], scCnt[2] ;
//long long intervalCover[MAX_POINT] ; // Use bit to represent which regionExons covered this interval.
int *cid ; // The current id(exonfrag id) for corresponded exon.
int varCnt ; // The number of variables in the LP that will be used.
double ret ;
int efCnt ;
double sum = 0 ;
struct _enumExon *regionExons = data.regionExons ;
bool *exonHasSoft = data.exonHasSoft ;
bool *eused = data.eused ;
struct _exonfrag *exonfrag = data.exonfrag ;
int *efid = data.efid ;
int *point = data.point ;
double *interval = data.interval ;
bool *visited = data.visited ;
int *leftEf = data.leftEf ;
int *rightEf = data.rightEf ;
struct _point *start = data.start ;
struct _point *end = data.end ;
cid = (int *)malloc( sizeof( int ) * ecnt ) ;
efCnt = 0 ;
memset( cid, -1, sizeof( int ) * ecnt ) ;
/*for ( i = 0 ; i <= intervalCnt ; ++i )
{
printf( "%d ", point[i] ) ;
}
printf( "\n" ) ;*/
/*for ( i = 0 ; i < ecnt ; ++i )
printf( "%d", eused[i] ) ;
printf( "\n" ) ;*/
for ( i = 0 ; i < intervalCnt ; ++i )
sum += interval[i] ;
if ( useExonFrag )
{
for ( i = 0 ; i < intervalCnt ; ++i )
{
//k = 0 ;
//intervalCover[i] = 0 ;
for ( j = 0 ; j < ecnt ; ++j )
{
if ( eused[j] && regionExons[j].start <= point[i + 1] && regionExons[j].end > point[i] )
{
//colno[k] = j + 1 ;
//row[k] = 1 ;
/*a = regionExons[j].start > point[i] ? regionExons[j].start : point[i] ;
b = regionExons[j].end < point[i + 1] ? regionExons[j].end : point[i + 1] ;
row[k] = ( b - a + 1 ) / (double)( regionExons[j].end - regionExons[j].start + 1 ) ; */
//printf( "### %d %d %lf\n", a, b, row[k] ) ;
//++k ;
exonfrag[ efCnt ].eid = j ;
exonfrag[ efCnt ].length = point[i + 1] - point[i] ;
exonfrag[ efCnt ].previd = cid[j] ;
if ( ( regionExons[j].start == point[i] + 1 && exonHasSoft[j * 2 + 0] ) ||
( regionExons[j].end == point[i + 1] && exonHasSoft[j * 2 + 1] ) )
{
exonfrag[ efCnt ].type = 1 ;
}
else
exonfrag[ efCnt ].type = 0 ;
if ( cid[j] == -1 )
efid[j * 2 + 0] = efCnt ;
cid[j] = efCnt ;
efid[j * 2 + 1] = efCnt ;
++efCnt ;
}
}
}
}
else
{
for ( i = 0 ; i < ecnt ; ++i )
{
exonfrag[ efCnt ].eid = i ;
exonfrag[ efCnt ].length = regionExons[i].end - regionExons[i].start + 1 ;
exonfrag[ efCnt ].previd = -1 ;
efid[i * 2 + 0] = efid[i * 2 + 1] = efCnt ; //i ;
cid[i] = efCnt ;
++efCnt ;
}
}
Ncol = efCnt + intervalCnt + efCnt + intervalCnt ; // The last two is for the slack variable.
colno = (int *)malloc( Ncol * sizeof( *colno ) ) ;
row = (double *)malloc( Ncol * sizeof( *row ) ) ;
lp = make_lp( 0, Ncol ) ;
set_add_rowmode(lp, TRUE) ;
varCnt = efCnt + intervalCnt ;
//printf( "#### %d\n", efCnt ) ;
p = 0 ;
for ( i = 0 ; i < intervalCnt ; ++i )
{
k = 0 ;
for ( j = 0 ; j < ecnt ; ++j )
{
if ( eused[j] && regionExons[j].start <= point[i + 1] && regionExons[j].end > point[i] )
{
if ( useExonFrag )
colno[k] = p + 1 ;
else
colno[k] = j + 1 ;
row[k] = 1 ;
++k ;
++p ;
}
}
colno[k] = efCnt + i + 1 ;
row[k] = -1 ;
//printf( "###%d %d %d\n", i, mergeInterval[i].read, mergeInterval[i].length) ;
add_constraintex(lp, k + 1, row, colno, LE, (double)interval[i] / ( point[i + 1] - point[i] ) ) ;
row[k] = 1 ;
add_constraintex(lp, k + 1, row, colno, GE, (double)interval[i] / ( point[i + 1] - point[i] ) ) ;
}
/**
Constraint from the splice junctions.
*/
// For the start points
p = 0 ;
for ( i = 0 ; i < 0/*intervalCnt*/ ; ++i )
{
scCnt[0] = scCnt[1] = 0 ;
for ( j = 0 ; j < ecnt ; ++j )
{
if ( eused[j] && regionExons[j].start <= point[i + 1] && regionExons[j].end > point[i] )
{
if ( regionExons[j].start == point[i] + 1 )
{
a = regionExons[j].strand ;
spliceExonfrag[a][ scCnt[a] ] = p ;
++scCnt[a] ;
spliceSupport[a] = start[ regionExons[j].startInd ].support ;
}
++p ;
}
}
for ( k = 0 ; k <= 1 ; ++k )
{
if ( !spliceSupport[k] || !scCnt[k] )
continue ;
for ( j = 0 ; j < scCnt[k] ; ++j )
{
colno[j] = spliceExonfrag[k][j] + 1 ;
row[j] = 1 ;
}
//add_constraintex( lp, scCnt[k], row, colno, GE, sqrt( spliceSupport[k] * 0.1 )) ; //- SPLICE_ALPHA * ( point[i + 1] - point[i] ) / 2 ) ;
add_constraintex( lp, scCnt[k], row, colno, LE, sqrt( spliceSupport[k] * 10 ) ) ; //+ SPLICE_ALPHA * ( point[i + 1] - point[i] ) / 2 ) ;
}
}
// For the end points
p = 0 ;
for ( i = 0 ; i < 0/*intervalCnt*/ ; ++i )
{
scCnt[0] = scCnt[1] = 0 ;
for ( j = 0 ; j < ecnt ; ++j )
{
if ( eused[j] && regionExons[j].start <= point[i + 1] && regionExons[j].end > point[i] )
{
if ( regionExons[j].end == point[i + 1] )
{
a = regionExons[j].strand ;
spliceExonfrag[a][ scCnt[a] ] = p ;
++scCnt[a] ;
spliceSupport[a] = end[ regionExons[j].endInd ].support ;
}
++p ;
}
}
for ( k = 0 ; k <= 1 ; ++k )
{
if ( !spliceSupport[k] || !scCnt[k] )
continue ;
for ( j = 0 ; j < scCnt[k] ; ++j )
{
colno[j] = spliceExonfrag[k][j] + 1 ;
row[j] = 1 ;
}
//add_constraintex( lp, scCnt[k], row, colno, GE, spliceSupport[k] * 0.1 ) ; //- SPLICE_ALPHA * ( point[i + 1] - point[i] ) / 2 ) ;
//add_constraintex( lp, scCnt[k], row, colno, LE, spliceSupport[k] * 10 ) ; //+ SPLICE_ALPHA * ( point[i + 1] - point[i] ) / 2 ) ;
}
}
/**
Every exonfrag should be greater than 1. ( TODO: ? )
*/
for ( j = 0 ; j < efCnt ; ++j )
{
colno[0] = j + 1 ;
row[0] = 1 ;
add_constraintex( lp, 1, row, colno, GE, 1 ) ; //( regionExons[j].end - regionExons[j].start + 1 ) ) ;
}
/**
Continuity: The relationship between adjacent exonfrag.
*/
for ( j = 0 ; j < ecnt ; ++j )
{
if ( !eused[j] )
continue ;
p = cid[j] ;
while ( 1 )
{
k = exonfrag[p].previd ;
if ( k == -1 )
break ;
colno[0] = k + 1 ;
colno[1] = p + 1 ;
row[0] = 1 ;
row[1] = -1 ;
/*if ( regionExons[j].strand == 0 )
row[0] = pow( 1.002, ( exonfrag[p].length + exonfrag[k].length ) / 2 ) ;
else
row[1] = -pow( 1.002, ( exonfrag[p].length + exonfrag[k].length ) / 2 ) ;*/
/*colno[2] = varCnt + 1 ;
row[2] = 1 ;
add_constraintex( lp, 3, row, colno, GE, -( exonfrag[p].length + exonfrag[k].length ) * (double)ALPHA / 2 ) ;
//row[0] = ALPHA ;
//row[1] = -1 ;
row[2] = -1 ;
add_constraintex( lp, 3, row, colno, LE, ( exonfrag[p].length + exonfrag[k].length ) * (double)ALPHA / 2 ) ;
++varCnt ;*/
//row[0] = 1 ;
//row[1] = -1 ;
//add_constraintex( lp, 2, row, colno, EQ, 0 ) ;
colno[2] = varCnt + 1 ;
row[2] = ( exonfrag[p].length + exonfrag[k].length ) / 2 ;
//add_constraintex( lp, 3, row, colno, GE, 0 ) ;
//if ( exonfrag[k].type == 0 && exonfrag[p].type == 0 )
//{
double tmp = ( exonfrag[p].length + exonfrag[k].length ) * (double)ALPHA / 2 ;
if ( tmp > 10 )
tmp = 10 ;
add_constraintex( lp, 3, row, colno, GE, -tmp ) ;
//}
//else
//{
// add_constraintex( lp, 3, row, colno, GE, -( exonfrag[p].length + exonfrag[k].length ) * (double)1 / 2 ) ;
//}
row[2] = -row[2] ;
//add_constraintex( lp, 3, row, colno, LE, 0 ) ;
//if ( exonfrag[k].type == 0 && exonfrag[p].type == 0 )
//{
add_constraintex( lp, 3, row, colno, LE, tmp ) ;
//}
//else
//{
// add_constraintex( lp, 3, row, colno, LE, ( exonfrag[p].length + exonfrag[k].length ) * (double)1 / 2 ) ;
//}
++varCnt ;
p = k ;
}
}
/**
The relationship from splice junctions that the regionExons on the left side should euqal to right side
*/
memset( visited, false, sizeof( visited[0] ) * ecnt * 2 ) ;
for ( i = 0 ; i < 0/*ecnt*/ ; ++i )
{
if ( !eused[i] )
continue ;
int leftEfCnt = 0, rightEfCnt = 0 ;
//printf( "## %d %d %d\n", i, visited[3][0], graph[3][5] ) ;
if ( !FindSpliceExon( i, 0, efCnt, ecnt, leftEfCnt, rightEfCnt, data ) )
continue ;
if ( rightEfCnt )
{
// printf( "## %d %d\n", leftEf[0], right) ;
for ( i = 0 ; i < leftEfCnt ; ++i )
{
colno[i] = leftEf[i] + 1 ;
row[i] = 1 ;
}
for ( ; i < leftEfCnt + rightEfCnt ; ++i )
{
colno[i] = rightEf[i - leftEfCnt] + 1 ;
row[i] = -1 ;
}
/*colno[i] = varCnt + 1 ;
row[i] = 1 ;
add_constraintex( lp, leftEfCnt + rightEfCnt + 1, row, colno, GE,
-( exonfrag[ leftEf[0] ].length + exonfrag[ rightEf[0] ].length ) * (double)ALPHA / 2 ) ;
row[i] = -1 ;
add_constraintex( lp, leftEfCnt + rightEfCnt + 1, row, colno, LE,
( exonfrag[ leftEf[0] ].length + exonfrag[ rightEf[0] ].length ) * (double)ALPHA / 2 ) ;
++varCnt ;*/
colno[i] = varCnt + 1 ;
row[i] = ( exonfrag[ leftEf[0] ].length + exonfrag[ rightEf[0] ].length ) / 2 ;
add_constraintex( lp, leftEfCnt + rightEfCnt + 1, row, colno, GE,
-( exonfrag[ leftEf[0] ].length + exonfrag[ rightEf[0] ].length ) * (double)ALPHA / 2 ) ;
row[i] = -row[i] ;
//add_constraintex( lp, leftEfCnt + rightEfCnt + 1, row, colno, LE, 0 ) ;
add_constraintex( lp, leftEfCnt + rightEfCnt + 1, row, colno, LE,
( exonfrag[ leftEf[0] ].length + exonfrag[ rightEf[0] ].length ) * (double)ALPHA / 2 ) ;
++varCnt ;
}
}
//if ( OFFSET == 156914811 )
// write_LP( lp, stdout ) ;
/**
The sum of them should be equal to the total number of reads.
*/
k = 0 ;
for ( j = 0 ; j < efCnt ; ++j )
{
colno[j] = j + 1 ;
row[j] = exonfrag[j].length ;
}
add_constraintex( lp, efCnt, row, colno, GE, sum - efCnt ) ;
add_constraintex( lp, efCnt, row, colno, LE, sum + efCnt ) ;
/*for ( i = 0 ; i < ecnt ; ++i )
set_int( lp, i + 1, TRUE ) ;*/
set_add_rowmode(lp, FALSE ) ;
for ( k = 0 ; k < intervalCnt ; ++k )
{
colno[k] = efCnt + k + 1 ;
row[k] = 1 ;
}
for ( ; k < varCnt - efCnt ; ++k )
{
colno[k] = k + efCnt + 1 ;
row[k] = SLACK_PENALTY ;
}
set_obj_fnex( lp, varCnt - efCnt, row, colno ) ;
set_minim( lp ) ;
set_verbose(lp, CRITICAL);
if ( solve( lp ) != 0 )
{
free( colno ) ;
free( row ) ;
free( cid ) ;
delete_lp( lp ) ;
return -1 ;
}
//printf( "LP_sum=%lf\n", sum ) ;
//write_LP(lp, stdout);
/*printf( "|" ) ;
for ( i = 0 ; i < ecnt ; ++i )
{
if ( eused[i] )
printf( "%d ", i ) ;
}
printf( "\n" ) ;*/
k = 0 ;
ret = get_objective( lp ) ;
/*if ( right )
printf("#Objective value: %lf\n", ret ) ;
else
printf("Objective value: %lf\n", ret ) ;
if ( right )
printf("#Objective value: " ) ;
else
printf("Objective value: " ) ;*/
/*get_variables(lp, row) ;
for ( i = 0 ; i < efCnt + intervalCnt ; ++i )
printf( "%lf ", row[i] ) ;
printf( "\n" ) ;*/
free( cid ) ;
free( colno ) ;
free( row ) ;
delete_lp( lp ) ;
//printf( "\n\n" ) ;
return ret ;
}
/**
Test whether there is a path from exon s to exon t.
*/
int TestExonPath( int s, int t, int ecnt, struct _solveRegionData &data )
{
int *exonPath = data.exonPath ;
bool *eused = data.eused ;
bool *graph = data.graph ;
int index = s * ecnt + t ;
if ( exonPath[index] != -1 )
return exonPath[index] ;
if ( s == t )
return exonPath[index] = 1 ;
int i ;
for ( i = s + 1 ; i <= t ; ++i )
{
if ( eused[i] && graph[s * ecnt + i] && TestExonPath( i, t, ecnt, data ) )
return exonPath[index] = 1 ;
}
return exonPath[index] = 0 ;
}
/**
Verify whether an enumeration satisfy the constraint from the mate-pair reads.
@parm: initialTest, remove the constraints that contradict with all the exons.
*/
bool VerifyConstraintPoints( int ecnt, int cpCnt, int mainStrand, struct _solveRegionData &data, bool initialTest = false )
{
int i, j, k ;
struct _enumExon *regionExons = data.regionExons ;
bool *eused = data.eused ;
int *exonPath = data.exonPath ;
struct _constraintPoint *constraintPoints = data.constraintPoints ;
memset( exonPath, -1, sizeof( int ) * ecnt * ecnt ) ;
if ( initialTest )
{
for ( i = 0 ; i < cpCnt ; ++i )
constraintPoints[i].valid = true ;
for ( i = 0 ; i < ecnt ; ++i )
eused[i] = true ;
for ( i = 0 ; i < cpCnt ; ++i )
if ( constraintPoints[i].type == 0 ||
constraintPoints[i].type == 3 ||
constraintPoints[i].type == 4 )
{
if ( constraintPoints[i].support < 10 )
constraintPoints[i].valid = false ;
}
}
for ( i = 0 ; i < cpCnt ; ++i )
{
if ( !constraintPoints[i].valid )
continue ;
if ( constraintPoints[i].type == 0 )
{
for ( j = 0 ; j < ecnt ; ++j )
{
if ( !eused[j] )
continue ;
if ( regionExons[j].start == constraintPoints[i].startPos &&
regionExons[j].end == constraintPoints[i].endPos &&
regionExons[j].strand == constraintPoints[i].strand )
break ;
}
if ( j >= ecnt )
{
if ( !initialTest )
return false ;
else
constraintPoints[i].valid = false ;
}
}
/*else if ( constraintPoints[i].type == 1 )
{
for ( j = 0 ; j < ecnt ; ++j )
{
if ( !eused[j] || regionExons[j].strand != constraintPoints[i].strand ||
regionExons[j].start != constraintPoints[i].startPos )
continue ;
for ( k = j ; k < ecnt ; ++k )
{
if ( !eused[k] || regionExons[k].strand != constraintPoints[i].strand ||
regionExons[k].end != constraintPoints[i].endPos )
continue ;
if ( TestExonPath( j, k ) )
{
break ;
}
}
if ( k < ecnt )
break ;
}
if ( j >= ecnt )
{
if ( !initialTest )
return false ;
else
constraintPoints[i].valid = false ;
}
}
else if ( constraintPoints[i].type == 2 )
{
if ( constraintPoints[i].strand != -1 ) // TODO: because of noise
continue ;
for ( j = 0 ; j < ecnt ; ++j )
{
if ( !eused[j] || regionExons[j].start > point[ constraintPoints[i].startPos ] + 1 ||
regionExons[j].end < point[ constraintPoints[i].startPos + 1] )
continue ;
for ( k = j ; k < ecnt ; ++k )
{
if ( !eused[k] || regionExons[k].start > point[ constraintPoints[i].endPos ] + 1 ||
regionExons[k].end < point[ constraintPoints[i].endPos + 1 ] )
continue ;
if ( regionExons[j].strand != regionExons[k].strand ||
( constraintPoints[i].strand != -1 && regionExons[j].strand != constraintPoints[i].strand ) )
continue ;
if ( TestExonPath( j, k ) )
break ;
}
if ( k < ecnt )
break ;
}
if ( j >= ecnt )
{
printf( "=.= %d %d %d\n" , constraintPoints[i].startPos, constraintPoints[i].endPos, constraintPoints[i].strand ) ;
return false ;
}
}*/
else if ( constraintPoints[i].type == 3 )
{
if ( constraintPoints[i].strand != -1 && constraintPoints[i].strand != mainStrand ) // TODO: because of noise
continue ;
for ( j = 0 ; j < ecnt ; ++j )
{
if ( eused[j] && regionExons[j].start <= constraintPoints[i].startPos
&& regionExons[j].end >= constraintPoints[i].endPos
&& ( constraintPoints[i].strand == -1 || constraintPoints[i].strand == regionExons[j].strand ) )
break ;
}
if ( j >= ecnt )
{
if ( !initialTest )
return false ;
else
constraintPoints[i].valid = false ;
}
/*{
printf( "+.+ %d %d %d %d %d %d\n" , constraintPoints[i].startPos + OFFSET, constraintPoints[i].endPos + OFFSET, constraintPoints[i].strand, regionExons[1].start + OFFSET, regionExons[1].end + OFFSET,
ecnt ) ;
return false ;
}*/
}
else if ( constraintPoints[i].type == 4 )
{
if ( constraintPoints[i].strand != -1 && constraintPoints[i].strand != mainStrand ) // TODO: because of noise
continue ;
for ( j = 0 ; j < ecnt ; ++j )
{
if ( !eused[j] )
continue ;
if ( regionExons[j].start > constraintPoints[i].startPos ||
regionExons[j].end < constraintPoints[i].endPos )
continue ;
if ( constraintPoints[i].precise[0] && regionExons[j].start != constraintPoints[i].startPos )
continue ;
for ( k = j ; k < ecnt ; ++k )
{
if ( !eused[k] )
continue ;
if ( regionExons[k].start > constraintPoints[i].otherPos[0] ||
regionExons[k].end < constraintPoints[i].otherPos[1] )
continue ;
if ( constraintPoints[i].precise[1] && regionExons[k].end != constraintPoints[i].otherPos[1] )
continue ;
//printf( "### %d %d %d %d: %d %d\n", i, j, k, TestExonPath(j , k), constraintPoints[i].startPos, constraintPoints[i].endPos ) ;
if ( TestExonPath( j, k, ecnt, data ) )
break ;
}
if ( k < ecnt )
break ;
}
if ( j >= ecnt )
{
if ( !initialTest )
return false ;
else
constraintPoints[i].valid = false ;
}
/*{
printf( "=.= %d %d %d\n" , constraintPoints[i].startPos, constraintPoints[i].endPos, constraintPoints[i].strand ) ;
return false ;
}*/
}
}
return true ;
}
/**
The (inner boundaries) distance between two regionExons/
*/
void FindExonDist( int s, int t, int ecnt, struct _solveRegionData &data )
{
struct _enumExon *regionExons = data.regionExons ;
int *exonDist = data.exonDist ;
bool *graph = data.graph ;
bool *eused = data.eused ;
int index = s * ecnt + t ;
if ( exonDist[ index * 2 + 0] != -1 )
return ;
if ( s == t || graph[s * ecnt + t] )
{
exonDist[index * 2 + 0] = 0 ;
exonDist[index * 2 + 1] = 0 ;
return ;
}
int i, k ;
for ( i = s + 1 ; i < t ; ++i )
{
int index2 = i * ecnt + t ;
if ( !eused[i] || !graph[s * ecnt + i] || !TestExonPath( i, t, ecnt, data ) )
continue ;
FindExonDist( i, t, ecnt, data ) ;
k = exonDist[index2 * 2 + 0] ;
if ( exonDist[ index * 2 + 0] == -1 ||
exonDist[ index2 * 2 + 0] + regionExons[i].end - regionExons[i].start + 1 < exonDist[index * 2 + 0] )
exonDist[ index * 2 + 0] = exonDist[index2 * 2 + 0] + regionExons[i].end - regionExons[i].start + 1 ;
k = exonDist[index2 * 2 + 1] ;
if ( exonDist[index * 2 + 1] == -1 ||
exonDist[ index2 * 2 + 1] + regionExons[i].end - regionExons[i].start + 1 > exonDist[index * 2 + 1] )
exonDist[ index * 2 + 1] = exonDist[ index2 * 2 + 1] + regionExons[i].end - regionExons[i].start + 1 ;
}
}
/**
Find the distance between two intervals from the type-2 constraint points.
return: The weight of pair intervals whose distance does not include the normal range.
*/
double IntervalDistance( int readCnt, int ecnt, int cpCnt, struct _solveRegionData &data )
{
int i, j, k ;
double ret = 0 ;
int min, max ;
int rmin, rmax ;
int scale = 0 ;
struct _enumExon *regionExons = data.regionExons ;
struct _constraintPoint *constraintPoints = data.constraintPoints ;
int *exonDist = data.exonDist ;
int *point = data.point ;
bool *eused = data.eused ;
memset( exonDist, -1, sizeof( exonDist[0] ) * ecnt * ecnt * 2 ) ;
for ( i = 0 ; i < cpCnt ; ++i )
{
if ( constraintPoints[i].type != 2 )
continue ;
++scale ;
rmin = MAX_LENGTH ;
rmax = -1 ;
for ( j = 0 ; j < ecnt ; ++j )
{
if ( !eused[j] || regionExons[j].start > point[ constraintPoints[i].startPos ] + 1 ||
regionExons[j].end < point[ constraintPoints[i].startPos + 1] )
continue ;
for ( k = j ; k < ecnt ; ++k )
{
if ( !eused[k] || regionExons[k].start > point[ constraintPoints[i].endPos ] + 1 ||
regionExons[k].end < point[ constraintPoints[i].endPos + 1 ] )
continue ;
if ( regionExons[j].strand != regionExons[k].strand ||
( constraintPoints[i].strand != -1 && regionExons[j].strand != constraintPoints[i].strand ) )
continue ;
if ( k != j )
{
FindExonDist( j, k, ecnt, data ) ;
int index = j * ecnt + k ;
min = exonDist[2 * index + 0] + regionExons[j].end - point[ constraintPoints[i].startPos + 1 ] + 1 +
point[ constraintPoints[i].endPos ] - regionExons[k].start + 2 ;
max = exonDist[2 * index + 1] + regionExons[j].end - point[ constraintPoints[i].startPos ] +
point[ constraintPoints[i].endPos + 1 ] - regionExons[k].start + 1 ;
}
else
{
min = point[ constraintPoints[i].endPos ] - point[ constraintPoints[i].startPos + 1 ] + 2 ;
max = point[ constraintPoints[i].endPos + 1 ] - point[ constraintPoints[i].startPos ] ;
}
if ( min < rmin )
rmin = min ;
if ( max > rmax )
rmax = max ;
}
}
if ( rmax < FRAG_LENGTH - 2 * FRAG_STD || rmin > FRAG_LENGTH + 2 * FRAG_STD ) // TODO: add parameter here.
{
//printf( "=.=: %d %d %d %d\n", constraintPoints[i].startPos, constraintPoints[i].endPos, rmin, rmax ) ;
ret += constraintPoints[i].support / (double)readCnt ;
}
} // end for i
return ret * scale ;
}
void EnumerateExon( int depth, int readCnt, int ecnt, int intervalCnt, int startCnt, int endCnt, int cpCnt, int mainStrand,
double &bestScore, bool *bestEused,
struct _solveRegionData &data, bool *fixedEused = NULL )
{
double lpResult, score ;
struct _enumExon *regionExons = data.regionExons ;
bool *eused = data.eused ;