Class: HTS::Bcf::Header
- Inherits:
-
Object
- Object
- HTS::Bcf::Header
- 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
-
#schema_version ⇒ Object
readonly
Returns the value of attribute schema_version.
-
#subset_imap_pointer ⇒ Object
readonly
Returns the value of attribute subset_imap_pointer.
-
#subset_samples ⇒ Object
readonly
Returns the value of attribute subset_samples.
Instance Method Summary collapse
- #add_contig(id, length: nil, **attributes) ⇒ Object
- #add_filter(id, description:, **attributes) ⇒ Object
- #add_format(id, number:, type:, description:, **attributes) ⇒ Object
- #add_info(id, number:, type:, description:, **attributes) ⇒ Object
- #add_meta(key, value = nil, **attributes) ⇒ Object
- #add_sample(sample, sync: true) ⇒ Object
- #append(line) ⇒ Object
-
#delete(bcf_hl_type, key = nil) ⇒ Object
FIXME.
- #edit ⇒ Object
- #get_hrec(bcf_hl_type, key, value, str_class = nil) ⇒ Object
- #get_tid(name) ⇒ Object
- #get_version ⇒ Object (also: #version)
- #id2name(id) ⇒ Object
-
#initialize(arg = nil) {|_self| ... } ⇒ Header
constructor
A new instance of Header.
- #merge(hdr) ⇒ Object
- #name2id(name) ⇒ Object
- #nsamples ⇒ Object
- #read_bcf(fname) ⇒ Object
- #remove_contig(id) ⇒ Object
- #remove_filter(id) ⇒ Object
- #remove_format(id) ⇒ Object
- #remove_info(id) ⇒ Object
- #samples ⇒ Object
- #seqnames ⇒ Object
- #set_version(version) ⇒ Object (also: #version=)
- #subset(samples) ⇒ Object
- #subset? ⇒ Boolean
- #subset_sample_count ⇒ Object
- #sync ⇒ Object
- #target_count ⇒ Object
- #target_name(rid) ⇒ Object
- #target_names ⇒ Object
- #to_s ⇒ Object
- #update_format(id, number:, type:, description:, **attributes) ⇒ Object
- #update_info(id, number:, type:, description:, **attributes) ⇒ Object
Constructor Details
#initialize(arg = nil) {|_self| ... } ⇒ Header
Returns a new instance of Header.
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_version ⇒ Object (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_pointer ⇒ Object (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_samples ⇒ Object (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 (attributes) ("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 (attributes) ("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 (attributes) ("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 (attributes) ("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 (key, value = nil, **attributes) if attributes.empty? append("###{key}=#{value}") sync_if_needed! self else (key.to_s, (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 |
#edit ⇒ Object
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_version ⇒ Object 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 |
#nsamples ⇒ Object
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 |
#samples ⇒ Object
79 80 81 |
# File 'lib/hts/bcf/header.rb', line 79 def samples @native.samples end |
#seqnames ⇒ Object
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
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
85 86 87 |
# File 'lib/hts/bcf/header.rb', line 85 def subset? !@subset_imap.nil? end |
#subset_sample_count ⇒ Object
89 90 91 |
# File 'lib/hts/bcf/header.rb', line 89 def subset_sample_count subset? ? @subset_samples.length : 0 end |
#sync ⇒ Object
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_count ⇒ Object
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_names ⇒ Object
75 76 77 |
# File 'lib/hts/bcf/header.rb', line 75 def target_names seqnames end |
#to_s ⇒ Object
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 |