From 93a6b60d26e2b0c70ea8755d5b0913c1df7bb88d Mon Sep 17 00:00:00 2001 From: Jonathan Bieler Date: Thu, 13 Oct 2022 15:06:39 +0200 Subject: [PATCH] added method for handling different bam index types --- src/bam/reader.jl | 12 ++++++------ test/test_bam.jl | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/bam/reader.jl b/src/bam/reader.jl index 43e228e..d1e71ec 100644 --- a/src/bam/reader.jl +++ b/src/bam/reader.jl @@ -28,13 +28,8 @@ function BioGenerics.IO.stream(reader::Reader) end function Reader(input::IO; index=nothing) - if isa(index, AbstractString) - index = BAI(index) - elseif index != nothing - error("unrecognizable index argument") - end reader = init_bam_reader(input) - reader.index = index + reader.index = init_bam_index(index) return reader end @@ -125,6 +120,11 @@ function init_bam_reader(input::IO) return init_bam_reader(BGZFStreams.BGZFStream(input)) end +init_bam_index(index::AbstractString) = BAI(index) +init_bam_index(index::BAI) = index +init_bam_index(index::Nothing) = nothing +init_bam_index(index) = error("unrecognizable index argument") + function _read!(reader::Reader, record) unsafe_read( reader.stream, diff --git a/test/test_bam.jl b/test/test_bam.jl index beb6d03..0a504b1 100644 --- a/test/test_bam.jl +++ b/test/test_bam.jl @@ -241,6 +241,21 @@ end + @testset "BAI" begin + + filepath = joinpath(bamdir, "GSE25840_GSM424320_GM06985_gencode_spliced.head.bam") + + index = BAM.BAI(filepath * ".bai") + reader = open(BAM.Reader, filepath, index=index) + + @test isa(eachoverlap(reader, "chr1", 1:100), BAM.OverlapIterator) + + close(reader) + + @test_throws ErrorException open(BAM.Reader, filepath, index=1234) + + end + @testset "Random access" begin filepath = joinpath(bamdir, "GSE25840_GSM424320_GM06985_gencode_spliced.head.bam") reader = open(BAM.Reader, filepath, index=filepath * ".bai")