Debian Codegraph @

src/abpoa_graph.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include "abpoa_align.h"
5 #include "abpoa_seq.h"
6 #include "abpoa_align_simd.h"
7 #include "abpoa_simd.h"
8 #include "kdq.h"
9
10 KDQ_INIT(int)
11 #define kdq_int_t kdq_t(int)
12
13 abpoa_node_t *abpoa_init_node(int n) {
14 abpoa_node_t *node = (abpoa_node_t*)_err_calloc(n, sizeof(abpoa_node_t));
15 return node;
16 }
17
18 void abpoa_set_graph_node(abpoa_graph_t *abg, int node_i) {
19 abg->node[node_i].node_id = node_i;
20 abg->node[node_i].in_edge_n = 0; abg->node[node_i].in_edge_m = 0;
21 abg->node[node_i].out_edge_n = 0; abg->node[node_i].out_edge_m = 0;
22 abg->node[node_i].aligned_node_n = 0; abg->node[node_i].aligned_node_m = 0;
23 abg->node[node_i].n_read = 0; abg->node[node_i].n_span_read = 0; abg->node[node_i].m_read = 0; abg->node[node_i].read_weight = NULL;
24 abg->node[node_i].read_ids_n = 0;
25 }
26
27 void abpoa_free_node(abpoa_node_t *node, int n) {
28 int i, j;
29 for (i = 0; i < n; ++i) {
30 if (node[i].in_edge_m > 0) {
31 free(node[i].in_id); free(node[i].in_edge_weight);
32 }
33 if (node[i].out_edge_m > 0) {
34 free(node[i].out_id); free(node[i].out_edge_weight);
35 if (node[i].read_ids_n > 0) {
36 for (j = 0; j < node[i].out_edge_m; ++j) {
37 free(node[i].read_ids[j]);
38 }
39 free(node[i].read_ids);
40 }
41 }
42 if (node[i].m_read > 0) free(node[i].read_weight);
43 if (node[i].aligned_node_m > 0) free(node[i].aligned_node_id);
44 }
45 free(node);
46 }
47
48 // 0: in_edge, 1: out_edge
49 abpoa_graph_t *abpoa_realloc_graph_edge(abpoa_graph_t *abg, int io, int id, int use_read_ids) {
50 if (io == 0) {
51 int m = abg->node[id].in_edge_m;
52 _uni_realloc(abg->node[id].in_id, abg->node[id].in_edge_n, m, int);
53 _uni_realloc(abg->node[id].in_edge_weight, abg->node[id].in_edge_n, abg->node[id].in_edge_m, int);
54 } else {
55 int edge_m = abg->node[id].out_edge_m;
56 if (edge_m <= 0) {
57 abg->node[id].out_edge_m = MAX_OF_TWO(abg->node[id].out_edge_n, 1);
58 abg->node[id].out_id = (int*)_err_malloc(abg->node[id].out_edge_m * sizeof(int));
59 abg->node[id].out_edge_weight = (int*)_err_malloc(abg->node[id].out_edge_m * sizeof(int));
60 if (use_read_ids || abg->node[id].read_ids_n > 0) {
61 abg->node[id].read_ids = (uint64_t**)_err_malloc(abg->node[id].out_edge_m * sizeof(uint64_t*));
62 if (abg->node[id].read_ids_n > 0) {
63 int i;
64 for (i = 0; i < abg->node[id].out_edge_m; ++i) {
65 abg->node[id].read_ids[i] = (uint64_t*)_err_calloc(abg->node[id].read_ids_n, sizeof(uint64_t));
66 }
67 }
68 }
69 } else if (abg->node[id].out_edge_n >= edge_m) {
70 abg->node[id].out_edge_m = abg->node[id].out_edge_n+1; kroundup32(abg->node[id].out_edge_m);
71 abg->node[id].out_id = (int*)_err_realloc(abg->node[id].out_id, abg->node[id].out_edge_m * sizeof(int));
72 abg->node[id].out_edge_weight = (int*)_err_realloc(abg->node[id].out_edge_weight, abg->node[id].out_edge_m * sizeof(int));
73 if (use_read_ids || abg->node[id].read_ids_n > 0) {
74 abg->node[id].read_ids = (uint64_t**)_err_realloc(abg->node[id].read_ids, abg->node[id].out_edge_m * sizeof(uint64_t*));
75 if (abg->node[id].read_ids_n > 0) {
76 int i;
77 for (i = edge_m; i < abg->node[id].out_edge_m; ++i) {
78 abg->node[id].read_ids[i] = (uint64_t*)_err_calloc(abg->node[id].read_ids_n, sizeof(uint64_t));
79 }
80 }
81 }
82 }
83 }
84 return abg;
85 }
86
87 abpoa_graph_t *abpoa_realloc_graph_node(abpoa_graph_t *abg) {
88 if (abg->node_m <= 0) {
89 abg->node_m = 1;
90 abg->node = (abpoa_node_t*)_err_calloc(1, sizeof(abpoa_node_t));
91 }
92 if (abg->node_n == abg->node_m) {
93 int i;
94 abg->node_m <<= 1;
95 abg->node = (abpoa_node_t*)_err_realloc(abg->node, abg->node_m * sizeof(abpoa_node_t));
96 for (i = abg->node_m >> 1; i < abg->node_m; ++i) {
97 abpoa_set_graph_node(abg, i);
98 }
99 }
100 return abg;
101 }
102
103 abpoa_graph_t *abpoa_init_graph(void) {
104 abpoa_graph_t *abg = (abpoa_graph_t*)_err_malloc(sizeof(abpoa_graph_t));
105 abg->node_n = 2, abg->node_m = 2, abg->index_rank_m = 0;
106 abg->node = abpoa_init_node(2);
107 abg->node[0].node_id = 0; abg->node[1].node_id = 1;
108 abg->node[0].read_ids_n = 0; abg->node[1].read_ids_n = 0;
109 abg->is_topological_sorted = abg->is_called_cons = 0;
110 abg->node_id_to_index = NULL; abg->index_to_node_id = NULL; abg->node_id_to_msa_rank = NULL;
111 abg->node_id_to_max_pos_left = NULL; abg->node_id_to_max_pos_right = NULL; abg->node_id_to_max_remain = NULL;
112 return abg;
113 }
114
115 void abpoa_free_graph(abpoa_graph_t *abg) {
116 if (abg->node_m > 0) abpoa_free_node(abg->node, abg->node_m);
117
118 if (abg->node_n > 0) {
119 free(abg->index_to_node_id);
120 free(abg->node_id_to_index);
121 if (abg->node_id_to_msa_rank) free(abg->node_id_to_msa_rank);
122
123 if (abg->node_id_to_max_pos_left) free(abg->node_id_to_max_pos_left);
124 if (abg->node_id_to_max_pos_right) free(abg->node_id_to_max_pos_right);
125 if (abg->node_id_to_max_remain) free(abg->node_id_to_max_remain);
126 }
127 free(abg);
128 }
129
130 abpoa_cons_t *abpoa_init_cons(void) {
131 abpoa_cons_t *abc = (abpoa_cons_t*)_err_malloc(sizeof(abpoa_cons_t));
132 abc->n_cons = 0; abc->msa_len = 0;
133 abc->clu_n_seq = NULL;
134 abc->cons_len = NULL;
135 abc->cons_node_ids = NULL;
136 abc->cons_base = NULL;
137 abc->msa_base = NULL;
138 abc->cons_cov = NULL;
139 abc->clu_read_ids = NULL;
140 abc->cons_phred_score = NULL;
141 return abc;
142 }
143
144 void abpoa_free_cons(abpoa_cons_t *abc) {
145 int i;
146 if (abc->n_cons > 0) {
147 if (abc->clu_n_seq != NULL) free(abc->clu_n_seq);
148 if (abc->cons_len != NULL) free(abc->cons_len);
149 if (abc->cons_node_ids != NULL) {
150 for (i = 0; i < abc->n_cons; ++i) free(abc->cons_node_ids[i]); free(abc->cons_node_ids);
151 }
152 if (abc->cons_base != NULL) {
153 for (i = 0; i < abc->n_cons; ++i) free(abc->cons_base[i]); free(abc->cons_base);
154 }
155 if (abc->cons_cov != NULL) {
156 for (i = 0; i < abc->n_cons; ++i) free(abc->cons_cov[i]); free(abc->cons_cov);
157 }
158 if (abc->clu_read_ids != NULL) {
159 for (i = 0; i < abc->n_cons; ++i) free(abc->clu_read_ids[i]); free(abc->clu_read_ids);
160 }
161 if (abc->cons_phred_score != NULL) {
162 for (i = 0; i < abc->n_cons; ++i) free(abc->cons_phred_score[i]); free(abc->cons_phred_score);
163 }
164 }
165 if (abc->msa_len > 0) {
166 if (abc->msa_base != NULL) {
167 for (i = 0; i < abc->n_seq+abc->n_cons; ++i) free(abc->msa_base[i]);
168 free(abc->msa_base);
169 }
170 }
171 free(abc);
172 }
173
174 abpoa_t *abpoa_init(void) {
175 abpoa_t *ab = (abpoa_t*)_err_malloc(sizeof(abpoa_t));
176 ab->abg = abpoa_init_graph();
177 ab->abs = abpoa_init_seq();
178 ab->abm = abpoa_init_simd_matrix();
179 ab->abc = abpoa_init_cons();
180 return ab;
181 }
182
183 void abpoa_free(abpoa_t *ab) {
184 abpoa_free_graph(ab->abg);
185 abpoa_free_seq(ab->abs);
186 abpoa_free_simd_matrix(ab->abm);
187 abpoa_free_cons(ab->abc);
188 free(ab);
189 }
190
191 // sort in_id/out_id by edge weight in descending order
192 void abpoa_sort_in_out_ids(abpoa_graph_t *abg) {
193 int i, j, k;
194 int tmp; uint64_t *tmp_read_ids;
195 for (i = 0; i < abg->node_n; ++i) {
196 // in_id
197 for (j = 0; j < abg->node[i].in_edge_n-1; ++j) {
198 for (k = j+1; k < abg->node[i].in_edge_n; ++k) {
199 if (abg->node[i].in_edge_weight[j] < abg->node[i].in_edge_weight[k]) {
200 tmp = abg->node[i].in_id[j]; abg->node[i].in_id[j] = abg->node[i].in_id[k]; abg->node[i].in_id[k] = tmp;
201 tmp = abg->node[i].in_edge_weight[j]; abg->node[i].in_edge_weight[j] = abg->node[i].in_edge_weight[k]; abg->node[i].in_edge_weight[k] = tmp;
202 }
203 }
204 }
205 // out_id
206 for (j = 0; j < abg->node[i].out_edge_n-1; ++j) {
207 for (k = j+1; k < abg->node[i].out_edge_n; ++k) {
208 if (abg->node[i].out_edge_weight[j] < abg->node[i].out_edge_weight[k]) {
209 tmp = abg->node[i].out_id[j]; abg->node[i].out_id[j] = abg->node[i].out_id[k]; abg->node[i].out_id[k] = tmp;
210 tmp = abg->node[i].out_edge_weight[j]; abg->node[i].out_edge_weight[j] = abg->node[i].out_edge_weight[k]; abg->node[i].out_edge_weight[k] = tmp;
211 if (abg->node[i].read_ids_n > 0) {
212 tmp_read_ids = abg->node[i].read_ids[j]; abg->node[i].read_ids[j] = abg->node[i].read_ids[k]; abg->node[i].read_ids[k] = tmp_read_ids;
213 }
214 }
215 }
216 }
217 }
218
219 }
220
221 void abpoa_BFS_set_node_index(abpoa_graph_t *abg, int src_id, int sink_id) {
222 int *id, cur_id, out_id, aligned_id;
223 int index = 0, q_size, new_q_size;
224
225 int *in_degree = (int*)_err_malloc(abg->node_n * sizeof(int));
226 int i, j;
227 for (i = 0; i < abg->node_n; ++i) in_degree[i] = abg->node[i].in_edge_n;
228
229 kdq_int_t *q = kdq_init_int();
230
231 // Breadth-First-Search
232 kdq_push_int(q, src_id); q_size = 1; new_q_size = 0; // node[q.id].in_degree equals 0
233 while (q_size > 0) {
234 if ((id = kdq_shift_int(q)) == 0) err_fatal_simple("Error in queue.");
235 cur_id = *id;
236 abg->index_to_node_id[index] = cur_id;
237 abg->node_id_to_index[cur_id] = index++;
238
239 if (cur_id == sink_id) {
240 kdq_destroy_int(q); free(in_degree);
241 return;
242 }
243 for (i = 0; i < abg->node[cur_id].out_edge_n; ++i) {
244 out_id = abg->node[cur_id].out_id[i];
245 if (--in_degree[out_id] == 0) {
246 for (j = 0; j < abg->node[out_id].aligned_node_n; ++j) {
247 aligned_id = abg->node[out_id].aligned_node_id[j];
248 if (in_degree[aligned_id] != 0) goto next_out_node;
249 }
250 kdq_push_int(q, out_id);
251 ++new_q_size;
252 for (j = 0; j < abg->node[out_id].aligned_node_n; ++j) {
253 aligned_id = abg->node[out_id].aligned_node_id[j];
254 kdq_push_int(q, aligned_id);
255 ++new_q_size;
256 }
257 }
258 next_out_node:;
259 }
260 if (--q_size == 0) {
261 q_size = new_q_size;
262 new_q_size = 0;
263 }
264 }
265 err_fatal_simple("Failed to set node index.");
266 }
267
268 void abpoa_BFS_set_node_remain(abpoa_graph_t *abg, int src_id, int sink_id) {
269 int *id, cur_id, i, out_id, in_id;
270
271 int *out_degree = (int*)_err_malloc(abg->node_n * sizeof(int));
272 for (i = 0; i < abg->node_n; ++i) {
273 out_degree[i] = abg->node[i].out_edge_n;
274 abg->node_id_to_max_remain[i] = 0;
275 }
276
277 kdq_int_t *q = kdq_init_int();
278
279 // Breadth-First-Search
280 kdq_push_int(q, sink_id); // node[q.id].in_degree equals 0
281 abg->node_id_to_max_remain[sink_id] = -1; // XXX not 0
282 while ((id = kdq_shift_int(q)) != 0) {
283 cur_id = *id;
284
285 // all out_id of cur_id have beed visited
286 // max weight out_id
287 if (cur_id != sink_id) {
288 int max_w=-1, max_id=-1;
289 for (i = 0; i < abg->node[cur_id].out_edge_n; ++i) {
290 out_id = abg->node[cur_id].out_id[i];
291 if (abg->node[cur_id].out_edge_weight[i] > max_w) {
292 max_w = abg->node[cur_id].out_edge_weight[i];
293 max_id = out_id;
294 }
295 }
296 abg->node_id_to_max_remain[cur_id] = abg->node_id_to_max_remain[max_id] + 1;
297 // fprintf(stderr, "%d -> %d\n", abg->node_id_to_index[cur_id], abg->node_id_to_max_remain[cur_id]);
298 }
299 if (cur_id == src_id) {
300 kdq_destroy_int(q); free(out_degree);
301 return;
302 }
303 for (i = 0; i < abg->node[cur_id].in_edge_n; ++i) {
304 in_id = abg->node[cur_id].in_id[i];
305 if (--out_degree[in_id] == 0) kdq_push_int(q, in_id);
306 }
307 }
308 err_fatal_simple("Failed to set node remain.");
309 }
310
311 // TODO
312 void abpoa_merge_nodes(abpoa_graph_t *abg, int src_id, int sink_id) {
313 if (abg->node_n <= 0) {
314 err_func_format_printf(__func__, "Empty graph.\n");
315 return;
316 }
317 }
318
319 // 1. index_to_node_id
320 // 2. node_id_to_index
321 // 3. node_id_to_rank
322 void abpoa_topological_sort(abpoa_graph_t *abg, abpoa_para_t *abpt) {
323 if (abg->node_n <= 0) {
324 err_func_format_printf(__func__, "Empty graph.\n");
325 return;
326 }
327 int node_n = abg->node_n;
328 if (node_n > abg->index_rank_m) {
329 abg->index_rank_m = node_n; kroundup32(abg->index_rank_m);
330 // fprintf(stderr, "node_n: %d, index_rank_m: %d\n", node_n, abg->index_rank_m);
331 abg->index_to_node_id = (int*)_err_realloc(abg->index_to_node_id, abg->index_rank_m * sizeof(int));
332 abg->node_id_to_index = (int*)_err_realloc(abg->node_id_to_index, abg->index_rank_m * sizeof(int));
333 if (abpt->out_msa || abpt->max_n_cons > 1 || abpt->cons_algrm == ABPOA_MF)
334 abg->node_id_to_msa_rank = (int*)_err_realloc(abg->node_id_to_msa_rank, abg->index_rank_m * sizeof(int));
335 if (abpt->wb >= 0) {
336 abg->node_id_to_max_pos_left = (int*)_err_realloc(abg->node_id_to_max_pos_left, abg->index_rank_m * sizeof(int));
337 abg->node_id_to_max_pos_right = (int*)_err_realloc(abg->node_id_to_max_pos_right, abg->index_rank_m * sizeof(int));
338 abg->node_id_to_max_remain = (int*)_err_realloc(abg->node_id_to_max_remain, abg->index_rank_m * sizeof(int));
339 } else if (abpt->zdrop > 0) {
340 abg->node_id_to_max_remain = (int*)_err_realloc(abg->node_id_to_max_remain, abg->index_rank_m * sizeof(int));
341 }
342 }
343 // start from ABPOA_SRC_NODE_ID to ABPOA_SINK_NODE_ID
344 abpoa_BFS_set_node_index(abg, ABPOA_SRC_NODE_ID, ABPOA_SINK_NODE_ID);
345 abpoa_sort_in_out_ids(abg);
346 // init min/max rank
347 if (abpt->wb >= 0) {
348 int i;
349 for (i = 0; i < node_n; ++i) {
350 abg->node_id_to_max_pos_right[i] = 0;
351 abg->node_id_to_max_pos_left[i] = node_n;
352 }
353 abpoa_BFS_set_node_remain(abg, ABPOA_SRC_NODE_ID, ABPOA_SINK_NODE_ID);
354 } else if (abpt->zdrop > 0)
355 abpoa_BFS_set_node_remain(abg, ABPOA_SRC_NODE_ID, ABPOA_SINK_NODE_ID);
356 abg->is_topological_sorted = 1;
357 }
358
359 void abpoa_DFS_set_msa_rank(abpoa_graph_t *abg, int src_id, int sink_id, int *in_degree) {
360 // fprintf(stderr, "node_n: %d, m: %d\n", abg->node_n, abg->index_rank_m);
361 if (abg->node_n > abg->index_rank_m) {
362 int m = abg->node_n; kroundup32(m);
363 abg->node_id_to_msa_rank = (int*)_err_realloc(abg->node_id_to_msa_rank, m * sizeof(int));
364 }
365 int *id, cur_id, i, j, out_id, aligned_id;
366 int msa_rank = 0;
367 kdq_int_t *q = kdq_init_int();
368
369 // Depth-First-Search
370 kdq_push_int(q, src_id); // node[q.id].in_degree equals 0
371 abg->node_id_to_msa_rank[src_id] = -1;
372 // printf("tot_node_n: %d, node_m: %d\n", abg->node_n, abg->node_m);
373
374 while((id = kdq_pop_int(q)) != 0) {
375 cur_id = *id;
376 if (abg->node_id_to_msa_rank[cur_id] < 0) {
377 abg->node_id_to_msa_rank[cur_id] = msa_rank;
378 for (i = 0; i < abg->node[cur_id].aligned_node_n; ++i) {
379 aligned_id = abg->node[cur_id].aligned_node_id[i];
380 abg->node_id_to_msa_rank[aligned_id] = msa_rank;
381 }
382 msa_rank++;
383 }
384
385 if (cur_id == sink_id) {
386 kdq_destroy_int(q);
387 abg->is_set_msa_rank = 1;
388 return;
389 }
390 for (i = 0; i < abg->node[cur_id].out_edge_n; ++i) {
391 out_id = abg->node[cur_id].out_id[i];
392 if (--in_degree[out_id] == 0) {
393 for (j = 0; j < abg->node[out_id].aligned_node_n; ++j) {
394 aligned_id = abg->node[out_id].aligned_node_id[j];
395 if (in_degree[aligned_id] != 0) goto next_out_node;
396 }
397 kdq_push_int(q, out_id);
398 abg->node_id_to_msa_rank[out_id] = -1;
399 for (j = 0; j < abg->node[out_id].aligned_node_n; ++j) {
400 aligned_id = abg->node[out_id].aligned_node_id[j];
401 kdq_push_int(q, aligned_id);
402 // printf("aln_id: %d\n", aligned_id);
403 abg->node_id_to_msa_rank[aligned_id] = -1;
404 }
405 }
406 next_out_node:;
407 }
408 }
409 err_fatal_simple("Error in set_msa_rank.\n");
410 }
411
412 void abpoa_set_msa_rank(abpoa_graph_t *abg, int src_id, int sink_id) {
413 if (abg->is_set_msa_rank == 0) {
414 int i, *in_degree = (int*)_err_malloc(abg->node_n * sizeof(int));
415 for (i = 0; i < abg->node_n; ++i) in_degree[i] = abg->node[i].in_edge_n;
416 abpoa_DFS_set_msa_rank(abg, src_id, sink_id, in_degree);
417 free(in_degree);
418 }
419 }
420
421 int abpoa_get_node_weight(abpoa_graph_t *abg, int node_id) {
422 int i, weight = 0;
423 for (i = 0; i < abg->node[node_id].out_edge_n; ++i) {
424 weight += abg->node[node_id].out_edge_weight[i];
425 }
426 return weight;
427 }
428
429 int abpoa_get_incre_path_score(abpoa_graph_t *abg, int node_id, int in_id_idx) {
430 if (in_id_idx < 0 || in_id_idx >= abg->node[node_id].in_edge_n) err_fatal(__func__, "Unexpected in_id_idx: %d.", in_id_idx);
431 int pre_node_id = abg->node[node_id].in_id[in_id_idx];
432 int node_w = abpoa_get_node_weight(abg, pre_node_id);
433 int edge_w = abg->node[node_id].in_edge_weight[in_id_idx];
434 if (node_w == 0 || edge_w == 0) return 0;
435 int score = round(log((double)edge_w/(double)node_w));
436 return MAX_OF_TWO(score, -20); // -20 is the minimum score
437 }
438
439 int abpoa_get_aligned_id(abpoa_graph_t *abg, int node_id, uint8_t base) {
440 int i, aln_id;
441 abpoa_node_t *node = abg->node;
442 for (i = 0; i < node[node_id].aligned_node_n; ++i) {
443 aln_id = node[node_id].aligned_node_id[i];
444 if (node[aln_id].base == base)
445 return aln_id;
446 }
447 return -1;
448 }
449
450 void abpoa_add_graph_aligned_node1(abpoa_node_t *node, int aligned_id) {
451 _uni_realloc(node->aligned_node_id, node->aligned_node_n, node->aligned_node_m, int);
452 node->aligned_node_id[node->aligned_node_n++] = aligned_id;
453 }
454
455 void abpoa_add_graph_aligned_node(abpoa_graph_t *abg, int node_id, int aligned_id) {
456 int i; abpoa_node_t *node = abg->node;
457 for (i = 0; i < node[node_id].aligned_node_n; ++i) {
458 abpoa_add_graph_aligned_node1(node + node[node_id].aligned_node_id[i], aligned_id);
459 abpoa_add_graph_aligned_node1(node + aligned_id, node[node_id].aligned_node_id[i]);
460 }
461 abpoa_add_graph_aligned_node1(abg->node + node_id, aligned_id);
462 abpoa_add_graph_aligned_node1(abg->node + aligned_id, node_id);
463 }
464
465 void abpoa_set_read_id(uint64_t *read_ids, int read_id) {
466 int n = read_id / 64;
467 uint64_t one = 1; int b = read_id & 0x3f;
468 read_ids[n] |= (one << b);
469 }
470
471 int abpoa_add_graph_node(abpoa_graph_t *abg, uint8_t base) {
472 int node_id = abg->node_n;
473 abpoa_realloc_graph_node(abg);
474 // add node
475 abg->node[node_id].base = base;
476 ++abg->node_n;
477 return node_id;
478 }
479
480 int abpoa_add_graph_edge(abpoa_graph_t *abg, int from_id, int to_id, int check_edge, int w, uint8_t add_read_id, uint8_t add_read_weight, int read_id, int read_ids_n, int tot_read_n) {
481 int ret = 1;
482 if (from_id < 0 || from_id >= abg->node_n || to_id < 0 || to_id >= abg->node_n) err_fatal(__func__, "node_n: %d\tfrom_id: %d\tto_id: %d.", abg->node_n, from_id, to_id);
483 // fprintf(stderr, "weigth: %d\n", w);
484 int out_edge_n = abg->node[from_id].out_edge_n;
485 int edge_exist = 0;
486 int out_edge_i = -1;
487 if (check_edge) {
488 int i;
489 // in edge
490 for (i = 0; i < abg->node[to_id].in_edge_n; ++i) {
491 if (abg->node[to_id].in_id[i] == from_id) { // edge exists
492 abg->node[to_id].in_edge_weight[i] += w; // update weight on existing edge
493 // update label id
494 break;
495 }
496 }
497 // out edge
498 for (i = 0; i < out_edge_n; ++i) {
499 if (abg->node[from_id].out_id[i] == to_id) { // edge exists
500 abg->node[from_id].out_edge_weight[i] += w; // update weight on existing edge
501 // update label id
502 edge_exist = 1;
503 out_edge_i = i;
504 break;
505 }
506 }
507 }
508
509 // add edge
510 if (edge_exist == 0) {
511 /// in edge
512 abpoa_realloc_graph_edge(abg, 0, to_id, 0);
513 abg->node[to_id].in_id[abg->node[to_id].in_edge_n] = from_id;
514 abg->node[to_id].in_edge_weight[abg->node[to_id].in_edge_n] = w;
515 ++abg->node[to_id].in_edge_n;
516 /// out edge
517 abpoa_realloc_graph_edge(abg, 1, from_id, add_read_id);
518 abg->node[from_id].out_id[out_edge_n] = to_id;
519 abg->node[from_id].out_edge_weight[out_edge_n] = w; // initial weight for new edge
520 out_edge_i = out_edge_n;
521 ++abg->node[from_id].out_edge_n;
522 }
523
524 // add read_id to out edge
525 if (add_read_id) {
526 if (out_edge_i < 0) err_fatal_simple("No edge found.");
527 if (read_ids_n <= 0) err_fatal(__func__, "Unexpected read_ids_n: %d.", read_ids_n);
528 int i, j;
529 abpoa_node_t *from_node = abg->node + from_id;
530 if (from_node->read_ids_n == 0) {
531 for (i = 0; i < from_node->out_edge_m; ++i) {
532 from_node->read_ids[i] = (uint64_t*)_err_calloc(read_ids_n, sizeof(uint64_t));
533 }
534 from_node->read_ids_n = read_ids_n;
535 } else if (from_node->read_ids_n < read_ids_n) {
536 // reallocate from_node->read_ids
537 for (i = 0; i < from_node->out_edge_m; ++i) {
538 from_node->read_ids[i] = (uint64_t*)_err_realloc(from_node->read_ids[i], read_ids_n * sizeof(uint64_t));
539 for (j = from_node->read_ids_n; j < read_ids_n; ++j) from_node->read_ids[i][j] = 0;
540 }
541 from_node->read_ids_n = read_ids_n;
542 }
543 abpoa_set_read_id(from_node->read_ids[out_edge_i], read_id);
544 }
545 abg->node[from_id].n_read += 1;
546 if (add_read_weight) {
547 if (tot_read_n > abg->node[from_id].m_read) {
548 abg->node[from_id].read_weight = (int*)_err_realloc(abg->node[from_id].read_weight, tot_read_n * sizeof(int));
549 int i;
550 for (i = abg->node[from_id].m_read; i < tot_read_n; ++i) abg->node[from_id].read_weight[i] = 0;
551 abg->node[from_id].m_read = tot_read_n;
552 }
553 abg->node[from_id].read_weight[read_id] = w;
554 }
555 return ret;
556 }
557
558 // XXX n_span_read should be updated after adding all edges/nodes in the graph, right before topological sort/consensus calling
559 void abpoa_update_node_n_span_reads(abpoa_graph_t *abg, int src_id, int sink_id, int inc_both_ends) {
560 int src_index = abg->node_id_to_index[src_id];
561 int sink_index = abg->node_id_to_index[sink_id];
562 int i;
563 for (i = src_index+1; i < sink_index; ++i) {
564 int id = abg->index_to_node_id[i];
565 abg->node[id].n_span_read += 1;
566 }
567 if (inc_both_ends) {
568 abg->node[src_id].n_span_read += 1;
569 abg->node[sink_id].n_span_read += 1;
570 }
571 }
572
573 void abpoa_add_graph_sequence(abpoa_graph_t *abg, abpoa_para_t *abpt, uint8_t *seq, int *weight, int seq_l, int *qpos_to_node_id, int start, int end, uint8_t add_read_id, uint8_t add_read_weight, int read_id, int read_ids_n, int tot_read_n) {
574 if (seq_l <= 0) return;
575 if (start >= seq_l || end <= start) err_fatal(__func__, "seq_l: %d\tstart: %d\tend: %d.", seq_l, start, end);
576 if (end > seq_l) end = seq_l;
577
578 int i, last_node_id, cur_node_id;
579 last_node_id = ABPOA_SRC_NODE_ID;
580 for (i = start; i < end; ++i) {
581 cur_node_id = abpoa_add_graph_node(abg, seq[i]);
582 if (qpos_to_node_id) qpos_to_node_id[i] = cur_node_id;
583 abpoa_add_graph_edge(abg, last_node_id, cur_node_id, 0, weight[i], add_read_id, add_read_weight, read_id, read_ids_n, tot_read_n);
584 abg->node[cur_node_id].n_span_read = abg->node[last_node_id].n_span_read;
585 last_node_id = cur_node_id;
586 }
587
588 abpoa_add_graph_edge(abg, last_node_id, ABPOA_SINK_NODE_ID, 0, weight[seq_l-1], add_read_id, add_read_weight, read_id, read_ids_n, tot_read_n);
589 abg->is_called_cons = abg->is_set_msa_rank = abg->is_topological_sorted = 0;
590 abpoa_topological_sort(abg, abpt);
591 abpoa_update_node_n_span_reads(abg, ABPOA_SRC_NODE_ID, ABPOA_SINK_NODE_ID, 1);
592 // abpoa_merge_nodes(abg, ABPOA_SRC_NODE_ID, ABPOA_SINK_NODE_ID);
593 }
594
595 int is_full_upstream_subgraph(abpoa_graph_t *abg, int up_index, int down_index, int beg_index, int end_index) {
596 int i, j, id, in_id;
597 int min_index = MIN_OF_TWO(up_index, beg_index);
598 int max_index = MAX_OF_TWO(down_index, end_index);
599 for (i = up_index+1; i <= down_index; ++i) {
600 id = abg->index_to_node_id[i];
601 for (j = 0; j < abg->node[id].in_edge_n; ++j) {
602 in_id = abg->node[id].in_id[j];
603 if (abg->node_id_to_index[in_id] < min_index || abg->node_id_to_index[in_id] > max_index) return 0;
604 }
605 }
606 return 1;
607 }
608
609 int abpoa_upstream_index(abpoa_graph_t *abg, int beg_index, int end_index) {
610 int min_index, in_index, i, j, node_id, in_id;
611
612 while (1) {
613 min_index = beg_index;
614 for (i = beg_index; i <= end_index; ++i) {
615 node_id = abg->index_to_node_id[i];
616 for (j = 0; j < abg->node[node_id].in_edge_n; ++j) {
617 in_id = abg->node[node_id].in_id[j];
618 in_index = abg->node_id_to_index[in_id];
619 min_index = MIN_OF_TWO(min_index, in_index);
620 }
621 }
622 if (is_full_upstream_subgraph(abg, min_index, beg_index, beg_index, end_index)) {
623 return min_index;
624 } else {
625 end_index = beg_index;
626 beg_index = min_index;
627 }
628 }
629 }
630
631 int is_full_downstream_subgraph(abpoa_graph_t *abg, int up_index, int down_index) {
632 int i, j, id, out_id;
633 for (i = up_index; i < down_index; ++i) {
634 id = abg->index_to_node_id[i];
635 for (j = 0; j < abg->node[id].out_edge_n; ++j) {
636 out_id = abg->node[id].out_id[j];
637 if (abg->node_id_to_index[out_id] > down_index) return 0;
638 }
639 }
640 return 1;
641 }
642
643 int abpoa_downstream_index(abpoa_graph_t *abg, int beg_index, int end_index) {
644 int max_index, out_index, i, j, node_id, out_id;
645
646 while (1) {
647 max_index = end_index;
648 for (i = beg_index; i <= end_index; ++i) {
649 node_id = abg->index_to_node_id[i];
650 for (j = 0; j < abg->node[node_id].out_edge_n; ++j) {
651 out_id = abg->node[node_id].out_id[j];
652 out_index = abg->node_id_to_index[out_id];
653 max_index = MAX_OF_TWO(max_index, out_index);
654 }
655 }
656 if (is_full_upstream_subgraph(abg, end_index, max_index, beg_index, end_index)) {
657 return max_index;
658 } else {
659 beg_index = end_index;
660 end_index = max_index;
661 }
662 }
663 }
664
665 // exc_beg | inc_beg ... inc_end | exc_end
666 void abpoa_subgraph_nodes(abpoa_t *ab, abpoa_para_t *abpt, int exc_beg0, int exc_end0, int *exc_beg, int *exc_end) {
667 abpoa_graph_t *abg = ab->abg;
668 if (ab->abg->is_topological_sorted == 0) abpoa_topological_sort(abg, abpt);
669 int inc_beg_index = abg->node_id_to_index[exc_beg0], inc_end_index = abg->node_id_to_index[exc_end0];
670
671 int exc_beg_index = abpoa_upstream_index(abg, inc_beg_index, inc_end_index);
672 int exc_end_index = abpoa_downstream_index(abg, inc_beg_index, inc_end_index);
673
674 if (exc_beg_index < 0 || exc_end_index >= abg->node_n)
675 err_fatal_simple("Error in subgraph_nodes");
676 *exc_beg = abg->index_to_node_id[exc_beg_index];
677 *exc_end = abg->index_to_node_id[exc_end_index];
678 }
679
680 // fusion stratergy :
681 // 1. Match: merge to one node
682 // 2. Mismatch: check if B is identical to A' aligned nodes, then merge to node; if not, add node
683 // 3. Insertion: add node
684 // 4. Deletion: nothing
685 // 5. Clipping: add node
686 // 6. For all first/last node, link to virtual start/end node
687
688 // inc_both_ends: set as 1 to add weight for edge between beg_node_id/end_node_id and internal node
689 int abpoa_add_subgraph_alignment(abpoa_t *ab, abpoa_para_t *abpt, int beg_node_id, int end_node_id, uint8_t *seq, int *_weight, int seq_l, int *qpos_to_node_id, abpoa_res_t res, int read_id, int tot_read_n, int inc_both_ends) {
690 abpoa_graph_t *abg = ab->abg;
691 int n_cigar = res.n_cigar; abpoa_cigar_t *abpoa_cigar = res.graph_cigar;
692 int read_ids_n = 1 + ((tot_read_n-1) >> 6);
693 uint8_t add_read_id = abpt->use_read_ids, add_read_weight = abpt->use_qv & (abpt->max_n_cons>1), add;
694 int i, *weight;
695 if (_weight == NULL) {
696 weight = (int*)_err_malloc(seq_l * sizeof(int));
697 for (i = 0; i < seq_l; ++i) weight[i] = 1;
698 } else weight = _weight;
699
700 if (abg->node_n == 2) { // empty graph
701 abpoa_add_graph_sequence(abg, abpt, seq, weight, seq_l, qpos_to_node_id, 0, seq_l, add_read_id, add_read_weight, read_id, read_ids_n, tot_read_n);
702 if (_weight == NULL) free(weight);
703 return 0;
704 } else {
705 if (abg->node_n < 2) {
706 err_fatal(__func__, "Graph node: %d.", abg->node_n);
707 } else if (n_cigar == 0) {
708 if (_weight == NULL) free(weight);
709 return 0;
710 //err_fatal(__func__, "Empty graph cigar.");
711 }
712 }
713 // normal graph, normal graph_cigar
714 int j; int op, len;
715 int node_id, query_id=-1, last_new = 0, last_id = beg_node_id, new_id, aligned_id;
716
717
718 for (i = 0; i < n_cigar; ++i) {
719 op = abpoa_cigar[i] & 0xf;
720 if (op == ABPOA_CMATCH) {
721 node_id = (abpoa_cigar[i] >> 34) & 0x3fffffff;
722 query_id++; // = (abpoa_cigar[i] >> 4) & 0x3fffffff;
723 if (abg->node[node_id].base != seq[query_id]) { // mismatch
724 // check if query base is identical to node_id's aligned node
725 if ((aligned_id = abpoa_get_aligned_id(abg, node_id, seq[query_id])) != -1) {
726 if (last_id != beg_node_id || inc_both_ends) add = 1; else add = 0;
727 abpoa_add_graph_edge(abg, last_id, aligned_id, 1-last_new, weight[query_id], add_read_id&add, add_read_weight, read_id, read_ids_n, tot_read_n);
728 if (add == 0) abg->node[last_id].n_read--;
729 last_id = aligned_id; last_new = 0;
730 } else {
731 new_id = abpoa_add_graph_node(abg, seq[query_id]);
732 if (last_id != beg_node_id || inc_both_ends) add = 1; else add = 0;
733 abpoa_add_graph_edge(abg, last_id, new_id, 0, weight[query_id], add_read_id&add, add_read_weight, read_id, read_ids_n, tot_read_n);
734 abg->node[new_id].n_span_read = abg->node[last_id].n_span_read;
735 if (add == 0) abg->node[last_id].n_read--;
736 last_id = new_id; last_new = 1;
737 // add new_id to node_id's aligned node
738 abpoa_add_graph_aligned_node(abg, node_id, new_id);
739 }
740 } else { // match
741 if (last_id != beg_node_id || inc_both_ends) add = 1; else add = 0;
742 abpoa_add_graph_edge(abg, last_id, node_id, 1-last_new, weight[query_id], add_read_id&add, add_read_weight, read_id, read_ids_n, tot_read_n);
743 if (add == 0) abg->node[last_id].n_read--;
744 last_id = node_id; last_new = 0;
745 }
746 if (qpos_to_node_id) qpos_to_node_id[query_id] = last_id;
747 } else if (op == ABPOA_CINS || op == ABPOA_CSOFT_CLIP || op == ABPOA_CHARD_CLIP) {
748 len = (abpoa_cigar[i] >> 4) & 0x3fffffff;
749 query_id+=len; // = (abpoa_cigar[i] >> 34) & 0x3fffffff;
750 for (j = len-1; j >= 0; --j) { // XXX use dynamic id, instead of static query_id
751 new_id = abpoa_add_graph_node(abg, seq[query_id-j]);
752 if (last_id != beg_node_id || inc_both_ends) add = 1; else add = 0;
753 abpoa_add_graph_edge(abg, last_id, new_id, 0, weight[query_id-j], add_read_id&add, add_read_weight, read_id, read_ids_n, tot_read_n);
754 abg->node[new_id].n_span_read = abg->node[last_id].n_span_read;
755 if (add == 0) abg->node[last_id].n_read--;
756 last_id = new_id; last_new = 1;
757 if (qpos_to_node_id) qpos_to_node_id[query_id-j] = last_id;
758 }
759 } else if (op == ABPOA_CDEL) {
760 // nothing;
761 continue;
762 }
763 }
764 // if (inc_both_ends) add = 1; else add = 0; XXX end_node_id is always excluded when adding weight
765 // abpoa_add_graph_edge(abg, last_id, end_node_id, 1-last_new, w, add_read_id&add, read_id, read_ids_n);
766 abpoa_add_graph_edge(abg, last_id, end_node_id, 1-last_new, weight[seq_l-1], add_read_id, add_read_weight, read_id, read_ids_n, tot_read_n);
767 abg->is_called_cons = abg->is_set_msa_rank = abg->is_topological_sorted = 0;
768 abpoa_topological_sort(abg, abpt);
769 // update node.n_read
770 abpoa_update_node_n_span_reads(abg, beg_node_id, end_node_id, inc_both_ends);
771 // abpoa_merge_nodes(abg, beg_node_id, end_node_id);
772 if (_weight == NULL) free(weight);
773 return 0;
774 }
775
776 int abpoa_add_graph_alignment(abpoa_t *ab, abpoa_para_t *abpt, uint8_t *seq, int *weight, int seq_l, int *qpos_to_node_id, abpoa_res_t res, int read_id, int tot_read_n, int inc_both_ends) {
777 return abpoa_add_subgraph_alignment(ab, abpt, ABPOA_SRC_NODE_ID, ABPOA_SINK_NODE_ID, seq, weight, seq_l, qpos_to_node_id, res, read_id, tot_read_n, inc_both_ends);
778 }
779
780 // reset allocated memery everytime init the graph
781 // * node
782 // * index_to_node_id/node_id_to_index/node_id_to_max_remain, max_pos_left/right
783 void abpoa_reset(abpoa_t *ab, abpoa_para_t *abpt, int qlen) {
784 abpoa_graph_t *abg = ab->abg;
785 int i, j, k, node_m;
786 abg->is_topological_sorted = abg->is_called_cons = 0;
787 for (i = 0; i < abg->node_n; ++i) {
788 for (j = 0; j < abg->node[i].out_edge_n; ++j) {
789 for (k = 0; k < abg->node[i].read_ids_n; ++k) abg->node[i].read_ids[j][k] = 0;
790 }
791 abg->node[i].in_edge_n = abg->node[i].out_edge_n = abg->node[i].aligned_node_n = 0;
792 abg->node[i].n_read = 0; abg->node[i].n_span_read = 0;
793
794 }
795 abg->node_n = 2;
796 if (qlen+2 > abg->node_m) {
797 node_m = qlen+2; kroundup32(node_m);
798 abg->node = (abpoa_node_t*)_err_realloc(abg->node, node_m * sizeof(abpoa_node_t));
799 for (i = abg->node_m; i < node_m; ++i)
800 abpoa_set_graph_node(abg, i);
801 abg->node_m = abg->index_rank_m = node_m;
802 abg->index_to_node_id = (int*)_err_realloc(abg->index_to_node_id, node_m * sizeof(int));
803 abg->node_id_to_index = (int*)_err_realloc(abg->node_id_to_index, node_m * sizeof(int));
804 if (abpt->out_msa || abpt->max_n_cons > 1 || abpt->cons_algrm == ABPOA_MF)
805 abg->node_id_to_msa_rank = (int*)_err_realloc(abg->node_id_to_msa_rank, node_m * sizeof(int));
806 if (abpt->wb >= 0) {
807 abg->node_id_to_max_pos_left = (int*)_err_realloc(abg->node_id_to_max_pos_left, node_m * sizeof(int));
808 abg->node_id_to_max_pos_right = (int*)_err_realloc(abg->node_id_to_max_pos_right, node_m * sizeof(int));
809 abg->node_id_to_max_remain = (int*)_err_realloc(abg->node_id_to_max_remain, node_m * sizeof(int));
810 } else if (abpt->zdrop > 0) {
811 abg->node_id_to_max_remain = (int*)_err_realloc(abg->node_id_to_max_remain, node_m * sizeof(int));
812 }
813 }
814 // fprintf(stderr, "qlen: %d, node_n: %d, node_m: %d\n", qlen, abg->node_n, abg->node_m);
815 // reset abs
816 ab->abs->n_seq = 0;
817 // reset cons
818 abpoa_cons_t *abc = ab->abc;
819 if (abc->n_cons > 0) {
820 if (abc->clu_n_seq != NULL) free(abc->clu_n_seq);
821 if (abc->cons_len != NULL) free(abc->cons_len);
822 if (abc->cons_node_ids != NULL) {
823 for (i = 0; i < abc->n_cons; ++i) free(abc->cons_node_ids[i]); free(abc->cons_node_ids);
824 }
825 if (abc->cons_base != NULL) {
826 for (i = 0; i < abc->n_cons; ++i) free(abc->cons_base[i]); free(abc->cons_base);
827 }
828 if (abc->cons_cov != NULL) {
829 for (i = 0; i < abc->n_cons; ++i) free(abc->cons_cov[i]); free(abc->cons_cov);
830 }
831 if (abc->clu_read_ids != NULL) {
832 for (i = 0; i < abc->n_cons; ++i) free(abc->clu_read_ids[i]); free(abc->clu_read_ids);
833 }
834 if (abc->cons_phred_score != NULL) {
835 for (i = 0; i < abc->n_cons; ++i) free(abc->cons_phred_score[i]); free(abc->cons_phred_score);
836 }
837 }
838 if (abc->msa_len > 0) {
839 if (abc->msa_base != NULL) {
840 for (i = 0; i < abc->n_seq+abc->n_cons; ++i) free(abc->msa_base[i]);
841 free(abc->msa_base);
842 }
843 }
844 abc->n_seq = abc->n_cons = abc->msa_len = 0;
845 }
846
847 void abpoa_clean_msa_cons(abpoa_t *ab) { // reset cons + msa
848 abpoa_cons_t *abc = ab->abc;
849 int i;
850 if (abc->n_cons > 0) {
851 if (abc->clu_n_seq != NULL) free(abc->clu_n_seq);
852 if (abc->cons_len != NULL) free(abc->cons_len);
853 if (abc->cons_node_ids != NULL) {
854 for (i = 0; i < abc->n_cons; ++i) free(abc->cons_node_ids[i]); free(abc->cons_node_ids);
855 }
856 if (abc->cons_base != NULL) {
857 for (i = 0; i < abc->n_cons; ++i) free(abc->cons_base[i]); free(abc->cons_base);
858 }
859 if (abc->cons_cov != NULL) {
860 for (i = 0; i < abc->n_cons; ++i) free(abc->cons_cov[i]); free(abc->cons_cov);
861 }
862 if (abc->clu_read_ids != NULL) {
863 for (i = 0; i < abc->n_cons; ++i) free(abc->clu_read_ids[i]); free(abc->clu_read_ids);
864 }
865 if (abc->cons_phred_score != NULL) {
866 for (i = 0; i < abc->n_cons; ++i) free(abc->cons_phred_score[i]); free(abc->cons_phred_score);
867 }
868 }
869 if (abc->msa_len > 0) {
870 if (abc->msa_base != NULL) {
871 for (i = 0; i < abc->n_seq+abc->n_cons; ++i) free(abc->msa_base[i]);
872 free(abc->msa_base);
873 }
874 }
875 abc->n_seq = abc->n_cons = abc->msa_len = 0;
876 }