-
Notifications
You must be signed in to change notification settings - Fork 38
/
format.c
195 lines (190 loc) · 6.56 KB
/
format.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "kalloc.h"
#include "mgpriv.h"
static inline void str_enlarge(kstring_t *s, int l)
{
if (s->l + l + 1 > s->m) {
s->m = s->l + l + 1;
kroundup32(s->m);
s->s = (char*)realloc(s->s, s->m);
}
}
static inline void str_copy(kstring_t *s, const char *st, const char *en)
{
str_enlarge(s, en - st);
memcpy(&s->s[s->l], st, en - st);
s->l += en - st;
}
static void mg_sprintf_lite(kstring_t *s, const char *fmt, ...)
{
char buf[16]; // for integer to string conversion
const char *p, *q;
va_list ap;
va_start(ap, fmt);
for (q = p = fmt; *p; ++p) {
if (*p == '%') {
if (p > q) str_copy(s, q, p);
++p;
if (*p == 'd') {
int c, i, l = 0;
unsigned int x;
c = va_arg(ap, int);
x = c >= 0? c : -c;
do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0);
if (c < 0) buf[l++] = '-';
str_enlarge(s, l);
for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
} else if (*p == 'u') {
int i, l = 0;
uint32_t x;
x = va_arg(ap, uint32_t);
do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0);
str_enlarge(s, l);
for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
} else if (*p == 's') {
char *r = va_arg(ap, char*);
str_copy(s, r, r + strlen(r));
} else if (*p == 'c') {
str_enlarge(s, 1);
s->s[s->l++] = va_arg(ap, int);
} else abort();
q = p + 1;
}
}
if (p > q) str_copy(s, q, p);
va_end(ap);
s->s[s->l] = 0;
}
void mg_print_lchain(FILE *fp, const mg_idx_t *gi, int n_lc, const mg_lchain_t *lc, const mg128_t *a, const char *qname)
{
kstring_t str = {0,0,0};
int i, j;
for (i = 0; i < n_lc; ++i) {
const mg_lchain_t *p = &lc[i];
int mlen, blen, span = a[p->off].y>>32&0xff;
mlen = blen = span;
for (j = 1; j < p->cnt; ++j) {
int ql = (int32_t)a[p->off + j].y - (int32_t)a[p->off + j - 1].y;
int pl = (int32_t)a[p->off + j].x - (int32_t)a[p->off + j - 1].x;
blen += pl > ql? pl : ql;
mlen += pl > span && ql > span? span : pl < ql? pl : ql;
}
str.l = 0;
mg_sprintf_lite(&str, "LC\t%s\t%d\t%d\t%c\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t", qname, p->qs, p->qe, "+-"[p->v&1], gi->g->seg[p->v>>1].name, gi->g->seg[p->v>>1].len, p->rs, p->re, p->score, mlen, blen, p->cnt);
for (j = 0; j < p->cnt; ++j)
mg_sprintf_lite(&str, "%d,", (int32_t)a[p->off + j].y);
mg_sprintf_lite(&str, "\t");
for (j = 0; j < p->cnt; ++j)
mg_sprintf_lite(&str, "%d,", (int32_t)a[p->off + j].x);
mg_sprintf_lite(&str, "\n");
fwrite(str.s, 1, str.l, fp);
}
free(str.s);
}
void mg_write_gaf(kstring_t *s, const gfa_t *g, const mg_gchains_t *gs, int32_t n_seg, const int32_t *qlens, const char *qname, uint64_t flag, void *km)
{
int32_t i, j, qlen;
s->l = 0;
for (i = 0, qlen = 0; i < n_seg; ++i) qlen += qlens[i];
if ((gs == 0 || gs->n_gc == 0) && (flag&MG_M_SHOW_UNMAP)) {
mg_sprintf_lite(s, "%s", qname);
if ((flag&MG_M_FRAG_MERGE) && n_seg == 2 && s->l > 2 && s->s[s->l-1] == '1' && s->s[s->l-2] == '/') s->l -= 2;
mg_sprintf_lite(s, "\t%d\t0\t0\t*\t*\t0\t0\t0\t0\t0\t0\n", qlen);
return;
}
if (gs == 0) return;
for (i = 0; i < gs->n_gc; ++i) {
const mg_gchain_t *p = &gs->gc[i];
int32_t sign_pos, compact;
if (p->id != p->parent && !(flag&MG_M_PRINT_2ND)) continue;
if (p->cnt == 0) continue;
mg_sprintf_lite(s, "%s", qname);
if ((flag&MG_M_FRAG_MERGE) && n_seg == 2 && s->l > 2 && s->s[s->l-1] == '1' && s->s[s->l-2] == '/') s->l -= 2;
mg_sprintf_lite(s, "\t%d\t%d\t%d\t+\t", qlen, p->qs, p->qe);
assert(p->cnt > 0);
sign_pos = s->l - 2;
if (flag & MG_M_VERTEX_COOR) {
compact = 0;
for (j = 0; j < p->cnt; ++j) {
const mg_llchain_t *q = &gs->lc[p->off + j];
mg_sprintf_lite(s, "%c%s", "><"[q->v&1], g->seg[q->v>>1].name);
}
} else {
int32_t last_pnid = -1, st = -1, en = -1, rev = -1;
compact = flag&MG_M_NO_COMP_PATH? 0 : 1;
for (j = 0; j < p->cnt; ++j) {
const mg_llchain_t *q;
const gfa_seg_t *t;
assert(p->off + j < gs->n_lc);
q = &gs->lc[p->off + j];
t = &g->seg[q->v>>1];
if (t->snid < 0) { // no stable ID; write the vertex coordinate
compact = 0;
if (last_pnid >= 0) mg_sprintf_lite(s, "%c%s:%d-%d", "><"[rev], g->sseq[last_pnid].name, st, en);
last_pnid = -1, st = -1, en = -1, rev = -1;
mg_sprintf_lite(s, "%c%s", "><"[q->v&1], g->seg[q->v>>1].name);
} else {
int cont = 0;
if (last_pnid >= 0 && t->snid == last_pnid && (q->v&1) == rev) { // same stable sequence and same strand
if (!(q->v&1)) { // forward strand
if (t->soff == en)
en = t->soff + t->len, cont = 1;
} else { // reverse strand
if (t->soff + t->len == st)
st = t->soff, cont = 1;
}
}
if (cont == 0) {
if (last_pnid >= 0) compact = 0;
if (last_pnid >= 0) mg_sprintf_lite(s, "%c%s:%d-%d", "><"[rev], g->sseq[last_pnid].name, st, en);
last_pnid = t->snid, rev = q->v&1, st = t->soff, en = st + t->len;
}
}
}
if (last_pnid >= 0) {
if (g->sseq[last_pnid].rank != 0 || g->sseq[last_pnid].min != 0)
compact = 0;
if (!compact) mg_sprintf_lite(s, "%c%s:%d-%d", "><"[rev], g->sseq[last_pnid].name, st, en);
} else compact = 0;
}
if (compact) {
int32_t rev = gs->lc[p->off].v&1;
const gfa_seg_t *t = &g->seg[gs->lc[rev? p->off + p->cnt - 1 : p->off].v>>1];
const gfa_sseq_t *ps = &g->sseq[t->snid];
mg_sprintf_lite(s, "%s\t%d\t", ps->name, ps->max);
if (rev) {
s->s[sign_pos] = '-';
mg_sprintf_lite(s, "%d\t%d", t->soff + (p->plen - p->pe), t->soff + (p->plen - p->ps));
} else {
mg_sprintf_lite(s, "%d\t%d", t->soff + p->ps, t->soff + p->pe);
}
} else mg_sprintf_lite(s, "\t%d\t%d\t%d", p->plen, p->ps, p->pe);
mg_sprintf_lite(s, "\t%d\t%d\t%d", p->mlen, p->blen, p->mapq);
mg_sprintf_lite(s, "\ttp:A:%c\tcm:i:%d\ts1:i:%d\ts2:i:%d", p->id == p->parent? 'P' : 'S', p->n_anchor, p->score, p->subsc);
if (p->div >= 0.0f && p->div <= 1.0f) {
char buf[16];
if (p->div == 0.0f) buf[0] = '0', buf[1] = 0;
else snprintf(buf, 16, "%.4f", p->div);
mg_sprintf_lite(s, "\tdv:f:%s", buf);
}
if (n_seg > 1) {
mg_sprintf_lite(s, "\tql:B:i");
for (j = 0; j < n_seg; ++j) mg_sprintf_lite(s, ",%d", qlens[j]);
}
mg_sprintf_lite(s, "\n");
if (mg_dbg_flag & MG_DBG_LCHAIN) {
for (j = 0; j < p->cnt; ++j) {
const mg_llchain_t *lc = &gs->lc[p->off + j];
mg_sprintf_lite(s, "- %s\t%d", g->seg[lc->v>>1].name, lc->cnt);
if (lc->cnt > 0) {
mg_sprintf_lite(s, "\t%d\t%d", (int32_t)gs->a[lc->off].y + 1 - (int32_t)(gs->a[lc->off].y>>32&0xff), (int32_t)gs->a[lc->off + lc->cnt - 1].y + 1);
}
mg_sprintf_lite(s, "\n");
}
}
}
}