Class: Bioinform::MotifModel::PM

Inherits:
Object
  • Object
show all
Defined in:
lib/bioinform/data_models/pm.rb

Direct Known Subclasses

PCM, PPM, PWM

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matrix, options = {}) ⇒ PM

Returns a new instance of PM.

Raises:



10
11
12
13
14
# File 'lib/bioinform/data_models/pm.rb', line 10

def initialize(matrix, options = {})
  @matrix = matrix
  @alphabet = options.fetch(:alphabet, NucleotideAlphabet)
  raise ValidationError.new('invalid matrix', validation_errors: validation_errors)  unless valid?
end

Instance Attribute Details

#alphabetObject (readonly)

Returns the value of attribute alphabet.



9
10
11
# File 'lib/bioinform/data_models/pm.rb', line 9

def alphabet
  @alphabet
end

#matrixObject (readonly)

Returns the value of attribute matrix.



9
10
11
# File 'lib/bioinform/data_models/pm.rb', line 9

def matrix
  @matrix
end

Class Method Details

.from_file(filename, options = {}) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/bioinform/data_models/pm.rb', line 23

def self.from_file(filename, options = {})
  parser = options.fetch(:parser, MatrixParser.new)
  alphabet = options.fetch(:alphabet, NucleotideAlphabet)
  info = parser.parse!(File.read(filename))
  name = (info[:name] && !info[:name].strip.empty?) ? info[:name] : File.basename(filename, File.extname(filename))
  self.new(info[:matrix], alphabet: alphabet).named( name )
end

.from_string(input, options = {}) ⇒ Object



16
17
18
19
20
21
# File 'lib/bioinform/data_models/pm.rb', line 16

def self.from_string(input, options = {})
  parser = options.fetch(:parser, MatrixParser.new)
  alphabet = options.fetch(:alphabet, NucleotideAlphabet)
  info = parser.parse!(input)
  self.new(info[:matrix], alphabet: alphabet).named( info[:name] )
end

Instance Method Details

#==(other) ⇒ Object



58
59
60
# File 'lib/bioinform/data_models/pm.rb', line 58

def ==(other)
  self.class == other.class && matrix == other.matrix && alphabet == other.alphabet
end

#complementedObject



74
75
76
# File 'lib/bioinform/data_models/pm.rb', line 74

def complemented
  self.class.new(complement_matrix, alphabet: alphabet)
end

#each_positionObject



62
63
64
65
66
67
68
# File 'lib/bioinform/data_models/pm.rb', line 62

def each_position
  if block_given?
    matrix.each{|pos| yield pos}
  else
    self.to_enum(:each_position)
  end
end

#lengthObject



50
51
52
# File 'lib/bioinform/data_models/pm.rb', line 50

def length
  matrix.size
end

#named(name) ⇒ Object

def consensus

ConsensusFormatter.by_maximal_elements.format_string(self)

end



95
96
97
# File 'lib/bioinform/data_models/pm.rb', line 95

def named(name)
  NamedModel.new(self, name)
end

#reverse_complementedObject Also known as: revcomp



78
79
80
# File 'lib/bioinform/data_models/pm.rb', line 78

def reverse_complemented
  self.class.new(complement_matrix.reverse, alphabet: alphabet)
end

#reversedObject



70
71
72
# File 'lib/bioinform/data_models/pm.rb', line 70

def reversed
  self.class.new(matrix.reverse, alphabet: alphabet)
end

#to_sObject



54
55
56
# File 'lib/bioinform/data_models/pm.rb', line 54

def to_s
  MotifFormatter.new.format(self)
end