Class: Bioinform::PWM

Inherits:
PM show all
Defined in:
lib/bioinform/data_models/pwm.rb

Instance Attribute Summary

Attributes inherited from PM

#matrix, #parameters

Instance Method Summary collapse

Methods inherited from PM

#==, #as_pcm, #as_ppm, #as_pwm, choose_parser, #discrete, #discrete!, #dup, #each_position, #initialize, #left_augment, #left_augment!, #length, #pretty_string, #probability, #reverse_complement, #reverse_complement!, #right_augment, #right_augment!, split_on_motifs, #to_hash, #to_s, #valid?, valid_matrix?, #vocabulary_volume, zero_column

Methods included from Parameters

#get_parameters, included, #parameter_defined?, #parameters, #set_parameters

Constructor Details

This class inherits a constructor from Bioinform::PM

Instance Method Details

#best_scoreObject



40
41
42
# File 'lib/bioinform/data_models/pwm.rb', line 40

def best_score
  best_suffix(0)
end

#best_suffix(i) ⇒ Object

best score of suffix s



48
49
50
# File 'lib/bioinform/data_models/pwm.rb', line 48

def best_suffix(i)
  @matrix[i...length].map(&:max).inject(0.0, &:+)
end

#score(word) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bioinform/data_models/pwm.rb', line 21

def score(word)
  raise ArgumentError, 'word in PWM#score(word) should have the same length as matrix'  unless word.length == length
  #raise ArgumentError, 'word in PWM#score(word) should have only ACGT-letters'  unless word.each_char.all?{|letter| %w{A C G T}.include? letter}

  (0...length).map do |pos|
    letter = word[pos]
    if IndexByLetter[letter]
      matrix[pos][IndexByLetter[letter]]
    elsif letter == 'N'
      matrix[pos].zip(probability).map{|el, p| el * p}.inject(0, &:+)
    else
      raise ArgumentError, "word in PWM#score(#{word}) should have only ACGT or N letters"
    end
  end.inject(0, &:+).to_f
end

#score_meanObject



5
6
7
# File 'lib/bioinform/data_models/pwm.rb', line 5

def score_mean
  each_position.inject(0){ |mean, position| mean + position.each_index.inject(0){|sum, letter| sum + position[letter] * probability[letter]} }
end

#score_varianceObject



8
9
10
11
12
13
# File 'lib/bioinform/data_models/pwm.rb', line 8

def score_variance
  each_position.inject(0) do |variance, position|
    variance  + position.each_index.inject(0) { |sum,letter| sum + position[letter]**2 * probability[letter] } -
                position.each_index.inject(0) { |sum,letter| sum + position[letter]    * probability[letter] }**2
  end
end

#threshold_gauss_estimation(pvalue) ⇒ Object



15
16
17
18
19
# File 'lib/bioinform/data_models/pwm.rb', line 15

def threshold_gauss_estimation(pvalue)
  sigma = Math.sqrt(score_variance)
  n_ = Math.inverf(1 - 2 * pvalue) * Math.sqrt(2)
  score_mean + n_ * sigma
end

#to_pwmObject



36
37
38
# File 'lib/bioinform/data_models/pwm.rb', line 36

def to_pwm
  self
end

#worst_scoreObject



43
44
45
# File 'lib/bioinform/data_models/pwm.rb', line 43

def worst_score
  worst_suffix(0)
end

#worst_suffix(i) ⇒ Object



52
53
54
# File 'lib/bioinform/data_models/pwm.rb', line 52

def worst_suffix(i)
  @matrix[i...length].map(&:min).inject(0.0, &:+)
end