module BioTable::Filter

Public Class Methods

apply_column_filter(fields, index) click to toggle source
# File lib/bio-table/filter.rb, line 82
def Filter::apply_column_filter fields, index
  if index
    index.map { |idx| fields[idx] }
  else
    fields
  end
end
create_column_index(columns, header) click to toggle source

Create an index to the column headers, so header A,B,C,D with columns C,A returns [2,0]. It can be the column index is already indexed, return it in that case.

# File lib/bio-table/filter.rb, line 53
def Filter::create_column_index columns, header
  return nil if not columns 
  # check whether columns is already a list of numbers
  numbers = columns.dup.delete_if { |v| not valid_int?(v) }
  if numbers.size == columns.size
    return columns.map { |v| v.to_i }
  end

  # create the index from names
  index = []
  columns.each do | name |
    pos = header.index(name)
    raise "Column name #{name} not found!" if pos == nil
    index << pos 
  end
  return index
end
filter_column_index(index, header, expression) click to toggle source

Filter on (indexed) column names, using an expression and return a new index

# File lib/bio-table/filter.rb, line 73
def Filter::filter_column_index index, header, expression
  return index if not expression or expression == ""
  index = (0..header.size-1).to_a if index == nil
  index.map { |idx| 
    colname = header[idx]
    (idx==0 || eval(expression) ? idx : nil)
  }.compact
end
generic(code, fields, header) click to toggle source
# File lib/bio-table/filter.rb, line 109
def Filter::generic code, fields, header
  return true if code == nil
  if fields
    filter = TextualFilter.new(header)
    filter.textual(code, fields)
  else
    false
  end
end
numeric(code, fields, header) click to toggle source
# File lib/bio-table/filter.rb, line 99
def Filter::numeric code, fields, header
  return true if code == nil
  if fields
    filter = NumericFilter.new(header)
    filter.numeric(code, fields)
  else
    false
  end
end
valid_int?(s) click to toggle source
# File lib/bio-table/filter.rb, line 90
def Filter::valid_int?(s)
  s.to_i.to_s == s
end
valid_number?(s) click to toggle source
# File lib/bio-table/filter.rb, line 94
def Filter::valid_number?(s)
  # s.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
  begin Float(s) ; true end rescue false
end