Class: Ytilib::PPM

Inherits:
PM show all
Defined in:
lib/sequence_logo/ytilib/pm.rb,
lib/sequence_logo/ytilib/infocod.rb

Constant Summary

Constants inherited from PM

Ytilib::PM::IUPAC_LS

Instance Attribute Summary

Attributes inherited from PM

#matrix, #size, #words_count

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PM

#best_hit, #best_word, #col_sum, col_sum, #collect_hits, #consensus, #consensus_string, #dup, #each_position, #each_position_index, #eql?, #fast_score_sigma, #fast_score_sigma_precise, #find_hit, #find_hits, #fixwc, #flexeql?, from_bismark, #get_ppm, #icd2of4, #icd3of4, #icd4of4, #icdThc, #icdTlc, load, #m3sd, new_pcm, new_pwm, #p_value, #positiv!, #revcomp!, #save, #score_mean, #score_sigma, #score_variance, #strict_consensus

Constructor Details

#initialize(size, matrix = nil, words_count = nil) ⇒ PPM

DEPRECATED, use iupacomp! instead def make_N_comp!

@matrix['N'] = (0...size).collect { 0.25 }
return self

end



443
444
445
446
447
448
# File 'lib/sequence_logo/ytilib/pm.rb', line 443

def initialize(size, matrix = nil, words_count = nil)
  checkerr("matrix['A'].size != size") { matrix != nil && size != matrix['A'].size }
  @size = size
  @matrix = matrix == nil ? PM.new_matrix(size) : matrix
  @words_count = words_count
end

Class Method Details

.from_IUPAC(iupac) ⇒ Object



556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/sequence_logo/ytilib/pm.rb', line 556

def PPM.from_IUPAC(iupac)
  matrix = {"A" => [], "C" => [], "G" => [], "T" => []}
  
  (0...iupac.size).each { |i|
    matrix.each_key { |k| matrix[k] << 0.0 }
    letters = IUPAC::REVCODE[iupac[i]]
    (0...letters.size).each { |j|
      matrix[letters[j]][-1] = 1.0/letters.size
    }
  }
  
  newppm = PPM.new(iupac.size, matrix, 4.0)
  newppm.iupacomp!
  
  newppm
end

.probs2IUPAC!(probs) ⇒ Object



494
495
496
497
498
499
# File 'lib/sequence_logo/ytilib/pm.rb', line 494

def PPM.probs2IUPAC!(probs)
  IUPAC_LS.each { |iul_ls|
    probs[iul_ls[0]] = iul_ls[1].inject(0) { |sum, l| sum += probs[l] } / iul_ls[1].size
  }
  return probs
end

Instance Method Details

#best_scoreObject



470
471
472
473
474
# File 'lib/sequence_logo/ytilib/pm.rb', line 470

def best_score
  return (0...size).inject(1) { |mul, i|
    mul *= ['A', 'C', 'G', 'T'].collect { |l| @matrix[l][i] }.max
  }    
end

#get_pcm(words_count = nil) ⇒ Object Also known as: to_pcm



542
543
544
545
546
547
548
549
550
551
552
553
# File 'lib/sequence_logo/ytilib/pm.rb', line 542

def get_pcm(words_count = nil)
  words_count = @words_count unless words_count
  checkerr("undefined words count") { !words_count }
  counts = PM.new_matrix(@size)
  (0...size).each { |i|
    ['A', 'C', 'G', 'T'].each { |l|
      counts[l][i] = @matrix[l][i] * words_count
    }
  }
  newpcm = PM.new(size, counts, words_count).iupacomp!
  return newpcm
end

#get_pwm(words_count = nil, probs = Randoom::DEF_PROBS, pseudocount = 1.0) ⇒ Object Also known as: to_pwm



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/sequence_logo/ytilib/pm.rb', line 501

def get_pwm(words_count = nil, probs = Randoom::DEF_PROBS, pseudocount = 1.0)
  
  probs = PPM.probs2IUPAC!(probs.dup)
  
  words_count = @words_count if !words_count || words_count == 0
  checkerr("undefined words count") { !words_count }
  
  pwm = @matrix['N'] ? PM.new_matrix_iupac(@size) : PM.new_matrix(@size)
  
  @matrix.each_key do |letter|
    (0...@size).each { |pos|
      
      pwm[letter][pos] = Math::log( (@matrix[letter][pos] * words_count + (probs[letter] * pseudocount) ) / ( (words_count + pseudocount) * probs[letter]) )
      
    }
  end
  return PM.new(@size, pwm, words_count)
  #pcm = get_pcm(words_count)
  #pcm.iupacomp! if @matrix['N']
  #return pcm.to_pwm!(words_count, probs, pseudocount)
end

#get_pwm0pc(probs = Randoom::DEF_PROBS) ⇒ Object



524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/sequence_logo/ytilib/pm.rb', line 524

def get_pwm0pc(probs = Randoom::DEF_PROBS)
  new_matrix = {}
  @matrix.each_key { |letter| new_matrix[letter] = @matrix[letter].dup }
  newpm = PM.new(@size, new_matrix, nil)
  
  new_matrix.each_key do |letter|
    (0...@size).each { |pos|
      new_matrix[letter][pos] = Math::log(@matrix[letter][pos] / probs[letter])
    }
  end
  
  return newpm
end

#icd(position = nil) ⇒ Object



104
105
106
# File 'lib/sequence_logo/ytilib/infocod.rb', line 104

def icd(position = nil)
  return to_pcm.infocod(position)
end

#infocod(position = nil) ⇒ Object



100
101
102
# File 'lib/sequence_logo/ytilib/infocod.rb', line 100

def infocod(position = nil)
  return to_pcm.infocod(position)
end

#iupacomp!Object



450
451
452
453
454
455
456
457
458
# File 'lib/sequence_logo/ytilib/pm.rb', line 450

def iupacomp!
  @words_count = 4.0 unless @words_count
  
  IUPAC_LS.each { |iul_ls|
    @matrix[iul_ls[0]] = (0...@size).collect { |i| col_sum(i, iul_ls[1]) / iul_ls[1].size }
  }
  
  return self
end

#score(word) ⇒ Object



460
461
462
463
464
465
466
467
468
# File 'lib/sequence_logo/ytilib/pm.rb', line 460

def score(word)
  checkerr("word size != ppm.size") { @size != word.size }
  checkerr("word #{word} has strange characters") { 
    @matrix.keys.include?('N') ? word.tr('ACGTRYKMSWBDHVN', '').size > 0 : word.tr('ACGT', '').size > 0
  }
  return (0...@size).inject(1) { |mul, i| 
    mul *= @matrix[word[i,1]][i]
  }
end

#to_bismark(b) ⇒ Object



482
483
484
485
486
487
488
489
490
491
492
# File 'lib/sequence_logo/ytilib/pm.rb', line 482

def to_bismark(b)
  attributes = {"length" => @size}
  attributes["words-count"] = @words_count if @words_count
  pe = b.add_element("PPM", attributes)
  (0...@matrix['A'].size).each { |i|
    pm_c = pe.add_element("pm-column", {"position" => i+1})
    ['A', 'C', 'G', 'T'].each { |l|
      pm_c.add_element(l.downcase).add_text(@matrix[l][i].to_s)
    }
  }
end

#to_pwm!Object



538
539
540
# File 'lib/sequence_logo/ytilib/pm.rb', line 538

def to_pwm!
  raise "cannot force PPM class to PWM, use to_pwm instead"
end

#worst_scoreObject



476
477
478
479
480
# File 'lib/sequence_logo/ytilib/pm.rb', line 476

def worst_score
  return (0...size).inject(0) { |mul, i|
    mul *= ['A', 'C', 'G', 'T'].collect { |l| @matrix[l][i] }.min
  }
end