|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h> |
|
|
|
|
|
#include "samtools.h" |
|
|
#include "htslib/sam.h" |
|
|
#include "sam_opts.h" |
|
|
#include "htslib/thread_pool.h" |
|
|
#include "htslib/khash.h" |
|
|
#include "sam_utils.h" |
|
|
#include <unistd.h> |
|
|
|
|
|
#define TAGNUM(X) (((X)[0] << 8) | (X)[1]) |
|
|
#define LONG_OPT(X) (128 + (X)) |
|
|
|
|
|
typedef struct conf_data |
|
|
{ |
|
|
int keepRGs; |
|
|
int noPGentry; |
|
|
auxhash_t aux_keep; |
|
|
auxhash_t aux_remove; |
|
|
char *pgid; |
|
|
} conf_data; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void usage(FILE *fp) |
|
|
{ |
|
|
fprintf(fp, "Usage: samtools reset [options]\n\ |
|
|
-o FILE Output file\n\ |
|
|
-x, --remove-tag STR\n\ |
|
|
Aux tags to be removed\n\ |
|
|
--keep-tag STR\n\ |
|
|
Aux tags to be retained. Equivalent to -x ^STR\n\ |
|
|
--reject-PG ID\n\ |
|
|
Removes PG line with ID matching to input and succeeding PG lines\n\ |
|
|
--no-RG To have RG lines or not\n\ |
|
|
--no-PG To have PG entry or not for reset operation\n"); |
|
|
|
|
|
sam_global_opt_help(fp, "--O--@--"); |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void removeauxtags(bam1_t *bamdata, conf_data *config) |
|
|
{ |
|
|
uint8_t *auxdata = NULL; |
|
|
const char *tag = NULL, rg[] = "RG"; |
|
|
khint_t iter = 0; |
|
|
int ret = 0; |
|
|
|
|
|
if (!bamdata || !config || (!config->aux_keep && !config->aux_remove && config->keepRGs)) |
|
|
return; |
|
|
|
|
|
|
|
|
if (!config->keepRGs) { |
|
|
if (!config->aux_keep && !config->aux_remove) { |
|
|
|
|
|
config->aux_remove = kh_init(aux_exists); |
|
|
} |
|
|
|
|
|
if (config->aux_keep) { |
|
|
|
|
|
iter = kh_get(aux_exists, config->aux_keep, TAGNUM(rg)); |
|
|
if (iter != kh_end(config->aux_keep)) { |
|
|
kh_del(aux_exists, config->aux_keep, iter); |
|
|
} |
|
|
} |
|
|
if (config->aux_remove) { |
|
|
|
|
|
iter = kh_get(aux_exists, config->aux_remove, TAGNUM(rg)); |
|
|
if (iter == kh_end(config->aux_remove)) { |
|
|
kh_put(aux_exists, config->aux_remove, TAGNUM(rg), &ret); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
for (auxdata = bam_aux_first(bamdata); auxdata; ) { |
|
|
tag = bam_aux_tag(auxdata); |
|
|
if (config->aux_keep) { |
|
|
iter = kh_get(aux_exists, config->aux_keep, TAGNUM(tag)); |
|
|
if (iter == kh_end(config->aux_keep)) { |
|
|
auxdata = bam_aux_remove(bamdata, auxdata); |
|
|
} |
|
|
else { |
|
|
auxdata = bam_aux_next(bamdata, auxdata); |
|
|
} |
|
|
} |
|
|
else if (config->aux_remove) { |
|
|
iter = kh_get(aux_exists, config->aux_remove, TAGNUM(tag)); |
|
|
if (iter != kh_end(config->aux_remove)) { |
|
|
auxdata = bam_aux_remove(bamdata, auxdata); |
|
|
} |
|
|
else { |
|
|
auxdata = bam_aux_next(bamdata, auxdata); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int getRGlines(sam_hdr_t *in_samhdr, sam_hdr_t *out_samhdr) |
|
|
{ |
|
|
kstring_t line = KS_INITIALIZE; |
|
|
int i = 0, ret = 0, count = 0; |
|
|
const char rg[] = "RG"; |
|
|
|
|
|
if (!in_samhdr || !out_samhdr) { |
|
|
fprintf(stderr, "Invalid parameters in getRGlines!\n"); |
|
|
return 1; |
|
|
} |
|
|
|
|
|
if (-1 == (count = sam_hdr_count_lines(in_samhdr, rg))) { |
|
|
fprintf(stderr, "Failed to get RG count!\n"); |
|
|
return 1; |
|
|
} |
|
|
|
|
|
for (i = 0; i < count; ++i) |
|
|
{ |
|
|
ks_clear(&line); |
|
|
if (sam_hdr_find_line_pos(in_samhdr, rg, i, &line)) { |
|
|
fprintf(stderr, "Failed to get RG data!\n"); |
|
|
ret = 1; |
|
|
break; |
|
|
} |
|
|
if (sam_hdr_add_lines(out_samhdr, line.s, line.l)) { |
|
|
fprintf(stderr, "Failed to add RG data!\n"); |
|
|
ret = 1; |
|
|
break; |
|
|
} |
|
|
} |
|
|
ks_free(&line); |
|
|
|
|
|
return ret; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int getPGlines(sam_hdr_t *in_samhdr, sam_hdr_t *out_samhdr, conf_data *config, const char *argdump) |
|
|
{ |
|
|
kstring_t line = KS_INITIALIZE, id = KS_INITIALIZE; |
|
|
int i = 0, ret = 0, count = 0; |
|
|
const char pg[] = "PG"; |
|
|
|
|
|
if (!in_samhdr || !out_samhdr || !config) { |
|
|
fprintf(stderr, "Invalid parameters in getPGlines!\n"); |
|
|
return 1; |
|
|
} |
|
|
|
|
|
if (-1 == (count = sam_hdr_count_lines(in_samhdr, pg))) { |
|
|
fprintf(stderr, "Failed to get PG count!\n"); |
|
|
return 1; |
|
|
} |
|
|
|
|
|
if (config->pgid && config->pgid[0]) { |
|
|
for (i = 0; i < count; ++i) { |
|
|
if (sam_hdr_find_tag_pos(in_samhdr, pg, i, "ID", &id)) { |
|
|
fprintf(stderr, "Failed to get PG entry fields for line %d!\n", i + 1); |
|
|
break; |
|
|
} |
|
|
|
|
|
if (!strcmp(id.s, config->pgid)) |
|
|
break; |
|
|
|
|
|
|
|
|
ks_clear(&line); |
|
|
if (sam_hdr_find_line_pos(in_samhdr, "PG", i, &line)) { |
|
|
fprintf(stderr, "Failed to get PG data at %d!\n", i + 1); |
|
|
ret = 1; |
|
|
break; |
|
|
} |
|
|
|
|
|
|
|
|
if (sam_hdr_add_lines(out_samhdr, line.s, line.l)) { |
|
|
fprintf(stderr, "Failed to add PG data!\n"); |
|
|
ret = 1; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
else { |
|
|
for (i = 0; i < count; ++i) { |
|
|
if (sam_hdr_find_line_pos(in_samhdr, "PG", i, &line)) { |
|
|
fprintf(stderr, "Failed to get PG data at %d!\n", i + 1); |
|
|
ret = 1; |
|
|
break; |
|
|
} |
|
|
|
|
|
if (sam_hdr_add_lines(out_samhdr, line.s, line.l)) { |
|
|
fprintf(stderr, "Failed to add PG data!\n"); |
|
|
ret = 1; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if (!ret && !config->noPGentry) { |
|
|
|
|
|
if (-1 == (ret = sam_hdr_add_pg(out_samhdr, "samtools", "CL", argdump, NULL))) { |
|
|
fprintf(stderr, "Failed to set PG entry!\n"); |
|
|
} |
|
|
} |
|
|
ks_free(&line); |
|
|
ks_free(&id); |
|
|
|
|
|
return ret; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int reset(samFile *infile, samFile *outfile, conf_data *config, char *args) |
|
|
{ |
|
|
sam_hdr_t *in_samhdr = NULL, *out_samhdr = NULL; |
|
|
int ret = EXIT_FAILURE, ret_r = 0, ret_w = 0, i = 0; |
|
|
bam1_t *bamdata = NULL, *outdata = NULL; |
|
|
kstring_t querydata = KS_INITIALIZE, qualdata = KS_INITIALIZE; |
|
|
char *sp = NULL, *qp = NULL; |
|
|
uint8_t *bamquery = NULL, *bamqual = NULL; |
|
|
|
|
|
if (!infile || !outfile) { |
|
|
fprintf(stderr, "Invalid parameters in reset!\n"); |
|
|
goto error; |
|
|
} |
|
|
|
|
|
|
|
|
in_samhdr = sam_hdr_read(infile); |
|
|
if (!in_samhdr) |
|
|
{ |
|
|
fprintf(stderr, "Failed to read header from file!\n"); |
|
|
goto error; |
|
|
} |
|
|
|
|
|
if (!(out_samhdr = sam_hdr_init())) |
|
|
{ |
|
|
fprintf(stderr, "Failed to create output header!\n"); |
|
|
goto error; |
|
|
} |
|
|
|
|
|
|
|
|
if (-1 == sam_hdr_add_line(out_samhdr,"HD", "VN", SAM_FORMAT_VERSION, NULL)) { |
|
|
fprintf(stderr, "Failed to set header data!\n"); |
|
|
goto error; |
|
|
} |
|
|
|
|
|
if ((config->keepRGs && getRGlines(in_samhdr, out_samhdr)) || |
|
|
getPGlines(in_samhdr, out_samhdr, config, args)) { |
|
|
goto error; |
|
|
} |
|
|
|
|
|
|
|
|
if (sam_hdr_write(outfile, out_samhdr)) { |
|
|
print_error_errno("reset", "Output header write failed (%d)!\n", errno); |
|
|
goto error; |
|
|
} |
|
|
|
|
|
bamdata = bam_init1(); |
|
|
outdata = bam_init1(); |
|
|
if (!bamdata || !outdata) |
|
|
{ |
|
|
fprintf(stderr, "Failed to allocate data memory!\n"); |
|
|
goto error; |
|
|
} |
|
|
|
|
|
errno = 0; i = 0; |
|
|
sp = NULL; qp = NULL; |
|
|
bamquery = NULL; bamqual = NULL; |
|
|
|
|
|
|
|
|
while (0 <= (ret_r = sam_read1(infile, in_samhdr, bamdata))) |
|
|
{ |
|
|
sp = NULL; qp = NULL; |
|
|
bamquery = NULL; bamqual = NULL; |
|
|
|
|
|
|
|
|
if (bamdata->core.flag & BAM_FSECONDARY || bamdata->core.flag & BAM_FSUPPLEMENTARY) { |
|
|
continue; |
|
|
} |
|
|
|
|
|
|
|
|
uint16_t flags = bamdata->core.flag & ~BAM_FPROPER_PAIR; |
|
|
flags |= BAM_FUNMAP; |
|
|
if (bamdata->core.flag & BAM_FPAIRED) { |
|
|
flags |= BAM_FMUNMAP; |
|
|
} |
|
|
flags &= ~BAM_FMREVERSE; |
|
|
|
|
|
if (0 > ks_resize(&querydata, bamdata->core.l_qseq) || |
|
|
0 > ks_resize(&qualdata, bamdata->core.l_qseq)) { |
|
|
fprintf(stderr, "Failed to get allocate memory!\n"); |
|
|
ret_r = -4; |
|
|
break; |
|
|
} |
|
|
ks_clear(&querydata); |
|
|
ks_clear(&qualdata); |
|
|
|
|
|
sp = ks_str(&querydata); |
|
|
qp = ks_str(&qualdata); |
|
|
bamquery = bam_get_seq(bamdata); |
|
|
bamqual = bam_get_qual(bamdata); |
|
|
if (bamdata->core.flag & BAM_FREVERSE) { |
|
|
|
|
|
for (i = bamdata->core.l_qseq - 1; i >= 0; --i) { |
|
|
*sp++ = "=TGKCYSBAWRDMHVN"[bam_seqi(bamquery, i)]; |
|
|
*qp++ = bamqual[i]; |
|
|
} |
|
|
flags &= ~BAM_FREVERSE; |
|
|
} |
|
|
else { |
|
|
|
|
|
for (i = 0; i < bamdata->core.l_qseq ; ++i) { |
|
|
*sp++ = seq_nt16_str[bam_seqi(bamquery, i)]; |
|
|
} |
|
|
memcpy(qp, bam_get_qual(bamdata), bamdata->core.l_qseq); |
|
|
} |
|
|
|
|
|
removeauxtags(bamdata, config); |
|
|
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)))) { |
|
|
print_error_errno("reset", "Failed to set output data (%d)!\n", errno); |
|
|
break; |
|
|
} |
|
|
|
|
|
memcpy(bam_get_aux(outdata), bam_get_aux(bamdata), bam_get_l_aux(bamdata)); |
|
|
outdata->l_data += bam_get_l_aux(bamdata); |
|
|
|
|
|
errno = 0; |
|
|
|
|
|
if (0 > (ret_w = sam_write1(outfile, out_samhdr, outdata))) |
|
|
{ |
|
|
print_error_errno("reset", "Failed to write output data (%d)!\n", errno); |
|
|
break; |
|
|
} |
|
|
|
|
|
errno = 0; |
|
|
} |
|
|
|
|
|
if (-1 > ret_r || 0 > ret_w) { |
|
|
|
|
|
fprintf(stderr, "Error during %s!\n", (-1 > ret_r)? "read" : "write"); |
|
|
} |
|
|
else { |
|
|
|
|
|
ret = EXIT_SUCCESS; |
|
|
} |
|
|
|
|
|
error: |
|
|
|
|
|
if (in_samhdr) |
|
|
sam_hdr_destroy(in_samhdr); |
|
|
if (out_samhdr) |
|
|
sam_hdr_destroy(out_samhdr); |
|
|
|
|
|
if (bamdata) |
|
|
bam_destroy1(bamdata); |
|
|
if (outdata) |
|
|
bam_destroy1(outdata); |
|
|
|
|
|
if (qualdata.s) |
|
|
ks_free(&qualdata); |
|
|
if (querydata.s) |
|
|
ks_free(&querydata); |
|
|
return ret; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cleanup(conf_data *config) |
|
|
{ |
|
|
if (config->aux_keep) { |
|
|
kh_destroy(aux_exists, config->aux_keep); |
|
|
config->aux_keep = NULL; |
|
|
} |
|
|
if (config->aux_remove) { |
|
|
kh_destroy(aux_exists, config->aux_remove); |
|
|
config->aux_remove = NULL; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main_reset(int argc, char *argv[]) |
|
|
{ |
|
|
static const struct option lopts[] = { |
|
|
SAM_OPT_GLOBAL_OPTIONS('-', '-', 'O', '-', '-', '@'), |
|
|
{"keep-tag", required_argument, NULL, LONG_OPT('x')}, |
|
|
{"remove-tag", required_argument, NULL, 'x'}, |
|
|
{"no-RG", no_argument, NULL, 1}, |
|
|
|
|
|
{"reject-PG", required_argument, NULL, 'p'}, |
|
|
{"no-PG", no_argument, NULL, 2}, |
|
|
{NULL, 0, NULL, 0} |
|
|
}; |
|
|
samFile *infile = NULL, *outfile = NULL; |
|
|
sam_global_args ga = SAM_GLOBAL_ARGS_INIT; |
|
|
htsThreadPool tpool = {NULL, 0}; |
|
|
const char *inname = NULL, *outname = NULL; |
|
|
int c = 0, ret = EXIT_FAILURE; |
|
|
char outmode[4] = "w", *args = NULL; |
|
|
conf_data resetconf = {1, 0, NULL, NULL, NULL}; |
|
|
|
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, "o:@:x:O:", lopts, NULL)) >= 0) |
|
|
{ |
|
|
switch (c) |
|
|
{ |
|
|
case 1: |
|
|
if (!resetconf.keepRGs) { |
|
|
usage(stderr); |
|
|
goto exit; |
|
|
} |
|
|
resetconf.keepRGs = 0; |
|
|
break; |
|
|
case 2: |
|
|
if (resetconf.noPGentry) { |
|
|
usage(stderr); |
|
|
goto exit; |
|
|
} |
|
|
resetconf.noPGentry = 1; |
|
|
break; |
|
|
case 'p': |
|
|
if (resetconf.pgid) { |
|
|
usage(stderr); |
|
|
goto exit; |
|
|
} |
|
|
resetconf.pgid = optarg; |
|
|
break; |
|
|
case 'o': |
|
|
if (outname) { |
|
|
usage(stderr); |
|
|
goto exit; |
|
|
|
|
|
} |
|
|
outname = optarg; |
|
|
break; |
|
|
case 'x': |
|
|
if (*optarg == '^') { |
|
|
if (parse_aux_list(&resetconf.aux_keep, optarg+1, "main_reset")) { |
|
|
usage(stderr); |
|
|
goto exit; |
|
|
} |
|
|
} |
|
|
else { |
|
|
if (parse_aux_list(&resetconf.aux_remove, optarg, "main_reset")) { |
|
|
usage(stderr); |
|
|
goto exit; |
|
|
} |
|
|
} |
|
|
break; |
|
|
case LONG_OPT('x'): |
|
|
if (parse_aux_list(&resetconf.aux_keep, optarg, "main_reset")) { |
|
|
usage(stderr); |
|
|
goto exit; |
|
|
} |
|
|
break; |
|
|
|
|
|
default: |
|
|
if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) |
|
|
break; |
|
|
|
|
|
|
|
|
case '?': |
|
|
usage(stderr); |
|
|
goto exit; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
if (argc == 1 && isatty(STDIN_FILENO)) { |
|
|
|
|
|
usage(stdout); |
|
|
ret = EXIT_SUCCESS; |
|
|
goto exit; |
|
|
} |
|
|
|
|
|
|
|
|
if (!outname) |
|
|
outname = "-"; |
|
|
|
|
|
|
|
|
c = argc - optind; |
|
|
if (c > 1) { |
|
|
usage(stderr); |
|
|
goto exit; |
|
|
} |
|
|
|
|
|
if (c == 1) { |
|
|
inname = argv[optind]; |
|
|
} |
|
|
else { |
|
|
inname = "-"; |
|
|
} |
|
|
|
|
|
|
|
|
sam_open_mode(outmode + 1, outname, NULL); |
|
|
|
|
|
|
|
|
infile = sam_open(inname, "r"); |
|
|
outfile = sam_open_format(outname, outmode, &ga.out); |
|
|
if (!infile || !outfile) { |
|
|
fprintf(stderr, "Could not open %s%s%s\n", !infile ? inname : "", (!infile && !outfile)? ", " : "", !outfile ? outname : ""); |
|
|
goto exit; |
|
|
} |
|
|
|
|
|
|
|
|
if (ga.nthreads > 0) |
|
|
{ |
|
|
if (!(tpool.pool = hts_tpool_init(ga.nthreads))) |
|
|
{ |
|
|
fprintf(stderr, "\nFailed to setup thread pool\n"); |
|
|
goto exit; |
|
|
} |
|
|
|
|
|
hts_set_opt(infile, HTS_OPT_THREAD_POOL, &tpool); |
|
|
hts_set_opt(outfile, HTS_OPT_THREAD_POOL, &tpool); |
|
|
} |
|
|
|
|
|
args = stringify_argv(argc + 1, argv - 1); |
|
|
|
|
|
|
|
|
ret = reset(infile, outfile, &resetconf, args); |
|
|
|
|
|
exit: |
|
|
if (args) |
|
|
free(args); |
|
|
if (infile) |
|
|
sam_close(infile); |
|
|
if (outfile) |
|
|
sam_close(outfile); |
|
|
if (tpool.pool) |
|
|
hts_tpool_destroy(tpool.pool); |
|
|
cleanup(&resetconf); |
|
|
sam_global_args_free(&ga); |
|
|
|
|
|
return ret; |
|
|
} |
|
|
|