Skip to content

Commit 75fdb1c

Browse files
committed
Add method sam_open_write for opening a htsFile file for writing only and setting a header to it.
Add getter methods hts_get_hdr, hts_get_fn.
1 parent f58a6f3 commit 75fdb1c

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

hts.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,16 @@ const htsFormat *hts_get_format(htsFile *fp)
12421242
return fp? &fp->format : NULL;
12431243
}
12441244

1245+
const struct sam_hdr_t *hts_get_hdr(htsFile *fp)
1246+
{
1247+
return fp? fp->bam_header : NULL;
1248+
}
1249+
1250+
const char *hts_get_fn(htsFile *fp)
1251+
{
1252+
return fp? fp->fn : NULL;
1253+
}
1254+
12451255
const char *hts_format_file_extension(const htsFormat *format) {
12461256
if (!format)
12471257
return "?";

htslib/hts.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,22 @@ int hts_close(htsFile *fp);
508508
HTSLIB_EXPORT
509509
const htsFormat *hts_get_format(htsFile *fp);
510510

511+
/*!
512+
@abstract Returns the file's header field
513+
@param fp The file handle
514+
@return Read-only pointer to the file's bam_header field.
515+
*/
516+
HTSLIB_EXPORT
517+
const struct sam_hdr_t *hts_get_hdr(htsFile *fp);
518+
519+
/*!
520+
@abstract Returns the file's name
521+
@param fp The file handle
522+
@return Read-only pointer to the file's fn field.
523+
*/
524+
HTSLIB_EXPORT
525+
const char *hts_get_fn(htsFile *fp);
526+
511527
/*!
512528
@ abstract Returns a string containing the file format extension.
513529
@ param format Format structure containing the file type.

htslib/sam.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,20 @@ const char *sam_parse_region(sam_hdr_t *h, const char *s, int *tid,
13251325
const char *mode,
13261326
const char *format);
13271327

1328+
/// sam_open_write - Open a file for writing only
1329+
/** The method opens a new alignment file for writing, writes header hdr to it
1330+
* and attaches the header struct to the returned htsFile struct.
1331+
* If successful, ownership of hdr is given to the htsFile struct and hdr
1332+
* should not be freed separately.
1333+
* @param fn Name of the file
1334+
* @param h Pointer to the header previously read or created
1335+
* @param mode Pointer to the mode string (must contain "w")
1336+
* @param fmt Pointer to the format
1337+
* @return Pointer to the htsFile (with hdr) on success, NULL on failure
1338+
*/
1339+
HTSLIB_EXPORT
1340+
htsFile *sam_open_write(const char *fn, sam_hdr_t *hdr, const char *mode, const htsFormat *fmt);
1341+
13281342
HTSLIB_EXPORT
13291343
int sam_hdr_change_HD(sam_hdr_t *h, const char *key, const char *val);
13301344

sam.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,30 @@ int sam_hdr_write(htsFile *fp, const sam_hdr_t *h)
16711671
return 0;
16721672
}
16731673

1674+
htsFile *sam_open_write(const char *fn, sam_hdr_t *hdr, const char *mode, const htsFormat *fmt) {
1675+
htsFile *sf = NULL;
1676+
if (fn && mode) {
1677+
if (strchr(mode, 'w')) {
1678+
sf = hts_open_format(fn, mode, fmt);
1679+
if (sf) {
1680+
if (sam_hdr_write(sf, hdr) < 0) {
1681+
hts_log_error("Writing header to file \"%s\" failed", fn);
1682+
sam_close(sf);
1683+
sf = NULL;
1684+
} else {
1685+
if (sf->bam_header)
1686+
sam_hdr_destroy(sf->bam_header);
1687+
sf->bam_header = hdr;
1688+
}
1689+
}
1690+
} else {
1691+
hts_log_error("Only write mode allowed");
1692+
}
1693+
}
1694+
1695+
return sf;
1696+
}
1697+
16741698
static int old_sam_hdr_change_HD(sam_hdr_t *h, const char *key, const char *val)
16751699
{
16761700
char *p, *q, *beg = NULL, *end = NULL, *newtext;

0 commit comments

Comments
 (0)