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
-
#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
- #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
- #struct ⇒ Object
- #subset(samples) ⇒ Object
- #subset? ⇒ Boolean
- #subset_sample_count ⇒ Object
- #sync ⇒ Object
- #target_count ⇒ Object
- #target_name(rid) ⇒ Object
- #target_names ⇒ Object
- #to_ptr ⇒ 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 43 |
# File 'lib/hts/bcf/header.rb', line 24 def initialize(arg = nil) case arg when LibHTS::HtsFile @bcf_hdr = LibHTS.bcf_hdr_read(arg) when LibHTS::BcfHdr @bcf_hdr = arg when nil @bcf_hdr = LibHTS.bcf_hdr_init("w") else raise TypeError, "Invalid argument" end @sync_depth = 0 @sync_needed = false @subset_samples = nil @subset_imap = nil @subset_imap_pointer = nil yield self if block_given? end |
Instance Attribute Details
#subset_imap_pointer ⇒ Object (readonly)
Returns the value of attribute subset_imap_pointer.
93 94 95 |
# File 'lib/hts/bcf/header.rb', line 93 def subset_imap_pointer @subset_imap_pointer end |
#subset_samples ⇒ Object (readonly)
Returns the value of attribute subset_samples.
93 94 95 |
# File 'lib/hts/bcf/header.rb', line 93 def subset_samples @subset_samples end |
Instance Method Details
#add_contig(id, length: nil, **attributes) ⇒ Object
191 192 193 194 195 196 |
# File 'lib/hts/bcf/header.rb', line 191 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
202 203 204 205 206 |
# File 'lib/hts/bcf/header.rb', line 202 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
228 229 230 231 232 233 |
# File 'lib/hts/bcf/header.rb', line 228 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
212 213 214 215 216 217 |
# File 'lib/hts/bcf/header.rb', line 212 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
244 245 246 247 248 249 250 251 252 |
# File 'lib/hts/bcf/header.rb', line 244 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
127 128 129 130 131 132 133 134 |
# File 'lib/hts/bcf/header.rb', line 127 def add_sample(sample, sync: true) rc = LibHTS.bcf_hdr_add_sample(@bcf_hdr, sample) raise "Failed to add sample #{sample}" if rc.negative? mark_sync_needed! sync_if_needed! if sync self end |
#append(line) ⇒ Object
158 159 160 161 162 163 164 |
# File 'lib/hts/bcf/header.rb', line 158 def append(line) rc = LibHTS.bcf_hdr_append(@bcf_hdr, line) raise "Failed to append VCF header line" if rc.negative? mark_sync_needed! self end |
#delete(bcf_hl_type, key = nil) ⇒ Object
FIXME
166 167 168 169 170 171 172 |
# File 'lib/hts/bcf/header.rb', line 166 def delete(bcf_hl_type, key = nil) # FIXME existed = hrec_exists?(bcf_hl_type, key) type = bcf_hl_type_to_int(bcf_hl_type) LibHTS.bcf_hdr_remove(@bcf_hdr, type, key) mark_sync_needed! if existed existed end |
#edit ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'lib/hts/bcf/header.rb', line 182 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
174 175 176 177 178 179 180 |
# File 'lib/hts/bcf/header.rb', line 174 def get_hrec(bcf_hl_type, key, value, str_class = nil) type = bcf_hl_type_to_int(bcf_hl_type) hrec = borrowed_hrec(type, key, value, str_class) return nil if hrec.to_ptr.null? HeaderRecord.new(owned_hrec(hrec)) end |
#get_tid(name) ⇒ Object
74 75 76 |
# File 'lib/hts/bcf/header.rb', line 74 def get_tid(name) name2id(name) end |
#get_version ⇒ Object
53 54 55 |
# File 'lib/hts/bcf/header.rb', line 53 def get_version LibHTS.bcf_hdr_get_version(@bcf_hdr) end |
#id2name(id) ⇒ Object
280 281 282 |
# File 'lib/hts/bcf/header.rb', line 280 def id2name(id) LibHTS.bcf_hdr_id2name(@bcf_hdr, id) end |
#merge(hdr) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/hts/bcf/header.rb', line 136 def merge(hdr) merged = LibHTS.bcf_hdr_merge(@bcf_hdr, hdr.struct) raise "Failed to merge BCF headers" if merged.to_ptr.null? @bcf_hdr = merged mark_sync_needed! sync_if_needed! self end |
#name2id(name) ⇒ Object
276 277 278 |
# File 'lib/hts/bcf/header.rb', line 276 def name2id(name) LibHTS.bcf_hdr_name2id(@bcf_hdr, name) end |
#nsamples ⇒ Object
66 67 68 |
# File 'lib/hts/bcf/header.rb', line 66 def nsamples LibHTS.bcf_hdr_nsamples(@bcf_hdr) end |
#read_bcf(fname) ⇒ Object
154 155 156 |
# File 'lib/hts/bcf/header.rb', line 154 def read_bcf(fname) LibHTS.bcf_hdr_set(@bcf_hdr, fname) end |
#remove_contig(id) ⇒ Object
198 199 200 |
# File 'lib/hts/bcf/header.rb', line 198 def remove_contig(id) delete("CONTIG", id.to_s).tap { sync_if_needed! } end |
#remove_filter(id) ⇒ Object
208 209 210 |
# File 'lib/hts/bcf/header.rb', line 208 def remove_filter(id) delete("FILTER", id.to_s).tap { sync_if_needed! } end |
#remove_format(id) ⇒ Object
240 241 242 |
# File 'lib/hts/bcf/header.rb', line 240 def remove_format(id) delete("FORMAT", id.to_s).tap { sync_if_needed! } end |
#remove_info(id) ⇒ Object
224 225 226 |
# File 'lib/hts/bcf/header.rb', line 224 def remove_info(id) delete("INFO", id.to_s).tap { sync_if_needed! } end |
#samples ⇒ Object
86 87 88 89 90 91 |
# File 'lib/hts/bcf/header.rb', line 86 def samples # bcf_hdr_id2name is macro function @bcf_hdr[:samples] .read_array_of_pointer(nsamples) .map(&:read_string) end |
#seqnames ⇒ Object
254 255 256 257 258 259 260 261 262 263 |
# File 'lib/hts/bcf/header.rb', line 254 def seqnames n = FFI::MemoryPointer.new(:int) names = LibHTS.bcf_hdr_seqnames(@bcf_hdr, n) begin names.read_array_of_pointer(n.read_int) .map(&:read_string) ensure LibHTS.hts_free(names) unless names.null? end end |
#set_version(version) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/hts/bcf/header.rb', line 57 def set_version(version) rc = LibHTS.bcf_hdr_set_version(@bcf_hdr, version) raise "Failed to set VCF header version" if rc.negative? mark_sync_needed! sync_if_needed! self end |
#struct ⇒ Object
45 46 47 |
# File 'lib/hts/bcf/header.rb', line 45 def struct @bcf_hdr end |
#subset(samples) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/hts/bcf/header.rb', line 103 def subset(samples) subset_samples = normalize_subset_samples(samples) validate_subset_samples!(subset_samples) nil imap_pointer = nil if subset_samples.empty? subset_hdr = LibHTS.bcf_hdr_subset(@bcf_hdr, 0, ::FFI::Pointer::NULL, ::FFI::Pointer::NULL) else encoded_samples = subset_samples.map { |name| FFI::MemoryPointer.from_string(name) } sample_pointers = FFI::MemoryPointer.new(:pointer, subset_samples.length) sample_pointers.write_array_of_pointer(encoded_samples) imap_pointer = FFI::MemoryPointer.new(:int, subset_samples.length) subset_hdr = LibHTS.bcf_hdr_subset(@bcf_hdr, subset_samples.length, sample_pointers, imap_pointer) end raise SubsetError, "Failed to subset BCF header samples #{subset_samples.inspect}" if subset_hdr.to_ptr.null? composed_imap = compose_subset_imap(read_subset_imap(imap_pointer, subset_samples.length)) self.class.new(subset_hdr).tap do |header| header.send(:set_subset_state, subset_samples, composed_imap) end end |
#subset? ⇒ Boolean
95 96 97 |
# File 'lib/hts/bcf/header.rb', line 95 def subset? !@subset_imap.nil? end |
#subset_sample_count ⇒ Object
99 100 101 |
# File 'lib/hts/bcf/header.rb', line 99 def subset_sample_count subset? ? @subset_samples.length : 0 end |
#sync ⇒ Object
146 147 148 149 150 151 152 |
# File 'lib/hts/bcf/header.rb', line 146 def sync rc = LibHTS.bcf_hdr_sync(@bcf_hdr) raise "Failed to sync BCF header" if rc.negative? @sync_needed = false self end |
#target_count ⇒ Object
70 71 72 |
# File 'lib/hts/bcf/header.rb', line 70 def target_count target_names.size end |
#target_name(rid) ⇒ Object
78 79 80 |
# File 'lib/hts/bcf/header.rb', line 78 def target_name(rid) id2name(rid) end |
#target_names ⇒ Object
82 83 84 |
# File 'lib/hts/bcf/header.rb', line 82 def target_names seqnames end |
#to_ptr ⇒ Object
49 50 51 |
# File 'lib/hts/bcf/header.rb', line 49 def to_ptr @bcf_hdr.to_ptr end |
#to_s ⇒ Object
265 266 267 268 269 270 271 272 273 274 |
# File 'lib/hts/bcf/header.rb', line 265 def to_s kstr = LibHTS::KString.new begin raise "Failed to get header string" if LibHTS.bcf_hdr_format(@bcf_hdr, 0, kstr).negative? kstr.read_string_copy ensure kstr.free_buffer end end |
#update_format(id, number:, type:, description:, **attributes) ⇒ Object
235 236 237 238 |
# File 'lib/hts/bcf/header.rb', line 235 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
219 220 221 222 |
# File 'lib/hts/bcf/header.rb', line 219 def update_info(id, number:, type:, description:, **attributes) delete("INFO", id.to_s) add_info(id, number:, type:, description:, **attributes) end |