Class: Bioinform::ConversionAlgorithms::PWM2IupacPWMConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/bioinform/conversion_algorithms/pwm2iupac_pwm_converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PWM2IupacPWMConverter

Returns a new instance of PWM2IupacPWMConverter.



7
8
9
# File 'lib/bioinform/conversion_algorithms/pwm2iupac_pwm_converter.rb', line 7

def initialize(options = {})
  @iupac_alphabet = options.fetch(:alphabet, NucleotideAlphabetWithN)
end

Instance Attribute Details

#iupac_alphabetObject (readonly)

Returns the value of attribute iupac_alphabet.



6
7
8
# File 'lib/bioinform/conversion_algorithms/pwm2iupac_pwm_converter.rb', line 6

def iupac_alphabet
  @iupac_alphabet
end

Instance Method Details

#convert(pwm) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bioinform/conversion_algorithms/pwm2iupac_pwm_converter.rb', line 10

def convert(pwm)
  raise Error, "Can convert only PWMs"  unless MotifModel.acts_as_pwm?(pwm)
  raise Error, 'this conversion is possible only for ACGT-nucleotide motifs'  unless pwm.alphabet == NucleotideAlphabet
  iupac_matrix = pwm.each_position.map do |pos|
    @iupac_alphabet.each_letter.map do |letter|
      nucleotide_indices = IUPAC::NucleotideIndicesByIUPACLetter[letter]
      nucleotide_indices.inject(0.0){|sum, nucleotide_index| sum + pos[nucleotide_index] } / nucleotide_indices.size
    end
  end
  MotifModel::PWM.new(iupac_matrix, alphabet: @iupac_alphabet)
end