vienna_rna 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/modules/base.rb CHANGED
@@ -1,5 +1,26 @@
1
+ require "benchmark"
2
+
1
3
  module ViennaRna
2
4
  class Base
5
+ class Rna
6
+ attr_reader :sequence, :structure
7
+
8
+ def initialize(sequence, structure = nil)
9
+ @sequence = sequence
10
+ @structure = structure
11
+ end
12
+
13
+ alias :seq :sequence
14
+
15
+ def safe_structure
16
+ structure || empty_structure
17
+ end
18
+
19
+ def empty_structure
20
+ "." * seq.length
21
+ end
22
+ end
23
+
3
24
  class_attribute :executable_name
4
25
 
5
26
  class << self
@@ -33,29 +54,33 @@ module ViennaRna
33
54
  end
34
55
  end
35
56
 
36
- attr_reader :fasta, :response
57
+ attr_reader :data, :response, :runtime
37
58
 
38
59
  def exec_name
39
60
  executable_name || "rna#{self.class.name.split('::').last.underscore}"
40
61
  end
41
62
 
42
63
  def exec_sequence_format
43
- fasta.seq
64
+ data.seq
44
65
  end
45
66
 
46
67
  def initialize(data)
47
- # Doesn't support structures on the third line yet.
48
- @fasta = case data
68
+ @data = case data
49
69
  when Bio::FastaFormat then data
50
- when String then Bio::FastaFormat.new(data.split(/\n/).length > 1 ? data : ">\n%s" % data)
70
+ when String then Rna.new(data)
71
+ when Hash then Rna.new(data[:sequence] || data[:seq], data[:structure] || data[:str])
51
72
  end
52
73
  end
53
74
 
54
75
  def run_with_hooks(flags = {})
55
76
  tap do
56
- pre_run_check unless respond_to?(:run_command)
57
- @response = run_without_hooks(flags)
58
- post_process if respond_to?(:post_process)
77
+ @runtime = Benchmark.measure do
78
+ pre_run_check unless respond_to?(:run_command)
79
+ @response = run_without_hooks(flags)
80
+ post_process if respond_to?(:post_process)
81
+ end
82
+
83
+ puts "Total runtime: %.3f sec." % runtime.real if ViennaRna.debug
59
84
  end
60
85
  end
61
86
 
@@ -0,0 +1,48 @@
1
+ module ViennaRna
2
+ class Fftbor < Base
3
+ attr_reader :flags
4
+
5
+ def run_command(flags)
6
+ @flags = flags
7
+
8
+ "./FFTbor -s %s -r %s -c %s -p %s" % [
9
+ data.seq,
10
+ data.safe_structure,
11
+ flags[:scaling_factor] ||= 1,
12
+ flags[:precision] ||= 6
13
+ ]
14
+ end
15
+
16
+ def parse_partition
17
+ response.split(/\n/).find { |line| line =~ /^Z\[1\]\[\d+\]:/ }.match(/^Z\[1\]\[\d+\]:\s*(.*)/)[1].to_f
18
+ end
19
+
20
+ def parse_total_count
21
+ response.split(/\n/).find { |line| line =~ /^Z\[\d+\]\[1\]:/ }.match(/^Z\[\d+\]\[1\]:\s*(.*)/)[1].to_i
22
+ end
23
+
24
+ def parse_points
25
+ self.class.parse(response, "ROOTS AND SOLUTIONS") { |line| line.strip.split(/\s\s+/).map { |value| eval("Complex(#{value})") } }
26
+ end
27
+
28
+ def parse_distribution
29
+ if flags[:scaling_factor] != 1
30
+ puts "Warning: The scaling factor was set to #{flags[:scaling_factor]}. The Boltzmann distribution is not setup to handle scaling in this fashion."
31
+ end
32
+
33
+ self.class.parse(response, "DISTRIBUTION") { |line| line.strip.split(/:\s*/).last.to_f }
34
+ end
35
+
36
+ def self.parse(response, delimiter)
37
+ response.split(/\n/).reject do |line|
38
+ line.empty?
39
+ end.drop_while do |line|
40
+ line !~ /^START #{delimiter}/i
41
+ end.reverse.drop_while do |line|
42
+ line !~ /^END #{delimiter}/i
43
+ end.reverse[1..-2].map do |line|
44
+ yield line
45
+ end
46
+ end
47
+ end
48
+ end
data/lib/modules/fold.rb CHANGED
@@ -5,8 +5,8 @@ module ViennaRna
5
5
  def post_process
6
6
  structure = @response.split(/\n/).last.gsub(/ \(\s*(-?\d*\.\d*)\)$/, "")
7
7
 
8
- unless fasta.seq.length == structure.length
9
- raise "Sequence: '#{fasta.seq}'\nStructure: '#{structure}'"
8
+ unless data.seq.length == structure.length
9
+ raise "Sequence: '#{data.seq}'\nStructure: '#{structure}'"
10
10
  else
11
11
  @structure, @mfe = structure, $1.to_f
12
12
  end
@@ -1,45 +1,38 @@
1
+ require "tempfile"
2
+
1
3
  module ViennaRna
2
4
  class Rnabor < Base
3
5
  def run_command(flags)
4
- "./RNAbor -s %s -c %s" % [fasta.seq, flags[:scaling_factor] || 1]
6
+ file = Tempfile.new("rna")
7
+ file.write("%s\n" % data.seq)
8
+ file.write("%s\n" % data.safe_structure)
9
+ file.close
10
+
11
+ "./RNAbor %s" % file.path
5
12
  end
6
13
 
7
- def parse_total_count
8
- response.split(/\n/).find { |line| line =~ /^Z\[\d+\]\[1\]:/ }.match(/^Z\[\d+\]\[1\]:\s*(.*)/)[1].to_i
14
+ def parse_partition
15
+ parse_non_zero_shells.sum
9
16
  end
10
17
 
11
- def parse_points
12
- self.class.parse(response, "ROOTS AND SOLUTIONS") { |line| line.strip.split(/\s\s+/).map { |value| eval("Complex(#{value})") } }
18
+ def parse_total_count
19
+ parse_counts.sum
13
20
  end
14
21
 
15
22
  def parse_counts
16
- self.class.parse(response, "UNSCALED SUM") { |line| line.strip.split(/:\s*/).map(&:to_f) }
23
+ (non_zero_counts = self.class.parse(response).map { |row| row[2].to_i }) + [0] * (data.seq.length - non_zero_counts.length + 1)
17
24
  end
18
25
 
19
- def in_r(options = {})
20
- results = solve_in_r(options).processed_response
21
-
22
- options[:unscale] ? results.map { |i| i * parse_total_count } : results
26
+ def parse_distribution
27
+ (non_zero_distribution = parse_non_zero_shells.map { |i| i / parse_partition }) + [0.0] * (data.seq.length - non_zero_distribution.length + 1)
23
28
  end
24
29
 
25
- def solve_in_r(options = {})
26
- options = { precision: 0, unscale: false }.merge(options)
27
-
28
- run unless response
29
-
30
- ViennaRna::FftInR.new(parse_points.map(&:last), parse_total_count, options[:precision]).run
30
+ def parse_non_zero_shells
31
+ self.class.parse(response).map { |row| row[1].to_f }
31
32
  end
32
33
 
33
- def self.parse(response, delimiter)
34
- response.split(/\n/).reject do |line|
35
- line.empty?
36
- end.drop_while do |line|
37
- line !~ /^START #{delimiter}/i
38
- end.reverse.drop_while do |line|
39
- line !~ /^END #{delimiter}/i
40
- end.reverse[1..-2].map do |line|
41
- yield line
42
- end
34
+ def self.parse(response)
35
+ response.split(/\n/)[2..-1].map { |line| line.split(/\t/) }
43
36
  end
44
37
  end
45
38
  end
data/lib/modules/utils.rb CHANGED
@@ -1,5 +1,3 @@
1
- # "/Users/evansenter/Source/mirna_5.1/hairpin.fa"
2
-
3
1
  module ViennaRna
4
2
  module Utils
5
3
  class << self
data/lib/vienna_rna.rb CHANGED
@@ -10,12 +10,12 @@ end
10
10
 
11
11
  module Enumerable
12
12
  def sum
13
- inject { |sum, i| sum + i }
13
+ inject(&:+)
14
14
  end
15
15
  end
16
16
 
17
17
  module ViennaRna
18
- @debug = false
18
+ @debug = true
19
19
 
20
20
  def self.const_missing(name)
21
21
  if Base.exec_exists?(name)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vienna_rna
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -49,24 +49,16 @@ executables: []
49
49
  extensions: []
50
50
  extra_rdoc_files: []
51
51
  files:
52
- - !binary |-
53
- Li9saWIvbW9kdWxlcy9iYXNlLnJi
54
- - !binary |-
55
- Li9saWIvbW9kdWxlcy9iYXRjaC5yYg==
56
- - !binary |-
57
- Li9saWIvbW9kdWxlcy9mZnRfaW5fci5yYg==
58
- - !binary |-
59
- Li9saWIvbW9kdWxlcy9mb2xkLnJi
60
- - !binary |-
61
- Li9saWIvbW9kdWxlcy9oZWF0LnJi
62
- - !binary |-
63
- Li9saWIvbW9kdWxlcy9ybmFib3IucmI=
64
- - !binary |-
65
- Li9saWIvbW9kdWxlcy9zdWJvcHQucmI=
66
- - !binary |-
67
- Li9saWIvbW9kdWxlcy91dGlscy5yYg==
68
- - !binary |-
69
- Li9saWIvdmllbm5hX3JuYS5yYg==
52
+ - ./lib/modules/base.rb
53
+ - ./lib/modules/batch.rb
54
+ - ./lib/modules/fft_in_r.rb
55
+ - ./lib/modules/fftbor.rb
56
+ - ./lib/modules/fold.rb
57
+ - ./lib/modules/heat.rb
58
+ - ./lib/modules/rnabor.rb
59
+ - ./lib/modules/subopt.rb
60
+ - ./lib/modules/utils.rb
61
+ - ./lib/vienna_rna.rb
70
62
  homepage: http://rubygems.org/gems/vienna_rna
71
63
  licenses: []
72
64
  post_install_message:
@@ -92,3 +84,4 @@ signing_key:
92
84
  specification_version: 3
93
85
  summary: Bindings to the Vienna RNA package.
94
86
  test_files: []
87
+ has_rdoc: