class Iroki::Biom

Attributes

single_group[RW]

Public Instance Methods

parse() click to toggle source
# File lib/iroki/biom.rb, line 28
def parse
  samples = []
  counts  = []
  lineno = -1
  first_line_count = -1

  self.each_line.with_index do |line, idx|
    unless line.start_with? "#"
      lineno += 1
      sample, *the_counts = line.chomp.split "\t"

      abort_if sample.nil? || sample.empty? || sample =~ / +/,
               "Line #{idx+1} has no sample"

      the_counts.flatten.each do |count|
        abort_unless valid_numerical_val?(count),
                     "The value '#{count}' in the " +
                     "biom file might not be valid"
      end

      if lineno.zero?
        first_line_count = the_counts.count
      else
        abort_unless first_line_count == the_counts.count,
                     "Line number #{idx+1} (#{line.inspect}) " +
                     "in the " +
                     "biom file has #{the_counts.count} " +
                     "columns when it should have " +
                     "#{first_line_count} columns like the " +
                     "first row does."
      end

      samples << sample

      if the_counts.length == 1
        counts << the_counts.first.to_f
        @single_group = true
      else
        counts << the_counts.map(&:to_f)
      end
    end
  end

  [samples, counts, @single_group]
end