Class: HTS::Bcf::Info

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

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Info

Returns a new instance of Info.



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

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

Instance Method Details

#[](key) ⇒ Object



31
32
33
# File 'lib/hts/bcf/info.rb', line 31

def [](key)
  get(key)
end

#fieldsObject

FIXME: naming? room for improvement.



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hts/bcf/info.rb', line 73

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: name,
      n: num,
      type: ht_type_to_sym(type),
      key: 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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hts/bcf/info.rb', line 36

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 htslib.cr.



27
28
29
# File 'lib/hts/bcf/info.rb', line 27

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

#get_float(key) ⇒ Object

For compatibility with htslib.cr.



17
18
19
# File 'lib/hts/bcf/info.rb', line 17

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

#get_int(key) ⇒ Object

For compatibility with htslib.cr.



12
13
14
# File 'lib/hts/bcf/info.rb', line 12

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

#get_string(key) ⇒ Object

For compatibility with htslib.cr.



22
23
24
# File 'lib/hts/bcf/info.rb', line 22

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

#lengthObject



87
88
89
# File 'lib/hts/bcf/info.rb', line 87

def length
  @record.struct[:n_info]
end

#sizeObject



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

def size
  length
end

#to_hObject



95
96
97
98
99
100
101
102
# File 'lib/hts/bcf/info.rb', line 95

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