<commit_before>#include <string.h>
#include <unistd.h>
#include "htslib/faidx.h"
#include "sam.h"

int samthreads(samfile_t *fp, int n_threads, int n_sub_blks)
{
	if (!fp->file->is_bin || !fp->file->is_write) return -1;
	bgzf_mt(fp->x.bam, n_threads, n_sub_blks);
	return 0;
}

samfile_t *samopen(const char *fn, const char *mode, const void *aux)
{
	// hts_open() is really sam_open(), except for #define games
	samFile *hts_fp = hts_open(fn, mode, aux);
	if (hts_fp == NULL)  return NULL;

	samfile_t *fp = malloc(sizeof (samfile_t));
	fp->file = hts_fp;
	fp->x.bam = hts_fp->fp.bgzf;
	if (strchr(mode, 'r')) {
		fp->header = sam_hdr_read(fp->file);  // samclose() will free this
		if (fp->header->n_targets == 0) { // no @SQ fields
			if (aux) { // check if aux is present
				bam_header_t *textheader = fp->header;
				fp->header = sam_header_read2((const char*)aux);
				// FIXME should merge in any non-@SQ headers from textheader
				bam_header_destroy(textheader);
			}
			if (fp->header->n_targets == 0 && bam_verbose >= 1)
				fprintf(stderr, "[samopen] no @SQ lines in the header.\n");
		}
	}
	else {
		fp->header = (bam_hdr_t *)aux;  // For writing, we won't free it
		if (fp->file->is_bin || strchr(mode, 'h')) sam_hdr_write(fp->file, fp->header);
	}

	return fp;
}

void samclose(samfile_t *fp)
{
	if (fp) {
		if (!fp->file->is_write && fp->header) bam_hdr_destroy(fp->header);
		sam_close(fp->file);
		free(fp);
	}
}

#if 0
int sampileup(samfile_t *fp, int mask, bam_pileup_f func, void *func_data)
{
	bam_plbuf_t *buf;
	int ret;
	bam1_t *b;
	b = bam_init1();
	buf = bam_plbuf_init(func, func_data);
	bam_plbuf_set_mask(buf, mask);
	while ((ret = samread(fp, b)) >= 0)
		bam_plbuf_push(b, buf);
	bam_plbuf_push(0, buf);
	bam_plbuf_destroy(buf);
	bam_destroy1(b);
	return 0;
}
#endif

char *samfaipath(const char *fn_ref)
{
	char *fn_list = 0;
	if (fn_ref == 0) return 0;
	fn_list = calloc(strlen(fn_ref) + 5, 1);
	strcat(strcpy(fn_list, fn_ref), ".fai");
	if (access(fn_list, R_OK) == -1) { // fn_list is unreadable
		if (access(fn_ref, R_OK) == -1) {
			fprintf(stderr, "[samfaipath] fail to read file %s.\n", fn_ref);
		} else {
			if (bam_verbose >= 3) fprintf(stderr, "[samfaipath] build FASTA index...\n");
			if (fai_build(fn_ref) == -1) {
				fprintf(stderr, "[samfaipath] fail to build FASTA index.\n");
				free(fn_list); fn_list = 0;
			}
		}
	}
	return fn_list;
}
<commit_msg>Fix old API samopen() wrapper fn_aux bugs<commit_after>#include <string.h>
#include <unistd.h>
#include "htslib/faidx.h"
#include "sam.h"

int samthreads(samfile_t *fp, int n_threads, int n_sub_blks)
{
	if (!fp->file->is_bin || !fp->file->is_write) return -1;
	bgzf_mt(fp->x.bam, n_threads, n_sub_blks);
	return 0;
}

samfile_t *samopen(const char *fn, const char *mode, const void *aux)
{
	// hts_open() is really sam_open(), except for #define games
	samFile *hts_fp = hts_open(fn, mode, strchr(mode, 'r')? aux : NULL);
	if (hts_fp == NULL)  return NULL;

	samfile_t *fp = malloc(sizeof (samfile_t));
	fp->file = hts_fp;
	fp->x.bam = hts_fp->fp.bgzf;
	if (strchr(mode, 'r')) {
		fp->header = sam_hdr_read(fp->file);  // samclose() will free this
		if (fp->header->n_targets == 0) { // no @SQ fields
			if (aux) { // check if aux is present
				bam_header_t *textheader = fp->header;
				fp->header = sam_header_read2((const char*)aux);
				// FIXME should merge in any non-@SQ headers from textheader
				bam_header_destroy(textheader);
			}
			if (fp->header->n_targets == 0 && bam_verbose >= 1)
				fprintf(stderr, "[samopen] no @SQ lines in the header.\n");
		}
	}
	else {
		fp->header = (bam_hdr_t *)aux;  // For writing, we won't free it
		if (fp->file->is_bin || fp->file->is_cram || strchr(mode, 'h')) sam_hdr_write(fp->file, fp->header);
	}

	return fp;
}

void samclose(samfile_t *fp)
{
	if (fp) {
		if (!fp->file->is_write && fp->header) bam_hdr_destroy(fp->header);
		sam_close(fp->file);
		free(fp);
	}
}

#if 0
int sampileup(samfile_t *fp, int mask, bam_pileup_f func, void *func_data)
{
	bam_plbuf_t *buf;
	int ret;
	bam1_t *b;
	b = bam_init1();
	buf = bam_plbuf_init(func, func_data);
	bam_plbuf_set_mask(buf, mask);
	while ((ret = samread(fp, b)) >= 0)
		bam_plbuf_push(b, buf);
	bam_plbuf_push(0, buf);
	bam_plbuf_destroy(buf);
	bam_destroy1(b);
	return 0;
}
#endif

char *samfaipath(const char *fn_ref)
{
	char *fn_list = 0;
	if (fn_ref == 0) return 0;
	fn_list = calloc(strlen(fn_ref) + 5, 1);
	strcat(strcpy(fn_list, fn_ref), ".fai");
	if (access(fn_list, R_OK) == -1) { // fn_list is unreadable
		if (access(fn_ref, R_OK) == -1) {
			fprintf(stderr, "[samfaipath] fail to read file %s.\n", fn_ref);
		} else {
			if (bam_verbose >= 3) fprintf(stderr, "[samfaipath] build FASTA index...\n");
			if (fai_build(fn_ref) == -1) {
				fprintf(stderr, "[samfaipath] fail to build FASTA index.\n");
				free(fn_list); fn_list = 0;
			}
		}
	}
	return fn_list;
}

{'fraction_non_alphanumeric': 0.17360114777618366, 'fraction_numerical': 0.00942816150850584, 'mean_word_length': 3.944275582573455, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 67, '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': '6209186', 'n_tokens_mistral': 2245, 'n_tokens_neox': 2102, 'n_words': 492}