Module: RankProduct

Defined in:
lib/MARQ/rankproduct.rb

Class Method Summary collapse

Class Method Details

.permutations(num_signatures, num = 1000) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/MARQ/rankproduct.rb', line 40

def self.permutations(num_signatures, num = 1000)
  scores = []
  num.times{
     value = 0
     num_signatures.times{|size_and_log| 
       value += Math::log(rand)
     } 
     scores << value
  }
  scores
end

.permutations_full(signature_sizes) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/MARQ/rankproduct.rb', line 52

def self.permutations_full(signature_sizes)
  gene_ranks = {}
  signature_sizes.each{|size|
    (1..size).to_a.shuffle.each_with_index{|gene, pos|
      gene_ranks[gene] ||= []
      gene_ranks[gene] << pos + 1
    }
  }
  gene_ranks.delete_if{|code, positions| positions.length != signature_sizes.length}

  scores = score(gene_ranks, signature_sizes)
  scores.values
end

.rankproduct(signatures, options = {}) ⇒ Object



66
67
68
69
70
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/MARQ/rankproduct.rb', line 66

def self.rankproduct(signatures, options = {})
  invert, from_FC, cross_platform = {
    :invert => [],
    :from_FC => false,
    :cross_platform => false,
  }.merge(options).values_at(:invert, :from_FC, :cross_platform)

  # Gather gene ranks from signatures
  ranks = {}
  signatures.each{|signature|
    dataset, experiment = signature.match(/^([^\:]*): (.*)/).values_at(1,2)
    dataset = dataset + '_cross_platform' if cross_platform
    ranks[signature] = self.ranks(dataset, experiment, from_FC, invert.include?(signature))
  }

  # Invert the hash, from signature keys to gene keys
  gene_ranks = {}
  sizes = []
  ranks.each{|signature, orders|
    sizes << orders.length
    orders.each{|code, position|
      next if position.nil?
      gene_ranks[code] ||= []
      gene_ranks[code] << position
    }
  }

  # Remove incomplete genes
  gene_ranks.delete_if{|code, positions| positions.length != signatures.uniq.length}
  
  # Compute scores
  scores = score(gene_ranks, sizes)

  # Compute permutations
  num_permutations = 50000
  permutation_scores = permutations(sizes.length, num_permutations)
  permutation_scores = permutation_scores.sort


  # Compute p-values from permutations
  results = {}
  scores.each {|gene, score|
    pos = permutation_scores.count_smaller(score)
    results[gene] = [score, pos.to_f / num_permutations]
  }

  # Complete the information with pfp
  num_genes = results.length
  results.sort {|a,b|
    a[1][0] <=> b[1][0]
  }.each_with_index{|p,i|
    info = p[1]
    pvalue = info[1]

    pfp  = pvalue * num_genes / (i + 1)
    info << pfp

  }
     
  results
end

.ranks(dataset, experiment, from_FC = false, invert = false) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/MARQ/rankproduct.rb', line 4

def self.ranks(dataset, experiment , from_FC = false, invert = false)
  codes = MARQ::Dataset.codes(dataset)
  
  if from_FC 
    ratios    = MARQ::Dataset.logratios(dataset)[experiment.strip]
    sorted_genes = codes.zip(ratios).
      reject  {|p| p[1].nil? }.
      sort_by {|p| p[1] }.
      collect {|p| p[0] }
    sorted_genes.reverse! unless invert
    ranks  = Hash[*sorted_genes.zip((1..sorted_genes.length).to_a).flatten]
    (codes - sorted_genes).each {|gene| ranks[gene] = nil}
  else
    orders = MARQ::Dataset.orders(dataset)[experiment.strip]

    if invert
      num_genes = codes.length + 1
      orders.collect! {|pos| pos.nil? ? nil : num_genes - pos }
    end

    ranks = Hash[*codes.zip(orders).flatten]
  end
  ranks
end

.score(gene_ranks, signature_sizes) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/MARQ/rankproduct.rb', line 29

def self.score(gene_ranks, signature_sizes)
  scores = {}
  log_sizes = signature_sizes.collect{|size| Math::log(size)}
  gene_ranks.each{|gene, positions|
    scores[gene] = positions.zip(log_sizes).
      collect{|p| Math::log(p[0]) - p[1]}.    # Take log and substract from size (normalize)
      inject(0){|acc, v| acc += v  }
  }
  scores
end