-
Notifications
You must be signed in to change notification settings - Fork 1
/
TranscriptDecider.cpp
4985 lines (4577 loc) · 135 KB
/
TranscriptDecider.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
#include "TranscriptDecider.h"
#define MAX_TRANSCRIPT 1000003
#define BEST_COVER 1
#define ALL_TRANSCRIPT 0
#define EVIDENCE_MARGIN 10
#define CUTOFF_RATIO 0
#define USE_F3 150
#define USE_DP 200000
#define DP_EXON_ID_STRIDE 6
#define DP_STRIDE_CNT 5
#define MAX_TRANSCRIPT_CONSTRAINT (MAX_READ+1000)
#define SIZEOF_STRIDE ( ( DP_EXON_ID_STRIDE + 1 )*( DP_EXON_ID_STRIDE + 1 )*( DP_EXON_ID_STRIDE + 1 )*( DP_EXON_ID_STRIDE + 1 )*( DP_EXON_ID_STRIDE + 1 ) )
extern int FRAG_LENGTH, FRAG_STD ;
extern int READS_LENGTH ;
extern long long TOTAL_READ_COUNT ;
extern bool VERBOSE ;
extern int NUM_OF_THREADS ;
extern pthread_attr_t pthreadAttr ;
bool USE_SET_COVER ;
double FPKM_FRACTION = 0.05 ;
extern char *gtfIdLabel ;
struct _transcript
{
int *eid ; // Exon id
int ecnt ; // exon cnt
int strand ;
double abundance ; // The heuristic number of fragments belonging to the transcript
int geneId, transcriptId ;
double FPKM ;
} ;
struct _transcriptConstraint
{
int type ; // 0-consecutive exons, 1-splice junction, 2-evidence
int info[2] ; // type 0: the exon indices. info[1], the end position.
int leftInfo[5][2], rightInfo[5][2] ;
int infoCnt, lcnt, rcnt ;
int strand ;
int support ; // The number of reads supporting it.
int uniqSupport ; // The number of unique reads supporting it
double abundance ;
double normAbund ; // normalized abundance
//int effectiveLength ; // normAbund * effectiveLength = support
double effectiveCount ; // How many count when updating the count for a transcript.
} ;
// The exon node in the splice graph
struct _exonNode
{
int id ; // The index in the exon array.
int *next, *prev ;
int ncnt, pcnt ;
int nsize, psize ; // The # of memory allocated for next and prev
int farthest ; // The farthest end position of substrancripts starting from this one
int before ; // The exons before
bool *used ;
int subcnt ;
int weight ; // some dummy weight
int supportTranscript ; // How many transcripts contains this exon
int geneId ;
} ;
// The argument passed to the pthread calling
struct _pthreadArgPickTranscripts
{
char *chrom ;
struct _exon *exons ;
int exonCnt ;
struct _exonNode *nodes ;
struct _transcriptConstraint *tc ;
int tcCnt ;
int from, to ;
int evidenceIndFrom, evidenceIndTo ;
} ;
int currPickTxptThreadsCnt ;
pthread_mutex_t pickTxptMutex ;
pthread_mutex_t outputTxptMutex ;
pthread_cond_t idleCond ;
pthread_cond_t clearCond ;
FILE *td_fpDepth ;
struct _readFile td_fpReads ;
int transcriptStart = -1, transcriptEnd = -1, transcriptLastExonStart = -1 ;
//struct _transcript transcripts[MAX_TRANSCRIPT], alltranscripts[MAX_TRANSCRIPT] ;
struct _transcript outputTranscripts[ MAX_TRANSCRIPT ] ;
int outputTxptCnt ;
//BitTable btable[MAX_TRANSCRIPT] ;
double maxAbundance[MAX_EXON] ;
//int tcnt, atcnt ;
//struct _read transcriptReads[MAX_READ_GENE] ;
struct _geneRead transcriptReads ;
int trCnt ;
// abundanceTranscriptConstraints holds the constraints from single-end constraints.
// transcriptConstraints combines the abundanceTranscriptConstraints if the data is paired-end
struct _transcriptConstraint transcriptConstraints[MAX_TRANSCRIPT_CONSTRAINT],
alltc[MAX_TRANSCRIPT_CONSTRAINT], abundanceConstraints[MAX_TRANSCRIPT_CONSTRAINT] ;
//int pairExonTc[MAX_TRANSCRIPT_CONSTRAINT] ;
//int tcUsedCnt[MAX_TRANSCRIPT_CONSTRAINT] ; // How many times a transcript constraint used.
//int eidUsed[MAX_TRANSCRIPT] ;
int euCnt = 0 ;
//bool eidUsedFlag[MAX_EXON] ;
int geneId[MAX_EXON] ; // This is actually the set.
int geneTranscriptId[MAX_EXON] ; // The transcript id used for each gene
int geneIdCnt ;
double geneAbundance[MAX_EXON] ;
//int bufferEid[MAX_EXON], toEndDist[MAX_EXON] ;
struct _evidence *evidences ;
int eviTag, // The beginning of the evidences
eviCnt ; // The count of the evidences
struct _dp
{
int *eid ;
int ecnt ;
double cover ;
int timeStamp ;
double minAbundance ;
} ;
struct _dpStride
{
// To memorize the consecutive exons up to 6 (5 strides) by looking at the strides
struct _dp p[SIZEOF_STRIDE] ;
} ;
struct _dpHash
{
int *eid ;
int ecnt ;
double cover ;
int cnt ; // The length of the header for eid.
double minAbundance ;
int timeStamp ;
} ;
// A list of transcriptConstraints Id
struct _tcList
{
int *id ;
int cnt ;
} ;
struct _dpAttribute
{
struct _dp *f1, **f2, ***f3 ;
struct _dpStride *stride ;
struct _dpHash *hash ;
struct _dpHash *longHash ; // The hash for long subtranscript
int offset ;
int size ;
double minAbundance ;
bool forAbundance ; // Is the DP for calculating most expressed transcript?
bool solveRemaining ; // The main part using evidences finished.
struct _tcList *pairExonTc ; // Store the tc starting with a exon and compatible with it and its next.
int *pairExonTcIds ;
int *bufferEid ;
//struct _tcList *partPairExonTc ; // The
int timeStamp ;
int *solveCaller ; // count how many times we called SolveSub without using memoir.
} ;
struct _dp SolveSubTranscript( int visit[], int vcnt, const struct _dpAttribute &attr, struct _exonNode nodes[], struct _exon exons[],
struct _transcriptConstraint *tc, int tcCnt ) ;
// For set cover
struct _setcover
{
int choose[1024] ;
int cnt ;
double weight ;
} ;
void TranscriptDecider_Init( char *prefix )
{
char buffer[1024] ;
sprintf( buffer, "%s.sam", prefix ) ;
td_fpReads = OpenReadFile( prefix ) ;
//printf( "file pointer: %lld\n", td_fpReads.fp ) ;
sprintf( buffer, "%s.depth", prefix ) ;
td_fpDepth = fopen( buffer, "r" ) ;
//atcnt = 0 ;
transcriptEnd = -1 ;
transcriptLastExonStart = -1 ;
InitGeneReads( &transcriptReads ) ;
//for ( int i = 0 ; i < MAX_TRANSCRIPT ; ++i )
// alltranscripts[i].eid = NULL ;
/*if ( evidence != NULL )
Evidence_Init( evidence, &evidences ) ;
else
evidences = NULL ;*/
memset( geneTranscriptId, 0, sizeof( geneTranscriptId ) ) ;
}
void TranscriptDecider_Destroy()
{
CloseReadFile( td_fpReads ) ;
ReleaseGeneReads( &transcriptReads ) ;
fclose( td_fpDepth ) ;
}
// @return: FPKM of the transcripts
double GetTranscriptAbundance( struct _exon exons[], struct _transcript *transcript )
{
int i, len = 0 ;
double factor = ( FRAG_LENGTH == READS_LENGTH ) ? 1000000 : 2000000 ;
double ret ;
for ( i = 0 ; i < transcript->ecnt ; ++i )
len += exons[ transcript->eid[i] ].end - exons[ transcript->eid[i] ].start + 1 ;
//printf( "%lf (%d)\n", transcript->abundance, len ) ;
ret = transcript->abundance / ( len / (double)1000.0 ) / ( TOTAL_READ_COUNT / factor ) ;
return ret ;
}
void OutputTranscript( char *chrom, struct _exon exons[], struct _transcript *transcript )
{
//return ;
//printf( "hi2 %d\n", transcript->eid ) ;
int i ;
int a = transcript->eid[0] ;
int b = transcript->eid[ transcript->ecnt - 1 ] ;
char strand[2] = "+" ;
if ( exons[a].strand == 0 )
strand[0] = '-' ;
else if ( exons[a].strand == -1 )
strand[0] = '.' ;
//transcript->geneId = 0 ;
//transcript->transcriptId = 0 ;
//transcript->FPKM = 0 ;
char prefix[512] ;
prefix[0] = '\0' ;
if ( gtfIdLabel != NULL )
{
sprintf( prefix, "%s_", gtfIdLabel ) ;
}
printf( "%s\tCLASS\ttranscript\t%d\t%d\t1000\t%s\t.\tgene_id \"%s%s.%d\"; transcript_id \"%s%s.%d.%d\"; Abundance \"%.6lf\";\n",
chrom, exons[a].start, exons[b].end, strand,
prefix, chrom, transcript->geneId,
prefix, chrom, transcript->geneId, transcript->transcriptId, transcript->FPKM ) ;
for ( i = 0 ; i < transcript->ecnt ; ++i )
{
printf( "%s\tCLASS\texon\t%d\t%d\t1000\t%s\t.\tgene_id \"%s%s.%d\"; "
"transcript_id \"%s%s.%d.%d\"; exon_number \"%d\"; Abundance \"%.6lf\"\n",
chrom, exons[ transcript->eid[i] ].start, exons[ transcript->eid[i] ].end, strand,
prefix, chrom, transcript->geneId,
prefix, chrom, transcript->geneId, transcript->transcriptId,
i + 1, transcript->FPKM ) ;
}
//++transcriptId ;
}
int CompInt( const void *p1, const void *p2 )
{
return ( *( int * )p1 ) - ( *( int * )p2 ) ;
}
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 ;
}
// Returnt the nearest exons ending before dest
int EvidenceBinarySearch( int dest, struct _exon *e, int ecnt )
{
int l, r, m ;
l = 0 ;
r = ecnt -1 ;
while ( l <= r )
{
m = ( l + r ) / 2 ;
if ( e[m].end == dest )
return m ;
else if ( e[m].end < dest )
l = m + 1 ;
else
r = m - 1 ;
}
return l - 1 ;
}
bool IsInEvidence( int tpair[][2], int strand )
{
int j, l ;
int epair[2][2] ;
j = Evidence_Val( tpair[0], tpair[1] ) ;
if ( j == 1 )
return true ;
else if ( j == 2 )
return false ;
for ( j = eviTag ; j < eviCnt && evidences[j].exons[0].start <= tpair[0][0] + EVIDENCE_MARGIN ; ++j )
{
if ( evidences[j].exons[0].strand != strand )
continue ;
l = EvidenceBinarySearch( tpair[0][0] - EVIDENCE_MARGIN, evidences[j].exons, evidences[j].ecnt ) ;
if ( l < 0 )
l = 0 ;
for ( ; l < evidences[j].ecnt - 2 &&
evidences[j].exons[l].end <= tpair[0][0] + EVIDENCE_MARGIN ; ++l )
{
epair[0][0] = evidences[j].exons[l].end ;
epair[0][1] = evidences[j].exons[l + 1].start ;
epair[1][0] = evidences[j].exons[l + 1].end ;
epair[1][1] = evidences[j].exons[l + 2].start ;
if ( WithinEvidenceMargin( tpair[0][0], epair[0][0] ) && WithinEvidenceMargin( tpair[0][1], epair[0][1] )
&& WithinEvidenceMargin( tpair[1][0], epair[1][0] ) && WithinEvidenceMargin( tpair[1][1], epair[1][1] ) )
{
//printf( "- (%d, %d, %d) %d %d\n", i, atcnt, k, tpair[0][0], tmpCnt ) ;
Evidence_Add( tpair[0], tpair[1], 1 ) ;
return true ;
}
}
}
Evidence_Add( tpair[0], tpair[1], 2 ) ;
return false ;
}
// Search the first constraints starts after dest.
int BinarySearchTranscriptConstraints( int dest, struct _transcriptConstraint *tc, int tcCnt )
{
//return 0 ;
int l, r, m ;
l = 0 ;
r = tcCnt - 1 ;
while ( l <= r )
{
m = ( l + r ) / 2 ;
if ( tc[m].leftInfo[0][0] >= dest )
r = m - 1 ;
else
l = m + 1 ;
}
/*if ( l > 0 )
return l - 1 ;
else
return 0 ;*/
return l ;
}
// Search the last exon index starting before dest
int BinarySearchExons( int dest, int eid[], int ecnt, struct _exon exons[] )
{
//return 0 ;
int l, r, m ;
l = 0 ;
r = ecnt - 1 ;
while ( l <= r )
{
m = ( l + r ) / 2 ;
if ( exons[ eid[m] ].start > dest )
r = m - 1 ;
else
l = m + 1 ;
}
if ( r >= 0 )
return r ;
else
return 0 ;
}
int TranscriptConstraintDifference( struct _transcriptConstraint *a, struct _transcriptConstraint *b )
{
/*if ( ( a->type == 0 || b->type == 0 ) && a->type != b->type )
return a->type - b->type ;
if ( a->leftInfo[0][0] != b->leftInfo[0][0] && a->type != 0 && b->type != 0 )
return a->leftInfo[0][0] - b->leftInfo[0][0] ;*/
if ( a->leftInfo[0][0] != b->leftInfo[0][0] )
return a->leftInfo[0][0] - b->leftInfo[0][0] ;
if ( a->type != b->type )
return a->type - b->type ;
else if ( a->strand != b->strand )
return a->strand - b->strand ;
else
{
if ( a->type == 0 )
return a->info[0] - b->info[0] ;
else if ( a->type == 1 )
{
int i ;
if ( a->lcnt != b->lcnt )
return a->lcnt - b->lcnt ;
if ( a->rcnt != b->rcnt )
return a->rcnt - b->rcnt ;
for ( i = 0 ; i < a->lcnt ; ++i )
{
if ( a->leftInfo[i][0] != b->leftInfo[i][0] )
return a->leftInfo[i][0] - b->leftInfo[i][0] ;
if ( a->leftInfo[i][1] != b->leftInfo[i][1] )
return a->leftInfo[i][1] - b->leftInfo[i][1] ;
}
for ( i = 0 ; i < a->rcnt ; ++i )
{
if ( a->rightInfo[i][0] != b->rightInfo[i][0] )
return a->rightInfo[i][0] - b->rightInfo[i][0] ;
if ( a->rightInfo[i][1] != b->rightInfo[i][1] )
return a->rightInfo[i][1] - b->rightInfo[i][1] ;
}
}
else if ( a->type == 2 )
{
int i ;
for ( i = 0 ; i < 2 ; ++i )
{
if ( a->leftInfo[i][0] != b->leftInfo[i][0] )
return a->leftInfo[i][0] - b->leftInfo[i][0] ;
if ( a->leftInfo[i][1] != b->leftInfo[i][1] )
return a->leftInfo[i][1] - b->leftInfo[i][1] ;
}
}
}
if ( a->effectiveCount > b->effectiveCount )
return 1 ;
else if ( a->effectiveCount < b->effectiveCount )
return -1 ;
return 0 ;
}
int CompTranscriptConstraint( const void *p1, const void *p2 )
{
struct _transcriptConstraint *a, *b ;
a = ( ( struct _transcriptConstraint * )p1 ) ;
b = ( ( struct _transcriptConstraint * )p2 ) ;
return TranscriptConstraintDifference( a, b ) ;
}
int GetFather( int tag, int *father )
{
if ( father[tag] != tag )
return father[tag] = GetFather( father[tag], father ) ;
return tag ;
}
/**
Use the LP to quantificate transcripts.
*/
void Quantification( char *chrom, int *points, int pcnt, struct _exon exons[],
struct _transcript *transcripts, int tcnt )
{
return ; // not used anymore
int i, j, k ;
int *depth = (int *)malloc( ( pcnt + 10 ) * sizeof( *depth ) ) ;
int tmp, psum = 0, sum = 0 ;
char buffer[100] ;
int position, d, tag = -1 ;
bool started = false ;
// Read in the depth file and build the depth for each region.
while ( 1 )
{
fscanf( td_fpDepth, "%s %d %d", buffer, &position, &d ) ;
tmp = strcmp( buffer, chrom ) ;
if ( ( !tmp && position > points[pcnt - 1] ) || ( tmp && started ) )
break ;
if ( ( tmp && !started ) || ( !tmp && position < points[0] ) )
continue ;
started = true ;
psum += d ;
sum += d ;
if ( position == points[tag + 1] )
{
if ( tag == -1 )
{
++tag ;
continue ;
}
depth[tag] = psum ;
psum = 0 ;
++tag ;
}
}
//printf( "### %d %d\n", tag, pcnt ) ;
// Build the lp system.
int intervalCnt = pcnt - 1 ;
int Ncol, *colno ;
double *row ;
int p ;
lprec *lp ;
Ncol = tcnt + intervalCnt ;
colno = (int *)malloc( Ncol * sizeof( *colno ) ) ;
row = (double *)malloc( Ncol * sizeof( *row ) ) ;
lp = make_lp( 0, Ncol ) ;
set_add_rowmode( lp, TRUE ) ;
for ( i = 0 ; i < intervalCnt ; ++i )
{
int k = 0 ;
for ( j = 0 ; j < tcnt ; ++j )
{
for ( p = 0 ; p < transcripts[j].ecnt ; ++p )
{
if ( points[i] >= exons[ transcripts[j].eid[p] ].start &&
points[i + 1] <= exons[transcripts[j].eid[p] ].end )
{
break ;
}
//printf( "%d: %d %d %d %d\n", j, points[i], points[i + 1], exons[transcripts[j].eid[p] ].start, exons[ transcripts[j].eid[p] ].end ) ;
}
if ( p >= transcripts[j].ecnt )
continue ;
colno[k] = j + 1 ;
row[k] = 1 ;
++k ;
}
colno[k] = tcnt + i + 1 ;
row[k] = -1 ;
add_constraintex(lp, k + 1, row, colno, LE, (double)depth[i] / ( points[i + 1] - points[i] + 1 ) ) ;
row[k] = 1 ;
add_constraintex(lp, k + 1, row, colno, GE, (double)depth[i] / ( points[i + 1] - points[i] + 1 ) ) ;
}
/*for ( i = 0 ; i < tcnt ; ++i )
{
colno[0] = i + 1 ;
row[i] = 1 ;
add_constraintex( lp, 1, row, colno, GE, 1 ) ;
}*/
for ( i = 0 ; i < tcnt ; ++i )
{
tmp = 0 ;
for ( p = 0 ; p < transcripts[i].ecnt ; ++p )
tmp += ( exons[ transcripts[i].eid[p] ].end - exons[transcripts[i].eid[p] ].start + 1 ) ;
colno[i] = i + 1 ;
row[i] = tmp ;
}
add_constraintex( lp, tcnt, row, colno, LE, sum + tcnt ) ;
add_constraintex( lp, tcnt, row, colno, GE, sum - tcnt ) ;
set_add_rowmode( lp, FALSE ) ;
for ( i = 0 ; i < intervalCnt ; ++i )
{
colno[i] = tcnt + i + 1 ;
row[i] = 1 ;
}
set_obj_fnex( lp, intervalCnt, row, colno ) ;
set_minim( lp ) ;
set_verbose( lp, IMPORTANT ) ;
solve( lp ) ;
get_variables( lp, row ) ;
//write_LP( lp, stdout ) ;
for ( i = 0 ; i < tcnt ; ++i )
transcripts[i].abundance = row[i] ;
free( colno ) ;
free( row ) ;
delete_lp( lp ) ;
free( depth ) ;
}
int enumTid[MAX_TRANSCRIPT] ;
void EnumerateSetCover( int tag, int tcCnt, int depth, BitTable &destCover, struct _setcover &best,
BitTable *btable, struct _transcript *transcripts, int tcnt )
{
int i, j, k ;
BitTable cover( tcCnt ) ;
cover.Reset() ;
for ( i = 0 ; i < depth ; ++i )
cover.Or( btable[ enumTid[i] ] ) ;
//printf( "# %d\n", cover.Count() ) ;
if ( cover.IsEqual( destCover ) )
{
double weight = 0 ;
for ( i = 0 ; i < depth ; ++i )
weight += transcripts[ enumTid[i] ].abundance ;
if ( weight < best.weight )
{
best.weight = weight ;
best.cnt = depth ;
for ( i = 0 ; i < depth ; ++i )
best.choose[i] = enumTid[i] ;
}
//printf( "%d %d %d\n", tcCnt, cover.Count(), destCover.Count() ) ;
return ;
}
//printf( "hi %d %d %d\n", tag, depth, destCover.Count() ) ;
for ( i = tag ; i < tcnt ; ++i ) //- ( best.cnt - depth ) + 1 ; ++i )
{
//printf( "hi %d %d %d\n", i, depth, destCover.Count() ) ;
enumTid[depth] = i ;
double weight ;
cover.Reset() ;
for ( j = 0 ; j <= depth ; ++j )
{
cover.Or( btable[ enumTid[j] ] ) ;
weight += transcripts[ enumTid[j] ].abundance ;
}
for ( j = tag + 1 ; j < tcnt ; ++j )
cover.Or( btable[j] ) ;
if ( !cover.IsEqual( destCover ) || weight > best.weight )
continue ;
EnumerateSetCover( i + 1, tcCnt, depth + 1, destCover, best, btable, transcripts, tcnt ) ;
}
}
// Test whether a constraints is compatible with the transcript.
// Return 0 - uncompatible or does not overlap at all. 1 - fully compatible. 2 - Head of the constraints compatible with the tail of the transcript
int IsConstraintInTranscript( int *eid, int ecnt, const struct _transcriptConstraint &c, struct _exon exons[], int *toEndDist = NULL )
{
int i, j, k, tag ;
if ( c.type == 0 )
{
for ( i = BinarySearchExons( c.leftInfo[0][0], eid, ecnt, exons ) ; i < ecnt ; ++i )
{
if ( eid[i] < c.info[0] )
break ;
if ( eid[i] == c.info[0] )
return 1 ;
}
return 0 ;
}
else if ( c.type == 1 )
{
int tag = 0 ;
bool first = true, flag ;
int leftInd, rightInd ;
if ( c.strand != -1 && exons[ eid[0] ].strand != -1 && c.strand != exons[ eid[0] ].strand )
return 0 ;
if ( c.lcnt )
{
k = 0 ;
for ( tag = BinarySearchExons( c.leftInfo[0][0], eid, ecnt, exons ) ; tag < ecnt ; ++tag )
{
if ( c.leftInfo[0][0] < exons[ eid[tag] ].start )
return 0 ;
if ( c.leftInfo[k][0] >= exons[ eid[tag] ].start
&& c.leftInfo[k][1] <= exons[ eid[tag] ].end
&& ( k == c.lcnt - 1 || c.leftInfo[k][1] == exons[ eid[tag]].end )
&& ( k == 0 || c.leftInfo[k][0] == exons[ eid[tag] ].start ) )
break ;
}
if ( tag >= ecnt )
return 0 ;
++tag ;
for ( k = 1 ; k < c.lcnt && tag < ecnt ; ++k, ++tag )
{
if ( c.leftInfo[k][0] >= exons[ eid[tag] ].start
&& c.leftInfo[k][1] <= exons[ eid[tag] ].end
&& ( k == c.lcnt - 1 || c.leftInfo[k][1] == exons[ eid[tag]].end )
&& ( k == 0 || c.leftInfo[k][0] == exons[ eid[tag] ].start ) )
continue ;
break ;
}
if ( k < c.lcnt && tag >= ecnt )
{
return 2 ;
}
if ( k < c.lcnt )
return 0 ;
leftInd = tag - 1 ;
}
//printf( "### %d %d %d %d %d\n", i, j, transcripts[i].ecnt, tc[j].lcnt, tc[j].rcnt ) ;
if ( c.rcnt )
{
int len = 0 ;
if ( c.lcnt && c.rightInfo[0][0] > exons[ eid[ ecnt - 1] ].end )
{
// Test the insert size.
len = 0 ;
for ( i = leftInd + 1 ; i < ecnt ; ++i )
len += ( exons[ eid[i] ].end - exons[ eid[i] ].start + 1 ) ;
len += ( exons[ eid[ leftInd ] ].end - c.leftInfo[ c.lcnt - 1 ][1] + 1 ) ;
if ( len >= FRAG_LENGTH + 2 * FRAG_STD - 2 * READS_LENGTH )
return 0 ;
return 2 ;
}
k = 0 ;
for ( tag = BinarySearchExons( c.rightInfo[0][0], eid, ecnt, exons ) ; tag < ecnt ; ++tag )
{
if ( c.rightInfo[0][0] < exons[ eid[ tag ] ].start )
return 0 ;
if ( c.rightInfo[k][0] >= exons[ eid[tag] ].start
&& c.rightInfo[k][1] <= exons[ eid[tag] ].end
&& ( k == c.rcnt - 1 || c.rightInfo[k][1] == exons[ eid[tag]].end )
&& ( k == 0 || c.rightInfo[k][0] == exons[ eid[tag] ].start ) )
break ;
}
if ( tag >= ecnt )
return 0 ;
rightInd = tag ;
++tag ;
// Test the insert size.
len = 0 ;
for ( k = leftInd + 1 ; k <= rightInd - 1 ; ++k )
{
len += ( exons[ eid[k] ].end - exons[ eid[k] ].start + 1 ) ;
}
if ( leftInd < rightInd )
{
len += ( exons[ eid[ leftInd ] ].end - c.leftInfo[ c.lcnt - 1 ][1] + 1 ) ;
len += ( c.rightInfo[0][0] - exons[ eid[ rightInd ] ].start + 1 ) ;
}
if ( len >= FRAG_LENGTH + 2 * FRAG_STD - 2 * READS_LENGTH )
return 0 ;
for ( k = 1 ; k < c.rcnt && tag < ecnt ; ++k, ++tag )
{
if ( c.rightInfo[k][0] >= exons[ eid[tag] ].start
&& c.rightInfo[k][1] <= exons[ eid[tag] ].end
&& ( k == c.rcnt - 1 || c.rightInfo[k][1] == exons[ eid[tag]].end )
&& ( k == 0 || c.rightInfo[k][0] == exons[ eid[tag] ].start ) )
continue ;
break ;
}
if ( k < c.rcnt && tag >= ecnt )
return 2 ;
if ( k < c.rcnt )
return 0 ;
//printf( "success\n" ) ;
}
//if ( k >= tc[j].rcnt && tc[j].rcnt )
// continue ;
/*if ( tc[j].leftInfo[0][1] == 2742878 && tc[j].leftInfo[1][0] == 2757641 )
{
// printf( "%d %d %d %d\n", i, j, k, tc[j].lcnt ) ;
//exit( 1 ) ;
}*/
return 1 ;
}
else if ( c.type == 2 )
{
if ( c.strand != exons[ eid[0] ].strand )
return 0 ;
int pairs[2][2] ;
for ( i = BinarySearchExons( c.leftInfo[0][0], eid, ecnt, exons ) ; i < ecnt - 2 ; ++i )
{
if ( c.leftInfo[0][0] < exons[ eid[i] ].end )
break ;
pairs[0][0] = exons[ eid[i] ].end ;
pairs[0][1] = exons[ eid[i + 1] ].start ;
pairs[1][0] = exons[ eid[i + 1] ].end ;
pairs[1][1] = exons[ eid[i + 2] ].start ;
for ( j = 0 ; j < 2 ; ++j )
{
if ( pairs[j][0] != c.leftInfo[j][0] || pairs[j][1] != c.leftInfo[j][1] )
break ;
}
if ( j >= 2 )
return 1 ;
}
if ( exons[ eid[ecnt -1 ] ].end == c.leftInfo[0][0] )
return 2 ;
if ( ecnt >= 2 && exons[ eid[ecnt - 2] ].end == c.leftInfo[0][0] &&
exons[ eid[ecnt - 1] ].start == c.leftInfo[0][1] &&
exons[ eid[ecnt - 1] ].end == c.leftInfo[1][0] )
return 2 ;
}
return 0 ;
}
/**
Compare two transcripts.
*/
int TranscriptsDifference( const struct _transcript &t1, const struct _transcript &t2 )
{
int i ;
int cnt = t1.ecnt < t2.ecnt ? t1.ecnt : t2.ecnt ;
for ( i = 0 ; i < cnt ; ++i )
{
if ( t1.eid[i] != t2.eid[i] )
return t1.eid[i] - t2.eid[i] ;
}
return t1.ecnt - t2.ecnt ;
}
// For qsort
int CompTranscripts( const void *p1, const void *p2 )
{
return TranscriptsDifference( *( struct _transcript * )p1, *( struct _transcript * )p2 ) ;
}
int CompTranscriptsAbundance( const void *p1, const void *p2 )
{
if ( ( ( struct _transcript *)p1 )->abundance != ( ( struct _transcript *)p2 )->abundance )
{
if ( ( ( struct _transcript *) p1 )->abundance < ( ( struct _transcript *)p2 )->abundance )
return 1 ;
else
return -1 ;
}
else
return 0 ;
}
int GetTranscriptExonWeight( struct _exonNode nodes[], const struct _transcript &t )
{
int ret = 0 ;
int i ;
for ( i = 0 ; i < t.ecnt ; ++i )
ret += nodes[ t.eid[i] ].weight ;
return ret ;
}
inline double ComputeScore( double cnt, double a, double A )
{
return cnt * ( 1 + pow( a / A, 0.25 ) ) ;
}
/**
Pick the minimum # of transcripts in region [tstart, tend] that satisfy
all the constraints.
*/
void PickTranscripts( char *chrom, struct _exonNode nodes[], struct _exon exons[],
struct _transcript *alltranscripts, const int &atcnt, struct _transcript *transcripts, int &tcnt,
struct _transcriptConstraint *tc, const int &tcCnt )
{
int i, j, k ;
int tag ;
int mate ;
//struct _transcriptConstraint *tc = transcriptConstraints ;
BitTable *btable = new BitTable[ atcnt ] ;
//BitTable *btable = (BitTable *)malloc( sizeof( *btable ) * atcnt ) ;
//delete [] btable ;
//int *tcUsedCnt ;
//tcUsedCnt = ( int * )malloc( sizeof( int ) * tcCnt ) ;
//memset( tcUsedCnt, 0, sizeof( tcUsedCnt ) ) ;
for ( i = 0 ; i < atcnt ; ++i )
btable[i].Init( tcCnt ) ;
tcnt = 0 ;
#if ALL_TRANSCRIPT
tcCnt = 0 ;
for ( i = 0 ; i < atcnt ; ++i )
{
int transcriptId = 1 ;
alltranscripts[i].transcriptId = transcriptId ;
OutputTranscript( chrom, exons, &alltranscripts[i] ) ;
}
delete [] btable ;
return ;
#endif
for ( i = 0 ; i < atcnt ; ++i )
{
for ( j = 0 ; j < tcCnt ; ++j )
{
if ( IsConstraintInTranscript( alltranscripts[i].eid, alltranscripts[i].ecnt, tc[j], exons) == 1 )
btable[i].Set( j ) ;
}
}
BitTable bAll( tcCnt ) ;
for ( i = 0 ; i < atcnt ; ++i )
bAll.Or( btable[i] ) ;
//printf( "# %d\n", tcCnt ) ;
/*for ( i = 0 ; i < tcCnt ; ++i )
{
if ( !bAll.Test(i) )
{
//tcUsedCnt[i] = MAX_READ ;
tc[i].support = tc[i].abundance = 0 ;
}
printf( "## %d %lf:", i, tc[i].normAbund ) ;
printf( "%d %d: ", tc[i].lcnt, tc[i].rcnt ) ;
for ( j = 0 ; j < tc[i].lcnt ; ++j )
printf( "(%d %d) ", tc[i].leftInfo[j][0], tc[i].leftInfo[j][1] ) ;
for ( j = 0 ; j < tc[i].rcnt ; ++j )
printf( "[%d %d] ", tc[i].rightInfo[j][0], tc[i].rightInfo[j][1] ) ;
printf( "\n" ) ;
}*/
/*for ( i = 0 ; i < tcCnt ; ++i )
{
if ( tc[i].support == 1 )
{
printf( "%d %d: ", tc[i].lcnt, tc[i].rcnt ) ;
for ( j = 0 ; j < tc[i].lcnt ; ++j )
printf( "(%d %d) ", tc[i].leftInfo[j][0], tc[i].leftInfo[j][1] ) ;
for ( j = 0 ; j < tc[i].rcnt ; ++j )
printf( "(%d %d) ", tc[i].rightInfo[j][0], tc[i].rightInfo[j][1] ) ;
printf( "\n" ) ;
}
}*/
/*for ( i = 0 ; i < atcnt ; ++i )
{
double *abundances = (double *)malloc( sizeof( double ) * tcCnt ) ;
int aused = 0 ;
int cut ;
for ( j = 0 ; j < tcCnt ; ++j )
{
if ( tc[j].support == MAX_READ || tc[j].support == 0 || !btable[i].Test(j))
continue ;
abundances[aused] = tc[j].normAbund ; //tc[j].support ;
++aused ;
}
qsort( abundances, aused, sizeof( abundances[0] ), CompDouble ) ;
cut = abundances[ int( aused * 0.5 ) ] ;
int abundance = MAX_READ ;
for ( j = 0 ; j < tcCnt ; ++j )
{
//if ( btable[i].Test(j) )
// printf( "%d %d %d\n", tc[j].abundance, tc[j].support, tc[j].type ) ;
if ( tc[j].abundance >= cut && tc[j].abundance < abundance && btable[i].Test(j) )
abundance = tc[j].abundance ;
}
if ( abundance == MAX_READ )
abundance = 1 ;
printf( "%d %d : ", i, btable[i].Count() ) ;
for ( j = 0 ; j < aused ; ++j )
printf( "%lf ", (double)abundances[j] ) ;
printf( "\n" ) ;
alltranscripts[i].transcriptId = i ;
//++i ;
OutputTranscript( chrom, exons, &alltranscripts[i] ) ;
free( abundances ) ;
}
return ;*/
for ( i = 0 ; i < 0 ; ++i )
{
OutputTranscript( chrom, exons, &alltranscripts[i] ) ;
printf( "constraints: " ) ;
for ( j = 0 ; j < tcCnt ; ++j )
{
if ( tc[j].type != 1 )
continue ;
printf( "%d", btable[i].Test( j ), tc[j].lcnt, tc[j].rcnt ) ;
}
printf( ": %d", btable[i].Count() ) ;
printf( "\n" ) ;
/*printf( "evidence: " ) ;
for ( j = 0 ; j < alltranscripts[i].ecnt - 2 ; ++j )
{
int tpair[2][2] ;
tpair[0][0] = exons[ alltranscripts[i].eid[j] ].end ;
tpair[0][1] = exons[ alltranscripts[i].eid[j + 1] ].start ;
tpair[1][0] = exons[ alltranscripts[i].eid[j + 1] ].end ;
tpair[1][1] = exons[ alltranscripts[i].eid[j + 2] ].start ;