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



76
77
78
# File 'lib/hts/bcf/format.rb', line 76

def [](key)
  get(key)
end

#fieldsObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hts/bcf/format.rb', line 80

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

@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`.



15
16
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
52
53
54
# File 'lib/hts/bcf/format.rb', line 15

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.



67
68
69
# File 'lib/hts/bcf/format.rb', line 67

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

#get_float(key) ⇒ Object

For compatibility with HTS.cr.



62
63
64
# File 'lib/hts/bcf/format.rb', line 62

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

#get_int(key) ⇒ Object

For compatibility with HTS.cr.



57
58
59
# File 'lib/hts/bcf/format.rb', line 57

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

#get_string(key) ⇒ Object

For compatibility with HTS.cr.



72
73
74
# File 'lib/hts/bcf/format.rb', line 72

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

#lengthObject



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

def length
  @record.struct[:n_fmt]
end

#sizeObject



98
99
100
# File 'lib/hts/bcf/format.rb', line 98

def size
  length
end

#to_hObject



102
103
104
105
106
107
108
109
# File 'lib/hts/bcf/format.rb', line 102

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