Module: BioTable::Filter

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

Class Method Summary collapse

Class Method Details

.apply_column_filter(fields, index) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/bio-table/filter.rb', line 69

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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bio-table/filter.rb', line 40

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



60
61
62
63
64
65
66
67
# File 'lib/bio-table/filter.rb', line 60

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



96
97
98
99
100
101
102
103
104
# File 'lib/bio-table/filter.rb', line 96

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



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

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

Returns:

  • (Boolean)


77
78
79
# File 'lib/bio-table/filter.rb', line 77

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

.valid_number?(s) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/bio-table/filter.rb', line 81

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