Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass reference to htsfile for better cram support #278

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

cdef = [
"typedef struct { ...; } bam_fset;"
"bam_fset* create_bam_fset(char* fname);"
"bam_fset* create_bam_fset(char* fname, char* fasta_path);"
"void destroy_bam_fset(bam_fset* fset);"
]
for header in ('clair3_pileup.h', 'clair3_full_alignment.h'):
Expand Down
8 changes: 4 additions & 4 deletions preprocess/CreateTensorPileupFromCffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ def pileup_counts_clair3(
"""
lib = libclair3.lib
featlenclair3 = lib.featlenclair3
bam = BAMHandler(bam)
bam = BAMHandler(bam, fasta)

def _process_region(reg):
# ctg start is 1-based, medaka.common.Region object is 0-based
region_str = '{}:{}-{}'.format(reg.ref_name, max(0, reg.start-1), reg.end)
if isinstance(bam, BAMHandler):
bam_handle = bam
else:
bam_handle = BAMHandler(bam)
bam_handle = BAMHandler(bam, fasta)
with bam_handle.borrow() as fh:
counts = lib.calculate_clair3_pileup(
region_str.encode(), fh, fasta.encode(), min_depth, min_snp_af, min_indel_af, min_mq, max_indel_length, call_snp_only, max_depth, gvcf)
Expand Down Expand Up @@ -88,7 +88,7 @@ def _process_region(reg):
class BAMHandler(object):
"""Opening of BAM file handles and indices."""

def __init__(self, bam, size=16):
def __init__(self, bam, fasta, size=16):
"""Initialise a pool of HTSlib filehandles."""
# note: the default size here is set to match the default
# `bam_workers` of prediction.DataLoader and `workers`
Expand All @@ -100,7 +100,7 @@ def __init__(self, bam, size=16):
lib, ffi = libclair3.lib, libclair3.ffi
for _ in range(size):
fset = ffi.gc(
lib.create_bam_fset(self.bam.encode()),
lib.create_bam_fset(self.bam.encode(), fasta.encode()),
self._destroy_fset)
self._pool.put(fset)

Expand Down
1 change: 1 addition & 0 deletions src/clair3_full_alignment.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ size_t min_mq, size_t min_bq, size_t matrix_depth, size_t max_indel_length)
bam_hdr_t *header;

hts_file = sam_open(bam_path, "r");
hts_set_opt(hts_file, CRAM_OPT_REFERENCE, fasta_path);
idx = sam_index_load(hts_file, bam_path);
header = sam_hdr_read(hts_file);
const int tid = bam_name2id(header, chr);
Expand Down
3 changes: 2 additions & 1 deletion src/medaka_bamiter.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ int read_bam(void *data, bam1_t *b) {


// Initialise BAM file, index and header structures
bam_fset* create_bam_fset(const char* fname) {
bam_fset* create_bam_fset(const char* fname, const char* fasta_path) {
bam_fset* fset = xalloc(1, sizeof(bam_fset), "bam fileset");
fset->fp = hts_open(fname, "rb");
hts_set_opt(fset->fp, CRAM_OPT_REFERENCE, fasta_path);
fset->idx = sam_index_load(fset->fp, fname);
fset->hdr = sam_hdr_read(fset->fp);
if (fset->hdr == 0 || fset->idx == 0 || fset->fp == 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/medaka_bamiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ typedef struct {


// Initialise BAM file, index and header structures
bam_fset* create_bam_fset(const char* fname);
bam_fset* create_bam_fset(const char* fname, const char* fasta_path);

// Destory BAM file, index and header structures
void destroy_bam_fset(bam_fset* fset);
Expand Down