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



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

def [](key)
  get(key)
end

#fieldsObject

FIXME: naming? room for improvement.



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

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.



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
71
# File 'lib/hts/bcf/info.rb', line 37

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.



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

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

#get_float(key) ⇒ Object

For compatibility with htslib.cr.



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

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

#get_int(key) ⇒ Object

For compatibility with htslib.cr.



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

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

#get_string(key) ⇒ Object

For compatibility with htslib.cr.



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

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

#lengthObject



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

def length
  @record.struct[:n_info]
end

#sizeObject



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

def size
  length
end

#to_hObject



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

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