Debian Codegraph @

src/abpoa_seq.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "abpoa_seq.h"
5 #include "abpoa_align.h"
6 #include "abpoa_graph.h"
7 #include "utils.h"
8 #include "kstring.h"
9 #include "khash.h"
10
11 KHASH_MAP_INIT_STR(abstr, uint32_t)
12
13 // for nt
14 // AaCcGgTtNn ==> 0,1,2,3,4
15 unsigned char ab_nt4_table[256] = {
16 0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
17 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
18 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 /*'-'*/, 4, 4,
19 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
20 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
21 4, 4, 4, 4, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
22 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
23 4, 4, 4, 4, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
24 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
25 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
26 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
27 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
28 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
29 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
30 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
31 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
32 };
33
34 // 65,97=>A, 67,99=>C, 71,103=>G, 84,85,116,117=>T, else=>N
35 const char ab_nt256_table[256] = {
36 'A', 'C', 'G', 'T', 'N', '-', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
37 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', '-', 'N', 'N', 'N', 'N',
38 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
39 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
40 'N', 'A', 'N', 'C', 'N', 'N', 'N', 'G', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
41 'N', 'N', 'N', 'N', 'T', 'T', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
42 'N', 'A', 'N', 'C', 'N', 'N', 'N', 'G', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
43 'N', 'N', 'N', 'N', 'T', 'T', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
44 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
45 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
46 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
47 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
48 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
49 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
50 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
51 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'
52 };
53
54 // for aa
55 // AaCcGgTtNn ... ==> 0,1,2,3,4 ...
56 // BbDdEeFf ... ==> 5,6,7,8 ...
57 unsigned char ab_aa26_table[256] = {
58 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
59 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 26, 26, 26, 26, 26,
60 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
61 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
62 26, 0, 5, 1, 6, 7, 8, 2, 9, 10, 11, 12, 13, 14, 4, 15,
63 16, 17, 18, 19, 3, 20, 21, 22, 23, 24, 25, 26, 26, 26, 26, 26,
64 26, 0, 5, 1, 6, 7, 8, 2, 9, 10, 11, 12, 13, 14, 4, 15,
65 16, 17, 18, 19, 3, 20, 21, 22, 23, 24, 25, 26, 26, 26, 26, 26,
66 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
67 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
68 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
69 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
70 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
71 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
72 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
73 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26
74 };
75
76 // 0/1/2/3/4=>ACGTN
77 // 5/6/7/8=>BDEF ...
78 const char ab_aa256_table[256] = {
79 'A', 'C', 'G', 'T', 'N', 'B', 'D', 'E', 'F', 'H', 'I', 'J', 'K', 'L', 'M', 'O',
80 'P', 'Q', 'R', 'S', 'U', 'V', 'W', 'X', 'Y', 'Z', '*', '-', '*', '*', '*', '*',
81 '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
82 '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
83 '*', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
84 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '*', '*', '*', '*', '*',
85 '*', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
86 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '*', '*', '*', '*', '*',
87 '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
88 '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
89 '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
90 '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
91 '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
92 '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
93 '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
94 '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'
95 };
96
97 char ab_char26_table[256];
98 char ab_char256_table[256];
99
100 abpoa_seq_t *abpoa_init_seq(void) {
101 abpoa_seq_t *abs = (abpoa_seq_t*)_err_malloc(sizeof(abpoa_seq_t));
102 abs->n_seq = 0; abs->m_seq = CHUNK_READ_N;
103 abs->seq = (abpoa_str_t*)_err_calloc(abs->m_seq, sizeof(abpoa_str_t));
104 abs->name = (abpoa_str_t*)_err_calloc(abs->m_seq, sizeof(abpoa_str_t));
105 abs->comment = (abpoa_str_t*)_err_calloc(abs->m_seq, sizeof(abpoa_str_t));
106 abs->qual = (abpoa_str_t*)_err_calloc(abs->m_seq, sizeof(abpoa_str_t));
107 abs->is_rc = (uint8_t*)_err_calloc(abs->m_seq, sizeof(uint8_t));
108 return abs;
109 }
110
111 void abpoa_free_seq(abpoa_seq_t *abs) {
112 int i;
113 for (i = 0; i < abs->m_seq; ++i) {
114 if (abs->seq[i].m > 0) free(abs->seq[i].s);
115 if (abs->name[i].m > 0) free(abs->name[i].s);
116 if (abs->comment[i].m > 0) free(abs->comment[i].s);
117 if (abs->qual[i].m > 0) free(abs->qual[i].s);
118 }
119 free(abs->seq); free(abs->name); free(abs->comment); free(abs->qual);
120 free(abs->is_rc); free(abs);
121 }
122
123 void abpoa_cpy_str(abpoa_str_t *str, char *s, int l) {
124 if (l > 0) {
125 // str->s = (char*)_err_malloc(str->m * sizeof(char));
126 if (str->m != 0) str->s = (char*)_err_realloc(str->s, (l+1) * sizeof(char));
127 else str->s = (char*)_err_malloc((l+1) * sizeof(char));
128 str->l = l; str->m = l + 1;
129 memcpy(str->s, s, l);
130 str->s[str->l] = 0;
131 }
132 }
133
134 // copy abs->seq[from_i] to abs->seq[to_i]
135 // copy abs->name[from_i] to abs->name[to_i]
136 // copy abs->comment[from_i] to abs->comment[to_i]
137 // copy abs->qual[from_i] to abs->qual[to_i]
138 void abpoa_cpy_abs(abpoa_seq_t *to_abs, int to_i, abpoa_seq_t *from_abs, int from_i) {
139 abpoa_cpy_str(to_abs->seq+to_i, from_abs->seq[from_i].s, from_abs->seq[from_i].l);
140 abpoa_cpy_str(to_abs->name+to_i, from_abs->name[from_i].s, from_abs->name[from_i].l);
141 abpoa_cpy_str(to_abs->comment+to_i, from_abs->comment[from_i].s, from_abs->comment[from_i].l);
142 abpoa_cpy_str(to_abs->qual+to_i, from_abs->qual[from_i].s, from_abs->qual[from_i].l);
143 }
144
145 void abpoa_cpy_seq(abpoa_seq_t *abs, int seq_i, kseq_t *kseq) {
146 abpoa_cpy_str(abs->seq+seq_i, kseq->seq.s, kseq->seq.l);
147 abpoa_cpy_str(abs->name+seq_i, kseq->name.s, kseq->name.l);
148 abpoa_cpy_str(abs->comment+seq_i, kseq->comment.s, kseq->comment.l);
149 abpoa_cpy_str(abs->qual+seq_i, kseq->qual.s, kseq->qual.l);
150 }
151
152 abpoa_seq_t *abpoa_realloc_seq(abpoa_seq_t *abs) {
153 if (abs->n_seq >= abs->m_seq) {
154 int m_seq = MAX_OF_TWO(abs->n_seq, abs->m_seq << 1);
155 abs->seq = (abpoa_str_t*)_err_realloc(abs->seq, m_seq * sizeof(abpoa_str_t));
156 abs->name = (abpoa_str_t*)_err_realloc(abs->name, m_seq * sizeof(abpoa_str_t));
157 abs->comment = (abpoa_str_t*)_err_realloc(abs->comment, m_seq * sizeof(abpoa_str_t));
158 abs->qual = (abpoa_str_t*)_err_realloc(abs->qual, m_seq * sizeof(abpoa_str_t));
159 abs->is_rc = (uint8_t*)_err_realloc(abs->is_rc, m_seq * sizeof(uint8_t));
160 int i;
161 for (i = abs->m_seq; i < m_seq; ++i) {
162 abs->seq[i].l = abs->seq[i].m = 0;
163 abs->name[i].l = abs->name[i].m = 0;
164 abs->comment[i].l = abs->comment[i].m = 0;
165 abs->qual[i].l = abs->qual[i].m = 0;
166 abs->is_rc[i] = 0;
167 }
168 abs->m_seq = m_seq;
169 }
170 return abs;
171 }
172
173 int abpoa_read_nseq(abpoa_seq_t *abs, kseq_t *kseq, int chunk_read_n) {
174 int n = 0;
175 while (n < chunk_read_n && kseq_read(kseq) >= 0) {
176 abpoa_realloc_seq(abs);
177 // copy kseq to abs->seq
178 abpoa_cpy_seq(abs, abs->n_seq, kseq);
179 abs->n_seq++; n++;
180 }
181 return n;
182 }
183
184 int abpoa_read_seq(abpoa_seq_t *abs, kseq_t *kseq) {
185 int n = 0;
186 while (kseq_read(kseq) >= 0) {
187 abpoa_realloc_seq(abs);
188 // copy kseq to abs->seq
189 abpoa_cpy_seq(abs, abs->n_seq, kseq);
190 abs->n_seq++; n++;
191 }
192 return n;
193 }
194
195 static long int _strtol10(const char *str, char **endptr) {
196 long int res = 0; unsigned d;
197 char *s;
198 for (s = (char*)str, d = s[0]-'0'; d < 10; ++s, d = s[0]-'0')
199 res = res * 10 + d;
200 if (endptr) *endptr = s;
201 return res;
202 }
203
204 static unsigned long int _strtoul10(const char *str, char **endptr) {
205 unsigned long int res = 0; unsigned d;
206 char *s;
207 for (s = (char*)str, d = s[0]-'0'; d < 10; ++s, d = s[0]-'0')
208 res = res * 10 + d;
209 if (endptr) *endptr = s;
210 return res;
211 }
212
213 int gfa_aux_parse(char *s, uint8_t **data, int *max)
214 {
215 char *q, *p;
216 kstring_t str;
217 if (s == 0) return 0;
218 str.l = 0, str.m = *max, str.s = (char*)*data;
219 if (*s == '\t') ++s;
220 for (p = q = s;; ++p) {
221 if (*p == 0 || *p == '\t') {
222 int c = *p;
223 *p = 0;
224 if (p - q >= 5 && q[2] == ':' && q[4] == ':' && (q[3] == 'I' || q[3] == 'A' || q[3] == 'i' || q[3] == 'f' || q[3] == 'Z' || q[3] == 'B')) {
225 int type = q[3];
226 kputsn_(q, 2, &str);
227 q += 5;
228 if (type == 'A') {
229 kputc_('A', &str);
230 kputc_(*q, &str);
231 } else if (type == 'I') {
232 uint32_t x;
233 // x = strtol(q, &q, 10);
234 x = _strtoul10(q, &q);
235 kputc_(type, &str); kputsn_((char*)&x, 4, &str);
236 } else if (type == 'i') {
237 int32_t x;
238 // x = strtol(q, &q, 10);
239 x = _strtol10(q, &q);
240 kputc_(type, &str); kputsn_((char*)&x, 4, &str);
241 } else if (type == 'f') {
242 float x;
243 x = strtod(q, &q);
244 kputc_('f', &str); kputsn_(&x, 4, &str);
245 } else if (type == 'Z') {
246 kputc_('Z', &str); kputsn_(q, p - q + 1, &str); // note that this include the trailing NULL
247 } else if (type == 'B') {
248 type = *q++; // q points to the first ',' following the typing byte
249 if (p - q >= 2 && (type == 'c' || type == 'C' || type == 's' || type == 'S' || type == 'i' || type == 'I' || type != 'f')) {
250 int32_t n;
251 char *r;
252 for (r = q, n = 0; *r; ++r)
253 if (*r == ',') ++n;
254 kputc_('B', &str); kputc_(type, &str); kputsn_(&n, 4, &str);
255 // TODO: to evaluate which is faster: a) aligned array and then memmove(); b) unaligned array; c) kputsn_()
256 if (type == 'c') while (q + 1 < p) { int8_t x = strtol(q + 1, &q, 0); kputc_(x, &str); }
257 else if (type == 'C') while (q + 1 < p) { uint8_t x = strtol(q + 1, &q, 0); kputc_(x, &str); }
258 else if (type == 's') while (q + 1 < p) { int16_t x = strtol(q + 1, &q, 0); kputsn_(&x, 2, &str); }
259 else if (type == 'S') while (q + 1 < p) { uint16_t x = strtol(q + 1, &q, 0); kputsn_(&x, 2, &str); }
260 // else if (type == 'i') while (q + 1 < p) { int32_t x = strtol(q + 1, &q, 0); kputsn_(&x, 4, &str); }
261 else if (type == 'i') while (q + 1 < p) { int32_t x = _strtol10(q + 1, &q); kputsn_(&x, 4, &str); }
262 // else if (type == 'I') while (q + 1 < p) { uint32_t x = strtol(q + 1, &q, 0); kputsn_(&x, 4, &str); }
263 else if (type == 'I') while (q + 1 < p) { uint32_t x = _strtoul10(q + 1, &q); kputsn_(&x, 4, &str); }
264 else if (type == 'f') while (q + 1 < p) { float x = strtod(q + 1, &q); kputsn_(&x, 4, &str); }
265 }
266 } // should not be here, as we have tested all types
267 }
268 q = p + 1;
269 if (c == 0) break;
270 }
271 }
272 if (str.l > 0 && str.l == str.m) ks_resize(&str, str.l + 1);
273 if (str.s) str.s[str.l] = 0;
274 *max = str.m, *data = (uint8_t*)str.s;
275 return str.l;
276 }
277
278 static inline int gfa_aux_type2size(int x)
279 {
280 if (x == 'C' || x == 'c' || x == 'A') return 1;
281 else if (x == 'S' || x == 's') return 2;
282 else if (x == 'I' || x == 'i' || x == 'f') return 4;
283 else return 0;
284 }
285
286 #define __skip_tag(s) do { \
287 int type = toupper(*(s)); \
288 ++(s); \
289 if (type == 'Z') { while (*(s)) ++(s); ++(s); } \
290 else if (type == 'B') (s) += 5 + gfa_aux_type2size(*(s)) * (*(int32_t*)((s)+1)); \
291 else (s) += gfa_aux_type2size(type); \
292 } while(0)
293
294 uint8_t *gfa_aux_get(int l_data, const uint8_t *data, const char tag[2])
295 {
296 const uint8_t *s = data;
297 int y = tag[0]<<8 | tag[1];
298 while (s < data + l_data) {
299 int x = (int)s[0]<<8 | s[1];
300 s += 2;
301 if (x == y) return (uint8_t*)s;
302 __skip_tag(s);
303 }
304 return 0;
305 }
306
307 int gfa_aux_del(int l_data, uint8_t *data, uint8_t *s)
308 {
309 uint8_t *p;
310 p = s - 2;
311 __skip_tag(s);
312 memmove(p, s, l_data - (s - data));
313 return l_data - (s - p);
314 }
315
316 int abpoa_gfa_parse_H(abpoa_graph_t *abg, int *n_s, int *n_l, int *n_p, char *s) {
317 if (s[1] != '\t' || s[2] == '0') return -1;
318 int l_aux, m_aux = 0; uint8_t *aux = 0, *info;
319 l_aux = gfa_aux_parse(s + 2, &aux, &m_aux);
320
321 info = gfa_aux_get(l_aux, aux, "NS");
322 if (info == 0 || info[0] != 'i') err_fatal_simple("Error: no \"NS\" tag in GFA header.");
323 *n_s = *(int32_t*)(info+1);
324 abg->node_m = *n_s + 2;
325 abg->node = (abpoa_node_t*)_err_realloc(abg->node, abg->node_m * sizeof(abpoa_node_t*));
326 l_aux = gfa_aux_del(l_aux, aux, info);
327
328 info = gfa_aux_get(l_aux, aux, "NL");
329 if (info == 0 || info[0] != 'i') err_fatal_simple("Error: no \"NL\" tag in GFA header.");
330 *n_l = *(int32_t*)(info+1);
331 l_aux = gfa_aux_del(l_aux, aux, info);
332
333 info = gfa_aux_get(l_aux, aux, "NP");
334 if (info == 0 || info[0] != 'i') err_fatal_simple("Error: no \"NP\" tag in GFA header.");
335 *n_p = *(int32_t*)(info+1);
336 l_aux = gfa_aux_del(l_aux, aux, info);
337
338 if (aux) free(aux);
339 return 0;
340 }
341
342 typedef struct {
343 int n, m;
344 kstring_t *seq, *name;
345 khash_t(abstr) *h;
346 } seg_seq_t;
347
348 seg_seq_t *seg_seq_init(void) {
349 seg_seq_t *s = (seg_seq_t*)_err_malloc(sizeof(seg_seq_t));
350 s->n = s->m = 0; s->seq = 0, s->name = 0;
351 s->h = kh_init(abstr);
352 return s;
353 }
354
355 seg_seq_t *seg_seq_realloc(seg_seq_t *r) {
356 if (r->n >= r->m) {
357 int m;
358 if (r->m == 0) m = 1;
359 else m = MAX_OF_TWO(r->n, (r->m) << 1);
360 r->seq = (kstring_t*)_err_realloc(r->seq, m * sizeof(kstring_t));
361 r->name = (kstring_t*)_err_realloc(r->name, m * sizeof(kstring_t));
362 int i;
363 for (i = r->m; i < m; ++i) {
364 r->seq[i] = (kstring_t){0,0,0};
365 r->name[i] = (kstring_t){0,0,0};
366 }
367 r->m = m;
368 }
369 return r;
370 }
371
372 void seg_seq_free(seg_seq_t *s) {
373 if (s->m > 0) {
374 int i;
375 for (i = 0; i < s->m; ++i) {
376 if (s->seq[i].m) free(s->seq[i].s);
377 if (s->name[i].m) free(s->name[i].s);
378 }
379 free(s->seq); free(s->name);
380 }
381 kh_destroy(abstr, s->h);
382 free(s);
383 }
384
385 int abpoa_gfa_parse_S(seg_seq_t *segs, char *s) {
386 if (s[1] != '\t' || s[2] == '\0') return -1;
387 char *deli_s, *info_s, *seq = 0;
388 int i, seq_len, seg_name_len, is_ok = 0;
389 char *seg_name=0;
390
391 for (i = 0, deli_s = info_s = s + 2;; ++deli_s) {
392 if (*deli_s == 0 || *deli_s == '\t') {
393 int c = *deli_s;
394 *deli_s = 0;
395 if (i == 0) {
396 seg_name = info_s;
397 seg_name_len = deli_s - info_s;
398 } else if (i == 1) {
399 seq = info_s;
400 seq_len = deli_s - info_s;
401 is_ok = 1;
402 break;
403 }
404 if (c == 0) break;
405 ++i, info_s = deli_s + 1;
406 }
407 }
408
409 if (is_ok) {
410 seg_seq_realloc(segs);
411 kputsn(seg_name, seg_name_len, segs->name+segs->n);
412 kputsn(seq, seq_len, segs->seq+segs->n);
413 int absent;
414 khint_t pos = kh_put(abstr, segs->h, segs->name[segs->n].s, &absent);
415 if (absent) kh_val(segs->h, pos) = segs->n;
416 else err_fatal(__func__, "Duplicated chromosome: \"%s\".", seg_name);
417 ++segs->n;
418 } else err_fatal(__func__, "Error: no seq in GFA segment line (%s).", seg_name);
419 return 0;
420 }
421
422 /*int abpoa_gfa_parse_S(abpoa_graph_t *abg, char *s) {
423 if (s[1] != '\t' || s[2] == '\0') return -1;
424 char *deli_s, *info_s, *seq = 0;
425 int i, seq_len, is_ok = 0;
426 char *seg_name=0;
427
428 for (i = 0, deli_s = info_s = s + 2;; ++deli_s) {
429 if (*deli_s == 0 || *deli_s == '\t') {
430 int c = *deli_s;
431 *deli_s = 0;
432 if (i == 0) {
433 seg_name = info_s;
434 abpoa_realloc_seq(seg_names);
435 abpoa_cpy_str(seg_names->name+seg_names->n_seq, seg_name, deli_s - info_s);
436 seg_names->n_seq++;
437 } else if (i == 1) {
438 seq = info_s;
439 seq_len = deli_s - info_s;
440 is_ok = 1;
441 break;
442 }
443 if (c == 0) break;
444 ++i, info_s = deli_s + 1;
445 }
446 }
447
448 if (is_ok) {
449 int seg_id, absent;
450 for (i = 0; i < seq_len; ++i) {
451 seg_id = abpoa_add_graph_node(abg, ab_char26_table[(int)(seq[i])]);
452 if (i == 0) {
453 khint_t pos = kh_put(str, seg_name2in_id, seg_names->name[seg_names->n_seq-1].s, &absent);
454 if (absent) kh_val(seg_name2in_id, pos) = seg_id;
455 else err_fatal(__func__, "Error: duplicated seg name (%s).", seg_name);
456 }
457 if (i == seq_len-1) {
458 khint_t pos = kh_put(str, seg_name2out_id, seg_names->name[seg_names->n_seq-1].s, &absent);
459 if (absent) kh_val(seg_name2out_id, pos) = seg_id;
460 else err_fatal(__func__, "Error: duplicated seg name (%s).", seg_name);
461 }
462 }
463 } else err_fatal(__func__, "Error: no seq in GFA segment line (%s).", seg_name);
464 return 0;
465 }*/
466
467 int abpoa_gfa_parse_P(abpoa_graph_t *abg, abpoa_seq_t *abs, seg_seq_t *segs, int add_read_id, int p_i, int p_n, khash_t(abstr) *seg_name2in_id, khash_t(abstr) *seg_name2out_id, char *s) {
468 if (s[1] != '\t' || s[2] == '\0') return -1;
469 char *deli_s, *info_s, *path = 0;
470 int i, is_ok = 0, is_rc = -1;
471 char *path_name=0; int path_name_len=0;
472 kstring_t *seg_seq, *seg_name; int read_ids_n = 1 + ((p_n-1) >> 6);
473
474 for (i = 0, deli_s = info_s = s + 2;; ++deli_s) {
475 if (*deli_s == 0 || *deli_s == '\t') {
476 int c = *deli_s;
477 *deli_s = 0;
478 if (i == 0) {
479 path_name = info_s;
480 path_name_len = deli_s - info_s;
481 } else if (i == 1) {
482 path = info_s;
483 is_ok = 1;
484 break;
485 }
486 if (c == 0) break;
487 ++i, info_s = deli_s + 1;
488 }
489 }
490
491 if (is_ok) {
492 char *deli_s, *info_s, *_seg_name; khint_t pos, seg_pos; int absent;
493 int id, in_id=-1, out_id=-1, last_id = ABPOA_SRC_NODE_ID, next_id = ABPOA_SINK_NODE_ID;
494 for (deli_s = info_s = path; ; ++deli_s) {
495 if (*deli_s == '+') {
496 if (is_rc == 1) err_fatal(__func__, "Error: path has both \'+\' and \'-\' seg. (%s)", path_name);
497 is_rc = 0; *deli_s = 0; _seg_name = info_s;
498 seg_pos = kh_get(abstr, segs->h, _seg_name);
499 if (seg_pos == kh_end(segs->h)) err_fatal(__func__, "Error: seg (%s) not exist.", info_s);
500 seg_name = segs->name + kh_val(segs->h, seg_pos);
501 seg_seq = segs->seq + kh_val(segs->h, seg_pos);
502
503 // check if seg already exist
504 pos = kh_put(abstr, seg_name2in_id, seg_name->s, &absent);
505 if (absent) { // add node for seg_seq
506 for (i = 0; i < (int)seg_seq->l; ++i) {
507 id = abpoa_add_graph_node(abg, ab_char26_table[(int)(seg_seq->s[i])]);
508 if (i == 0) in_id = id;
509 if (i == (int)seg_seq->l-1) out_id = id;
510 }
511 kh_val(seg_name2in_id, pos) = in_id;
512 pos = kh_put(abstr, seg_name2out_id, seg_name->s, &absent);
513 kh_val(seg_name2out_id, pos) = out_id;
514 } else {
515 in_id = kh_val(seg_name2in_id, pos);
516 pos = kh_put(abstr, seg_name2out_id, seg_name->s, &absent);
517 out_id = kh_val(seg_name2out_id, pos);
518 }
519 // add edge
520 abpoa_add_graph_edge(abg, last_id, in_id, 1, 1, add_read_id, 0, p_i, read_ids_n, p_n);
521 if (in_id < out_id) {
522 for (i = 0; i < out_id - in_id; ++i)
523 abpoa_add_graph_edge(abg, in_id+i, in_id+i+1, 1, 1, add_read_id, 0, p_i, read_ids_n, p_n);
524 } else if (in_id > out_id) err_fatal(__func__, "Error: in_id (%d) > out_id (%d).", in_id, out_id);
525
526 last_id = out_id;
527 info_s = deli_s + 2;
528 } else if (*deli_s == '-') {
529 if (is_rc == 0) err_fatal(__func__, "Error: path has both \'+\' and \'-\' seg. (%s)", path_name);
530 is_rc = 1; *deli_s = 0; _seg_name = info_s;
531 seg_pos = kh_get(abstr, segs->h, _seg_name);
532 if (seg_pos == kh_end(segs->h)) err_fatal(__func__, "Error: seg (%s) not exist.", info_s);
533 seg_name = segs->name + kh_val(segs->h, seg_pos);
534 seg_seq = segs->seq + kh_val(segs->h, seg_pos);
535
536 // check if seg exist
537 pos = kh_put(abstr, seg_name2in_id, seg_name->s, &absent);
538 if (absent) { // add node for seg_seq
539 for (i = 0; i < (int)seg_seq->l; ++i) {
540 id = abpoa_add_graph_node(abg, ab_char26_table[(int)(seg_seq->s[i])]);
541 if (i == 0) in_id = id;
542 if (i == (int)seg_seq->l-1) out_id = id;
543 }
544 kh_val(seg_name2in_id, pos) = in_id;
545 pos = kh_put(abstr, seg_name2out_id, seg_name->s, &absent);
546 kh_val(seg_name2out_id, pos) = out_id;
547 } else {
548 in_id = kh_val(seg_name2in_id, pos); out_id = kh_val(seg_name2out_id, pos);
549 }
550
551 // add edge
552 abpoa_add_graph_edge(abg, out_id, next_id, 1, 1, add_read_id, 0, p_i, read_ids_n, p_n);
553 if (in_id < out_id) {
554 for (i = 0; i < out_id - in_id; ++i)
555 abpoa_add_graph_edge(abg, in_id+i, in_id+i+1, 1, 1, add_read_id, 0, p_i, read_ids_n, p_n);
556 } else if (in_id > out_id) err_fatal(__func__, "Error: in_id (%d) > out_id (%d).", in_id, out_id);
557
558 next_id = in_id;
559 info_s = deli_s + 2;
560 } else if (*deli_s == 0 || *deli_s == '\t') break;
561 }
562 if (is_rc) abpoa_add_graph_edge(abg, ABPOA_SRC_NODE_ID, next_id, 1, 1, add_read_id, 0, p_i, read_ids_n, p_n);
563 else abpoa_add_graph_edge(abg, last_id, ABPOA_SINK_NODE_ID, 1, 1, add_read_id, 0, p_i, read_ids_n, p_n);
564 // set abs
565 abpoa_realloc_seq(abs);
566 abpoa_cpy_str(abs->name+abs->n_seq, path_name, path_name_len);
567 abs->is_rc[abs->n_seq] = is_rc; abs->n_seq++;
568 } else err_fatal(__func__, "Error: no path in GFA path line (%s).", path_name);
569 return 0;
570 }
571
572 int abpoa_fa_parse_seq(abpoa_graph_t *abg, abpoa_seq_t *abs, kstring_t *seq, kstring_t *name, int add_read_id, int p_i, int p_n, int **rank2node_id) {
573 if (*rank2node_id == 0) {
574 *rank2node_id = (int*)_err_calloc(seq->l, sizeof(int));
575 }
576 char *s = seq->s;
577 int32_t read_ids_n = 1 + ((p_n-1) >> 6);
578 int32_t i, rank, last_id = ABPOA_SRC_NODE_ID, cur_id, aln_id; uint8_t base;
579 for (i = 0; s[i]; ++i) {
580 if (s[i] == '-') continue; // gap
581 else {
582 base = ab_char26_table[(int)(s[i])];
583 rank = i;
584 cur_id = (*rank2node_id)[rank];
585 if (cur_id == 0) {
586 cur_id = abpoa_add_graph_node(abg, base);
587 (*rank2node_id)[rank] = cur_id;
588 } else {
589 if (abg->node[cur_id].base != base) {
590 aln_id = abpoa_get_aligned_id(abg, cur_id, base);
591 if (aln_id == -1) {
592 aln_id = abpoa_add_graph_node(abg, base);
593 abpoa_add_graph_aligned_node(abg, cur_id, aln_id);
594 }
595 cur_id = aln_id;
596 }
597 }
598 abpoa_add_graph_edge(abg, last_id, cur_id, 1, 1, add_read_id, 0, p_i, read_ids_n, p_n);
599 last_id = cur_id;
600 }
601 }
602 abpoa_add_graph_edge(abg, last_id, ABPOA_SINK_NODE_ID, 1, 1, add_read_id, 0, p_i, read_ids_n, p_n);
603 abpoa_realloc_seq(abs);
604 abpoa_cpy_str(abs->name + abs->n_seq, name->s, name->l); abs->n_seq++;
605 return 0;
606 }
607
608 abpoa_t *abpoa_restore_graph(abpoa_t *ab, abpoa_para_t *abpt) {
609 char *fn = abpt->incr_fn;
610 if (fn == NULL) return ab;
611 gzFile fp = fn && strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(0, "r"); if (fp == 0) return NULL;
612 kstream_t *ks = ks_init(fp); kstring_t s={0,0,0}; int dret, line_n=0, read_name_e;
613 seg_seq_t *seqs = seg_seq_init();
614 khash_t(abstr) *seg_name2in_id = kh_init(abstr), *seg_name2out_id = kh_init(abstr);
615 int add_read_id = abpt->use_read_ids;
616 int p_i = -1, is_fa = 0, *rank2node_id=0;
617
618 abpoa_graph_t *abg = ab->abg; abpoa_seq_t *abs = ab->abs;
619 while (ks_getuntil(ks, KS_SEP_LINE, &s, &dret) >= 0) {
620 line_n++;
621 int ret = 0;
622 if (is_fa) {
623 if (s.l > 0 && s.s[0] == '>') { // name
624 // parse_seq
625 if (seqs->seq[seqs->n].l > 0) {
626 ret = abpoa_fa_parse_seq(abg, abs, seqs->seq+seqs->n, seqs->name+seqs->n, add_read_id, p_i, p_i+1, &rank2node_id);
627 seqs->n++;
628 }
629 // kputsn seqs->name
630
631 read_name_e = 1;
632 while (read_name_e < (int)s.l && !isspace(s.s[read_name_e])) read_name_e++;
633 seg_seq_realloc(seqs);
634 kputsn(s.s+1, read_name_e-1, seqs->name + seqs->n);
635 p_i++;
636 } else { // seq
637 // kputsn seqs->seq
638 kputsn(s.s, s.l, seqs->seq + seqs->n);
639 }
640 } else {
641 if (s.l > 0 && s.s[0] == '>') {
642 read_name_e = 1;
643 while (read_name_e < (int)s.l && !isspace(s.s[read_name_e])) read_name_e++;
644 seg_seq_realloc(seqs);
645 kputsn(s.s+1, read_name_e-1, seqs->name + seqs->n);
646 is_fa = 1; p_i++;
647 }
648 // else if (s.l < 2 || s.s[0] == '#') continue; // comment
649 // else if (s.s[0] == 'H') ret = abpoa_gfa_parse_H(abg, &s_n, &l_n, &p_n, s.s);
650 else if (s.s[0] == 'S') ret = abpoa_gfa_parse_S(seqs, s.s); // include Link information
651 // else if (s.s[0] == 'L') ret = abpoa_gfa_parse_L(abg, seg_name, s.s);
652 else if (s.s[0] == 'P') {
653 p_i++;
654 ret = abpoa_gfa_parse_P(abg, abs, seqs, add_read_id, p_i, p_i+1, seg_name2in_id, seg_name2out_id, s.s);
655 }
656 }
657 if (ret < 0) err_fatal(__func__, "Error in %c-line at line %ld (error code %d)", s.s[0], (long)line_n, ret);
658 }
659 if (is_fa) { // last seq
660 abpoa_fa_parse_seq(abg, abs, seqs->seq+seqs->n, seqs->name+seqs->n, add_read_id, p_i, p_i+1, &rank2node_id);
661 seqs->n++;
662 }
663 if (s.m) free(s.s);
664 ks_destroy(ks); gzclose(fp);
665 seg_seq_free(seqs); kh_destroy(abstr, seg_name2in_id); kh_destroy(abstr, seg_name2out_id);
666 if (rank2node_id) free(rank2node_id);
667 if (abs->n_seq == 0) {
668 err_func_printf(__func__, "Warning: no graph/sequence restored from file \'%s\'.\n", fn);
669 abg->node_n = 2;
670 }
671 abg->is_called_cons = abg->is_set_msa_rank = abg->is_topological_sorted = 0;
672 return ab;
673 }