-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathread.c
318 lines (305 loc) · 9.18 KB
/
read.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <math.h>
#include <zlib.h>
#include "pgpriv.h"
#include "kseq.h"
KSTREAM_INIT(gzFile, gzread, 0x10000)
pg_data_t *pg_data_init(void)
{
pg_data_t *d;
d = PG_CALLOC(pg_data_t, 1);
d->d_ctg = pg_dict_init(1);
d->d_gene = pg_dict_init(1);
d->d_prot = pg_dict_init(1);
return d;
}
void pg_data_destroy(pg_data_t *d)
{
int32_t i;
for (i = 0; i < d->n_genome; ++i) {
pg_genome_t *g = &d->genome[i];
free(g->ctg); free(g->hit); free(g->exon); free(g->label);
}
free(d->genome); free(d->gene); free(d->prot);
pg_dict_destroy(d->d_ctg);
pg_dict_destroy(d->d_gene);
pg_dict_destroy(d->d_prot);
free(d);
}
typedef struct {
int32_t n_exon, m_exon;
pg_exon_t *exon;
} pg_exons_t;
static inline void pg_add_exon(pg_exons_t *tmp, int32_t st)
{
pg_exon_t *p;
PG_GROW(pg_exon_t, tmp->exon, tmp->n_exon, tmp->m_exon);
p = &tmp->exon[tmp->n_exon++];
p->os = p->oe = st;
}
static void pg_parse_cigar(pg_data_t *d, pg_genome_t *g, pg_hit_t *hit, pg_exons_t *tmp, const char *cg)
{
const char *p = cg;
char *r;
int64_t x = 0;
int32_t i, n_fs = 0;
pg_exon_t *t;
tmp->n_exon = 0;
pg_add_exon(tmp, 0);
while (*p) {
int64_t l;
l = strtol(p, &r, 10);
if (*r == 'N' || *r == 'U' || *r == 'V') {
int64_t st, en;
if (*r == 'N') st = x, en = x + l;
else if (*r == 'U') st = x + 1, en = x + l - 2;
else st = x + 2, en = x + l - 1;
tmp->exon[tmp->n_exon - 1].oe = st;
pg_add_exon(tmp, en);
x += l;
} else if (*r == 'M' || *r == 'X' || *r == '=' || *r == 'D') {
x += l * 3;
} else if (*r == 'F' || *r == 'G') {
x += l, ++n_fs;
}
p = r + 1;
}
tmp->exon[tmp->n_exon - 1].oe = x;
assert(x == hit->ce - hit->cs);
PG_GROW(pg_exon_t, g->exon, g->n_exon + tmp->n_exon - 1, g->m_exon);
t = &g->exon[g->n_exon];
if (!hit->rev) {
memcpy(t, tmp->exon, tmp->n_exon * sizeof(pg_exon_t));
} else {
for (i = tmp->n_exon - 1; i >= 0; --i, ++t) {
t->os = x - tmp->exon[i].oe;
t->oe = x - tmp->exon[i].os;
}
}
hit->n_exon = tmp->n_exon;
hit->off_exon = g->n_exon;
hit->lof = n_fs;
g->n_exon += tmp->n_exon;
}
static char *pg_read_label(const char *fn)
{
char *label;
int32_t st = 0, en, i, len;
len = en = strlen(fn);
for (i = len - 1; i >= 0 && fn[i] != '/'; --i) {}
st = i + 1;
if (strncmp(&fn[en-3], ".gz", 3) == 0) en -= 3;
if (strncmp(&fn[en-4], ".paf", 4) == 0) en -= 4;
if (st >= en) return 0;
label = PG_CALLOC(char, en - st + 1);
strncpy(label, &fn[st], en - st);
return label;
}
int32_t pg_read_paf(const pg_opt_t *opt, pg_data_t *d, const char *fn)
{
gzFile fp;
kstream_t *ks;
kstring_t str = {0,0,0};
int32_t dret, absent, n_tot = 0, check_strand = !!(opt->flag&PG_F_CHECK_STRAND);
void *d_ctg, *hit_rank;
pg_genome_t *g;
pg_exons_t buf = {0,0,0};
fp = fn && strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(0, "r");
if (fp == 0) return -1;
hit_rank = pg_dict_init(0);
d_ctg = pg_dict_init(0);
PG_GROW0(pg_genome_t, d->genome, d->n_genome, d->m_genome);
g = &d->genome[d->n_genome++];
memset(g, 0, sizeof(*g));
g->label = pg_read_label(fn);
ks = ks_init(fp);
while (ks_getuntil(ks, KS_SEP_LINE, &str, &dret) >= 0) {
char *p, *q, *r;
int32_t i, pid, gid, n_fs = -1, n_stop = -1;
pg_hit_t hit;
++n_tot;
memset(&hit, 0, sizeof(hit));
hit.pid = hit.pid_dom = hit.cid = hit.off_exon = hit.n_exon = -1;
for (p = q = str.s, i = 0;; ++p) {
if (*p == '\t' || *p == 0) {
int32_t c = *p;
*p = 0;
if (i == 0) { // query name
int32_t rank, preferred = 0, included = 0, has_delim = 0;
const char *tmp;
for (r = q; r < p && *r != opt->gene_delim; ++r) {}
// add gene
if (opt->excl && pg_dict_get(opt->excl, q) >= 0) break; // excluded by transcript name
if (*r == opt->gene_delim) has_delim = 1, *r = 0;
if (opt->excl && pg_dict_get(opt->excl, q) >= 0) break; // excluded by gene name
if (opt->preferred && pg_dict_get(opt->preferred, q) >= 0)
preferred = 1;
if (opt->incl && pg_dict_get(opt->incl, q) >= 0)
included = 1;
tmp = *pg_dict_put(d->d_gene, q, pg_dict_size(d->d_gene), &gid, &absent);
if (has_delim) *r = opt->gene_delim;
if (absent) {
d->n_gene++;
PG_GROW0(pg_gene_t, d->gene, gid, d->m_gene);
}
d->gene[gid].name = tmp;
d->gene[gid].preferred = preferred;
d->gene[gid].included = included;
// add protein
tmp = *pg_dict_put(d->d_prot, q, pg_dict_size(d->d_prot), &pid, &absent);
if (absent) { // protein is new
d->n_prot++;
PG_GROW0(pg_prot_t, d->prot, pid, d->m_prot);
}
d->prot[pid].name = tmp;
d->prot[pid].gid = gid;
d->prot[pid].len = 0;
hit.pid = pid;
rank = pg_dict_inc(hit_rank, d->prot[pid].name, 0);
hit.rank = rank;
} else if (i == 1) { // query length
int32_t len;
len = strtol(q, &r, 10);
assert(d->prot[pid].len == 0 || d->prot[pid].len == len);
d->prot[pid].len = len;
d->gene[gid].len = d->gene[gid].len > len? d->gene[gid].len : len;
} else if (i == 2) { // query start
hit.qs = strtol(q, &r, 10);
} else if (i == 3) { // query end
hit.qe = strtol(q, &r, 10);
if (hit.qe - hit.qs < d->prot[hit.pid].len * opt->min_prot_ratio) // alignment fraction too low
break;
} else if (i == 4) { // strand
if (*q != '+' && *q != '-') break;
hit.rev = *q == '+'? 0 : 1;
} else if (i == 5) { // contig name
int32_t cid;
const char **ret;
ret = pg_dict_put(d_ctg, q, pg_dict_size(d_ctg), &cid, &absent);
if (absent) { // a new contig not seen in this PAF file
const char *name;
PG_GROW0(pg_ctg_t, g->ctg, g->n_ctg, g->m_ctg);
name = *pg_dict_put(d->d_ctg, q, pg_dict_size(d->d_ctg), 0, 0);
g->ctg[g->n_ctg++].name = *ret = name;
}
assert(cid < g->m_ctg);
hit.cid = cid;
} else if (i == 6) { // contig length
g->ctg[hit.cid].len = strtol(q, &r, 10);
} else if (i == 7) { // contig start
hit.cs = strtol(q, &r, 10);
} else if (i == 8) { // contig end
hit.ce = strtol(q, &r, 10);
} else if (i == 9) { // matching length
hit.mlen = strtol(q, &r, 10);
} else if (i == 10) { // block length
hit.blen = strtol(q, &r, 10);
if (hit.mlen < hit.blen * opt->min_prot_iden) // identity too low
break;
} else if (i >= 12) { // tags
if (strncmp(q, "ms:i:", 5) == 0) { // score
double div = 1.0 - (double)hit.mlen / hit.blen;
double uncov = 1.0 - (double)(hit.qe - hit.qs) / d->prot[hit.pid].len;
hit.score_ori = strtol(q + 5, &r, 10);
hit.score_adj = (int32_t)(hit.score_ori * expl(-opt->score_adj_coef * (div + uncov)) + .499);
} else if (strncmp(q, "fs:i:", 5) == 0) { // number of frameshifts
n_fs = strtol(q + 5, &r, 10);
} else if (strncmp(q, "st:i:", 5) == 0) { // number of stop codons
n_stop = strtol(q + 5, &r, 10);
} else if (strncmp(q, "cg:Z:", 5) == 0) { // CIGAR
pg_parse_cigar(d, g, &hit, &buf, q + 5);
}
}
q = p + 1, ++i;
if (c == 0) break;
}
}
if (hit.n_exon >= 1) {
int32_t lof = (n_fs > 0? n_fs : 0) + (n_stop > 0? n_stop : 0);
hit.lof = hit.lof > lof? hit.lof : lof;
PG_GROW0(pg_hit_t, g->hit, g->n_hit, g->m_hit);
hit.cm = pg_hit_cal_cm(&hit, &g->exon[hit.off_exon]);
g->hit[g->n_hit++] = hit;
}
}
free(buf.exon);
pg_dict_destroy(d_ctg);
pg_dict_destroy(hit_rank);
free(str.s);
ks_destroy(ks);
gzclose(fp);
{ // postprocessing
int32_t i, n_pseudo = 0, n_flt_subopt = 0, n_flt_ov_iso = 0, n_flt_chain = 0;
n_pseudo = pg_flag_pseudo(d->prot, g);
PG_SET_FILTER(d, pseudo == 1);
pg_hit_sort(g, 0);
pg_shadow(opt, d, d->n_genome - 1, 1, check_strand);
for (i = 0; i < g->n_hit; ++i) {
pg_hit_t *a = &g->hit[i];
a->pid_dom0 = a->pid_dom;
a->pid_dom = -1, a->shadow = 0; // reset
}
n_flt_ov_iso = pg_flt_ov_isoform(opt, d, d->n_genome - 1, check_strand);
n_flt_chain = pg_flt_chain_shadow(d->prot, d->n_prot, g);
n_flt_subopt = pg_flt_subopt_isoform(d->prot, d->n_gene, g);
if (pg_verbose >= 3)
fprintf(stderr, "[M::%s::%s] [%d] %s: %d hits parsed, %d kept and %d+%d+%d+%d filtered\n",
__func__, pg_timestamp(), d->n_genome-1, g->label, n_tot, g->n_hit, n_pseudo, n_flt_ov_iso, n_flt_chain, n_flt_subopt);
}
return 0;
}
// adapted from gfa_read_list() in gfatools
char **pg_read_list(const char *o, int *n_)
{
int n = 0, m = 0;
char **s = 0;
*n_ = 0;
if (*o != '@') {
const char *q = o, *p;
for (p = q;; ++p) {
if (*p == ',' || *p == ' ' || *p == '\t' || *p == 0) {
if (p - q > 0) {
PG_GROW0(char*, s, n, m);
s[n++] = pg_strndup(q, p - q);
}
if (*p == 0) break;
q = p + 1;
}
}
} else {
gzFile fp;
kstream_t *ks;
kstring_t str = {0,0,0};
int dret;
fp = gzopen(o + 1, "r");
if (fp == 0) return 0;
ks = ks_init(fp);
while (ks_getuntil(ks, KS_SEP_LINE, &str, &dret) >= 0) {
char *p;
for (p = str.s; *p && !isspace(*p); ++p);
PG_GROW0(char*, s, n, m);
s[n++] = pg_strndup(str.s, p - str.s);
}
ks_destroy(ks);
gzclose(fp);
}
if (s) s = PG_REALLOC(char*, s, n);
*n_ = n;
return s;
}
void *pg_read_list_dict(const char *o)
{
int i, n, absent;
char **s;
void *d;
s = pg_read_list(o, &n);
d = pg_dict_init(0);
for (i = 0; i < n; ++i) {
pg_dict_put(d, s[i], i, 0, &absent);
if (!absent) free(s[i]);
}
free(s);
return d;
}