vienna_rna 0.7.0 → 0.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 80d4f6159bca186181c792f0a09d696db387a4e3
4
- data.tar.gz: 50bc4db672365259c1c25f78659b268e5d3c60c6
3
+ metadata.gz: 99cbd2a9afe1fb15daae72432caf558589d780f9
4
+ data.tar.gz: fbb4a27a788983898147fbb67bcbf91cae0cbaed
5
5
  SHA512:
6
- metadata.gz: e534a3181b7c41f6536a7e49ea2458d015944f1c7d6f0c66020839004a28c33d0004d649f237c1bb82d8bf8ec17bce7fd8d58f516307a58d0dc48aba2ee98d3c
7
- data.tar.gz: c16e11c3a1e79b2b909692b173b90bf2a20568823376d270859fcbd5133ba09d751d409531ae5c8b1d9c4f44d1f6aced99148791ab98b9690cef5dbf511e73ee
6
+ metadata.gz: 17f72d39b411f7ad5b19aaa60d11e91e392a86eae84712f95c804ce121648f55b85adbb904bf536ec137b590707f70d6b66b4a85cdf501501e0e68b8286f224f
7
+ data.tar.gz: 550bb20dbbc67f04cec01471db211378aa50e5784296c603f1871e2ab60d3bda6f02e2924d97cba4cbd39ab5d4b268392aa2634a55b1758e089187b3252e7ce1
@@ -25,12 +25,13 @@ module ViennaRna
25
25
  new(data).run(flags)
26
26
  end
27
27
 
28
- def bootstrap(data, output)
28
+ def bootstrap(data: nil, output: "")
29
29
  new(data).tap do |object|
30
- object.instance_variable_set(:@response, output)
30
+ object.instance_variable_set(:@response, File.exist?(output) ? File.read(output).chomp : output)
31
31
  end
32
32
  end
33
33
 
34
+ # Time to redo batch stuff.
34
35
  def batch(fastas = [])
35
36
  ViennaRna::Batch.new(self, fastas).tap do |batch|
36
37
  if const_defined?(:Batch)
@@ -69,6 +70,7 @@ module ViennaRna
69
70
  when *(1..3).map { |i| [String] * i } then Rna.init_from_string(*data)
70
71
  when [Hash] then Rna.init_from_hash(*data)
71
72
  when [Array] then Rna.init_from_array(*data)
73
+ when [NilClass] then Rna.placeholder
72
74
  else raise TypeError.new("Unsupported ViennaRna::Rna#initialize format: #{data}")
73
75
  end
74
76
  end
@@ -0,0 +1,63 @@
1
+ module ViennaRna
2
+ class EnergyGrid2d < Base
3
+ include Enumerable
4
+
5
+ def self.inherited(subclass)
6
+ subclass.class_eval { prepend EnergyGrid2dWrapper }
7
+ end
8
+
9
+ module EnergyGrid2dWrapper
10
+ def distribution
11
+ super.map { |row| Row2d.new(*row) }.select { |row| row.p > 0 }.sort
12
+ end
13
+ end
14
+
15
+ class Row2d
16
+ attr_reader :i, :j, :p, :ensemble
17
+
18
+ def initialize(i, j, p, ensemble)
19
+ @i, @j, @p, @ensemble = i.to_i, j.to_i, BigDecimal.new(p.to_s), BigDecimal.new(ensemble.to_s)
20
+ end
21
+
22
+ def position
23
+ [i, j]
24
+ end
25
+
26
+ def <=>(other_row)
27
+ i == other_row.i ? j <=> other_row.j : i <=> other_row.i
28
+ end
29
+
30
+ def inspect
31
+ "#<Row2d (%d, %d), p: %+f, ensemble: %+f>" % [i, j, p, ensemble]
32
+ end
33
+ end
34
+
35
+ def self.aligned_distributions(*energy_grids)
36
+ point_set = set_of_points(*energy_grids)
37
+
38
+ energy_grids.map do |grid|
39
+ (grid.distribution + (point_set - grid.map(&:position)).map { |i, j| Row2d.new(i, j, 0, Float::INFINITY) }).sort
40
+ end
41
+ end
42
+
43
+ def self.set_of_points(*energy_grids)
44
+ energy_grids.inject([]) { |list, grid| list + grid.map(&:position) }.uniq.sort
45
+ end
46
+
47
+ def each(&block)
48
+ distribution.each(&block)
49
+ end
50
+
51
+ def quick_plot(num_colors: 8)
52
+ Graphing::R.matrix_heatmap(
53
+ distribution.map(&:i),
54
+ distribution.map(&:j),
55
+ distribution.map { |row| Math.log(row.p) },
56
+ title: "#{self.class.name} Matrix Heatmap",
57
+ x_label: "Distance from structure 2",
58
+ y_label: "Distance from structure 1",
59
+ num_colors: num_colors
60
+ )
61
+ end
62
+ end
63
+ end
@@ -1,8 +1,6 @@
1
1
  module ViennaRna
2
- class Fftbor2d < Base
2
+ class Fftbor2d < EnergyGrid2d
3
3
  BASE_FLAGS = {
4
- E: "/usr/local/bin/rna_turner1999.par",
5
- P: 8,
6
4
  S: :empty
7
5
  }
8
6
 
@@ -19,10 +17,7 @@ module ViennaRna
19
17
  end
20
18
 
21
19
  def distribution
22
- response.split(/\n/).map do |line|
23
- i, j, p, ensemble = line.split(/\t/)
24
- [i, j].map(&:to_i) + [p, ensemble].map(&:to_f)
25
- end
20
+ response.split(/\n/).map { |line| line.split(/\t/) }
26
21
  end
27
22
  end
28
23
  end
@@ -14,19 +14,24 @@ module ViennaRna
14
14
  graph do |r|
15
15
  r.assign("line_graph.x", data.map(&:first))
16
16
  r.assign("line_graph.y", data.map(&:last))
17
-
18
- if filename && (filename = filename.end_with?(".pdf") ? filename : filename + ".pdf")
19
- r.eval <<-STR
20
- pdf("#{filename}", 6, 6)
21
- plot(line_graph.x, line_graph.y, xlab = "#{x_label}", ylab = "#{y_label}", main = "#{title || 'Line Graph'}", type = "#{type}")
22
- dev.off()
23
- STR
24
- else
25
- r.eval <<-STR
26
- quartz("Histogram", 6, 6)
27
- plot(line_graph.x, line_graph.y, xlab = "#{x_label}", ylab = "#{y_label}", main = "#{title || 'Line Graph'}", type = "#{type}")
28
- STR
29
- end
17
+
18
+ r.eval("%s('%s', 6, 6)" % [
19
+ writing_file?(filename) ? "pdf" : "quartz",
20
+ writing_file?(filename) ? filename : "Graph",
21
+ ])
22
+
23
+ r.eval <<-STR
24
+ plot(
25
+ line_graph.x,
26
+ line_graph.y,
27
+ xlab = "#{x_label}",
28
+ ylab = "#{y_label}",
29
+ main = "#{title || 'Line Graph'}",
30
+ type = "#{type}"
31
+ )
32
+ STR
33
+
34
+ r.eval("dev.off()") if writing_file?(filename)
30
35
  end
31
36
  end
32
37
 
@@ -38,18 +43,68 @@ module ViennaRna
38
43
  graph do |r|
39
44
  r.assign("histogram.data", data)
40
45
  r.assign("histogram.breaks", breaks)
41
-
42
- if filename && (filename = filename.end_with?(".pdf") ? filename : filename + ".pdf")
46
+
47
+ r.eval("%s('%s', 6, 6)" % [
48
+ writing_file?(filename) ? "pdf" : "quartz",
49
+ writing_file?(filename) ? filename : "Histogram",
50
+ ])
51
+
52
+ r.eval <<-STR
53
+ hist(
54
+ histogram.data,
55
+ breaks = histogram.breaks,
56
+ xlab = "#{x_label} (width: #{bin_size})",
57
+ main = "#{title || 'Histogram'}",
58
+ freq = #{relative ? 'FALSE' : 'TRUE'}
59
+ )
60
+ STR
61
+
62
+ r.eval("dev.off()") if writing_file?(filename)
63
+ end
64
+ end
65
+
66
+ def matrix_heatmap(x, y, z, title: nil, x_label: "Column index", y_label: "Row index", filename: false, num_colors: 64)
67
+ graph do |r|
68
+ if r.pull("ifelse('Matrix' %in% rownames(installed.packages()), 1, -1)") > 0
69
+ if forced_square = (x.max != y.max)
70
+ x << [x, y].map(&:max).max
71
+ y << [x, y].map(&:max).max
72
+ z << 0
73
+ end
74
+
75
+ r.assign("matrix.i", x)
76
+ r.assign("matrix.j", y)
77
+ r.assign("matrix.x", z)
43
78
  r.eval <<-STR
44
- pdf("#{filename}", 6, 6)
45
- hist(histogram.data, breaks = histogram.breaks, xlab = "#{x_label} (width: #{bin_size})", main = "#{title || 'Histogram'}", freq = #{relative ? 'FALSE' : 'TRUE'})
46
- dev.off()
79
+ require("Matrix")
80
+ matrix.data <- sparseMatrix(
81
+ i = matrix.i,
82
+ j = matrix.j,
83
+ x = matrix.x,
84
+ index1 = F
85
+ )
47
86
  STR
87
+
88
+ generate_graph("Heatmap") do
89
+ <<-STR
90
+ filtered.values <- Filter(function(i) { is.finite(i) & i != 0 }, matrix.x)
91
+ print(apply(as.matrix(matrix.data), 2, rev))
92
+ print(c(sort(filtered.values)[2], max(filtered.values)))
93
+
94
+ image(
95
+ x = 1:max(c(dim(matrix.data)[[1]], dim(matrix.data)[[2]])),
96
+ y = 1:max(c(dim(matrix.data)[[1]], dim(matrix.data)[[2]])),
97
+ z = as.matrix(matrix.data),
98
+ col = rev(heat.colors(#{num_colors})),
99
+ zlim = #{forced_square ? "c(sort(filtered.values)[2], max(filtered.values))" : "c(min(filtered.values), max(filtered.values))"},
100
+ xlab = "#{x_label} (1-indexed)",
101
+ ylab = "#{y_label} (1-indexed)"
102
+ )
103
+ title("#{title || 'Matrix Heatmap'}")
104
+ STR
105
+ end
48
106
  else
49
- r.eval <<-STR
50
- quartz("Histogram", 6, 6)
51
- hist(histogram.data, breaks = histogram.breaks, xlab = "#{x_label} (width: #{bin_size})", main = "#{title || 'Histogram'}", freq = #{relative ? 'FALSE' : 'TRUE'})
52
- STR
107
+ puts "Please install the Matrix package for R before using this function."
53
108
  end
54
109
  end
55
110
  end
@@ -75,17 +130,24 @@ module ViennaRna
75
130
  # plot([{ x: roc_curve.map(&:first), y: roc_curve.map(&:last), style: "lines" }], options)
76
131
  end
77
132
 
78
- # def quick_plot(data, title = "", options = {})
79
- # quick_overlay([{ data: data }], title, options)
80
- # end
133
+ private
81
134
 
82
- # def quick_overlay(data, title = "", options = {})
83
- # # [{ data: [[x_0, y_0], [x_1, y_1], ...], label: "Label" }, { data: [[x_0, y_0], [x_1, y_1], ...] }]
84
- # options[:plot] = ((options[:plot] || {}).merge(title: title))
85
- # options.merge!(output: "file") if options[:filename]
135
+ def generate_graph(window_title = "ViennaRNA Graph in R", &block)
136
+ r, filename = block.binding.eval("[r, filename]")
86
137
 
87
- # plot(data.map { |hash| { title: hash[:label], x: hash[:data].map(&:first), y: hash[:data].map(&:last), style: "linespoints" }.merge(hash[:options] || {}) }, options)
88
- # end
138
+ r.eval("%s('%s', 6, 6)" % [
139
+ writing_file?(filename) ? "pdf" : "quartz",
140
+ writing_file?(filename) ? filename : window_title,
141
+ ])
142
+
143
+ r.eval(yield)
144
+
145
+ r.eval("dev.off()") if writing_file?(filename)
146
+ end
147
+
148
+ def writing_file?(filename)
149
+ filename && (filename = filename.end_with?(".pdf") ? filename : filename + ".pdf")
150
+ end
89
151
  end
90
152
  end
91
153
 
@@ -40,6 +40,8 @@ module ViennaRna
40
40
  raw_data: rna.raw_data
41
41
  )
42
42
  end
43
+
44
+ alias_method :placeholder, :new
43
45
  end
44
46
 
45
47
  def initialize(sequence: "", structure: "", second_structure: "", raw_data: {})
@@ -70,16 +72,13 @@ module ViennaRna
70
72
  alias :str_1 :structure
71
73
  alias :str_2 :second_structure
72
74
 
73
- def inspect
74
- "#<%s>" % [
75
- "#{self.class.name}",
76
- ("#{seq[0, 20] + (seq.length > 20 ? '...' : '')}" if seq),
77
- ("#{str_1[0, 20] + (str_1.length > 20 ? ' [truncated]' : '')}" if str_1),
78
- ("#{str_2[0, 20] + (str_2.length > 20 ? ' [truncated]' : '')}" if str_2),
79
- ].compact.join(" ")
75
+ def empty_structure
76
+ "." * seq.length
80
77
  end
78
+
79
+ alias :empty_str :empty_structure
81
80
 
82
- def write_fa!(filename, comment = "")
81
+ def write_fa!(filename, comment = nil)
83
82
  filename.tap do |filename|
84
83
  File.open(filename, ?w) do |file|
85
84
  file.write("> %s\n" % comment) if comment
@@ -102,10 +101,13 @@ module ViennaRna
102
101
  end
103
102
  end
104
103
 
105
- def empty_structure
106
- "." * seq.length
104
+ def inspect
105
+ "#<%s>" % [
106
+ "#{self.class.name}",
107
+ ("#{seq[0, 20] + (seq.length > 20 ? '...' : '')}" if seq && !seq.empty?),
108
+ ("#{str_1[0, 20] + (str_1.length > 20 ? ' [truncated]' : '')}" if str_1 && !str_1.empty?),
109
+ ("#{str_2[0, 20] + (str_2.length > 20 ? ' [truncated]' : '')}" if str_2 && !str_1.empty?),
110
+ ].compact.join(" ")
107
111
  end
108
-
109
- alias :empty_str :empty_structure
110
112
  end
111
113
  end
@@ -0,0 +1,25 @@
1
+ module ViennaRna
2
+ class Rna2dfold < EnergyGrid2d
3
+ BASE_FLAGS = {
4
+ d: 0,
5
+ p: :empty,
6
+ "-noBT" => :empty
7
+ }
8
+
9
+ self.executable_name = "RNA2Dfold"
10
+
11
+ def run_command(flags = {})
12
+ ViennaRna.debugger { "Running RNA2Dfold on #{data.inspect}" }
13
+
14
+ "cat %s | %s %s" % [
15
+ data.temp_fa_file!,
16
+ exec_name,
17
+ stringify_flags(BASE_FLAGS.merge(self.class.const_defined?(:FLAGS) ? self.class.const_get(:FLAGS) : {}).merge(flags))
18
+ ]
19
+ end
20
+
21
+ def distribution
22
+ response.split(/\n/)[6..-1].map { |line| line.split(/\t/).at_indexes([0, 1, 2, 6]) }
23
+ end
24
+ end
25
+ end
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.7.0
4
+ version: 0.8.6
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-08-23 00:00:00.000000000 Z
11
+ date: 2013-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bio
@@ -102,6 +102,7 @@ extra_rdoc_files: []
102
102
  files:
103
103
  - lib/vienna_rna/modules/base.rb
104
104
  - lib/vienna_rna/modules/batch.rb
105
+ - lib/vienna_rna/modules/energy_grid_2d.rb
105
106
  - lib/vienna_rna/modules/eval.rb
106
107
  - lib/vienna_rna/modules/fftbor.rb
107
108
  - lib/vienna_rna/modules/fftbor2d.rb
@@ -112,6 +113,7 @@ files:
112
113
  - lib/vienna_rna/modules/heat.rb
113
114
  - lib/vienna_rna/modules/parser.rb
114
115
  - lib/vienna_rna/modules/rna.rb
116
+ - lib/vienna_rna/modules/rna2dfold.rb
115
117
  - lib/vienna_rna/modules/rna_extensions.rb
116
118
  - lib/vienna_rna/modules/rnabor.rb
117
119
  - lib/vienna_rna/modules/subopt.rb