Class: HTS::Bcf::Header

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

Overview

A class for working with VCF records. NOTE: This class has a lot of methods that are not stable. The method names and the number of arguments may change in the future.

Constant Summary collapse

BCF_TYPE_MAP =
{
  int: "Integer",
  integer: "Integer",
  int32: "Integer",
  float: "Float",
  real: "Float",
  string: "String",
  str: "String",
  character: "Character",
  char: "Character",
  flag: "Flag"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg = nil) {|_self| ... } ⇒ Header

Returns a new instance of Header.

Yields:

  • (_self)

Yield Parameters:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hts/bcf/header.rb', line 24

def initialize(arg = nil)
  case arg
  when Native::BcfHeaderHandle
    @native = arg
  when nil
    @native = Native::BcfHeaderHandle.create
  else
    raise TypeError, "Invalid argument"
  end

  @sync_depth = 0
  @sync_needed = false
  @schema_version = 0
  @subset_samples = nil
  @subset_imap = nil
  @subset_imap_pointer = nil

  yield self if block_given?
end

Instance Attribute Details

#schema_versionObject (readonly)

Returns the value of attribute schema_version.



81
82
83
# File 'lib/hts/bcf/header.rb', line 81

def schema_version
  @schema_version
end

#subset_imap_pointerObject (readonly)

Returns the value of attribute subset_imap_pointer.



81
82
83
# File 'lib/hts/bcf/header.rb', line 81

def subset_imap_pointer
  @subset_imap_pointer
end

#subset_samplesObject (readonly)

Returns the value of attribute subset_samples.



81
82
83
# File 'lib/hts/bcf/header.rb', line 81

def subset_samples
  @subset_samples
end

Instance Method Details

#add_contig(id, length: nil, **attributes) ⇒ Object



164
165
166
167
168
169
# File 'lib/hts/bcf/header.rb', line 164

def add_contig(id, length: nil, **attributes)
  fields = [["ID", id.to_s]]
  fields << ["length", length.to_s] unless length.nil?
  fields.concat normalize_meta_attributes(attributes)
  append_structured_meta("contig", fields)
end

#add_filter(id, description:, **attributes) ⇒ Object



175
176
177
178
179
# File 'lib/hts/bcf/header.rb', line 175

def add_filter(id, description:, **attributes)
  fields = [["ID", id.to_s], ["Description", description.to_s]]
  fields.concat normalize_meta_attributes(attributes)
  append_structured_meta("FILTER", fields)
end

#add_format(id, number:, type:, description:, **attributes) ⇒ Object



201
202
203
204
205
206
# File 'lib/hts/bcf/header.rb', line 201

def add_format(id, number:, type:, description:, **attributes)
  fields = [["ID", id.to_s], ["Number", normalize_bcf_number(number)], ["Type", normalize_bcf_type(type)],
            ["Description", description.to_s]]
  fields.concat normalize_meta_attributes(attributes)
  append_structured_meta("FORMAT", fields)
end

#add_info(id, number:, type:, description:, **attributes) ⇒ Object



185
186
187
188
189
190
# File 'lib/hts/bcf/header.rb', line 185

def add_info(id, number:, type:, description:, **attributes)
  fields = [["ID", id.to_s], ["Number", normalize_bcf_number(number)], ["Type", normalize_bcf_type(type)],
            ["Description", description.to_s]]
  fields.concat normalize_meta_attributes(attributes)
  append_structured_meta("INFO", fields)
end

#add_meta(key, value = nil, **attributes) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/hts/bcf/header.rb', line 217

def add_meta(key, value = nil, **attributes)
  if attributes.empty?
    append("###{key}=#{value}")
    sync_if_needed!
    self
  else
    append_structured_meta(key.to_s, normalize_meta_attributes(attributes))
  end
end

#add_sample(sample, sync: true) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/hts/bcf/header.rb', line 105

def add_sample(sample, sync: true)
  rc = @native.add_sample(sample)
  raise "Failed to add sample #{sample}" if rc.negative?

  mark_sync_needed!
  sync_if_needed! if sync
  self
end

#append(line) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/hts/bcf/header.rb', line 135

def append(line)
  rc = @native.append(line)
  raise "Failed to append VCF header line" if rc.negative?

  mark_sync_needed!
  self
end

#delete(bcf_hl_type, key = nil) ⇒ Object

FIXME



143
144
145
146
147
148
# File 'lib/hts/bcf/header.rb', line 143

def delete(bcf_hl_type, key = nil) # FIXME
  existed = hrec_exists?(bcf_hl_type, key)
  @native.remove(bcf_hl_type.to_s, key)
  mark_sync_needed! if existed
  existed
end

#editObject



155
156
157
158
159
160
161
162
# File 'lib/hts/bcf/header.rb', line 155

def edit
  @sync_depth += 1
  yield self
  self
ensure
  @sync_depth -= 1
  sync_if_needed!
end

#get_hrec(bcf_hl_type, key, value, str_class = nil) ⇒ Object



150
151
152
153
# File 'lib/hts/bcf/header.rb', line 150

def get_hrec(bcf_hl_type, key, value, str_class = nil)
  hrec = @native.get_hrec(bcf_hl_type.to_s, key, value, str_class)
  hrec ? HeaderRecord.new(hrec) : nil
end

#get_tid(name) ⇒ Object



65
66
67
# File 'lib/hts/bcf/header.rb', line 65

def get_tid(name)
  name2id(name)
end

#get_versionObject



44
45
46
# File 'lib/hts/bcf/header.rb', line 44

def get_version
  @native.version
end

#id2name(id) ⇒ Object



239
240
241
# File 'lib/hts/bcf/header.rb', line 239

def id2name(id)
  @native.id2name(id)
end

#merge(hdr) ⇒ Object



114
115
116
117
118
119
# File 'lib/hts/bcf/header.rb', line 114

def merge(hdr)
  @native.merge(hdr.__send__(:native_handle))
  mark_sync_needed!
  sync_if_needed!
  self
end

#name2id(name) ⇒ Object



235
236
237
# File 'lib/hts/bcf/header.rb', line 235

def name2id(name)
  @native.name2id(name)
end

#nsamplesObject



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

def nsamples
  @native.nsamples
end

#read_bcf(fname) ⇒ Object



129
130
131
132
133
# File 'lib/hts/bcf/header.rb', line 129

def read_bcf(fname)
  result = @native.read_file(fname)
  @schema_version += 1 unless result.negative?
  result
end

#remove_contig(id) ⇒ Object



171
172
173
# File 'lib/hts/bcf/header.rb', line 171

def remove_contig(id)
  delete("CONTIG", id.to_s).tap { sync_if_needed! }
end

#remove_filter(id) ⇒ Object



181
182
183
# File 'lib/hts/bcf/header.rb', line 181

def remove_filter(id)
  delete("FILTER", id.to_s).tap { sync_if_needed! }
end

#remove_format(id) ⇒ Object



213
214
215
# File 'lib/hts/bcf/header.rb', line 213

def remove_format(id)
  delete("FORMAT", id.to_s).tap { sync_if_needed! }
end

#remove_info(id) ⇒ Object



197
198
199
# File 'lib/hts/bcf/header.rb', line 197

def remove_info(id)
  delete("INFO", id.to_s).tap { sync_if_needed! }
end

#samplesObject



77
78
79
# File 'lib/hts/bcf/header.rb', line 77

def samples
  @native.samples
end

#seqnamesObject



227
228
229
# File 'lib/hts/bcf/header.rb', line 227

def seqnames
  @native.seqnames
end

#set_version(version) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/hts/bcf/header.rb', line 48

def set_version(version)
  rc = @native.set_version(version)
  raise "Failed to set VCF header version" if rc.negative?

  mark_sync_needed!
  sync_if_needed!
  self
end

#subset(samples) ⇒ Object

Raises:



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hts/bcf/header.rb', line 91

def subset(samples)
  subset_samples = normalize_subset_samples(samples)
  validate_subset_samples!(subset_samples)

  result = @native.subset(subset_samples)
  raise SubsetError, "Failed to subset BCF header samples #{subset_samples.inspect}" unless result

  subset_header, imap = result
  composed_imap = compose_subset_imap(imap)
  self.class.new(subset_header).tap do |header|
    header.send(:set_subset_state, subset_samples, composed_imap)
  end
end

#subset?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/hts/bcf/header.rb', line 83

def subset?
  !@subset_imap.nil?
end

#subset_sample_countObject



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

def subset_sample_count
  subset? ? @subset_samples.length : 0
end

#syncObject



121
122
123
124
125
126
127
# File 'lib/hts/bcf/header.rb', line 121

def sync
  rc = @native.sync
  raise "Failed to sync BCF header" if rc.negative?

  @sync_needed = false
  self
end

#target_countObject



61
62
63
# File 'lib/hts/bcf/header.rb', line 61

def target_count
  target_names.size
end

#target_name(rid) ⇒ Object



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

def target_name(rid)
  id2name(rid)
end

#target_namesObject



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

def target_names
  seqnames
end

#to_sObject



231
232
233
# File 'lib/hts/bcf/header.rb', line 231

def to_s
  @native.to_s
end

#update_format(id, number:, type:, description:, **attributes) ⇒ Object



208
209
210
211
# File 'lib/hts/bcf/header.rb', line 208

def update_format(id, number:, type:, description:, **attributes)
  delete("FORMAT", id.to_s)
  add_format(id, number:, type:, description:, **attributes)
end

#update_info(id, number:, type:, description:, **attributes) ⇒ Object



192
193
194
195
# File 'lib/hts/bcf/header.rb', line 192

def update_info(id, number:, type:, description:, **attributes)
  delete("INFO", id.to_s)
  add_info(id, number:, type:, description:, **attributes)
end