vienna_rna 0.4.4 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d9804f2f40ec0ae56690e373c7f9a3e6e37e2c9
4
- data.tar.gz: 8e0513d28b214658a90f1c0000aa09457c0dc568
3
+ metadata.gz: 11244542ad92a82ba2d504e21688cf47a58d3c2b
4
+ data.tar.gz: aed5057e2cb136a593f73b30e6ea178f58f0815b
5
5
  SHA512:
6
- metadata.gz: 2ca3480228c5c2356ef78c730479cc9c9b9e76761ffcb58e32f294b4910e0f8afc4c66b0493cdc4c8de521470ed09495ff6b29abd4af74a8d987e27ffcf3423c
7
- data.tar.gz: 6c64233272aa7cf6188a467f65e965bd589f4a15a122d8730ad3ee430ee851bb75c3152fa3584c19a03e300900cec8bf6f00050d4bbf3eaca195e9254d71dcc1
6
+ metadata.gz: 0472640b877355124a29e88f61293b5fb33fb1ba59badbf1dbbbf25028e40cbd0f773904d23ad55288468b84e9a7c56a1dc3377832ac3862e10c12a2592cd4a7
7
+ data.tar.gz: e1930c78ac509d2fde5e52fbcffdf25cfc3ebe28e52573196abcbf71d750cd6afa9c74c23b4ff1f882ec488453a31f6fa05f588c3b2cfbc14fed24a0d5e0d415
@@ -0,0 +1,177 @@
1
+ module ViennaRna
2
+ module Graphing
3
+ module R
4
+ class << self
5
+ def graph(&block)
6
+ begin
7
+ (yield r_instance = RinRuby.new).tap { ap r_instance.close }
8
+ rescue RuntimeError => e
9
+ raise unless e.message == "Unsupported data type on R's end"
10
+ end
11
+ end
12
+
13
+ def pdf(string)
14
+
15
+ end
16
+
17
+ def histogram(data, title: nil, x_label: "Bins", bin_size: 1, filename: false)
18
+ half = bin_size / 2.0
19
+ range = Range.new((data.min - half).floor, (data.max + half).ceil)
20
+ breaks = (range.min + half).step(range.max + half, bin_size).to_a
21
+
22
+ graph do |r|
23
+ r.assign("histogram.data", data)
24
+ r.assign("histogram.breaks", breaks)
25
+
26
+ if filename && (filename = filename.end_with?(".pdf") ? filename : filename + ".pdf")
27
+ r.eval <<-STR
28
+ pdf("#{filename}", 6, 6)
29
+ hist(histogram.data, breaks = histogram.breaks, xlab = "#{x_label} (width: #{bin_size})", main = "#{title || 'Histogram'}")
30
+ dev.off()
31
+ STR
32
+ else
33
+ r.eval <<-STR
34
+ quartz("Histogram", 6, 6)
35
+ hist(histogram.data, breaks = histogram.breaks, xlab = "#{x_label} (width: #{bin_size})", main = "#{title || 'Histogram'}")
36
+ STR
37
+ end
38
+ end
39
+ end
40
+
41
+ def roc(data, title = "", options = {})
42
+ # data = [[true_score_1, true_score_2, ...], [false_score_1, false_score_2, ...]]
43
+
44
+ if R.pull("ifelse('ROCR' %in% rownames(installed.packages()), 1, -1)") > 0
45
+
46
+ else
47
+ puts "Please install the ROCR package for R before using this function."
48
+ end
49
+
50
+ # roc_curve = ROC.curve_points({ 1 => data[0], -1 => data[1] }.inject([]) { |data, (truth, values)| data.concat(values.map { |i| [i, truth] })})
51
+ # area = roc_curve.each_cons(2).inject(0) do |sum, (a, b)|
52
+ # delta_x, delta_y = b[0] - a[0], b[1] - a[1]
53
+ # sum + (delta_x * delta_y / 2 + delta_x * [a[1], b[1]].min)
54
+ # end
55
+
56
+ # options.merge!(output: "file") if options[:filename]
57
+ # options.merge!({ plot: { title: "%s %s %.4f" % [title, "AUC:", area] } })
58
+
59
+ # plot([{ x: roc_curve.map(&:first), y: roc_curve.map(&:last), style: "lines" }], options)
60
+ end
61
+
62
+ # def quick_plot(data, title = "", options = {})
63
+ # quick_overlay([{ data: data }], title, options)
64
+ # end
65
+
66
+ # def quick_overlay(data, title = "", options = {})
67
+ # # [{ data: [[x_0, y_0], [x_1, y_1], ...], label: "Label" }, { data: [[x_0, y_0], [x_1, y_1], ...] }]
68
+ # options[:plot] = ((options[:plot] || {}).merge(title: title))
69
+ # options.merge!(output: "file") if options[:filename]
70
+
71
+ # plot(data.map { |hash| { title: hash[:label], x: hash[:data].map(&:first), y: hash[:data].map(&:last), style: "linespoints" }.merge(hash[:options] || {}) }, options)
72
+ # end
73
+ end
74
+ end
75
+
76
+ module Gnuplot
77
+ class << self
78
+ def plot(data, options = {})
79
+ Gnuplot.open do |gnuplot|
80
+ Gnuplot::Plot.new(gnuplot) do |plot|
81
+ plot.autoscale
82
+
83
+ case options[:output]
84
+ when /file/i then
85
+ plot.output(options[:filename])
86
+ plot.terminal("png size %s" % (options[:dimensions] || "800,600"))
87
+ end
88
+
89
+ (options[:plot] || {}).keys.each do |option|
90
+ plot.send(option, options[:plot][option])
91
+ end
92
+
93
+ plot.data = data.map do |data_hash|
94
+ Gnuplot::DataSet.new([data_hash[:x], data_hash[:y]]) do |dataset|
95
+ dataset.with = data_hash[:style] || "points"
96
+ dataset.linecolor = "rgb '#{data_hash[:color]}'" if data_hash[:color]
97
+
98
+ data_hash[:title] ? dataset.title = data_hash[:title] : dataset.notitle
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ def splot(data, options = {})
106
+ # [[x_1, y_1, z_1], [x_2, y_2, z_2], ...]
107
+ orthogonal_data = data.inject([[], [], []]) { |array, list| array.zip(list).map { |a, e| a << e } }
108
+
109
+ Gnuplot.open do |gnuplot|
110
+ Gnuplot::SPlot.new(gnuplot) do |plot|
111
+ plot.autoscale
112
+
113
+ case options[:output]
114
+ when /file/i then
115
+ plot.output(options[:filename])
116
+ plot.terminal("png size 800,600")
117
+ end
118
+
119
+ (options[:plot] || {}).keys.each do |option|
120
+ plot.send(option, options[:plot][option])
121
+ end
122
+
123
+ plot.data = [
124
+ Gnuplot::DataSet.new(orthogonal_data) do |dataset|
125
+ dataset.with = options[:style] || "lines"
126
+ end
127
+ ]
128
+ end
129
+ end
130
+ end
131
+
132
+ def histogram(data, title = "", options = {})
133
+ bin_size = options.delete(:bin_size) || 1
134
+ half = bin_size / 2.0
135
+ range = Range.new((data.min - half).floor, (data.max + half).ceil)
136
+ groups = (range.min + half).step(range.max, bin_size).map { |x| [x, data.count { |i| i >= x - half && i < x + half }] }
137
+
138
+ options.merge!(output: "file") if options[:filename]
139
+ options[:plot] = (options[:plot] || {}).merge({
140
+ title: title,
141
+ yrange: "[0:#{groups.map(&:last).max * 1.1}]",
142
+ xtics: "#{[bin_size, 5].max}",
143
+ style: "fill solid 0.5 border"
144
+ })
145
+
146
+ plot([{ x: groups.map(&:first), y: groups.map(&:last), style: "boxes" }], options)
147
+ end
148
+
149
+ def roc(data, title = "", options = {})
150
+ # data = [[true_score_1, true_score_2, ...], [false_score_1, false_score_2, ...]]~
151
+ roc_curve = ROC.curve_points({ 1 => data[0], -1 => data[1] }.inject([]) { |data, (truth, values)| data.concat(values.map { |i| [i, truth] })})
152
+ area = roc_curve.each_cons(2).inject(0) do |sum, (a, b)|
153
+ delta_x, delta_y = b[0] - a[0], b[1] - a[1]
154
+ sum + (delta_x * delta_y / 2 + delta_x * [a[1], b[1]].min)
155
+ end
156
+
157
+ options.merge!(output: "file") if options[:filename]
158
+ options.merge!({ plot: { title: "%s %s %.4f" % [title, "AUC:", area] } })
159
+
160
+ plot([{ x: roc_curve.map(&:first), y: roc_curve.map(&:last), style: "lines" }], options)
161
+ end
162
+
163
+ def quick_plot(data, title = "", options = {})
164
+ quick_overlay([{ data: data }], title, options)
165
+ end
166
+
167
+ def quick_overlay(data, title = "", options = {})
168
+ # [{ data: [[x_0, y_0], [x_1, y_1], ...], label: "Label" }, { data: [[x_0, y_0], [x_1, y_1], ...] }]
169
+ options[:plot] = ((options[:plot] || {}).merge(title: title))
170
+ options.merge!(output: "file") if options[:filename]
171
+
172
+ plot(data.map { |hash| { title: hash[:label], x: hash[:data].map(&:first), y: hash[:data].map(&:last), style: "linespoints" }.merge(hash[:options] || {}) }, options)
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
@@ -63,7 +63,11 @@ module ViennaRna
63
63
  end
64
64
 
65
65
  def run(module_name, options = {})
66
- ViennaRna.const_missing("#{module_name}".camelize).run(self, options)
66
+ if rna_module = ViennaRna.const_missing("#{module_name}".camelize)
67
+ rna_module.run(self, options)
68
+ else
69
+ raise ArgumentError.new("#{module_name} can't be resolved as an executable")
70
+ end
67
71
  end
68
72
 
69
73
  private
@@ -29,103 +29,6 @@ module ViennaRna
29
29
 
30
30
  ((x_matrix.transpose * x_matrix).inverse * x_matrix.transpose * y_matrix).transpose.to_a[0]
31
31
  end
32
-
33
- def plot(data, options = {})
34
- Gnuplot.open do |gnuplot|
35
- Gnuplot::Plot.new(gnuplot) do |plot|
36
- plot.autoscale
37
-
38
- case options[:output]
39
- when /file/i then
40
- plot.output(options[:filename])
41
- plot.terminal("png size %s" % (options[:dimensions] || "800,600"))
42
- end
43
-
44
- (options[:plot] || {}).keys.each do |option|
45
- plot.send(option, options[:plot][option])
46
- end
47
-
48
- plot.data = data.map do |data_hash|
49
- Gnuplot::DataSet.new([data_hash[:x], data_hash[:y]]) do |dataset|
50
- dataset.with = data_hash[:style] || "points"
51
- dataset.linecolor = "rgb '#{data_hash[:color]}'" if data_hash[:color]
52
-
53
- data_hash[:title] ? dataset.title = data_hash[:title] : dataset.notitle
54
- end
55
- end
56
- end
57
- end
58
- end
59
-
60
- def splot(data, options = {})
61
- # [[x_1, y_1, z_1], [x_2, y_2, z_2], ...]
62
- orthogonal_data = data.inject([[], [], []]) { |array, list| array.zip(list).map { |a, e| a << e } }
63
-
64
- Gnuplot.open do |gnuplot|
65
- Gnuplot::SPlot.new(gnuplot) do |plot|
66
- plot.autoscale
67
-
68
- case options[:output]
69
- when /file/i then
70
- plot.output(options[:filename])
71
- plot.terminal("png size 800,600")
72
- end
73
-
74
- (options[:plot] || {}).keys.each do |option|
75
- plot.send(option, options[:plot][option])
76
- end
77
-
78
- plot.data = [
79
- Gnuplot::DataSet.new(orthogonal_data) do |dataset|
80
- dataset.with = options[:style] || "lines"
81
- end
82
- ]
83
- end
84
- end
85
- end
86
-
87
- def histogram(data, title = "", options = {})
88
- bin_size = options.delete(:bin_size) || 1
89
- half = bin_size / 2.0
90
- range = Range.new((data.min - half).floor, (data.max + half).ceil)
91
- groups = (range.min + half).step(range.max, bin_size).map { |x| [x, data.count { |i| i >= x - half && i < x + half }] }
92
-
93
- options.merge!(output: "file") if options[:filename]
94
- options[:plot] = (options[:plot] || {}).merge({
95
- title: title,
96
- yrange: "[0:#{groups.map(&:last).max * 1.1}]",
97
- xtics: "#{[bin_size, 5].max}",
98
- style: "fill solid 0.5 border"
99
- })
100
-
101
- plot([{ x: groups.map(&:first), y: groups.map(&:last), style: "boxes" }], options)
102
- end
103
-
104
- def roc(data, title = "", options = {})
105
- # data = [[true_score_1, true_score_2, ...], [false_score_1, false_score_2, ...]]~
106
- roc_curve = ROC.curve_points({ 1 => data[0], -1 => data[1] }.inject([]) { |data, (truth, values)| data.concat(values.map { |i| [i, truth] })})
107
- area = roc_curve.each_cons(2).inject(0) do |sum, (a, b)|
108
- delta_x, delta_y = b[0] - a[0], b[1] - a[1]
109
- sum + (delta_x * delta_y / 2 + delta_x * [a[1], b[1]].min)
110
- end
111
-
112
- options.merge!(output: "file") if options[:filename]
113
- options.merge!({ plot: { title: "%s %s %.4f" % [title, "AUC:", area] } })
114
-
115
- plot([{ x: roc_curve.map(&:first), y: roc_curve.map(&:last), style: "lines" }], options)
116
- end
117
-
118
- def quick_plot(data, title = "", options = {})
119
- quick_overlay([{ data: data }], title, options)
120
- end
121
-
122
- def quick_overlay(data, title = "", options = {})
123
- # [{ data: [[x_0, y_0], [x_1, y_1], ...], label: "Label" }, { data: [[x_0, y_0], [x_1, y_1], ...] }]
124
- options[:plot] = ((options[:plot] || {}).merge(title: title))
125
- options.merge!(output: "file") if options[:filename]
126
-
127
- plot(data.map { |hash| { title: hash[:label], x: hash[:data].map(&:first), y: hash[:data].map(&:last), style: "linespoints" }.merge(hash[:options] || {}) }, options)
128
- end
129
32
  end
130
33
  end
131
34
  end
@@ -50,7 +50,7 @@ module ViennaRna
50
50
  end
51
51
 
52
52
  def quick_plot(options = {})
53
- ViennaRna::Utils.quick_plot(
53
+ ViennaRna::Graphing::Gnuplot.quick_plot(
54
54
  k_p_points,
55
55
  options[:title] || "%s\\n%s\\n%s" % [self.class.name, data.seq, data.safe_structure],
56
56
  options
@@ -58,7 +58,7 @@ module ViennaRna
58
58
  end
59
59
 
60
60
  def inspect
61
- "#<ViennaRna::#{self.class.name} #{data.inspect}>"
61
+ "#<#{self.class.name} #{data.inspect}>"
62
62
  end
63
63
  end
64
64
  end
data/lib/vienna_rna.rb CHANGED
@@ -3,11 +3,14 @@ require "bio"
3
3
  require "shuffle"
4
4
  require "matrix"
5
5
  require "gnuplot"
6
+ require "rinruby"
6
7
  require "rroc"
7
8
  require "active_support/inflector"
8
9
  require "active_support/core_ext/class"
9
10
  require "active_support/core_ext/module/aliasing"
10
11
 
12
+ begin; R.quit; rescue IOError; end
13
+
11
14
  module ViennaRna
12
15
  @debug = true
13
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vienna_rna
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Senter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-13 00:00:00.000000000 Z
11
+ date: 2013-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bio
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: 2.5.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rinruby
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.3
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.3
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rroc
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +108,7 @@ files:
94
108
  - lib/vienna_rna/modules/ffthairpin.rb
95
109
  - lib/vienna_rna/modules/fftmultiloop.rb
96
110
  - lib/vienna_rna/modules/fold.rb
111
+ - lib/vienna_rna/modules/graphing.rb
97
112
  - lib/vienna_rna/modules/heat.rb
98
113
  - lib/vienna_rna/modules/parser.rb
99
114
  - lib/vienna_rna/modules/rna.rb