Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for gzopen() failure and print error messages #1013

Merged
merged 1 commit into from Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -189,7 +189,7 @@ bam_tview_html.o: bam_tview_html.c config.h $(bam_tview_h)
bam_flags.o: bam_flags.c config.h $(htslib_sam_h)
bamshuf.o: bamshuf.c config.h $(htslib_sam_h) $(htslib_hts_h) $(htslib_ksort_h) samtools.h $(htslib_thread_pool_h) $(sam_opts_h) $(htslib_khash_h)
bamtk.o: bamtk.c config.h $(htslib_hts_h) samtools.h version.h
bedcov.o: bedcov.c config.h $(htslib_kstring_h) $(htslib_sam_h) $(htslib_thread_pool_h) $(sam_opts_h) $(htslib_kseq_h)
bedcov.o: bedcov.c config.h $(htslib_kstring_h) $(htslib_sam_h) $(htslib_thread_pool_h) samtools.h $(sam_opts_h) $(htslib_kseq_h)
bedidx.o: bedidx.c config.h $(bedidx_h) $(htslib_ksort_h) $(htslib_kseq_h) $(htslib_khash_h)
cut_target.o: cut_target.c config.h $(htslib_hts_h) $(htslib_sam_h) $(htslib_faidx_h) samtools.h $(sam_opts_h)
dict.o: dict.c config.h $(htslib_kseq_h) $(htslib_hts_h)
Expand Down
5 changes: 5 additions & 0 deletions bedcov.c
Expand Up @@ -34,6 +34,7 @@ DEALINGS IN THE SOFTWARE. */
#include "htslib/kstring.h"
#include "htslib/sam.h"
#include "htslib/thread_pool.h"
#include "samtools.h"
#include "sam_opts.h"

#include "htslib/kseq.h"
Expand Down Expand Up @@ -139,6 +140,10 @@ int main_bedcov(int argc, char *argv[])
cnt = calloc(n, 8);

fp = gzopen(argv[optind], "rb");
if (fp == NULL) {
print_error_errno("bedcov", "can't open BED file '%s'", argv[optind]);
return 2;
}
ks = ks_init(fp);
n_plp = calloc(n, sizeof(int));
plp = calloc(n, sizeof(bam_pileup1_t*));
Expand Down
3 changes: 2 additions & 1 deletion misc/ace2sam.c
Expand Up @@ -93,7 +93,8 @@ int main(int argc, char *argv[])
s.l = s.m = 0; s.s = 0;
af_n = af_max = af_i = 0; af = 0;
for (i = 0; i < N_TMPSTR; ++i) t[i].l = t[i].m = 0, t[i].s = 0;
fp = strcmp(argv[1], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
fp = strcmp(argv[optind], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
if (fp == NULL) fatal("can't open input file");
ks = ks_init(fp);
while (ks_getuntil(ks, 0, &s, &dret) >= 0) {
if (strcmp(s.s, "CO") == 0) { // contig sequence
Expand Down
8 changes: 7 additions & 1 deletion misc/wgsim.c
Expand Up @@ -38,6 +38,7 @@
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <zlib.h>
#include "../version.h"
#include "htslib/kseq.h"
Expand Down Expand Up @@ -241,6 +242,12 @@ void wgsim_core(FILE *fpout1, FILE *fpout2, const char *fn, int is_hap, uint64_t
mut_t *target;
int max_loop, max_loop_err = 0;

fp_fa = gzopen(fn, "r");
if (fp_fa == NULL) {
fprintf(stderr, "[wgsim] can't open '%s': %s\n", fn, strerror(errno));
return;
}

l = size_l > size_r? size_l : size_r;
qstr = (char*)calloc(l+1, 1);
tmp_seq[0] = (uint8_t*)calloc(l+2, 1);
Expand All @@ -250,7 +257,6 @@ void wgsim_core(FILE *fpout1, FILE *fpout2, const char *fn, int is_hap, uint64_t

Q = (ERR_RATE == 0.0)? 'I' : (int)(-10.0 * log(ERR_RATE) / log(10.0) + 0.499) + 33;

fp_fa = gzopen(fn, "r");
ks = kseq_init(fp_fa);
tot_len = n_ref = 0;
fprintf(stderr, "[%s] calculating the total length of the reference sequence...\n", __func__);
Expand Down
9 changes: 8 additions & 1 deletion phase.c
Expand Up @@ -511,9 +511,15 @@ static khash_t(set64) *loadpos(const char *fn, bam_hdr_t *h)
kstring_t *str;
khash_t(set64) *hash;

fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
if (fp == NULL) {
print_error_errno("phase", "Couldn't open site file '%s'", fn);
return NULL;
}

hash = kh_init(set64);
str = calloc(1, sizeof(kstring_t));
fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");

ks = ks_init(fp);
while (ks_getuntil(ks, 0, str, &dret) >= 0) {
int tid = bam_name2id(h, str->s);
Expand Down Expand Up @@ -638,6 +644,7 @@ int main_phase(int argc, char *argv[])
}
if (fn_list) { // read the list of sites to phase
set = loadpos(fn_list, g.fp_hdr);
if (set == NULL) return 1;
free(fn_list);
} else g.flag &= ~FLAG_LIST_EXCL;
if (g.pre) { // open BAMs to write
Expand Down