I think one way to handle such error is to check whether a BAM file contains the region before eachoverlap()
is called.
Does anybody know how to do this?
julia> using XAM
julia> using GenomicFeatures
julia> function print_overlapped_record(bam_file, chrom, leftpos, rightpos)
open(BAM.Reader, bam_file, index = bam_file * ".bai") do reader
for record in eachoverlap(reader, chrom, leftpos:rightpos)
print(record)
end
end
end
print_overlapped_record (generic function with 1 method)
julia> testfun(bam_file, "chr1", 3205900, 3616344)
XAM.BAM.Record:
template name: NS500723:19:H5LLWBGXX:2:13108:16341:13952
flag: 16
reference ID: 1
position: 3279555
mapping quality: 255
CIGAR: 75M
next reference ID: 0
next position: 0
template length: 0
sequence: ACCAAAGGTCCACCTTGCCTTCACCTTATTTGTCCTAAATTTCTTGCACACCGATATCAAAGGCCAGTCTGCAGC
base quality: UInt8[0x24, 0x24, 0x0e, 0x20, 0x24, 0x20, 0x24, 0x0e, 0x0e, 0x0e, 0x20, 0x24, 0x0e, 0x0e, 0x0e, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x0e, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x0e, 0x24, 0x0e, 0x0e, 0x24, 0x24, 0x20, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x20, 0x24, 0x24, 0x24, 0x24, 0x0e, 0x24, 0x24, 0x20, 0x15, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x20, 0x24, 0x24, 0x15, 0x24, 0x24, 0x20, 0x24, 0x24, 0x20, 0x20, 0x20, 0x20, 0x15]
auxiliary data: AS=-3 XN=0 XM=1 XO=0 XG=0 NM=1 MD=8C66 YT=UU NH=1XAM.BAM.Record:
template name: NS500723:19:H5LLWBGXX:2:12211:9394:16712
flag: 16
reference ID: 1
position: 3532296
mapping quality: 255
CIGAR: 52M
next reference ID: 0
next position: 0
template length: 0
sequence: TTTATCAAGTGTGGGTCGGAGCCAAGCAGCACGAACTGCAGGTGAACTCCAT
base quality: UInt8[0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x20, 0x20, 0x20, 0x20, 0x20]
auxiliary data: AS=-5 XN=0 XM=1 XO=0 XG=0 NM=1 MD=4A47 YT=UU NH=1XAM.BAM.Record:
template name: NS500723:19:H5LLWBGXX:3:13405:17228:11793
flag: 0
reference ID: 1
position: 3532529
mapping quality: 255
CIGAR: 75M
next reference ID: 0
next position: 0
template length: 0
sequence: GGGAACACGTCACCAGTCACCTACAACATGTTCGGACACTTCAAGTTCTGCATCACTCTCTGCGGAGGATACATT
base quality: UInt8[0x20, 0x20, 0x20, 0x20, 0x20, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24]
auxiliary data: AS=-5 XN=0 XM=1 XO=0 XG=0 NM=1 MD=7T67 YT=UU NH=1XAM.BAM.Record:
template name: NS500723:19:H5LLWBGXX:4:11504:6970:1796
flag: 16
reference ID: 1
position: 3532535
mapping quality: 255
CIGAR: 75M
next reference ID: 0
next position: 0
template length: 0
sequence: ACGTCACCAGTCACCTACAACATGTTCGGACACTTCAAGTTCTGCATCACTCTCTGCGGAGGATACATTCTATTT
base quality: UInt8[0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x20, 0x20, 0x20, 0x20, 0x20]
auxiliary data: AS=-5 ZS=-10 XN=0 XM=1 XO=0 XG=0 NM=1 MD=1T73 YT=UU NH=1XAM.BAM.Record:
template name: NS500723:19:H5LLWBGXX:1:13307:15032:13241
flag: 16
reference ID: 1
position: 3532548
mapping quality: 255
CIGAR: 39M
next reference ID: 0
next position: 0
template length: 0
sequence: CCTACAACATGTTCGGACACTTCAAGTTCTGCATCACTC
base quality: UInt8[0x0e, 0x24, 0x20, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x0e, 0x24, 0x24, 0x0e, 0x15, 0x24, 0x15, 0x24, 0x24, 0x20, 0x20, 0x20, 0x0e, 0x24, 0x20, 0x24, 0x24, 0x20, 0x24, 0x24, 0x24, 0x20, 0x20, 0x20, 0x20, 0x20]
auxiliary data: AS=0 ZS=-5 XN=0 XM=0 XO=0 XG=0 NM=0 MD=39 YT=UU NH=1
julia> testfun(bam_file, "chrZ", 3205900, 3516344)
ERROR: MethodError: no method matching overlapchunks(::Indexes.BGZFIndex, ::Nothing, ::UnitRange{Int64})
Closest candidates are:
overlapchunks(::Indexes.BGZFIndex, ::Integer, ::UnitRange) at /Users/ozakiharuka/.julia/packages/Indexes/fj340/src/bgzfindex.jl:41
Stacktrace:
[1] iterate(::XAM.BAM.OverlapIterator{IOStream}) at /Users/ozakiharuka/.julia/packages/XAM/ahh4D/src/bam/overlap.jl:54
[2] (::var"#9#10"{String,Int64,Int64})(::XAM.BAM.Reader{IOStream}) at ./REPL[25]:4
[3] #open#2(::Base.Iterators.Pairs{Symbol,String,Tuple{Symbol},NamedTuple{(:index,),Tuple{String}}}, ::typeof(open), ::var"#9#10"{String,Int64,Int64}, ::Type{XAM.BAM.Reader}, ::String) at /Users/ozakiharuka/.julia/packages/BioGenerics/cCuGr/src/IO.jl:48
[4] #open at ./none:0 [inlined]
[5] testfun(::String, ::String, ::Int64, ::Int64) at ./REPL[25]:3
[6] top-level scope at REPL[41]:1