lib/bioinform/parsers/matrix_parser.rb
# fix_nucleotides_number -- raises if matrix has not enough nucleotide columns
- raises if matrix has not enough nucleotide columns
- raises if matrix has not enough nucleotide columns
def parse!(input)
      lines = input.strip.lines.to_a
      if @has_name == :auto
        match = lines.first.match(@name_pattern)
        if match
          lines.shift
          name = match[:name]
        end
      elsif @has_name == false
        name = nil
      else
        match = lines.shift.match(@name_pattern)
        raise Error, "Name pattern doesn't match"  unless match
        name = match[:name]
      end
      lines.shift  if @has_header_row
      matrix = lines.map(&:rstrip).reject(&:empty?).map{|line| line.split }
      matrix = matrix.map{|row| row.drop(1) }  if @has_header_column
      matrix = matrix.map{|row| row.map{|el| Float(el) } }
      case @nucleotides_in
      when :columns
        matrix = matrix
      when :rows
        matrix = matrix.transpose
      when :auto
        if @fix_nucleotides_number && need_transpose?(matrix)
          matrix = matrix.transpose
        end
      end
      if @fix_nucleotides_number
        raise Error, 'Not enough nucleotides in a matrix'  unless matrix.all?{|pos| pos.size >= @fix_nucleotides_number}
        matrix = matrix.map{|pos| pos.first(@fix_nucleotides_number) }
      end
      {matrix: matrix, name: name}
    rescue => e
      raise Error, e.message
    end
def parse! ( input ) lines = input . strip . lines . to_a if @has_name == :auto match = lines . first . match ( @name_pattern ) if match lines . shift name = match [ :name ] end elsif @has_name == false name = nil else match = lines . shift . match ( @name_pattern ) raise Error , "Name pattern doesn't match" unless match name = match [ :name ] end lines . shift if @has_header_row matrix = lines . map ( & :rstrip ) . reject ( & :empty? ) . map { | line | line . split } matrix = matrix . map { | row | row . drop ( 1 ) } if @has_header_column matrix = matrix . map { | row | row . map { | el | Float ( el ) } } case @nucleotides_in when :columns matrix = matrix when :rows matrix = matrix . transpose when :auto if @fix_nucleotides_number && need_transpose? ( matrix ) matrix = matrix . transpose end end if @fix_nucleotides_number raise Error , 'Not enough nucleotides in a matrix' unless matrix . all? { | pos | pos . size >= @fix_nucleotides_number } matrix = matrix . map { | pos | pos . first ( @fix_nucleotides_number ) } end { matrix : matrix , name : name } rescue => e raise Error , e . message end
fix_nucleotides_number -- raises if matrix has not enough nucleotide columns
fix_nucleotides_number -- raises if matrix has not enough nucleotide columns
input
<not_specific>