Skip to content

Commit

Permalink
Change auto_index method to return a numeric status and let HTSlib …
Browse files Browse the repository at this point in the history
…handle the index file name memory.
  • Loading branch information
valeriuo committed Apr 3, 2020
1 parent a7f0c96 commit a32db38
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 67 deletions.
9 changes: 3 additions & 6 deletions bam_addrprg.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,11 @@ static bool readgroupise(parsed_opts_t *opts, state_t* state, char *arg_list)
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)))
if (auto_index(state->output_file, opts->output_name, state->output_header) < 0) {
print_error_errno("addreplacerg", "[%s] Auto-indexing failed", __func__);
return false;
}
}

bam1_t* file_read = bam_init1();
Expand All @@ -424,25 +425,21 @@ static bool readgroupise(parsed_opts_t *opts, state_t* state, char *arg_list)
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;
}
}
Expand Down
5 changes: 3 additions & 2 deletions bam_markdup.c
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,6 @@ static int bam_mark_duplicates(md_param_t *param) {
long np_duplicate, np_opt_duplicate;
long opt_warnings = 0;
tmp_file_t temp;
char *idx_fn = NULL;
int exclude = 0;

if (!pair_hash || !single_hash || !read_buffer || !dup_hash) {
Expand Down Expand Up @@ -1185,8 +1184,10 @@ static int bam_mark_duplicates(md_param_t *param) {
goto fail;
}
if (param->write_index) {
if (!(idx_fn = auto_index(param->out, param->out_fn, header)))
if (auto_index(param->out, param->out_fn, header) < 0) {
fprintf(stderr, "[markdup] error auto-indexing.\n");
goto fail;
}
}

// used for coordinate order checks
Expand Down
16 changes: 3 additions & 13 deletions bam_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,6 @@ int bam_merge_core2(int by_qname, char* sort_tag, const char *out, const char *m
sam_hdr_t **hdr = NULL;
trans_tbl_t *translation_tbl = NULL;
int *rtrans = NULL;
char *out_idx_fn = NULL;
void *hreg = NULL;
hts_reglist_t *lreg = NULL;
merged_header_t *merged_hdr = init_merged_header();
Expand Down Expand Up @@ -1274,7 +1273,7 @@ int bam_merge_core2(int by_qname, char* sort_tag, const char *out, const char *m
return -1;
}
if (write_index) {
if (!(out_idx_fn = auto_index(fpout, out, hout))){
if (auto_index(fpout, out, hout) < 0){
sam_close(fpout);
return -1;
}
Expand All @@ -1293,7 +1292,6 @@ int bam_merge_core2(int by_qname, char* sort_tag, const char *out, const char *m
if (sam_write1(fpout, hout, b) < 0) {
print_error_errno(cmd, "failed writing to \"%s\"", out);
sam_close(fpout);
free(out_idx_fn);
return -1;
}
if ((j = (iter[heap->i]? sam_itr_next(fp[heap->i], iter[heap->i], b) : sam_read1(fp[heap->i], hdr[heap->i], b))) >= 0) {
Expand Down Expand Up @@ -1325,7 +1323,6 @@ int bam_merge_core2(int by_qname, char* sort_tag, const char *out, const char *m
goto fail;
}
}
free(out_idx_fn);

// Clean up and close
if (flag & MERGE_RG) {
Expand Down Expand Up @@ -1377,7 +1374,6 @@ int bam_merge_core2(int by_qname, char* sort_tag, const char *out, const char *m
free(heap);
free(fp);
free(rtrans);
free(out_idx_fn);
return -1;
}

Expand Down Expand Up @@ -1621,7 +1617,6 @@ static int bam_merge_simple(int by_qname, char *sort_tag, const char *out,
heap1_t *heap = NULL;
uint64_t idx = 0;
int i, heap_size = n + num_in_mem;
char *out_idx_fn = NULL;

g_is_by_qname = by_qname;
if (sort_tag) {
Expand Down Expand Up @@ -1697,7 +1692,7 @@ static int bam_merge_simple(int by_qname, char *sort_tag, const char *out,
}

if (write_index) {
if (!(out_idx_fn = auto_index(fpout, out, hout))){
if (auto_index(fpout, out, hout) < 0){
sam_close(fpout);
return -1;
}
Expand Down Expand Up @@ -1740,7 +1735,6 @@ static int bam_merge_simple(int by_qname, char *sort_tag, const char *out,
print_error_errno("merge", "writing index failed");
goto fail;
}
free(out_idx_fn);
}

if (sam_close(fpout) < 0) {
Expand All @@ -1762,7 +1756,6 @@ static int bam_merge_simple(int by_qname, char *sort_tag, const char *out,
free(fp);
free(heap);
if (fpout) sam_close(fpout);
free(out_idx_fn);
return -1;
}

Expand Down Expand Up @@ -1937,7 +1930,6 @@ static int write_buffer(const char *fn, const char *mode, size_t l, bam1_tag *bu
{
size_t i;
samFile* fp;
char *out_idx_fn = NULL;

fp = sam_open_format(fn, mode, fmt);
if (fp == NULL) return -1;
Expand All @@ -1950,7 +1942,7 @@ static int write_buffer(const char *fn, const char *mode, size_t l, bam1_tag *bu
if (sam_hdr_write(fp, h) != 0) goto fail;

if (write_index)
if (!(out_idx_fn = auto_index(fp, fn, (sam_hdr_t *)h))) goto fail;
if (auto_index(fp, fn, (sam_hdr_t *)h) < 0) goto fail;

if (n_threads > 1) hts_set_threads(fp, n_threads);
for (i = 0; i < l; ++i) {
Expand All @@ -1969,15 +1961,13 @@ static int write_buffer(const char *fn, const char *mode, size_t l, bam1_tag *bu
print_error_errno("merge", "writing index failed");
goto fail;
}
free(out_idx_fn);
}


if (sam_close(fp) < 0) return -1;
return 0;
fail:
sam_close(fp);
free(out_idx_fn);
return -1;
}

Expand Down
23 changes: 6 additions & 17 deletions bam_split.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ struct state {
sam_hdr_t* unaccounted_header;
size_t output_count;
char** rg_id;
char **rg_index_file_name;
char **rg_output_file_name;
samFile** rg_output_file;
sam_hdr_t** rg_output_header;
Expand Down Expand Up @@ -341,9 +340,8 @@ static int prep_sam_file(parsed_opts_t *opts, state_t *state, const char *tag, c
hts_set_opt(new_sam_file, HTS_OPT_THREAD_POOL, &state->p);

if (state->write_index) {
char *new_idx_fn = auto_index(new_sam_file, new_file_name, tmp_hdr);
if (!new_idx_fn) {
print_error_errno("split", "Creating index file for file \"%s\" failed", new_file_name);
if (auto_index(new_sam_file, new_file_name, tmp_hdr) < 0) {
print_error_errno("split", "Auto-indexing for file \"%s\" failed", new_file_name);
sam_close(new_sam_file);
ret = -1;
goto cleanup;
Expand All @@ -353,7 +351,6 @@ static int prep_sam_file(parsed_opts_t *opts, state_t *state, const char *tag, c
i = kh_put_t2f(state->tf_hash, tag_key, &ret);
if (ret < 0) {
print_error_errno("split", "Adding file \"%s\" failed", new_file_name);
free((void *)new_sam_file->fnidx);
sam_close(new_sam_file);
ret = -1;
goto cleanup;
Expand Down Expand Up @@ -452,13 +449,11 @@ static state_t* init(parsed_opts_t* opts, const char *arg_list)
if (opts->verbose) fprintf(stderr, "@RG's found %zu\n",retval->output_count);
// Prevent calloc(0, size);
size_t num = retval->output_count ? retval->output_count : 1;
retval->rg_index_file_name = (char **)calloc(num, sizeof(char *));
retval->rg_output_file_name = (char **)calloc(num, sizeof(char *));
retval->rg_output_file = (samFile**)calloc(num, sizeof(samFile*));
retval->rg_output_header = (sam_hdr_t**)calloc(num, sizeof(sam_hdr_t*));
retval->rg_hash = kh_init_c2i();
if (!retval->rg_output_file_name || !retval->rg_output_file || !retval->rg_output_header ||
!retval->rg_hash || !retval->rg_index_file_name) {
if (!retval->rg_output_file_name || !retval->rg_output_file || !retval->rg_output_header) {
print_error_errno("split", "Could not initialise output file array");
cleanup_state(retval, false);
return NULL;
Expand Down Expand Up @@ -567,10 +562,9 @@ static bool split(state_t* state, parsed_opts_t *opts, char *arg_list)
goto error;
}
if (state->write_index) {
state->rg_index_file_name[i] = auto_index(state->rg_output_file[i],
if (auto_index(state->rg_output_file[i],
state->rg_output_file_name[i],
state->rg_output_header[i]);
if (!state->rg_index_file_name[i]) {
state->rg_output_header[i]) < 0) {
print_error_errno("split", "Could not create index for file \"%s\"", state->rg_output_file_name[i]);
goto error;
}
Expand Down Expand Up @@ -631,7 +625,6 @@ static bool split(state_t* state, parsed_opts_t *opts, char *arg_list)
print_error_errno("split", "writing index failed");
return false;
}
free(state->rg_index_file_name[i]);
}
}
} else {
Expand Down Expand Up @@ -675,11 +668,8 @@ static bool split(state_t* state, parsed_opts_t *opts, char *arg_list)
for (iter = kh_begin(state->tf_hash); iter < kh_end(state->tf_hash); iter++) {
if (kh_exist(state->tf_hash, iter)) {
samFile *sf = kh_val(state->tf_hash, iter);
if (sf) {
if (sam_idx_save(sf) < 0)
if (sf && sam_idx_save(sf) < 0) {
print_error_errno("split", "Writing index for file \"%s\" failed", hts_get_fn(sf));
if (sf->fnidx)
free((void *)sf->fnidx);
}
}
}
Expand Down Expand Up @@ -723,7 +713,6 @@ static int cleanup_state(state_t* status, bool check_close)
free(status->rg_output_header);
free(status->rg_output_file);
free(status->rg_output_file_name);
free(status->rg_index_file_name);
kh_destroy_c2i(status->rg_hash);
if (status->tf_hash) {
khiter_t k;
Expand Down
6 changes: 2 additions & 4 deletions padding.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ int main_pad2unpad(int argc, char *argv[])
sam_hdr_t *h = 0, *h_fix = 0;
faidx_t *fai = 0;
int c, compress_level = -1, is_long_help = 0, no_pg = 0;
char in_mode[5], out_mode[6], *fn_out = 0, *fn_fai = 0, *fn_out_idx = NULL;
char in_mode[5], out_mode[6], *fn_out = 0, *fn_fai = 0;
int ret=0;
char *arg_list = NULL;
sam_global_args ga = SAM_GLOBAL_ARGS_INIT;
Expand Down Expand Up @@ -560,7 +560,7 @@ int main_pad2unpad(int argc, char *argv[])
goto depad_end;
}
if (ga.write_index) {
if (!(fn_out_idx = auto_index(out, fn_out, h_fix))) {
if (auto_index(out, fn_out, h_fix) < 0) {
ret = 1;
goto depad_end;
}
Expand Down Expand Up @@ -588,8 +588,6 @@ int main_pad2unpad(int argc, char *argv[])
ret = 1;
}
free(fn_fai); free(fn_out);
if (fn_out_idx)
free(fn_out_idx);
sam_global_args_free(&ga);
return ret;
}
Expand Down
22 changes: 10 additions & 12 deletions sam_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,46 +93,44 @@ static char *idx_suffix(htsFile *fp) {
* Utility function to add an index to a file we've opened for write.
* NB: Call this after writing the header and before writing sequences.
*
* The returned index filename should be freed by the caller, but only
* after sam_idx_save has been called.
*
* Returns index filename on success,
* NULL on failure.
* Returns 0 on success,
* -1 on failure.
*/
char *auto_index(htsFile *fp, const char *fn, bam_hdr_t *header) {
int auto_index(htsFile *fp, const char *fn, bam_hdr_t *header) {
char *fn_idx;
int min_shift = 14; /* CSI */
if (!fn || !*fn || strcmp(fn, "-") == 0)
return NULL;
return -1;

char *delim = strstr(fn, HTS_IDX_DELIM);
if (delim != NULL) {
delim += strlen(HTS_IDX_DELIM);

fn_idx = strdup(delim);
if (!fn_idx)
return NULL;
return -1;

size_t l = strlen(fn_idx);
if (l >= 4 && strcmp(fn_idx + l - 4, ".bai") == 0)
min_shift = 0;
} else {
char *suffix = idx_suffix(fp);
if (!suffix)
return NULL;
return -1;

fn_idx = malloc(strlen(fn)+6);
if (!fn_idx)
return NULL;
return -1;

sprintf(fn_idx, "%s.%s", fn, suffix);
}

if (sam_idx_init(fp, header, min_shift, fn_idx) < 0) {
print_error_errno("auto_index", "failed to open index \"%s\" for writing", fn_idx);
free(fn_idx);
return NULL;
return -1;
}

return fn_idx;
free(fn_idx);
return 0;
}
10 changes: 3 additions & 7 deletions sam_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ int main_samview(int argc, char *argv[])
sam_hdr_t *header = NULL;
char out_mode[5], out_un_mode[5], *out_format = "";
char *fn_in = 0, *fn_idx_in = 0, *fn_out = 0, *fn_fai = 0, *q, *fn_un_out = 0;
char *fn_out_idx = NULL, *fn_un_out_idx = NULL, *arg_list = NULL;
char *arg_list = NULL;
sam_global_args ga = SAM_GLOBAL_ARGS_INIT;
htsThreadPool p = {NULL, 0};
int filter_state = ALL, filter_op = 0;
Expand Down Expand Up @@ -550,7 +550,7 @@ int main_samview(int argc, char *argv[])
}
}
if (ga.write_index) {
if (!(fn_out_idx = auto_index(out, fn_out, header))) {
if (auto_index(out, fn_out, header) < 0) {
ret = 1;
goto view_end;
}
Expand Down Expand Up @@ -579,7 +579,7 @@ int main_samview(int argc, char *argv[])
}
}
if (ga.write_index) {
if (!(fn_un_out_idx = auto_index(un_out, fn_un_out, header))) {
if (auto_index(un_out, fn_un_out, header) < 0) {
ret = 1;
goto view_end;
}
Expand Down Expand Up @@ -789,10 +789,6 @@ int main_samview(int argc, char *argv[])
if (p.pool)
hts_tpool_destroy(p.pool);

if (fn_out_idx)
free(fn_out_idx);
if (fn_un_out_idx)
free(fn_un_out_idx);
free(arg_list);

return ret;
Expand Down
9 changes: 3 additions & 6 deletions samtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ void check_sam_close(const char *subcmd, samFile *fp, const char *fname, const c
* Utility function to add an index to a file we've opened for write.
* NB: Call this after writing the header and before writing sequences.
*
* The returned index filename should be freed by the caller, but only
* after sam_idx_save has been called.
*
* Returns index filename on success,
* NULL on failure.
* Returns 0 on success,
* -1 on failure.
*/
char *auto_index(htsFile *fp, const char *fn, bam_hdr_t *header);
int auto_index(htsFile *fp, const char *fn, bam_hdr_t *header);

#endif

0 comments on commit a32db38

Please sign in to comment.