Class: Bioinform::ConversionAlgorithms::PWM2PCMConverter

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

Overview

This algorithm is a purely heuristic based on our algorithm of PWM calculation. pcm –> pwm:

s_{\alpha,j} = ln(\frac{x_{\alpha,j} + \cappa p_{\alpha}}{(N+\cappa)p_{\alpha}}) - \beta_{j}
\beta_j is an arbitrary constant

Hence pwm –> pcm:

x_{\alpha,j} = (N + \cappa) p_{\alpha} \exp{ s_{\alpha,j} - \beta_j } - \cappa p_{\alpha}
\beta_j = log(\sum_{\alpha}p_{\alpha}s_{\alpha,j})  because \sum_{\alpha} x_{\alpha,j} = N

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PWM2PCMConverter

Returns a new instance of PWM2PCMConverter.



17
18
19
20
21
# File 'lib/bioinform/conversion_algorithms/pwm2pcm_converter.rb', line 17

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

Instance Attribute Details

#backgroundObject (readonly)

Returns the value of attribute background.



15
16
17
# File 'lib/bioinform/conversion_algorithms/pwm2pcm_converter.rb', line 15

def background
  @background
end

#countObject (readonly)

Returns the value of attribute count.



15
16
17
# File 'lib/bioinform/conversion_algorithms/pwm2pcm_converter.rb', line 15

def count
  @count
end

#pseudocountObject (readonly)

Returns the value of attribute pseudocount.



15
16
17
# File 'lib/bioinform/conversion_algorithms/pwm2pcm_converter.rb', line 15

def pseudocount
  @pseudocount
end

Instance Method Details

#calculate_pseudocount(pwm) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bioinform/conversion_algorithms/pwm2pcm_converter.rb', line 23

def calculate_pseudocount(pwm)
  case @pseudocount
  when Numeric
    @pseudocount
  when :default
    # *0.95 is to guarantee that rounding errors won't exceed real max pseudocount and generate PCM with negative elements
    max_pseudocount = max_pseudocount_fraction(pwm) * @count
    (Math.log(@count) <= max_pseudocount*0.95) ? Math.log(@count) : max_pseudocount * 0.95
  when Proc
    @pseudocount.call(pwm)
  else
    raise Error, 'Unknown pseudocount type use numeric or :default or Proc with taking pcm parameter'
  end
end

#convert(pwm) ⇒ Object

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bioinform/conversion_algorithms/pwm2pcm_converter.rb', line 65

def convert(pwm)
  raise Error, "Can convert only PWMs"  unless MotifModel.acts_as_pwm?(pwm)
  actual_pseudocount = calculate_pseudocount(pwm)
  matrix = pwm.each_position.map do |pos|
    beta = Math.log( weighted_position_exponent(pos) )
    pwm_pos = pos.each_index.map do |index|
      (@count + actual_pseudocount) * @background.frequencies[index] * Math.exp( pos[index] ) * Math.exp( -beta ) - actual_pseudocount * @background.frequencies[index]
    end
    pwm_pos
  end

  pcm = MotifModel::PCM.new(matrix)
  if pwm.respond_to? :name
    pcm.named(pwm.name)
  else
    pcm
  end
end