Module: RankProduct

Defined in:
lib/MARQ/rankproduct.rb

Class Method Summary collapse

Class Method Details

.permutations(num_signatures, num = 1000) ⇒ Object



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

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



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/MARQ/rankproduct.rb', line 66

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



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
127
128
129
130
131
132
133
134
# File 'lib/MARQ/rankproduct.rb', line 80

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)

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

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

  gene_ranks.delete_if{|code, positions| positions.length != signatures.uniq.length}
  
  scores = score(gene_ranks, sizes)
  num_permutations = 50000

  permutation_scores = permutations(sizes.length, num_permutations)

  permutation_scores = permutation_scores.sort


  results = {}
  scores.each{|gene, score|
    pos = permutation_scores.count_smaller(score)
    results[gene] = [score, pos.to_f / num_permutations]
  }


  num_genes = results.length
  results.sort{|a,b|
    a[1][0] <=> b[1][0]
  }.each_with_index{|p,i|
    gene = p[0]
    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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/MARQ/rankproduct.rb', line 4

def self.ranks(dataset, experiment , from_FC = false, invert = false)
  path = MARQ.dataset_path(dataset)
  codes = Open.read(path + '.codes').collect{|line| line.strip}

  experiments = Open.read(path + '.experiments').collect{|line| line.strip}
  field = experiments.index experiment.strip

  if from_FC
    ratios = Open.read(path + '.logratios').collect{|line| 
      value = line.strip.split("\t")[field]
      case
      when value == "NA"
        nil

      # To sort decreasingly we change sign by default
      when invert
        value.to_f
      else
        - value.to_f
      end
    }
    Hash[*codes.zip(ratios).sort{|a,b| b[1] <=> a[1]}.collect{|p| p[0]}.zip((1..codes.length).to_a).flatten]
  else
    orders = Open.read(path + '.orders').collect{|line| 
      value = line.strip.split("\t")[field]
      case
      when value == "NA"
        nil
      when invert
        codes.length - line.strip.split("\t")[field].to_i + 1
      else
        line.strip.split("\t")[field].to_i
      end

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

.score(gene_ranks, signature_sizes) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/MARQ/rankproduct.rb', line 43

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]}.
      inject(0){|acc, v| acc += v  }
  }
  scores
end