vienna_rna 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/modules/base.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module ViennaRna
2
2
  class Base
3
- class_inheritable_accessor :exec_name
3
+ class_attribute :executable_name
4
4
 
5
5
  class << self
6
6
  def method_added(name)
@@ -17,15 +17,34 @@ module ViennaRna
17
17
  def exec_exists?(name)
18
18
  !%x[which rna#{name.to_s.downcase}].empty?
19
19
  end
20
+
21
+ def run(data, flags = {})
22
+ new(data).run(flags)
23
+ end
24
+
25
+ def batch(fastas = [])
26
+ ViennaRna::Batch.new(self, fastas).tap do |batch|
27
+ if const_defined?(:Batch)
28
+ @@me = self
29
+
30
+ batch.singleton_class.class_eval { include @@me.const_get(:Batch) }
31
+ end
32
+ end
33
+ end
20
34
  end
21
35
 
22
- attr_reader :fasta
36
+ attr_reader :fasta, :response
23
37
 
24
38
  def exec_name
25
- @exec_name || "rna#{self.class.name.split('::').last.underscore}"
39
+ executable_name || "rna#{self.class.name.split('::').last.underscore}"
40
+ end
41
+
42
+ def exec_sequence_format
43
+ fasta.seq
26
44
  end
27
45
 
28
46
  def initialize(data)
47
+ # Doesn't support structures on the third line yet.
29
48
  @fasta = case data
30
49
  when Bio::FastaFormat then data
31
50
  when String then Bio::FastaFormat.new(data.split(/\n/).length > 1 ? data : ">\n%s" % data)
@@ -33,9 +52,11 @@ module ViennaRna
33
52
  end
34
53
 
35
54
  def run_with_hooks(flags = {})
36
- pre_run_check
37
- response = run_without_hooks(flags)
38
- self.class.method_defined?(:post_process) ? post_process(response) : response
55
+ 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)
59
+ end
39
60
  end
40
61
 
41
62
  def pre_run_check
@@ -45,11 +66,15 @@ module ViennaRna
45
66
  end
46
67
 
47
68
  def stringify_flags(flags)
48
- flags.inject("") { |string, flag| (string + (" -%s %s" % flag)).strip }
69
+ flags.inject("") { |string, (flag, value)| (string + (value == :empty ? " -%s" % flag : " -%s %s" % [flag, value])).strip }
49
70
  end
50
71
 
51
72
  def run(flags = {})
52
- %x[echo #{fasta.seq} | #{exec_name} #{stringify_flags(flags)}]
73
+ if respond_to?(:run_command)
74
+ %x[#{method(:run_command).arity.zero? ? run_command : run_command(flags)}]
75
+ else
76
+ %x[echo #{exec_sequence_format} | #{exec_name} #{stringify_flags(flags)}]
77
+ end
53
78
  end
54
79
  end
55
80
  end
data/lib/modules/batch.rb CHANGED
@@ -1 +1,26 @@
1
- # This should be a meta-overlord class that runs the same command on a collection of subclasses of base.rb
1
+ module ViennaRna
2
+ class Batch
3
+ include Enumerable
4
+
5
+ attr_reader :type, :collection
6
+
7
+ def initialize(type, data)
8
+ @type = type
9
+ @collection = data.map(&type.method(:new))
10
+ end
11
+
12
+ def each
13
+ collection.each { |element| yield element }
14
+ end
15
+
16
+ def run(flags = {})
17
+ tap do
18
+ if (@memo ||= {})[flags]
19
+ @memo[flags]
20
+ else
21
+ @memo[flags] = map { |element| element.run(flags) }
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
data/lib/modules/fold.rb CHANGED
@@ -2,23 +2,24 @@ module ViennaRna
2
2
  class Fold < Base
3
3
  attr_reader :structure, :mfe
4
4
 
5
- def post_process(response)
6
- tap do
7
- structure = response.split(/\n/).last.gsub(/ \(\s*(-?\d*\.\d*)\)$/, "")
5
+ def post_process
6
+ structure = @response.split(/\n/).last.gsub(/ \(\s*(-?\d*\.\d*)\)$/, "")
8
7
 
9
- unless fasta.seq.length == structure.length
10
- raise "Sequence: '#{fasta.seq}'\nStructure: '#{structure}'"
11
- else
12
- @structure, @mfe = structure, $1
13
- end
8
+ unless fasta.seq.length == structure.length
9
+ raise "Sequence: '#{fasta.seq}'\nStructure: '#{structure}'"
10
+ else
11
+ @structure, @mfe = structure, $1.to_f
14
12
  end
15
13
  end
16
- end
17
-
18
- # Mix this baby into the Batch class after I write it for great cleanness!
19
- module Batch
20
- def prune_same_structures
21
-
14
+
15
+ module Batch
16
+ def with_different_structures
17
+ run.inject(Hash.new { |hash, key| hash[key] = [] }) do |hash, folded_sequence|
18
+ hash.tap do
19
+ hash[folded_sequence.structure] << folded_sequence
20
+ end
21
+ end.values.map(&:first)
22
+ end
22
23
  end
23
24
  end
24
25
  end
@@ -0,0 +1,13 @@
1
+ module ViennaRna
2
+ class Heat < Base
3
+ attr_reader :specific_heats
4
+
5
+ def post_process
6
+ @specific_heats = @response.split(/\n/).map { |line| line.split(/\s+/).map(&:to_f) }.inject({}) do |hash, (temp, specific_heat)|
7
+ hash.tap do
8
+ hash[temp] = specific_heat
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ module ViennaRna
2
+ class Rnabor < Base
3
+ def run_command(flags)
4
+ "./RNAbor -s %s -c %s" % [fasta.seq, flags[:scaling_factor]]
5
+ end
6
+
7
+ def parse_total_count
8
+ response.split(/\n/).find { |line| line =~ /^Z\[\d+\]\[1\]:/ }.match(/^Z\[\d+\]\[1\]:\s*(.*)/)[1].to_i
9
+ end
10
+
11
+ def parse_points
12
+ self.class.parse(response, "ROOTS AND SOLUTIONS") { |line| line.strip.split(/\s\s+/).map { |value| eval("Complex(#{value})") } }
13
+ end
14
+
15
+ def parse_counts
16
+ self.class.parse(response, "UNSCALED SUM") { |line| line.strip.split(/:\s*/).map(&:to_f) }
17
+ end
18
+
19
+ def self.parse(response, delimiter)
20
+ response.split(/\n/).reject do |line|
21
+ line.empty?
22
+ end.drop_while do |line|
23
+ line !~ /^START #{delimiter}/i
24
+ end.reverse.drop_while do |line|
25
+ line !~ /^END #{delimiter}/i
26
+ end.reverse[1..-2].map do |line|
27
+ yield line
28
+ end
29
+ end
30
+ end
31
+ end
@@ -2,10 +2,8 @@ module ViennaRna
2
2
  class Subopt < Base
3
3
  attr_reader :structures
4
4
 
5
- def post_process(response)
6
- tap do
7
- @structures = response.split(/\n/)
8
- end
5
+ def post_process
6
+ @structures = @response.split(/\n/)
9
7
  end
10
8
 
11
9
  def bin(count = 1)
data/lib/modules/utils.rb CHANGED
@@ -7,6 +7,22 @@ module ViennaRna
7
7
  # Force it to not be lazy.
8
8
  Bio::FlatFile.auto(path).to_enum.map { |fasta| fasta }
9
9
  end
10
+
11
+ def write_fastas!(fastas, directory, base_name, group_size = 10)
12
+ fastas.each_slice(group_size).each_with_index do |fasta_group, i|
13
+ path = File.join(directory, base_name + "_#{i}.fa")
14
+
15
+ unless File.exists?(path)
16
+ File.open(path, "w") do |file|
17
+ fasta_group.each do |folding|
18
+ file.write(">%s\n%s\n" % [folding.fasta.definition, folding.fasta.seq])
19
+ end
20
+ end
21
+ else
22
+ puts "Warning: file '#{path}' exists. Skipping."
23
+ end
24
+ end
25
+ end
10
26
  end
11
27
  end
12
28
  end
data/lib/vienna_rna.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require "bio"
2
2
  require "active_support/inflector"
3
3
  require "active_support/core_ext/class"
4
- require "active_support/core_ext/module"
4
+ require "active_support/core_ext/module/aliasing"
5
5
 
6
6
  # Clean up this include order.
7
7
  Dir[File.join(File.dirname(__FILE__), "/modules/*")].each do |file|
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.2
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bio
16
- requirement: &70120459237640 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,18 +21,28 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70120459237640
25
- - !ruby/object:Gem::Dependency
26
- name: active_support
27
- requirement: &70120459237200 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
28
25
  none: false
29
26
  requirements:
30
27
  - - ! '>='
31
28
  - !ruby/object:Gem::Version
32
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.2.5
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70120459237200
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.2.5
36
46
  description: A Ruby API for interacting with the Vienna RNA package.
37
47
  email: evansenter@gmail.com
38
48
  executables: []
@@ -45,6 +55,10 @@ files:
45
55
  Li9saWIvbW9kdWxlcy9iYXRjaC5yYg==
46
56
  - !binary |-
47
57
  Li9saWIvbW9kdWxlcy9mb2xkLnJi
58
+ - !binary |-
59
+ Li9saWIvbW9kdWxlcy9oZWF0LnJi
60
+ - !binary |-
61
+ Li9saWIvbW9kdWxlcy9ybmFib3IucmI=
48
62
  - !binary |-
49
63
  Li9saWIvbW9kdWxlcy9zdWJvcHQucmI=
50
64
  - !binary |-
@@ -71,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
85
  version: '0'
72
86
  requirements: []
73
87
  rubyforge_project:
74
- rubygems_version: 1.8.11
88
+ rubygems_version: 1.8.24
75
89
  signing_key:
76
90
  specification_version: 3
77
91
  summary: Bindings to the Vienna RNA package.