Module: Heatmap

Defined in:
lib/rbbt/plots/heatmap.rb

Class Method Summary collapse

Class Method Details

.heatmap(values, filename, options = {}) ⇒ Object



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
42
43
# File 'lib/rbbt/plots/heatmap.rb', line 5

def self.heatmap(values, filename, options = {})
  scale, take_log, add_to_height, colors = Misc.process_options options, 
    :scale, :take_log, :add_to_height, :colors

  width = 200 + (values.fields.length * 16)
  height = 200 + (values.length * 16)
  size = [width, height].max
  size = [size, 10000].min

  heatmap_script = <<-EOF 
  #{ take_log ? "data <- log(data)" : ""}
      my.hclust <- function(d){ hclust(d, method="ward") }; 
      my.hclust <- function(d){ hclust(d) }; 
      rbbt.png_plot(
        '#{filename}', 
  #{ size }, 
  #{ (defined?(add_to_height) and not add_to_height.nil?) ? (size + (add_to_height * 16 * [1, (height.to_f / width)].max).to_i) : size }, 
        'heatmap(as.matrix(data),
  #{
  case scale.to_s
  when "true", 'row'
    'scale="row",' 
  when 'column' 
    'scale="column",' 
  when "none", ""
    'scale="none",'
  end
  }
  #{colors.nil? ? "" : "ColSideColors=#{colors},"} 
        hclustfun=my.hclust, 
        )',
        pointsize=12, type='cairo', res=150)
      data = NULL;
  EOF

  values.R heatmap_script
  
  filename
end

.heatmap2(values, filename, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rbbt/plots/heatmap.rb', line 45

def self.heatmap2(values, filename, options = {})
  scale, take_log, add_to_height, colors = Misc.process_options options, 
    :scale, :take_log, :add_to_height, :colors

  width = 1200 + (values.fields.length * 100)
  height = 1000 + (values.length * 50)
  size = [width, height].max
  size = [size, 20000].min
  width = [size, width].min
  height = [size, height].min

  take_log = take_log ? "TRUE" : "FALSE"
  heatmap_script = <<-EOF 
library(ggplot2);
rbbt.heatmap('#{filename}', #{ width }, #{ height }, data, take_log=#{take_log});
  EOF

  values.R heatmap_script
  
  filename
end

.heatmap3(values, filename, options = {}) ⇒ Object



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
# File 'lib/rbbt/plots/heatmap.rb', line 67

def self.heatmap3(values, filename, options = {})
  scale, take_log, add_to_height, colors = Misc.process_options options, 
    :scale, :take_log, :add_to_height, :colors

  width = 1200 + (values.fields.length * 100)
  height = 1000 + (values.length * 50)
  size = [width, height].max
  size = [size, 2000].min
  width = [size, width].min
  height = [size, height].min

  take_log = take_log ? "TRUE" : "FALSE"

  map = options.delete :map

  if map
    values = values.slice(map.keys)
    clab = TSV.setup(map.keys, :type => :list, :fields => [], :key_field => map.key_field)

    options[:keys] = []
    options[:colors] = []
    map.fields.each do |field|
      color = Colorize.tsv map.slice(field)
      clab.add_field field do |k, values|
        color[k].to_rgb
      end
      options[:keys] << "" unless options[:keys].empty?
      options[:keys].concat map.values.uniq
      options[:colors] << "#000" unless options[:colors].empty?
      options[:colors].concat color.values_at(*map.keys).collect{|c| c.to_rgb}.uniq
    end

    if options[:keys].length > 20 
      options.delete :keys
      options.delete :colors
    end

    options[:ColSideColors] = clab
  end

  other_params = ", " << options.collect{|k,v| [R.ruby2R(k), R.ruby2R(v)] * "="} * ", " if options.any?

  heatmap_script = <<-EOF 
library(ggplot2, quietly = TRUE, warn.conflicts = TRUE);
source('#{Rbbt.share.R["heatmap.3.R"].find}');

rbbt.heatmap.3('#{filename}', #{ width }, #{ height }, data, take_log=#{take_log}#{other_params});
  EOF

  values.R heatmap_script
  
  filename
end