1
0
Fork 0
mirror of https://github.com/MillironX/XAM.jl.git synced 2024-11-14 22:33:14 +00:00

Merge pull request #56 from jonathanBieler/bam_index_method

Added method for handling different bam index types.
This commit is contained in:
Ciarán O'Mara 2022-10-14 22:15:31 +11:00 committed by GitHub
commit 8272cea71e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 7 deletions

View file

@ -8,7 +8,7 @@ Create a data reader of the BAM file format.
# Arguments # Arguments
* `input`: data source * `input`: data source
* `index=nothing`: filepath to a random access index (currently *bai* is supported) * `index=nothing`: filepath to a random access index (currently *bai* is supported) or BAI object
""" """
mutable struct Reader{T} <: BioGenerics.IO.AbstractReader mutable struct Reader{T} <: BioGenerics.IO.AbstractReader
stream::BGZFStreams.BGZFStream{T} stream::BGZFStreams.BGZFStream{T}
@ -28,13 +28,8 @@ function BioGenerics.IO.stream(reader::Reader)
end end
function Reader(input::IO; index=nothing) 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 = init_bam_reader(input)
reader.index = index reader.index = init_bam_index(index)
return reader return reader
end end
@ -125,6 +120,11 @@ function init_bam_reader(input::IO)
return init_bam_reader(BGZFStreams.BGZFStream(input)) return init_bam_reader(BGZFStreams.BGZFStream(input))
end 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) function _read!(reader::Reader, record)
unsafe_read( unsafe_read(
reader.stream, reader.stream,

View file

@ -241,6 +241,21 @@
end 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 @testset "Random access" begin
filepath = joinpath(bamdir, "GSE25840_GSM424320_GM06985_gencode_spliced.head.bam") filepath = joinpath(bamdir, "GSE25840_GSM424320_GM06985_gencode_spliced.head.bam")
reader = open(BAM.Reader, filepath, index=filepath * ".bai") reader = open(BAM.Reader, filepath, index=filepath * ".bai")