-
Notifications
You must be signed in to change notification settings - Fork 461
Add method sam_open_write #1055
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
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -894,7 +894,9 @@ int bam_index_build(const char *fn, int min_shift) | |
| // Initialise fp->idx for the current format type. | ||
| // This must be called after the header has been written but no other data. | ||
| int sam_idx_init(htsFile *fp, sam_hdr_t *h, int min_shift, const char *fnidx) { | ||
| fp->fnidx = fnidx; | ||
| fp->fnidx = strdup(fnidx); | ||
| if (!fp->fnidx) return -1; | ||
|
|
||
| if (fp->format.format == bam || fp->format.format == bcf || | ||
| (fp->format.format == sam && fp->format.compression == bgzf)) { | ||
| int n_lvls, fmt = HTS_FMT_CSI; | ||
|
|
@@ -1671,6 +1673,30 @@ int sam_hdr_write(htsFile *fp, const sam_hdr_t *h) | |
| return 0; | ||
| } | ||
|
|
||
| htsFile *sam_open_write(const char *fn, sam_hdr_t *hdr, const char *mode, const htsFormat *fmt) { | ||
| htsFile *sf = NULL; | ||
| if (fn && mode) { | ||
| if (strchr(mode, 'w')) { | ||
| sf = hts_open_format(fn, mode, fmt); | ||
| if (sf) { | ||
| if (sam_hdr_write(sf, hdr) < 0) { | ||
| hts_log_error("Writing header to file \"%s\" failed", fn); | ||
| sam_close(sf); | ||
| sf = NULL; | ||
| } else { | ||
| if (sf->bam_header) | ||
| sam_hdr_destroy(sf->bam_header); | ||
| sf->bam_header = hdr; | ||
| } | ||
| } | ||
| } else { | ||
| hts_log_error("Only write mode allowed"); | ||
| } | ||
| } | ||
|
|
||
| return sf; | ||
| } | ||
|
|
||
| static int old_sam_hdr_change_HD(sam_hdr_t *h, const char *key, const char *val) | ||
| { | ||
| char *p, *q, *beg = NULL, *end = NULL, *newtext; | ||
|
|
@@ -3192,8 +3218,10 @@ int sam_format1(const bam_hdr_t *h, const bam1_t *b, kstring_t *str) | |
|
|
||
| // Sadly we need to be able to modify the bam_hdr here so we can | ||
| // reference count the structure. | ||
| int sam_write1(htsFile *fp, const sam_hdr_t *h, const bam1_t *b) | ||
| int sam_write1(htsFile *fp, const sam_hdr_t *hdr, const bam1_t *b) | ||
| { | ||
| const sam_hdr_t *h = hdr; | ||
| if (!h) h = fp->bam_header; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clearer as const sam_hdr_t *h = hdr? hdr : fp->bam_header;
if (h == NULL) { errno = EINVAL; return -3; }as in particular passing NULL when no headers are attached should be an error rather than a crash 😄 |
||
| switch (fp->format.format) { | ||
| case binary_format: | ||
| fp->format.category = sequence_data; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3295,7 +3295,8 @@ static int vcf_idx_init(htsFile *fp, bcf_hdr_t *h, int min_shift, const char *fn | |
| fp->idx = NULL; | ||
| return -1; | ||
| } | ||
| fp->fnidx = fnidx; | ||
| fp->fnidx = strdup(fnidx); | ||
| if (!fp->fnidx) return -1; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should tear down
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The top caller (
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. If
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well ideally the caller would inspect the return value of |
||
|
|
||
| return 0; | ||
| } | ||
|
|
@@ -3315,7 +3316,8 @@ int bcf_idx_init(htsFile *fp, bcf_hdr_t *h, int min_shift, const char *fnidx) { | |
|
|
||
| fp->idx = hts_idx_init(nids, HTS_FMT_CSI, bgzf_tell(fp->fp.bgzf), min_shift, n_lvls); | ||
| if (!fp->idx) return -1; | ||
| fp->fnidx = fnidx; | ||
| fp->fnidx = strdup(fnidx); | ||
| if (!fp->fnidx) return -1; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there other parts of the API that have
…fnin API function names? I'd rather spell it out as…filename(and it needs to be clear what if any relationship this has tohts_set_fai_filename).