Class: HTS::Bcf::Info

Inherits:
Object
  • Object
show all
Defined in:
lib/hts/bcf/info.rb

Overview

Info field

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Info

Returns a new instance of Info.



7
8
9
10
# File 'lib/hts/bcf/info.rb', line 7

def initialize(record)
  @record = record
  @p1 = FFI::MemoryPointer.new(:pointer) # FIXME: naming
end

Instance Method Details

#[](key) ⇒ Object



73
74
75
# File 'lib/hts/bcf/info.rb', line 73

def [](key)
  get(key)
end

#fieldsObject

FIXME: naming? room for improvement.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hts/bcf/info.rb', line 78

def fields
  keys.map do |key|
    name = LibHTS.bcf_hdr_int2id(@record.header.struct, LibHTS::BCF_DT_ID, key)
    num  = LibHTS.bcf_hdr_id2number(@record.header.struct, LibHTS::BCF_HL_INFO, key)
    type = LibHTS.bcf_hdr_id2type(@record.header.struct, LibHTS::BCF_HL_INFO, key)
    {
      name:,
      n: num,
      type: ht_type_to_sym(type),
      key:
    }
  end
end

#get(key, type = nil) ⇒ Object

Note:

Specify the type. If you don’t specify a type, it will still work, but it will be slower.

@note: Why is this method named “get” instead of “fetch”? This is for compatibility with the Crystal language which provides methods like ‘get_int`, `get_float`, etc. I think they are better than `fetch_int“ and `fetch_float`.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hts/bcf/info.rb', line 17

def get(key, type = nil)
  n = FFI::MemoryPointer.new(:int)
  p1 = @p1
  h = @record.header.struct
  r = @record.struct

  info_values = proc do |typ|
    ret = LibHTS.bcf_get_info_values(h, r, key, p1, n, typ)
    return nil if ret < 0 # return from method.

    p1.read_pointer
  end

  type ||= ht_type_to_sym(get_info_type(key))

  case type&.to_sym
  when :int, :int32
    info_values.call(LibHTS::BCF_HT_INT)
               .read_array_of_int32(n.read_int)
  when :float, :real
    info_values.call(LibHTS::BCF_HT_REAL)
               .read_array_of_float(n.read_int)
  when :flag, :bool
    case ret = LibHTS.bcf_get_info_flag(h, r, key, p1, n)
    when 1 then true
    when 0 then false
    when -1 then nil
    else
      raise "Unknown return value from bcf_get_info_flag: #{ret}"
    end
  when :string, :str
    info_values.call(LibHTS::BCF_HT_STR)
               .read_string
  end
end

#get_flag(key) ⇒ Object

For compatibility with HTS.cr.



69
70
71
# File 'lib/hts/bcf/info.rb', line 69

def get_flag(key)
  get(key, :flag)
end

#get_float(key) ⇒ Object

For compatibility with HTS.cr.



59
60
61
# File 'lib/hts/bcf/info.rb', line 59

def get_float(key)
  get(key, :float)
end

#get_int(key) ⇒ Object

For compatibility with HTS.cr.



54
55
56
# File 'lib/hts/bcf/info.rb', line 54

def get_int(key)
  get(key, :int)
end

#get_string(key) ⇒ Object

For compatibility with HTS.cr.



64
65
66
# File 'lib/hts/bcf/info.rb', line 64

def get_string(key)
  get(key, :string)
end

#lengthObject



92
93
94
# File 'lib/hts/bcf/info.rb', line 92

def length
  @record.struct[:n_info]
end

#sizeObject



96
97
98
# File 'lib/hts/bcf/info.rb', line 96

def size
  length
end

#to_hObject



100
101
102
103
104
105
106
107
# File 'lib/hts/bcf/info.rb', line 100

def to_h
  ret = {}
  keys.each do |key|
    name = LibHTS.bcf_hdr_int2id(@record.header.struct, LibHTS::BCF_DT_ID, key)
    ret[name] = get(name)
  end
  ret
end