Skip to content

Commit

Permalink
Add a new function to deal with makeing the index.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitwham committed Sep 12, 2024
1 parent b6c7801 commit 4ef9f3e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
20 changes: 18 additions & 2 deletions htslib/vcf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 15 additions & 12 deletions vcf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down

0 comments on commit 4ef9f3e

Please sign in to comment.