Skip to content

Commit

Permalink
Add methods to BCF::Header
Browse files Browse the repository at this point in the history
  • Loading branch information
kojix2 committed Jul 19, 2023
1 parent e3bd05a commit bb3c21e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/hts/bcf/header.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ module HTS
String.new LibHTS.bcf_hdr_get_version(@bcf_hdr)
end

def set_version(version)
LibHTS.bcf_hdr_set_version(@bcf_hdr, version)
end

def nsamples
LibHTS2.bcf_hdr_nsamples(@bcf_hdr)
end
Expand All @@ -33,6 +37,32 @@ module HTS
end
end

def add_sample(sample, sync : Bool = true)
LibHTS.bcf_hdr_add_sample(@bcf_hdr, sample)
self.sync if sync
end

def merge(hdr)
LibHTS.bcf_hdr_merge(@bcf_hdr, hdr)
end

def sync
LibHTS.bcf_hdr_sync(@bcf_hdr)
end

def read_bcf(fname)
LibHTS.bcf_hdr_set(@bcf_hdr, fname)
end

def append(line)
LibHTS.bcf_hdr_append(@bcf_hdr, line)
end

def delete(bcf_hl_type, key)
type = bcf_hl_type_to_int(bcf_hl_type)
LibHTS.bcf_hdr_remove(@bcf_hdr, type, key)
end

def to_s(io : IO)
kstr = LibHTS::KstringT.new
unless LibHTS.bcf_hdr_format(@bcf_hdr, 0, pointerof(kstr))
Expand All @@ -48,6 +78,26 @@ module HTS
def finalize
LibHTS.bcf_hdr_destroy(@bcf_hdr) unless @bcf_hdr.null?
end

private def bcf_hl_type_to_int(bcf_hl_type)
return bcf_hl_type if bcf_hl_type.is_a?(Int32)
case bcf_hl_type.to_s.upcase
when "FILTER", "FIL"
LibHTS2::BCF_HL_FLT
when "INFO"
LibHTS2::BCF_HL_INFO
when "FORMAT", "FMT"
LibHTS2::BCF_HL_FMT
when "CONTIG", "CTG"
LibHTS2::BCF_HL_CTG
when "STRUCTURED", "STR"
LibHTS2::BCF_HL_STR
when "GENOTYPE", "GEN"
LibHTS2::BCF_HL_GEN
else
raise "Unknown bcf_hl_type: #{bcf_hl_type}"
end
end
end
end
end
24 changes: 24 additions & 0 deletions test/hts/bcf/header_test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class BcfHeaderTest < Minitest::Test
assert_equal "VCFv4.2", hdr.get_version
end

def test_set_version
hdr2 = hdr.clone
hdr2.set_version("VCFv9.9")
assert_equal "VCFv9.9", hdr2.get_version
end

def test_nsamples
assert_equal 1, hdr.nsamples
end
Expand All @@ -36,6 +42,24 @@ class BcfHeaderTest < Minitest::Test
assert_equal ["poo.sort.bam"], hdr.samples
end

def test_sync
hdr2 = hdr.clone
hdr2.add_sample("kojix1", sync: false)
hdr2.add_sample("kojix2", sync: false)
hdr2.add_sample("kojix3", sync: false)
assert_equal 1, hdr2.nsamples
assert_equal ["poo.sort.bam"], hdr2.samples
hdr2.sync
assert_equal 4, hdr2.nsamples
assert_equal ["poo.sort.bam", "kojix1", "kojix2", "kojix3"], hdr2.samples
end

def test_append_delete
h = HTS::Bcf::Header.new
h.append("##FILTER=<ID=Nessie,Description=\"Nessie is a creature in Scottish folklore that is said to inhabit Loch Ness in the Scottish Highlands.\">")
h.delete("FILTER", "Nessie")
end

def test_to_s
# md5 = Digest::MD5.hexdigest(bcf.header.to_s)
# exp = "ca7d2c7ac2a51e4f2b2b88004615e98b"
Expand Down

0 comments on commit bb3c21e

Please sign in to comment.