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.



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

def schema_version
  @schema_version
end

#subset_imap_pointerObject (readonly)

Returns the value of attribute subset_imap_pointer.



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

def subset_imap_pointer
  @subset_imap_pointer
end

#subset_samplesObject (readonly)

Returns the value of attribute subset_samples.



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

def subset_samples
  @subset_samples
end

Instance Method Details

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



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

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



177
178
179
180
181
# File 'lib/hts/bcf/header.rb', line 177

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



152
153
154
155
# File 'lib/hts/bcf/header.rb', line 152

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



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

def get_tid(name)
  name2id(name)
end

#get_versionObject Also known as: version



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

def get_version
  @native.version
end

#id2name(id) ⇒ Object



241
242
243
# File 'lib/hts/bcf/header.rb', line 241

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

#merge(hdr) ⇒ Object



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

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

#name2id(name) ⇒ Object



237
238
239
# File 'lib/hts/bcf/header.rb', line 237

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

#nsamplesObject



59
60
61
# File 'lib/hts/bcf/header.rb', line 59

def nsamples
  @native.nsamples
end

#read_bcf(fname) ⇒ Object



131
132
133
134
135
# File 'lib/hts/bcf/header.rb', line 131

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

#remove_contig(id) ⇒ Object



173
174
175
# File 'lib/hts/bcf/header.rb', line 173

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

#remove_filter(id) ⇒ Object



183
184
185
# File 'lib/hts/bcf/header.rb', line 183

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

#remove_format(id) ⇒ Object



215
216
217
# File 'lib/hts/bcf/header.rb', line 215

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

#remove_info(id) ⇒ Object



199
200
201
# File 'lib/hts/bcf/header.rb', line 199

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

#samplesObject



79
80
81
# File 'lib/hts/bcf/header.rb', line 79

def samples
  @native.samples
end

#seqnamesObject



229
230
231
# File 'lib/hts/bcf/header.rb', line 229

def seqnames
  @native.seqnames
end

#set_version(version) ⇒ Object Also known as: version=



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

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:



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

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)


85
86
87
# File 'lib/hts/bcf/header.rb', line 85

def subset?
  !@subset_imap.nil?
end

#subset_sample_countObject



89
90
91
# File 'lib/hts/bcf/header.rb', line 89

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

#syncObject



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

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

  @sync_needed = false
  self
end

#target_countObject



63
64
65
# File 'lib/hts/bcf/header.rb', line 63

def target_count
  target_names.size
end

#target_name(rid) ⇒ Object



71
72
73
# File 'lib/hts/bcf/header.rb', line 71

def target_name(rid)
  id2name(rid)
end

#target_namesObject



75
76
77
# File 'lib/hts/bcf/header.rb', line 75

def target_names
  seqnames
end

#to_sObject



233
234
235
# File 'lib/hts/bcf/header.rb', line 233

def to_s
  @native.to_s
end

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



210
211
212
213
# File 'lib/hts/bcf/header.rb', line 210

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



194
195
196
197
# File 'lib/hts/bcf/header.rb', line 194

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