<commit_before>#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include "kstring.h"
#include "bcf.h"

int bcf_hdr_read(bcf_t *b);

void bcf_hdr_clear(bcf_hdr_t *b)
{
	free(b->name); free(b->sname); free(b->txt); free(b->ns); free(b->sns);
	memset(b, 0, sizeof(bcf_hdr_t));
}

bcf_t *bcf_open(const char *fn, const char *mode)
{
	bcf_t *b;
	b = calloc(1, sizeof(bcf_t));
	if (strchr(mode, 'w')) {
		b->fp = strcmp(fn, "-")? bgzf_open(fn, mode) : bgzf_fdopen(fileno(stdout), "w");
	} else {
		b->fp = strcmp(fn, "-")? bgzf_open(fn, mode) : bgzf_fdopen(fileno(stdin), "r");
	}
	b->fp->owned_file = 1;
	if (strchr(mode, 'r')) bcf_hdr_read(b);
	return b;
}

int bcf_close(bcf_t *b)
{
	int ret;
	if (b == 0) return 0;
	ret = bgzf_close(b->fp);
	free(b->h.name); free(b->h.sname); free(b->h.txt); free(b->h.ns); free(b->h.sns);
	free(b);
	return ret;
}

int bcf_hdr_write(bcf_t *b)
{
	bcf_hdr_t *h;
	if (b == 0) return -1;
	h = &b->h;
	bgzf_write(b->fp, "BCF\4", 4);
	bgzf_write(b->fp, &h->l_nm, 4);
	bgzf_write(b->fp, h->name, h->l_nm);
	bgzf_write(b->fp, &h->l_smpl, 4);
	bgzf_write(b->fp, h->sname, h->l_smpl);
	bgzf_write(b->fp, &h->l_txt, 4);
	bgzf_write(b->fp, h->txt, h->l_txt);
	bgzf_flush(b->fp);
	return 16 + h->l_nm + h->l_smpl + h->l_txt;
}

int bcf_hdr_read(bcf_t *b)
{
	uint8_t magic[4];
	bcf_hdr_t *h;
	if (b == 0) return -1;
	bcf_hdr_clear(&b->h);
	h = &b->h;
	bgzf_read(b->fp, magic, 4);
	bgzf_read(b->fp, &h->l_nm, 4);
	h->name = malloc(h->l_nm);
	bgzf_read(b->fp, h->name, h->l_nm);
	bgzf_read(b->fp, &h->l_smpl, 4);
	h->sname = malloc(h->l_smpl);
	bgzf_read(b->fp, h->sname, h->l_smpl);
	bgzf_read(b->fp, &h->l_txt, 4);
	h->txt = malloc(h->l_txt);
	bgzf_read(b->fp, h->txt, h->l_txt);
	bcf_hdr_sync(&b->h);
	return 16 + h->l_nm + h->l_smpl + h->l_txt;
}

static inline char **cnt_null(int l, char *str, int *_n)
{
	int n = 0;
	char *p, **list;
	*_n = 0;
	if (l == 0 || str == 0) return 0;
	for (p = str; p != str + l; ++p)
		if (*p == 0) ++n;
	*_n = n;
	list = calloc(n, sizeof(void*));
	list[0] = str;
	for (p = str, n = 1; p < str + l - 1; ++p)
		if (*p == 0) list[n++] = p + 1;
	return list;
}

int bcf_hdr_sync(bcf_hdr_t *b)
{
	if (b == 0) return -1;
	b->ns = cnt_null(b->l_nm, b->name, &b->n_ref);
	b->sns = cnt_null(b->l_smpl, b->sname, &b->n_smpl);
	return 0;
}

#define char2int(s) (((int)s[0])<<8|s[1])

int bcf_sync(int n_smpl, bcf1_t *b)
{
	char *p, *tmp[4], *s;
	int i, n, c;
	// set ref, alt, flt, info, fmt
	b->ref = b->alt = b->flt = b->info = b->fmt = 0;
	for (p = b->str, n = 0; p < b->str + b->l_str; ++p)
		if (*p == 0 && p+1 != b->str + b->l_str) tmp[n++] = p + 1;
	if (n != 5) return -1;
	b->ref = tmp[0]; b->alt = tmp[1]; b->flt = tmp[2]; b->info = tmp[3]; b->fmt = tmp[4];
	// set n_alleles
	for (p = b->alt, n = 1; *p; ++p)
		if (*p == ',') ++n;
	b->n_alleles = n;
	c = toupper(*b->ref);
	if (b->alt - b->ref == 2 && (c == 'A' || c == 'C' || c == 'G' || c == 'T'))
		++b->n_alleles;
	// set n_gi and gi[i].fmt
	for (p = b->fmt, n = 1; *p; ++p)
		if (*p == ':') ++n;
	if (n > b->m_gi) {
		int old_m = b->m_gi;
		b->m_gi = n;
		kroundup32(b->m_gi);
		b->gi = realloc(b->gi, b->m_gi * sizeof(bcf_ginfo_t));
		memset(b->gi + old_m, 0, (b->m_gi - old_m) * sizeof(bcf_ginfo_t));
	}
	b->n_gi = n;
	for (p = s = b->fmt, n = 0; *p; ++p) {
		if (*p == ':' || *(p+1) == 0) {<commit_msg>fixed a bug in bcf reader<commit_after>#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include "kstring.h"
#include "bcf.h"

int bcf_hdr_read(bcf_t *b);

void bcf_hdr_clear(bcf_hdr_t *b)
{
	free(b->name); free(b->sname); free(b->txt); free(b->ns); free(b->sns);
	memset(b, 0, sizeof(bcf_hdr_t));
}

bcf_t *bcf_open(const char *fn, const char *mode)
{
	bcf_t *b;
	b = calloc(1, sizeof(bcf_t));
	if (strchr(mode, 'w')) {
		b->fp = strcmp(fn, "-")? bgzf_open(fn, mode) : bgzf_fdopen(fileno(stdout), "w");
	} else {
		b->fp = strcmp(fn, "-")? bgzf_open(fn, mode) : bgzf_fdopen(fileno(stdin), "r");
	}
	b->fp->owned_file = 1;
	if (strchr(mode, 'r')) bcf_hdr_read(b);
	return b;
}

int bcf_close(bcf_t *b)
{
	int ret;
	if (b == 0) return 0;
	ret = bgzf_close(b->fp);
	free(b->h.name); free(b->h.sname); free(b->h.txt); free(b->h.ns); free(b->h.sns);
	free(b);
	return ret;
}

int bcf_hdr_write(bcf_t *b)
{
	bcf_hdr_t *h;
	if (b == 0) return -1;
	h = &b->h;
	bgzf_write(b->fp, "BCF\4", 4);
	bgzf_write(b->fp, &h->l_nm, 4);
	bgzf_write(b->fp, h->name, h->l_nm);
	bgzf_write(b->fp, &h->l_smpl, 4);
	bgzf_write(b->fp, h->sname, h->l_smpl);
	bgzf_write(b->fp, &h->l_txt, 4);
	bgzf_write(b->fp, h->txt, h->l_txt);
	bgzf_flush(b->fp);
	return 16 + h->l_nm + h->l_smpl + h->l_txt;
}

int bcf_hdr_read(bcf_t *b)
{
	uint8_t magic[4];
	bcf_hdr_t *h;
	if (b == 0) return -1;
	bcf_hdr_clear(&b->h);
	h = &b->h;
	bgzf_read(b->fp, magic, 4);
	bgzf_read(b->fp, &h->l_nm, 4);
	h->name = malloc(h->l_nm);
	bgzf_read(b->fp, h->name, h->l_nm);
	bgzf_read(b->fp, &h->l_smpl, 4);
	h->sname = malloc(h->l_smpl);
	bgzf_read(b->fp, h->sname, h->l_smpl);
	bgzf_read(b->fp, &h->l_txt, 4);
	h->txt = malloc(h->l_txt);
	bgzf_read(b->fp, h->txt, h->l_txt);
	bcf_hdr_sync(&b->h);
	return 16 + h->l_nm + h->l_smpl + h->l_txt;
}

static inline char **cnt_null(int l, char *str, int *_n)
{
	int n = 0;
	char *p, **list;
	*_n = 0;
	if (l == 0 || str == 0) return 0;
	for (p = str; p != str + l; ++p)
		if (*p == 0) ++n;
	*_n = n;
	list = calloc(n, sizeof(void*));
	list[0] = str;
	for (p = str, n = 1; p < str + l - 1; ++p)
		if (*p == 0) list[n++] = p + 1;
	return list;
}

int bcf_hdr_sync(bcf_hdr_t *b)
{
	if (b == 0) return -1;
	b->ns = cnt_null(b->l_nm, b->name, &b->n_ref);
	b->sns = cnt_null(b->l_smpl, b->sname, &b->n_smpl);
	return 0;
}

#define char2int(s) (((int)s[0])<<8|s[1])

int bcf_sync(int n_smpl, bcf1_t *b)
{
	char *p, *tmp[5], *s;
	int i, n, c;
	// set ref, alt, flt, info, fmt
	b->ref = b->alt = b->flt = b->info = b->fmt = 0;
	for (p = b->str, n = 0; p < b->str + b->l_str; ++p)
		if (*p == 0 && p+1 != b->str + b->l_str) tmp[n++] = p + 1;
	if (n != 5) return -1;
	b->ref = tmp[0]; b->alt = tmp[1]; b->flt = tmp[2]; b->info = tmp[3]; b->fmt = tmp[4];
	// set n_alleles
	for (p = b->alt, n = 1; *p; ++p)
		if (*p == ',') ++n;
	b->n_alleles = n;
	c = toupper(*b->ref);
	if (b->alt - b->ref == 2 && (c == 'A' || c == 'C' || c == 'G' || c == 'T'))
		++b->n_alleles;
	// set n_gi and gi[i].fmt
	for (p = b->fmt, n = 1; *p; ++p)
		if (*p == ':') ++n;
	if (n > b->m_gi) {
		int old_m = b->m_gi;
		b->m_gi = n;
		kroundup32(b->m_gi);
		b->gi = realloc(b->gi, b->m_gi * sizeof(bcf_ginfo_t));
		memset(b->gi + old_m, 0, (b->m_gi - old_m) * sizeof(bcf_ginfo_t));
	}
	b->n_gi = n;
	for (p = s = b->fmt, n = 0; *p; ++p) {
		if (*p == ':' || *(p+1) == 0) {
{'fraction_non_alphanumeric': 0.2686298076923077, 'fraction_numerical': 0.01953125, 'mean_word_length': 3.379605263157895, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 221, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2776807', 'n_tokens_mistral': 3721, 'n_tokens_neox': 3600, 'n_words': 769}