Class: HTS::Bam::BaseMod
- Inherits:
-
Object
- Object
- HTS::Bam::BaseMod
- Includes:
- Enumerable
- Defined in:
- lib/hts/bam/base_mod.rb
Overview
BaseMod is a view object that references data in a Record. The state is maintained in hts_base_mod_state structure.
Base modification information from MM/ML tags
This class provides access to DNA/RNA base modifications such as methylation. It wraps the htslib base modification API and provides a Ruby-friendly interface.
Defined Under Namespace
Classes: Error, Modification, NotParsedError, Position
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Instance Method Summary collapse
-
#[](position) ⇒ Position?
Array-style access to modifications at a position.
-
#at_pos(position, max_mods: 10) ⇒ Position?
Get modification information at a specific query position.
-
#close ⇒ void
Explicitly free the state.
-
#each_position(max_mods: 10) {|Position| ... } ⇒ Enumerator
(also: #each)
Iterate over all positions with modifications.
-
#each_raw(max_mods: 10) ⇒ Object
Iterate primitive modification values without Position/Modification object allocation.
-
#ensure_parsed!(flags = 0) ⇒ void
Ensure MM/ML have been parsed, performing lazy parse if enabled.
-
#initialize(record, auto_parse: true) ⇒ BaseMod
constructor
Initialize a new BaseMod object.
-
#inspect ⇒ String
Inspect string.
-
#modification_types ⇒ Array<Integer>
(also: #recorded_types)
Get list of modification types present in this record.
-
#parse(flags = 0) ⇒ Integer
Parse MM and ML tags from the record.
-
#parsed? ⇒ Boolean
Whether this object has parsed MM/ML tags already.
-
#query_type(code) ⇒ Hash?
Query information about a specific modification type by code.
-
#query_type_at(index) ⇒ Hash?
Query information about i-th modification type.
-
#to_a ⇒ Array<Position>
Get all modifications as an array.
-
#to_s ⇒ String
String representation for debugging.
Constructor Details
#initialize(record, auto_parse: true) ⇒ BaseMod
Initialize a new BaseMod object
135 136 137 138 139 140 141 |
# File 'lib/hts/bam/base_mod.rb', line 135 def initialize(record, auto_parse: true) @record = record @state = Native::BaseModHandle.open(record.__send__(:native_handle)) @closed = false @auto_parse = !!auto_parse @parsed = false end |
Instance Attribute Details
#record ⇒ Object (readonly)
Returns the value of attribute record.
18 19 20 |
# File 'lib/hts/bam/base_mod.rb', line 18 def record @record end |
Instance Method Details
#[](position) ⇒ Position?
Array-style access to modifications at a position
199 200 201 |
# File 'lib/hts/bam/base_mod.rb', line 199 def [](position) at_pos(position) end |
#at_pos(position, max_mods: 10) ⇒ Position?
Get modification information at a specific query position
186 187 188 189 190 191 192 193 194 |
# File 'lib/hts/bam/base_mod.rb', line 186 def at_pos(position, max_mods: 10) # Reset state to ensure deterministic results even after prior iteration parsed? ? parse : ensure_parsed! values = @state.at(position, max_mods) return nil unless values build_position(position, values) end |
#close ⇒ void
This method returns an undefined value.
Explicitly free the state
145 146 147 148 149 150 151 |
# File 'lib/hts/bam/base_mod.rb', line 145 def close return if @closed @state.close @state = nil @closed = true end |
#each_position(max_mods: 10) {|Position| ... } ⇒ Enumerator Also known as: each
Iterate over all positions with modifications
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/hts/bam/base_mod.rb', line 207 def each_position(max_mods: 10) return enum_for(__method__, max_mods: max_mods) unless block_given? current_position = nil modifications = [] each_raw(max_mods: max_mods) do |position, canonical, modified, strand, qual| if current_position && position != current_position yield Position.new(current_position, modifications) modifications = [] end current_position = position modifications << Modification.new( modified_base: modified, canonical_base: canonical, strand: strand, qual: qual ) end yield Position.new(current_position, modifications) if current_position self end |
#each_raw(max_mods: 10) ⇒ Object
Iterate primitive modification values without Position/Modification object allocation.
231 232 233 234 235 236 237 |
# File 'lib/hts/bam/base_mod.rb', line 231 def each_raw(max_mods: 10) return enum_for(__method__, max_mods: max_mods) unless block_given? parsed? ? parse : ensure_parsed! @state.each_raw(max_mods) { |*values| yield(*values) } self end |
#ensure_parsed!(flags = 0) ⇒ void
This method returns an undefined value.
Ensure MM/ML have been parsed, performing lazy parse if enabled.
162 163 164 165 166 167 168 |
# File 'lib/hts/bam/base_mod.rb', line 162 def ensure_parsed!(flags = 0) return if @parsed raise NotParsedError, "BaseMod is not parsed. Call #parse first (auto_parse is disabled)." unless @auto_parse parse(flags) end |
#inspect ⇒ String
Inspect string
289 290 291 |
# File 'lib/hts/bam/base_mod.rb', line 289 def inspect to_s end |
#modification_types ⇒ Array<Integer> Also known as: recorded_types
Get list of modification types present in this record
241 242 243 244 245 |
# File 'lib/hts/bam/base_mod.rb', line 241 def modification_types ensure_parsed! @state.types end |
#parse(flags = 0) ⇒ Integer
Parse MM and ML tags from the record
174 175 176 177 178 179 180 |
# File 'lib/hts/bam/base_mod.rb', line 174 def parse(flags = 0) ret = @state.parse(flags) raise Error, "Failed to parse base modifications" if ret < 0 @parsed = true ret end |
#parsed? ⇒ Boolean
Whether this object has parsed MM/ML tags already
155 156 157 |
# File 'lib/hts/bam/base_mod.rb', line 155 def parsed? @parsed end |
#query_type(code) ⇒ Hash?
Query information about a specific modification type by code
252 253 254 255 256 257 258 |
# File 'lib/hts/bam/base_mod.rb', line 252 def query_type(code) ensure_parsed! code = code.ord if code.is_a?(String) @state.query(code) end |
#query_type_at(index) ⇒ Hash?
Query information about i-th modification type
263 264 265 266 267 |
# File 'lib/hts/bam/base_mod.rb', line 263 def query_type_at(index) ensure_parsed! @state.query_at(index) end |
#to_a ⇒ Array<Position>
Get all modifications as an array
271 272 273 |
# File 'lib/hts/bam/base_mod.rb', line 271 def to_a each_position.to_a end |
#to_s ⇒ String
String representation for debugging
277 278 279 280 281 282 283 284 285 |
# File 'lib/hts/bam/base_mod.rb', line 277 def to_s return "#<HTS::Bam::BaseMod (not parsed)>" unless @parsed mods = [] each_position do |pos| mods << pos.to_s end "#<HTS::Bam::BaseMod #{mods.join(' ')}>" end |