Module: Signature

Defined in:
lib/rbbt/expression_old/signature.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.open(file, field = nil, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/rbbt/expression_old/signature.rb', line 10

def self.open(file, field = nil, options = {})
  options = Misc.add_defaults options, :fields => nil, :cast => :to_f, :type => :single

  options[:fields] ||= [field] if field

  tsv = TSV.open(file, options)
  tsv.extend Signature
  tsv
end

.setup(hash, options = {}) ⇒ Object



5
6
7
8
# File 'lib/rbbt/expression_old/signature.rb', line 5

def self.setup(hash, options = {})
  hash.extend Signature
  hash
end

.tsv_field(tsv, field, cast = nil) ⇒ Object



20
21
22
23
# File 'lib/rbbt/expression_old/signature.rb', line 20

def self.tsv_field(tsv, field, cast = nil)
  tsv = TSV.open(tsv) unless TSV === tsv
  Signature.setup(tsv.column(field, cast))
end

Instance Method Details

#absObject



47
48
49
# File 'lib/rbbt/expression_old/signature.rb', line 47

def abs
  transform{|value| value.abs}
end

#clean_emptyObject

{{{ Rank stuff



69
70
71
# File 'lib/rbbt/expression_old/signature.rb', line 69

def clean_empty
  Signature.setup(select{|k,v| v.nil? ? false : (v.respond_to?(:empty) ? !v.empty? : true)}.tap{|s| s.unnamed = true})
end

#logObject



51
52
53
# File 'lib/rbbt/expression_old/signature.rb', line 51

def log
  transform{|value| Math.log(value)}
end

#pvalue_fdr_adjust!Object



96
97
98
99
# File 'lib/rbbt/expression_old/signature.rb', line 96

def pvalue_fdr_adjust!
  FDR.adjust_hash! self
  self
end

#pvalue_scoreObject



101
102
103
# File 'lib/rbbt/expression_old/signature.rb', line 101

def pvalue_score
  transform{|value| value > 0 ? -Math.log(value + 0.00000001) : Math.log(-value + 0.00000001)}
end

#pvalue_sortedObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rbbt/expression_old/signature.rb', line 105

def pvalue_sorted
  OrderedList.setup(clean_empty.transform{|v| v.to_f}.sort{|a,b| 
    a = a[1]
    b = b[1]
    case
    when a == b
      0
    when (a <= 0 and b >= 0)
      1
    when (a >= 0 and b <= 0)
      -2
    when a > 0
      a.abs <=> b.abs
    else
      b.abs <=> a.abs
    end
  
  }.collect{|elem,v| elem})
end

#pvalue_sorted_weightsObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rbbt/expression_old/signature.rb', line 125

def pvalue_sorted_weights
  sorted = clean_empty.transform{|v| v.to_f}.sort{|a,b| 
    a = a[1]
    b = b[1]
    case
    when a == b
      0
    when (a <= 0 and b >= 0)
      1
    when (a >= 0 and b <= 0)
      -2
    when a > 0
      a.abs <=> b.abs
    else
      b.abs <=> a.abs
    end
  }

  keys = []
  weights = []
  sorted.each{|k,v| keys << k; weights << - Math.log(v.abs)}

  OrderedList.setup(keys, weights)
end

#ranksObject



77
78
79
80
81
82
83
# File 'lib/rbbt/expression_old/signature.rb', line 77

def ranks
  ranks = TSV.setup({}, :key_field => self.key_field, :fields => ["Rank"], :cast => :to_i, :type => :single)
  sorted.each_with_index do |elem, i|
    ranks[elem] = i
  end
  ranks
end

#select(*args, &block) ⇒ Object

{{{ Basic manipulation



27
28
29
# File 'lib/rbbt/expression_old/signature.rb', line 27

def select(*args, &block)
  Signature.setup(super(*args, &block))
end

#significant_pvalues(threshold) ⇒ Object

{{{ Pvalue stuff



87
88
89
90
91
92
93
94
95
# File 'lib/rbbt/expression_old/signature.rb', line 87

def significant_pvalues(threshold)
  entity_options = self.entity_options
  entity_options[:organism] ||= self.namespace
  if threshold > 0
    Misc.prepare_entity(self.select{|k,v| v > 0 and v <= threshold}.collect{|k,v| k}, self.key_field, entity_options)
  else
    Misc.prepare_entity(self.select{|k,v| v < 0 and v >= threshold}.collect{|k,v| k}, self.key_field, entity_options)
  end
end

#sortedObject



73
74
75
# File 'lib/rbbt/expression_old/signature.rb', line 73

def sorted
  OrderedList.setup(clean_empty.sort_by{|elem,v| v}.collect{|elem,v| elem})
end

#transform(&block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rbbt/expression_old/signature.rb', line 31

def transform(&block)
  case
  when (block_given? and block.arity == 2)
    self.each do |key, value|
      self[key] = yield key, value
    end
  when (block_given? and block.arity == 1)
    self.each do |key, value|
      self[key] = yield value
    end
  else
    raise "Block not given, or arity not 1 or 2"
  end
  self
end

#values_over(threshold) ⇒ Object



55
56
57
58
59
# File 'lib/rbbt/expression_old/signature.rb', line 55

def values_over(threshold)
  entity_options = self.entity_options
  entity_options[:organism] ||= self.namespace
  Misc.prepare_entity(self.select{|k,v| v >= threshold}.collect{|k,v| k}, self.key_field, entity_options)
end

#values_under(threshold) ⇒ Object



61
62
63
64
65
# File 'lib/rbbt/expression_old/signature.rb', line 61

def values_under(threshold)
  entity_options = self.entity_options
  entity_options[:organism] ||= self.namespace
  Misc.prepare_entity(self.select{|k,v| v <= threshold}.collect{|k,v| k}, self.key_field, entity_options)
end