Module: Annotations

Defined in:
lib/MARQ/annotations.rb

Defined Under Namespace

Modules: GO, UMLS

Constant Summary collapse

RANK_SIZE_BINS =
%w(1 2 3 4 5 7 10 15 20 30 40 50 65 80 100 125 150 175 200 250 300 350 400 450 500 600 700 800 900 1000 1500 2000 2500 3000)

Class Method Summary collapse

Class Method Details

.annotations(scores, type, pvalue = 0.05, algorithm = :rank) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/MARQ/annotations.rb', line 295

def self.annotations(scores, type, pvalue = 0.05, algorithm = :rank) 
  annot = {}
  relevant = []

  dict_options = {}
  if type == "Words"
    dict_options = {:low => 0, :hi => 0.05, :limit => 100000}
  else
    dict_options = {:low => 0, :hi => 0.5, :limit => 100000}
  end

  case
  when type =~ /^(.*)_direct$/
    side = :direct
    type = $1
  when type =~ /^(.*)_inverse$/
    side = :inverse
    type = $1
  end


  terms_cache = {}
  scores.each{|experiment, info|
    dataset = experiment.match(/^(.*?): /)[1]
    name = $'.strip 
    case
    when side.nil?
      term_file = File.join(MARQ.datadir, MARQ.platform_type(dataset).to_s , 'annotations',type, dataset)
    when side == :direct && info[:score] > 0 || side == :inverse && info[:score] < 0
      term_file = File.join(MARQ.datadir, MARQ.platform_type(dataset).to_s , 'annotations',type + '_up', dataset)
    else
      term_file = File.join(MARQ.datadir, MARQ.platform_type(dataset).to_s , 'annotations',type + '_down', dataset)
    end

    if File.exist? term_file
      terms_cache[term_file] ||= YAML::load(File.open(term_file))
      terms = terms_cache[term_file] 
      annot[experiment] = {:dataset => (terms[:dataset] || []), :signature => (terms[name] || [])}
    else
      annot[experiment] = {:dataset =>  [], :signature => []}
    end

    relevant << experiment if info[:pvalue] <= pvalue
  }

  if algorithm == :rank
    ranks = scores.sort{|a,b| compare(a[1],b[1]) }.collect{|p| p[0]}
    terms = enrichment_rank(annot, ranks, dict_options)
  else
    terms = enrichment_hypergeometric(annot, relevant, dict_options)
  end

  merged_annotations = {}
  annot.each{|key, info|
    merged_annotations[key] = info[:dataset] + info[:signature]
  }
  [merged_annotations, terms]
end

.compare(a, b) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/MARQ/annotations.rb', line 100

def self.compare(a,b)
  case 
  when a[:pvalue] < b[:pvalue]
    -1 
  when a[:pvalue] > b[:pvalue]
    1 
  when a[:pvalue] == b[:pvalue]
    b[:score].abs <=> a[:score].abs
  end
end

.enrichment_hypergeometric(annotations, relevant, options) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/MARQ/annotations.rb', line 227

def self.enrichment_hypergeometric(annotations, relevant, options)
  dict_options = {
    :dict_options => {:low => 0, :hi => 0.5, :limit => 100000}
  }.merge(options)[:dict_options]
  positions = {}
  found_datasets = []

  dict = Dictionary::TF_IDF.new
  ranks.each_with_index{|experiment, rank|
    info = annotations[experiment]

    dataset_terms   = info[:dataset]
    signature_terms = info[:signature]
    
    dataset = exp2gds experiment

    terms = signature_terms
    terms += dataset_terms

    term_count = {}
    terms.each{|term|
      term_count[term] ||= 0
      term_count[term] += 1
    }
    dict.add(term_count)
  }
 
  best = dict.best(dict_options).keys

  terms = {}
  found_datasets = []
  annotations.each{|experiment, info|
    dataset_terms   = info[:dataset]
    signature_terms = info[:signature]

    dataset = exp2gds experiment

    signature_terms.each{|term|
      next if ! best.include? term
      terms[term] ||= {:relevant => 0, :total => 0}
      terms[term][:total]    += 1 
      terms[term][:relevant] += 1 if relevant.include? experiment
    }
    
    next if found_datasets.include? dataset
    found_datasets << dataset

    dataset_terms.each{|term|
      next if ! best.include? term
      terms[term] ||= {:relevant => 0, :total => 0}
      terms[term][:total]    += 1 
      terms[term][:relevant] += 1 if relevant.include? experiment
    }
  }
 

  total   = annotations.keys.length
  list    = relevant.length

  terms.each{|term, info|
    info[:pvalue] = Annotations.hypergeometric(total,info[:total],list, info[:relevant])
  }

  terms
end

.enrichment_rank(annotations, ranks, options = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/MARQ/annotations.rb', line 113

def self.enrichment_rank(annotations, ranks, options = {})
  dict_options = {
    :dict_options => {:low => 0, :hi => 0.5, :limit => 100000}
  }.merge(options)[:dict_options]
  positions = {}
  found_datasets = []

  dict = Dictionary::TF_IDF.new
  ranks.each_with_index{|experiment, rank|
    info = annotations[experiment]

    dataset_terms   = info[:dataset]
    signature_terms = info[:signature]
    
    dataset = exp2gds experiment

    terms = signature_terms
    terms += dataset_terms

    term_count = {}
    terms.each{|term|
      term_count[term] ||= 0
      term_count[term] += 1
    }
    dict.add(term_count)
  }
 
  best = dict.best(dict_options).keys

  found_datasets = []
  ranks.each_with_index{|experiment, rank|
    info = annotations[experiment]

    dataset_terms   = info[:dataset]
    signature_terms = info[:signature]

    dataset = exp2gds experiment

    terms = signature_terms
    
    if ! found_datasets.include? dataset
      terms += dataset_terms
      found_datasets << dataset
    end

    terms.uniq.each{|term|
      next if not best.include? term
      positions[term] ||= []
      positions[term] <<  rank
    }
  }
 
  scores = []


  sizes = {}
  RANK_SIZE_BINS.each{|size| sizes[size.to_i] = []}


  # For each term compute the rank score. Also, place it in the closest size
  # bin for the permutations.
  best.each_with_index{|term, pos|
    if positions[term]
      list = positions[term]

      # place it on the size bin 
      found = false
      sizes.keys.sort.each_with_index{|size,i|
        next if found
        if list.length < size
          found = true
          sizes[sizes.keys.sort[i-1]] << pos
        end
      }
      sizes[sizes.keys.sort.last] << pos if !found
      
      scores << Score::score(list, ranks.length, 0)[:score]
    else # it has no score
      scores << nil
    end
  }

  info = {}

  # Go through all the size bins, run the permutations and assign the pvalues
  # to all terms in the bin.
  sizes.keys.each{|size|
    next if size == 1
    next if sizes[size].empty?

    # This are the actual scores for the terms in the bin
    sub_list_scores = sizes[size].collect{|pos| scores[pos] || 0}

    # Compute the pvalues for all the terms in the bin. The size of the
    # permutation list is that of the bin
    pvalues = Score::pvalues(sub_list_scores, size, 0, ranks.length)

    # Save the information from the terms, score, hits, and pvalues.
    sizes[size].zip(pvalues).each{|p|
      pos = p[0]
      pvalue = p[1]
      score = scores[pos]
      next if score < 0

      term = best[pos]
      hits = positions[term].nil? ? 0 : positions[term].length 

      info[term] = {:score => score, :hits => hits, :pvalue => pvalue}
    }
  }

  info
end

.exp2gds(experiment) ⇒ Object



95
96
97
98
# File 'lib/MARQ/annotations.rb', line 95

def self.exp2gds(experiment)
  experiment =~ /(.*?):/
  $1
end