-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.h
398 lines (337 loc) · 10.3 KB
/
query.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
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
#ifndef PLS_CHAIN_QUERY_H
#define PLS_CHAIN_QUERY_H
#include "kseq.h"
#include "khash.h"
#include "kmer.h"
#include "dtype.h"
#include "tree.h"
#include "file_io.h"
#include <zlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
KHASH_MAP_INIT_INT64(1, tuple_t)
int read_cfg_file(char * db_dir);
char *** read_idx_file(char * db_dir, int * num_comp, int ** sizes);
void free_idx_arrs(char *** idx_arrs, int * sizes, int num_comp);
int read_udb_file(char * db_dir, size_t * k, khash_t(1) * ukmer_table);
layer_t * proc_query(kseq_t * seq, size_t k, int num_comp, khash_t(1) * ukmer_table, char *** idx_arrs, int * status);
int query_file(char * db_dir, char * out_dir, char * qry_file) {
size_t k;
// seq parsing
gzFile fp;
kseq_t *seq;
int l;
// hash table
khash_t(1) *ukmer_table;
// idx array
char *** idx_arrs;
int * sizes;
int num_comp;
// cfg parameters
int plasmid_length = read_cfg_file(db_dir);
if (plasmid_length < 0){
return 1;
}
int length_upper = 3*plasmid_length;
printf("Read Length Upper Bound: %d\n", length_upper);
// output
FILE *fd_qry_total;
int status;
if ((fd_qry_total = open_file(out_dir, "qry_total.csv", "wb")) == NULL) {
return 1;
}
if ((idx_arrs = read_idx_file(db_dir, &num_comp, &sizes)) == NULL) {
fclose(fd_qry_total);
return 1;
}
ukmer_table = kh_init(1);
// error handling
if ((status = read_udb_file(db_dir, &k, ukmer_table)) == 1){
fclose(fd_qry_total);
kh_destroy(1, ukmer_table);
free_idx_arrs(idx_arrs, sizes, num_comp);
return 1;
}
// process query
if ((fp = gzopen(qry_file, "r")) == Z_NULL) {
fclose(fd_qry_total);
fprintf(stderr, "Not able to open the query file %s\n", qry_file);
kh_destroy(1, ukmer_table);
free_idx_arrs(idx_arrs, sizes, num_comp);
return 1;
}
seq = kseq_init(fp);
layer_t * res = NULL;
int alpha, beta;
int rcount = 0, rclassified = 0, runclassified = 0, rcontamination = 0;
while ((l = kseq_read(seq)) >= 0) {
rcount ++;
fprintf(fd_qry_total, "%s", seq->name.s);
// pre-filter by read length
if (seq->seq.l > length_upper) {
rcontamination ++;
fprintf(fd_qry_total, ",contamination\n");
continue;
}
res = proc_query(seq, k, num_comp, ukmer_table, idx_arrs, &status);
if (status != 1) {
runclassified ++;
fprintf(fd_qry_total, ",fail\n");
} else {
rclassified ++;
for (int i = 0; i < res->l; i ++){
alpha = (res->tetras)[i].alpha;
beta = (res->tetras)[i].beta;
if (alpha == -1 || beta == -1) {
fprintf(fd_qry_total, ",*");
} else {
fprintf(fd_qry_total, ",%s", idx_arrs[alpha][beta]);
}
}
fprintf(fd_qry_total, "\n");
free(res->tetras);
free(res);
}
}
// clean up
fclose(fd_qry_total);
kh_destroy(1, ukmer_table);
free_idx_arrs(idx_arrs, sizes, num_comp);
kseq_destroy(seq);
gzclose(fp);
printf("Query File: %s\n", qry_file);
printf("Processed: %d reads\n", rcount);
printf("[%.2f%%] Classified: %d\n", (float) 100 * rclassified / rcount, rclassified);
printf("[%.2f%%] Unclassified: %d\n", (float) 100 * runclassified / rcount, runclassified);
printf("[%.2f%%] Contamination: %d\n", (float) 100 * rcontamination / rcount, rcontamination);
return 0;
}
void free_idx_arrs(char *** idx_arrs, int * sizes, int num_comp){
int size, i, j;
for (i = 0; i < num_comp; i ++){
size = sizes[i];
for (j = 0; j < size; j ++){
free(idx_arrs[i][j]);
idx_arrs[i][j] = NULL;
}
free(idx_arrs[i]);
idx_arrs[i] = NULL;
}
free(idx_arrs);
free(sizes);
}
int read_cfg_file(char * db_dir){
FILE *fd_cfg;
int ret;
char *line = NULL;
size_t len = 0;
ssize_t read;
if ((fd_cfg = open_file(db_dir, "comps_cfg.txt", "rb")) == NULL) {
return -1;
}
read = getline(&line, &len, fd_cfg); // get first line
line[read - 1] = '\0';
ret = atoi(line);
free(line);
fclose(fd_cfg);
return ret;
}
char *** read_idx_file(char * db_dir, int * num_comp, int ** sizes){
int i, j, s;
char *** idx_arrs = NULL;
FILE *fd_idx;
char *line = NULL;
size_t len = 0;
ssize_t read;
if ((fd_idx = open_file(db_dir, "comps_idx.txt", "rb")) == NULL) {
return NULL;
}
i = 0;
read = getline(&line, &len, fd_idx); // get first line
line[read - 1] = '\0';
*num_comp = atoi(line);
*sizes = (int *) malloc(sizeof(int) * *num_comp);
idx_arrs = (char ***) calloc(*num_comp, sizeof(char **));
s = ARR_SIZE;
idx_arrs[i] = (char **) calloc(s, sizeof(char *));
(*sizes)[i] = 0;
j = 0;
while ((read = getline(&line, &len, fd_idx)) != -1) {
if (read == 1){ //read a \n
if (i < *num_comp - 1){
i ++;
s = ARR_SIZE;
idx_arrs[i] = (char **) calloc(s, sizeof(char *));
(*sizes)[i] = 0;
j=0;
}
continue;
}
if (j == s) {
s += STEP_SIZE;
idx_arrs[i] = (char **) realloc(idx_arrs[i], s * sizeof(char *));
}
idx_arrs[i][j] = (char *) malloc(read * sizeof(char));
memcpy(idx_arrs[i][j], line, (read - 1) * sizeof(char));
idx_arrs[i][j][read - 1] = '\0';
(*sizes)[i] ++;
j++;
}
free(line);
fclose(fd_idx);
return idx_arrs;
}
int read_udb_file(char * db_dir, size_t * k, khash_t(1) * ukmer_table){
FILE *fd_udb;
char *line = NULL;
size_t len = 0;
ssize_t read;
khint64_t mask;
khiter_t kh;
int absent;
char *token;
if ((fd_udb = open_file(db_dir, "comps_udb.txt", "rb")) == NULL) {
return 1;
}
read = getline(&line, &len, fd_udb); // get first line
line[read - 1] = '\0';
*k = atoi(line);
mask = get_mask(*k);
while ((read = getline(&line, &len, fd_udb)) != -1) {
line[read - 1] = '\0';
token = strtok(line, "\t");
khint64_t key = (khint64_t) strtoul(token, NULL, 10);
token = strtok(NULL, "\t");
tuple_t value;
value.comp_idx = atoi(token);
token = strtok(NULL, "\t");
value.s_idx = atoi(token);
kh = kh_put(1, ukmer_table, key, &absent);
kh_val(ukmer_table, kh) = value;
kh = kh_put(1, ukmer_table, revComp(key, mask, *k), &absent);
kh_val(ukmer_table, kh) = value;
}
if (line != NULL) free(line);
fclose(fd_udb);
return 0;
}
// status = 2 if error, 1 if success, 0 if fail
layer_t * proc_query(kseq_t * seq, size_t k, int num_comp, khash_t(1) * ukmer_table, char *** idx_arrs, int * status){
*status = 0;
int i, len, q_pos;
khint64_t key, mask;
khiter_t kh;
if (seq->seq.l < k) {
// fprintf(stderr, "invalid k=%zu selection on sname %s\n", k, seq->name.s);
*status = 2;
return NULL;
}
len = 0;
key = 0;
mask = get_mask(k);
int arr_s = ARR_SIZE;
int a_idx = 0;
item_t * arraylist = (item_t *) malloc(sizeof(item_t) * arr_s);
init_item(&arraylist[a_idx], -1, -1, 0);
tuple_t kmer_idd;
// parse our kmers
for (i = 0; i < (int) seq->seq.l; i ++){
if (!(seq->seq.s[i] == 'A' || seq->seq.s[i] == 'C' || seq->seq.s[i] == 'G' || seq->seq.s[i] == 'T'))
{
key = 0;
len = 0;
continue;
}
key = (key << 0b10);
key = (key | ((seq->seq.s[i] >> 0b1) & 0b11));
key = mask & key;
len++;
if (len == k)
{
// key
kh = kh_get(1, ukmer_table, key);
if (kh != kh_end(ukmer_table)){
kmer_idd = kh_val(ukmer_table, kh);
if (!ieq_item(&arraylist[a_idx], kmer_idd)) {
if (arraylist[a_idx].count > 0) {
a_idx ++;
if (a_idx == arr_s) {
arr_s += STEP_SIZE;
arraylist = (item_t *) realloc(arraylist, arr_s * sizeof(item_t));
}
}
init_item(&arraylist[a_idx], kmer_idd.comp_idx, kmer_idd.s_idx, 1);
}
}
len--;
}
}
if (a_idx == 0) {
// no unique k-mer be found
// fprintf(stderr, "no unique-kmer for %s\n", seq->name.s);
free(arraylist);
*status = 0;
return NULL;
}
item_t item;
layer_t * layers = (layer_t *) calloc(num_comp, sizeof(layer_t));
for (i = 0; i < num_comp; i ++){
layers[i].l = 0;
layers[i].m = 0;
layers[i].tetras = NULL;
}
int alpha;
for (q_pos = 0; q_pos < a_idx + 1; q_pos ++) {
item = arraylist[q_pos];
alpha = item.value.comp_idx;
append_layer(&layers[alpha], item, q_pos);
}
free(arraylist);
tree_t * chain_tree_asc = (tree_t *) malloc(sizeof(tree_t));
tree_t * chain_tree_dsc = (tree_t *) malloc(sizeof(tree_t));
init_tree(chain_tree_asc);
init_tree(chain_tree_dsc);
for (i = 0; i < num_comp; i ++) {
append_tree(chain_tree_asc, &layers[i]);
append_tree(chain_tree_dsc, &layers[num_comp - i - 1]);
}
free_layer(layers, num_comp);
int w0, l0, m0;
int w1, l1, m1;
tetra_t * path0 = get_max_path(chain_tree_asc, &m0, &l0, &w0, true);
tetra_t * path1 = get_max_path(chain_tree_dsc, &m1, &l1, &w1, false);
free_tree(chain_tree_asc);
free_tree(chain_tree_dsc);
tetra_t * max_path;
int max_l, max_m;
if (w0 >= w1){
if (path1 != NULL) free(path1);
max_path = path0;
max_l = l0;
max_m = m0;
} else {
if (path0 != NULL) free(path0);
max_path = path1;
max_l = l1;
max_m = m1;
}
layer_t * rtn = NULL;
if (max_l != 0){
*status = 1;
rtn = (layer_t *) malloc(sizeof(layer_t));
rtn->l = max_l;
rtn->m = max_m;
rtn->tetras = max_path;
} else {
*status = 0;
// fprintf(stdout, "skip %s\n", seq->name.s);
}
return rtn;
}
#endif