-
Notifications
You must be signed in to change notification settings - Fork 240
/
bam_sample.c
398 lines (360 loc) · 13.4 KB
/
bam_sample.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* bam_sample.c -- group data by sample.
Copyright (C) 2010, 2011 Broad Institute.
Copyright (C) 2013, 2016-2022 Genome Research Ltd.
Author: Heng Li <[email protected]>, Petr Danecek <[email protected]>
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 <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <htslib/hts.h>
#include <htslib/kstring.h>
#include <htslib/khash_str2int.h>
#include <khash_str2str.h>
#include "bam_sample.h"
#include "bcftools.h"
typedef struct
{
char *fname;
void *rg2idx; // hash: read group name to BCF output sample index. Maintained by bsmpl_add_readgroup
int default_idx; // default BCF output sample index, set only when all readgroups are treated as one sample
}
file_t;
struct _bam_smpl_t
{
kstring_t tmp;
file_t *files;
int ignore_rg, nsmpl, nfiles;
char **smpl; // list of BCF output sample names. Maintained by bsmpl_add_readgroup
void *sample_list; // hash: BAM input sample name to BCF output sample name. This is the -s/-S list
int sample_logic; // the -s/-S logic, 1: include, 0: exclude
void *rg_list; // hash: BAM/rg_id to sample name or */rg_id for global ids. This is the -G list
int rg_logic; // the -G logic, 1: include, 0: exclude
void *name2idx; // hash: BCF output sample name to BCF output sample index. Maintained by bsmpl_add_readgroup
};
bam_smpl_t *bam_smpl_init(void)
{
bam_smpl_t *bsmpl;
bsmpl = (bam_smpl_t*) calloc(1, sizeof(bam_smpl_t));
bsmpl->name2idx = khash_str2int_init();
return bsmpl;
}
void bam_smpl_destroy(bam_smpl_t *bsmpl)
{
if ( !bsmpl ) return;
if ( bsmpl->name2idx ) khash_str2int_destroy_free(bsmpl->name2idx);
if ( bsmpl->sample_list ) khash_str2str_destroy_free_all(bsmpl->sample_list);
if ( bsmpl->rg_list ) khash_str2str_destroy_free_all(bsmpl->rg_list);
int i;
for (i=0; i<bsmpl->nfiles; i++)
{
file_t *file = &bsmpl->files[i];
if ( file->rg2idx ) khash_str2int_destroy_free(file->rg2idx);
free(file->fname);
}
free(bsmpl->smpl);
free(bsmpl->files);
free(bsmpl->tmp.s);
free(bsmpl);
}
void bam_smpl_ignore_readgroups(bam_smpl_t* bsmpl)
{
bsmpl->ignore_rg = 1;
}
static void bsmpl_add_readgroup(bam_smpl_t *bsmpl, file_t *file, const char *rg_id, const char *smpl_name)
{
int ismpl = -1;
if ( smpl_name )
{
if ( khash_str2int_get(bsmpl->name2idx,smpl_name,&ismpl) < 0 )
{
// new sample
bsmpl->nsmpl++;
bsmpl->smpl = (char**) realloc(bsmpl->smpl,sizeof(char*)*bsmpl->nsmpl);
bsmpl->smpl[bsmpl->nsmpl-1] = strdup(smpl_name);
ismpl = khash_str2int_inc(bsmpl->name2idx,bsmpl->smpl[bsmpl->nsmpl-1]);
}
}
if ( !strcmp("*",rg_id) )
{
// all read groups in the bam treated as the same sample
file->default_idx = ismpl;
return;
}
if ( !file->rg2idx ) file->rg2idx = khash_str2int_init();
if ( khash_str2int_has_key(file->rg2idx,rg_id) ) return; // duplicate @RG:ID
khash_str2int_set(file->rg2idx, strdup(rg_id), ismpl);
}
static int bsmpl_keep_readgroup(bam_smpl_t *bsmpl, file_t *file, const char *rg_id, const char **smpl_name)
{
char *rg_smpl = khash_str2str_get(bsmpl->rg_list,rg_id); // unique read group present in one bam only
if ( !rg_smpl )
{
// read group specific to this bam
bsmpl->tmp.l = 0;
ksprintf(&bsmpl->tmp,"%s\t%s",rg_id,file->fname);
rg_smpl = khash_str2str_get(bsmpl->rg_list,bsmpl->tmp.s);
}
if ( !rg_smpl )
{
// any read group in this file?
bsmpl->tmp.l = 0;
ksprintf(&bsmpl->tmp,"*\t%s",file->fname);
rg_smpl = khash_str2str_get(bsmpl->rg_list,bsmpl->tmp.s);
}
if ( !rg_smpl && bsmpl->rg_logic ) return 0;
if ( rg_smpl && !bsmpl->rg_logic ) return 0;
if ( rg_smpl && rg_smpl[0]!='\t' ) *smpl_name = rg_smpl; // rename the sample
return 1;
}
/*
The logic of this function is a bit complicated because we want to work
also with broken bams containing read groups that are not listed in the
header. The desired behavior is as follows:
- when -G is given, read groups which are not listed in the header must
be given explicitly using the "?" symbol in -G.
Otherwise:
- if the bam has no header, all reads in the file are assigned to a
single sample named after the file
- if there is at least one sample defined in the header, reads with no
read group id or with a read group id not listed in the header are
assigned to the first sample encountered in the header
*/
int bam_smpl_add_bam(bam_smpl_t *bsmpl, char *bam_hdr, const char *fname)
{
bsmpl->nfiles++;
bsmpl->files = (file_t*) realloc(bsmpl->files,bsmpl->nfiles*sizeof(file_t));
file_t *file = &bsmpl->files[bsmpl->nfiles-1];
memset(file,0,sizeof(file_t));
file->fname = strdup(fname);
file->default_idx = -1;
if ( bsmpl->ignore_rg || !bam_hdr )
{
// The option --ignore-RG is set or there is no BAM header: use the file name as the sample name
bsmpl_add_readgroup(bsmpl,file,"*",file->fname);
return bsmpl->nfiles-1;
}
void *bam_smpls = khash_str2int_init();
int first_smpl = -1, nskipped = 0;
const char *p = bam_hdr, *q, *r;
while (p != NULL && (q = strstr(p, "@RG")) != 0)
{
char *eol = strchr(q + 3, '\n');
if (q > bam_hdr && *(q - 1) != '\n') { // @RG must be at start of line
p = eol;
continue;
}
p = q + 3;
if ((q = strstr(p, "\tID:")) != 0) q += 4;
if ((r = strstr(p, "\tSM:")) != 0) r += 4;
if (r && q)
{
char *u, *v;
int ioq, ior;
for (u = (char*)q; *u && *u != '\t' && *u != '\n'; ++u);
for (v = (char*)r; *v && *v != '\t' && *v != '\n'; ++v);
ioq = *u; ior = *v; *u = *v = '\0';
// q now points to a null terminated read group id
// r points to a null terminated sample name
if ( !strcmp("*",q) || !strcmp("?",q) )
error("Error: the read group IDs \"*\" and \"?\" have a special meaning in the mpileup code. Please fix the code or the bam: %s\n", fname);
int accept_rg = 1;
if ( bsmpl->sample_list )
{
// restrict samples based on the -s/-S options
char *name = khash_str2str_get(bsmpl->sample_list,r);
if ( bsmpl->sample_logic==0 )
accept_rg = name ? 0 : 1;
else if ( !name )
accept_rg = 0;
else
r = name;
}
if ( accept_rg && bsmpl->rg_list )
{
// restrict readgroups based on the -G option, possibly renaming the sample
accept_rg = bsmpl_keep_readgroup(bsmpl,file,q,&r);
}
if ( accept_rg )
bsmpl_add_readgroup(bsmpl,file,q,r);
else
{
bsmpl_add_readgroup(bsmpl,file,q,NULL); // ignore this RG but note that it was seen in the header
nskipped++;
}
if ( first_smpl<0 )
khash_str2int_get(bsmpl->name2idx,r,&first_smpl);
if ( !khash_str2int_has_key(bam_smpls,r) )
khash_str2int_inc(bam_smpls,strdup(r));
*u = ioq; *v = ior;
}
else
break;
p = eol;
}
int nsmpls = khash_str2int_size(bam_smpls);
khash_str2int_destroy_free(bam_smpls);
const char *smpl_name = NULL;
int accept_null_rg = 1;
if ( bsmpl->rg_list && !bsmpl_keep_readgroup(bsmpl,file,"?",&smpl_name) ) accept_null_rg = 0;
if ( bsmpl->sample_list && first_smpl==-1 ) accept_null_rg = 0;
if ( !accept_null_rg && first_smpl==-1 )
{
// no suitable read group is available in this bam: ignore the whole file.
free(file->fname);
if ( file->rg2idx ) khash_str2int_destroy_free(file->rg2idx);
bsmpl->nfiles--;
return -1;
}
if ( !accept_null_rg ) return bsmpl->nfiles-1;
if ( nsmpls==1 && !nskipped )
{
file->default_idx = first_smpl;
return bsmpl->nfiles-1;
}
if ( !smpl_name ) smpl_name = first_smpl==-1 ? file->fname : bsmpl->smpl[first_smpl];
bsmpl_add_readgroup(bsmpl,file,"?",smpl_name);
return bsmpl->nfiles-1;
}
const char **bam_smpl_get_samples(bam_smpl_t *bsmpl, int *nsmpl)
{
*nsmpl = bsmpl->nsmpl;
return (const char**)bsmpl->smpl;
}
int bam_smpl_get_sample_id(bam_smpl_t *bsmpl, int bam_id, bam1_t *bam_rec)
{
file_t *file = &bsmpl->files[bam_id];
if ( file->default_idx >= 0 ) return file->default_idx;
char *aux_rg = (char*) bam_aux_get(bam_rec, "RG");
aux_rg = aux_rg ? aux_rg+1 : "?";
int rg_id;
if ( khash_str2int_get(file->rg2idx, aux_rg, &rg_id)==0 ) return rg_id;
if ( khash_str2int_get(file->rg2idx, "?", &rg_id)==0 ) return rg_id;
return -1;
}
int bam_smpl_add_samples(bam_smpl_t *bsmpl, char *list, int is_file)
{
if ( list[0]!='^' ) bsmpl->sample_logic = 1;
else list++;
int i, nsamples = 0;
char **samples = hts_readlist(list, is_file, &nsamples);
if ( !samples || !nsamples ) return 0;
kstring_t ori = {0,0,0};
kstring_t ren = {0,0,0};
bsmpl->sample_list = khash_str2str_init();
for (i=0; i<nsamples; i++)
{
char *ptr = samples[i];
ori.l = ren.l = 0;
int escaped = 0;
while ( *ptr )
{
if ( *ptr=='\\' && !escaped ) { escaped = 1; ptr++; continue; }
if ( isspace(*ptr) && !escaped ) break;
kputc(*ptr, &ori);
escaped = 0;
ptr++;
}
if ( *ptr )
{
while ( *ptr && isspace(*ptr) ) ptr++;
while ( *ptr )
{
if ( *ptr=='\\' && !escaped ) { escaped = 1; ptr++; continue; }
if ( isspace(*ptr) && !escaped ) break;
kputc(*ptr, &ren);
escaped = 0;
ptr++;
}
}
khash_str2str_set(bsmpl->sample_list,strdup(ori.s),strdup(ren.l?ren.s:ori.s));
free(samples[i]);
}
free(samples);
free(ori.s);
free(ren.s);
return nsamples;
}
int bam_smpl_add_readgroups(bam_smpl_t *bsmpl, char *list, int is_file)
{
if ( list[0]!='^' ) bsmpl->rg_logic = 1;
else list++;
int i, nrows = 0;
char **rows = hts_readlist(list, is_file, &nrows);
if ( !rows || !nrows ) return 0;
kstring_t fld1 = {0,0,0};
kstring_t fld2 = {0,0,0};
kstring_t fld3 = {0,0,0};
bsmpl->rg_list = khash_str2str_init();
for (i=0; i<nrows; i++)
{
char *ptr = rows[i];
fld1.l = fld2.l = fld3.l = 0;
int escaped = 0;
while ( *ptr )
{
if ( *ptr=='\\' && !escaped ) { escaped = 1; ptr++; continue; }
if ( isspace(*ptr) && !escaped ) break;
kputc(*ptr, &fld1);
escaped = 0;
ptr++;
}
if ( *ptr )
{
while ( *ptr && isspace(*ptr) ) ptr++;
while ( *ptr )
{
if ( *ptr=='\\' && !escaped ) { escaped = 1; ptr++; continue; }
if ( isspace(*ptr) && !escaped ) break;
kputc(*ptr, &fld2);
escaped = 0;
ptr++;
}
}
if ( *ptr )
{
while ( *ptr && isspace(*ptr) ) ptr++;
while ( *ptr )
{
if ( *ptr=='\\' && !escaped ) { escaped = 1; ptr++; continue; }
if ( isspace(*ptr) && !escaped ) break;
kputc(*ptr, &fld3);
escaped = 0;
ptr++;
}
}
if ( fld3.l )
{
// ID FILE SAMPLE
kputc('\t',&fld1);
kputs(fld2.s,&fld1);
fld2.l = 0;
kputs(fld3.s,&fld2);
}
// fld2.s now contains a new sample name. If NULL, use \t to keep the bam header name
char *value = khash_str2str_get(bsmpl->rg_list,fld1.s);
if ( !value )
khash_str2str_set(bsmpl->rg_list,strdup(fld1.s),strdup(fld2.l?fld2.s:"\t"));
else if ( strcmp(value,fld2.l?fld2.s:"\t") )
error("Error: The read group \"%s\" was assigned to two different samples: \"%s\" and \"%s\"\n", fld1.s,value,fld2.l?fld2.s:"\t");
free(rows[i]);
}
free(rows);
free(fld1.s);
free(fld2.s);
free(fld3.s);
return nrows;
}