Class: Bioinform::ConversionAlgorithms::PCM2PWMConverter

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

Overview

s_alpha,j = ln(fracx_{alpha,j + cappa p_alpha}(N+cappa)p_{alpha})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PCM2PWMConverter

Returns a new instance of PCM2PWMConverter.



10
11
12
13
# File 'lib/bioinform/conversion_algorithms/pcm2pwm_converter.rb', line 10

def initialize(options = {})
  @background = options.fetch(:background, Bioinform::Background::Uniform)
  @pseudocount = options.fetch(:pseudocount, :log)
end

Instance Attribute Details

#backgroundObject (readonly)

Returns the value of attribute background.



9
10
11
# File 'lib/bioinform/conversion_algorithms/pcm2pwm_converter.rb', line 9

def background
  @background
end

#pseudocountObject (readonly)

Returns the value of attribute pseudocount.



9
10
11
# File 'lib/bioinform/conversion_algorithms/pcm2pwm_converter.rb', line 9

def pseudocount
  @pseudocount
end

Instance Method Details

#calculate_pseudocount(pcm) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bioinform/conversion_algorithms/pcm2pwm_converter.rb', line 15

def calculate_pseudocount(pcm)
  case @pseudocount
  when Numeric
    @pseudocount
  when :log
    Math.log(pcm.count)
  when :sqrt
    Math.sqrt(pcm.count)
  when Proc
    @pseudocount.call(pcm)
  else
    raise Error, 'Unknown pseudocount type use numeric or :log or :sqrt or Proc with taking pcm parameter'
  end
end

#convert(pcm) ⇒ Object

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bioinform/conversion_algorithms/pcm2pwm_converter.rb', line 30

def convert(pcm)
  raise Error, "#{self.class}#convert accepts only models acting as PCM"  unless MotifModel.acts_as_pcm?(pcm)
  actual_pseudocount = calculate_pseudocount(pcm)
  matrix = pcm.each_position.map do |pos|
    count = pos.inject(0.0, &:+)
    pos.each_index.map do |index|
      Math.log((pos[index] + @background.frequencies[index] * actual_pseudocount).to_f / (@background.frequencies[index]*(count + actual_pseudocount)) )
    end
  end
  pwm = MotifModel::PWM.new(matrix)
  if pcm.respond_to? :name
    pwm.named(pcm.name)
  else
    pwm
  end
end