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
|
#include "samtools.pysam.h"
/* bam_addrprg.c -- samtools command to add or replace readgroups.
Copyright (c) 2013, 2015-2017, 2019 Genome Research Limited.
Author: Martin O. Pollard <mp15@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 <htslib/sam.h>
#include <htslib/kstring.h>
#include "samtools.h"
#include "htslib/thread_pool.h"
#include "sam_opts.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
#include <assert.h>
#include <unistd.h>
typedef enum {
overwrite_all,
orphan_only,
} rg_mode;
struct parsed_opts {
char* input_name;
char* output_name;
char* rg_id;
char* rg_line;
int no_pg;
rg_mode mode;
sam_global_args ga;
htsThreadPool p;
};
struct state;
typedef struct parsed_opts parsed_opts_t;
typedef struct state state_t;
struct state {
samFile* input_file;
sam_hdr_t* input_header;
samFile* output_file;
sam_hdr_t* output_header;
char* rg_id;
void (*mode_func)(const state_t*, bam1_t*);
};
static void cleanup_opts(parsed_opts_t* opts)
{
if (!opts) return;
free(opts->rg_id);
free(opts->output_name);
free(opts->input_name);
free(opts->rg_line);
if (opts->p.pool) hts_tpool_destroy(opts->p.pool);
sam_global_args_free(&opts->ga);
free(opts);
}
static void cleanup_state(state_t* state)
{
if (!state) return;
free(state->rg_id);
if (state->output_file) sam_close(state->output_file);
sam_hdr_destroy(state->output_header);
if (state->input_file) sam_close(state->input_file);
sam_hdr_destroy(state->input_header);
free(state);
}
// Converts \t and \n into real tabs and newlines
static char* basic_unescape(const char* in)
{
assert(in);
char *ptr, *out;
out = ptr = malloc(strlen(in)+1);
size_t size = 0;
while (*in) {
if (*in == '\\') {
++in;
if (*in == '\0') {
fprintf(samtools_stderr, "[%s] Unterminated escape sequence.\n", __func__);
free(out);
return NULL;
}
switch (*in) {
case '\\':
*ptr = '\\';
break;
case 't':
*ptr = '\t';
break;
case 'n':
fprintf(samtools_stderr, "[%s] \\n in escape sequence is not supported.\n", __func__);
free(out);
return NULL;
default:
fprintf(samtools_stderr, "[%s] Unsupported escape sequence.\n", __func__);
free(out);
return NULL;
}
} else {
*ptr = *in;
}
++in;
++ptr;
++size;
}
*ptr = '\0';
++size;
char* tmp = (char*)realloc(out, size);
if (!tmp) {
free(out);
}
return tmp;
}
// Malloc a string containing [s,slim) or to the end of s if slim is NULL.
// If lenp is non-NULL, stores the length of the resulting string there.
static char *dup_substring(const char *s, const char *slim, size_t *lenp)
{
size_t len = slim? (slim - s) : strlen(s);
char *ns = malloc(len+1);
if (ns == NULL) return NULL;
memcpy(ns, s, len);
ns[len] = '\0';
if (lenp) *lenp = len;
return ns;
}
// Given a @RG line return the id
static char* get_rg_id(const char *line)
{
const char *id = strstr(line, "\tID:");
if (! id) return NULL;
id += 4;
return dup_substring(id, strchr(id, '\t'), NULL);
}
static void usage(FILE *fp)
{
fprintf(fp,
"Usage: samtools addreplacerg [options] [-r <@RG line> | -R <existing id>] [-o <output.bam>] <input.bam>\n"
"\n"
"Options:\n"
" -m MODE Set the mode of operation from one of overwrite_all, orphan_only [overwrite_all]\n"
" -o FILE Where to write output to [samtools_stdout]\n"
" -r STRING @RG line text\n"
" -R STRING ID of @RG line in existing header to use\n"
" --no-PG Do not add a PG line\n"
);
sam_global_opt_help(fp, "..O..@..");
}
static bool parse_args(int argc, char** argv, parsed_opts_t** opts)
{
*opts = NULL;
int n;
if (argc == 1) { usage(samtools_stdout); return true; }
parsed_opts_t* retval = calloc(1, sizeof(parsed_opts_t));
if (! retval ) {
fprintf(samtools_stderr, "[%s] Out of memory allocating parsed_opts_t\n", __func__);
return false;
}
// Set defaults
retval->mode = overwrite_all;
sam_global_args_init(&retval->ga);
static const struct option lopts[] = {
SAM_OPT_GLOBAL_OPTIONS(0, 0, 'O', 0, 0, '@'),
{"no-PG", no_argument, NULL, 1},
{ NULL, 0, NULL, 0 }
};
kstring_t rg_line = {0,0,NULL};
while ((n = getopt_long(argc, argv, "r:R:m:o:O:l:h@:", lopts, NULL)) >= 0) {
switch (n) {
case 'r':
// Are we adding to existing rg line?
if (ks_len(&rg_line) == 0) {
if (strlen(optarg)<3 || (optarg[0] != '@' && optarg[1] != 'R' && optarg[2] != 'G')) {
kputs("@RG\t", &rg_line);
}
} else {
kputs("\t", &rg_line);
}
kputs(optarg, &rg_line);
break;
case 'R':
retval->rg_id = strdup(optarg);
break;
case 'm': {
if (strcmp(optarg, "overwrite_all") == 0) {
retval->mode = overwrite_all;
} else if (strcmp(optarg, "orphan_only") == 0) {
retval->mode = orphan_only;
} else {
usage(samtools_stderr);
return false;
}
break;
}
case 'o':
retval->output_name = strdup(optarg);
break;
case 'h':
usage(samtools_stdout);
free(retval);
return true;
case 1:
retval->no_pg = 1;
break;
case '?':
usage(samtools_stderr);
free(retval);
return false;
case 'O':
default:
if (parse_sam_global_opt(n, optarg, lopts, &retval->ga) == 0) break;
usage(samtools_stderr);
free(retval);
return false;
}
}
retval->rg_line = ks_release(&rg_line);
if (argc-optind < 1) {
fprintf(samtools_stderr, "You must specify an input file.\n");
usage(samtools_stderr);
cleanup_opts(retval);
return false;
}
if (retval->rg_id && retval->rg_line) {
fprintf(samtools_stderr, "The options -r and -R are mutually exclusive.\n");
cleanup_opts(retval);
return false;
}
if (retval->rg_line)
{
char* tmp = basic_unescape(retval->rg_line);
if ((retval->rg_id = get_rg_id(tmp)) == NULL) {
fprintf(samtools_stderr, "[%s] The supplied RG line lacks an ID tag.\n", __func__);
free(tmp);
cleanup_opts(retval);
return false;
}
free(retval->rg_line);
retval->rg_line = tmp;
}
retval->input_name = strdup(argv[optind+0]);
if (retval->ga.nthreads > 0) {
if (!(retval->p.pool = hts_tpool_init(retval->ga.nthreads))) {
fprintf(samtools_stderr, "Error creating thread pool\n");
return false;
}
}
*opts = retval;
return true;
}
static void overwrite_all_func(const state_t* state, bam1_t* file_read)
{
uint8_t* data = (uint8_t*)strdup(state->rg_id);
int len = strlen(state->rg_id)+1;
// If the old exists delete it
uint8_t* old = bam_aux_get(file_read, "RG");
if (old != NULL) {
bam_aux_del(file_read, old);
}
bam_aux_append(file_read, "RG", 'Z', len, data);
free(data);
}
static void orphan_only_func(const state_t* state, bam1_t* file_read)
{
uint8_t* data = (uint8_t*)strdup(state->rg_id);
int len = strlen(state->rg_id)+1;
// If the old exists don't do anything
uint8_t* old = bam_aux_get(file_read, "RG");
if (old == NULL) {
bam_aux_append(file_read, "RG",'Z',len,data);
}
free(data);
}
static bool init(const parsed_opts_t* opts, state_t** state_out) {
char output_mode[8] = "w";
state_t* retval = (state_t*) calloc(1, sizeof(state_t));
if (retval == NULL) {
fprintf(samtools_stderr, "[init] Out of memory allocating state struct.\n");
return false;
}
*state_out = retval;
// Open files
retval->input_file = sam_open_format(opts->input_name, "r", &opts->ga.in);
if (retval->input_file == NULL) {
print_error_errno("addreplacerg", "could not open \"%s\"", opts->input_name);
return false;
}
retval->input_header = sam_hdr_read(retval->input_file);
retval->output_header = sam_hdr_dup(retval->input_header);
if (opts->output_name) // File format auto-detection
sam_open_mode(output_mode + 1, opts->output_name, NULL);
retval->output_file = sam_open_format(opts->output_name == NULL?"-":opts->output_name, output_mode, &opts->ga.out);
if (retval->output_file == NULL) {
print_error_errno("addreplacerg", "could not create \"%s\"", opts->output_name);
return false;
}
if (opts->p.pool) {
hts_set_opt(retval->input_file, HTS_OPT_THREAD_POOL, &opts->p);
hts_set_opt(retval->output_file, HTS_OPT_THREAD_POOL, &opts->p);
}
if (opts->rg_line) {
// Append new RG line to header.
// Check does not already exist
kstring_t hdr_line = { 0, 0, NULL };
if (sam_hdr_find_line_id(retval->output_header, "RG", "ID", opts->rg_id, &hdr_line) == 0) {
fprintf(samtools_stderr, "[init] ID of new RG line specified conflicts with that of an existing header RG line. Overwrite not yet implemented.\n");
free(hdr_line.s);
return false;
}
if (-1 == sam_hdr_add_lines(retval->output_header, opts->rg_line, strlen(opts->rg_line))) {
fprintf(samtools_stderr, "[init] Error adding RG line with ID:%s to the output header.\n", opts->rg_id);
return false;
}
if (opts->mode == overwrite_all &&
-1 == sam_hdr_remove_except(retval->output_header, "RG", "ID", opts->rg_id)) {
fprintf(samtools_stderr, "[init] Error removing the old RG lines from the output header.\n");
return false;
}
retval->rg_id = strdup(opts->rg_id);
} else {
if (opts->rg_id) {
// Confirm what has been supplied exists
kstring_t hdr_line = { 0, 0, NULL };
if (sam_hdr_find_line_id(retval->output_header, "RG", "ID", opts->rg_id, &hdr_line) < 0) {
fprintf(samtools_stderr, "RG ID supplied does not exist in header. Supply full @RG line with -r instead?\n");
return false;
}
retval->rg_id = strdup(opts->rg_id);
free(hdr_line.s);
} else {
kstring_t rg_id = { 0, 0, NULL };
if (sam_hdr_find_tag_id(retval->output_header, "RG", NULL, NULL, "ID", &rg_id) < 0) {
fprintf(samtools_stderr, "No RG specified on command line or in existing header.\n");
return false;
}
retval->rg_id = ks_release(&rg_id);
}
}
switch (opts->mode) {
case overwrite_all:
retval->mode_func = &overwrite_all_func;
break;
case orphan_only:
retval->mode_func = &orphan_only_func;
break;
}
return true;
}
static bool readgroupise(parsed_opts_t *opts, state_t* state, char *arg_list)
{
if (!opts->no_pg && sam_hdr_add_pg(state->output_header, "samtools",
"VN", samtools_version(),
arg_list ? "CL": NULL,
arg_list ? arg_list : NULL,
NULL))
return false;
if (sam_hdr_write(state->output_file, state->output_header) != 0) {
print_error_errno("addreplacerg", "[%s] Could not write header to output file", __func__);
return false;
}
char *idx_fn = NULL;
if (opts->ga.write_index) {
if (!(idx_fn = auto_index(state->output_file, opts->output_name, state->output_header)))
return false;
}
bam1_t* file_read = bam_init1();
int ret;
while ((ret = sam_read1(state->input_file, state->input_header, file_read)) >= 0) {
state->mode_func(state, file_read);
if (sam_write1(state->output_file, state->output_header, file_read) < 0) {
print_error_errno("addreplacerg", "[%s] Could not write read to output file", __func__);
bam_destroy1(file_read);
free(idx_fn);
return false;
}
}
bam_destroy1(file_read);
if (ret != -1) {
print_error_errno("addreplacerg", "[%s] Error reading from input file", __func__);
free(idx_fn);
return false;
} else {
if (opts->ga.write_index) {
if (sam_idx_save(state->output_file) < 0) {
print_error_errno("addreplacerg", "[%s] Writing index failed", __func__);
free(idx_fn);
return false;
}
}
free(idx_fn);
return true;
}
}
int main_addreplacerg(int argc, char** argv)
{
parsed_opts_t* opts = NULL;
state_t* state = NULL;
char *arg_list = stringify_argv(argc+1, argv-1);
if (!arg_list)
return EXIT_FAILURE;
if (!parse_args(argc, argv, &opts)) goto error;
if (opts) { // Not an error but user doesn't want us to proceed
if (!init(opts, &state) || !readgroupise(opts, state, arg_list))
goto error;
}
cleanup_state(state);
cleanup_opts(opts);
free(arg_list);
return EXIT_SUCCESS;
error:
cleanup_state(state);
cleanup_opts(opts);
free(arg_list);
return EXIT_FAILURE;
}
|