-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfa_parser.c
More file actions
595 lines (470 loc) · 17.8 KB
/
fa_parser.c
File metadata and controls
595 lines (470 loc) · 17.8 KB
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
#include "fa_parser.h"
static inline uint64_t MurmurHash3_32(const void *key, int len) {
const uint8_t *data = (const uint8_t *)key;
const int nblocks = len / 4;
uint32_t h1 = 42;
const uint32_t c1 = 0xcc9e2d51;
const uint32_t c2 = 0x1b873593;
// Body: Process blocks of 4 bytes at a time
const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
for (int i = -nblocks; i; i++) {
uint32_t k1 = blocks[i];
k1 *= c1;
k1 = (k1 << 15) | (k1 >> (32 - 15));
k1 *= c2;
h1 ^= k1;
h1 = (h1 << 15) | (h1 >> (32 - 15));
h1 = h1 * 5 + 0xe6546b64;
}
// Tail: Process remaining bytes
const uint8_t *tail = (const uint8_t *)(data + nblocks * 4);
uint32_t k1 = 0;
switch (len & 3) {
case 3:
k1 ^= tail[2] << 16;
break;
case 2:
k1 ^= tail[1] << 8;
break;
case 1:
k1 ^= tail[0];
k1 *= c1;
k1 = (k1 << 15) | (k1 >> (32 - 15));
k1 *= c2;
h1 ^= k1;
}
// Finalization: Mix the hash to ensure the last few bits are fully mixed
h1 ^= len;
/* fmix32 */
h1 ^= h1 >> 16;
h1 *= 0x85ebca6b;
h1 ^= h1 >> 13;
h1 *= 0xc2b2ae35;
h1 ^= h1 >> 16;
return (uint64_t)h1;
}
void free_ref_seq(struct ref_seq *seqs) {
if (seqs->size) {
for (int i=0; i<seqs->size; i++) {
free(seqs->chrs[i].seq_name);
free(seqs->chrs[i].seq);
if (seqs->chrs[i].cores_size) {
free(seqs->chrs[i].cores);
}
if (seqs->chrs[i].ids) {
for (int j=0; j<seqs->chrs[i].cores_size; j++) {
if (seqs->chrs[i].ids[j] != NULL) free(seqs->chrs[i].ids[j]);
}
free(seqs->chrs[i].ids);
}
}
free(seqs->chrs);
seqs->size = 0;
}
}
void vgx_process_chrom(char *sequence, uint64_t seq_size, int lcp_level, int skip_masked, struct chr *chrom, uint64_t *core_id_index, int thread_number) {
uint64_t id = *core_id_index;
uint64_t estimated_core_size = (uint64_t)(seq_size / pow(1.5, lcp_level));
uint64_t estimated_core_length = (uint64_t)(3 * pow(2, lcp_level-1));
chrom->cores_size = 0;
if (estimated_core_size == 0) {
chrom->cores = NULL;
return;
}
chrom->cores = (struct simple_core*)malloc(estimated_core_size * sizeof(struct simple_core));
uint64_t index = 0;
uint64_t last_core_index = 0;
int valid_chars[256] = {0};
valid_chars['A'] = valid_chars['C'] = valid_chars['T'] = valid_chars['G'] = 1;
valid_chars['a'] = valid_chars['c'] = valid_chars['t'] = valid_chars['g'] = 1;
while (index < seq_size) {
{
uint64_t temp_index = index;
while (index < seq_size && !valid_chars[(unsigned char)sequence[index]]) {
index++;
}
if (!skip_masked) {
if (temp_index != index) {
uint64_t i = temp_index;
while (i+estimated_core_length < index) {
chrom->cores[last_core_index].id = id;
chrom->cores[last_core_index].start = i;
chrom->cores[last_core_index].end = i+estimated_core_length;
id++;
last_core_index++;
i += estimated_core_length;
}
chrom->cores[last_core_index].id = id;
chrom->cores[last_core_index].start = i;
chrom->cores[last_core_index].end = index;
id++;
last_core_index++;
}
}
}
if (index == seq_size)
break;
uint64_t end = index;
while (end < seq_size && valid_chars[(unsigned char)sequence[end]]) {
end++;
}
struct lps str;
init_lps_offset(&str, sequence+index, end-index, index);
if (thread_number == 1) {
lps_deepen(&str, lcp_level);
} else {
lps_deepen_parallel(&str, lcp_level, thread_number);
}
if (str.size) {
if (str.cores[0].start != index) {
chrom->cores[last_core_index].id = id;
chrom->cores[last_core_index].start = index;
chrom->cores[last_core_index].end = str.cores[0].start;
id++;
last_core_index++;
}
for (int i=0; i<str.size; i++) {
chrom->cores[last_core_index].id = id;
chrom->cores[last_core_index].start = str.cores[i].start;
chrom->cores[last_core_index].end = str.cores[i].end;
id++;
last_core_index++;
}
if (str.cores[str.size-1].end != end) {
chrom->cores[last_core_index].id = id;
chrom->cores[last_core_index].start = str.cores[str.size-1].end;
chrom->cores[last_core_index].end = end;
id++;
last_core_index++;
}
} else {
uint64_t i = index;
while (i+estimated_core_length < end) {
chrom->cores[last_core_index].id = id;
chrom->cores[last_core_index].start = i;
chrom->cores[last_core_index].end = i+estimated_core_length;
id++;
last_core_index++;
i += estimated_core_length;
}
chrom->cores[last_core_index].id = id;
chrom->cores[last_core_index].start = i;
chrom->cores[last_core_index].end = end;
id++;
last_core_index++;
}
index = end;
free_lps(&str);
}
chrom->cores_size = last_core_index;
if (chrom->cores_size) {
struct simple_core *temp = (struct simple_core*)realloc(chrom->cores, chrom->cores_size * sizeof(struct simple_core));
if (temp != NULL) {
chrom->cores = temp;
}
} else {
free(chrom->cores);
chrom->cores = NULL;
}
*core_id_index = id;
}
void ldbg_process_chrom(char *sequence, uint64_t seq_size, int lcp_level, struct chr *chrom) {
uint64_t estimated_core_size = (int)(seq_size / pow(1.5, lcp_level));
chrom->cores_size = 0;
if (estimated_core_size == 0) {
chrom->cores = NULL;
return;
}
chrom->cores = (struct simple_core*)malloc(estimated_core_size * sizeof(struct simple_core));
uint64_t index = 0;
uint64_t last_core_index = 0;
while (index < seq_size) {
while (index < seq_size && sequence[index] == 'N') {
index++;
}
uint64_t end = index;
while (end < seq_size && sequence[end] != 'N') {
end++;
}
struct lps str;
init_lps_offset(&str, sequence+index, end-index, index);
lps_deepen(&str, lcp_level);
for (int i=0; i<str.size; i++) {
chrom->cores[last_core_index].id = (MurmurHash3_32(sequence + str.cores[i].start, (int)(str.cores[i].end-str.cores[i].start)) << 32) | str.cores[i].label;
chrom->cores[last_core_index].start = str.cores[i].start;
chrom->cores[last_core_index].end = str.cores[i].end;
last_core_index++;
}
chrom->cores_size = last_core_index;
index = end;
free_lps(&str);
}
struct simple_core *temp = (struct simple_core*)realloc(chrom->cores, chrom->cores_size * sizeof(struct simple_core));
if (temp != NULL) {
chrom->cores = temp;
}
}
typedef struct {
char *seq;
uint64_t seq_size;
uint64_t core_id_index;
int index;
int busy;
} chrom_task_t;
pthread_t *workers;
chrom_task_t *tasks;
pthread_mutex_t task_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t task_cond = PTHREAD_COND_INITIALIZER;
int terminate_workers = 0;
int worker_count;
struct opt_arg *g_args;
struct ref_seq *g_seqs;
void *worker_func(void *arg) {
int tid = *(int*)arg;
while (1) {
pthread_mutex_lock(&task_mutex);
while (!tasks[tid].busy && !terminate_workers)
pthread_cond_wait(&task_cond, &task_mutex);
if (terminate_workers) {
pthread_mutex_unlock(&task_mutex);
break;
}
chrom_task_t task = tasks[tid];
pthread_mutex_unlock(&task_mutex);
/* process chromosome */
if (g_args->program == VG || g_args->program == VGX) {
vgx_process_chrom(task.seq, task.seq_size, g_args->lcp_level, g_args->skip_masked, &(g_seqs->chrs[task.index]), &(task.core_id_index), 1);
}
else if (g_args->program == LDBG) {
ldbg_process_chrom(task.seq, task.seq_size, g_args->lcp_level, &(g_seqs->chrs[task.index]));
}
pthread_mutex_lock(&task_mutex);
tasks[tid].busy = 0;
pthread_cond_signal(&task_cond);
pthread_mutex_unlock(&task_mutex);
}
return NULL;
}
void submit_chromosome(char *seq, uint64_t seq_size, int index, uint64_t core_id_index) {
pthread_mutex_lock(&task_mutex);
int slot = -1;
while (slot == -1) {
for (int i = 0; i < worker_count; i++) {
if (!tasks[i].busy) {
slot = i;
break;
}
}
if (slot == -1)
pthread_cond_wait(&task_cond, &task_mutex);
}
tasks[slot].seq = seq;
tasks[slot].seq_size = seq_size;
tasks[slot].core_id_index = core_id_index;
tasks[slot].index = index;
tasks[slot].busy = 1;
pthread_cond_broadcast(&task_cond);
pthread_mutex_unlock(&task_mutex);
}
void process_fasta_parallel(struct opt_arg *args, struct ref_seq *seqs) {
g_args = args;
g_seqs = seqs;
worker_count = args->thread_number;
workers = malloc(sizeof(pthread_t) * worker_count);
tasks = calloc(worker_count, sizeof(chrom_task_t));
int *tids = malloc(sizeof(int) * worker_count);
for (int i = 0; i < worker_count; i++) {
tasks[i].busy = 0;
tids[i] = i;
pthread_create(&workers[i], NULL, worker_func, &tids[i]);
}
int line_size = 1024;
char line[line_size];
// move to read reference file
FILE *ref = fopen(args->fasta_path, "r");
if (ref == NULL) {
fprintf(stderr, "REF: Couldn't open file %s\n", args->fasta_path);
exit(EXIT_FAILURE);
}
// make necesarry declaretions and initialization
uint64_t sequence_size = 0;
int index = 0;
uint64_t core_id_index = 0;
// process reference file
while (fgets(line, line_size, ref)) {
line[strcspn(line, "\n")] = '\0';
if (line[0] == '>') {
if (sequence_size != 0) {
submit_chromosome(seqs->chrs[index].seq, sequence_size, index, core_id_index);
core_id_index += sequence_size / pow(1.5, args->lcp_level);
sequence_size = 0;
index++;
}
seqs->chrs[index].seq = (char *)malloc(seqs->chrs[index].seq_size + 1);
if (seqs->chrs[index].seq == NULL) {
fprintf(stderr, "REF: Couldn't allocate memory to chromosome string.\n");
exit(EXIT_FAILURE);
}
seqs->chrs[index].seq[seqs->chrs[index].seq_size] = '\0';
} else {
uint64_t line_len = strlen(line);
memcpy(seqs->chrs[index].seq + sequence_size, line, line_len);
sequence_size += line_len;
}
}
if (sequence_size != 0) {
submit_chromosome(seqs->chrs[index].seq, sequence_size, index, core_id_index);
core_id_index += sequence_size / pow(1.5, args->lcp_level);
index++;
}
fclose(ref);
pthread_mutex_lock(&task_mutex);
terminate_workers = 1;
pthread_cond_broadcast(&task_cond);
pthread_mutex_unlock(&task_mutex);
for (int i = 0; i < worker_count; i++)
pthread_join(workers[i], NULL);
free(workers);
free(tasks);
free(tids);
args->core_id_index = core_id_index;
}
void process_fasta(struct opt_arg *args, struct ref_seq *seqs) {
int line_size = 1024;
char line[line_size];
// move to read reference file
FILE *ref = fopen(args->fasta_path, "r");
if (ref == NULL) {
fprintf(stderr, "REF: Couldn't open file %s\n", args->fasta_path);
exit(EXIT_FAILURE);
}
// make necesarry declaretions and initialization
uint64_t sequence_size = 0;
int index = 0;
// process reference file
while (fgets(line, line_size, ref)) {
line[strcspn(line, "\n")] = '\0';
if (line[0] == '>') {
if (sequence_size != 0) {
if (args->program == VG || args->program == VGX) {
vgx_process_chrom(seqs->chrs[index].seq, sequence_size, args->lcp_level, args->skip_masked, &(seqs->chrs[index]), &(args->core_id_index), args->thread_number);
} else if (args->program == LDBG) {
ldbg_process_chrom(seqs->chrs[index].seq, sequence_size, args->lcp_level, &(seqs->chrs[index]));
}
sequence_size = 0;
index++;
}
seqs->chrs[index].seq = (char *)malloc(seqs->chrs[index].seq_size+1);
if (seqs->chrs[index].seq == NULL) {
fprintf(stderr, "REF: Couldn't allocate memory to chromosome string.\n");
exit(EXIT_FAILURE);
}
seqs->chrs[index].seq[seqs->chrs[index].seq_size] = '\0';
} else {
uint64_t line_len = strlen(line);
memcpy(seqs->chrs[index].seq + sequence_size, line, line_len);
sequence_size += line_len;
}
}
if (sequence_size != 0) {
if (args->program == VG || args->program == VGX) {
vgx_process_chrom(seqs->chrs[index].seq, sequence_size, args->lcp_level, args->skip_masked, &(seqs->chrs[index]), &(args->core_id_index), args->thread_number);
} else if (args->program == LDBG) {
ldbg_process_chrom(seqs->chrs[index].seq, sequence_size, args->lcp_level, &(seqs->chrs[index]));
}
index++;
}
fclose(ref);
}
void read_fasta(struct opt_arg *args, struct ref_seq *seqs) {
printf("[INFO] Processing reference...\n");
// read index (fai) file to get chromosome count and allocate space for chromosomes for later processing
int line_size = 1024;
char line[line_size];
FILE *idx = fopen(args->fasta_fai_path, "r");
if (idx == NULL) {
fprintf(stderr, "REF: Couldn't open file %s\n", args->fasta_fai_path);
exit(EXIT_FAILURE);
}
int chrom_index = 0;
while (fgets(line, sizeof(line), idx) != NULL) {
chrom_index++;
}
if (chrom_index == 0) {
fprintf(stderr, "REF: Index file is empty.\n");
exit(EXIT_FAILURE);
}
seqs->size = chrom_index;
seqs->chrs = (struct chr *)malloc(chrom_index*sizeof(struct chr));
if (seqs->chrs == NULL) {
fprintf(stderr, "REF: Couldn't allocate memory to ref sequences\n");
exit(EXIT_FAILURE);
}
rewind(idx);
chrom_index = 0;
uint64_t global_index = 0;
time_t main_start;
time(&main_start);
while (fgets(line, sizeof(line), idx) != NULL) {
char *name, *length;
// assign name
char *saveptr;
name = strtok_r(line, "\t", &saveptr);
seqs->chrs[chrom_index].global_index = global_index;
uint64_t name_len = strlen(name);
seqs->chrs[chrom_index].seq_name = (char *)malloc(name_len+1);
memcpy(seqs->chrs[chrom_index].seq_name, name, name_len);
seqs->chrs[chrom_index].seq_name[name_len] = '\0';
// assign size and allocate in memory
length = strtok_r(NULL, "\t", &saveptr);
seqs->chrs[chrom_index].seq_size = strtol(length, NULL, 10);
global_index += seqs->chrs[chrom_index].seq_size;
seqs->chrs[chrom_index].ids = NULL;
chrom_index++;
}
fclose(idx);
if (args->full_parallel) {
process_fasta_parallel(args, seqs);
} else {
process_fasta(args, seqs);
}
time_t main_end;
time(&main_end);
printf("[INFO] Reference processing completed in %0.2f sec.\n", difftime(main_end, main_start));
}
void print_ref_seqs(const struct ref_seq *seqs, int is_rgfa, FILE *out) {
printf("[INFO] Printing reference...\n");
fprintf(out, "H\tVN:Z:1.1\n");
// iterate through each chromosome
for (int i=0; i<seqs->size; i++) {
if (seqs->chrs[i].cores_size) {
const char *seq_name = seqs->chrs[i].seq_name;
const char *seq = seqs->chrs[i].seq;
{
const struct simple_core *curr_core = &(seqs->chrs[i].cores[0]);
uint64_t start = curr_core->start;
print_seq(curr_core->id, seq+start, (int)(curr_core->end - start), seq_name, start, 0, is_rgfa, out);
}
for (int j=1; j<seqs->chrs[i].cores_size; j++) {
const struct simple_core *curr_core = &(seqs->chrs[i].cores[j]);
const struct simple_core *prev_core = &(seqs->chrs[i].cores[j-1]);
uint64_t curr_start = curr_core->start;
int seq_len = (int)(curr_core->end - curr_start);
int overlap = (int)(prev_core->end - curr_start);
// there might be graps ('N') in genome, hence
if (overlap < 0) {
overlap = 0;
}
print_seq(curr_core->id, seq+curr_start, seq_len, seq_name, curr_start, 0, is_rgfa, out);
print_link(prev_core->id, '+', curr_core->id, '+', overlap, out);
}
// Print Path (P)
fprintf(out, "P\t%s\t", seq_name);
fprintf(out, "%lu+", seqs->chrs[i].cores[0].id);
for (int j=1; j<seqs->chrs[i].cores_size; j++) {
fprintf(out, ",%lu+", seqs->chrs[i].cores[j].id);
}
fprintf(out, "\t*\n");
}
}
}