Repository navigation
Expand file tree
/
Copy pathsample.c
More file actions
177 lines (159 loc) · 5.33 KB
/
Copy pathsample.c
File metadata and controls
177 lines (159 loc) · 5.33 KB
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
/* sample.c -- group data by sample.
Copyright (C) 2010, 2011 Broad Institute.
Copyright (C) 2013, 2026 Genome Research Ltd.
Author: Heng Li <lh3@sanger.ac.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. */
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include "sample.h"
#include "htslib/khash.h"
KHASH_MAP_INIT_STR(sm, int)
bam_sample_t *bam_smpl_init(void)
{
bam_sample_t *s;
s = calloc(1, sizeof(bam_sample_t));
if (!s)
return NULL;
s->rg2smid = kh_init(sm);
if (!s->rg2smid) {
free(s);
return NULL;
}
s->sm2id = kh_init(sm);
if (!s->sm2id) {
kh_destroy(sm, s->rg2smid);
free(s);
return NULL;
}
return s;
}
void bam_smpl_destroy(bam_sample_t *sm)
{
int i;
khint_t k;
if (sm == NULL) return;
if (sm->smpl) {
for (i = 0; i < sm->n; ++i) free(sm->smpl[i]);
free(sm->smpl);
}
if (sm->rg2smid) {
khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
for (k = kh_begin(rg2smid); k != kh_end(rg2smid); ++k)
if (kh_exist(rg2smid, k)) free((char*)kh_key(rg2smid, k));
kh_destroy(sm, sm->rg2smid);
}
kh_destroy(sm, sm->sm2id);
free(sm);
}
static int add_pair(bam_sample_t *sm, khash_t(sm) *sm2id, const char *key, const char *val)
{
khint_t k_rg, k_sm;
int ret;
khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
k_rg = kh_get(sm, rg2smid, key);
if (k_rg != kh_end(rg2smid)) return 0; // duplicated @RG-ID
k_sm = kh_get(sm, sm2id, val);
if (k_sm == kh_end(sm2id)) { // absent
if (sm->n == sm->m) {
int new_m = sm->m? sm->m<<1 : 1;
char **new_smpl = realloc(sm->smpl, sizeof(char*) * new_m);
if (!new_smpl)
return -1;
sm->smpl = new_smpl;
sm->m = new_m;
}
sm->smpl[sm->n] = strdup(val);
if (!sm->smpl[sm->n])
return -1;
k_sm = kh_put(sm, sm2id, sm->smpl[sm->n], &ret);
if (ret < 0) {
free(sm->smpl[sm->n]);
return -1;
}
kh_val(sm2id, k_sm) = sm->n++;
}
char *key_copy = strdup(key);
k_rg = kh_put(sm, rg2smid, key_copy, &ret);
if (ret < 0) {
free(key_copy);
return -1;
}
kh_val(rg2smid, k_rg) = kh_val(sm2id, k_sm);
return 0;
}
int bam_smpl_add(bam_sample_t *sm, const char *fn, const char *txt)
{
const char *p = txt, *q, *r;
kstring_t buf = KS_INITIALIZE, first_sm = KS_INITIALIZE;
int n = 0;
khash_t(sm) *sm2id = (khash_t(sm)*)sm->sm2id;
if (txt == 0) {
return add_pair(sm, sm2id, fn, fn);
}
while ((q = strstr(p, "@RG")) != 0) {
p = q + 3;
r = q = 0;
if ((q = strstr(p, "\tID:")) != 0) q += 4;
if ((r = strstr(p, "\tSM:")) != 0) r += 4;
if (r && q) {
char *u, *v;
int oq, or;
for (u = (char*)q; *u && *u != '\t' && *u != '\n'; ++u);
for (v = (char*)r; *v && *v != '\t' && *v != '\n'; ++v);
oq = *u; or = *v; *u = *v = '\0';
buf.l = 0;
if (kputs(fn, &buf) < 0) goto fail;
if (kputc('/', &buf) < 0) goto fail;
if (kputs(q, &buf) < 0) goto fail;
if (add_pair(sm, sm2id, buf.s, r) , 0) goto fail;
if ( !first_sm.s )
if (kputs(r,&first_sm) < 0) goto fail;
*u = oq; *v = or;
} else break;
p = q > r? q : r;
++n;
}
if (n == 0) {
if (add_pair(sm, sm2id, fn, fn) < 0) goto fail;
// If there is only one RG tag present in the header and reads are not annotated, don't refuse to work but
// use the tag instead.
} else if ( n==1 && first_sm.s ) {
if (add_pair(sm,sm2id,fn,first_sm.s) < 0) goto fail;
}
ks_free(&first_sm);
// add_pair(sm, sm2id, fn, fn);
ks_free(&buf);
return 0;
fail:
ks_free(&first_sm);
ks_free(&buf);
return -1;
}
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;
if (kputs(fn, str) < 0) return -2;
if (kputc('/', str) < 0) return -2;
if (kputs(rg, str) < 0) return -2;
k = kh_get(sm, rg2smid, str->s);
} else k = kh_get(sm, rg2smid, fn);
return k == kh_end(rg2smid)? -1 : kh_val(rg2smid, k);
}