From 6893503daefe04fccc21b7e85e97081df3082d62 Mon Sep 17 00:00:00 2001 From: Andrew Whitwham Date: Thu, 12 Sep 2024 14:29:35 +0100 Subject: [PATCH] Add a new function to deal with makeing the index. --- htslib/vcf.h | 20 ++++++++++++++++++-- vcf.c | 27 +++++++++++++++------------ 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/htslib/vcf.h b/htslib/vcf.h index f46a309e15..29ea4c6bb0 100644 --- a/htslib/vcf.h +++ b/htslib/vcf.h @@ -516,9 +516,25 @@ set to one of BCF_ERR* codes and must be checked before calling bcf_write(). */ HTSLIB_EXPORT int vcf_write_line(htsFile *fp, kstring_t *line); - + + + + /// Write a line to a VCF file for indexing + /** @param line Line to write + @param fp File to write it to + @param h The header for the vcf file + @param chr_id Chromosome id + @param name Chromosome name + @param beg Beginning position + @param end End position + @return 0 on success; -1 on failure + + @note Similar to vcf_write_line. No checks are done on the line being added, apart from + ensuring that it ends with a newline. This function + should therefore be used with care. + */ HTSLIB_EXPORT - int vcf_write_line_with_index(htsFile *fp, const bcf_hdr_t *h, kstring_t *line, int tid, hts_pos_t pos, hts_pos_t len); + int vcf_write_line_with_index(htsFile *fp, const bcf_hdr_t *h, kstring_t *line, int chr_id, char *name, hts_pos_t pos, hts_pos_t len); /************************************************************************** * Header querying and manipulation routines diff --git a/vcf.c b/vcf.c index 44bd928998..024d8ab10c 100644 --- a/vcf.c +++ b/vcf.c @@ -4258,28 +4258,31 @@ int vcf_write(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v) } -int vcf_write_line_with_index(htsFile *fp, const bcf_hdr_t *h, kstring_t *line, int tid, - hts_pos_t pos, hts_pos_t len) +int vcf_write_line_with_index(htsFile *fp, const bcf_hdr_t *h, kstring_t *line, int chr_id, + char *name, hts_pos_t pos, hts_pos_t len) { ssize_t ret; - - fprintf(stderr, "vcf_write_line_with_index start\n"); if (fp->format.compression == no_compression || !fp->idx) { // compressed reads only for indexing return -1; } - - ret = bgzf_write(fp->fp.bgzf, line->s, line->l); // error handling? - + + kputs("\n", line); + ret = bgzf_write(fp->fp.bgzf, line->s, line->l); + + int tid; + + if ((tid = hts_idx_tbi_name(fp->idx, chr_id, name)) < 0) { + return -1; + } + if (bgzf_idx_push(fp->fp.bgzf, fp->idx, tid, pos, pos + len, bgzf_tell(fp->fp.bgzf), 1) < 0) { return -1; } - - fprintf(stderr, "vcf_write_line_with_index end\n"); - - return 0; // again, more error handling needed + + return ret==line->l ? 0 : -1; } - + /************************ * Data access routines *