-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConstraints.cpp
438 lines (391 loc) · 13.3 KB
/
Constraints.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
#include "Constraints.hpp"
// return whether this constraint is compatible with the subexons.
bool Constraints::ConvertAlignmentToBitTable( struct _pair *segments, int segCnt,
struct _subexon *subexons, int seCnt, int seStart, struct _constraint &ct )
{
int i, j, k ;
k = seStart ;
ct.vector.Init( seCnt ) ;
// Each segment of an alignment can cover several subexons.
// But the first and last segment can partially cover a subexon.
for ( i = 0 ; i < segCnt ; ++i )
{
int leftIdx, rightIdx ; // the range of subexons covered by this segment.
leftIdx = -1 ;
rightIdx = -1 ;
for ( ; k < seCnt ; ++k )
{
//if ( segments[0].b == 110282529 && segCnt == 2 )
// printf( "(%d:%d %d):(%d:%d %d)\n", i, (int)segments[i].a, (int)segments[i].b, k, (int)subexons[k].start, (int)subexons[k].end ) ;
if ( subexons[k].start > segments[i].b )
break ;
if ( segments[i].a > subexons[k].end )
continue ;
int relaxedWidth = 0 ;
if ( ( subexons[k].start >= segments[i].a && subexons[k].end <= segments[i].b )
|| ( i == 0 && subexons[k].start - relaxedWidth < segments[i].a && subexons[k].end <= segments[i].b )
|| ( i == segCnt - 1 && subexons[k].start >= segments[i].a && subexons[k].end + relaxedWidth > segments[i].b )
|| ( i == 0 && i == segCnt - 1 && subexons[k].start - relaxedWidth < segments[i].a && subexons[k].end + relaxedWidth > segments[i].b ) )
{
if ( leftIdx == -1 )
leftIdx = k ;
rightIdx = k ;
ct.vector.Set( k ) ;
}
else
{
return false ;
}
}
if ( leftIdx == -1 )
return false ;
// The cover contradict the boundary.
if ( !( ( subexons[leftIdx].leftType == 0 || subexons[leftIdx].start <= segments[i].a )
&& ( subexons[rightIdx].rightType == 0 || subexons[rightIdx].end >= segments[i].b ) ) )
return false ;
// The intron must exists in the subexon graph.
if ( i > 0 )
{
for ( j = 0 ; j < subexons[ ct.last ].nextCnt ; ++j )
if ( subexons[ct.last].next[j] == leftIdx )
break ;
if ( j >= subexons[ ct.last ].nextCnt )
return false ;
}
// The subexons must be consecutive
for ( j = leftIdx + 1 ; j <= rightIdx ; ++j )
if ( subexons[j].start > subexons[j - 1].end + 1 )
return false ;
if ( i == 0 )
ct.first = leftIdx ;
ct.last = rightIdx ;
}
return true ;
}
void Constraints::CoalesceSameConstraints()
{
int i, k ;
int size = constraints.size() ;
for ( i = 0 ; i < size ; ++i )
{
constraints[i].info = i ;
//printf( "constraints %d: %d %d %d\n", i, constraints[i].vector.Test( 0 ), constraints[i].vector.Test(1), constraints[i].support ) ;
}
std::vector<int> newIdx ;
newIdx.resize( size, 0 ) ;
// Update the constraints.
if ( size > 0 )
{
std::sort( constraints.begin(), constraints.end(), CompSortConstraints ) ;
k = 0 ;
newIdx[ constraints[0].info ] = 0 ;
for ( i = 1 ; i < size ; ++i )
{
if ( constraints[k].vector.IsEqual( constraints[i].vector ) )
{
constraints[k].weight += constraints[i].weight ;
constraints[k].support += constraints[i].support ;
constraints[k].uniqSupport += constraints[i].uniqSupport ;
constraints[k].maxReadLen = ( constraints[k].maxReadLen > constraints[i].maxReadLen ) ?
constraints[k].maxReadLen : constraints[i].maxReadLen ;
constraints[i].vector.Release() ;
}
else
{
++k ;
if ( k != i )
constraints[k] = constraints[i] ;
}
newIdx[ constraints[i].info ] = k ;
}
constraints.resize( k + 1 ) ;
}
// Update the mate pairs.
size = matePairs.size() ;
if ( size > 0 )
{
for ( i = 0 ; i < size ; ++i )
{
//printf( "%d %d: %d => %d | %d =>%d\n", i, newIdx.size(), matePairs[i].i, newIdx[ matePairs[i].i ],
// matePairs[i].j, newIdx[ matePairs[i].j ] ) ;
matePairs[i].i = newIdx[ matePairs[i].i ] ;
matePairs[i].j = newIdx[ matePairs[i].j ] ;
}
std::sort( matePairs.begin(), matePairs.end(), CompSortMatePairs ) ;
k = 0 ;
for ( i = 1 ; i < size ; ++i )
{
if ( matePairs[i].i == matePairs[k].i && matePairs[i].j == matePairs[k].j )
{
matePairs[k].support += matePairs[i].support ;
matePairs[k].uniqSupport += matePairs[i].uniqSupport ;
}
else
{
++k ;
matePairs[k] = matePairs[i] ;
}
}
//printf( "%s: %d\n", __func__, matePairs[1].i) ;
matePairs.resize( k + 1 ) ;
}
// Update the data structure for future mate pairs.
mateReadIds.UpdateIdx( newIdx ) ;
}
void Constraints::ComputeNormAbund( struct _subexon *subexons )
{
int i, j ;
int ctSize = constraints.size() ;
for ( i = 0 ; i < ctSize ; ++i )
{
// spanned more than 2 subexon
int readLen = constraints[i].maxReadLen ;
if ( constraints[i].first + 1 < constraints[i].last )
{
std::vector<int> subexonInd ;
constraints[i].vector.GetOnesIndices( subexonInd ) ;
int size = subexonInd.size() ;
for ( j = 1 ; j < size - 1 ; ++j )
{
int a = subexonInd[j] ;
readLen -= ( subexons[a].end - subexons[a].start + 1 ) ;
}
}
int effectiveLength ;
if ( constraints[i].first == constraints[i].last )
{
effectiveLength = ( subexons[ constraints[i].first ].end - readLen + 1 )- subexons[ constraints[i].first ].start + 1 ;
if ( effectiveLength <= 0 ) // this happens in the 3',5'-end subexon, where we trimmed the length
effectiveLength = ( subexons[ constraints[i].first ].end - subexons[ constraints[i].first ].start + 1 ) / 2 + 1 ;
}
else
{
int a = constraints[i].first ;
int b = constraints[i].last ;
int start, end ; // the range of the possible start sites of a read in subexons[a].
start = subexons[a].end + 1 - ( readLen - 1 ) ;
if ( start < subexons[a].start )
start = subexons[a].start ;
if ( subexons[b].end - subexons[b].start + 1 >= readLen - 1 || subexons[b].rightType == 0 )
end = subexons[a].end ;
else
{
end = subexons[a].end + 1 - ( readLen - ( subexons[b].end - subexons[b].start + 1 ) ) ;
}
if ( end < start ) // when we trimmed the subexon.
end = subexons[a].start ;
effectiveLength = end - start + 1 ;
}
//printf( "%d: effectiveLength=%d support=%d\n", i, effectiveLength, constraints[i].support ) ;
constraints[i].normAbund = (double)constraints[i].weight / (double)effectiveLength ;
if ( ( subexons[ constraints[i].first ].leftType == 0 && subexons[ constraints[i].first ].end - subexons[ constraints[i].first ].start + 1 >= 8 * pAlignments->readLen )
|| ( subexons[ constraints[i].last ].rightType == 0 && subexons[ constraints[i].last ].end - subexons[ constraints[i].last ].start + 1 >= 8 * pAlignments->readLen ) ) // some random elongation of the sequence might make unnecessary long effective length.
{
constraints[i].normAbund *= 2 ;
}
constraints[i].abundance = constraints[i].normAbund ;
}
ctSize = matePairs.size() ;
for ( i = 0 ; i < ctSize ; ++i )
{
double a = constraints[ matePairs[i].i ].normAbund ;
double b = constraints[ matePairs[i].j ].normAbund ;
matePairs[i].normAbund = a < b ? a : b ;
if ( matePairs[i].i != matePairs[i].j )
{
if ( subexons[ constraints[ matePairs[i].i ].first ].leftType == 0
&& constraints[ matePairs[i].i ].first == constraints[ matePairs[i].i ].last
&& a < b )
{
matePairs[i].normAbund = b ;
}
else if ( subexons[ constraints[ matePairs[i].j ].last ].rightType == 0
&& constraints[ matePairs[i].j ].first == constraints[ matePairs[i].j ].last
&& a > b )
{
matePairs[i].normAbund = a ;
}
}
//matePairs[i].normAbund = sqrt( a * b ) ;
matePairs[i].abundance = matePairs[i].normAbund ;
}
}
int Constraints::BuildConstraints( struct _subexon *subexons, int seCnt, int start, int end )
{
int i ;
int tag = 0 ;
int coalesceThreshold = 16384 ;
Alignments &alignments = *pAlignments ;
// Release the memory from previous gene.
int size = constraints.size() ;
if ( size > 0 )
{
for ( i = 0 ; i < size ; ++i )
constraints[i].vector.Release() ;
std::vector<struct _constraint>().swap( constraints ) ;
}
std::vector<struct _matePairConstraint>().swap( matePairs ) ;
mateReadIds.Clear() ;
// Start to build the constraints.
bool callNext = false ; // the last used alignment
if ( alignments.IsAtBegin() )
callNext = true ;
while ( !alignments.IsAtEnd() )
{
if ( callNext )
{
if ( !alignments.Next() )
break ;
}
else
callNext = true ;
if ( alignments.GetChromId() < subexons[0].chrId )
continue ;
else if ( alignments.GetChromId() > subexons[0].chrId )
break ;
// locate the first subexon in this region that overlapps with current alignment.
for ( ; tag < seCnt && subexons[tag].end < alignments.segments[0].a ; ++tag )
;
if ( tag >= seCnt )
break ;
if ( alignments.segments[ alignments.segCnt - 1 ].b < subexons[tag].start )
continue ;
int uniqSupport = 0 ;
if ( usePrimaryAsUnique )
uniqSupport = alignments.IsPrimary() ? 1 : 0 ;
else
uniqSupport = alignments.IsUnique() ? 1 : 0 ;
struct _constraint ct ;
ct.vector.Init( seCnt ) ;
//printf( "%s %d: %lld-%lld | %d-%d\n", __func__, alignments.segCnt, alignments.segments[0].a, alignments.segments[0].b, subexons[tag].start, subexons[tag].end ) ;
ct.weight = 1.0 / alignments.GetNumberOfHits() ;
if ( alignments.IsGCRich() )
ct.weight *= 10 ;
ct.normAbund = 0 ;
ct.support = 1 ;
ct.uniqSupport = uniqSupport ;
ct.maxReadLen = alignments.GetRefCoverLength() ;
if ( alignments.IsPrimary() && ConvertAlignmentToBitTable( alignments.segments, alignments.segCnt,
subexons, seCnt, tag, ct ) )
{
//printf( "%s ", alignments.GetReadId() ) ;
//ct.vector.Print() ;
// If the alignment has clipped end or tail. We only keep those clipped in the 3'/5'-end
bool validClip = true ;
if ( alignments.HasClipHead() )
{
if ( ( ct.first < seCnt - 1 && subexons[ct.first].end + 1 == subexons[ct.first + 1].start )
|| subexons[ct.first].prevCnt > 0
|| alignments.segments[0].b - alignments.segments[0].a + 1 <= alignments.GetRefCoverLength() / 3.0 )
validClip = false ;
}
if ( alignments.HasClipTail() )
{
int tmp = alignments.segCnt - 1 ;
if ( ( ct.last > 0 && subexons[ct.last].start - 1 == subexons[ct.last - 1].end )
|| subexons[ct.last].nextCnt > 0
|| alignments.segments[tmp].b - alignments.segments[tmp].a + 1 <= alignments.GetRefCoverLength() / 3.0 )
validClip = false ;
}
if ( validClip )
{
constraints.push_back( ct ) ; // if we just coalesced but the list size does not decrease, this will force capacity increase.
//if ( !strcmp( alignments.GetReadId(), "ERR188021.8489052" ) )
// ct.vector.Print() ;
// Add the mate-pair information.
int mateChrId ;
int64_t matePos ;
alignments.GetMatePosition( mateChrId, matePos ) ;
if ( alignments.GetChromId() == mateChrId )
{
if ( matePos < alignments.segments[0].a )
{
int mateIdx = mateReadIds.Query( alignments.GetReadId(), alignments.segments[0].a ) ;
if ( mateIdx != -1 )
{
struct _matePairConstraint nm ;
nm.i = mateIdx ;
nm.j = constraints.size() - 1 ;
nm.abundance = 0 ;
nm.support = 1 ;
nm.uniqSupport = uniqSupport ;
nm.effectiveCount = 2 ;
matePairs.push_back( nm ) ;
}
}
else if ( matePos > alignments.segments[0].a )
{
mateReadIds.Insert( alignments.GetReadId(), alignments.segments[0].a, constraints.size() - 1, matePos ) ;
}
else // two mates have the same coordinate.
{
if ( alignments.IsFirstMate() )
{
struct _matePairConstraint nm ;
nm.i = constraints.size() - 1 ;
nm.j = constraints.size() - 1 ;
nm.abundance = 0 ;
nm.support = 1 ;
nm.uniqSupport = uniqSupport ;
nm.effectiveCount = 2 ;
matePairs.push_back( nm ) ;
}
}
}
}
else
ct.vector.Release() ;
// Coalesce if necessary.
size = constraints.size() ;
if ( (int)size > coalesceThreshold && size == (int)constraints.capacity() )
{
//printf( "start coalescing. %d\n", constraints.capacity() ) ;
CoalesceSameConstraints() ;
// Not coalesce enough
if ( constraints.size() >= constraints.capacity() / 2 )
{
coalesceThreshold *= 2 ;
}
}
}
else
{
//printf( "not compatible\n" ) ;
ct.vector.Release() ;
}
}
//printf( "start coalescing. %d %d\n", constraints.size(), matePairs.size() ) ;
CoalesceSameConstraints() ;
//printf( "after coalescing. %d %d\n", constraints.size(), matePairs.size() ) ;
//for ( i = 0 ; i < matePairs.size() ; ++i )
// printf( "matePair: %d %d %d\n", matePairs[i].i, matePairs[i].j, matePairs[i].support ) ;
// single-end data set
//if ( matePairs.size() == 0 )
if ( alignments.fragStdev == 0 )
{
int size = constraints.size() ;
matePairs.clear() ;
for ( i = 0 ; i < size ; ++i )
{
struct _matePairConstraint nm ;
nm.i = i ;
nm.j = i ;
nm.abundance = 0 ;
nm.support = constraints[i].support ;
nm.uniqSupport = constraints[i].uniqSupport ;
nm.effectiveCount = 1 ;
matePairs.push_back( nm ) ;
}
}
ComputeNormAbund( subexons ) ;
/*for ( i = 0 ; i < constraints.size() ; ++i )
{
printf( "constraints %d: %lf %d %d %d ", i, constraints[i].normAbund, constraints[i].first, constraints[i].last, constraints[i].support ) ;
constraints[i].vector.Print() ;
}
for ( i = 0 ; i < matePairs.size() ; ++i )
{
printf( "mates %d: %lf %d %d %d %d\n", i, matePairs[i].normAbund, matePairs[i].i, matePairs[i].j, matePairs[i].support, matePairs[i].uniqSupport ) ;
}*/
return 0 ;
}