diff --git a/htslib/sam.h b/htslib/sam.h index 7d6b983f9..831c6888a 100644 --- a/htslib/sam.h +++ b/htslib/sam.h @@ -1,7 +1,7 @@ /// @file htslib/sam.h /// High-level SAM/BAM/CRAM sequence file operations. /* - Copyright (C) 2008, 2009, 2013-2019 Genome Research Ltd. + Copyright (C) 2008, 2009, 2013-2020 Genome Research Ltd. Copyright (C) 2010, 2012, 2013 Broad Institute. Author: Heng Li @@ -818,6 +818,27 @@ char *stringify_argv(int argc, char *argv[]); HTSLIB_EXPORT void sam_hdr_incr_ref(sam_hdr_t *h); +/// Gets the inner header from the sam file +HTSLIB_EXPORT +const sam_hdr_t *sam_hdr_get(samFile *f); + +/// Sets the header of the sam file +/*! + * The method sets samFile::bam_header to the new hdr, following the replace + * policy (1 - replace the old header, 0 - keep the old header, if there is + * one). + * + * If the new header struct (hdr) is attached to f, its memory will be freed + * by sam_close, so the user must not attempt to free hdr separately. + * + * @param f SAM/BAM/CRAM file structure + * @param hdr Detached header structure to be set to f + * @param replace 1 - replace the old header, 0 - keep the old header + * @return -1 on failure, 0 header not replaced, 1 header replaced + */ +HTSLIB_EXPORT +int sam_hdr_set(samFile *f, sam_hdr_t *hdr, int replace); + /* * Macros for changing the \@HD line. They eliminate the need to use NULL method arguments. */ diff --git a/sam.c b/sam.c index 12742b5ec..ca5d6c355 100644 --- a/sam.c +++ b/sam.c @@ -1,6 +1,6 @@ /* sam.c -- SAM and BAM file I/O and manipulation. - Copyright (C) 2008-2010, 2012-2019 Genome Research Ltd. + Copyright (C) 2008-2010, 2012-2020 Genome Research Ltd. Copyright (C) 2010, 2012, 2013 Broad Institute. Author: Heng Li @@ -1671,6 +1671,24 @@ int sam_hdr_write(htsFile *fp, const sam_hdr_t *h) return 0; } +const sam_hdr_t *sam_hdr_get(samFile *f) { + sam_hdr_t *hdr = NULL; + if (f) hdr = f->bam_header; + + return hdr; +} + +int sam_hdr_set(samFile *f, sam_hdr_t *hdr, int replace) { + if (!f) return -1; + if (f->bam_header) { + if (replace) sam_hdr_destroy(f->bam_header); + else return 0; + } + f->bam_header = hdr; + + return 1; +} + 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;