-
Notifications
You must be signed in to change notification settings - Fork 8
/
hit.c
247 lines (231 loc) · 6.54 KB
/
hit.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
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "pgpriv.h"
#include "ksort.h"
#define sort_key_128x(a) ((a).x)
KRADIX_SORT_INIT(pg128x, pg128_t, sort_key_128x, 8)
#define sort_key_generic(a) (a)
KRADIX_SORT_INIT(pg64, uint64_t, sort_key_generic, 8)
int64_t pg_hit_cal_cm(const pg_hit_t *a, const pg_exon_t *e)
{
int32_t i, len, half;
for (i = 0, len = 0; i < a->n_exon; ++i)
len += e[i].oe - e[i].os;
half = len>>1;
for (i = 0, len = 0; i < a->n_exon; ++i) {
if (len <= half && half < len + e[i].oe - e[i].os)
return a->cs + e[i].os + half - len;
len += e[i].oe - e[i].os;
}
abort();
return -1;
}
void pg_hit_sort(pg_genome_t *g, int32_t by_cm)
{
int32_t i, *n, *off;
pg_hit_t *a;
pg128_t *tmp;
n = PG_CALLOC(int32_t, g->n_ctg);
for (i = 0; i < g->n_hit; ++i)
++n[g->hit[i].cid];
off = PG_CALLOC(int32_t, g->n_ctg + 1);
for (i = 1; i <= g->n_ctg; ++i)
off[i] = off[i - 1] + n[i - 1];
tmp = PG_MALLOC(pg128_t, g->n_hit);
for (i = 0; i < g->n_hit; ++i) {
pg128_t t;
t.x = by_cm? g->hit[i].cm : g->hit[i].cs;
t.y = i;
tmp[off[g->hit[i].cid]++] = t;
}
for (i = 1, off[0] = 0; i <= g->n_ctg; ++i)
off[i] = off[i - 1] + n[i - 1];
for (i = 0; i < g->n_ctg; ++i)
radix_sort_pg128x(&tmp[off[i]], &tmp[off[i+1]]);
free(off);
free(n);
a = PG_MALLOC(pg_hit_t, g->n_hit);
for (i = 0; i < g->n_hit; ++i)
a[i] = g->hit[tmp[i].y];
free(tmp);
free(g->hit);
g->hit = a;
}
int32_t pg_flag_pseudo(const pg_prot_t *prot, pg_genome_t *g)
{
int32_t i, i0, j, n_pseudo = 0;
pg128_t *a;
a = PG_MALLOC(pg128_t, g->n_hit);
for (i = 0; i < g->n_hit; ++i) {
a[i].x = (uint64_t)g->hit[i].pid<<32 | g->hit[i].rank;
a[i].y = i;
}
radix_sort_pg128x(a, a + g->n_hit);
for (i = 1, i0 = 0; i <= g->n_hit; ++i) {
if (i == g->n_hit || a[i].x>>32 != a[i0].x>>32) {
int32_t max_n = 0, min_n = INT32_MAX;
for (j = i0; j < i; ++j) {
int32_t n_exon = g->hit[a[j].y].n_exon;
max_n = max_n > n_exon? max_n : n_exon;
min_n = min_n < n_exon? min_n : n_exon;
}
if (max_n > 1 && (min_n == 1 || min_n * 2 <= max_n)) {
int32_t j1 = -1;
for (j = i0; j < i; ++j) {
int32_t n_exon = g->hit[a[j].y].n_exon;
if (n_exon == 1 || n_exon * 2 <= max_n)
g->hit[a[j].y].pseudo = 1, ++n_pseudo;
else if (j1 < 0)
j1 = j;
}
assert(j1 >= 0);
if (g->hit[a[j1].y].rank > 0) { // promote the first multi-exon hit to rank 0
for (j = i0; j < j1; ++j)
g->hit[a[j].y].rank++;
g->hit[a[j1].y].rank = 0;
}
}
i0 = i;
}
}
free(a);
return n_pseudo;
}
int32_t pg_flt_subopt_isoform(const pg_prot_t *prot, int32_t n_gene, pg_genome_t *g)
{
uint64_t *best;
int32_t i, gid, n_flt = 0;
best = PG_CALLOC(uint64_t, n_gene);
for (i = 0; i < g->n_hit; ++i) {
const pg_hit_t *a = &g->hit[i];
if (a->flt || a->rank > 0) continue;
gid = prot[a->pid].gid;
if (a->score_adj > best[gid]>>32)
best[gid] = (uint64_t)a->score_adj << 32 | a->pid;
}
for (i = 0; i < g->n_hit; ++i) {
pg_hit_t *a = &g->hit[i];
if (a->flt) continue;
gid = prot[a->pid].gid;
if (a->pid != (int32_t)best[gid])
a->flt = a->flt_iso_sub_self = 1, ++n_flt;
}
free(best);
return n_flt;
}
int32_t pg_flt_chain_shadow(const pg_prot_t *prot, int32_t n_prot, pg_genome_t *g)
{
int32_t i, n_flt = 0;
int8_t *flag;
flag = PG_CALLOC(int8_t, n_prot);
for (i = 0; i < n_prot; ++i) flag[i] = 1;
for (i = 0; i < g->n_hit; ++i)
if (!g->hit[i].flt_iso_ov)
flag[g->hit[i].pid] = 0;
for (i = 0; i < g->n_hit; ++i) {
pg_hit_t *a = &g->hit[i];
if (a->pid_dom0 >= 0 && flag[a->pid_dom0])
a->flt = a->flt_chain = 1, ++n_flt;
}
free(flag);
return n_flt;
}
typedef struct {
int32_t c[2];
int64_t s[2];
} pseudo_joint_aux_t;
int32_t pg_flag_pseudo_joint(const pg_opt_t *opt, pg_data_t *d) // call after pg_flag_pseudo()
{
int32_t i, j, n_pseudo = 0;
pseudo_joint_aux_t *aux;
aux = PG_CALLOC(pseudo_joint_aux_t, d->n_prot);
for (j = 0; j < d->n_genome; ++j) {
const pg_genome_t *g = &d->genome[j];
for (i = 0; i < g->n_hit; ++i) {
const pg_hit_t *a = &g->hit[i];
if (a->flt) continue;
if (a->rank == 0) {
int32_t w = a->n_exon == 1? 0 : 1;
aux[a->pid].c[w]++;
aux[a->pid].s[w] += a->score_ori;
}
}
}
for (j = 0; j < d->n_genome; ++j) {
pg_genome_t *g = &d->genome[j];
for (i = 0; i < g->n_hit; ++i) {
pg_hit_t *a = &g->hit[i];
pseudo_joint_aux_t *p = &aux[a->pid];
if (a->flt || a->pseudo) continue;
if (a->n_exon == 1 && p->c[1] > 0 && p->c[1] >= d->n_genome * opt->min_vertex_ratio
&& ((double)p->s[1] / p->c[1]) / ((double)p->s[0] / p->c[0]) >= 0.99) // multi-exon should have higher score
{
a->pseudo = 1, ++n_pseudo;
} else if (a->n_exon == 1 && (p->c[1] == 0 || p->c[1] <= d->n_genome * opt->min_vertex_ratio) && (opt->flag & PG_F_DROP_SGL_EXON)) {
a->pseudo = 1, ++n_pseudo;
}
}
}
free(aux);
return n_pseudo;
}
void pg_flag_representative(pg_data_t *d) // flag representative isoform
{
pg128_t *z;
int32_t i, j;
z = PG_CALLOC(pg128_t, d->n_prot);
for (i = 0; i < d->n_gene; ++i) d->gene[i].rep_pid = -1;
for (i = 0; i < d->n_prot; ++i) z[i].y = i, d->prot[i].rep = 0;
for (j = 0; j < d->n_genome; ++j) {
pg_genome_t *g = &d->genome[j];
for (i = 0; i < g->n_hit; ++i) {
pg_hit_t *a = &g->hit[i];
if (a->rank == 0 && a->flt == 0)
z[a->pid].x += (uint64_t)a->score_adj<<32 | 1; // NB: assuming each protein has only one rank=0 hit
a->rep = 0;
}
}
for (i = 0; i < d->n_prot; ++i) {
d->prot[i].n = (uint32_t)z[i].x;
d->prot[i].avg_score_adj = d->prot[i].n? (int32_t)((double)(z[i].x>>32) / d->prot[i].n + .499) : 0;
}
radix_sort_pg128x(z, z + d->n_prot);
for (i = d->n_prot - 1; i >= 0; --i) {
int32_t pid = z[i].y;
int32_t gid = d->prot[pid].gid;
if (d->gene[gid].rep_pid < 0) {
d->gene[gid].rep_pid = pid;
d->prot[pid].rep = 1;
}
}
free(z);
for (j = 0; j < d->n_genome; ++j) {
pg_genome_t *g = &d->genome[j];
for (i = 0; i < g->n_hit; ++i)
if (d->prot[g->hit[i].pid].rep)
g->hit[i].rep = 1;
}
}
void pg_cap_score_dom(pg_data_t *d)
{
int32_t i, j;
for (i = 0; i < d->n_prot; ++i) d->prot[i].max_score_ori = 0;
for (j = 0; j < d->n_genome; ++j) {
pg_genome_t *g = &d->genome[j];
for (i = 0; i < g->n_hit; ++i) {
pg_hit_t *a = &g->hit[i];
pg_prot_t *p = &d->prot[a->pid];
p->max_score_ori = p->max_score_ori > a->score_ori? p->max_score_ori : a->score_ori;
}
}
for (j = 0; j < d->n_genome; ++j) {
pg_genome_t *g = &d->genome[j];
for (i = 0; i < g->n_hit; ++i) {
pg_hit_t *a = &g->hit[i];
if (a->score_dom > d->prot[a->pid].max_score_ori)
a->score_dom = d->prot[a->pid].max_score_ori;
}
}
}