Module: BioTable::Filter

Defined in:
lib/bio-table/filter.rb

Class Method Summary collapse

Class Method Details

.apply_column_filter(fields, index) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/bio-table/filter.rb', line 53

def Filter::apply_column_filter fields, index
  if index
    index.map { |idx| fields[idx] }
  else
    fields
  end
end

.create_column_index(columns, header) ⇒ Object

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.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bio-table/filter.rb', line 24

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) ⇒ Object

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



44
45
46
47
48
49
50
51
# File 'lib/bio-table/filter.rb', line 44

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, tablefields) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bio-table/filter.rb', line 86

def Filter::generic code, tablefields
  return true if code == nil
  if tablefields
    field = tablefields.dup
    begin
      eval(code)
    rescue Exception
      $stderr.print "Failed to evaluate ",fields," with ",code,"\n"
      raise 
    end
  else
    false
  end
end

.numeric(code, fields) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bio-table/filter.rb', line 70

def Filter::numeric code, fields
  return true if code == nil
  if fields
    # values = fields.map { |field| (valid_number?(field) ? field.to_f : nil ) } 
    values = LazyValues.new(fields)
    begin
      eval(code)
    rescue Exception
      $stderr.print "Failed to evaluate ",fields," with ",code,"\n"
      raise 
    end
  else
    false
  end
end

.valid_int?(s) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/bio-table/filter.rb', line 61

def Filter::valid_int?(s)
  s.to_i.to_s == s
end

.valid_number?(s) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/bio-table/filter.rb', line 65

def Filter::valid_number?(s)
  # s.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
  begin Float(s) ; true end rescue false
end