Module: ViennaRna::Graphing::R

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

Class Method Summary collapse

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 { ap 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", bin_size: 1, filename: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vienna_rna/modules/graphing.rb', line 17

def histogram(data, title: nil, x_label: "Bins", bin_size: 1, 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 "        pdf(\"\#{filename}\", 6, 6)\n        hist(histogram.data, breaks = histogram.breaks, xlab = \"\#{x_label} (width: \#{bin_size})\", main = \"\#{title || 'Histogram'}\")\n        dev.off()\n      STR\n    else\n      r.eval <<-STR\n        quartz(\"Histogram\", 6, 6)\n        hist(histogram.data, breaks = histogram.breaks, xlab = \"\#{x_label} (width: \#{bin_size})\", main = \"\#{title || 'Histogram'}\")\n      STR\n    end\n  end\nend\n"

.pdf(string) ⇒ Object



13
14
15
# File 'lib/vienna_rna/modules/graphing.rb', line 13

def pdf(string)

end

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vienna_rna/modules/graphing.rb', line 41

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