-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpop_sample.cpp
264 lines (226 loc) · 6.8 KB
/
pop_sample.cpp
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
/** \file pop_sample.cpp
* \brief Functions for parsing samples in a BAM file
* \author Daniel Garrigan
* \version 0.3
*/
#include "popbam.h"
#include "khash.h"
KHASH_MAP_INIT_STR(sm, int);
static void add_sample_pair(bam_sample_t*, khash_t(sm)*, const char*, const char*);
static void add_pop_pair(bam_sample_t*, khash_t(sm)*, const char*, const char*);
int popbamData::bam_smpl_add(void)
{
int n;
const char *p;
const char *q;
const char *r;
const char *s;
kstring_t buf;
kstring_t bug;
khash_t(sm) *sm2id = (khash_t(sm)*)sm->sm2id;
khash_t(sm) *pop2sm = (khash_t(sm)*)sm->pop2sm;
p = h->text;
n = 0;
memset(&buf, 0, sizeof(kstring_t));
memset(&bug, 0, sizeof(kstring_t));
while ((q = strstr(p, "@RG")) != 0)
{
p = q+3;
r = q = s = 0;
if ((q = strstr(p, "\tID:")) != 0)
q += 4;
if ((r = strstr(p, "\tSM:")) != 0)
r += 4;
if ((s = strstr(p, "\tPO:")) != 0)
s += 4;
// if no PO tag is found in the header
if (r && q && !s)
{
char *u;
char *v;
int oq;
int or1;
for (u=(char*)q; *u && *u != '\t' && *u != '\n'; ++u);
for (v=(char*)r; *v && *v != '\t' && *v != '\n'; ++v);
oq = *u;
or1 = *v;
*u = *v = '\0';
buf.l = 0;
kputs(bamfile.c_str(), &buf);
kputc('/', &buf);
kputs(q, &buf);
add_sample_pair(sm, sm2id, buf.s, r);
*u = oq;
*v = or1;
}
// if PO tag is found
else if (r && q && s)
{
char *u, *v, *w;
int oq, or1, os;
for (u=(char *)q; *u && *u != '\t' && *u != '\n'; ++u);
for (v=(char *)r; *v && *v != '\t' && *v != '\n'; ++v);
for (w=(char *)s; *w && *w != '\t' && *w != '\n'; ++w);
oq = *u;
or1 = *v;
os = *w;
*u = *v = *w = '\0';
buf.l = 0;
bug.l = 0;
kputs(bamfile.c_str(), &buf);
kputs(bamfile.c_str(), &bug);
kputc('/', &buf);
kputc('/', &bug);
kputs(q, &buf);
kputs(r, &bug);
add_sample_pair(sm, sm2id, buf.s, r);
add_pop_pair(sm, pop2sm, bug.s, s);
*u = oq;
*v = or1;
*w = os;
}
// no read group header line is found
else
break;
p = q > r ? q : r;
p = p > s ? p : s;
++n;
}
if (n == 0)
{
add_sample_pair(sm, sm2id, bamfile.c_str(), bamfile.c_str());
add_pop_pair(sm, sm2id, bamfile.c_str(), bamfile.c_str());
}
free(buf.s);
free(bug.s);
return 0;
}
void popbamData::bam_smpl_init(void)
{
sm = (bam_sample_t*)calloc(1, sizeof(bam_sample_t));
sm->sm2popid = kh_init(sm);
sm->rg2smid = kh_init(sm);
sm->sm2id = kh_init(sm);
sm->pop2sm = kh_init(sm);
}
void popbamData::bam_smpl_destroy(void)
{
int i;
khint_t k;
khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
khash_t(sm) *sm2popid = (khash_t(sm)*)sm->sm2popid;
if (sm == 0)
return;
for (i=0; i < sm->n; i++)
free(sm->smpl[i]);
free(sm->smpl);
for (i=0; i < sm->npops; i++)
free(sm->popul[i]);
free(sm->popul);
//dealloc strdups
for (k=kh_begin(rg2smid); k != kh_end(rg2smid); ++k)
if (kh_exist(rg2smid, k))
free((char*)kh_key(rg2smid, k));
for (k=kh_begin(sm2popid); k != kh_end(sm2popid); ++k)
if (kh_exist(sm2popid, k))
free((char*)kh_key(sm2popid, k));
kh_destroy(sm, static_cast<kh_sm_t*>(sm->sm2popid));
kh_destroy(sm, static_cast<kh_sm_t*>(sm->rg2smid));
kh_destroy(sm, static_cast<kh_sm_t*>(sm->sm2id));
kh_destroy(sm, static_cast<kh_sm_t*>(sm->pop2sm));
free(sm);
}
static void add_sample_pair(bam_sample_t *sm, khash_t(sm) *sm2id, const char *key, const char *val)
{
int ret;
khint_t k_rg, k_sm;
khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
k_rg = kh_get(sm, rg2smid, key);
if (k_rg != kh_end(rg2smid))
return;
else
k_rg = kh_put(sm, rg2smid, strdup(key), &ret);
k_sm = kh_get(sm, sm2id, val);
if (k_sm == kh_end(sm2id))
{
//need to allocate for more samples?
if (sm->n == sm->m)
{
sm->m = sm->m ? sm->m << 1 : 1;
sm->smpl = static_cast<char**>(realloc(sm->smpl, sizeof(void*)*sm->m));
}
//add sample name
sm->smpl[sm->n] = strdup(val);
//add new entry in sm2id hash table
k_sm = kh_put(sm, sm2id, sm->smpl[sm->n], &ret);
if (ret > 0)
kh_val(sm2id, k_sm) = sm->n++;
}
//update rg2smid hash table
kh_val(rg2smid, k_rg) = kh_val(sm2id, k_sm);
}
static void add_pop_pair(bam_sample_t *sm, khash_t(sm) *pop2sm, const char *key, const char *val)
{
int ret;
khint_t k_rg, k_sm;
khash_t(sm) *sm2popid = (khash_t(sm)*)sm->sm2popid;
//is the key already in the sm2popid hash table?
k_rg = kh_get(sm, sm2popid, key);
//duplicated @RG-ID
if (k_rg != kh_end(sm2popid))
return;
else
k_rg = kh_put(sm, sm2popid, strdup(key), &ret);
//does val appear as a key in the pop2sm hash table?
k_sm = kh_get(sm, pop2sm, val);
//if val does not appears as a key
if (k_sm == kh_end(pop2sm))
{
//need to allocate more samples?
if (sm->b == sm->npops)
{
sm->b = sm->b ? sm->b << 1 : 1;
sm->popul = static_cast<char**>(realloc(sm->popul, sizeof(void*)*sm->b));
}
//add sample name
sm->popul[sm->npops] = strdup(val);
//add new entry in pop2sm hash table
k_sm = kh_put(sm, pop2sm, sm->popul[sm->npops], &ret);
if (ret > 0)
kh_val(pop2sm, k_sm) = sm->npops++;
}
//update sm2popid hash table
kh_val(sm2popid, k_rg) = kh_val(pop2sm, k_sm);
}
int bam_smpl_rg2smid(const bam_sample_t *sm, const char *fn, const char *rg, kstring_t *str)
{
khint_t k;
khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
if (rg)
{
str->l = 0;
kputs(fn, str);
kputc('/', str);
kputs(rg, str);
k = kh_get(sm, rg2smid, str->s);
}
else
k = kh_get(sm, rg2smid, fn);
return k == kh_end(rg2smid) ? -1 : kh_val(rg2smid, k);
}
int bam_smpl_sm2popid(const bam_sample_t *sm, const char *fn, const char *smpl, kstring_t *str)
{
khint_t k;
khash_t(sm) *sm2popid = (khash_t(sm)*)sm->sm2popid;
if (smpl)
{
str->l = 0;
kputs(fn, str);
kputc('/', str);
kputs(smpl, str);
k = kh_get(sm, sm2popid, str->s);
}
else
k = kh_get(sm, sm2popid, fn);
return k == kh_end(sm2popid) ? -1 : kh_val(sm2popid, k);
}