Class: StrideAlign::SubstitutionMatrix

Inherits:
Object
  • Object
show all
Defined in:
lib/stride_align/matrices.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, alphabet:, matrix:, gap_score: -4,, wildcard: "X", gap_open: nil, gap_extend: nil) ⇒ SubstitutionMatrix

Returns a new instance of SubstitutionMatrix.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/stride_align/matrices.rb', line 8

def initialize(name:, alphabet:, matrix:, gap_score: -4, wildcard: "X",
               gap_open: nil, gap_extend: nil)
  raise ArgumentError, "name must be one String" unless name.is_a?(String)
  raise ArgumentError, "alphabet must be one String" unless alphabet.is_a?(String)

  symbols = alphabet.each_char.to_a
  raise ArgumentError, "alphabet has duplicate symbols" unless symbols.uniq.length == symbols.length
  unless matrix.is_a?(Array) && matrix.length == symbols.length &&
         matrix.all? { |row| row.is_a?(Array) && row.length == symbols.length }
    raise ArgumentError, "matrix dimensions must match the alphabet"
  end

  values = matrix.map do |row|
    row.map do |score|
      unless score.is_a?(Numeric) && score.finite? && score == score.to_i
        raise ArgumentError, "matrix must contain finite integer scores"
      end
      score = score.to_i
      raise ArgumentError, "matrix scores must fit in signed 8-bit integers" unless (-128..127).cover?(score)
      score
    end.freeze
  end.freeze
  unless wildcard.is_a?(String) && wildcard.each_char.count == 1 && symbols.include?(wildcard)
    raise ArgumentError, "wildcard must be one symbol in alphabet"
  end

  @name = name.dup.freeze
  @alphabet = alphabet.dup.freeze
  @matrix = values
  @gap_score = gap_score
  @wildcard = wildcard.dup.freeze
  @gap_open = gap_open
  @gap_extend = gap_extend
  @stride = symbols.length
  @max_abs = values.flatten.map(&:abs).max || 0
  @matrix_bytes = values.flatten.pack("c*").freeze
  @symbol_indices = symbols.each_with_index.to_h.freeze
  @wildcard_index = @symbol_indices.fetch(@wildcard)
end

Instance Attribute Details

#alphabetObject (readonly)

Returns the value of attribute alphabet.



5
6
7
# File 'lib/stride_align/matrices.rb', line 5

def alphabet
  @alphabet
end

#gap_extendObject (readonly)

Returns the value of attribute gap_extend.



5
6
7
# File 'lib/stride_align/matrices.rb', line 5

def gap_extend
  @gap_extend
end

#gap_openObject (readonly)

Returns the value of attribute gap_open.



5
6
7
# File 'lib/stride_align/matrices.rb', line 5

def gap_open
  @gap_open
end

#gap_scoreObject (readonly)

Returns the value of attribute gap_score.



5
6
7
# File 'lib/stride_align/matrices.rb', line 5

def gap_score
  @gap_score
end

#matrixObject (readonly)

Returns the value of attribute matrix.



5
6
7
# File 'lib/stride_align/matrices.rb', line 5

def matrix
  @matrix
end

#matrix_bytesObject (readonly)

Returns the value of attribute matrix_bytes.



5
6
7
# File 'lib/stride_align/matrices.rb', line 5

def matrix_bytes
  @matrix_bytes
end

#max_absObject (readonly)

Returns the value of attribute max_abs.



5
6
7
# File 'lib/stride_align/matrices.rb', line 5

def max_abs
  @max_abs
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/stride_align/matrices.rb', line 5

def name
  @name
end

#strideObject (readonly)

Returns the value of attribute stride.



5
6
7
# File 'lib/stride_align/matrices.rb', line 5

def stride
  @stride
end

#wildcardObject (readonly)

Returns the value of attribute wildcard.



5
6
7
# File 'lib/stride_align/matrices.rb', line 5

def wildcard
  @wildcard
end

Instance Method Details

#encode(sequence) ⇒ Object



48
49
50
# File 'lib/stride_align/matrices.rb', line 48

def encode(sequence)
  StrideAlign.substitution_matrix_encode(self, sequence)
end

#encode_one(sequence) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
# File 'lib/stride_align/matrices.rb', line 66

def encode_one(sequence)
  raise ArgumentError, "sequence must be a String" unless sequence.is_a?(String)

  sequence.encode(Encoding::UTF_8).each_char.map do |symbol|
    @symbol_indices.fetch(symbol, @wildcard_index)
  end
end

#score(query, target) ⇒ Object



52
53
54
# File 'lib/stride_align/matrices.rb', line 52

def score(query, target)
  StrideAlign.substitution_matrix_score(self, query, target)
end

#score_step_limit(gap_score: nil, gap_open: nil, gap_extend: nil) ⇒ Object



56
57
58
59
60
# File 'lib/stride_align/matrices.rb', line 56

def score_step_limit(gap_score: nil, gap_open: nil, gap_extend: nil)
  StrideAlign.substitution_matrix_score_step_limit(
    self, gap_score: gap_score, gap_open: gap_open, gap_extend: gap_extend
  )
end

#transpose(name: "#{@name}.T") ⇒ Object



62
63
64
# File 'lib/stride_align/matrices.rb', line 62

def transpose(name: "#{@name}.T")
  StrideAlign.substitution_matrix_transpose(self, name: name)
end