| 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 |
#include <config.h> |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <unistd.h> |
| 31 |
#include <string.h> |
| 32 |
#include <ctype.h> |
| 33 |
|
| 34 |
#include "htslib/sam.h" |
| 35 |
#include "htslib/cram.h" |
| 36 |
#include "samtools.h" |
| 37 |
#include "sam_opts.h" |
| 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 |
#define haszero(x) (((x)-0x0101010101010101UL)&~(x)&0x8080808080808080UL) |
| 65 |
#define MIN(a,b) ((a)<(b)?(a):(b)) |
| 66 |
static int dump_ref(sam_hdr_t *h, hts_itr_t *iter, int ref_id, |
| 67 |
char *ref, uint64_t ref_len, FILE *fp, int verbose) { |
| 68 |
int N = 0; |
| 69 |
if (iter && iter->end >= HTS_POS_MAX) |
| 70 |
iter->end = ref_len; |
| 71 |
if (iter && (iter->beg > 0 || iter->end < ref_len)) { |
| 72 |
fprintf(fp, ">%s:%"PRIhts_pos"-%"PRIhts_pos"\n", |
| 73 |
sam_hdr_tid2name(h, ref_id), iter->beg+1, iter->end); |
| 74 |
ref += iter->beg; |
| 75 |
ref_len = MIN(ref_len, iter->end) - iter->beg; |
| 76 |
} else { |
| 77 |
fprintf(fp, ">%s\n", sam_hdr_tid2name(h, ref_id)); |
| 78 |
} |
| 79 |
|
| 80 |
int i, j; |
| 81 |
uint64_t rem = ref_len; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if (verbose) { |
| 90 |
int n4[8] = {0}; |
| 91 |
for (j = 0; j < ref_len && (((uintptr_t) &ref[j] & 7) != 0); j++) |
| 92 |
N += ref[j] == 'N'; |
| 93 |
uint64_t fast_end = ((ref_len - j) & ~7) + j; |
| 94 |
for (; j < fast_end; j+=8) { |
| 95 |
uint64_t i64 = *(uint64_t *)&ref[j]; |
| 96 |
if (!haszero(i64 ^ 0x4e4e4e4e4e4e4e4eUL)) |
| 97 |
continue; |
| 98 |
|
| 99 |
n4[0] += ref[j+0] == 'N'; |
| 100 |
n4[1] += ref[j+1] == 'N'; |
| 101 |
n4[2] += ref[j+2] == 'N'; |
| 102 |
n4[3] += ref[j+3] == 'N'; |
| 103 |
n4[4] += ref[j+4] == 'N'; |
| 104 |
n4[5] += ref[j+5] == 'N'; |
| 105 |
n4[6] += ref[j+6] == 'N'; |
| 106 |
n4[7] += ref[j+7] == 'N'; |
| 107 |
} |
| 108 |
for (; j < ref_len; j++) |
| 109 |
N += ref[j] == 'N'; |
| 110 |
N += n4[0]+n4[1]+n4[2]+n4[3]+ |
| 111 |
n4[4]+n4[5]+n4[6]+n4[7]; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
for (i = 0; i < ref_len; i += 60, rem -= 60) { |
| 116 |
int len = (int)(rem < 60 ? rem : 60); |
| 117 |
if (fwrite(ref, 1, len, fp) != len) |
| 118 |
return -1; |
| 119 |
putc('\n', fp); |
| 120 |
ref += 60; |
| 121 |
} |
| 122 |
|
| 123 |
if (verbose) |
| 124 |
fprintf(stderr, "Dump ref %d len %"PRId64", coverage %.2f%%\n", |
| 125 |
ref_id, ref_len, 100 - N*100.0 / ref_len); |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
static int cram2ref(samFile *in, sam_hdr_t *h, hts_idx_t *idx, char *reg, |
| 140 |
FILE *outfp, int verbose) { |
| 141 |
cram_fd *in_c; |
| 142 |
cram_container *c = NULL; |
| 143 |
cram_block *blk = NULL; |
| 144 |
cram_block_slice_hdr *shdr = NULL; |
| 145 |
|
| 146 |
int curr_ref_id = -99; |
| 147 |
char *ref = NULL; |
| 148 |
uint64_t ref_len = 0; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
hts_itr_t *iter = NULL; |
| 165 |
if (reg) { |
| 166 |
iter = sam_itr_querys(idx, h, reg); |
| 167 |
if (!iter) { |
| 168 |
print_error("reference", "failed to parse region '%s'", reg); |
| 169 |
goto err; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
in_c = in->fp.cram; |
| 174 |
int eor = 0; |
| 175 |
while (!eor && (c = cram_read_container(in_c))) { |
| 176 |
if (cram_container_is_empty(in_c)) { |
| 177 |
cram_block *blk; |
| 178 |
|
| 179 |
if (!(blk = cram_read_block(in_c))) |
| 180 |
goto err; |
| 181 |
cram_free_block(blk); |
| 182 |
cram_free_container(c); |
| 183 |
c = NULL; blk = NULL; |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
int32_t num_slices; |
| 189 |
if (!(blk = cram_read_block(in_c))) |
| 190 |
goto err; |
| 191 |
cram_free_block(blk); |
| 192 |
blk = NULL; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
(void)cram_container_get_landmarks(c, &num_slices); |
| 197 |
int i, j; |
| 198 |
for (i = 0; i < num_slices; i++) { |
| 199 |
|
| 200 |
if (!(blk = cram_read_block(in_c))) |
| 201 |
goto err; |
| 202 |
if (!(shdr = cram_decode_slice_header(in_c, blk))) |
| 203 |
goto err; |
| 204 |
cram_free_block(blk); |
| 205 |
blk = NULL; |
| 206 |
|
| 207 |
int num_blocks = cram_slice_hdr_get_num_blocks(shdr); |
| 208 |
int embed_id = cram_slice_hdr_get_embed_ref_id(shdr); |
| 209 |
int ref_id; |
| 210 |
hts_pos_t ref_start, ref_span; |
| 211 |
cram_slice_hdr_get_coords(shdr, &ref_id, &ref_start, &ref_span); |
| 212 |
|
| 213 |
if (iter) { |
| 214 |
if (iter->tid != ref_id || ref_start > iter->end) { |
| 215 |
|
| 216 |
cram_free_slice_header(shdr); |
| 217 |
eor = 1; |
| 218 |
break; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (embed_id < 0 && ref_id != -1) { |
| 223 |
fprintf(stderr, "CRAM file has slice without embedded " |
| 224 |
"reference\n"); |
| 225 |
goto err; |
| 226 |
} |
| 227 |
|
| 228 |
if (ref_id != curr_ref_id) { |
| 229 |
if (curr_ref_id >= 0) { |
| 230 |
if (dump_ref(h, iter, curr_ref_id, ref, ref_len, |
| 231 |
outfp, verbose) < 0) |
| 232 |
goto err; |
| 233 |
} |
| 234 |
|
| 235 |
ref_len = sam_hdr_tid2len(h, ref_id); |
| 236 |
if (ref_len) { |
| 237 |
char *ref2 = realloc(ref, ref_len); |
| 238 |
if (!ref2) |
| 239 |
goto err; |
| 240 |
else |
| 241 |
ref = ref2; |
| 242 |
memset(ref, 'N', ref_len); |
| 243 |
} |
| 244 |
curr_ref_id = ref_id; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
for (j = 0; j < num_blocks; j++) { |
| 249 |
|
| 250 |
if (!(blk = cram_read_block(in_c))) |
| 251 |
goto err; |
| 252 |
if (cram_block_get_content_id(blk) == embed_id) { |
| 253 |
cram_uncompress_block(blk); |
| 254 |
|
| 255 |
|
| 256 |
int32_t usize = cram_block_get_uncomp_size(blk); |
| 257 |
int ref_end = ref_start + usize; |
| 258 |
if (ref_end > ref_len+1) |
| 259 |
ref_end = ref_len+1; |
| 260 |
if (ref_end > ref_start) |
| 261 |
memcpy(ref + ref_start-1, cram_block_get_data(blk), |
| 262 |
ref_end - ref_start); |
| 263 |
} |
| 264 |
cram_free_block(blk); |
| 265 |
blk = NULL; |
| 266 |
} |
| 267 |
cram_free_slice_header(shdr); |
| 268 |
shdr = NULL; |
| 269 |
} |
| 270 |
|
| 271 |
cram_free_container(c); |
| 272 |
c = NULL; |
| 273 |
} |
| 274 |
|
| 275 |
int ret = 0; |
| 276 |
if (curr_ref_id >= 0) { |
| 277 |
ret = dump_ref(h, iter, curr_ref_id, ref, ref_len, outfp, verbose); |
| 278 |
} else if (reg) { |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
ref_len = MIN(iter->end, sam_hdr_tid2len(h, iter->tid)); |
| 283 |
ref = malloc(ref_len); |
| 284 |
memset(ref, 'N', ref_len); |
| 285 |
if (!ref) |
| 286 |
goto err; |
| 287 |
ret = dump_ref(h, iter, iter->tid, ref, ref_len, outfp, verbose); |
| 288 |
} |
| 289 |
|
| 290 |
free(ref); |
| 291 |
if (iter) |
| 292 |
hts_itr_destroy(iter); |
| 293 |
|
| 294 |
return ret; |
| 295 |
|
| 296 |
err: |
| 297 |
free(ref); |
| 298 |
if (blk) |
| 299 |
cram_free_block(blk); |
| 300 |
if (shdr) |
| 301 |
cram_free_slice_header(shdr); |
| 302 |
if (c) |
| 303 |
cram_free_container(c); |
| 304 |
if (iter) |
| 305 |
hts_itr_destroy(iter); |
| 306 |
|
| 307 |
return -1; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
static inline |
| 318 |
int next_cigar_op(uint32_t *cigar, int *ncigar, int *skip, int *spos, |
| 319 |
uint32_t *cig_ind, uint32_t *cig_op, uint32_t *cig_len) { |
| 320 |
for(;;) { |
| 321 |
while (*cig_len == 0) { |
| 322 |
if (*cig_ind < *ncigar) { |
| 323 |
*cig_op = cigar[*cig_ind] & BAM_CIGAR_MASK; |
| 324 |
*cig_len = cigar[*cig_ind] >> BAM_CIGAR_SHIFT; |
| 325 |
(*cig_ind)++; |
| 326 |
} else { |
| 327 |
return -1; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
if (skip[*cig_op]) { |
| 332 |
*spos += (bam_cigar_type(*cig_op)&1) * *cig_len; |
| 333 |
*cig_len = 0; |
| 334 |
continue; |
| 335 |
} |
| 336 |
|
| 337 |
(*cig_len)--; |
| 338 |
break; |
| 339 |
} |
| 340 |
|
| 341 |
return *cig_op; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
static int build_ref(bam1_t *b, char *ref, size_t ref_len) { |
| 351 |
uint8_t *seq = bam_get_seq(b); |
| 352 |
uint32_t *cigar = bam_get_cigar(b); |
| 353 |
int ncigar = b->core.n_cigar; |
| 354 |
uint32_t cig_op = 0, cig_len = 0, cig_ind = 0; |
| 355 |
|
| 356 |
const uint8_t *MD = bam_aux_get(b, "MD"); |
| 357 |
if (!MD || *MD != 'Z') |
| 358 |
return 0; |
| 359 |
MD++; |
| 360 |
|
| 361 |
|
| 362 |
int iseq = 0, iref = b->core.pos, next_op; |
| 363 |
int cig_skip[16] = {0,1,0,1,1,1,1,0,0,1,1,1,1,1,1,1}; |
| 364 |
while (iseq < b->core.l_qseq && *MD) { |
| 365 |
if (isdigit(*MD)) { |
| 366 |
|
| 367 |
int len = strtol((char *)MD, (char **)&MD, 10); |
| 368 |
while (iseq < b->core.l_qseq && len) { |
| 369 |
if ((next_op = next_cigar_op(cigar, &ncigar, cig_skip, |
| 370 |
&iseq, &cig_ind, &cig_op, |
| 371 |
&cig_len)) < 0) |
| 372 |
return -1; |
| 373 |
|
| 374 |
if (next_op != BAM_CMATCH && |
| 375 |
next_op != BAM_CEQUAL) { |
| 376 |
print_error("MD2ref", |
| 377 |
"MD:Z and CIGAR are incompatible"); |
| 378 |
return -1; |
| 379 |
} |
| 380 |
|
| 381 |
if (iref < ref_len) |
| 382 |
ref[iref] = seq_nt16_str[bam_seqi(seq, iseq)]; |
| 383 |
iseq++; |
| 384 |
iref++; |
| 385 |
len--; |
| 386 |
} |
| 387 |
} else if (*MD == '^') { |
| 388 |
|
| 389 |
MD++; |
| 390 |
while (*MD && isalpha(*MD)) { |
| 391 |
if ((next_op = next_cigar_op(cigar, &ncigar, cig_skip, |
| 392 |
&iseq, &cig_ind, &cig_op, |
| 393 |
&cig_len)) < 0) |
| 394 |
return -1; |
| 395 |
|
| 396 |
if (next_op != BAM_CDEL) { |
| 397 |
print_error("MD2ref", |
| 398 |
"MD:Z and CIGAR are incompatible"); |
| 399 |
return -1; |
| 400 |
} |
| 401 |
|
| 402 |
if (iref < ref_len) |
| 403 |
ref[iref] = *MD; |
| 404 |
|
| 405 |
MD++; |
| 406 |
iref++; |
| 407 |
} |
| 408 |
} else { |
| 409 |
|
| 410 |
if ((next_op = next_cigar_op(cigar, &ncigar, cig_skip, |
| 411 |
&iseq, &cig_ind, &cig_op, |
| 412 |
&cig_len)) < 0) |
| 413 |
return -1; |
| 414 |
|
| 415 |
if (next_op != BAM_CMATCH && next_op != BAM_CDIFF) { |
| 416 |
print_error("MD2ref", "MD:Z and CIGAR are incompatible"); |
| 417 |
return -1; |
| 418 |
} |
| 419 |
if (iref < ref_len) |
| 420 |
ref[iref] = *MD; |
| 421 |
|
| 422 |
MD++; |
| 423 |
iref++; |
| 424 |
iseq++; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
return 1; |
| 429 |
} |
| 430 |
|
| 431 |
static int MD2ref(samFile *in, sam_hdr_t *h, hts_idx_t *idx, char *reg, |
| 432 |
FILE *outfp, int verbose) { |
| 433 |
bam1_t *b = bam_init1(); |
| 434 |
int r, last_tid = -99; |
| 435 |
size_t ref_len = 0; |
| 436 |
char *ref = NULL; |
| 437 |
int ret = -1; |
| 438 |
|
| 439 |
hts_itr_t *iter = NULL; |
| 440 |
if (idx && reg) { |
| 441 |
iter = sam_itr_querys(idx, h, reg); |
| 442 |
if (!iter) { |
| 443 |
print_error("reference", "failed to parse region '%s'", reg); |
| 444 |
goto err; |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
while ((r = iter |
| 449 |
? sam_itr_next(in, iter, b) |
| 450 |
: sam_read1(in, h, b)) >= 0) { |
| 451 |
|
| 452 |
if (b->core.tid != last_tid) { |
| 453 |
if (last_tid >= 0) |
| 454 |
if (dump_ref(h, iter, last_tid, ref, ref_len, outfp, |
| 455 |
verbose) < 0) |
| 456 |
goto err; |
| 457 |
|
| 458 |
last_tid = b->core.tid; |
| 459 |
ref_len = sam_hdr_tid2len(h, last_tid); |
| 460 |
if (ref_len) { |
| 461 |
char *ref2 = realloc(ref, ref_len); |
| 462 |
if (!ref2) |
| 463 |
goto err; |
| 464 |
else |
| 465 |
ref = ref2; |
| 466 |
memset(ref, 'N', ref_len); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
if (build_ref(b, ref, ref_len) < 0) |
| 471 |
goto err; |
| 472 |
} |
| 473 |
|
| 474 |
if (last_tid >= 0) { |
| 475 |
if (dump_ref(h, iter, last_tid, ref, ref_len, outfp, verbose) < 0) |
| 476 |
goto err; |
| 477 |
} else if (reg) { |
| 478 |
|
| 479 |
|
| 480 |
ref_len = MIN(iter->end, sam_hdr_tid2len(h, iter->tid)); |
| 481 |
ref = malloc(ref_len); |
| 482 |
memset(ref, 'N', ref_len); |
| 483 |
if (!ref) |
| 484 |
goto err; |
| 485 |
if (dump_ref(h, iter, iter->tid, ref, ref_len, outfp, verbose) < 0) |
| 486 |
goto err; |
| 487 |
} |
| 488 |
|
| 489 |
if (r < -1) |
| 490 |
goto err; |
| 491 |
|
| 492 |
ret = 0; |
| 493 |
|
| 494 |
err: |
| 495 |
if (iter) |
| 496 |
hts_itr_destroy(iter); |
| 497 |
bam_destroy1(b); |
| 498 |
free(ref); |
| 499 |
return ret; |
| 500 |
} |
| 501 |
|
| 502 |
int main_reference(int argc, char *argv[]) |
| 503 |
{ |
| 504 |
int c, usage = 0, verbose = 1, use_embedded = 0; |
| 505 |
sam_hdr_t *h = 0; |
| 506 |
samFile *in = NULL; |
| 507 |
hts_idx_t *idx = NULL; |
| 508 |
sam_global_args ga; |
| 509 |
FILE *outfp = stdout; |
| 510 |
char *reg = NULL; |
| 511 |
|
| 512 |
static const struct option lopts[] = { |
| 513 |
{"output", required_argument, NULL, 'o'}, |
| 514 |
{"quiet", no_argument, NULL, 'q'}, |
| 515 |
{"embedded", no_argument, NULL, 'e'}, |
| 516 |
{"region", required_argument, NULL, 'r'}, |
| 517 |
SAM_OPT_GLOBAL_OPTIONS('-', '-', '-', '-', '-', '@'), |
| 518 |
{ NULL, 0, NULL, 0 } |
| 519 |
}; |
| 520 |
|
| 521 |
sam_global_args_init(&ga); |
| 522 |
|
| 523 |
while ((c = getopt_long(argc, argv, "@:qo:er:", lopts, NULL)) >= 0) { |
| 524 |
switch (c) { |
| 525 |
case 'o': |
| 526 |
if (!(outfp = fopen(optarg, "w"))) { |
| 527 |
perror(optarg); |
| 528 |
goto err; |
| 529 |
} |
| 530 |
break; |
| 531 |
|
| 532 |
case 'q': |
| 533 |
verbose = 0; |
| 534 |
break; |
| 535 |
|
| 536 |
case 'e': |
| 537 |
use_embedded = 1; |
| 538 |
break; |
| 539 |
|
| 540 |
case 'r': |
| 541 |
reg = optarg; |
| 542 |
break; |
| 543 |
|
| 544 |
default: |
| 545 |
if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) break; |
| 546 |
|
| 547 |
case '?': usage=1; break; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
if ((optind == argc && isatty(0)) || usage) { |
| 552 |
printf("Usage: samtools reference [-@ N] [-r region] [-e] [-q] [-o out.fa] [in.cram]\n"); |
| 553 |
return 0; |
| 554 |
} |
| 555 |
|
| 556 |
char *fn = optind < argc ? argv[optind] : "-"; |
| 557 |
if (!(in = sam_open(fn, "r"))) { |
| 558 |
print_error_errno("reference", "failed to open file '%s'", fn); |
| 559 |
return 1; |
| 560 |
} |
| 561 |
|
| 562 |
if (ga.nthreads > 0) |
| 563 |
hts_set_threads(in, ga.nthreads); |
| 564 |
|
| 565 |
if (!(h = sam_hdr_read(in))) |
| 566 |
goto err; |
| 567 |
|
| 568 |
if (reg) { |
| 569 |
idx = sam_index_load(in, fn); |
| 570 |
if (!idx) { |
| 571 |
print_error_errno("reference", "Failed to load the index"); |
| 572 |
goto err; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
int ret = use_embedded |
| 577 |
? cram2ref(in, h, idx, reg, outfp, verbose) |
| 578 |
: MD2ref(in, h, idx, reg, outfp, verbose); |
| 579 |
|
| 580 |
sam_hdr_destroy(h); |
| 581 |
if (outfp != stdout) |
| 582 |
fclose(outfp); |
| 583 |
if (idx) |
| 584 |
hts_idx_destroy(idx); |
| 585 |
sam_close(in); |
| 586 |
|
| 587 |
return ret; |
| 588 |
|
| 589 |
err: |
| 590 |
if (idx) |
| 591 |
hts_idx_destroy(idx); |
| 592 |
if (in) |
| 593 |
sam_close(in); |
| 594 |
if (h) |
| 595 |
sam_hdr_destroy(h); |
| 596 |
|
| 597 |
return 1; |
| 598 |
} |