Sha256: f8e75d177bfdda651e6786f6de4a66387adfc9e40f41f35852418138df6ec463

Contents?: true

Size: 1.75 KB

Versions: 8

Compression:

Stored size: 1.75 KB

Contents

# Part of the Optimus package for managing E-Prime data
# 
# Copyright (C) 2008-09 Board of Regents of the University of Wisconsin System
# 
# Written by Nathan Vack <njvack@wisc.edu>, at the Waisman Laborotory for Brain
# Imaging and Behavior, University of Wisconsin - Madison


module Optimus
  class Reader
    
    # This class is for reading tab-delimited Optimus files. (Or, really, any tab-delimited file).
    # The main option of interest is the :skip_lines option, which specifies how many lines
    # to skip before finding column names. For example:
    #
    # TabfileParser.new(stream, :skip_lines => 1)
    #
    # is what you'd use for skipping the filename line in a standard optimus Excel file.
    #
    # Note: you'll generally be using subclasses of this, and not manually specifying skip_lines.
    
    class TabfileParser
      def initialize(file, options = {})
        @file = file
        @skip_lines = options[:skip_lines] || 0
        @columns = options[:columns]
      end
      
      def to_optimus
        lines = @file.readlines
        @skip_lines.times do
          lines.shift
        end
        
        file_columns = lines.shift.split("\t").map {|elt| elt.strip }
        expected_size = file_columns.size
        columns = file_columns
        data = Optimus::Data.new(columns)
        current_line = @skip_lines+1
        lines.each do |line|
          current_line += 1
          row = data.add_row
          col_data = line.split("\t").map {|e| e.strip }
          if col_data.size != expected_size
            raise DamagedFileError.new("In #{@file.path}, line #{current_line} should have #{expected_size} columns but had #{col_data.size}.")
          end
          row.values = col_data
        end
        return data
      end
    end
  end 
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
optimus-ep-0.10.3 lib/tabfile_parser.rb
optimus-ep-0.10.2 lib/tabfile_parser.rb
optimus-ep-0.10.1 lib/tabfile_parser.rb
optimus-ep-0.10.0 lib/tabfile_parser.rb
optimus-ep-0.9.1 lib/tabfile_parser.rb
optimus-ep-0.9.0 lib/tabfile_parser.rb
optimus-ep-0.8.1 lib/tabfile_parser.rb
optimus-ep-0.8.0 lib/tabfile_parser.rb