| 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 |
#include <config.h> |
| 29 |
|
| 30 |
#include "samtools.h" |
| 31 |
#include "htslib/sam.h" |
| 32 |
#include "sam_opts.h" |
| 33 |
#include "htslib/thread_pool.h" |
| 34 |
#include "htslib/khash.h" |
| 35 |
#include "sam_utils.h" |
| 36 |
#include <unistd.h> |
| 37 |
|
| 38 |
#define TAGNUM(X) (((X)[0] << 8) | (X)[1]) //to create key for aux tags, like type key in htslib |
| 39 |
#define LONG_OPT(X) (128 + (X)) //to handle long and short options with same char |
| 40 |
|
| 41 |
#define RESET_KEEPDUPFLAG 1 //keep dup flag as such, as in initial implementation |
| 42 |
|
| 43 |
typedef struct conf_data |
| 44 |
{ |
| 45 |
int keepRGs; |
| 46 |
int noPGentry; |
| 47 |
int ctrlFlags; |
| 48 |
auxhash_t aux_keep; |
| 49 |
auxhash_t aux_remove; |
| 50 |
char *pgid; |
| 51 |
} conf_data; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static void usage(FILE *fp) |
| 58 |
{ |
| 59 |
fprintf(fp, "Usage: samtools reset [options]\n\ |
| 60 |
-o FILE Output file\n\ |
| 61 |
-x, --remove-tag STR\n\ |
| 62 |
Aux tags to be removed\n\ |
| 63 |
--keep-tag STR\n\ |
| 64 |
Aux tags to be retained. Equivalent to -x ^STR\n\ |
| 65 |
--reject-PG ID\n\ |
| 66 |
Removes PG line with ID matching to input and succeeding PG lines\n\ |
| 67 |
--no-RG To have RG lines or not\n\ |
| 68 |
--no-PG To have PG entry or not for reset operation\n\ |
| 69 |
--dupflag\n\ |
| 70 |
Keeps the duplicate flag as it is\n"); |
| 71 |
|
| 72 |
sam_global_opt_help(fp, "--O--@--"); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
void update_aux_conf(conf_data *config) |
| 81 |
{ |
| 82 |
const char rg[] = "RG"; |
| 83 |
const char *default_tags[] = {"AS", "CC", "CG", "CP", "H1", "H2", "HI", "H0", "IH", |
| 84 |
"MC", "MD", "MQ", "NM", "SA", "TS"}; |
| 85 |
khint_t iter = 0; |
| 86 |
int ret = 0, i = 0; |
| 87 |
|
| 88 |
if (!config) |
| 89 |
return; |
| 90 |
|
| 91 |
if (!config->aux_keep && !config->aux_remove) { |
| 92 |
|
| 93 |
config->aux_remove = kh_init(aux_exists); |
| 94 |
} |
| 95 |
if (config->aux_keep) { |
| 96 |
|
| 97 |
if (!config->keepRGs) { |
| 98 |
iter = kh_get(aux_exists, config->aux_keep, TAGNUM(rg)); |
| 99 |
if (iter != kh_end(config->aux_keep)) { |
| 100 |
kh_del(aux_exists, config->aux_keep, iter); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
if (config->aux_remove) { |
| 105 |
if (!config->keepRGs) { |
| 106 |
|
| 107 |
iter = kh_get(aux_exists, config->aux_remove, TAGNUM(rg)); |
| 108 |
if (iter == kh_end(config->aux_remove)) { |
| 109 |
kh_put(aux_exists, config->aux_remove, TAGNUM(rg), &ret); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
for (i = 0; i < sizeof(default_tags) / sizeof(default_tags[0]); ++i) { |
| 115 |
iter = kh_get(aux_exists, config->aux_remove, TAGNUM(default_tags[i])); |
| 116 |
if (iter == kh_end(config->aux_remove)) { |
| 117 |
kh_put(aux_exists, config->aux_remove, TAGNUM(default_tags[i]), &ret); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
void removeauxtags(bam1_t *bamdata, conf_data *config) |
| 129 |
{ |
| 130 |
uint8_t *auxdata = NULL; |
| 131 |
const char *tag = NULL; |
| 132 |
khint_t iter = 0; |
| 133 |
|
| 134 |
if (!bamdata || !config) |
| 135 |
return; |
| 136 |
|
| 137 |
for (auxdata = bam_aux_first(bamdata); auxdata; ) { |
| 138 |
tag = bam_aux_tag(auxdata); |
| 139 |
if (config->aux_keep) { |
| 140 |
iter = kh_get(aux_exists, config->aux_keep, TAGNUM(tag)); |
| 141 |
if (iter == kh_end(config->aux_keep)) { |
| 142 |
auxdata = bam_aux_remove(bamdata, auxdata); |
| 143 |
} |
| 144 |
else { |
| 145 |
auxdata = bam_aux_next(bamdata, auxdata); |
| 146 |
} |
| 147 |
} |
| 148 |
else if (config->aux_remove) { |
| 149 |
iter = kh_get(aux_exists, config->aux_remove, TAGNUM(tag)); |
| 150 |
if (iter != kh_end(config->aux_remove)) { |
| 151 |
auxdata = bam_aux_remove(bamdata, auxdata); |
| 152 |
} |
| 153 |
else { |
| 154 |
auxdata = bam_aux_next(bamdata, auxdata); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
int getRGlines(sam_hdr_t *in_samhdr, sam_hdr_t *out_samhdr) |
| 167 |
{ |
| 168 |
kstring_t line = KS_INITIALIZE; |
| 169 |
int i = 0, ret = 0, count = 0; |
| 170 |
const char rg[] = "RG"; |
| 171 |
|
| 172 |
if (!in_samhdr || !out_samhdr) { |
| 173 |
fprintf(stderr, "Invalid parameters in getRGlines!\n"); |
| 174 |
return 1; |
| 175 |
} |
| 176 |
|
| 177 |
if (-1 == (count = sam_hdr_count_lines(in_samhdr, rg))) { |
| 178 |
fprintf(stderr, "Failed to get RG count!\n"); |
| 179 |
return 1; |
| 180 |
} |
| 181 |
|
| 182 |
for (i = 0; i < count; ++i) |
| 183 |
{ |
| 184 |
ks_clear(&line); |
| 185 |
if (sam_hdr_find_line_pos(in_samhdr, rg, i, &line)) { |
| 186 |
fprintf(stderr, "Failed to get RG data!\n"); |
| 187 |
ret = 1; |
| 188 |
break; |
| 189 |
} |
| 190 |
if (sam_hdr_add_lines(out_samhdr, line.s, line.l)) { |
| 191 |
fprintf(stderr, "Failed to add RG data!\n"); |
| 192 |
ret = 1; |
| 193 |
break; |
| 194 |
} |
| 195 |
} |
| 196 |
ks_free(&line); |
| 197 |
|
| 198 |
return ret; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
int getPGlines(sam_hdr_t *in_samhdr, sam_hdr_t *out_samhdr, conf_data *config, const char *argdump) |
| 209 |
{ |
| 210 |
kstring_t line = KS_INITIALIZE, id = KS_INITIALIZE; |
| 211 |
int i = 0, ret = 0, count = 0; |
| 212 |
const char pg[] = "PG"; |
| 213 |
|
| 214 |
if (!in_samhdr || !out_samhdr || !config) { |
| 215 |
fprintf(stderr, "Invalid parameters in getPGlines!\n"); |
| 216 |
return 1; |
| 217 |
} |
| 218 |
|
| 219 |
if (-1 == (count = sam_hdr_count_lines(in_samhdr, pg))) { |
| 220 |
fprintf(stderr, "Failed to get PG count!\n"); |
| 221 |
return 1; |
| 222 |
} |
| 223 |
|
| 224 |
if (config->pgid && config->pgid[0]) { |
| 225 |
for (i = 0; i < count; ++i) { |
| 226 |
if (sam_hdr_find_tag_pos(in_samhdr, pg, i, "ID", &id)) { |
| 227 |
fprintf(stderr, "Failed to get PG entry fields for line %d!\n", i + 1); |
| 228 |
break; |
| 229 |
} |
| 230 |
|
| 231 |
if (!strcmp(id.s, config->pgid)) |
| 232 |
break; |
| 233 |
|
| 234 |
|
| 235 |
ks_clear(&line); |
| 236 |
if (sam_hdr_find_line_pos(in_samhdr, "PG", i, &line)) { |
| 237 |
fprintf(stderr, "Failed to get PG data at %d!\n", i + 1); |
| 238 |
ret = 1; |
| 239 |
break; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
if (sam_hdr_add_lines(out_samhdr, line.s, line.l)) { |
| 244 |
fprintf(stderr, "Failed to add PG data!\n"); |
| 245 |
ret = 1; |
| 246 |
break; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
else { |
| 251 |
for (i = 0; i < count; ++i) { |
| 252 |
if (sam_hdr_find_line_pos(in_samhdr, "PG", i, &line)) { |
| 253 |
fprintf(stderr, "Failed to get PG data at %d!\n", i + 1); |
| 254 |
ret = 1; |
| 255 |
break; |
| 256 |
} |
| 257 |
|
| 258 |
if (sam_hdr_add_lines(out_samhdr, line.s, line.l)) { |
| 259 |
fprintf(stderr, "Failed to add PG data!\n"); |
| 260 |
ret = 1; |
| 261 |
break; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if (!ret && !config->noPGentry) { |
| 267 |
|
| 268 |
if (-1 == (ret = sam_hdr_add_pg(out_samhdr, "samtools", "CL", argdump, NULL))) { |
| 269 |
fprintf(stderr, "Failed to set PG entry!\n"); |
| 270 |
} |
| 271 |
} |
| 272 |
ks_free(&line); |
| 273 |
ks_free(&id); |
| 274 |
|
| 275 |
return ret; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
int reset(samFile *infile, samFile *outfile, conf_data *config, char *args) |
| 287 |
{ |
| 288 |
sam_hdr_t *in_samhdr = NULL, *out_samhdr = NULL; |
| 289 |
int ret = EXIT_FAILURE, ret_r = 0, ret_w = 0, i = 0; |
| 290 |
bam1_t *bamdata = NULL, *outdata = NULL; |
| 291 |
kstring_t querydata = KS_INITIALIZE, qualdata = KS_INITIALIZE; |
| 292 |
char *sp = NULL, *qp = NULL; |
| 293 |
uint8_t *bamquery = NULL, *bamqual = NULL; |
| 294 |
|
| 295 |
if (!infile || !outfile) { |
| 296 |
fprintf(stderr, "Invalid parameters in reset!\n"); |
| 297 |
goto error; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
in_samhdr = sam_hdr_read(infile); |
| 302 |
if (!in_samhdr) |
| 303 |
{ |
| 304 |
fprintf(stderr, "Failed to read header from file!\n"); |
| 305 |
goto error; |
| 306 |
} |
| 307 |
|
| 308 |
if (!(out_samhdr = sam_hdr_init())) |
| 309 |
{ |
| 310 |
fprintf(stderr, "Failed to create output header!\n"); |
| 311 |
goto error; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
if (-1 == sam_hdr_add_line(out_samhdr,"HD", "VN", SAM_FORMAT_VERSION, NULL)) { |
| 316 |
fprintf(stderr, "Failed to set header data!\n"); |
| 317 |
goto error; |
| 318 |
} |
| 319 |
|
| 320 |
if ((config->keepRGs && getRGlines(in_samhdr, out_samhdr)) || |
| 321 |
getPGlines(in_samhdr, out_samhdr, config, args)) { |
| 322 |
goto error; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
if (sam_hdr_write(outfile, out_samhdr)) { |
| 327 |
print_error_errno("reset", "Output header write failed (%d)!\n", errno); |
| 328 |
goto error; |
| 329 |
} |
| 330 |
|
| 331 |
bamdata = bam_init1(); |
| 332 |
outdata = bam_init1(); |
| 333 |
if (!bamdata || !outdata) |
| 334 |
{ |
| 335 |
fprintf(stderr, "Failed to allocate data memory!\n"); |
| 336 |
goto error; |
| 337 |
} |
| 338 |
|
| 339 |
errno = 0; i = 0; |
| 340 |
sp = NULL; qp = NULL; |
| 341 |
bamquery = NULL; bamqual = NULL; |
| 342 |
|
| 343 |
|
| 344 |
while (0 <= (ret_r = sam_read1(infile, in_samhdr, bamdata))) |
| 345 |
{ |
| 346 |
sp = NULL; qp = NULL; |
| 347 |
bamquery = NULL; bamqual = NULL; |
| 348 |
|
| 349 |
|
| 350 |
if (bamdata->core.flag & BAM_FSECONDARY || bamdata->core.flag & BAM_FSUPPLEMENTARY) { |
| 351 |
continue; |
| 352 |
} |
| 353 |
|
| 354 |
uint16_t flags = bamdata->core.flag & ~BAM_FPROPER_PAIR; |
| 355 |
flags |= BAM_FUNMAP; |
| 356 |
if (bamdata->core.flag & BAM_FPAIRED) { |
| 357 |
flags |= BAM_FMUNMAP; |
| 358 |
} |
| 359 |
flags &= ~BAM_FMREVERSE; |
| 360 |
if (!(config->ctrlFlags & RESET_KEEPDUPFLAG)) { |
| 361 |
flags &= ~BAM_FDUP; |
| 362 |
} |
| 363 |
|
| 364 |
if (0 > ks_resize(&querydata, bamdata->core.l_qseq) || |
| 365 |
0 > ks_resize(&qualdata, bamdata->core.l_qseq)) { |
| 366 |
fprintf(stderr, "Failed to get allocate memory!\n"); |
| 367 |
ret_r = -4; |
| 368 |
break; |
| 369 |
} |
| 370 |
ks_clear(&querydata); |
| 371 |
ks_clear(&qualdata); |
| 372 |
|
| 373 |
sp = ks_str(&querydata); |
| 374 |
qp = ks_str(&qualdata); |
| 375 |
bamquery = bam_get_seq(bamdata); |
| 376 |
bamqual = bam_get_qual(bamdata); |
| 377 |
if (bamdata->core.flag & BAM_FREVERSE) { |
| 378 |
|
| 379 |
for (i = bamdata->core.l_qseq - 1; i >= 0; --i) { |
| 380 |
*sp++ = "=TGKCYSBAWRDMHVN"[bam_seqi(bamquery, i)]; |
| 381 |
*qp++ = bamqual[i]; |
| 382 |
} |
| 383 |
flags &= ~BAM_FREVERSE; |
| 384 |
} |
| 385 |
else { |
| 386 |
|
| 387 |
for (i = 0; i < bamdata->core.l_qseq ; ++i) { |
| 388 |
*sp++ = seq_nt16_str[bam_seqi(bamquery, i)]; |
| 389 |
} |
| 390 |
memcpy(qp, bam_get_qual(bamdata), bamdata->core.l_qseq); |
| 391 |
} |
| 392 |
|
| 393 |
removeauxtags(bamdata, config); |
| 394 |
if (0 > (ret_w = bam_set1(outdata, bamdata->core.l_qname - bamdata->core.l_extranul - 1, bam_get_qname(bamdata), flags, -1, -1, 0, 0, NULL, -1, -1, 0, bamdata->core.l_qseq, querydata.s, qualdata.s, bam_get_l_aux(bamdata)))) { |
| 395 |
print_error_errno("reset", "Failed to set output data (%d)!\n", errno); |
| 396 |
break; |
| 397 |
} |
| 398 |
|
| 399 |
memcpy(bam_get_aux(outdata), bam_get_aux(bamdata), bam_get_l_aux(bamdata)); |
| 400 |
outdata->l_data += bam_get_l_aux(bamdata); |
| 401 |
|
| 402 |
errno = 0; |
| 403 |
|
| 404 |
if (0 > (ret_w = sam_write1(outfile, out_samhdr, outdata))) |
| 405 |
{ |
| 406 |
print_error_errno("reset", "Failed to write output data (%d)!\n", errno); |
| 407 |
break; |
| 408 |
} |
| 409 |
|
| 410 |
errno = 0; |
| 411 |
} |
| 412 |
|
| 413 |
if (-1 > ret_r || 0 > ret_w) { |
| 414 |
|
| 415 |
fprintf(stderr, "Error during %s!\n", (-1 > ret_r)? "read" : "write"); |
| 416 |
} |
| 417 |
else { |
| 418 |
|
| 419 |
ret = EXIT_SUCCESS; |
| 420 |
} |
| 421 |
|
| 422 |
error: |
| 423 |
|
| 424 |
if (in_samhdr) |
| 425 |
sam_hdr_destroy(in_samhdr); |
| 426 |
if (out_samhdr) |
| 427 |
sam_hdr_destroy(out_samhdr); |
| 428 |
|
| 429 |
if (bamdata) |
| 430 |
bam_destroy1(bamdata); |
| 431 |
if (outdata) |
| 432 |
bam_destroy1(outdata); |
| 433 |
|
| 434 |
if (qualdata.s) |
| 435 |
ks_free(&qualdata); |
| 436 |
if (querydata.s) |
| 437 |
ks_free(&querydata); |
| 438 |
return ret; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
void cleanup(conf_data *config) |
| 446 |
{ |
| 447 |
if (config->aux_keep) { |
| 448 |
kh_destroy(aux_exists, config->aux_keep); |
| 449 |
config->aux_keep = NULL; |
| 450 |
} |
| 451 |
if (config->aux_remove) { |
| 452 |
kh_destroy(aux_exists, config->aux_remove); |
| 453 |
config->aux_remove = NULL; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
int main_reset(int argc, char *argv[]) |
| 463 |
{ |
| 464 |
static const struct option lopts[] = { |
| 465 |
SAM_OPT_GLOBAL_OPTIONS('-', '-', 'O', '-', '-', '@'), |
| 466 |
{"keep-tag", required_argument, NULL, LONG_OPT('x')}, |
| 467 |
{"remove-tag", required_argument, NULL, 'x'}, |
| 468 |
{"no-RG", no_argument, NULL, 1}, |
| 469 |
|
| 470 |
{"reject-PG", required_argument, NULL, 'p'}, |
| 471 |
{"no-PG", no_argument, NULL, 2}, |
| 472 |
{"dupflag", no_argument, NULL, 3}, |
| 473 |
{NULL, 0, NULL, 0} |
| 474 |
}; |
| 475 |
samFile *infile = NULL, *outfile = NULL; |
| 476 |
sam_global_args ga = SAM_GLOBAL_ARGS_INIT; |
| 477 |
htsThreadPool tpool = {NULL, 0}; |
| 478 |
const char *inname = NULL, *outname = NULL; |
| 479 |
int c = 0, ret = EXIT_FAILURE; |
| 480 |
char outmode[4] = "w", *args = NULL; |
| 481 |
conf_data resetconf = {1, 0, 0, NULL, NULL, NULL}; |
| 482 |
|
| 483 |
|
| 484 |
while ((c = getopt_long(argc, argv, "o:@:x:O:", lopts, NULL)) >= 0) |
| 485 |
{ |
| 486 |
switch (c) |
| 487 |
{ |
| 488 |
case 1: |
| 489 |
if (!resetconf.keepRGs) { |
| 490 |
usage(stderr); |
| 491 |
goto exit; |
| 492 |
} |
| 493 |
resetconf.keepRGs = 0; |
| 494 |
break; |
| 495 |
case 2: |
| 496 |
if (resetconf.noPGentry) { |
| 497 |
usage(stderr); |
| 498 |
goto exit; |
| 499 |
} |
| 500 |
resetconf.noPGentry = 1; |
| 501 |
break; |
| 502 |
case 3: |
| 503 |
resetconf.ctrlFlags |= RESET_KEEPDUPFLAG; |
| 504 |
break; |
| 505 |
case 'p': |
| 506 |
if (resetconf.pgid) { |
| 507 |
usage(stderr); |
| 508 |
goto exit; |
| 509 |
} |
| 510 |
resetconf.pgid = optarg; |
| 511 |
break; |
| 512 |
case 'o': |
| 513 |
if (outname) { |
| 514 |
usage(stderr); |
| 515 |
goto exit; |
| 516 |
|
| 517 |
} |
| 518 |
outname = optarg; |
| 519 |
break; |
| 520 |
case 'x': |
| 521 |
if (*optarg == '^') { |
| 522 |
if (parse_aux_list(&resetconf.aux_keep, optarg+1, "main_reset")) { |
| 523 |
usage(stderr); |
| 524 |
goto exit; |
| 525 |
} |
| 526 |
} |
| 527 |
else { |
| 528 |
if (parse_aux_list(&resetconf.aux_remove, optarg, "main_reset")) { |
| 529 |
usage(stderr); |
| 530 |
goto exit; |
| 531 |
} |
| 532 |
} |
| 533 |
break; |
| 534 |
case LONG_OPT('x'): |
| 535 |
if (parse_aux_list(&resetconf.aux_keep, optarg, "main_reset")) { |
| 536 |
usage(stderr); |
| 537 |
goto exit; |
| 538 |
} |
| 539 |
break; |
| 540 |
|
| 541 |
default: |
| 542 |
if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) |
| 543 |
break; |
| 544 |
|
| 545 |
|
| 546 |
case '?': |
| 547 |
usage(stderr); |
| 548 |
goto exit; |
| 549 |
break; |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
if (argc == 1 && isatty(STDIN_FILENO)) { |
| 554 |
|
| 555 |
usage(stdout); |
| 556 |
ret = EXIT_SUCCESS; |
| 557 |
goto exit; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
if (!outname) |
| 562 |
outname = "-"; |
| 563 |
|
| 564 |
|
| 565 |
c = argc - optind; |
| 566 |
if (c > 1) { |
| 567 |
usage(stderr); |
| 568 |
goto exit; |
| 569 |
} |
| 570 |
|
| 571 |
if (c == 1) { |
| 572 |
inname = argv[optind]; |
| 573 |
} |
| 574 |
else { |
| 575 |
inname = "-"; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
update_aux_conf(&resetconf); |
| 580 |
|
| 581 |
sam_open_mode(outmode + 1, outname, NULL); |
| 582 |
|
| 583 |
|
| 584 |
infile = sam_open(inname, "r"); |
| 585 |
outfile = sam_open_format(outname, outmode, &ga.out); |
| 586 |
if (!infile || !outfile) { |
| 587 |
fprintf(stderr, "Could not open %s%s%s\n", !infile ? inname : "", (!infile && !outfile)? ", " : "", !outfile ? outname : ""); |
| 588 |
goto exit; |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
if (ga.nthreads > 0) |
| 593 |
{ |
| 594 |
if (!(tpool.pool = hts_tpool_init(ga.nthreads))) |
| 595 |
{ |
| 596 |
fprintf(stderr, "\nFailed to setup thread pool\n"); |
| 597 |
goto exit; |
| 598 |
} |
| 599 |
|
| 600 |
hts_set_opt(infile, HTS_OPT_THREAD_POOL, &tpool); |
| 601 |
hts_set_opt(outfile, HTS_OPT_THREAD_POOL, &tpool); |
| 602 |
} |
| 603 |
|
| 604 |
args = stringify_argv(argc + 1, argv - 1); |
| 605 |
|
| 606 |
|
| 607 |
ret = reset(infile, outfile, &resetconf, args); |
| 608 |
|
| 609 |
exit: |
| 610 |
if (args) |
| 611 |
free(args); |
| 612 |
if (infile) |
| 613 |
sam_close(infile); |
| 614 |
if (outfile) |
| 615 |
sam_close(outfile); |
| 616 |
if (tpool.pool) |
| 617 |
hts_tpool_destroy(tpool.pool); |
| 618 |
cleanup(&resetconf); |
| 619 |
sam_global_args_free(&ga); |
| 620 |
|
| 621 |
return ret; |
| 622 |
} |