Module: Annotations

Defined in:
lib/MARQ/annotations.rb

Defined Under Namespace

Modules: Genes, 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)
@@terms_cache =
{}

Class Method Summary collapse

Class Method Details

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



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
353
# File 'lib/MARQ/annotations.rb', line 302

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

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

  scores.each{|experiment, info|
    dataset = experiment.match(/^(.*?): /)[1]
    name = $'.strip 
    
    case
    when side.nil?
      experiment_type = type
    when side == :direct && info[:score] >= 0 || side == :inverse && info[:score] < 0
      experiment_type = type + '_up'
    else
      experiment_type = type + '_down'
    end

    annot[experiment] = dataset_annotations(dataset, experiment_type, name)

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

  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

  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



104
105
106
107
108
109
110
111
112
113
# File 'lib/MARQ/annotations.rb', line 104

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

.dataset_annotations(dataset, type, experiment) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/MARQ/annotations.rb', line 288

def self.dataset_annotations(dataset, type, experiment)
  annotation_dir = File.join(MARQ.datadir, (MARQ::Dataset.is_GEO?(dataset) ? 'GEO' : 'CustomDS'), 'annotations')

  term_file =  File.join(annotation_dir, type, MARQ::Name.clean(dataset))
 
  if File.exist? term_file
    @@terms_cache[term_file] ||= YAML::load(File.open(term_file))
    terms = @@terms_cache[term_file] 
    {:dataset => (terms[:dataset] || []), :signature => (terms[experiment] || [])}
  else
    {:dataset =>  [], :signature => []}
  end
end

.enrichment_hypergeometric(annotations, relevant, options) ⇒ Object



220
221
222
223
224
225
226
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
# File 'lib/MARQ/annotations.rb', line 220

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



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
# File 'lib/MARQ/annotations.rb', line 117

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
    }
  }

  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.
  scores = []
  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)
    else # it has no score
      scores << nil
    end
  }

  info = {}
  sizes.each do |size, pos_list|
    next if size == 1
    next if pos_list.empty?

    size_info = {}
    pos_list.each do |pos|
      score = scores[pos]
      term  = best[pos]
      hits  = positions[term].nil? ? 0 : positions[term].length

      size_info[term] = {:score => score, :hits => hits}
    end

    null_scores     = Score.null_scores(size, 0)
    size_info       = Score.add_pvalues(size_info, null_scores)

    info.merge! size_info
  end


  info
end

.exp2gds(experiment) ⇒ Object



99
100
101
102
# File 'lib/MARQ/annotations.rb', line 99

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