-
Notifications
You must be signed in to change notification settings - Fork 1
/
states.h
337 lines (319 loc) · 13.8 KB
/
states.h
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
#ifndef STATES_H
#define STATES_H
#include "constants.h"
#include "parameters.h"
#include "state.h"
#include "bwt.h"
/******* States type *******/
/**
* The states stores the mapping information of the prefixes of reads.
* It is:
* - an array of states: 1st dimension is the depth, 2nd is the number of errors
* - the number of states per depth, per errors
* - the number of states per depth
* - the min number of error per depth
* - the max number of error per depth
* - the tree size
*/
typedef struct {
state_t **states;
size_t **firstState;
size_t **nStates;
size_t *allocatedNStates;
size_t *nStatesPerPosition;
size_t *minErrors;
size_t *maxErrors;
size_t depth;
} states_t;
void printStates (const states_t *states, size_t depth) {
for (size_t i = 0; i < depth; ++i) {
if (states->minErrors[i] == SIZE_MAX) {
printf("\t\t\t\t(%zu) ---\n", i);
}
else {
printf("\t\t\t\t(%zu)", i);
for (size_t j = states->minErrors[i]; j <= states->maxErrors[i]; ++j) {
printf(" %zu errors: %zu states ", j, states->nStates[i][j]);
}
printf("\n");
}
}
for (size_t i = 0; i <= depth; ++i) {
printf("\t\t\t\tdepth %zu:", i);
for (size_t nErrors = 0; nErrors <= parameters->maxNErrors; ++nErrors) {
printf("\t%zu errors: %zu (%zu) cells, starting @ %zu", nErrors, states->nStates[i][nErrors], states->firstState[i+1][nErrors] - states->firstState[i][nErrors], states->firstState[i][nErrors]);
//assert(states->firstState[i+1][nErrors] - states->firstState[i][nErrors] == states->nStates[i][nErrors]);
}
printf("\n");
}
}
size_t getNStates(const states_t *states, size_t depth, size_t nErrors) {
return states->nStates[depth][nErrors];
}
state_t *getState(const states_t *states, size_t depth, size_t nErrors, size_t i) {
//printf("Getting state #%zu at depth %zu with %zu errors: %zu\n", i, depth, nErrors, states->firstState[depth][nErrors]+i);
//printf("\tfirst state: %zu, next first state: %zu\n", states->firstState[depth][nErrors], states->firstState[depth+1][nErrors]);
//printStates(states, depth);
assert(i < getNStates(states, depth, nErrors));
//assert((depth == states->depth) || (states->firstState[depth+1][nErrors] == 0) || (states->firstState[depth+1][nErrors] >= states->firstState[depth][nErrors]));
return &states->states[nErrors][states->firstState[depth][nErrors]+i];
}
/**
* Update counts and allocate arrays to receive new states
*/
void updateCounts (states_t *states, size_t depth, size_t nErrors, size_t nStates) {
//printf("Entering update counts with states %p depth %zu, %zu errors, and %zu states, errors [%zu-%zu/%lu]\n", states, depth, nErrors, nStates, states->minErrors[depth], states->maxErrors[depth], SIZE_MAX); fflush(stdout);
assert(depth <= states->depth);
size_t nStatesPerError;
if (states->nStates[depth][nErrors] == 0) {
// TODO: BAD BUG FIX! SW should be complete!
if (depth == 0) {
states->firstState[depth][nErrors] = 0;
}
else {
size_t previousDepth;
for (previousDepth = depth-1; (previousDepth > 0) && (states->nStates[previousDepth][nErrors] == 0); --previousDepth) ;
states->firstState[depth][nErrors] = states->firstState[previousDepth][nErrors] + states->nStates[previousDepth][nErrors];
}
}
/*
if (depth > 0) {
printf("Previous state: %zu/%zu\n", states->firstState[depth-1][nErrors], states->nStates[depth-1][nErrors]);
fflush(stdout);
}
printf("first state: %zu\n", states->firstState[depth][nErrors]); fflush(stdout);
*/
states->nStates[depth][nErrors] += nStates;
states->nStatesPerPosition[depth] += nStates;
nStatesPerError = states->firstState[depth][nErrors] + states->nStates[depth][nErrors];
//stats->maxNStates[nErrors] = MAX(stats->maxNStates[nErrors], nStatesPerError);
if (nStatesPerError >= states->allocatedNStates[nErrors]-1) {
while (nStatesPerError >= states->allocatedNStates[nErrors]-1) {
states->allocatedNStates[nErrors] *= 2;
}
states->states[nErrors] = (state_t *) reallocOrDie(states->states[nErrors], states->allocatedNStates[nErrors] * sizeof(state_t));
//printf("Reallocating states with %zu errors to %zu states\n", nErrors, states->allocatedNStates[nErrors]);
if (states->states[nErrors] == NULL) {
fprintf(stderr, "Memory error: Cannot allocate an array of States for errors %zu of size %zu * %zu\n", nErrors, states->allocatedNStates[nErrors], sizeof(state_t));
exit(EXIT_FAILURE);
}
//printf("Reallocation #states with %zu errors: now %zu\n", nErrors, states->allocatedNStates[nErrors]); fflush(stdout);
//printStates(states, depth);
//return NULL;
}
//heapifyStates(states->states[depth][nErrors], states->nStates[depth][nErrors]);
if (states->minErrors[depth] == SIZE_MAX) {
states->minErrors[depth] = nErrors;
states->maxErrors[depth] = nErrors;
}
else {
if (nErrors < states->minErrors[depth]) {
states->minErrors[depth] = nErrors;
}
if (nErrors > states->maxErrors[depth]) {
states->maxErrors[depth] = nErrors;
}
}
//return &states->states[nErrors][states->nStatesPerError[nErrors]-1];
}
void printBacktrace (states_t *states, int depth, int nErrors, size_t stateId) {
printf("Backtrace:\n");
state_t *state;
while ((depth >= 0) || (nErrors > 0)) {
state = getState(states, depth, nErrors, stateId);
printf("\tdepth: %i, errors: %i\t", depth, nErrors);
printState(state, depth); fflush(stdout);
if (hasTrace(state, MATCH)) {
--depth;
}
else if (hasTrace(state, MISMATCH)) {
--depth;
--nErrors;
}
else if (hasTrace(state, INSERTION)) {
--depth;
assert(nErrors > 0);
--nErrors;
}
else if (hasTrace(state, DELETION)) {
--nErrors;
}
else {
assert(false);
}
stateId = state->previousState;
}
}
state_t *_addState (states_t *states, size_t depth, size_t nErrors, bwtinterval_t *interval, unsigned char trace, unsigned char nucleotide, unsigned int previousState) {
//printf("Inserting interval %" PRIu64 "-%" PRIu64 " at %zu, %zu (%c/%c), with %zu elements\n", interval->k, interval->l, depth, nErrors, CIGAR[trace], "ACGT"[nucleotide], states->nStates[depth][nErrors]); fflush(stdout);
assert((states->nStates[depth][nErrors] == 0) || ((interval->l == 0) == (states->states[nErrors][states->firstState[depth][nErrors]].interval.l == 0)));
state_t *state;
updateCounts(states, depth, nErrors, 1);
state = &states->states[nErrors][states->firstState[depth][nErrors] + states->nStates[depth][nErrors] - 1];
setState(state, interval, trace, nucleotide, previousState);
//printf("Inserted interval %" PRIu64 "-%" PRIu64 " at %zu, %zu (%c/%c), with %zu elements\n", interval->k, interval->l, depth, nErrors, CIGAR[trace], "ACGT"[nucleotide], states->nStates[depth][nErrors]); fflush(stdout);
//printStates(states, depth); fflush(stdout);
return state;
}
void simplifyStates (states_t *states, size_t depth, size_t nErrors) {
assert(depth < states->depth);
assert(nErrors <= states->maxErrors[depth]);
size_t previousNStates = states->nStates[depth][nErrors];
size_t nextNStates = 0;
state_t *theseStates = states->states[nErrors] + states->firstState[depth][nErrors];
if (previousNStates <= 1) {
return;
}
//TODO check that the pointers are good!
//printf("\t\t\t\tSimplify states from %zu ", previousNStates);
//printf("Entering Simplify States @ depth %zu with %zu errors and %zu elements.\n", depth, nErrors, previousNStates); fflush(stdout);
qsort(theseStates, previousNStates, sizeof(state_t), sortCompareStates);
for (size_t secondStateId = 1; secondStateId < previousNStates; ++secondStateId) {
//printf("\tCurrent state: %zu/%zu/%zu\n", nextNStates, secondStateId, previousNStates); fflush(stdout);
if (! areStatesEqual(&theseStates[nextNStates], &theseStates[secondStateId])) {
//if (! canMerge(&states[firstStateId], &states[secondStateId])) {
++nextNStates;
if (nextNStates < secondStateId) {
theseStates[nextNStates] = theseStates[secondStateId];
}
assert(nextNStates <= previousNStates);
assert(nextNStates <= secondStateId);
assert(secondStateId < N_STATES);
}
}
++nextNStates;
//printf("to %zu\n", nextNStates);
states->nStates[depth][nErrors] = nextNStates;
assert(states->nStatesPerPosition[depth] >= previousNStates - nextNStates);
states->nStatesPerPosition[depth] -= previousNStates - nextNStates;
}
state_t *addState (states_t *states, size_t depth, size_t nErrors, bwtinterval_t *interval, unsigned char trace, unsigned char nucleotide, unsigned int previousState) {
return _addState(states, depth, nErrors, interval, trace, nucleotide, previousState);
}
void clearStates (states_t *states) {
for (size_t depth = 0; depth < states->depth; ++depth) {
memset(states->firstState[depth], 0, (parameters->maxNErrors+1) * sizeof(size_t));
memset(states->nStates[depth], 0, (parameters->maxNErrors+1) * sizeof(size_t));
states->minErrors[depth] = SIZE_MAX;
states->maxErrors[depth] = SIZE_MAX;
}
bwtinterval_t interval = {0, bwt->seq_len};
addState(states, 0, 0, &interval, 0, 0, 0);
}
states_t *initializeStates(size_t treeSize) {
states_t *states = (states_t *) mallocOrDie(sizeof(states_t));
states->depth = treeSize + 1 + parameters->maxNErrors;
states->states = (state_t **) mallocOrDie((parameters->maxNErrors+1) * sizeof(states_t *));
states->firstState = (size_t **) mallocOrDie((states->depth+1) * sizeof(size_t *)); // Allocate one unit more for the backtrack.
states->nStates = (size_t **) mallocOrDie(states->depth * sizeof(size_t *));
states->allocatedNStates = (size_t *) mallocOrDie((parameters->maxNErrors+1) * sizeof(size_t));
states->nStatesPerPosition = (size_t *) callocOrDie(states->depth+1, sizeof(size_t)); // Allocate one unit more for the backtrack.
states->minErrors = (size_t *) mallocOrDie(states->depth * sizeof(size_t));
states->maxErrors = (size_t *) mallocOrDie(states->depth * sizeof(size_t));
for (size_t depth = 0; depth < states->depth; ++depth) {
states->firstState[depth] = (size_t *) mallocOrDie((parameters->maxNErrors+1) * sizeof(size_t));
states->nStates[depth] = (size_t *) mallocOrDie((parameters->maxNErrors+1) * sizeof(size_t));
}
states->firstState[states->depth] = (size_t *) callocOrDie(parameters->maxNErrors+1, sizeof(size_t));
states->allocatedNStates[0] = treeSize + 3;
states->states[0] = (state_t *) malloc(states->allocatedNStates[0] * sizeof(state_t));
if (states->states[0] == NULL) {
fprintf(stderr, "Error! Cannot allocate sufficient memory (%zu cells) for states with 0 errors.\nExiting.\n", states->allocatedNStates[0]);
exit(EXIT_FAILURE);
}
for (size_t nErrors = 1; nErrors <= parameters->maxNErrors; ++nErrors) {
states->allocatedNStates[nErrors] = N_STATES;
states->states[nErrors] = (state_t *) malloc(states->allocatedNStates[nErrors] * sizeof(state_t));
if (states->states[nErrors] == NULL) {
fprintf(stderr, "Error! Cannot allocate sufficient memory (%zu cells) for states with %zu errors.\nExiting.\n", states->allocatedNStates[nErrors], nErrors);
exit(EXIT_FAILURE);
}
}
clearStates(states);
//createBwtBuffer(&states->bwtBuffer);
return states;
}
void backtrackStates(states_t *states, size_t level) {
//printf("\t\t\tBacktracking to level %zu\n", level);
//printStates(states, level);
for (size_t i = level; i <= states->depth; ++i) {
//printf("\t\t\t\tnow %zu/%zu -> %zu\n", i, states->depth, states->nStatesPerPosition[i]); fflush(stdout);
if (states->nStatesPerPosition[i] == 0) {
//printf("\t\t\tFinally2\n");
//printStates(states, level);
return;
}
states->nStatesPerPosition[i] = 0;
states->minErrors[i] = SIZE_MAX;
states->maxErrors[i] = SIZE_MAX;
for (size_t j = 0; j <= parameters->maxNErrors; ++j) {
states->nStates[i][j] = 0;
states->firstState[i+1][j] = states->firstState[i][j];
}
}
//printf("\t\t\tFinally\n");
//printStates(states, level);
}
size_t computeBacktrace (states_t *states, int depth, int nErrors, size_t stateId, outputSam_t *outputSam) {
state_t *state;
char cigar;
int readSize = 0;
outputSam->backtraceSize = 0;
//printf("Compute BT\n");
//while ((depth >= 0) || (nErrors > 0)) {
while ((depth > 0) || (nErrors > 0)) {
state = getState(states, depth, nErrors, stateId);
//printState(state, 101); fflush(stdout);
cigar = CIGAR[state->trace];
if ((outputSam->backtraceSize == 0) || (outputSam->backtraceCigar[outputSam->backtraceSize-1] != cigar)) {
outputSam->backtraceCigar[outputSam->backtraceSize] = cigar;
outputSam->backtraceLengths[outputSam->backtraceSize] = 1;
++outputSam->backtraceSize;
}
else {
++outputSam->backtraceLengths[outputSam->backtraceSize-1];
}
if (hasTrace(state, MATCH)) {
--depth;
++readSize;
}
else if (hasTrace(state, MISMATCH)) {
--depth;
assert(nErrors > 0);
--nErrors;
++readSize;
}
else if (hasTrace(state, INSERTION)) {
--depth;
assert(nErrors > 0);
--nErrors;
}
else if (hasTrace(state, DELETION)) {
--nErrors;
++readSize;
}
else {
assert(false);
}
stateId = state->previousState;
}
return readSize;
}
void freeStates(states_t *states) {
for (size_t i = 0; i <= parameters->maxNErrors; ++i) {
free(states->states[i]);
}
for (size_t depth = 0; depth < states->depth; ++depth) {
free(states->firstState[depth]);
free(states->nStates[depth]);
}
free(states->states);
free(states->nStates);
free(states->allocatedNStates);
free(states->firstState);
free(states->nStatesPerPosition);
free(states->minErrors);
free(states->maxErrors);
free(states);
}
#endif