Class: HTS::Bcf::Format

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

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Format

Returns a new instance of Format.



6
7
8
9
# File 'lib/hts/bcf/format.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/format.rb', line 31

def [](key)
  get(key)
end

#fieldsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hts/bcf/format.rb', line 76

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

#get(key, type = nil) ⇒ Object



35
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
71
72
73
74
# File 'lib/hts/bcf/format.rb', line 35

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

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

    p1.read_pointer
  end

  # The GT FORMAT field is special in that it is marked as a string in the header,
  # but it is actually encoded as an integer.
  if key == "GT"
    type = :int
  elsif type.nil?
    type = ht_type_to_sym(get_fmt_type(key))
  end

  case type&.to_sym
  when :int, :int32
    format_values.call(LibHTS::BCF_HT_INT)
                 .read_array_of_int32(n.read_int)
  when :float, :real
    format_values.call(LibHTS::BCF_HT_REAL)
                 .read_array_of_float(n.read_int)
  when :flag
    raise NotImplementedError, "Flag type not implemented yet. " \
    "Please file an issue on GitHub."
    # format_values.call(LibHTS::BCF_HT_FLAG)
    #              .read_int == 1
  when :string, :str
    raise NotImplementedError, "String type not implemented yet. " \
    "Please file an issue on GitHub."
    # format_values.call(LibHTS::BCF_HT_STR)
    #              .read_string
  end
end

#get_flag(key) ⇒ Object

For compatibility with HTS.cr.



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

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

#get_float(key) ⇒ Object

For compatibility with HTS.cr.



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

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

#get_int(key) ⇒ Object

For compatibility with HTS.cr.



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

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

#get_string(key) ⇒ Object

For compatibility with HTS.cr.



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

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

#lengthObject



90
91
92
# File 'lib/hts/bcf/format.rb', line 90

def length
  @record.struct[:n_fmt]
end

#sizeObject



94
95
96
# File 'lib/hts/bcf/format.rb', line 94

def size
  length
end

#to_hObject



98
99
100
101
102
103
104
105
# File 'lib/hts/bcf/format.rb', line 98

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