Module: Score

Defined in:
lib/MARQ/score.rb

Constant Summary collapse

COLORS =
{
  :red =>   PNG::Color::Red,
  :green => PNG::Color::Green,
  :white => PNG::Color::White,
  :black => PNG::Color::Black,

}

Class Method Summary collapse

Class Method Details

.add_pvalues(scores, null_scores) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/MARQ/score.rb', line 142

def self.add_pvalues(scores, null_scores)
  null_scores = null_scores.sort
  times       = null_scores.length

  scores.each do |experiment, info|
    info[:pvalue] = (times - null_scores.count_smaller(info[:score].abs)).to_f / times
  end

  scores
end

.combine(up, down) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/MARQ/score.rb', line 97

def self.combine(up, down)
  return down if up == 0
  return up if down == 0

  return up - down
  if (up > 0) == (down > 0)
    return 0
  else
    up - down
  end
end

.draw_hits(hits, total, filename = nil, options = {}) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/MARQ/score.rb', line 161

def self.draw_hits(hits, total, filename = nil, options = {})

  size = options[:size] || total
  bg_color = options[:bg_color] || :white
  width = options[:width] || 20
  sections = options[:sections] || []

  size = [size, total].min

  hits = hits.collect{|h| h - 1}
  if size < total
    hits = hits.collect{|h| (h.to_f * size / total).to_i}
  end

  canvas = PNG::Canvas.new size, width, COLORS[bg_color]

  sections.each{|color, info|
    start = info[0]
    finish = info[1]
    (start..finish).each{|x|
      (0..width - 1).each{|y|
        canvas[x,y] = COLORS[color]
      }
    }
  }

  hits.each{|hit|
    canvas.line hit, 0, hit , width - 1, PNG::Color::Black
  }

  png = PNG.new canvas

  if filename
    png.save filename
  else
    png.to_blob
  end
end

.null_scores(up_size, down_size, times = 10000) ⇒ Object



135
136
137
138
139
140
# File 'lib/MARQ/score.rb', line 135

def self.null_scores(up_size, down_size, times = 10000)
  up_perm   = permutations(up_size, times)
  down_perm = permutations(down_size, times)

  up_perm.zip(down_perm).collect{|p| up, down = p; combine(up, down).abs}
end

.permutations(size, times) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/MARQ/score.rb', line 124

def self.permutations(size, times)
  total = 10000
  if size == 0
    [0] * times
  else
    (1..times).collect do
      fast_score_scale(Array.new(size){ (rand * total).to_i }.sort, total, 0)
    end
  end
end

.score(*args) ⇒ Object



67
68
69
# File 'lib/MARQ/score.rb', line 67

def self.score(*args)
  self.fast_score_scale(*args)
end

.scores(dataset, genes) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/MARQ/score.rb', line 71

def self.scores(dataset, genes)
  positions   = MADB.load_positions(dataset, genes)
  values      = MADB.num_values(dataset)

  experiments = positions.keys

  scores      = {}
  experiments.each do |experiment|
    hits  = positions[experiment].compact
    total = values[experiment]
    if hits.nil? || hits.empty?
      score = 0
    else
      missing = genes.length - hits.length
      score = self.fast_score_scale(hits.sort, total, missing) 
    end
    scores[experiment] = {
      :positions => positions[experiment],
      :score     => score,
      :total     => total,
    }
  end

  scores
end

.scores_up_down(dataset, up, down) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/MARQ/score.rb', line 109

def self.scores_up_down(dataset, up, down)
  scores_up   = scores(dataset, up)
  scores_down = scores(dataset, down)

  scores      = {}
  scores_up.keys.each do |experiment|
    scores[experiment] = {}
    scores[experiment][:up]    = scores_up[experiment]
    scores[experiment][:down]  = scores_down[experiment]
    scores[experiment][:score] = combine(scores_up[experiment][:score], scores_down[experiment][:score])
  end

  scores
end