Debian Codegraph @

src/abpoa_output.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include "abpoa_output.h"
7 #include "abpoa.h"
8 #include "abpoa_graph.h"
9 #include "utils.h"
10 #include "abpoa_seq.h"
11 #include "kdq.h"
12
13 extern char ab_char256_table[256];
14 char ab_LogTable65536[65536];
15 char ab_bit_table16[65536];
16
17 #define NAT_E 2.718281828459045
18 static const char ab_LogTable256[256] = {
19 #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
20 -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
21 LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
22 LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
23 };
24
25 static inline int ilog2_32(uint32_t v)
26 {
27 uint32_t t, tt;
28 if ((tt = v>>16)) return (t = tt>>8) ? 24 + ab_LogTable256[t] : 16 + ab_LogTable256[tt];
29 return (t = v>>8) ? 8 + ab_LogTable256[t] : ab_LogTable256[v];
30 }
31
32 void set_65536_table(void) {
33 int i;
34 for (i = 0; i < 65536; ++i) {
35 ab_LogTable65536[i] = ilog2_32(i);
36 }
37 }
38
39 void set_bit_table16(void) {
40 int i; ab_bit_table16[0] = 0;
41 for (i = 0; i != 65536; ++i) ab_bit_table16[i] = (i&1) + ab_bit_table16[i>>1];
42 }
43
44 #define get_bit_cnt4(table, b) (table[(b)&0xffff] + table[(b)>>16&0xffff] + table[(b)>>32&0xffff] + table[(b)>>48&0xffff])
45
46 static inline int ilog2_64(uint64_t v) {
47 uint64_t t, tt;
48 if ((tt = v >> 32)) return (t = tt >> 16) ? 48 + ab_LogTable65536[t] : 32 + ab_LogTable65536[tt];
49 return (t = v>>16) ? 16 + ab_LogTable65536[t] : ab_LogTable65536[v];
50 }
51
52 KDQ_INIT(int)
53 #define kdq_int_t kdq_t(int)
54
55 static inline int get_read_cnt(uint64_t *read_ids, int read_ids_n) {
56 int i, c;
57 for (i = c =0; i < read_ids_n; ++i) {
58 c += get_bit_cnt4(ab_bit_table16, read_ids[i]);
59 }
60 return c;
61 }
62
63 abpoa_cons_t *abpoa_allocate_rc_msa(abpoa_cons_t *abc, int msa_len, int n_seq, int n_cons) {
64 int i;
65 abc->n_seq = n_seq; abc->msa_len = msa_len;
66 abc->msa_base = (uint8_t**)_err_malloc((n_seq+n_cons) * sizeof(uint8_t*));
67 for (i = 0; i < n_seq+n_cons; ++i) {
68 abc->msa_base[i] = (uint8_t*)_err_malloc(msa_len * sizeof(uint8_t));
69 }
70 return abc;
71 }
72
73 void abpoa_output_rc_msa(abpoa_t *ab, abpoa_para_t *abpt, FILE *out_fp) {
74 if (out_fp == NULL) return;
75 int i, j;
76 abpoa_seq_t *abs = ab->abs; abpoa_cons_t *abc = ab->abc;
77 if (abc->msa_len <= 0) return;
78 for (i = 0; i < abs->n_seq; ++i) {
79 if (abs->name[i].l > 0) {
80 if (abs->is_rc[i]) fprintf(out_fp, ">%s_reverse_complement\n", abs->name[i].s);
81 else fprintf(out_fp, ">%s\n", abs->name[i].s);
82 } else {
83 fprintf(out_fp, ">Seq_%d\n", i+1);
84 }
85 for (j = 0; j < abc->msa_len; ++j) fprintf(out_fp, "%c", ab_char256_table[abc->msa_base[i][j]]);
86 fprintf(out_fp, "\n");
87 }
88 if (abpt->out_cons) { // RC-MSA for consensus sequence
89 int cons_i;
90 for (cons_i = 0; cons_i < abc->n_cons; cons_i++) {
91 fprintf(out_fp, ">Consensus_sequence");
92 if (abc->n_cons > 1) {
93 fprintf(out_fp, "_%d ", cons_i+1);
94 for (j = 0; j < abc->clu_n_seq[cons_i]; ++j) { // cluter read_id
95 if (j != 0) fprintf(out_fp, ",");
96 fprintf(out_fp, "%d", abc->clu_read_ids[cons_i][j]);
97 }
98 }
99 fprintf(out_fp, "\n");
100 for (i = 0; i < abc->msa_len; ++i) fprintf(out_fp, "%c", ab_char256_table[abc->msa_base[abc->n_seq+cons_i][i]]);
101 fprintf(out_fp, "\n");
102 }
103 }
104 }
105
106 void abpoa_set_msa_seq(abpoa_node_t node, int rank, uint8_t **msa_base) {
107 int i, j, b, read_id; uint8_t base = node.base;
108 uint64_t num, tmp;
109
110 b = 0;
111 for (i = 0; i < node.read_ids_n; ++i) {
112 for (j = 0; j < node.out_edge_n; ++j) {
113 num = node.read_ids[j][i];
114 while (num) {
115 tmp = num & -num;
116 read_id = ilog2_64(tmp);
117 msa_base[b+read_id][rank-1] = base;
118 num ^= tmp;
119 }
120 }
121 b += 64;
122 }
123 }
124
125 int abpoa_collect_msa(abpoa_graph_t *abg, abpoa_para_t *abpt, uint8_t **msa, int n_seq) {
126 if (abg->node_n <= 2) return 0;
127 abpoa_set_msa_rank(abg, ABPOA_SRC_NODE_ID, ABPOA_SINK_NODE_ID);
128 int msa_len = abg->node_id_to_msa_rank[ABPOA_SINK_NODE_ID]-1;
129 int i, j;
130 for (i = 0; i < n_seq; ++i) {
131 msa[i] = (uint8_t*)_err_malloc(msa_len * sizeof(uint8_t));
132 for (j = 0; j < msa_len; ++j) msa[i][j] = abpt->m;
133 }
134 int rank, aligned_id;
135 // if (out_fp && abpt->out_msa_header == 0) fprintf(out_fp, ">Multiple_sequence_alignment\n");
136 for (i = 2; i < abg->node_n; ++i) {
137 // get msa rank
138 rank = abpoa_graph_node_id_to_msa_rank(abg, i);
139 for (j = 0; j < abg->node[i].aligned_node_n; ++j) {
140 aligned_id = abg->node[i].aligned_node_id[j];
141 rank = MAX_OF_TWO(rank, abpoa_graph_node_id_to_msa_rank(abg, aligned_id));
142 }
143 // assign seq
144 abpoa_set_msa_seq(abg->node[i], rank, msa);
145 }
146 return msa_len;
147 }
148
149 // only generate rc-msa, output in separated func
150 void abpoa_generate_rc_msa(abpoa_t *ab, abpoa_para_t *abpt) {
151 abpoa_graph_t *abg = ab->abg;
152 if (abg->node_n <= 2) return;
153 abpoa_set_msa_rank(abg, ABPOA_SRC_NODE_ID, ABPOA_SINK_NODE_ID);
154 if (abpt->out_cons) abpoa_generate_consensus(ab, abpt);
155
156 abpoa_seq_t *abs = ab->abs; abpoa_cons_t *abc = ab->abc;
157 int i, j, aligned_id, n_seq = abs->n_seq;
158 int msa_len = abg->node_id_to_msa_rank[ABPOA_SINK_NODE_ID]-1;
159
160 abpoa_allocate_rc_msa(abc, msa_len, n_seq, abc->n_cons);
161 for (i = 0; i < n_seq; ++i) {
162 for (j = 0; j < abc->msa_len; ++j)
163 abc->msa_base[i][j] = abpt->m;
164 }
165
166 int rank;
167 // if (out_fp && abpt->out_msa_header == 0) fprintf(out_fp, ">Multiple_sequence_alignment\n");
168 for (i = 2; i < abg->node_n; ++i) {
169 // get msa rank
170 rank = abpoa_graph_node_id_to_msa_rank(abg, i);
171 for (j = 0; j < abg->node[i].aligned_node_n; ++j) {
172 aligned_id = abg->node[i].aligned_node_id[j];
173 rank = MAX_OF_TWO(rank, abpoa_graph_node_id_to_msa_rank(abg, aligned_id));
174 }
175 // assign seq
176 abpoa_set_msa_seq(abg->node[i], rank, abc->msa_base);
177 }
178 if (abpt->out_cons) {
179 int cons_i, cur_id;
180 for (cons_i = 0; cons_i < abc->n_cons; cons_i++) {
181 for (i = 0; i < msa_len; ++i) abc->msa_base[n_seq+cons_i][i] = abpt->m;
182 for (i = 0; i < abc->cons_len[cons_i]; ++i) {
183 cur_id = abc->cons_node_ids[cons_i][i];
184 rank = abpoa_graph_node_id_to_msa_rank(abg, cur_id);
185 for (j = 0; j < abg->node[cur_id].aligned_node_n; ++j) {
186 aligned_id = abg->node[cur_id].aligned_node_id[j];
187 rank = MAX_OF_TWO(rank, abpoa_graph_node_id_to_msa_rank(abg, aligned_id));
188 }
189 abc->msa_base[n_seq+cons_i][rank-1] = abc->cons_base[cons_i][i];
190 }
191 }
192 }
193 }
194
195 // generate & output gfa
196 void abpoa_generate_gfa(abpoa_t *ab, abpoa_para_t *abpt, FILE *out_fp) {
197 if (out_fp == NULL) return;
198 abpoa_seq_t *abs = ab->abs; abpoa_graph_t *abg = ab->abg;
199 if (abg->node_n <= 2) return;
200
201 // traverse graph
202 int *in_degree = (int*)_err_malloc(abg->node_n * sizeof(int));
203 int n_seq = abs->n_seq;
204 int **read_paths = (int**)_err_malloc(n_seq * sizeof(int*)), *read_path_i = (int*)_err_calloc(n_seq, sizeof(int));
205 int i, j, cur_id, pre_id, out_id, *id;
206 for (i = 0; i < abg->node_n; ++i) in_degree[i] = abg->node[i].in_edge_n;
207 for (i = 0; i < n_seq; ++i) read_paths[i] = (int*)_err_malloc(abg->node_n * sizeof(int));
208
209 // output comment and header
210 int nl = 0;
211 for (i = 2; i < abg->node_n; ++i) nl += abg->node[i].in_edge_n;
212 fprintf(out_fp, "H\tVN:Z:1.0\tNS:i:%d\tNL:i:%d\tNP:i:%d\n", abg->node_n-2, nl - abg->node[ABPOA_SRC_NODE_ID].out_edge_n, n_seq + abpt->out_cons);
213
214 kdq_int_t *q = kdq_init_int();
215
216 // Breadth-First-Search
217 kdq_push_int(q, ABPOA_SRC_NODE_ID);
218 while ((id = kdq_shift_int(q)) != 0) {
219 cur_id = *id;
220 if (cur_id == ABPOA_SINK_NODE_ID) {
221 kdq_destroy_int(q);
222 break;
223 } else {
224 if (cur_id != ABPOA_SRC_NODE_ID) {
225 // output node
226 fprintf(out_fp, "S\t%d\t%c\n", cur_id-1, ab_char256_table[abg->node[cur_id].base]);
227 // output all links based pre_ids
228 for (i = 0; i < abg->node[cur_id].in_edge_n; ++i) {
229 pre_id = abg->node[cur_id].in_id[i];
230 if (pre_id != ABPOA_SRC_NODE_ID)
231 fprintf(out_fp, "L\t%d\t+\t%d\t+\t0M\n", pre_id-1, cur_id-1);
232 }
233 // add node id to read path
234 int b, read_id; uint64_t num, tmp;
235 b = 0;
236 for (i = 0; i < abg->node[cur_id].read_ids_n; ++i) {
237 for (j = 0; j < abg->node[cur_id].out_edge_n; ++j) {
238 num = abg->node[cur_id].read_ids[j][i];
239 while (num) {
240 tmp = num & -num;
241 read_id = ilog2_64(tmp);
242 read_paths[b+read_id][read_path_i[b+read_id]++] = cur_id-1;
243 num ^= tmp;
244 }
245 }
246 b += 64;
247 }
248 }
249 for (i = 0; i < abg->node[cur_id].out_edge_n; ++i) {
250 out_id = abg->node[cur_id].out_id[i];
251 if (--in_degree[out_id] == 0) {
252 kdq_push_int(q, out_id);
253 }
254 }
255 }
256 }
257 // output read paths
258 for (i = 0; i < n_seq; ++i) {
259 if (abs->name[i].l > 0) fprintf(out_fp, "P\t%s\t", abs->name[i].s);
260 else fprintf(out_fp, "P\t%d\t", i+1);
261 if (abs->is_rc[i]) {
262 for (j = read_path_i[i]-1; j >= 0; --j) {
263 fprintf(out_fp, "%d-", read_paths[i][j]);
264 if (j != 0) fprintf(out_fp, ",");
265 else fprintf(out_fp, "\t*\n");
266 }
267 } else {
268 for (j = 0; j < read_path_i[i]; ++j) {
269 fprintf(out_fp, "%d+", read_paths[i][j]);
270 if (j != read_path_i[i]-1) fprintf(out_fp, ",");
271 else fprintf(out_fp, "\t*\n");
272 }
273 }
274 }
275 if (abpt->out_cons) {
276 abpoa_generate_consensus(ab, abpt);
277 abpoa_cons_t *abc = ab->abc;
278 int cons_i;
279 for (cons_i = 0; cons_i < abc->n_cons; ++cons_i) {
280 fprintf(out_fp, "P\tConsensus_sequence");
281 if (abc->n_cons > 1) fprintf(out_fp, "_%d", cons_i+1);
282 fprintf(out_fp, "\t");
283 for (i = 0; i < abc->cons_len[cons_i]; ++i) {
284 cur_id = abc->cons_node_ids[cons_i][i];
285 fprintf(out_fp, "%d+", cur_id-1);
286 if (i != abc->cons_len[cons_i]-1) fprintf(out_fp, ",");
287 else fprintf(out_fp, "\t*\n");
288 }
289
290 }
291 }
292 free(in_degree);
293 for (i = 0; i < n_seq; ++i) free(read_paths[i]);
294 free(read_paths); free(read_path_i);
295 }
296
297 int abpoa_cons_phred_score(int n_cov, int n_seq) {
298 if (n_cov > n_seq) err_fatal(__func__, "Error: unexpected n_cov/n_seq (%d/%d).", n_cov, n_seq);
299 double x, p;
300 x = 13.8 * (1.25 * n_cov / n_seq - 0.25);
301 p = 1 - 1.0 / (1.0 + pow(NAT_E, -1 * x));
302 return (33 + (int)(-10 * log10(p) + 0.499));
303 }
304
305 int get_edge_inclu_read_count(int edge_i, int cons_i, abpoa_node_t *node, uint64_t **clu_read_ids) {
306 int n = 0, i; uint64_t b;
307 for (i = 0; i < node->read_ids_n; ++i) {
308 b = node->read_ids[edge_i][i] & clu_read_ids[cons_i][i];
309 n += get_bit_cnt4(ab_bit_table16, b);
310 }
311 return n;
312 }
313
314 // for n_clu >= 2, get weight of reads within cons_i'th clu
315 int get_inclu_edge_weight(int edge_i, int cons_i, abpoa_node_t *node, uint64_t **clu_read_ids, int use_qv) {
316 // collect read-wise weight
317 if (use_qv == 0) return get_edge_inclu_read_count(edge_i, cons_i, node, clu_read_ids);
318 int w = 0, i; uint64_t c, one = 1;
319 for (i = 0; i < node->m_read; ++i) {
320 if (node->read_weight[i] > 0) {
321 int n = i / 64, b = i & 0x3f;
322 c = node->read_ids[edge_i][n] & clu_read_ids[cons_i][n];
323 if ((c & (one << b)) > 0)
324 w += node->read_weight[i];
325 }
326 }
327 return w;
328 }
329
330 int get_edge_weight(int edge_i, int cons_i, abpoa_node_t *node, uint64_t **clu_read_ids, int use_qv, int n_clu) {
331 if (n_clu == 1) { // use weight directly
332 return node->out_edge_weight[edge_i];
333 } else { // collect weight of reads within cons_i'th clu
334 return get_inclu_edge_weight(edge_i, cons_i, node, clu_read_ids, use_qv);
335 }
336 }
337
338 int get_node_weight(int n_clu, int cons_i, abpoa_node_t *node, uint64_t **clu_read_ids, int use_qv) {
339 int w = 0, i;
340 for (i = 0; i < node->out_edge_n; ++i) {
341 w += get_edge_weight(i, cons_i, node, clu_read_ids, use_qv, n_clu);
342 }
343 return w;
344 }
345
346 // get base coverage for node[id]
347 int abpoa_node_out_cov(abpoa_node_t *node, int id, uint64_t **clu_read_ids, int cons_i, int n_cons) {
348 if (n_cons == 1) return node[id].n_read;
349 int i, out_cov = 0;
350 for (i = 0; i < node[id].out_edge_n; ++i) {
351 out_cov += get_edge_inclu_read_count(i, cons_i, node+id, clu_read_ids);
352 }
353 return out_cov;
354 }
355
356 int abpoa_node_in_cov(abpoa_node_t *node, int id, uint64_t **clu_read_ids, int cons_i, int n_cons) {
357 if (n_cons == 1) return node[id].n_read;
358 int i, j, in_id, in_cov = 0;
359 // for each id: get max{left_weigth, right_weight}
360 for (i = 0; i < node->in_edge_n; ++i) {
361 in_id = node[id].in_id[i];
362 for (j = 0; j < node[in_id].out_edge_n; ++j) {
363 if (node[in_id].out_id[j] == id) {
364 in_cov += get_edge_inclu_read_count(j, cons_i, node+in_id, clu_read_ids);
365 break;
366 }
367 }
368 }
369 return in_cov;
370 }
371 int abpoa_node_cov(abpoa_node_t *node, int id, uint64_t **clu_read_ids, int cons_i, int n_cons) {
372 if (n_cons == 1) return node[id].n_read;
373 return MAX_OF_TWO(abpoa_node_in_cov(node, id, clu_read_ids, cons_i, n_cons), abpoa_node_out_cov(node, id, clu_read_ids, cons_i, n_cons));
374 }
375
376 void abpoa_set_hb_cons(abpoa_graph_t *abg, int **max_out_id, int n_cons, uint64_t **clu_read_ids, int src_id, int sink_id, abpoa_cons_t *abc) {
377 abc->n_cons = n_cons;
378 int i, j, cur_id;
379 for (i = 0; i < n_cons; ++i) {
380 cur_id = max_out_id[i][src_id];
381 j = 0;
382 while (cur_id != sink_id) {
383 abc->cons_node_ids[i][j] = cur_id;
384 abc->cons_base[i][j] = abg->node[cur_id].base;
385 abc->cons_cov[i][j] = abpoa_node_cov(abg->node, cur_id, clu_read_ids, i, n_cons);
386 abc->cons_phred_score[i][j] = abpoa_cons_phred_score(abc->cons_cov[i][j], abc->clu_n_seq[i]);
387 ++j;
388 cur_id = max_out_id[i][cur_id];
389 }
390 abc->cons_len[i] = j;
391 }
392 }
393
394 void abpoa_set_major_voting_cons(abpoa_graph_t *abg, int use_span_read_count, int m, int ***row_column_count, int **msa_node_id, int src_id, int sink_id, int msa_l, abpoa_cons_t *abc) {
395 int cur_id, cons_i, i, j, max_c, max_base, total_c, gap_c, c;
396 int cons_l;
397 for (cons_i = 0; cons_i < abc->n_cons; ++cons_i) {
398 cons_l = 0;
399 for (i = 0; i < msa_l; ++i) {
400 max_c = 0, total_c = 0, max_base = m; //, gap_c = abc->clu_n_seq[cons_i];
401 for (j = 0; j < m-1; ++j) {
402 c = row_column_count[cons_i][i][j];
403 if (c > max_c) {
404 max_c = c;
405 max_base = j;
406 }
407 total_c += c;
408 // gap_c -= c;
409 }
410 // assert(max_base != m);
411 if (use_span_read_count) gap_c = abg->node[msa_node_id[i][max_base]].n_span_read - total_c;
412 else gap_c = abc->clu_n_seq[cons_i] - total_c;
413 if (max_c >= gap_c) { // append consensus base to abc
414 cur_id = msa_node_id[i][max_base];
415 abc->cons_node_ids[cons_i][cons_l] = cur_id;
416 abc->cons_base[cons_i][cons_l] = max_base;
417 abc->cons_cov[cons_i][cons_l] = max_c;
418 abc->cons_phred_score[cons_i][cons_l] = abpoa_cons_phred_score(abc->cons_cov[cons_i][cons_l], abc->clu_n_seq[cons_i]);
419 cons_l++;
420 }
421 // fprintf(stderr, "i: %d, max_base: %d, max_c: %d, gap_c: %d, total_c: %d, n_span: %d\n", i, max_base, max_c, gap_c, total_c, abg->node[msa_node_id[i][max_base]].n_span_read);
422 }
423 abc->cons_len[cons_i] = cons_l;
424 }
425 }
426
427 void abpoa_set_row_column_weight(abpoa_graph_t *abg, int n_clu, int m, int ***rc_weight, uint64_t **clu_read_ids, int **msa_node_id) {
428 int i, cons_i, k, rank, aligned_id;
429 int node_w;
430 for (i = 2; i < abg->node_n; ++i) {
431 // get msa rank
432 rank = abpoa_graph_node_id_to_msa_rank(abg, i);
433 for (k = 0; k < abg->node[i].aligned_node_n; ++k) {
434 aligned_id = abg->node[i].aligned_node_id[k];
435 rank = MAX_OF_TWO(rank, abpoa_graph_node_id_to_msa_rank(abg, aligned_id));
436 }
437 msa_node_id[rank-1][abg->node[i].base] = i;
438 // assign seq
439 for (cons_i = 0; cons_i < n_clu; ++cons_i) {
440 node_w = abpoa_node_out_cov(abg->node, i, clu_read_ids, cons_i, n_clu);
441 rc_weight[cons_i][rank-1][abg->node[i].base] = node_w;
442 // for (k = 0; k < abg->node[i].read_ids_n; ++k) {
443 // for (j = 0; j < abg->node[i].out_edge_n; ++j) {
444 // if (n_clu > 1) b = abg->node[i].read_ids[j][k] & clu_read_ids[cons_i][k];
445 // else b = abg->node[i].read_ids[j][k];
446 // rc_count[cons_i][rank-1][abg->node[i].base] += get_bit_cnt4(ab_bit_table16, b);
447 // }
448 // }
449 rc_weight[cons_i][rank-1][m-1] -= rc_weight[cons_i][rank-1][abg->node[i].base];
450 }
451 }
452 }
453
454 void set_clu_read_ids(abpoa_cons_t *abc, uint64_t **read_ids, int cons_i, int n_seq) {
455 int n, i, j; uint64_t b, one = 1;
456 for (i = n = 0; i < n_seq; ++i) {
457 j = i / 64; b = i & 0x3f;
458 if (read_ids[cons_i][j] & (one << b)) {
459 abc->clu_read_ids[cons_i][n++] = i;
460 }
461 }
462 if (n != abc->clu_n_seq[cons_i])
463 err_fatal(__func__, "Error in set cluster read ids. (%d, %d)", n, abc->clu_n_seq[cons_i]);
464 }
465
466 void set_clu_read_ids0(abpoa_cons_t *abc, uint64_t **read_ids, int n_seq) {
467 int i;
468 for (i = 0; i < n_seq; ++i) {
469 abc->clu_read_ids[0][i] = i;
470 }
471 if (i != abc->clu_n_seq[0])
472 err_fatal(__func__, "Error in set cluster read ids. (%d, %d)", i, abc->clu_n_seq[0]);
473 }
474
475 // heaviest_bundling
476 // 1. argmax{cur->weight}
477 // 2. argmax{out_node->weight}
478 void abpoa_heaviest_bundling(abpoa_graph_t *abg, abpoa_para_t *abpt, int src_id, int sink_id, int *out_degree, int n_clu, int read_ids_n, uint64_t **clu_read_ids, abpoa_cons_t *abc) {
479 int *id, cons_i, i, cur_id, in_id, out_id, max_id; int max_w, out_w;
480
481 int *_out_degree = (int*)_err_malloc(abg->node_n * sizeof(int));
482 int *score = (int*)_err_malloc(abg->node_n * sizeof(int));
483 int **max_out_id = (int**)_err_malloc(n_clu * sizeof(int*));
484 for (i = 0; i < n_clu; ++i) max_out_id[i] = (int*)_err_malloc(abg->node_n * sizeof(int));
485 if (n_clu == 1) {
486 abc->clu_n_seq[0] = abc->n_seq;
487 set_clu_read_ids0(abc, clu_read_ids, abc->n_seq);
488 } else {
489 for (cons_i = 0; cons_i < n_clu; cons_i++) {
490 abc->clu_n_seq[cons_i] = get_read_cnt(clu_read_ids[cons_i], read_ids_n);
491 set_clu_read_ids(abc, clu_read_ids, cons_i, abc->n_seq);
492 }
493 }
494
495 for (cons_i = 0; cons_i < n_clu; cons_i++) {
496 for (i = 0; i < abg->node_n; ++i) _out_degree[i] = out_degree[i];
497 kdq_int_t *q = kdq_init_int();
498 kdq_push_int(q, sink_id);
499 // reverse Breadth-First-Search
500 while ((id = kdq_shift_int(q)) != 0) {
501 cur_id = *id;
502 if (cur_id == sink_id) {
503 max_out_id[cons_i][cur_id] = -1;
504 score[cur_id] = 0;
505 } else {
506 max_id = -1;
507 if (cur_id == src_id) {
508 int path_score = -1, path_max_w = -1;
509 for (i = 0; i < abg->node[cur_id].out_edge_n; ++i) {
510 out_id = abg->node[cur_id].out_id[i];
511 out_w = get_edge_weight(i, cons_i, abg->node+cur_id, clu_read_ids, abpt->use_qv, n_clu);
512 if (out_w > path_max_w || (out_w == path_max_w && score[out_id] > path_score)) {
513 max_id = out_id;
514 path_score = score[out_id];
515 path_max_w = out_w;
516 }
517 }
518 max_out_id[cons_i][cur_id] = max_id;
519 kdq_destroy_int(q);
520 break;
521 } else {
522 max_w = INT32_MIN;
523 for (i = 0; i < abg->node[cur_id].out_edge_n; ++i) {
524 out_id = abg->node[cur_id].out_id[i];
525 out_w = get_edge_weight(i, cons_i, abg->node+cur_id, clu_read_ids, abpt->use_qv, n_clu);
526 if (max_w < out_w) {
527 max_w = out_w;
528 max_id = out_id;
529 } else if (max_w == out_w && score[max_id] <= score[out_id]) {
530 max_id = out_id;
531 }
532 }
533 score[cur_id] = max_w + score[max_id];
534 max_out_id[cons_i][cur_id] = max_id;
535 }
536 }
537 for (i = 0; i < abg->node[cur_id].in_edge_n; ++i) {
538 in_id = abg->node[cur_id].in_id[i];
539 if (--_out_degree[in_id] == 0)
540 kdq_push_int(q, in_id);
541 }
542 }
543 }
544 abpoa_set_hb_cons(abg, max_out_id, n_clu, clu_read_ids, src_id, sink_id, abc);
545
546 free(score); free(_out_degree);
547 for (i = 0; i < n_clu; ++i) free(max_out_id[i]); free(max_out_id);
548 }
549
550 void abpoa_most_frequent(abpoa_graph_t *abg, abpoa_para_t *abpt, int src_id, int sink_id, int n_clu, int read_ids_n, uint64_t **clu_read_ids, abpoa_cons_t *abc) {
551 int use_span_read_count = abpt->sub_aln;
552 abpoa_set_msa_rank(abg, src_id, sink_id);
553 int i, cons_i, msa_l = abg->node_id_to_msa_rank[sink_id]-1;
554 int ***row_column_weight = (int***)_err_malloc(n_clu * sizeof(int**));
555 int **msa_node_id = (int**)_err_malloc(msa_l * sizeof(int*));
556 // init row_column_weight
557 for (cons_i = 0; cons_i < n_clu; ++cons_i) {
558 row_column_weight[cons_i] = (int**)_err_malloc(msa_l * sizeof(int*));
559 for (i = 0; i < msa_l; ++i) {
560 row_column_weight[cons_i][i] = (int*)_err_calloc(abpt->m, sizeof(int));
561 row_column_weight[cons_i][i][abpt->m-1] = abc->clu_n_seq[cons_i];
562 }
563 }
564 for (i = 0; i < msa_l; ++i) msa_node_id[i] = (int*)_err_calloc(abpt->m, sizeof(int));
565 abc->n_cons = n_clu;
566 if (n_clu == 1) {
567 abc->clu_n_seq[0] = abc->n_seq;
568 set_clu_read_ids0(abc, clu_read_ids, abc->n_seq);
569 } else {
570 for (cons_i = 0; cons_i < n_clu; ++cons_i) {
571 abc->clu_n_seq[cons_i] = get_read_cnt(clu_read_ids[cons_i], read_ids_n);
572 set_clu_read_ids(abc, clu_read_ids, cons_i, abc->n_seq);
573 }
574 }
575 // no quality weight used for now, only read count
576 abpoa_set_row_column_weight(abg, n_clu, abpt->m, row_column_weight, clu_read_ids, msa_node_id);
577 abpoa_set_major_voting_cons(abg, use_span_read_count, abpt->m, row_column_weight, msa_node_id, src_id, sink_id, msa_l, abc);
578 for (cons_i = 0; cons_i < n_clu; ++cons_i) {
579 for (i = 0; i < msa_l; ++i) {
580 free(row_column_weight[cons_i][i]);
581 } free(row_column_weight[cons_i]);
582 }
583 for (i = 0; i < msa_l; ++i) {
584 free(msa_node_id[i]);
585 }
586 free(row_column_weight); free(msa_node_id);
587 }
588
589 void abpoa_output_fx_consensus(abpoa_t *ab, abpoa_para_t *abpt, FILE *out_fp) {
590 if (out_fp == NULL) return;
591 int cons_i, j;
592 abpoa_cons_t *abc = ab->abc;
593 for (cons_i = 0; cons_i < abc->n_cons; ++cons_i) {
594 if (abpt->out_fq) fprintf(out_fp, "@Consensus_sequence");
595 else fprintf(out_fp, ">Consensus_sequence");
596 if (abpt->batch_index > 0) {
597 fprintf(out_fp, "_%d", abpt->batch_index); // batch index for file mapping
598 }
599 if (abc->n_cons > 1) {
600 fprintf(out_fp, "_%d ", cons_i+1); // cons_id
601 for (j = 0; j < abc->clu_n_seq[cons_i]; ++j) { // cluter read_id
602 if (j != 0) fprintf(out_fp, ",");
603 fprintf(out_fp, "%d", abc->clu_read_ids[cons_i][j]);
604 }
605 }
606 fprintf(out_fp, "\n");
607 for (j = 0; j < abc->cons_len[cons_i]; ++j) {
608 fprintf(out_fp, "%c", ab_char256_table[abc->cons_base[cons_i][j]]);
609 } fprintf(out_fp, "\n");
610 if (abpt->out_fq) {
611 fprintf(out_fp, "+Consensus_sequence");
612 if (abpt->batch_index > 0) {
613 fprintf(out_fp, "_%d", abpt->batch_index); // batch index for file mapping
614 }
615 if (abc->n_cons > 1) {
616 fprintf(out_fp, "_%d ", cons_i+1); // cons_id
617 for (j = 0; j < abc->clu_n_seq[cons_i]; ++j) { // cluter read_id
618 if (j != 0) fprintf(out_fp, ",");
619 fprintf(out_fp, "%d", abc->clu_read_ids[cons_i][j]);
620 }
621 }
622 fprintf(out_fp, "\n");
623 for (j = 0; j < abc->cons_len[cons_i]; ++j) {
624 fprintf(out_fp, "%c", abc->cons_phred_score[cons_i][j]);
625 } fprintf(out_fp, "\n");
626 }
627 }
628 }
629
630 abpoa_cons_t *abpoa_allocate_cons(abpoa_cons_t *abc, int n_node, int n_seq, int n_cons) {
631 int i; abc->n_cons = n_cons, abc->n_seq = n_seq;
632 // allocate
633 abc->clu_n_seq = (int*)_err_calloc(n_cons, sizeof(int));
634 abc->cons_len = (int*)_err_calloc(n_cons, sizeof(int));
635 abc->cons_node_ids = (int**)_err_malloc(n_cons * sizeof(int*));
636 abc->cons_base = (uint8_t**)_err_malloc(n_cons * sizeof(uint8_t*));
637 abc->cons_cov = (int**)_err_malloc(n_cons * sizeof(int*));
638 abc->clu_read_ids = (int**)_err_malloc(n_cons * sizeof(int*));
639 abc->cons_phred_score = (int**)_err_malloc(n_cons * sizeof(int*));
640 for (i = 0; i < n_cons; ++i) {
641 abc->cons_node_ids[i] = (int*)_err_malloc(n_node * sizeof(int));
642 abc->cons_base[i] = (uint8_t*)_err_malloc(n_node * sizeof(uint8_t));
643 abc->cons_cov[i] = (int*)_err_malloc(n_node * sizeof(int));
644 abc->clu_read_ids[i] = (int*)_err_malloc(n_seq * sizeof(int));
645 abc->cons_phred_score[i] = (int*)_err_malloc(n_node * sizeof(int));
646 }
647 return abc;
648 }
649
650 int allele_clu_exist(cand_het_pos_t *cand_het_pos, int n_het_pos, int n_uniq_alles, int *alleles, int *n_clu_reads, int **clu_read_ids) {
651 int het_i = -1;
652 int i, j, k, exist;
653 for (i = n_het_pos-1; i >= 0; --i) {
654 if (cand_het_pos[i].n_uniq_alles != n_uniq_alles) continue;
655 exist = 1;
656 for (j = 0; j < n_uniq_alles; ++j) {
657 int allele_x = cand_het_pos[i].alle_bases[j];
658 int allele_y = alleles[j];
659 if (cand_het_pos[i].n_clu_reads[allele_x] != n_clu_reads[allele_y]) {
660 exist = 0; break;
661 }
662 for (k = 0; k < n_clu_reads[allele_y]; ++k) {
663 if (cand_het_pos[i].clu_read_ids[allele_x][k] != clu_read_ids[allele_y][k]) {
664 exist = 0; break;
665 }
666 }
667 if (exist == 0) break;
668 }
669 if (exist) {
670 het_i = i; break;
671 }
672 }
673 return het_i;
674 }
675
676 // allow >2 alleles for each het position
677 int abpoa_collect_cand_het_pos(uint8_t **msa, int msa_l, int n_seq, int m, int min_het, cand_het_pos_t *cand_het_pos, int *cand_het_priority, int verbose) {
678 int n_het_pos = 0;
679 int i, j, k;
680 // int min_hom = n_seq - min_het; // het >= min_het && <= min_hom
681 min_het = MAX_OF_TWO(2, min_het/2);
682 int min_hom = n_seq - min_het; // het >= min_het && <= min_hom
683 int *depth = (int*)_err_malloc((m+1) * sizeof(int));
684 int n_uniq_alles, var_type;
685 int *alleles = (int*)malloc((m+1) * sizeof(int)); // 0..m, alleles are sorted by the first appearance in n_seq reads
686 int *allele_to_idx = (int*)malloc((m+1) * sizeof(int));
687 int *allele_apperance = (int*)malloc((m+1) * sizeof(int)); // 0..m, record the first appearance of each allele in n_seq reads
688 int *n_clu_reads = (int*)malloc((m+1) * sizeof(int));
689 int **clu_read_ids = (int**)malloc((m+1) * sizeof(int*));
690 for (j = 0; j < m+1; ++j) {
691 clu_read_ids[j] = (int*)malloc(n_seq * sizeof(int));
692 }
693 for (i = 0; i < msa_l; ++i) {
694 n_uniq_alles = 0; var_type = 0;
695 int total_depth = 0;
696 memset(depth, 0, (m+1) * sizeof(int));
697 memset(n_clu_reads, 0, (m+1) * sizeof(int));
698 for (j = 0; j < n_seq; ++j) {
699 depth[msa[j][i]]++;
700 if (depth[msa[j][i]] == 1) // first appearance
701 allele_apperance[msa[j][i]] = j;
702 }
703 for (j = 0; j < m+1; ++j) {
704 if (depth[j] >= min_het && depth[j] <= min_hom) {
705 alleles[n_uniq_alles] = j;
706 total_depth += depth[j];
707 n_uniq_alles++;
708 if (j == m) var_type = 1; // indel
709 }
710 }
711 if (n_uniq_alles < 2) continue;
712 // sort alleles by the first appearance in n_seq reads
713 for (j = 0; j < n_uniq_alles-1; ++j) {
714 for (k = j+1; k < n_uniq_alles; ++k) {
715 if (allele_apperance[alleles[j]] > allele_apperance[alleles[k]]) {
716 int tmp = alleles[j];
717 alleles[j] = alleles[k];
718 alleles[k] = tmp;
719 }
720 }
721 }
722 for (j = 0; j < n_uniq_alles; ++j) allele_to_idx[alleles[j]] = j;
723
724 for (j = 0; j < n_seq; ++j) {
725 for (k = 0; k < n_uniq_alles; ++k) {
726 if (msa[j][i] == alleles[k]) {
727 clu_read_ids[alleles[k]][n_clu_reads[alleles[k]]++] = j;
728 break;
729 }
730 }
731 }
732 int het_i = allele_clu_exist(cand_het_pos, n_het_pos, n_uniq_alles, alleles, n_clu_reads, clu_read_ids);
733 if (het_i >= 0) { // exist
734 // fprintf(stdout, "het pos: %d, same as %d\n", i, cand_het_pos[het_i].pos);
735 cand_het_pos[het_i].count++;
736 if (var_type == 0) cand_het_pos[het_i].var_type = 0; // X if any is X
737 continue;
738 }
739 // new het pos
740 // fprintf(stdout, "het pos: %d, ", i);
741 // for (j = 0; j < n_uniq_alles; ++j) {
742 // fprintf(stdout, "%d (%d): ", alleles[j], n_clu_reads[alleles[j]]);
743 // for (k = 0; k < n_clu_reads[alleles[j]]; ++k) {
744 // if (k != 0) fprintf(stdout, ",");
745 // fprintf(stdout, "%d", clu_read_ids[alleles[j]][k]);
746 // } fprintf(stdout, "; ");
747 // } fprintf(stdout, "\n");
748 cand_het_pos[n_het_pos].pos = i;
749 cand_het_pos[n_het_pos].depth = total_depth; // XXX partial aligned reads???
750 cand_het_pos[n_het_pos].var_type = var_type; // ID
751 cand_het_pos[n_het_pos].count = 1;
752 cand_het_pos[n_het_pos].n_uniq_alles = n_uniq_alles;
753 cand_het_pos[n_het_pos].n_clu_reads = (int*)malloc((m+1)* sizeof(int));
754 cand_het_pos[n_het_pos].clu_read_ids = (int**)malloc((m+1)* sizeof(int*));
755 cand_het_pos[n_het_pos].read_id_to_allele_idx = (int*)malloc(n_seq * sizeof(int));
756 for (j = 0; j < n_seq; ++j) cand_het_pos[n_het_pos].read_id_to_allele_idx[j] = -1; // init
757 for (j = 0; j < m+1; ++j) {
758 cand_het_pos[n_het_pos].n_clu_reads[j] = n_clu_reads[j];
759 cand_het_pos[n_het_pos].clu_read_ids[j] = (int*)malloc(n_seq * sizeof(int));
760 memcpy(cand_het_pos[n_het_pos].clu_read_ids[j], clu_read_ids[j], n_clu_reads[j] * sizeof(int));
761 for (k = 0; k < n_clu_reads[j]; ++k) {
762 cand_het_pos[n_het_pos].read_id_to_allele_idx[clu_read_ids[j][k]] = allele_to_idx[j];
763 }
764 }
765 cand_het_pos[n_het_pos].alle_bases = (uint8_t*)malloc(n_uniq_alles * sizeof(uint8_t));
766 for (j = 0; j < n_uniq_alles; ++j) {
767 cand_het_pos[n_het_pos].alle_bases[j] = alleles[j];
768 }
769 cand_het_priority[n_het_pos] = n_het_pos;
770 n_het_pos++;
771 }
772 // sort cand_het_pos by count and type: var_type (X > ID), store new priority in cand_het_priority
773 for (i = 0; i < n_het_pos; ++i) {
774 cand_het_priority[i] = i;
775 }
776 // sort by count, depth, var_type using bubble sort
777 int swapped;
778 do {
779 swapped = 0;
780 for (j = 0; j < n_het_pos-1; ++j) {
781 int _j = cand_het_priority[j];
782 int _j1 = cand_het_priority[j+1];
783 if (cand_het_pos[_j].count < cand_het_pos[_j1].count || (cand_het_pos[_j].count == cand_het_pos[_j1].count && cand_het_pos[_j].depth < cand_het_pos[_j1].depth) ||
784 (cand_het_pos[_j].count == cand_het_pos[_j1].count && cand_het_pos[_j].depth == cand_het_pos[_j1].depth && cand_het_pos[_j].var_type > cand_het_pos[_j1].var_type)) {
785 // printf("Swap: %d (%d, %d) <-> %d (%d, %d)\n", j, cand_het_pos[_j].count, cand_het_pos[_j].var_type, j+1, cand_het_pos[_j1].count, cand_het_pos[_j1].var_type);
786 int tmp = cand_het_priority[j];
787 cand_het_priority[j] = cand_het_priority[j+1];
788 cand_het_priority[j+1] = tmp;
789 swapped = 1;
790 }
791 }
792 } while (swapped);
793
794 // selection sort
795 // for (i = 0; i < n_het_pos-1; ++i) {
796 // int _i = cand_het_priority[i];
797 // for (j = i+1; j < n_het_pos; ++j) {
798 // int _j = cand_het_priority[j];
799 // if (cand_het_pos[_i].count < cand_het_pos[_j].count || (cand_het_pos[_i].count == cand_het_pos[_j].count && cand_het_pos[_i].var_type > cand_het_pos[_j].var_type)) {
800 // printf("Swap: %d (%d, %d) <-> %d (%d, %d)\n", i, cand_het_pos[_i].count, cand_het_pos[_i].var_type, j, cand_het_pos[_j].count, cand_het_pos[_j].var_type);
801 // int tmp = cand_het_priority[i];
802 // cand_het_priority[i] = cand_het_priority[j];
803 // cand_het_priority[j] = tmp;
804 // }
805 // }
806 // }
807 // print cand_het_pos
808 // for (i = 0; i < n_het_pos; ++i) {
809 // int het_i = cand_het_priority[i];
810 // fprintf(stderr, "cand het pos: %d, %d, count: %d, ", i, cand_het_pos[het_i].pos, cand_het_pos[het_i].count);
811 // for (j = 0; j < cand_het_pos[het_i].n_uniq_alles; ++j) {
812 // fprintf(stderr, "%d (%d): ", cand_het_pos[het_i].alle_bases[j], cand_het_pos[het_i].n_clu_reads[cand_het_pos[het_i].alle_bases[j]]);
813 // for (k = 0; k < cand_het_pos[het_i].n_clu_reads[cand_het_pos[het_i].alle_bases[j]]; ++k) {
814 // if (k != 0) fprintf(stderr, ",");
815 // fprintf(stderr, "%d", cand_het_pos[het_i].clu_read_ids[cand_het_pos[het_i].alle_bases[j]][k]);
816 // } fprintf(stderr, "; ");
817 // } fprintf(stderr, "\n");
818 // }
819 for (j = 0; j < m+1; ++j) free(clu_read_ids[j]);
820 free(clu_read_ids); free(n_clu_reads); free(alleles); free(allele_to_idx); free(allele_apperance); free(depth);
821 return n_het_pos;
822 }
823
824 int abpoa_collect_msa_dis1(uint8_t **msa, int msa_l, int n_seq, int m, cand_het_pos_t *cand_het_pos, int n_het_pos, int i, int j) {
825 int dis = 0;
826 for (int k = 0; k < n_het_pos; ++k) {
827 int pos = cand_het_pos[k].pos;
828 // for (int pos = 0; pos < msa_l; ++pos) {
829 int var_weight = 1; // Gap
830 if (cand_het_pos[k].var_type == 0) var_weight = 2; // SNP
831 uint8_t base1 = msa[i][pos], base2 = msa[j][pos];
832 int base1_is_valid = 0, base2_is_valid = 0;
833 for (int i = 0; i < cand_het_pos[k].n_uniq_alles; ++i) {
834 if (base1 == cand_het_pos[k].alle_bases[i]) base1_is_valid = 1;
835 if (base2 == cand_het_pos[k].alle_bases[i]) base2_is_valid = 1;
836 }
837 if (base1_is_valid == 0 || base2_is_valid == 0) continue;
838 if (base1 != base2) dis += var_weight*cand_het_pos[k].count;
839 }
840 return dis;
841 }
842
843 int **abpoa_collect_msa_dis_matrix(uint8_t **msa, int msa_l, int n_seq, int m, cand_het_pos_t *cand_het_pos, int n_het_pos, int verbose) {
844 int i, j, **dis_matrix = (int**)malloc(n_seq * sizeof(int*));
845 for (i = 0; i < n_seq; ++i) {
846 dis_matrix[i] = (int*)calloc(n_seq, sizeof(int));
847 }
848
849 for (i = 0; i < n_seq; ++i) {
850 for (j = i+1; j < n_seq; ++j) {
851 dis_matrix[i][j] = abpoa_collect_msa_dis1(msa, msa_l, n_seq, m, cand_het_pos, n_het_pos, i, j);
852 dis_matrix[j][i] = dis_matrix[i][j];
853 }
854 }
855 // print dis_matrix
856 // for (i = 0; i < n_seq; ++i) {
857 // for (j = 0; j < n_seq; ++j) {
858 // printf("%d vs %d: %d\n", i, j, dis_matrix[i][j]);
859 // }
860 // printf("\n");
861 // }
862 return dis_matrix;
863 }
864
865 // pick two reads from two different clusters with max_dis
866 int abpoa_collect_2medoids(cand_het_pos_t *cand_het_pos, int het_i, int **dis_matrix, int n_seq, int *med) {
867 int max_dis = 0, max_i = -1, max_j = -1;
868 for (int i = 0; i < cand_het_pos[het_i].n_uniq_alles-1; ++i) { // i' cluster
869 int allele_i = cand_het_pos[het_i].alle_bases[i];
870 for (int j = i+1; j < cand_het_pos[het_i].n_uniq_alles; ++j) { // j' cluster
871 int allele_j = cand_het_pos[het_i].alle_bases[j];
872 // printf("clu %d: %c vs clu %d: %c\n", i, "ACGTN-"[allele_i], j, "ACGTN-"[allele_j]);
873 // loop over all reads in i' cluster and j' cluster
874 for (int r1 = 0; r1 < cand_het_pos[het_i].n_clu_reads[allele_i]; ++r1) {
875 int read_i = cand_het_pos[het_i].clu_read_ids[allele_i][r1];
876 for (int r2 = 0; r2 < cand_het_pos[het_i].n_clu_reads[allele_j]; ++r2) {
877 int read_j = cand_het_pos[het_i].clu_read_ids[allele_j][r2];
878 if (dis_matrix[read_i][read_j] > max_dis) {
879 max_dis = dis_matrix[read_i][read_j];
880 max_i = read_i; max_j = read_j;
881 }
882 }
883 }
884 }
885 }
886 if (max_dis > 0) {
887 med[0] = max_i; med[1] = max_j;
888 // printf("medoids: %d, %d\n", max_i, max_j);
889 return 2;
890 } else return 0;
891 }
892
893 int get_partition_index(cand_het_pos_t *cand_het_pos, int het_i, int read_i) {
894 int partition_index = 0;
895 for (int k = 0; k <= het_i; ++k) {
896 partition_index *= (cand_het_pos[k].n_uniq_alles + 1);
897 partition_index += cand_het_pos[k].read_id_to_allele_idx[read_i] + 1;
898 }
899 return partition_index;
900 }
901
902 // 1) if any {0 X ... X het_i}'s clusters is empty, pick one read from the empty cluster with max(min_dis) to existing medoids
903 // 2) if all {0 X ... X het_i}'s clusters are used, within each cluster, pick one read with max(min_dis) to existing medoids, pick the one with max_dis among the all clusters
904 int abpoa_collect_1medoid(cand_het_pos_t *cand_het_pos, int het_i, int **dis_matrix, int n_seq, int *med, int n_medoids) {
905 assert(n_medoids > 0);
906 int n_partitions = 1;
907 for (int k = 0; k <= het_i; ++k) {
908 n_partitions *= (cand_het_pos[k].n_uniq_alles + 1); // +1 for not covered
909 }
910 int *partition_counts = (int*)calloc(n_partitions, sizeof(int));
911 for (int i = 0; i < n_seq; ++i) {
912 int partition_index = get_partition_index(cand_het_pos, het_i, i);
913 partition_counts[partition_index] += 1;
914 }
915
916 int found = 0;
917 // check if any cluster is not used by existing medoids
918 int max_dis = 0, max_read_i = -1, max_n_partition_total_count = -1;
919 for (int i = 0; i < n_seq; ++i) {
920 found = 0;
921 int read_partition_index = get_partition_index(cand_het_pos, het_i, i);
922 int read_partition_total_count = partition_counts[read_partition_index];
923 for (int j = 0; j < n_medoids; ++j) {
924 int med_partition_index = get_partition_index(cand_het_pos, het_i, med[j]);
925 if (read_partition_index == med_partition_index) {
926 found = 1; break;
927 }
928 }
929
930 if (found == 0) { // case 1: read i's partition is not used by any existing medoids
931 int min_dis = INT_MAX;
932 for (int j = 0; j < n_medoids; ++j) { // min among all medoids
933 if (dis_matrix[i][med[j]] < min_dis) min_dis = dis_matrix[i][med[j]];
934 }
935 if (read_partition_total_count > max_n_partition_total_count || (read_partition_total_count == max_n_partition_total_count && min_dis > max_dis)) { // prefer smaller n_null_allele
936 // printf("min: %d, read: %d, n_partition_total_count: %d\n", min_dis, i, read_partition_total_count);
937 max_dis = min_dis; max_read_i = i;
938 max_n_partition_total_count = read_partition_total_count;
939 }
940 }
941 }
942 if (max_read_i == -1) { // case 2: all clusters are used, pick one read from each cluster, then pick the one with max(min_dis)
943 for (int i = 0; i < cand_het_pos[het_i].n_uniq_alles; ++i) {
944 int allele = cand_het_pos[het_i].alle_bases[i];
945 for (int r = 0; r < cand_het_pos[het_i].n_clu_reads[allele]; ++r) {
946 int read_i = cand_het_pos[het_i].clu_read_ids[allele][r];
947 int min_dis = INT_MAX; int skip = 0;
948 for (int j = 0; j < n_medoids; ++j) { // min among all medoids
949 if (med[j] == read_i) {
950 skip = 1;
951 // fprintf(stderr, "Error: medoid %d already in the cluster\n", read_i);
952 continue;
953 }
954 if (dis_matrix[read_i][med[j]] < min_dis) {
955 min_dis = dis_matrix[read_i][med[j]];
956 }
957 }
958 if (min_dis > max_dis && !skip) {
959 max_dis = min_dis;
960 max_read_i = read_i;
961 }
962 }
963 }
964 }
965 free(partition_counts);
966 if (max_read_i != -1) {
967 med[n_medoids] = max_read_i;
968 // printf("medoid: %d\n", max_read_i);
969 return 1;
970 } else return 0;
971 }
972
973 // return up to n_uniq_alleles reads with max dis, each come from one of the cluster defined by het_i
974 int abpoa_collect_multi_medoids(cand_het_pos_t *cand_het_pos, int het_i, int **dis_matrix, int n_seq, int max_n_cons, int *med, int n_medoids) {
975 int n_to_collect = MIN_OF_TWO(cand_het_pos[het_i].n_uniq_alles, max_n_cons);
976 while (n_medoids < n_to_collect) {
977 int new_medoids = 0;
978 if (n_medoids == 0) {
979 new_medoids = abpoa_collect_2medoids(cand_het_pos, het_i, dis_matrix, n_seq, med);
980 } else {
981 new_medoids = abpoa_collect_1medoid(cand_het_pos, het_i, dis_matrix, n_seq, med, n_medoids);
982 }
983 if (new_medoids == 0) break;
984 n_medoids += new_medoids;
985 }
986 return n_medoids;
987 }
988
989 // allow >2 clusters
990 int abpoa_init_kmedoids(cand_het_pos_t *cand_het_pos, int *cand_het_priority, int n_het_pos, int **dis_matrix, int n_seq, int max_n_cons, int *medoids) {
991 assert(max_n_cons >= 2);
992 int n_medoids = 0, het_i = 0;
993 while (n_medoids < max_n_cons) {
994 if (n_medoids == 0) n_medoids += abpoa_collect_multi_medoids(cand_het_pos, cand_het_priority[het_i], dis_matrix, n_seq, max_n_cons, medoids, n_medoids);
995 else n_medoids += abpoa_collect_1medoid(cand_het_pos, cand_het_priority[het_i], dis_matrix, n_seq, medoids, n_medoids);
996 het_i++;
997 if (het_i >= n_het_pos) break;
998 }
999 return n_medoids;
1000 }
1001
1002 // within each cluster, find the medoid that has the smallest sum of distances to all other members
1003 void abpoa_collect_kmedoids0(int **dis_matrix, int n_seq, int max_n_cons, int **clu_reads, int *n_clu_seqs, int *medoids) {
1004 for (int i = 0; i < max_n_cons; ++i) {
1005 int min_sum_dis = INT_MAX, min_sum_dis_read_i = -1;
1006 for (int j = 0; j < n_clu_seqs[i]; ++j) {
1007 int sum_dis = 0, read_i = clu_reads[i][j];
1008 for (int k = 0; k < n_clu_seqs[i]; ++k) {
1009 if (j == k) continue;
1010 int read_j = clu_reads[i][k];
1011 sum_dis += dis_matrix[read_i][read_j];
1012 }
1013 if (sum_dis < min_sum_dis) {
1014 min_sum_dis = sum_dis;
1015 min_sum_dis_read_i = read_i;
1016 }
1017 }
1018 if (min_sum_dis_read_i != -1) medoids[i] = min_sum_dis_read_i;
1019 }
1020 // sort medoids, from small to large
1021 for (int i = 0; i < max_n_cons-1; ++i) {
1022 for (int j = i+1; j < max_n_cons; ++j) {
1023 if (medoids[i] > medoids[j]) {
1024 int tmp = medoids[i]; medoids[i] = medoids[j]; medoids[j] = tmp;
1025 }
1026 }
1027 }
1028 }
1029
1030 // XXX swap medoids with non-medoids ???
1031 int abpoa_update_kmedoids(int **dis_matrix, int n_seq, int max_n_cons, int **medoids, int **clu_reads, int *n_clu_seqs, int verbose) {
1032 int *new_medoids = (int*)malloc(max_n_cons * sizeof(int));
1033 for (int i = 0; i < max_n_cons; ++i) new_medoids[i] = -1;
1034 memset(n_clu_seqs, 0, sizeof(int) * max_n_cons);
1035 for (int i = 0; i < n_seq; ++i) {
1036 int min_dis = INT_MAX, min_clu = -1, tied = 0;
1037 for (int j = 0; j < max_n_cons; ++j) {
1038 if (dis_matrix[i][(*medoids)[j]] < min_dis) {
1039 min_dis = dis_matrix[i][(*medoids)[j]];
1040 min_clu = j;
1041 tied = 0;
1042 } else if (dis_matrix[i][(*medoids)[j]] == min_dis) {
1043 tied = 1;
1044 }
1045 }
1046 // if (tied != 0 || min_clu == -1) continue;
1047 if (min_clu == -1) continue;
1048 if (tied == 1) {
1049 if (n_clu_seqs[0] < n_clu_seqs[1]) min_clu = 0;
1050 else min_clu = 1;
1051 }
1052 clu_reads[min_clu][n_clu_seqs[min_clu]++] = i;
1053 }
1054 // printf("Clusters after assignment:\n");
1055 // for (int i = 0; i < max_n_cons; ++i) {
1056 // printf("Cluster %d: ", i);
1057 // for (int j = 0; j < n_clu_seqs[i]; ++j) {
1058 // printf("%d ", clu_reads[i][j]);
1059 // }
1060 // printf("\n");
1061 // }
1062 abpoa_collect_kmedoids0(dis_matrix, n_seq, max_n_cons, clu_reads, n_clu_seqs, new_medoids);
1063 // print clu_reads and new_medoids
1064 // printf("Clusters after updating medoids:\n");
1065 // for (int i = 0; i < max_n_cons; ++i) {
1066 // printf("Cluster %d: ", i);
1067 // for (int j = 0; j < n_clu_seqs[i]; ++j) {
1068 // printf("%d ", clu_reads[i][j]);
1069 // }
1070 // printf("\n");
1071 // }
1072 // printf("New Medoids: ");
1073 // for (int i = 0; i < max_n_cons; ++i) {
1074 // printf("%d ", new_medoids[i]);
1075 // }
1076 // printf("\n");
1077 int changed = 0;
1078 for (int i = 0; i < max_n_cons; ++i) {
1079 if (new_medoids[i] == -1) { // no reads in this cluster
1080 changed = 0; break;
1081 }
1082 if (new_medoids[i] != (*medoids)[i]) changed = 1;
1083 }
1084 free(*medoids);
1085 *medoids = new_medoids;
1086 return changed;
1087 }
1088
1089 int abpoa_clu_reads_kmedoids(cand_het_pos_t *cand_het_pos, int *cand_het_priority, int n_het_pos, int **dis_matrix, int n_seq, int min_het, int max_n_cons, uint64_t ***clu_read_ids, int verbose) {
1090 // iterative update clusters until convergence
1091 int *medoids = (int*)_err_malloc(sizeof(int) * max_n_cons);
1092 int **clu_reads = (int**)_err_malloc(sizeof(int*) * max_n_cons);
1093 int *n_clu_seqs = (int*)_err_malloc(sizeof(int) * max_n_cons);
1094 for (int i = 0; i < max_n_cons; ++i) {
1095 clu_reads[i] = (int*)_err_malloc(n_seq * sizeof(int));
1096 }
1097 int to_collect_medoids = max_n_cons, n_clusters = 1;
1098 while (1) {
1099 if (abpoa_init_kmedoids(cand_het_pos, cand_het_priority, n_het_pos, dis_matrix, n_seq, to_collect_medoids, medoids) <= 0) break; //return 0;
1100 int iter = 0;
1101 while (1) {
1102 int changed = abpoa_update_kmedoids(dis_matrix, n_seq, to_collect_medoids, &medoids, clu_reads, n_clu_seqs, verbose);
1103 iter++;
1104 if (changed == 0 || iter >= 10) break;
1105 }
1106 int n_clu = 0, n_clustered_reads = 0;
1107 for (int i = 0; i < to_collect_medoids; ++i) {
1108 if (n_clu_seqs[i] >= min_het) n_clu++;
1109 n_clustered_reads += n_clu_seqs[i];
1110 }
1111 if (n_clu != to_collect_medoids || n_clustered_reads < ceil(n_seq * 0.8)) {
1112 to_collect_medoids--;
1113 if (to_collect_medoids < 2) break;
1114 } else {
1115 n_clusters = n_clu;
1116 break;
1117 }
1118 }
1119 // printf("Final clusters: %d\n", n_clusters);
1120 if (n_clusters != 1) {
1121 (*clu_read_ids) = (uint64_t**)_err_malloc(sizeof(uint64_t*) * n_clusters);
1122 int read_id_n = (n_seq-1)/64+1;
1123 for (int i = 0; i < n_clusters; ++i) {
1124 (*clu_read_ids)[i] = (uint64_t*)_err_calloc(read_id_n, sizeof(uint64_t));
1125 for (int j = 0; j < n_clu_seqs[i]; ++j) {
1126 int read_i = clu_reads[i][j];
1127 (*clu_read_ids)[i][read_i/64] |= (1ULL << (read_i & 0x3f));
1128 }
1129 }
1130 }
1131 for (int i = 0; i < max_n_cons; ++i) free(clu_reads[i]);
1132 free(clu_reads); free(n_clu_seqs); free(medoids);
1133 return n_clusters;
1134 }
1135
1136 int abpoa_multip_read_clu_kmedoids(abpoa_graph_t *abg, abpoa_para_t *abpt, int src_id, int sink_id, int n_seq, int m, int max_n_cons, double min_freq, uint64_t ***clu_read_ids, int verbose) {
1137 abpoa_set_msa_rank(abg, src_id, sink_id);
1138 int n_clu;
1139 uint8_t **msa = (uint8_t**)_err_malloc(sizeof(uint8_t*) * n_seq);
1140 int msa_l = abpoa_collect_msa(abg, abpt, msa, n_seq); // abg->node_id_to_msa_rank[sink_id]-1,
1141 // abpoa_collect_allele_freq(msa, msa_l, n_seq, m, verbose);
1142 int min_w = MAX_OF_TWO(2, ceil(n_seq * min_freq));
1143 cand_het_pos_t *cand_het_pos = (cand_het_pos_t*)_err_malloc(msa_l * sizeof(cand_het_pos_t));
1144 int *cand_het_priority = (int*)_err_malloc(msa_l * sizeof(int));
1145 int n_het_pos = abpoa_collect_cand_het_pos(msa, msa_l, n_seq, m, min_w, cand_het_pos, cand_het_priority, verbose);
1146 // print cand_het_pos
1147 // printf("n_het_pos: %d\n", n_het_pos);
1148 // for (int j = 0; j < n_het_pos; ++j) {
1149 // int i = cand_het_priority[j];
1150 // fprintf(stdout, "pos: %d, var_type: %d, count: %d, n_uniq_alles: %d, alle_bases: ", cand_het_pos[i].pos, cand_het_pos[i].var_type, cand_het_pos[i].count, cand_het_pos[i].n_uniq_alles);
1151 // for (int k = 0; k < cand_het_pos[i].n_uniq_alles; ++k) {
1152 // int allele = cand_het_pos[i].alle_bases[k];
1153 // fprintf(stdout, "%c: ", "ACGTN-"[allele]);
1154 // for (int l = 0; l < cand_het_pos[i].n_clu_reads[allele]; ++l) {
1155 // fprintf(stdout, "%d ", cand_het_pos[i].clu_read_ids[allele][l]);
1156 // } fprintf(stdout, "; ");
1157 // } fprintf(stdout, "\n");
1158 // }
1159 // for (int i = 0; i < n_seq; ++i) {
1160 // fprintf(stdout, "read %d: ", i);
1161 // for (int k = 0; k < 3; ++k) {
1162 // int j = cand_het_priority[k];
1163 // int allele_idx = cand_het_pos[j].read_id_to_allele_idx[i];
1164 // if (allele_idx != -1)
1165 // fprintf(stdout, "%c; ", "ACGTN-"[cand_het_pos[j].alle_bases[allele_idx]]);
1166 // } fprintf(stdout, "\n");
1167 // }
1168 if (n_het_pos < 1) n_clu = 1;
1169 else {
1170 int **dis_matrix = abpoa_collect_msa_dis_matrix(msa, msa_l, n_seq, m, cand_het_pos, n_het_pos, verbose);
1171 n_clu = abpoa_clu_reads_kmedoids(cand_het_pos, cand_het_priority, n_het_pos, dis_matrix, n_seq, min_w, max_n_cons, clu_read_ids, verbose);
1172 for (int i = 0; i < n_seq; ++i) free(dis_matrix[i]); free(dis_matrix);
1173 }
1174 for (int i = 0; i < n_het_pos; ++i) {
1175 free(cand_het_pos[i].alle_bases);
1176 for (int j = 0; j < m+1; ++j) free(cand_het_pos[i].clu_read_ids[j]);
1177 free(cand_het_pos[i].clu_read_ids); free(cand_het_pos[i].n_clu_reads); free(cand_het_pos[i].read_id_to_allele_idx);
1178 } free(cand_het_pos); free(cand_het_priority);
1179 for (int i = 0; i < n_seq; ++i) free(msa[i]); free(msa);
1180 return n_clu;
1181 }
1182
1183 // should always do topological sort first, then generate consensus
1184 void abpoa_generate_consensus(abpoa_t *ab, abpoa_para_t *abpt) {
1185 if (ab->abg->is_called_cons == 1) return;
1186 abpoa_graph_t *abg = ab->abg;
1187 if (abg->node_n <= 2) return;
1188 // if (abpt->max_n_cons == 2) {
1189 // abpoa_generate_2cons(ab, abpt);
1190 // return;
1191 // }
1192 int i, *out_degree = (int*)_err_malloc(abg->node_n * sizeof(int));
1193 for (i = 0; i < abg->node_n; ++i) {
1194 out_degree[i] = abg->node[i].out_edge_n;
1195 }
1196
1197 int n_clu, n_seq = ab->abs->n_seq; uint64_t **clu_read_ids=NULL;
1198 int read_ids_n = (n_seq-1)/64+1;
1199
1200 n_clu = 1;
1201 if (abpt->max_n_cons > 1) {
1202 n_clu = abpoa_multip_read_clu_kmedoids(abg, abpt, ABPOA_SRC_NODE_ID, ABPOA_SINK_NODE_ID, n_seq, abpt->m, abpt->max_n_cons, abpt->min_freq, &clu_read_ids, abpt->verbose);
1203 }
1204
1205 abpoa_cons_t *abc = ab->abc;
1206 abpoa_allocate_cons(abc, abg->node_n, ab->abs->n_seq, n_clu);
1207 if (abpt->cons_algrm == ABPOA_HB)
1208 abpoa_heaviest_bundling(abg, abpt, ABPOA_SRC_NODE_ID, ABPOA_SINK_NODE_ID, out_degree, n_clu, read_ids_n, clu_read_ids, abc);
1209 else
1210 abpoa_most_frequent(abg, abpt, ABPOA_SRC_NODE_ID, ABPOA_SINK_NODE_ID, n_clu, read_ids_n, clu_read_ids, abc);
1211 if (n_clu > 1) {
1212 for (i = 0; i < n_clu; ++i) free(clu_read_ids[i]); free(clu_read_ids);
1213 }
1214 abg->is_called_cons = 1; free(out_degree);
1215 }