Skip to content

Commit 13c5ebe

Browse files
committed
Add support for querying multiple regions in Bcf class
1 parent 775f66f commit 13c5ebe

2 files changed

Lines changed: 55 additions & 6 deletions

File tree

lib/hts/bcf.rb

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,20 @@ def query(region, beg = nil, end_ = nil, copy: false, &block)
215215
raise "query is only available for BCF files" unless file_format == "bcf"
216216
raise "Index file is required to call the query method." unless index_loaded?
217217

218-
if beg && end_
219-
tid = header.name2id(region)
220-
queryi(tid, beg, end_, copy:, &block)
221-
elsif beg.nil? && end_.nil?
222-
querys(region, copy:, &block)
218+
case region
219+
when Array
220+
raise ArgumentError, "beg and end must not be specified when region is an Array" unless beg.nil? && end_.nil?
221+
222+
query_regions(region, copy:, &block)
223223
else
224-
raise ArgumentError, "beg and end must be specified together"
224+
if beg && end_
225+
tid = header.name2id(region)
226+
queryi(tid, beg, end_, copy:, &block)
227+
elsif beg.nil? && end_.nil?
228+
querys(region, copy:, &block)
229+
else
230+
raise ArgumentError, "beg and end must be specified together"
231+
end
225232
end
226233
end
227234

@@ -243,6 +250,14 @@ def querys(region, copy: false, &block)
243250
end
244251
end
245252

253+
def query_regions(regions, copy: false, &block)
254+
if copy
255+
query_regions_copy(regions, &block)
256+
else
257+
query_regions_reuse(regions, &block)
258+
end
259+
end
260+
246261
def queryi_reuse(tid, beg, end_, &block)
247262
return to_enum(__method__, tid, beg, end_) unless block_given?
248263

@@ -263,6 +278,15 @@ def querys_reuse(region, &block)
263278
self
264279
end
265280

281+
def query_regions_reuse(regions, &block)
282+
return to_enum(__method__, regions) unless block_given?
283+
284+
regions.each do |region|
285+
querys_reuse(region, &block)
286+
end
287+
self
288+
end
289+
266290
def query_reuse_yield(qiter)
267291
bcf1 = LibHTS.bcf_init
268292
record = Record.new(header, bcf1)
@@ -299,6 +323,15 @@ def querys_copy(region, &block)
299323
self
300324
end
301325

326+
def query_regions_copy(regions, &block)
327+
return to_enum(__method__, regions) unless block_given?
328+
329+
regions.each do |region|
330+
querys_copy(region, &block)
331+
end
332+
self
333+
end
334+
302335
def query_copy_yield(qiter)
303336
loop do
304337
bcf1 = LibHTS.bcf_init

test/bcf_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,22 @@ def test_query
170170
assert_equal [4021, 4310, 4337], r
171171
end
172172

173+
def test_query_multi_regions
174+
bcf = HTS::Bcf.open(Fixtures["test.bcf"])
175+
176+
r = bcf.query(["poo:4000-4100", "poo:4300-4400"]).map { |aln| aln.pos + 1 }
177+
assert_equal [4021, 4310, 4337], r
178+
179+
r = bcf.query(["poo:4000-4100", "poo:4300-4400"], copy: true).map { |aln| aln.pos + 1 }
180+
assert_equal [4021, 4310, 4337], r
181+
182+
r = bcf.query(["poo:4000-4500"]).map { |aln| aln.pos + 1 }
183+
assert_equal [4021, 4310, 4337], r
184+
185+
r = bcf.query(["poo:4000-4500"], copy: true).map { |aln| aln.pos + 1 }
186+
assert_equal [4021, 4310, 4337], r
187+
end
188+
173189
def test_build_index
174190
bcf = HTS::Bcf.open(Fixtures["test.bcf"])
175191
bcf.build_index("test_bcf_index_file")

0 commit comments

Comments
 (0)