Class: HTS::Bam::Aux

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hts/bam/auxi.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Aux

Returns a new instance of Aux.



9
# File 'lib/hts/bam/auxi.rb', line 9

def initialize(record) = @record = record

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



7
8
9
# File 'lib/hts/bam/auxi.rb', line 7

def record
  @record
end

Class Method Details

.tag_id(tag) ⇒ Object

Raises:

  • (ArgumentError)


137
138
139
140
141
# File 'lib/hts/bam/auxi.rb', line 137

def self.tag_id(tag)
  raise ArgumentError, "AUX tag must be a 2-byte String" unless tag.is_a?(String) && tag.bytesize == 2

  tag.getbyte(0) | (tag.getbyte(1) << 8)
end

Instance Method Details

#[](key) ⇒ Object



19
# File 'lib/hts/bam/auxi.rb', line 19

def [](key) = get(key)

#[]=(key, value) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/hts/bam/auxi.rb', line 32

def []=(key, value)
  case value
  when Integer then update_int(key, value)
  when Float then update_float(key, value)
  when String then update_string(key, value)
  when Array then update_array(key, value)
  else raise ArgumentError, "Unsupported type: #{value.class}"
  end
end

#delete(key) ⇒ Object



111
# File 'lib/hts/bam/auxi.rb', line 111

def delete(key) = native.aux_delete(key)

#eachObject Also known as: each_pair



123
124
125
126
127
# File 'lib/hts/bam/auxi.rb', line 123

def each
  return enum_for(__method__) unless block_given?

  entries.each { |tag, _, value| yield tag, value }
end

#each_array(key, &block) ⇒ Object

Raises:

  • (TypeError)


21
22
23
24
25
26
27
28
29
30
# File 'lib/hts/bam/auxi.rb', line 21

def each_array(key, &block)
  return enum_for(__method__, key) unless block_given?

  pair = native.aux_get(key, nil)
  return nil unless pair
  raise TypeError, "AUX tag #{key} is not a B array" unless pair.first.start_with?("B:")

  pair.last.each(&block)
  self
end

#each_tag_idObject



130
131
132
133
134
135
# File 'lib/hts/bam/auxi.rb', line 130

def each_tag_id
  return enum_for(__method__) unless block_given?

  entries.each { |tag, _, value| yield self.class.tag_id(tag), value }
  self
end

#each_valueObject



117
118
119
120
121
# File 'lib/hts/bam/auxi.rb', line 117

def each_value
  return enum_for(__method__) unless block_given?

  entries.each { |_, _, value| yield value }
end

#each_with_type(&block) ⇒ Object



143
144
145
146
147
# File 'lib/hts/bam/auxi.rb', line 143

def each_with_type(&block)
  return enum_for(__method__) unless block_given?

  entries.each(&block)
end

#firstObject



115
# File 'lib/hts/bam/auxi.rb', line 115

def first = entries.first&.last

#get(key, type = nil) ⇒ Object



11
12
13
14
# File 'lib/hts/bam/auxi.rb', line 11

def get(key, type = nil)
  pair = native.aux_get(key, type&.to_s)
  pair&.last
end

#get_float(key) ⇒ Object



17
# File 'lib/hts/bam/auxi.rb', line 17

def get_float(key) = get(key, "f")

#get_int(key) ⇒ Object



16
# File 'lib/hts/bam/auxi.rb', line 16

def get_int(key) = get(key, "i")

#get_string(key) ⇒ Object



18
# File 'lib/hts/bam/auxi.rb', line 18

def get_string(key) = get(key, "Z")

#key?(key) ⇒ Boolean Also known as: include?

Returns:

  • (Boolean)


112
# File 'lib/hts/bam/auxi.rb', line 112

def key?(key) = native.aux_key?(key)

#to_hObject



149
# File 'lib/hts/bam/auxi.rb', line 149

def to_h = entries.to_h { |tag, _, value| [tag, value] }

#update_array(key, value, type: nil) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hts/bam/auxi.rb', line 94

def update_array(key, value, type: nil)
  validate_tag!(key)
  raise ArgumentError, "Array cannot be empty" if value.empty?

  type ||= if value.all? { |item| item.is_a?(Integer) }
             "i"
           elsif value.all? { |item| item.is_a?(Numeric) }
             "f"
           else
             raise ArgumentError, "Array must contain only integers or floats"
           end
  validate_array!(value, type)
  raise "Failed to update array tag '#{key}'" if native.aux_update_array(key, type, value).negative?

  value
end

#update_char(key, value) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/hts/bam/auxi.rb', line 72

def update_char(key, value)
  validate_tag!(key)
  string = value.to_s
  validate_char_value!(string)
  replace_with_append(key, "A", string.b)
  string
end

#update_double(key, value) ⇒ Object



88
89
90
91
92
# File 'lib/hts/bam/auxi.rb', line 88

def update_double(key, value)
  validate_tag!(key)
  replace_with_append(key, "d", [Float(value)].pack("E"))
  value.to_f
end

#update_float(key, value) ⇒ Object



56
57
58
59
60
61
# File 'lib/hts/bam/auxi.rb', line 56

def update_float(key, value)
  validate_tag!(key)
  raise "Failed to update float tag '#{key}'" if native.aux_update_float(key, value.to_f).negative?

  value
end

#update_hex(key, value) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/hts/bam/auxi.rb', line 80

def update_hex(key, value)
  validate_tag!(key)
  string = value.to_s
  validate_hex_value!(string)
  replace_with_append(key, "H", string.b + "\0")
  string
end

#update_int(key, value) ⇒ Object



42
43
44
45
46
47
# File 'lib/hts/bam/auxi.rb', line 42

def update_int(key, value)
  validate_tag!(key)
  raise "Failed to update integer tag '#{key}'" if native.aux_update_int(key, value.to_i).negative?

  value
end

#update_int16(key, value) ⇒ Object



51
# File 'lib/hts/bam/auxi.rb', line 51

def update_int16(key, value) = update_exact_integer(key, value, "s", -32_768, 32_767)

#update_int32(key, value) ⇒ Object



53
# File 'lib/hts/bam/auxi.rb', line 53

def update_int32(key, value) = update_exact_integer(key, value, "i", -2_147_483_648, 2_147_483_647)

#update_int8(key, value) ⇒ Object



49
# File 'lib/hts/bam/auxi.rb', line 49

def update_int8(key, value) = update_exact_integer(key, value, "c", -128, 127)

#update_string(key, value) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/hts/bam/auxi.rb', line 63

def update_string(key, value)
  validate_tag!(key)
  string = value.to_s
  validate_string_value!(string)
  raise "Failed to update string tag '#{key}'" if native.aux_update_string(key, string).negative?

  string
end

#update_uint16(key, value) ⇒ Object



52
# File 'lib/hts/bam/auxi.rb', line 52

def update_uint16(key, value) = update_exact_integer(key, value, "S", 0, 65_535)

#update_uint32(key, value) ⇒ Object



54
# File 'lib/hts/bam/auxi.rb', line 54

def update_uint32(key, value) = update_exact_integer(key, value, "I", 0, 4_294_967_295)

#update_uint8(key, value) ⇒ Object



50
# File 'lib/hts/bam/auxi.rb', line 50

def update_uint8(key, value) = update_exact_integer(key, value, "C", 0, 255)