Debian Codegraph @

src/abpoa_align.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include "abpoa.h"
6 #include "abpoa_align_simd.h"
7 #include "abpoa_align.h"
8 #include "abpoa_seq.h"
9 #include "abpoa_output.h"
10 #include "utils.h"
11 #include "abpoa_seed.h"
12
13 void gen_simple_mat(abpoa_para_t *abpt) {
14 int m = abpt->m, i, j;
15 int match = abpt->match < 0 ? -abpt->match : abpt->match;
16 int mismatch = abpt->mismatch > 0? -abpt->mismatch : abpt->mismatch;
17 for (i = 0; i < m - 1; ++i) {
18 for (j = 0; j < m - 1; ++j)
19 abpt->mat[i * m + j] = i == j ? match : mismatch;
20 abpt->mat[i * m + m - 1] = 0;
21 }
22 for (j = 0; j < m; ++j)
23 abpt->mat[(m - 1) * m + j] = 0;
24 abpt->max_mat = match;
25 abpt->min_mis = -mismatch;
26 }
27
28 extern char ab_nt4_table[256];
29 extern char ab_nt256_table[256];
30 extern char ab_aa26_table[256];
31 extern char ab_aa256_table[256];
32 extern char ab_char26_table[256];
33 extern char ab_char256_table[256];
34
35 void parse_mat_first_line(char *l, int *order) {
36 int i, n;
37 for (i = n = 0; l[i]; ++i) {
38 if (isspace(l[i])) continue;
39 order[n++] = ab_char26_table[(int)l[i]];
40 }
41 }
42
43 void parse_mat_score_line(char *l, int *order, int m, int *mat) {
44 int n, is_base=1, _i=-1; long s; char *str = l, *pEnd=NULL;
45 for (n = 0; *str; ++str) {
46 if (!isalpha(*str) && !isdigit(*str) && *str != '+' && *str != '-') continue;
47 if (is_base) { // get base
48 _i = ab_char26_table[(int)*str];
49 if (_i >= m) err_fatal(__func__, "Unknown base: \"%c\" (%d).\n", *str, _i);
50 is_base = 0;
51 } else { // get score
52 if (n == m)
53 err_fatal_simple("Too many scores in matrix.\n");
54 s = strtol(str, &pEnd, 10);
55 str = pEnd;
56 mat[_i *m + order[n]] = s;
57 n++;
58 }
59 }
60 }
61
62 void abpoa_set_mat_from_file(abpoa_para_t *abpt, char *mat_fn) {
63 char *l = (char*)_err_malloc(1024 * sizeof(char)); FILE *fp;
64 if ((fp = fopen(mat_fn, "r")) == NULL) err_fatal(__func__, "Unable to open scoring matrix file: \"%s\"\n", mat_fn);
65 int first_line = 1;
66 int *order = (int*)_err_malloc(abpt->m * sizeof(int));
67 while (fgets(l, 1024, fp) != NULL) {
68 if (l[0] == '#') continue;
69 if (first_line) {
70 first_line = 0;
71 // get A/C/G/T/N bases
72 parse_mat_first_line(l, order);
73 } else {
74 // get match/mismatch scores
75 parse_mat_score_line(l, order, abpt->m, abpt->mat);
76 }
77 }
78 int i; abpt->min_mis = 0, abpt->max_mat = 0;
79 for (i = 0; i < abpt->m * abpt->m; ++i) {
80 if (abpt->mat[i] > abpt->max_mat)
81 abpt->max_mat = abpt->mat[i];
82 if (-abpt->mat[i] > abpt->min_mis)
83 abpt->min_mis = -abpt->mat[i];
84 }
85 free(l); free(order); fclose(fp);
86 }
87
88 void abpoa_set_gap_mode(abpoa_para_t *abpt) {
89 if (abpt->match < 0 || abpt->mismatch < 0 || abpt->gap_open1 < 0 || abpt->gap_open2 < 0 || abpt->gap_ext1 < 0 || abpt->gap_ext2 < 0) {
90 err_fatal(__func__, "Invalid negative scoring parameters: match=%d, mismatch=%d, gap_open1=%d, gap_open2=%d, gap_ext1=%d, gap_ext2=%d.",
91 abpt->match, abpt->mismatch, abpt->gap_open1, abpt->gap_open2, abpt->gap_ext1, abpt->gap_ext2);
92 }
93 if (abpt->gap_ext1 == 0 && abpt->gap_ext2 == 0) {
94 err_fatal(__func__, "Invalid gap extension parameters, expect at least one postive: gap_ext1=%d, gap_ext2=%d.", abpt->gap_ext1, abpt->gap_ext2);
95 }
96 if (abpt->gap_open1 == 0) abpt->gap_mode = ABPOA_LINEAR_GAP;
97 else if (abpt->gap_open1 > 0 && abpt->gap_open2 == 0) abpt->gap_mode = ABPOA_AFFINE_GAP;
98 else abpt->gap_mode = ABPOA_CONVEX_GAP;
99 }
100
101 abpoa_para_t *abpoa_init_para(void) {
102 abpoa_para_t *abpt = (abpoa_para_t*)_err_malloc(sizeof(abpoa_para_t));
103 abpt->align_mode = ABPOA_GLOBAL_MODE;
104 abpt->gap_mode = ABPOA_CONVEX_GAP;
105 abpt->zdrop = -1; // disable zdrop
106 abpt->end_bonus = -1; // disable end bonus
107
108 abpt->inc_path_score = 0;
109 abpt->sort_input_seq = 0;
110 abpt->put_gap_on_right = 0;
111 abpt->put_gap_at_end = 0;
112
113 abpt->wb = ABPOA_EXTRA_B; // extra bandwidth
114 abpt->wf = ABPOA_EXTRA_F; // extra bandwidth
115
116 abpt->amb_strand = 0; // ambiguous strand
117 abpt->ret_cigar = 1; // return cigar
118 abpt->rev_cigar = 0; // reverse cigar
119 abpt->out_cons = 1; // output consensus sequence in fasta
120 abpt->out_fq = 0; // output consensus sequence in fastq
121 abpt->out_gfa = 0; // out graph in GFA format
122 abpt->out_msa = 0; // output msa
123 abpt->cons_algrm = ABPOA_HB; // consensus calling algorithm
124 abpt->max_n_cons = 1; // number of max. generated consensus sequence
125 abpt->sub_aln = 0;
126 abpt->min_freq = MULTIP_MIN_FREQ;
127 abpt->use_read_ids = 0;
128 abpt->incr_fn = NULL; // incrementally align seq to an existing graph
129 abpt->out_pog = NULL; // dump partial order graph to file
130
131 // number of residue types
132 abpt->m = 5; // nucleotide
133 abpt->mat = (int*)_err_malloc(abpt->m * abpt->m * sizeof(int));
134
135 // score matrix
136 abpt->use_score_matrix = 0;
137 abpt->mat_fn = NULL;
138 abpt->match = ABPOA_MATCH;
139 abpt->mismatch = ABPOA_MISMATCH;
140 abpt->gap_open1 = ABPOA_GAP_OPEN1;
141 abpt->gap_open2 = ABPOA_GAP_OPEN2;
142 abpt->gap_ext1 = ABPOA_GAP_EXT1;
143 abpt->gap_ext2 = ABPOA_GAP_EXT2;
144
145 abpt->use_qv = 0;
146 abpt->disable_seeding = 1; // no seeding by default
147 abpt->k = ABPOA_MMK;
148 abpt->w = ABPOA_MMW;
149 abpt->min_w = ABPOA_MIN_POA_WIN;
150 abpt->progressive_poa = 0; // progressive partial order alignment
151
152 abpt->verbose = ABPOA_NONE_VERBOSE;
153 abpt->batch_index = 0; // 0 means not in batch mode
154
155 // abpt->simd_flag = simd_check();
156
157 return abpt;
158 }
159
160 void abpoa_post_set_para(abpoa_para_t *abpt) {
161 abpoa_set_gap_mode(abpt);
162 if (abpt->out_msa || abpt->out_gfa || abpt->max_n_cons > 1 || abpt->cons_algrm == ABPOA_MF) {
163 abpt->use_read_ids = 1;
164 if (abpt->out_msa || abpt->out_gfa || abpt->max_n_cons > 1) set_65536_table();
165 if (abpt->max_n_cons > 1 || abpt->cons_algrm == ABPOA_MF) set_bit_table16();
166 }
167 if (abpt->align_mode == ABPOA_LOCAL_MODE) abpt->wb = -1;
168 int i;
169 if (abpt->m > 5) { // for aa sequence
170 for (i = 0; i < 256; ++i) {
171 ab_char26_table[i] = ab_aa26_table[i];
172 ab_char256_table[i] = ab_aa256_table[i];
173 }
174 if (abpt->k > 11) {
175 abpt->k = 7, abpt->w = 4;
176 }
177 } else {
178 for (i = 0; i < 256; ++i) {
179 ab_char26_table[i] = ab_nt4_table[i];
180 ab_char256_table[i] = ab_nt256_table[i];
181 }
182 }
183 if (abpt->use_score_matrix == 0) gen_simple_mat(abpt);
184 else abpoa_set_mat_from_file(abpt, abpt->mat_fn);
185 }
186
187 void abpoa_free_para(abpoa_para_t *abpt) {
188 if (abpt->mat != NULL) free(abpt->mat);
189 if (abpt->mat_fn != NULL) free(abpt->mat_fn);
190 if (abpt->out_pog != NULL) free(abpt->out_pog);
191 if (abpt->incr_fn != NULL) free(abpt->incr_fn);
192 free(abpt);
193 }
194
195 int abpoa_align_sequence_to_subgraph(abpoa_t *ab, abpoa_para_t *abpt, int exc_beg_node_id, int exc_end_node_id, uint8_t *query, int qlen, abpoa_res_t *res) {
196 if (ab->abg->node_n <= 2) return -1;
197 if (ab->abg->is_topological_sorted == 0) abpoa_topological_sort(ab->abg, abpt);
198 simd_abpoa_align_sequence_to_subgraph(ab, abpt, exc_beg_node_id, exc_end_node_id, query, qlen, res);
199 return 0;
200 }
201
202 int abpoa_align_sequence_to_graph(abpoa_t *ab, abpoa_para_t *abpt, uint8_t *query, int qlen, abpoa_res_t *res) {
203 if (ab->abg->node_n <= 2) return -1;
204 if (ab->abg->is_topological_sorted == 0) abpoa_topological_sort(ab->abg, abpt);
205 simd_abpoa_align_sequence_to_graph(ab, abpt, query, qlen, res);
206 return 0;
207 }
208
209 int abpoa_anchor_poa(abpoa_t *ab, abpoa_para_t *abpt, uint8_t **seqs, int **weights, int *seq_lens, ab_u64_v par_anchors, int *par_c, int *tpos_to_node_id, int *qpos_to_node_id, int *read_id_map, int exist_n_seq, int n_seq) {
210 // err_func_format_printf(__func__, "Performing POA between anchors ...");
211 abpoa_res_t res; int read_id, last_read_id = -1, m_c = 0, k = abpt->k, qlen;
212 abpoa_seq_t *abs = ab->abs;
213 int *tmp;
214 int i, _i, ai, j, tot_n_seq = exist_n_seq + n_seq;
215 uint8_t *qseq; int *weight; abpoa_res_t whole_res;
216 // uint8_t *seq1;
217 for (_i = 0; _i < n_seq; ++_i) {
218 i = read_id_map[_i]; read_id = exist_n_seq + i; qlen = seq_lens[i]; whole_res.n_cigar = 0, whole_res.m_cigar = 0, whole_res.graph_cigar = 0;
219 // fprintf(stderr, "seq: # %d\n", i);
220 if (abpt->verbose >= ABPOA_DEBUG_VERBOSE) fprintf(stderr, "seq: # %d\n", i);
221 // seq-to-graph alignment and add alignment within each split window
222 if (_i == 0) ai = 0; else ai = par_c[_i-1];
223
224 int beg_id = ABPOA_SRC_NODE_ID, beg_qpos = 0, end_id=-1, end_tpos=-1, end_qpos=-1;
225 if (ai < par_c[_i]) {
226 abs->is_rc[read_id] = (abs->is_rc[last_read_id] ^ (par_anchors.a[ai] >> 63));
227 // construct rc qseq
228 if (abs->is_rc[read_id]) {
229 qseq = (uint8_t*)_err_malloc(qlen * sizeof(uint8_t));
230 weight = (int*)_err_malloc(qlen * sizeof(int));
231 for (j = 0; j < qlen; ++j) {
232 if (seqs[i][qlen-j-1] < 4) qseq[j] = 3 - seqs[i][qlen-j-1];
233 else qseq[j] = 4;
234 weight[j] = weights[i][qlen-j-1];
235 }
236 if (abs->is_rc[last_read_id]) { // reset tpos/qpos in par_anchors
237 int last_qlen = seq_lens[read_id_map[_i-1]];
238 for (j = ai; j < par_c[_i]; ++j) {
239 end_tpos = ((par_anchors.a[j] >> 32) & 0x7fffffff); end_qpos = (uint32_t)par_anchors.a[j];
240 par_anchors.a[j] = (par_anchors.a[j] >> 63) << 63 | (uint64_t)(last_qlen-end_tpos+k) << 32 | (qlen-end_qpos+k);
241 }
242 for (j = 0; j < (par_c[_i]-ai)/2; ++j) {
243 uint64_t tmp = par_anchors.a[ai+j];
244 par_anchors.a[ai+j] = par_anchors.a[par_c[_i]-1-j];
245 par_anchors.a[par_c[_i]-1-j] = tmp;
246 }
247 }
248 } else {
249 qseq = seqs[i];
250 weight = weights[i];
251 if (abs->is_rc[last_read_id]) { // reset tpos/qpos in par_anchors
252 int last_qlen = seq_lens[read_id_map[_i-1]];
253 for (j = ai; j < par_c[_i]; ++j) {
254 end_tpos = ((par_anchors.a[j] >> 32) & 0x7fffffff); end_qpos = (uint32_t)par_anchors.a[j];
255 par_anchors.a[j] = (par_anchors.a[j] >> 63) << 63 | (uint64_t)(last_qlen-end_tpos+k) << 32 | (qlen-end_qpos+k);
256 }
257 for (j = 0; j < (par_c[_i]-ai)/2; ++j) {
258 uint64_t tmp = par_anchors.a[ai+j];
259 par_anchors.a[ai+j] = par_anchors.a[par_c[_i]-1-j];
260 par_anchors.a[par_c[_i]-1-j] = tmp;
261 }
262 }
263 }
264 } else {
265 abs->is_rc[read_id] = 0, qseq = seqs[i]; weight = weights[i];
266 }
267
268 for (; ai < par_c[_i]; ++ai) {
269 end_tpos = ((par_anchors.a[ai] >> 32) & 0x7fffffff) - k + 1; end_id = tpos_to_node_id[end_tpos];
270 end_qpos = (uint32_t)par_anchors.a[ai] - k + 1;
271 if (abpt->verbose >= ABPOA_DEBUG_VERBOSE) fprintf(stderr, "\tanchor: t: %d (id: %d), q: %d\n", end_tpos, end_id, end_qpos);
272
273 res.graph_cigar = 0; res.n_cigar = 0;
274 abpoa_align_sequence_to_subgraph(ab, abpt, beg_id, end_id, qseq+beg_qpos, end_qpos-beg_qpos, &res);
275 abpoa_push_whole_cigar(&whole_res.n_cigar, &whole_res.m_cigar, &whole_res.graph_cigar, res.n_cigar, res.graph_cigar);
276 if (res.n_cigar) free(res.graph_cigar);
277 // abpoa_add_subgraph_alignment(ab, abpt, beg_id, end_id, qseq+beg_qpos, end_qpos-beg_qpos, qpos_to_node_id+beg_qpos, res, read_id, tot_n_seq, 1);
278
279 // add alignment for anchors
280 res.graph_cigar = (abpoa_cigar_t*)_err_malloc((k) * sizeof(abpoa_cigar_t)); res.n_cigar = 0; m_c = k;
281 for (j = 0; j < k; ++j)
282 res.graph_cigar = abpoa_push_cigar(&(res.n_cigar), &m_c, res.graph_cigar, ABPOA_CMATCH, 1, tpos_to_node_id[end_tpos+j], j);
283 // for (j = 0; j < k; ++j) qpos_to_node_id[end_qpos+j] = tpos_to_node_id[end_tpos+j];
284 // abpoa_add_subgraph_alignment(ab, abpt, end_id, tpos_to_node_id[end_tpos+k-1], qseq+end_qpos, k, NULL, res, read_id, tot_n_seq, 1);
285 abpoa_push_whole_cigar(&whole_res.n_cigar, &whole_res.m_cigar, &whole_res.graph_cigar, res.n_cigar, res.graph_cigar);
286 if (res.n_cigar) free(res.graph_cigar);
287
288 // for next anchor
289 beg_id = tpos_to_node_id[end_tpos+k-1]; beg_qpos = end_qpos+k;
290 }
291 end_id = ABPOA_SINK_NODE_ID; end_qpos = seq_lens[i];
292
293 if (abpt->verbose >= ABPOA_DEBUG_VERBOSE) fprintf(stderr, "\tanchor: t: %d (id: %d), q: %d\n", end_tpos, end_id, end_qpos);
294 res.graph_cigar = 0; res.n_cigar = 0;
295 abpoa_align_sequence_to_subgraph(ab, abpt, beg_id, end_id, qseq+beg_qpos, end_qpos-beg_qpos, &res);
296 abpoa_push_whole_cigar(&whole_res.n_cigar, &whole_res.m_cigar, &whole_res.graph_cigar, res.n_cigar, res.graph_cigar);
297 if (res.n_cigar) free(res.graph_cigar);
298
299 abpoa_add_subgraph_alignment(ab, abpt, ABPOA_SRC_NODE_ID, ABPOA_SINK_NODE_ID, qseq, weight, qlen, qpos_to_node_id, whole_res, read_id, tot_n_seq, 1);
300 if (abs->is_rc[read_id]) {
301 free(qseq); free(weight);
302 }
303 if (whole_res.n_cigar) free(whole_res.graph_cigar);
304
305 tmp = qpos_to_node_id; qpos_to_node_id = tpos_to_node_id; tpos_to_node_id = tmp;
306 last_read_id = read_id;
307 }
308 // err_func_format_printf(__func__, "Performing POA between anchors done.");
309 return 0;
310 }
311
312 // simply partial order alignment, no seeding-based anchor or progressive tree
313 int abpoa_poa(abpoa_t *ab, abpoa_para_t *abpt, uint8_t **seqs, int **weights, int *seq_lens, int exist_n_seq, int n_seq) {
314 // err_func_format_printf(__func__, "Performing POA ...");
315 abpoa_seq_t *abs = ab->abs;
316 abpoa_res_t res; int i, j, read_id, qlen, tot_n_seq = exist_n_seq + n_seq;
317 uint8_t *qseq, *rc_qseq; int *weight, *rc_weight;
318 // uint8_t *seq1;
319 for (i = 0; i < n_seq; ++i) {
320 qlen = seq_lens[i]; qseq = seqs[i]; weight = weights[i]; read_id = exist_n_seq + i;
321 if (abpt->verbose >= ABPOA_DEBUG_VERBOSE) fprintf(stderr, "seq: # %d\n", i);
322 res.graph_cigar = 0; res.n_cigar = 0;
323 if (abpoa_align_sequence_to_graph(ab, abpt, qseq, qlen, &res) >= 0) {
324 if (abpt->amb_strand && (res.best_score < MIN_OF_TWO(qlen, ab->abg->node_n-2) * abpt->max_mat * .3333)) { // TODO .3333
325 rc_qseq = (uint8_t*)_err_malloc(sizeof(uint8_t) * qlen);
326 for (j = 0; j < qlen; ++j) {
327 if (qseq[qlen-j-1] < 4) rc_qseq[j] = 3 - qseq[qlen-j-1];
328 else rc_qseq[j] = 4;
329 }
330 rc_weight = (int*)_err_malloc(sizeof(int) * qlen);
331 for (j = 0; j < qlen; ++j) {
332 rc_weight[j] = weight[qlen-j-1];
333 }
334 abpoa_res_t rc_res; rc_res.n_cigar = 0, rc_res.graph_cigar = 0;
335 simd_abpoa_align_sequence_to_graph(ab, abpt, rc_qseq, qlen, &rc_res);
336 if (rc_res.best_score > res.best_score) {
337 abpoa_res_copy(&res, &rc_res);
338 qseq = rc_qseq;
339 weight = rc_weight;
340 abs->is_rc[read_id] = 1;
341 } else {
342 free(rc_qseq); free(rc_weight);
343 }
344 if (rc_res.n_cigar) free(rc_res.graph_cigar);
345 }
346 }
347 abpoa_add_graph_alignment(ab, abpt, qseq, weight, qlen, NULL, res, read_id, tot_n_seq, 1);
348 if (abs->is_rc[read_id]) { free(qseq); free(weight); }
349 if (res.n_cigar) free(res.graph_cigar);
350 }
351 // err_func_format_printf(__func__, "Performing POA ... done.");
352 return 0;
353 }
354
355 void abpoa_output(abpoa_t *ab, abpoa_para_t *abpt, FILE *out_fp) {
356 // generate & output GFA
357 if (abpt->out_gfa) abpoa_generate_gfa(ab, abpt, out_fp);
358 else {
359 // generate rc-msa/cons
360 if (abpt->out_msa) abpoa_generate_rc_msa(ab, abpt);
361 if (abpt->out_cons) {
362 abpoa_generate_consensus(ab, abpt);
363 if (ab->abg->is_called_cons == 0) err_printf("Warning: no consensus sequence generated.\n");
364 }
365 // output cons/rc-msa
366 if (abpt->out_msa) abpoa_output_rc_msa(ab, abpt, out_fp);
367 else if (abpt->out_cons) abpoa_output_fx_consensus(ab, abpt, out_fp);
368 }
369 // plot partial-order graph using dot
370 if (abpt->out_pog) abpoa_dump_pog(ab, abpt);
371 }
372
373 // only sort new sequences
374 void abpoa_sort_seq_by_length(abpoa_seq_t *abs, int exist_n_seq, int n_seq) {
375 int i, j;
376 // sort by length
377 abpoa_seq_t *tmp_seq = (abpoa_seq_t*)calloc(1, sizeof(abpoa_seq_t));
378 tmp_seq->n_seq = 1; abpoa_realloc_seq(tmp_seq);
379 for (i = 0; i < n_seq-1; ++i) {
380 for (j = i+1; j < n_seq; ++j) {
381 if (abs->seq[exist_n_seq+i].l < abs->seq[exist_n_seq+j].l) {
382 // swap i and j
383 abpoa_cpy_abs(tmp_seq, 0, abs, exist_n_seq+i);
384 abpoa_cpy_abs(abs, exist_n_seq+i, abs, exist_n_seq+j);
385 abpoa_cpy_abs(abs, exist_n_seq+j, tmp_seq, 0);
386 }
387 }
388 }
389 // free
390 abpoa_free_seq(tmp_seq);
391 }
392
393 // do msa for a set of input sequences
394 // @function:
395 // generate consensus sequence
396 // generate rc-msa (row column multiple sequence alignment)
397 // @para:
398 // ab/abpt: abpoa related variable and parameter
399 // n_seq: number of input sequences
400 // seq_len: array of input sequence length, size: seq_n
401 // seqs: array of input sequences, 0123 for ACGT, size: seq_n * seq_len[]
402 int abpoa_msa(abpoa_t *ab, abpoa_para_t *abpt, int n_seq, char **seq_names, int *seq_lens, uint8_t **seqs, int **qual_weights, FILE *out_fp) {
403 // if ((!abpt->out_msa && !abpt->out_cons && !abpt->out_gfa) || n_seq <= 0) return 0;
404 if (n_seq <= 0) return 0;
405 abpoa_seq_t *abs = ab->abs;
406 if (abs->n_seq <= 0) {
407 abpoa_reset(ab, abpt, 1024);
408 if (abpt->incr_fn) abpoa_restore_graph(ab, abpt); // restore existing graph
409 } else {
410 if (abpt->incr_fn != NULL) // WARNING: graph already exists, but incr_fn is also provided
411 err_func_format_printf(__func__, "Graph already exists, but incr_fn is also provided. Not restoring graph from file.");
412 }
413 int i, exist_n_seq = abs->n_seq;
414
415 // set ab->abs, name
416 abs->n_seq += n_seq; abpoa_realloc_seq(abs);
417
418 if (seq_names) {
419 for (i = 0; i < n_seq; ++i) {
420 abpoa_cpy_str(abs->name+exist_n_seq+i, seq_names[i], strlen(seq_names[i]));
421 }
422 } else {
423 for (i = 0; i < n_seq; ++i) {
424 abs->name[exist_n_seq+i].l = 0; abs->name[exist_n_seq+i].m = 0;
425 }
426 }
427 // always reset graph before perform POA
428 int max_len = 0;
429 for (i = 0; i < n_seq; ++i) {
430 if (seq_lens[i] > max_len) max_len = seq_lens[i];
431 }
432
433 int j, **weights = (int**)_err_malloc(n_seq * sizeof(int*));
434 for (i = 0; i < n_seq; ++i) {
435 weights[i] = (int*)_err_malloc(seq_lens[i] * sizeof(int));
436 if (abpt->use_qv && qual_weights != NULL && qual_weights[i] != NULL) {
437 for (j = 0; j < seq_lens[i]; ++j) weights[i][j] = (int)qual_weights[i][j];
438 } else {
439 for (j = 0; j < seq_lens[i]; ++j) weights[i][j] = 1;
440 }
441 }
442
443 if ((abpt->disable_seeding && abpt->progressive_poa==0) || abpt->align_mode != ABPOA_GLOBAL_MODE) {
444 abpoa_poa(ab, abpt, seqs, weights, seq_lens, exist_n_seq, n_seq);
445 } else {
446 // sequence pos to node id
447 int *tpos_to_node_id = (int*)_err_calloc(max_len, sizeof(int)), *qpos_to_node_id = (int*)_err_calloc(max_len, sizeof(int));
448 // seeding, build guide tree, and partition into small windows
449 int *read_id_map = (int*)_err_malloc(sizeof(int) * n_seq); // guide tree order -> input order
450 ab_u64_v par_anchors = {0, 0, 0}; int *par_c = (int*)_err_calloc(n_seq, sizeof(int));
451
452 abpoa_build_guide_tree_partition(seqs, seq_lens, n_seq, abpt, read_id_map, &par_anchors, par_c);
453 if (abpt->incr_fn) { // collect anchors between last one path and first seq
454 // anchors
455 // new_par_anchors
456 // push anchors
457 // free(par_anchors.a);
458 // par_anchors = new_par_anchors;
459 // collect tpos_to_node_id for last one path
460 }
461
462 // perform partial order alignment
463 abpoa_anchor_poa(ab, abpt, seqs, weights, seq_lens, par_anchors, par_c, tpos_to_node_id, qpos_to_node_id, read_id_map, exist_n_seq, n_seq);
464 free(read_id_map); free(tpos_to_node_id); free(qpos_to_node_id); free(par_c);
465 if (par_anchors.m > 0) free(par_anchors.a);
466 }
467
468 // output
469 abpoa_output(ab, abpt, out_fp);
470 for (i = 0; i < n_seq; ++i) free(weights[i]); free(weights);
471 return 0;
472 }
473
474 int abpoa_msa1(abpoa_t *ab, abpoa_para_t *abpt, char *read_fn, FILE *out_fp) {
475 if (!abpt->out_msa && !abpt->out_cons && !abpt->out_gfa) return 0;
476 abpoa_reset(ab, abpt, 1024);
477 if (abpt->incr_fn) abpoa_restore_graph(ab, abpt); // restore existing graph
478 abpoa_seq_t *abs = ab->abs; int exist_n_seq = abs->n_seq;
479
480 // read seq from read_fn
481 gzFile readfp = xzopen(read_fn, "r"); kseq_t *ks = kseq_init(readfp);
482 int i, j, n_seq = abpoa_read_seq(abs, ks);
483
484 if (abpt->sort_input_seq) abpoa_sort_seq_by_length(abs, exist_n_seq, n_seq);
485
486 // always reset graph before perform POA
487 int max_len = 0;
488 for (i = 0; i < abs->n_seq; ++i) {
489 if (abs->seq[i].l > max_len) max_len = abs->seq[i].l;
490 }
491
492 // set seqs, seq_lens
493 extern char ab_char26_table[256];
494 uint8_t **seqs = (uint8_t**)_err_malloc(n_seq * sizeof(uint8_t*)); int *seq_lens = (int*)_err_malloc(n_seq * sizeof(int));
495 int **weights = (int**)_err_malloc(n_seq * sizeof(int*));
496 for (i = 0; i < n_seq; ++i) {
497 seq_lens[i] = abs->seq[exist_n_seq+i].l;
498 seqs[i] = (uint8_t*)_err_malloc(sizeof(uint8_t) * seq_lens[i]);
499 weights[i] = (int*)_err_malloc(sizeof(int) * seq_lens[i]);
500 for (j = 0; j < seq_lens[i]; ++j) seqs[i][j] = ab_char26_table[(int)abs->seq[exist_n_seq+i].s[j]];
501 if (abpt->use_qv && abs->qual[exist_n_seq+i].l > 0) {
502 for (j = 0; j < seq_lens[i]; ++j) weights[i][j] = (int)abs->qual[exist_n_seq+i].s[j]-32;
503 } else {
504 for (j = 0; j < seq_lens[i]; ++j) weights[i][j] = 1;
505 }
506 }
507 if ((abpt->disable_seeding && abpt->progressive_poa==0) || abpt->align_mode != ABPOA_GLOBAL_MODE) {
508 abpoa_poa(ab, abpt, seqs, weights, seq_lens, exist_n_seq, n_seq);
509 } else {
510 // sequence pos to node id
511 int *tpos_to_node_id = (int*)_err_calloc(max_len, sizeof(int)), *qpos_to_node_id = (int*)_err_calloc(max_len, sizeof(int));
512 // seeding, build guide tree, and partition into small windows
513 int *read_id_map = (int*)_err_malloc(sizeof(int) * n_seq); // guide tree order -> input order
514 ab_u64_v par_anchors = {0, 0, 0}; int *par_c = (int*)_err_calloc(n_seq, sizeof(int));
515
516 abpoa_build_guide_tree_partition(seqs, seq_lens, n_seq, abpt, read_id_map, &par_anchors, par_c);
517 if (abpt->incr_fn) { // TODO collect anchors between last one path and first seq
518 // anchors
519 // new_par_anchors
520 // push anchors
521 // free(par_anchors.a);
522 // par_anchors = new_par_anchors;
523 // collect tpos_to_node_id for last one path
524 // set tpos_to_node_id
525 //
526 }
527 abpoa_anchor_poa(ab, abpt, seqs, weights, seq_lens, par_anchors, par_c, tpos_to_node_id, qpos_to_node_id, read_id_map, exist_n_seq, n_seq);
528 free(read_id_map); free(tpos_to_node_id); free(qpos_to_node_id); free(par_c);
529 if (par_anchors.m > 0) free(par_anchors.a);
530 }
531
532 // output
533 abpoa_output(ab, abpt, out_fp);
534
535 kseq_destroy(ks); gzclose(readfp);
536 for (i = 0; i < n_seq; ++i) {
537 free(seqs[i]); free(weights[i]);
538 } free(seqs); free(weights); free(seq_lens);
539 return 0;
540 }