Sha256: 5dbfcad463e4085cc857a33bb29f394de329841c8e138c62464ab9bbab0a1a31

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

require 'bioinform/support'

module Bioinform
  class Parser
    attr_reader :input, :matrix
    
    def initialize(input)
      @input = input
    end
    
    def parse
      inp = input
      transpose =  inp.is_a?(Hash)
      inp = ClassMethods.try_convert_to_array(inp)
      inp.map!{|x| ClassMethods.try_convert_to_array(x)}
      transpose = true  if (not inp.all?{|x| x.size == 4}) && inp.size == 4 && inp.same_by?(&:size)
      @matrix = transpose ? inp.transpose : inp
      result
    rescue
      {}
    end
      
    def result(options={})
      raise 'Parsing Error' unless matrix.is_a?(Array) && matrix.all?(&:is_a?.(Array)) && matrix.all?{|pos| pos.size == 4} && matrix.all?(&:all?.(&:is_a?.(Numeric)))
      options.merge(matrix: @matrix)
    end
    
    class ClassMethods
      def self.array_from_acgt_hash(hsh)
        hsh = hsh.collect_hash{|key,value| [key.to_s.upcase, value] }
        raise 'some of hash keys A,C,G,T are missing or hash has excess keys' unless hsh.keys.sort == %w[A C G T]
        %w[A C G T].collect{|letter| hsh[letter] }
      end
      def self.try_convert_to_array(inp)
        return inp  if inp.is_a? Array
        array_from_acgt_hash(inp)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bioinform-0.1.4 lib/bioinform/parsers/parser.rb