wrnap 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,84 @@
1
+ module Wrnap
2
+ module Package
3
+ class Population < Base
4
+ THREE_COLUMN_REGEX = /^([+-]\d+\.\d+\t){2}[+-]\d+\.\d+$/
5
+
6
+ attr_reader :str_1_to_str_2, :str_1_to_str_1
7
+
8
+ self.default_flags = ->(context, flags) do
9
+ {
10
+ "-fftbor2d-i" => context.data.seq,
11
+ "-fftbor2d-j" => context.data.str_1,
12
+ "-fftbor2d-k" => context.data.str_2,
13
+ "-spectral-i" => -10,
14
+ "-spectral-j" => 10,
15
+ "-spectral-p" => 1e-2,
16
+ }
17
+ end
18
+ self.quote_flag_params = %w|-fftbor2d-i -fftbor2d-j -fftbor2d-k|
19
+
20
+ class PopulationProportion
21
+ include Enumerable
22
+
23
+ attr_reader :proportion_over_time
24
+
25
+ def initialize(time, proportion)
26
+ @proportion_over_time = time.zip(proportion)
27
+ end
28
+
29
+ def time_range(from, to)
30
+ proportion_over_time.select { |time, _| ((from.to_f)..(to.to_f)) === time }
31
+ end
32
+
33
+ def equilibrium(percentile: 95, window_size: 5, epsilon: 1e-4)
34
+ start = proportion_points.first
35
+ stop = proportion_points.last
36
+ sign = stop > start ? :increasing : :decreasing
37
+ # If the population is increasing over time, we want the 95%, otherwise we want the 5%
38
+ spread_100 = (start.zero? || stop.zero? ? [start, stop].max : (stop / start).abs) / 100
39
+ # Look for the first index at the percentile we're interested in, and scan to the right from there.
40
+ start_index = proportion_points.each_with_index.find do |proportion, i|
41
+ case sign
42
+ when :increasing then proportion > (stop - (spread_100 * (100 - percentile)))
43
+ when :decreasing then proportion < (stop + (spread_100 * (100 - percentile)))
44
+ end
45
+ end.last
46
+
47
+ # The first slice starting at x we find where abs(p(x + i), p(x)) < epslion for all 1 <= x < window_size is equilibrium,
48
+ # and we return that time point.
49
+ proportion_over_time[start_index..-1].each_cons(window_size).find do |proportion_slice|
50
+ proportion_slice.all? { |time, proportion| (proportion - proportion_slice.first.last).abs < epsilon }
51
+ end.first.first
52
+ end
53
+
54
+ def time_points; proportion_over_time.map(&:first); end
55
+ def proportion_points; proportion_over_time.map(&:last); end
56
+
57
+ def each
58
+ proportion_over_time.each { |_| yield _ }
59
+ end
60
+
61
+ def inspect
62
+ "#<Wrnap::Package::Population::PopulationProportion time: (%f..%f), proportion: (%f..%f)>" % [
63
+ time_points[0],
64
+ time_points[-1],
65
+ proportion_points[0],
66
+ proportion_points[-1],
67
+ ]
68
+ end
69
+ end
70
+
71
+ def run_command(flags)
72
+ Wrnap.debugger { "Running #{exec_name} on #{data.inspect}" }
73
+
74
+ "%s %s" % [exec_name, stringify_flags(flags)]
75
+ end
76
+
77
+ def post_process
78
+ time_points, str_1_to_str_2, str_1_to_str_1 = response.split(/\n/).select { |line| line =~ THREE_COLUMN_REGEX }.map { |line| line.split(/\t/).map(&:to_f) }.transpose
79
+ @str_1_to_str_2 = PopulationProportion.new(time_points, str_1_to_str_2)
80
+ @str_1_to_str_1 = PopulationProportion.new(time_points, str_1_to_str_1)
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,27 @@
1
+ module Wrnap
2
+ module Package
3
+ class Rna2dfold < EnergyGrid2d
4
+ self.default_flags = {
5
+ d: 0,
6
+ p: true,
7
+ "-noBT" => true
8
+ }
9
+
10
+ self.executable_name = "RNA2Dfold"
11
+
12
+ def run_command(flags)
13
+ Wrnap.debugger { "Running RNA2Dfold on #{data.inspect}" }
14
+
15
+ "cat %s | %s %s" % [
16
+ data.temp_fa_file!,
17
+ exec_name,
18
+ stringify_flags(flags)
19
+ ]
20
+ end
21
+
22
+ def distribution
23
+ response.split(/\n/)[6..-1].map { |line| line.split(/\t/).at_indexes([0, 1, 2, 6]) }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,32 @@
1
+ module Wrnap
2
+ module Package
3
+ class Rnabor < Xbor
4
+ FLAGS = {
5
+ nodangle: :empty
6
+ }
7
+
8
+ def partition
9
+ non_zero_shells.inject(&:+)
10
+ end
11
+
12
+ def total_count
13
+ counts.inject(&:+)
14
+ end
15
+
16
+ def counts
17
+ (non_zero_counts = self.class.parse(response).map { |row| BigDecimal.new(row[2]).to_i }) + [0] * (data.seq.length - non_zero_counts.length + 1)
18
+ end
19
+
20
+ def distribution(options = {})
21
+ options = { precision: 4 }.merge(options)
22
+
23
+ distribution_before_precision = (non_zero_distribution = non_zero_shells.map { |i| i / partition }) + [0.0] * (data.seq.length - non_zero_distribution.length + 1)
24
+ distribution_before_precision.map { |value| options[:precision].zero? ? value : (value * 10 ** options[:precision]).truncate / 10.0 ** options[:precision] }
25
+ end
26
+
27
+ def non_zero_shells
28
+ self.class.parse(response).map { |row| BigDecimal.new(row[1]) }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ module Wrnap
2
+ module Package
3
+ class Spectral < Base
4
+ self.default_flags = ->(context, flags) { { seq: context.data.seq, step_size: "1e-2" } }
5
+
6
+ attr_reader :eigenvalues, :time_kinetics
7
+
8
+ def run_command(flags)
9
+ "%s %s" % [
10
+ exec_name,
11
+ stringify_flags(flags)
12
+ ]
13
+ end
14
+
15
+ def post_process
16
+ if flags.keys.include?(:eigen_only)
17
+ @eigenvalues = response.split(?\n).map(&:to_f).sort_by(&:abs)
18
+ else
19
+ @time_kinetics = response.split(?\n).map { |line| line.split(?\t).map(&:to_f) }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ module Wrnap
2
+ module Package
3
+ class Subopt < Base
4
+ attr_reader :structures
5
+
6
+ def post_process
7
+ @structures = response.split(/\n/)[1..-1].map do |output|
8
+ structure, mfe = output.split(/\s+/)
9
+
10
+ RNA.from_string(data.seq, structure).tap do |rna|
11
+ rna.instance_variable_set(:@mfe, mfe.to_f)
12
+ rna.class_eval { attr_reader :mfe }
13
+ end
14
+ end
15
+ end
16
+
17
+ def bin(count = 1)
18
+ run(p: count).structures.inject(Hash.new { |hash, key| hash[key] = 0 }) do |hash, structure|
19
+ hash.tap do
20
+ hash[structure] += 1
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,50 @@
1
+ module Wrnap
2
+ module Package
3
+ class TabuPath < Base
4
+ self.executable_name = "get_barrier_tabu"
5
+ self.default_flags = ->(context, flags) { { iterations: 10, min_weight: 10, max_weight: 70 } }
6
+
7
+ attr_reader :paths
8
+
9
+ def run_command(flags)
10
+ Wrnap.debugger { "Running #{exec_name} on #{data.inspect}" }
11
+
12
+ [
13
+ exec_name,
14
+ data.seq.inspect,
15
+ data.str_1.inspect,
16
+ data.str_2.inspect,
17
+ flags[:iterations],
18
+ flags[:min_weight],
19
+ flags[:max_weight]
20
+ ].join(" ")
21
+ end
22
+
23
+ def post_process
24
+ @paths = response.split(data.str_1 + ?\n).reject(&:empty?).map { |path_string| Path.new(data, path_string) }
25
+ end
26
+
27
+ class Path
28
+ attr_reader :rna, :path, :barrier, :best_weight
29
+
30
+ def initialize(rna, output)
31
+ @rna = rna
32
+ @path = output.split(?\n)[0..-2].unshift(rna.str_1)
33
+ @barrier, _, @best_weight = output.split(?\n)[-1].gsub(/[^\d\.]/, " ").strip.split(/\s+/).map(&:to_f)
34
+ end
35
+
36
+ def length
37
+ path.length
38
+ end
39
+
40
+ def full_path?
41
+ rna.str_1 == path.first && rna.str_2 == path.last
42
+ end
43
+
44
+ def inspect
45
+ "#<#{self.class.name} with barrier %.2f and length %d on #{rna.inspect}>" % [barrier, best_weight]
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,63 @@
1
+ module Wrnap
2
+ module Package
3
+ class Xbor < Base
4
+ self.default_flags = {
5
+ E: "/usr/local/bin/rna_turner2004.par"
6
+ }
7
+
8
+ self.executable_name = ->(context) { context.class.name.demodulize.gsub(/^([A-Z].*)bor$/) { |match| $1.upcase + "bor" } }
9
+
10
+ def run_command(flags)
11
+ file = Tempfile.new("rna")
12
+ file.write("%s\n" % data.seq)
13
+ file.write("%s\n" % data.str)
14
+ file.close
15
+
16
+ Wrnap.debugger { "Running FFTbor on #{data.inspect}" }
17
+
18
+ "%s %s %s" % [
19
+ exec_name,
20
+ stringify_flags(flags),
21
+ file.path
22
+ ]
23
+ end
24
+
25
+ def self.parse(response)
26
+ response.split(/\n/).select { |line| line =~ /^\d+\t-?\d+/ }.map { |line| line.split(/\t/) }
27
+ end
28
+
29
+ def full_distribution
30
+ distribution = run.distribution
31
+ full_distribution = distribution + ([0.0] * ((differnece = data.seq.length - distribution.length + 1) < 0 ? 0 : differnece))
32
+ end
33
+
34
+ def k_p_points
35
+ full_distribution.each_with_index.to_a.map(&:reverse)[0..data.seq.length]
36
+ end
37
+
38
+ def expected_k
39
+ k_p_points.map { |array| array.inject(&:*) }.inject(&:+)
40
+ end
41
+
42
+ def to_csv
43
+ k_p_points.map { |k, p| "%d,%.8f" % [k, p] }.join(?\n) + ?\n
44
+ end
45
+
46
+ def to_csv!(filename)
47
+ File.open(filename, ?w) { |file| file.write(to_csv) }
48
+ end
49
+
50
+ def quick_plot(filename: false)
51
+ Wrnap::Graphing::R.line_graph(
52
+ k_p_points,
53
+ title: options[:title] || "%s\\n%s\\n%s" % [self.class.name, data.seq, data.safe_structure],
54
+ filename: false
55
+ )
56
+ end
57
+
58
+ def inspect
59
+ "#<#{self.class.name} #{data.inspect}>"
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module Wrnap
2
+ VERSION = "0.0.1"
3
+ end
data/lib/wrnap.rb ADDED
@@ -0,0 +1,80 @@
1
+ require "benchmark"
2
+ require "set"
3
+ require "shuffle"
4
+ require "rinruby"
5
+ require "tempfile"
6
+ require "bigdecimal"
7
+ require "rroc"
8
+ require "active_support/inflector"
9
+ require "active_support/core_ext/class"
10
+
11
+ require "wrnap/version"
12
+ require "wrnap/global/rna_extensions"
13
+ require "wrnap/global/rna"
14
+ require "wrnap/global/parser"
15
+ require "wrnap/global/run_extensions"
16
+ require "wrnap/global/chain_extensions"
17
+ require "wrnap/graphing/r"
18
+ require "wrnap/package/base"
19
+
20
+ begin; R.quit; rescue IOError; end
21
+
22
+ module Wrnap
23
+ RT = 1e-3 * 1.9872041 * (273.15 + 37) # kcal / K / mol @ 37C
24
+ @debug = true
25
+
26
+ module Package
27
+ Dir[File.join(File.dirname(__FILE__), "wrnap", "package", "*.rb")].reject { |file| file =~ /\/base.rb/ }.each do |file|
28
+ autoload(File.basename(file, ".rb").camelize.to_sym, "wrnap/package/#{File.basename(file, '.rb')}")
29
+ end
30
+
31
+ def self.const_missing(name)
32
+ if const_defined?(name)
33
+ const_get(name)
34
+ elsif Base.exec_exists?(name)
35
+ module_eval do
36
+ const_set(name, Class.new(Base))
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ def self.deserialize(string)
43
+ YAML.load(File.exist?(string) ? File.read(string) : string)
44
+ end
45
+
46
+ def self.debugger
47
+ STDERR.puts yield if Wrnap.debug
48
+ end
49
+
50
+ def self.debug
51
+ @debug
52
+ end
53
+
54
+ def self.debug=(value)
55
+ @debug = value
56
+ end
57
+ end
58
+
59
+ # This dirties up the public namespace, but I use it so many times that I want a shorthand to it
60
+ unless defined? RNA
61
+ def RNA(*args)
62
+ RNA.from_array(args)
63
+ end
64
+ end
65
+
66
+ module RNA
67
+ def self.load_all(pattern = "*.fa")
68
+ Dir[File.directory?(pattern) ? pattern + "/*.fa" : pattern].map { |file| RNA.from_fasta(file) }
69
+ end
70
+
71
+ def self.random(size, *args)
72
+ RNA.from_array(args.unshift(Wrnap::Global::Rna.generate_sequence(size).seq))
73
+ end
74
+
75
+ def self.method_missing(name, *args, &block)
76
+ if "#{name}" =~ /^from_\w+$/
77
+ Wrnap::Global::Rna.send("init_#{name}", *args)
78
+ else super end
79
+ end
80
+ end
data/wrnap.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wrnap/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wrnap"
8
+ spec.version = Wrnap::VERSION
9
+ spec.authors = ["Evan Senter"]
10
+ spec.email = ["evansenter@gmail.com"]
11
+ spec.summary = %q{A comprehensive wrapper (wRNApper) for various RNA CLI programs.}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_runtime_dependency "activesupport", "~> 4.0"
25
+ spec.add_runtime_dependency "shuffle", "~> 0.1"
26
+ spec.add_runtime_dependency "rinruby", "~> 2.0"
27
+ spec.add_runtime_dependency "rroc", "~> 0.1"
28
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wrnap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Evan Senter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: shuffle
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.1'
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'
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'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rroc
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.1'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.1'
97
+ description: ''
98
+ email:
99
+ - evansenter@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.md
107
+ - README.md
108
+ - Rakefile
109
+ - lib/wrnap.rb
110
+ - lib/wrnap/global/chain_extensions.rb
111
+ - lib/wrnap/global/parser.rb
112
+ - lib/wrnap/global/rna.rb
113
+ - lib/wrnap/global/rna_extensions.rb
114
+ - lib/wrnap/global/run_extensions.rb
115
+ - lib/wrnap/graphing/r.rb
116
+ - lib/wrnap/package/base.rb
117
+ - lib/wrnap/package/energy_grid_2d.rb
118
+ - lib/wrnap/package/eval.rb
119
+ - lib/wrnap/package/fft_mfpt.rb
120
+ - lib/wrnap/package/fftbor.rb
121
+ - lib/wrnap/package/fftbor2d.rb
122
+ - lib/wrnap/package/ffthairpin.rb
123
+ - lib/wrnap/package/fftmultiloop.rb
124
+ - lib/wrnap/package/fold.rb
125
+ - lib/wrnap/package/heat.rb
126
+ - lib/wrnap/package/kinwalker.rb
127
+ - lib/wrnap/package/mfpt.rb
128
+ - lib/wrnap/package/plot.rb
129
+ - lib/wrnap/package/population.rb
130
+ - lib/wrnap/package/rna2dfold.rb
131
+ - lib/wrnap/package/rnabor.rb
132
+ - lib/wrnap/package/spectral.rb
133
+ - lib/wrnap/package/subopt.rb
134
+ - lib/wrnap/package/tabu_path.rb
135
+ - lib/wrnap/package/xbor.rb
136
+ - lib/wrnap/version.rb
137
+ - wrnap.gemspec
138
+ homepage: ''
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.2.2
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: A comprehensive wrapper (wRNApper) for various RNA CLI programs.
162
+ test_files: []