-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.cpp
495 lines (395 loc) · 17 KB
/
bootstrap.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
#include "bootstrap.h"
#include "io.h"
#include <algorithm>
bblocks_t* bblocks_init(void) {
bblocks_t* bblocks = (bblocks_t*)malloc(sizeof(bblocks_t));
ASSERT(bblocks != NULL);
bblocks->n_blocks = 0;
bblocks->n_contigs = 0;
bblocks->n_ind = 0;
bblocks->nsites_per_block = NULL;
bblocks->block_start_pos = NULL;
bblocks->block_end_pos = NULL;
bblocks->block_contig = NULL;
bblocks->block_start_siteidx = NULL;
bblocks->contig_names = NULL;
return(bblocks);
}
void bblocks_destroy(bblocks_t* bblocks) {
for (size_t i = 0;i < (size_t) args->nBootstraps;++i) {
FREE(bblocks->rblocks[i]);
}
FREE(bblocks->rblocks);
FREE(bblocks->nsites_per_block);
FREE(bblocks->block_start_pos);
FREE(bblocks->block_end_pos);
FREE(bblocks->block_contig);
FREE(bblocks->block_start_siteidx);
strArray_destroy(bblocks->contig_names);
FREE(bblocks);
return;
}
void bblocks_sample_with_replacement(bblocks_t* bblocks) {
DEVASSERT(bblocks != NULL);
const size_t nReps = args->nBootstraps;
const int nBlocks = bblocks->n_blocks;
for (size_t rep = 0;rep < (size_t) nReps;++rep) {
for (size_t block = 0;block < (size_t) nBlocks;++block) {
bblocks->rblocks[rep][block] = (int)(nBlocks * drand48());
}
}
return;
}
void bblocks_print_bootstrap_samples(bblocks_t* bblocks, outfile_t* outfile) {
kstring_t* kbuf = &outfile->kbuf;
LOG("Writing bootstrap samples to file: %s\n", outfile->fn);
ksprintf(kbuf, "Rep\tPos\tBlockID\tBlockContig\tBlockStart\tBlockEnd\n");
for (size_t rep = 0;rep < (size_t) args->nBootstraps;++rep) {
for (size_t block = 0;block < (size_t) bblocks->n_blocks;++block) {
ksprintf(kbuf, "%ld\t%ld\t%ld\t%s\t%ld\t%ld\n", rep, block, bblocks->rblocks[rep][block],bblocks->contig_names->d[bblocks->block_contig[bblocks->rblocks[rep][block]]],bblocks->block_start_pos[bblocks->rblocks[rep][block]]+1, bblocks->block_end_pos[bblocks->rblocks[rep][block]]);
}
}
return;
}
// blocks tsv file = 1-based, [start, end]
// internal representation = 0-based, [start, end)
// conversion from internal to blocks tab: start+1,end
void bblocks_print_blocks_tab(bblocks_t* bblocks, outfile_t* outfile) {
kstring_t* kbuf = &outfile->kbuf;
LOG("(%s) Writing the bootstrapping blocks to tsv file: %s", "--print-blocks", outfile->fn);
size_t ci;
for (size_t bi = 0;bi < bblocks->n_blocks;++bi) {
ci = bblocks->block_contig[bi];
ksprintf(kbuf, "%s\t%ld\t%ld\n", bblocks->contig_names->d[ci], bblocks->block_start_pos[bi] + 1, bblocks->block_end_pos[bi]);
}
return;
}
void bblocks_generate_blocks_with_size(bblocks_t* bblocks, vcfData* vcfd, paramStruct* pars, const uint64_t blockSize) {
BEGIN_LOGSECTION_MSG("(--block-size) Generating blocks with given block size");
LOG("(--block-size) Targeted block size: %ld", blockSize);
const size_t nInd = pars->names->len;
const size_t nContigs = vcfd->nContigs;
const int targeted_blockSize = args->blockSize;
int current_blockSize = targeted_blockSize;
bblocks->n_contigs = nContigs;
bblocks->contig_names = strArray_alloc(nContigs);
uint64_t prev_nBlocks;
uint64_t totnBlocks = 0;
for (size_t ci = 0;ci < nContigs;++ci) {
const uint64_t contigSize = vcfd->hdr->id[BCF_DT_CTG][ci].val->info[0];
const char* contigName = vcfd->hdr->id[BCF_DT_CTG][ci].key;
bblocks->contig_names->add(contigName);
LOG("Found contig %s with size %ld", contigName, contigSize);
if ((size_t) targeted_blockSize > contigSize) {
WARN("Size of the contig %s (%ld) is smaller than the targeted block size (%d). Setting the block size to the size of the contig.", contigName, contigSize, targeted_blockSize);
current_blockSize = contigSize;
} else {
current_blockSize = targeted_blockSize;
}
// +1 to account for the remainder (last block)
const int nBlocks_perContig = (contigSize % current_blockSize == 0) ? (contigSize / current_blockSize) : ((contigSize / current_blockSize) + 1);
DEVASSERT(nBlocks_perContig > 0);
LOG("Generating %d block%c of size %d for contig %s", nBlocks_perContig, (nBlocks_perContig > 1) ? 's' : '\0', current_blockSize, contigName);
prev_nBlocks = totnBlocks;
totnBlocks += nBlocks_perContig;
if (ci == 0) {
bblocks->block_start_pos = (size_t*)malloc(totnBlocks * sizeof(size_t));
ASSERT(bblocks->block_start_pos != NULL);
bblocks->block_end_pos = (size_t*)malloc(totnBlocks * sizeof(size_t));
ASSERT(bblocks->block_end_pos != NULL);
bblocks->block_contig = (size_t*)malloc(totnBlocks * sizeof(size_t));
ASSERT(bblocks->block_contig != NULL);
bblocks->block_start_siteidx = (size_t*)malloc(totnBlocks * sizeof(size_t));
ASSERT(bblocks->block_start_siteidx != NULL);
for (size_t i = 0;i < totnBlocks;++i) {
bblocks->block_start_pos[i] = 0;
bblocks->block_end_pos[i] = 0;
bblocks->block_contig[i] = 0;
bblocks->block_start_siteidx[i] = 0;
}
} else {
size_t* tmp = NULL;
tmp = (size_t*)realloc(bblocks->block_start_pos, totnBlocks * sizeof(size_t));
ASSERT(tmp != NULL);
bblocks->block_start_pos = tmp;
tmp = NULL;
tmp = (size_t*)realloc(bblocks->block_end_pos, totnBlocks * sizeof(size_t));
ASSERT(tmp != NULL);
bblocks->block_end_pos = tmp;
tmp = NULL;
tmp = (size_t*)realloc(bblocks->block_contig, totnBlocks * sizeof(size_t));
ASSERT(tmp != NULL);
bblocks->block_contig = tmp;
tmp = NULL;
tmp = (size_t*)realloc(bblocks->block_start_siteidx, totnBlocks * sizeof(size_t));
ASSERT(tmp != NULL);
bblocks->block_start_siteidx = tmp;
tmp = NULL;
for (size_t i = prev_nBlocks;i < totnBlocks;++i) {
bblocks->block_start_pos[i] = 0;
bblocks->block_end_pos[i] = 0;
bblocks->block_contig[i] = 0;
bblocks->block_start_siteidx[i] = 0;
}
}
// -> set
for (size_t bi = 0;bi < (size_t) nBlocks_perContig;++bi) {
bblocks->block_start_pos[bi + prev_nBlocks] = bi * current_blockSize;
bblocks->block_end_pos[bi + prev_nBlocks] = ((bi + 1) * current_blockSize);
bblocks->block_contig[bi + prev_nBlocks] = ci;
}
// <- set
}
if (totnBlocks == 1) {
ERROR("Cannot perform block bootstrapping with only one block. Please decrease the block size and try again.");
}
bblocks->n_blocks = totnBlocks;
bblocks->n_ind = nInd;
bblocks->rblocks = (size_t**)malloc(args->nBootstraps * sizeof(size_t*));
ASSERT(bblocks->rblocks != NULL);
for (size_t i = 0;i < (size_t) args->nBootstraps;++i) {
bblocks->rblocks[i] = (size_t*)malloc(totnBlocks * sizeof(size_t));
ASSERT(bblocks->rblocks[i] != NULL);
for (size_t j = 0;j < (size_t) totnBlocks;++j) {
bblocks->rblocks[i][j] = 0;
}
}
bblocks->nsites_per_block = (uint64_t*)malloc(totnBlocks * sizeof(uint64_t));
ASSERT(bblocks->nsites_per_block != NULL);
for (size_t i = 0;i < totnBlocks;++i) {
bblocks->nsites_per_block[i] = 0;
}
END_LOGSECTION_MSG("(--block-size) Generating blocks with given block size");
return;
}
// blocks tab file = 1-based, [start, end]
// internal representation = 0-based, [start, end)
void bblocks_read_tab(bblocks_t* bblocks, const char* fn, vcfData* vcfd, paramStruct* pars) {
LOG("(in-blocks-tab) Reading blocks tab file: %s", fn);
FILE* fp = IO::getFile(fn, "r");
char* firstLine = IO::readFile::getFirstLine(fp);
int nCols = IO::inspectFile::count_nCols(firstLine, "\t");
if (nCols != 3) {
ERROR("Blocks tab file must have 3 columns. Found %d columns.", nCols);
}
FREE(firstLine);
ASSERT(fseek(fp, 0, SEEK_SET) == 0);
char* tok = NULL;
char chr[256];
int64_t start;
int64_t end;
size_t nContigsFound = 0;
char lastChr[256];
bool contigChanged = false;
int nContigsVcf;
const char** vcfContigs = bcf_hdr_seqnames(vcfd->hdr, &nContigsVcf);
DEVASSERT(nContigsVcf == vcfd->nContigs);
size_t tmp_nBlocks = 512;
bblocks->block_start_pos = (size_t*)malloc(tmp_nBlocks * sizeof(size_t));
ASSERT(bblocks->block_start_pos != NULL);
bblocks->block_end_pos = (size_t*)malloc(tmp_nBlocks * sizeof(size_t));
ASSERT(bblocks->block_end_pos != NULL);
for (size_t i = 0;i < tmp_nBlocks;++i) {
bblocks->block_start_pos[i] = 0;
bblocks->block_end_pos[i] = 0;
}
bblocks->contig_names = strArray_init();
size_t nBlocks = 0;
while (EOF != fscanf(fp, "%s\t%ld\t%ld", chr, &start, &end)) {
// tab file positions are 1-based, but bblocks_t positions are 0-based
// so use -1 to make it 0-based
// both tab file start and bblocks_t start are inclusive
if (start <= 0) {
ERROR("Start position must be greater than 0. Note: Tab files have 1-based indexing with [start:inclusive, end:inclusive].");
}
bblocks->block_start_pos[nBlocks] = start - 1;
// tab file end is inclusive, but bblocks_t end is exclusive
// +1 to make it exclusive
// -1+1 = 0 // so no change
if (end <= 0) {
ERROR("End position must be greater than 0. Note: Tab files have 1-based indexing with [start:inclusive, end:inclusive].");
}
bblocks->block_end_pos[nBlocks] = end;
if (end <= start) {
ERROR("End position must be greater than start position. Found start: %ld, end: %ld at line %ld", start, end, nBlocks);
}
// DEVPRINT("Found block %ld with size %ld at %s:%ld-%ld", nBlocks, end - start+1, chr, start, end);
if (nContigsFound == 0) {
// first loop
strcpy(lastChr, chr);
++nContigsFound;
contigChanged = true;
} else {
if (strcmp(lastChr, chr) == 0) {
contigChanged = false;
} else {
contigChanged = true;
strcpy(lastChr, chr);
++nContigsFound;
}
}
// if found new contig:
// -> check if the contig in input blocks file exists in the VCF file
if (contigChanged) {
size_t contig_i;
for (contig_i = 0; contig_i < (size_t) nContigsVcf; ++contig_i) {
if (strcmp(chr, vcfContigs[contig_i]) == 0) {
break;
}
if ((int)contig_i == nContigsVcf - 1) {
ERROR("Contig %s in input blocks file does not exist in the VCF file.", chr);
}
}
bblocks->contig_names->add(chr);
size_t* tmp = (size_t*)realloc(bblocks->block_contig, (nBlocks + 1) * sizeof(size_t));
ASSERT(tmp != NULL);
bblocks->block_contig = tmp;
bblocks->block_contig[nBlocks] = contig_i;
}
++nBlocks;
if (tmp_nBlocks == nBlocks) {
tmp_nBlocks += 256;
bblocks->block_start_pos = (size_t*)realloc(bblocks->block_start_pos, tmp_nBlocks * sizeof(size_t));
ASSERT(bblocks->block_start_pos != NULL);
bblocks->block_end_pos = (size_t*)realloc(bblocks->block_end_pos, tmp_nBlocks * sizeof(size_t));
ASSERT(bblocks->block_end_pos != NULL);
for (size_t i = nBlocks;i < tmp_nBlocks;++i) {
bblocks->block_start_pos[i] = 0;
bblocks->block_end_pos[i] = 0;
}
}
}
bblocks->n_blocks = nBlocks;
bblocks->n_contigs = nContigsFound;
const size_t nInd = pars->names->len;
bblocks->n_ind = nInd;
bblocks->nsites_per_block = (uint64_t*)malloc(nBlocks * sizeof(uint64_t));
ASSERT(bblocks->nsites_per_block != NULL);
for (size_t i = 0;i < nBlocks;++i) {
bblocks->nsites_per_block[i] = 0;
}
FREE(tok);
FCLOSE(fp);
return;
}
void bblocks_read_bed(bblocks_t* bblocks, const char* fn, vcfData* vcfd, paramStruct* pars) {
LOG("(in-blocks-bed) Reading blocks bed file: %s", fn);
FILE* fp = IO::getFile(fn, "r");
char* firstLine = IO::readFile::getFirstLine(fp);
int nCols = IO::inspectFile::count_nCols(firstLine, "\t");
if (nCols != 3) {
ERROR("Blocks tab file must have 3 columns. Found %d columns.", nCols);
}
FREE(firstLine);
ASSERT(fseek(fp, 0, SEEK_SET) == 0);
char* tok = NULL;
char chr[256];
int64_t start;
int64_t end;
size_t nContigsFound = 0;
char lastChr[256];
bool contigChanged = false;
int nContigsVcf;
const char** vcfContigs = bcf_hdr_seqnames(vcfd->hdr, &nContigsVcf);
DEVASSERT(nContigsVcf == vcfd->nContigs);
size_t tmp_nBlocks = 256;
bblocks->block_start_pos = (size_t*)malloc(tmp_nBlocks * sizeof(size_t));
ASSERT(bblocks->block_start_pos != NULL);
bblocks->block_end_pos = (size_t*)malloc(tmp_nBlocks * sizeof(size_t));
ASSERT(bblocks->block_end_pos != NULL);
for (size_t i = 0;i < tmp_nBlocks;++i) {
bblocks->block_start_pos[i] = 0;
bblocks->block_end_pos[i] = 0;
}
bblocks->block_contig = (size_t*)malloc(sizeof(size_t));
ASSERT(bblocks->block_contig != NULL);
bblocks->contig_names = strArray_init();
size_t nBlocks = 0;
while (EOF != fscanf(fp, "%s\t%ld\t%ld", chr, &start, &end)) {
// both bed file and bblocks_t positions are 0-based
// both bed file start and bblocks_t start are inclusive
// both bed file end and bblocks_t end are exclusive
// so no change
bblocks->block_start_pos[nBlocks] = start;
if (end <= 0) {
ERROR("End position must be greater than 0. Note: Tab files have 1-based indexing with [start:inclusive, end:inclusive].");
}
bblocks->block_end_pos[nBlocks] = end;
if (end <= start) {
ERROR("End position must be greater than start position. Found start: %ld, end: %ld at line %ld", start, end, nBlocks);
}
// DEVPRINT("Found block %ld with size %ld at %s:%ld-%ld", nBlocks, end - start, chr, start, end);
if (nContigsFound == 0) {
// first loop
strcpy(lastChr, chr);
++nContigsFound;
contigChanged = true;
} else {
if (strcmp(lastChr, chr) == 0) {
contigChanged = false;
} else {
contigChanged = true;
strcpy(lastChr, chr);
++nContigsFound;
}
}
// if found new contig:
// -> check if the contig in input blocks file exists in the VCF file
if (contigChanged) {
size_t contig_i;
for (contig_i = 0; contig_i < (size_t) nContigsVcf; ++contig_i) {
if (strcmp(chr, vcfContigs[contig_i]) == 0) {
break;
}
if ((int)contig_i == nContigsVcf - 1) {
ERROR("Contig %s in input blocks file does not exist in the VCF file.", chr);
}
}
bblocks->contig_names->add(chr);
size_t* tmp = (size_t*)realloc(bblocks->block_contig, (nBlocks + 1) * sizeof(size_t));
ASSERT(tmp != NULL);
bblocks->block_contig = tmp;
bblocks->block_contig[nBlocks] = contig_i;
}
++nBlocks;
if (tmp_nBlocks == nBlocks) {
tmp_nBlocks *= 2;
bblocks->block_start_pos = (size_t*)realloc(bblocks->block_start_pos, tmp_nBlocks * sizeof(size_t));
ASSERT(bblocks->block_start_pos != NULL);
bblocks->block_end_pos = (size_t*)realloc(bblocks->block_end_pos, tmp_nBlocks * sizeof(size_t));
ASSERT(bblocks->block_end_pos != NULL);
for (size_t i = nBlocks;i < tmp_nBlocks;++i) {
bblocks->block_start_pos[i] = 0;
bblocks->block_end_pos[i] = 0;
}
}
}
bblocks->n_blocks = nBlocks;
bblocks->n_contigs = nContigsFound;
const size_t nInd = pars->names->len;
bblocks->n_ind = nInd;
bblocks->nsites_per_block = (uint64_t*)malloc(nBlocks * sizeof(uint64_t));
ASSERT(bblocks->nsites_per_block != NULL);
for (size_t i = 0;i < nBlocks;++i) {
bblocks->nsites_per_block[i] = 0;
}
FREE(tok);
FCLOSE(fp);
return;
}
void bblocks_get(bblocks_t* bblocks, vcfData* vcfd, paramStruct* pars) {
if (PROGRAM_HAS_INPUT_BLOCKS) {
if (args->in_blocks_bed_fn != NULL) {
bblocks_read_bed(bblocks, args->in_blocks_bed_fn, vcfd, pars);
} else if (args->in_blocks_tab_fn != NULL) {
bblocks_read_tab(bblocks, args->in_blocks_tab_fn, vcfd, pars);
} else {
NEVER;
}
} else if (args->blockSize > 0) {
bblocks_generate_blocks_with_size(bblocks, vcfd, pars, args->blockSize);
} else {
NEVER;
}
return;
}