Module: ViennaRna::Graphing::R

Defined in:
lib/vienna_rna/graphing/r.rb

Class Method Summary collapse

Class Method Details

.graph(&block) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/vienna_rna/graphing/r.rb', line 5

def graph(&block)
  begin
    (yield (r_instance = RinRuby.new)).tap { r_instance.close }
  rescue RuntimeError => e
    raise unless e.message == "Unsupported data type on R's end"
  end
end

.histogram(data, title: nil, x_label: "Bins", num_bins: false, bin_size: 1, x_arrow: false, relative: false, filename: false) ⇒ 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
# File 'lib/vienna_rna/graphing/r.rb', line 80

def histogram(data, title: nil, x_label: "Bins", num_bins: false, bin_size: 1, x_arrow: false, relative: false, filename: false)
  half     = bin_size / 2.0
  range    = Range.new((data.min - half).floor, (data.max + half).ceil)
  breaks   = num_bins ? num_bins : (range.min + half).step(range.max + half, bin_size).to_a
  
  graph do |r|
    r.assign("histogram.data", data)
    r.assign("histogram.breaks", breaks)
    
    r.eval("%s('%s', 6, 6)" % [
      writing_file?(filename) ? "pdf" : "quartz", 
      writing_file?(filename) ? filename : "Histogram", 
    ])
    
    r.eval "      hist(\n        histogram.data, \n        breaks   = histogram.breaks, \n        xlab     = \"\#{x_label}\", \n        main     = \"\#{title || 'Histogram'}\", \n        freq     = \#{relative ? 'F' : 'T'},\n        cex.main = 0.9,\n        cex.lab  = 0.9,\n        cex.axis = 0.9\n      )\n    STR\n    \n    r.eval(\"abline(v = \#{x_arrow}, lty = 'dashed')\") if x_arrow\n    \n    r.eval(\"dev.off()\") if writing_file?(filename)\n  end\nend\n"

.line_graph(data, title: nil, type: ?l, x_label: "Independent", y_label: "Dependent", filename: false) ⇒ Object



76
77
78
# File 'lib/vienna_rna/graphing/r.rb', line 76

def line_graph(data, title: nil, type: ?l, x_label: "Independent", y_label: "Dependent", filename: false)
  overlay([{ data: data }], title: title, type: type, x_label: x_label, y_label: y_label, legend: false, filename: filename)
end

.matrix_heatmap(x, y, z, title: nil, x_label: "Column index", y_label: "Row index", filename: false, num_colors: 64) ⇒ 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
# File 'lib/vienna_rna/graphing/r.rb', line 113

def matrix_heatmap(x, y, z, title: nil, x_label: "Column index", y_label: "Row index", filename: false, num_colors: 64)
  graph do |r|
    if r.pull("ifelse('Matrix' %in% rownames(installed.packages()), 1, -1)") > 0
      if forced_square = (x.max != y.max)
        x << [x, y].map(&:max).max
        y << [x, y].map(&:max).max
        z << 0
      end
      
      r.assign("matrix.i", x)
      r.assign("matrix.j", y)
      r.assign("matrix.x", z)
      r.eval "        require(\"Matrix\")\n        matrix.data <- sparseMatrix(\n        i      = matrix.i,\n        j      = matrix.j,\n        x      = matrix.x,\n        index1 = F\n        )\n      STR\n      \n      generate_graph(\"Heatmap\") do\n        <<-STR\n          filtered.values <- Filter(function(i) { is.finite(i) & i != 0 }, matrix.x)\n          print(apply(as.matrix(matrix.data), 2, rev))\n          print(c(sort(filtered.values)[2], max(filtered.values)))\n        \n          image(\n            x    = 1:max(c(dim(matrix.data)[[1]], dim(matrix.data)[[2]])), \n            y    = 1:max(c(dim(matrix.data)[[1]], dim(matrix.data)[[2]])), \n            z    = as.matrix(matrix.data),\n            col  = rev(heat.colors(\#{num_colors})),\n            zlim = \#{forced_square ? \"c(sort(filtered.values)[2], max(filtered.values))\" : \"c(min(filtered.values), max(filtered.values))\"},\n            xlab = \"\#{x_label} (1-indexed)\",\n            ylab = \"\#{y_label} (1-indexed)\"\n          )\n          title(\"\#{title || 'Matrix Heatmap'}\")\n        STR\n      end\n    else\n      puts \"Please install the Matrix package for R before using this function.\"\n    end\n  end\nend\n"

.overlay(data, title: nil, type: ?l, x_label: "Independent", y_label: "Dependent", legend: "topleft", filename: false) ⇒ Object



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vienna_rna/graphing/r.rb', line 13

def overlay(data, title: nil, type: ?l, x_label: "Independent", y_label: "Dependent", legend: "topleft", filename: false)
  # data: [{ data: [[x_0, y_0], ..., [x_n, y_n]], legend: "Line 1" }, ...]
  
  x_points = data.map { |hash| hash[:data].map(&:first) }
  y_points = data.map { |hash| hash[:data].map(&:last) }
  x_range  = Range.new(x_points.map(&:min).min.floor, x_points.map(&:max).max.ceil)
  y_range  = Range.new(y_points.map(&:min).min.floor, y_points.map(&:max).max.ceil)
  
  graph do |r|
    r.eval("%s('%s', 6, 6)" % [
      writing_file?(filename) ? "pdf" : "quartz", 
      writing_file?(filename) ? filename : "Graph", 
    ])
    
    r.assign("legend.titles", data.each_with_index.map { |hash, index| hash[:legend] || "Line #{index + 1}" })
    r.eval("line.colors <- rainbow(%d)" % data.size)
    r.eval("plot(0, 0, type = 'n', cex = .75, cex.axis = .9, xlab = '', ylab = '', xlim = c(%d, %d), ylim = c(%d, %d))" % [
      x_range.min, x_range.max, y_range.min, y_range.max
    ])
    
    data.each_with_index do |hash, index|
      r.assign("line_graph.x.%d" % index, x_points[index])
      r.assign("line_graph.y.%d" % index, y_points[index])
      
      r.eval "        lines(\n          line_graph.x.\#{index}, \n          line_graph.y.\#{index}, \n          col  = line.colors[\#{index + 1}],\n          type = \"\#{type}\",\n          pch  = \#{index}\n        )\n      STR\n    end\n    \n    r.eval <<-STR\n      title(\n        xlab     = \"\#{x_label}\", \n        ylab     = \"\#{y_label}\", \n        main     = \"\#{title || 'Line Graph'}\",\n        cex.main = .9,\n        cex.lab  = .9\n      )\n    STR\n      \n    if legend\n      r.eval <<-STR\n        legend(\n          \"\#{legend}\",\n          legend.titles,\n          bty = \"n\",\n          col = line.colors,\n          lty = rep(1, \#{data.size}),\n          pch = 0:\#{data.size},\n          cex = .9\n        )\n      STR\n    end\n    \n    r.eval(\"dev.off()\") if writing_file?(filename)\n  end\nend\n"

.roc(data, title = "", options = {}) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/vienna_rna/graphing/r.rb', line 159

def roc(data, title = "", options = {})
  # data = [[true_score_1, true_score_2, ...], [false_score_1, false_score_2, ...]]

  if R.pull("ifelse('ROCR' %in% rownames(installed.packages()), 1, -1)") > 0

  else
    puts "Please install the ROCR package for R before using this function."
  end

  # roc_curve = ROC.curve_points({ 1 => data[0], -1 => data[1] }.inject([]) { |data, (truth, values)| data.concat(values.map { |i| [i, truth] })})
  # area      = roc_curve.each_cons(2).inject(0) do |sum, (a, b)| 
  #   delta_x, delta_y = b[0] - a[0], b[1] - a[1]
  #   sum + (delta_x * delta_y / 2 + delta_x * [a[1], b[1]].min)
  # end
  
  # options.merge!(output: "file") if options[:filename]
  # options.merge!({ plot: { title: "%s %s %.4f" % [title, "AUC:", area] } })
    
  # plot([{ x: roc_curve.map(&:first), y: roc_curve.map(&:last), style: "lines" }], options)
end