forked from samtools/samtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference.c
598 lines (516 loc) · 18.2 KB
/
reference.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/* bam_reference.c -- extracts an embedded reference from a CRAM file,
or creates it from alignments plus MD:Z tags.
Copyright (C) 2022 Genome Research Ltd.
Author: James Bonfield <jkb@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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include "htslib/sam.h"
#include "htslib/cram.h"
#include "samtools.h"
#include "sam_opts.h"
/*
* There are two main modes of operation.
*
* 1. Extracting the reference from the CRAM file embed_ref blocks.
* 2. Generation of reference by analysing consensus plus patches applied
* via MD tags.
*
* The first is very rapid, but only applies to a CRAM files generated with
* the specific options (not commonly used and not the default). The second
* is a slow operation, but applies to any data type.
*
* This is also a testing ground for a future CRAM auto-embed-ref option that
* permits the use of an embedded reference without having to first extract
* the reference. (Note this may require the creation of MD tags during
* decode by use of an existing embedded reference, if the records don't
* have an MD tag themselves, but that's an issue for htslib when we get
* there.)
*/
/*
* ---------------------------------------------------------------------------
* Shared utility functions by both methods.
*/
#define haszero(x) (((x)-0x0101010101010101UL)&~(x)&0x8080808080808080UL)
#define MIN(a,b) ((a)<(b)?(a):(b))
static int dump_ref(sam_hdr_t *h, hts_itr_t *iter, int ref_id,
char *ref, uint64_t ref_len, FILE *fp, int verbose) {
int N = 0;
if (iter && iter->end >= HTS_POS_MAX)
iter->end = ref_len;
if (iter && (iter->beg > 0 || iter->end < ref_len)) {
fprintf(fp, ">%s:%"PRIhts_pos"-%"PRIhts_pos"\n",
sam_hdr_tid2name(h, ref_id), iter->beg+1, iter->end);
ref += iter->beg;
ref_len = MIN(ref_len, iter->end) - iter->beg;
} else {
fprintf(fp, ">%s\n", sam_hdr_tid2name(h, ref_id));
}
int i, j;
uint64_t rem = ref_len;
// Count coverage, purely for information purposes.
// About 90% of dump_ref CPU is here, so maybe this isn't useful,
// but this is still 3-4x faster than the obvious naive loop.
//
// Overall though it's only about 5% overhead of the entire process
// (was ~20%).
if (verbose) {
int n4[8] = {0};
for (j = 0; j < ref_len && (((uintptr_t) &ref[j] & 7) != 0); j++)
N += ref[j] == 'N';
uint64_t fast_end = ((ref_len - j) & ~7) + j;
for (; j < fast_end; j+=8) {
uint64_t i64 = *(uint64_t *)&ref[j];
if (!haszero(i64 ^ 0x4e4e4e4e4e4e4e4eUL)) // 'N' <-> 0
continue;
n4[0] += ref[j+0] == 'N';
n4[1] += ref[j+1] == 'N';
n4[2] += ref[j+2] == 'N';
n4[3] += ref[j+3] == 'N';
n4[4] += ref[j+4] == 'N';
n4[5] += ref[j+5] == 'N';
n4[6] += ref[j+6] == 'N';
n4[7] += ref[j+7] == 'N';
}
for (; j < ref_len; j++)
N += ref[j] == 'N';
N += n4[0]+n4[1]+n4[2]+n4[3]+
n4[4]+n4[5]+n4[6]+n4[7];
}
// Format reference
for (i = 0; i < ref_len; i += 60, rem -= 60) {
int len = (int)(rem < 60 ? rem : 60);
if (fwrite(ref, 1, len, fp) != len)
return -1;
putc('\n', fp);
ref += 60;
}
if (verbose)
fprintf(stderr, "Dump ref %d len %"PRId64", coverage %.2f%%\n",
ref_id, ref_len, 100 - N*100.0 / ref_len);
return 0;
}
/*
* ---------------------------------------------------------------------------
* CRAM embedded reference method of reference construction
*/
/*
* Extracts an embedded reference from a sorted CRAM file.
* Modelled on the CRAM container copy loop from bam_cat.c.
*/
static int cram2ref(samFile *in, sam_hdr_t *h, hts_idx_t *idx, char *reg,
FILE *outfp, int verbose) {
cram_fd *in_c;
cram_container *c = NULL;
cram_block *blk = NULL;
cram_block_slice_hdr *shdr = NULL;
int curr_ref_id = -99;
char *ref = NULL;
uint64_t ref_len = 0;
// We have no direct public API for seeking in CRAM to a specific
// location by genome coordinates. The sam_itr_query API is
// designed for fetching records, rather than seeks to specific
// file locations.
//
// TODO: consider exposing cram_range and cram_seek_to_refpos API.
// After a sam_index_load which will add the index to infp, these
// functions should seek direct to the start of a container.
// Or use cram_index *e =cram_index_query(cram, tid, beg, NULL);
//
// However, fortuitously(?) sam_itr_querys calls cram_seek_to_refpos
// so we can do a region query and let that do the initial seek.
// We still need to do our own end-range detection though.
hts_itr_t *iter = NULL;
if (reg) {
iter = sam_itr_querys(idx, h, reg);
if (!iter) {
print_error("reference", "failed to parse region '%s'", reg);
goto err;
}
}
in_c = in->fp.cram; // low level htslib abuse?
int eor = 0;
while (!eor && (c = cram_read_container(in_c))) {
if (cram_container_is_empty(in_c)) {
cram_block *blk;
// Container compression header
if (!(blk = cram_read_block(in_c)))
goto err;
cram_free_block(blk);
cram_free_container(c);
c = NULL; blk = NULL;
continue;
}
// Container compression header; read and discard
int32_t num_slices;
if (!(blk = cram_read_block(in_c)))
goto err;
cram_free_block(blk);
blk = NULL;
// Container num_blocks can be invalid, due to a bug.
// Instead we iterate in slice context instead.
(void)cram_container_get_landmarks(c, &num_slices);
int i, j;
for (i = 0; i < num_slices; i++) {
// Slice header
if (!(blk = cram_read_block(in_c)))
goto err;
if (!(shdr = cram_decode_slice_header(in_c, blk)))
goto err;
cram_free_block(blk);
blk = NULL;
int num_blocks = cram_slice_hdr_get_num_blocks(shdr);
int embed_id = cram_slice_hdr_get_embed_ref_id(shdr);
int ref_id;
hts_pos_t ref_start, ref_span;
cram_slice_hdr_get_coords(shdr, &ref_id, &ref_start, &ref_span);
if (iter) {
if (iter->tid != ref_id || ref_start > iter->end) {
// Beyond end of specified region.
cram_free_slice_header(shdr);
eor = 1;
break;
}
}
if (embed_id < 0 && ref_id != -1) {
fprintf(stderr, "CRAM file has slice without embedded "
"reference\n");
goto err;
}
if (ref_id != curr_ref_id) {
if (curr_ref_id >= 0) {
if (dump_ref(h, iter, curr_ref_id, ref, ref_len,
outfp, verbose) < 0)
goto err;
}
ref_len = sam_hdr_tid2len(h, ref_id);
if (ref_len) {
char *ref2 = realloc(ref, ref_len);
if (!ref2)
goto err;
else
ref = ref2;
memset(ref, 'N', ref_len);
}
curr_ref_id = ref_id;
}
// Slice data blocks
for (j = 0; j < num_blocks; j++) {
// read and discard, unless it's the ref-ID block
if (!(blk = cram_read_block(in_c)))
goto err;
if (cram_block_get_content_id(blk) == embed_id) {
cram_uncompress_block(blk);
//printf("%.*s\n", blk->uncomp_size, blk->data);
int32_t usize = cram_block_get_uncomp_size(blk);
int ref_end = ref_start + usize;
if (ref_end > ref_len+1)
ref_end = ref_len+1;
if (ref_end > ref_start)
memcpy(ref + ref_start-1, cram_block_get_data(blk),
ref_end - ref_start);
}
cram_free_block(blk);
blk = NULL;
}
cram_free_slice_header(shdr);
shdr = NULL;
}
cram_free_container(c);
c = NULL;
}
int ret = 0;
if (curr_ref_id >= 0) {
ret = dump_ref(h, iter, curr_ref_id, ref, ref_len, outfp, verbose);
} else if (reg) {
// no data present
// no data present, but we explicitly asked for the reference so
// report it still as Ns.
ref_len = MIN(iter->end, sam_hdr_tid2len(h, iter->tid));
ref = malloc(ref_len);
memset(ref, 'N', ref_len);
if (!ref)
goto err;
ret = dump_ref(h, iter, iter->tid, ref, ref_len, outfp, verbose);
}
free(ref);
if (iter)
hts_itr_destroy(iter);
return ret;
err:
free(ref);
if (blk)
cram_free_block(blk);
if (shdr)
cram_free_slice_header(shdr);
if (c)
cram_free_container(c);
if (iter)
hts_itr_destroy(iter);
return -1;
}
/*
* ---------------------------------------------------------------------------
* MD method of reference construction
*/
// Returns the next cigar op code: one of the BAM_C* codes,
// or -1 if no more are present.
static inline
int next_cigar_op(uint32_t *cigar, int *ncigar, int *skip, int *spos,
uint32_t *cig_ind, uint32_t *cig_op, uint32_t *cig_len) {
for(;;) {
while (*cig_len == 0) {
if (*cig_ind < *ncigar) {
*cig_op = cigar[*cig_ind] & BAM_CIGAR_MASK;
*cig_len = cigar[*cig_ind] >> BAM_CIGAR_SHIFT;
(*cig_ind)++;
} else {
return -1;
}
}
if (skip[*cig_op]) {
*spos += (bam_cigar_type(*cig_op)&1) * *cig_len;
*cig_len = 0;
continue;
}
(*cig_len)--;
break;
}
return *cig_op;
}
// Converts a bam object with SEQ, POS/CIGAR and MD:Z to a reference.
// Updates ref[] array.
//
// Returns >0 on success,
// 0 on no-MD found,
// -1 on failure (eg inconsistent data)
static int build_ref(bam1_t *b, char *ref, size_t ref_len) {
uint8_t *seq = bam_get_seq(b);
uint32_t *cigar = bam_get_cigar(b);
int ncigar = b->core.n_cigar;
uint32_t cig_op = 0, cig_len = 0, cig_ind = 0;
const uint8_t *MD = bam_aux_get(b, "MD");
if (!MD || *MD != 'Z')
return 0;
MD++;
// Walk through MD + seq to generate ref
int iseq = 0, iref = b->core.pos, next_op;
int cig_skip[16] = {0,1,0,1,1,1,1,0,0,1,1,1,1,1,1,1};
while (iseq < b->core.l_qseq && *MD) {
if (isdigit(*MD)) {
// match
int len = strtol((char *)MD, (char **)&MD, 10);
while (iseq < b->core.l_qseq && len) {
if ((next_op = next_cigar_op(cigar, &ncigar, cig_skip,
&iseq, &cig_ind, &cig_op,
&cig_len)) < 0)
return -1;
if (next_op != BAM_CMATCH &&
next_op != BAM_CEQUAL) {
print_error("MD2ref",
"MD:Z and CIGAR are incompatible");
return -1;
}
if (iref < ref_len)
ref[iref] = seq_nt16_str[bam_seqi(seq, iseq)];
iseq++;
iref++;
len--;
}
} else if (*MD == '^') {
// deletion
MD++;
while (*MD && isalpha(*MD)) {
if ((next_op = next_cigar_op(cigar, &ncigar, cig_skip,
&iseq, &cig_ind, &cig_op,
&cig_len)) < 0)
return -1;
if (next_op != BAM_CDEL) {
print_error("MD2ref",
"MD:Z and CIGAR are incompatible");
return -1;
}
if (iref < ref_len)
ref[iref] = *MD;
MD++;
iref++;
}
} else {
// substitution
if ((next_op = next_cigar_op(cigar, &ncigar, cig_skip,
&iseq, &cig_ind, &cig_op,
&cig_len)) < 0)
return -1;
if (next_op != BAM_CMATCH && next_op != BAM_CDIFF) {
print_error("MD2ref", "MD:Z and CIGAR are incompatible");
return -1;
}
if (iref < ref_len)
ref[iref] = *MD;
MD++;
iref++;
iseq++;
}
}
return 1;
}
static int MD2ref(samFile *in, sam_hdr_t *h, hts_idx_t *idx, char *reg,
FILE *outfp, int verbose) {
bam1_t *b = bam_init1();
int r, last_tid = -99;
size_t ref_len = 0;
char *ref = NULL;
int ret = -1;
hts_itr_t *iter = NULL;
if (idx && reg) {
iter = sam_itr_querys(idx, h, reg);
if (!iter) {
print_error("reference", "failed to parse region '%s'", reg);
goto err;
}
}
while ((r = iter
? sam_itr_next(in, iter, b)
: sam_read1(in, h, b)) >= 0) {
// check b->core.tid and flush old seq.
if (b->core.tid != last_tid) {
if (last_tid >= 0)
if (dump_ref(h, iter, last_tid, ref, ref_len, outfp,
verbose) < 0)
goto err;
last_tid = b->core.tid;
ref_len = sam_hdr_tid2len(h, last_tid);
if (ref_len) {
char *ref2 = realloc(ref, ref_len);
if (!ref2)
goto err;
else
ref = ref2;
memset(ref, 'N', ref_len);
}
}
if (build_ref(b, ref, ref_len) < 0)
goto err;
}
if (last_tid >= 0) {
if (dump_ref(h, iter, last_tid, ref, ref_len, outfp, verbose) < 0)
goto err;
} else if (reg) {
// no data present, but we explicitly asked for the reference so
// report it still as Ns.
ref_len = MIN(iter->end, sam_hdr_tid2len(h, iter->tid));
ref = malloc(ref_len);
memset(ref, 'N', ref_len);
if (!ref)
goto err;
if (dump_ref(h, iter, iter->tid, ref, ref_len, outfp, verbose) < 0)
goto err;
}
if (r < -1)
goto err;
ret = 0;
err:
if (iter)
hts_itr_destroy(iter);
bam_destroy1(b);
free(ref);
return ret;
}
int main_reference(int argc, char *argv[])
{
int c, usage = 0, verbose = 1, use_embedded = 0;
sam_hdr_t *h = 0;
samFile *in = NULL;
hts_idx_t *idx = NULL;
sam_global_args ga;
FILE *outfp = stdout;
char *reg = NULL;
static const struct option lopts[] = {
{"output", required_argument, NULL, 'o'},
{"quiet", no_argument, NULL, 'q'},
{"embedded", no_argument, NULL, 'e'},
{"region", required_argument, NULL, 'r'},
SAM_OPT_GLOBAL_OPTIONS('-', '-', '-', '-', '-', '@'),
{ NULL, 0, NULL, 0 }
};
sam_global_args_init(&ga);
while ((c = getopt_long(argc, argv, "@:qo:er:", lopts, NULL)) >= 0) {
switch (c) {
case 'o':
if (!(outfp = fopen(optarg, "w"))) {
perror(optarg);
goto err;
}
break;
case 'q':
verbose = 0;
break;
case 'e':
use_embedded = 1;
break;
case 'r':
reg = optarg;
break;
default:
if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) break;
/* else fall-through */
case '?': usage=1; break;
}
}
if ((optind == argc && isatty(0)) || usage) {
printf("Usage: samtools reference [-@ N] [-r region] [-e] [-q] [-o out.fa] [in.cram]\n");
return 0;
}
char *fn = optind < argc ? argv[optind] : "-";
if (!(in = sam_open(fn, "r"))) {
print_error_errno("reference", "failed to open file '%s'", fn);
return 1;
}
if (ga.nthreads > 0)
hts_set_threads(in, ga.nthreads);
if (!(h = sam_hdr_read(in)))
goto err;
if (reg) {
idx = sam_index_load(in, fn);
if (!idx) {
print_error_errno("reference", "Failed to load the index");
goto err;
}
}
int ret = use_embedded
? cram2ref(in, h, idx, reg, outfp, verbose)
: MD2ref(in, h, idx, reg, outfp, verbose);
sam_hdr_destroy(h);
if (outfp != stdout)
fclose(outfp);
if (idx)
hts_idx_destroy(idx);
sam_close(in);
return ret;
err:
if (idx)
hts_idx_destroy(idx);
if (in)
sam_close(in);
if (h)
sam_hdr_destroy(h);
return 1;
}