vienna_rna 0.1.7 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ module ViennaRna
4
4
  "-noPS" => :empty
5
5
  }
6
6
 
7
- attr_reader :structure, :mfe
7
+ attr_reader :mfe_rna, :structure, :mfe
8
8
 
9
9
  def post_process
10
10
  structure = @response.split(/\n/).last.gsub(Parser::REGEXP[:mfe], "")
@@ -12,7 +12,7 @@ module ViennaRna
12
12
  unless data.seq.length == structure.length
13
13
  raise "Sequence: '#{data.seq}'\nStructure: '#{structure}'"
14
14
  else
15
- @structure, @mfe = structure, Parser.mfe(@response)
15
+ @mfe_rna, @structure, @mfe = ViennaRna::Rna.new(data.seq, structure), structure, Parser.mfe(@response)
16
16
  end
17
17
  end
18
18
 
@@ -3,8 +3,14 @@ module ViennaRna
3
3
  attr_reader :sequence, :structure
4
4
 
5
5
  def initialize(sequence, structure = nil)
6
- @sequence = sequence.upcase
7
- @structure = (structure == :mfe ? ViennaRna::Fold.run(seq).structure : structure)
6
+ if sequence.class == self.class
7
+ # Too bad you can't do this in a cleaner way without method chaining initialize
8
+ @sequence = sequence.sequence
9
+ @structure = sequence.structure
10
+ else
11
+ @sequence = sequence.upcase
12
+ @structure = (structure == :mfe ? ViennaRna::Fold.run(seq).structure : structure)
13
+ end
8
14
  end
9
15
 
10
16
  alias :seq :sequence
@@ -22,6 +28,10 @@ module ViennaRna
22
28
  self.class.bp_distance(structure, other_structure)
23
29
  end
24
30
 
31
+ def symmetric_bp_distance(other_structure)
32
+ self.class.symmetric_bp_distance(structure, other_structure)
33
+ end
34
+
25
35
  def base_pairs
26
36
  self.class.base_pairs(structure)
27
37
  end
@@ -49,6 +59,7 @@ module ViennaRna
49
59
  end
50
60
 
51
61
  def bp_distance(structure_1, structure_2)
62
+ # Takes two structures and calculates the distance between them by |symmetric difference(bp_in_a, bp_in_b)|
52
63
  raise "The two structures are not the same length" unless structure_1.length == structure_2.length
53
64
 
54
65
  bp_set_1, bp_set_2 = base_pairs(structure_1), base_pairs(structure_2)
@@ -56,6 +67,20 @@ module ViennaRna
56
67
  ((bp_set_1 - bp_set_2) + (bp_set_2 - bp_set_1)).count
57
68
  end
58
69
 
70
+ def symmetric_bp_distance(structure_1, structure_2)
71
+ # Takes two structures and calculates the distance between them by: sum { ((x_j - x_i) - (y_j - y_i)).abs }
72
+ raise "The two structures are not the same length" unless structure_1.length == structure_2.length
73
+
74
+ bp_dist = ->(array, i) { array[i] == -1 ? 0 : array[i] - i }
75
+
76
+ structure_1_pairings = get_pairings(structure_1)
77
+ structure_2_pairings = get_pairings(structure_2)
78
+
79
+ structure_1.length.times.inject(0) do |distance, i|
80
+ distance + (bp_dist[structure_1_pairings, i] - bp_dist[structure_2_pairings, i]).abs
81
+ end
82
+ end
83
+
59
84
  def base_pairs(structure)
60
85
  get_pairings(structure).each_with_index.inject(Set.new) do |set, (j, i)|
61
86
  j >= 0 ? set << Set[i, j] : set
@@ -39,9 +39,9 @@ module ViennaRna
39
39
  plot.terminal("png size 800,600")
40
40
  end
41
41
 
42
- plot.title(options[:title])
43
- plot.xlabel(options[:x_label])
44
- plot.ylabel(options[:y_label])
42
+ options[:plot].keys.each do |option|
43
+ plot.send(option, options[:plot][option])
44
+ end
45
45
 
46
46
  plot.data = data.map do |data_hash|
47
47
  Gnuplot::DataSet.new([data_hash[:x], data_hash[:y]]) do |dataset|
@@ -53,14 +53,14 @@ module ViennaRna
53
53
  end
54
54
  end
55
55
 
56
- def quick_plot(title, data, filename = false)
57
- quick_overlay(title, [{ data: data }], filename)
56
+ def quick_plot(title, data, options = {})
57
+ quick_overlay(title, [{ data: data }], options)
58
58
  end
59
59
 
60
- def quick_overlay(title, data, filename = false)
60
+ def quick_overlay(title, data, options = {})
61
61
  # [{ data: [[x_0, y_0], [x_1, y_1], ...], label: "Label" }, { data: [[x_0, y_0], [x_1, y_1], ...] }]
62
- options = { title: title }
63
- options.merge!(output: "file", filename: filename) if filename
62
+ options[:plot] = ((options[:plot] || {}).merge(title: title))
63
+ options.merge!(output: "file") if options[:filename]
64
64
 
65
65
  plot(data.map { |hash| { title: hash[:label], x: hash[:data].map(&:first), y: hash[:data].map(&:last), style: "linespoints" } }, options)
66
66
  end
@@ -39,10 +39,11 @@ module ViennaRna
39
39
  k_p_points.map { |array| array.inject(&:*) }.inject(&:+)
40
40
  end
41
41
 
42
- def quick_plot(title = nil)
42
+ def quick_plot(options = {})
43
43
  ViennaRna::Utils.quick_plot(
44
- title || "%s\\n%s\\n%s" % [self.class.name, data.seq, data.safe_structure],
45
- k_p_points
44
+ options[:title] || "%s\\n%s\\n%s" % [self.class.name, data.seq, data.safe_structure],
45
+ k_p_points,
46
+ options
46
47
  )
47
48
  end
48
49
 
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.1.7
4
+ version: 0.1.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-05 00:00:00.000000000 Z
12
+ date: 2012-09-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bio