Skip to content

Commit 0b5cba9

Browse files
committed
Implement build_index method in Bam class and add corresponding tests
1 parent 59461ce commit 0b5cba9

2 files changed

Lines changed: 47 additions & 14 deletions

File tree

lib/hts/bam.rb

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ def self.open(*args, **kw)
3131
file
3232
end
3333

34+
def self.build_index(file_name, index_name = nil, min_shift = 0, threads = 0, verbose = true)
35+
if verbose
36+
if index_name
37+
warn "Create index for #{file_name} to #{index_name}"
38+
else
39+
warn "Create index for #{file_name}"
40+
end
41+
end
42+
43+
case LibHTS.sam_index_build3(file_name, index_name, min_shift, threads)
44+
when 0 # successful
45+
when -1 then raise "indexing failed"
46+
when -2 then raise "opening #{file_name} failed"
47+
when -3 then raise "format not indexable"
48+
when -4 then raise "failed to create and/or save the index"
49+
else raise "unknown error"
50+
end
51+
end
52+
3453
def initialize(file_name, mode = "r", index: nil, fai: nil, threads: nil,
3554
build_index: false)
3655
if block_given?
@@ -74,22 +93,10 @@ def initialize(file_name, mode = "r", index: nil, fai: nil, threads: nil,
7493
@start_position = tell
7594
end
7695

77-
def build_index(index_name = nil, min_shift: 0, threads: 2)
96+
def build_index(index_name = nil, min_shift: 0, threads: 2, verbose: true)
7897
check_closed
7998

80-
if index_name
81-
warn "Create index for #{@file_name} to #{index_name}"
82-
else
83-
warn "Create index for #{@file_name}"
84-
end
85-
case LibHTS.sam_index_build3(@file_name, index_name, min_shift, @nthreads || threads)
86-
when 0 # successful
87-
when -1 then raise "indexing failed"
88-
when -2 then raise "opening #{@file_name} failed"
89-
when -3 then raise "format not indexable"
90-
when -4 then raise "failed to create and/or save the index"
91-
else raise "unknown error"
92-
end
99+
self.class.build_index(@file_name, index_name, min_shift, @nthreads || threads, verbose)
93100
self # for method chaining
94101
end
95102

test/bam_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
require_relative "test_helper"
4+
require "fileutils"
5+
require "tmpdir"
46

57
class BamTest < Minitest::Test
68
def teardown
@@ -334,6 +336,30 @@ def test_build_index
334336
File.unlink("test_bam_index_file") if File.exist?("test_bam_index_file")
335337
end
336338

339+
def test_class_build_index_with_explicit_index_name
340+
Dir.mktmpdir do |dir|
341+
bam_path = File.join(dir, "copy.bam")
342+
index_path = File.join(dir, "copy.bam.bai")
343+
FileUtils.cp(path_bam_string, bam_path)
344+
345+
HTS::Bam.build_index(bam_path, index_path, 0, 0, false)
346+
347+
assert_equal true, File.exist?(index_path)
348+
end
349+
end
350+
351+
def test_class_build_index_with_default_index_name
352+
Dir.mktmpdir do |dir|
353+
bam_path = File.join(dir, "copy.bam")
354+
default_index_path = "#{bam_path}.bai"
355+
FileUtils.cp(path_bam_string, bam_path)
356+
357+
HTS::Bam.build_index(bam_path, nil, 0, 0, false)
358+
359+
assert_equal true, File.exist?(default_index_path)
360+
end
361+
end
362+
337363
# Tag writing tests
338364
def test_aux_update_int
339365
bam = HTS::Bam.new(path_bam_string)

0 commit comments

Comments
 (0)