Module: ViennaRna::Graphing::R
- Defined in:
- lib/vienna_rna/modules/graphing.rb
Class Method Summary collapse
- .graph(&block) ⇒ Object
- .histogram(data, title: nil, x_label: "Bins", bin_size: 1, relative: false, filename: false) ⇒ Object
- .line_graph(data, title: nil, type: "l", x_label: "Independent", y_label: "Dependent", filename: false) ⇒ Object
- .roc(data, title = "", options = {}) ⇒ Object
Class Method Details
.graph(&block) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/vienna_rna/modules/graphing.rb', line 5 def graph(&block) begin (yield (r_instance = RinRuby.new)).tap { r_instance.close } rescue RuntimeError => e raise unless e. == "Unsupported data type on R's end" end end |
.histogram(data, title: nil, x_label: "Bins", bin_size: 1, relative: false, filename: false) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/vienna_rna/modules/graphing.rb', line 33 def histogram(data, title: nil, x_label: "Bins", bin_size: 1, relative: false, filename: false) half = bin_size / 2.0 range = Range.new((data.min - half).floor, (data.max + half).ceil) breaks = (range.min + half).step(range.max + half, bin_size).to_a graph do |r| r.assign("histogram.data", data) r.assign("histogram.breaks", breaks) if filename && (filename = filename.end_with?(".pdf") ? filename : filename + ".pdf") r.eval <<-STR pdf("#{filename}", 6, 6) hist(histogram.data, breaks = histogram.breaks, xlab = "#{x_label} (width: #{bin_size})", main = "#{title || 'Histogram'}", freq = #{relative ? 'FALSE' : 'TRUE'}) dev.off() STR else r.eval <<-STR quartz("Histogram", 6, 6) hist(histogram.data, breaks = histogram.breaks, xlab = "#{x_label} (width: #{bin_size})", main = "#{title || 'Histogram'}", freq = #{relative ? 'FALSE' : 'TRUE'}) STR end end end |
.line_graph(data, title: nil, type: "l", x_label: "Independent", y_label: "Dependent", filename: false) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/vienna_rna/modules/graphing.rb', line 13 def line_graph(data, title: nil, type: "l", x_label: "Independent", y_label: "Dependent", filename: false) graph do |r| r.assign("line_graph.x", data.map(&:first)) r.assign("line_graph.y", data.map(&:last)) if filename && (filename = filename.end_with?(".pdf") ? filename : filename + ".pdf") r.eval <<-STR pdf("#{filename}", 6, 6) plot(line_graph.x, line_graph.y, xlab = "#{x_label}", ylab = "#{y_label}", main = "#{title || 'Line Graph'}", type = "#{type}") dev.off() STR else r.eval <<-STR quartz("Histogram", 6, 6) plot(line_graph.x, line_graph.y, xlab = "#{x_label}", ylab = "#{y_label}", main = "#{title || 'Line Graph'}", type = "#{type}") STR end end end |
.roc(data, title = "", options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/vienna_rna/modules/graphing.rb', line 57 def roc(data, title = "", = {}) # 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 |