Sha256: 6eebba18e7a9c09ff646a1742e45099f08427d5f92c3fe763de7e983f79bff85

Contents?: true

Size: 930 Bytes

Versions: 5

Compression:

Stored size: 930 Bytes

Contents

require_relative '../alphabet'

module Bioinform
  module ConversionAlgorithms
    class PWM2IupacPWMConverter
      attr_reader :iupac_alphabet
      def initialize(options = {})
        @iupac_alphabet = options.fetch(:alphabet, NucleotideAlphabetWithN)
      end
      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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bioinform-0.3.1 lib/bioinform/conversion_algorithms/pwm2iupac_pwm_converter.rb
bioinform-0.3.0 lib/bioinform/conversion_algorithms/pwm2iupac_pwm_converter.rb
bioinform-0.2.2 lib/bioinform/conversion_algorithms/pwm2iupac_pwm_converter.rb
bioinform-0.2.1 lib/bioinform/conversion_algorithms/pwm2iupac_pwm_converter.rb
bioinform-0.2.0 lib/bioinform/conversion_algorithms/pwm2iupac_pwm_converter.rb