Module: ViennaRna::Graphing::Gnuplot

Defined in:
lib/vienna_rna/modules/graphing.rb

Class Method Summary collapse

Class Method Details

.histogram(data, title = "", options = {}) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/vienna_rna/modules/graphing.rb', line 148

def histogram(data, title = "", options = {})
  bin_size = options.delete(:bin_size) || 1
  half     = bin_size / 2.0
  range    = Range.new((data.min - half).floor, (data.max + half).ceil)
  groups   = (range.min + half).step(range.max, bin_size).map { |x| [x, data.count { |i| i >= x - half && i < x + half }] }
  
  options.merge!(output: "file") if options[:filename]
  options[:plot] = (options[:plot] || {}).merge({
    title:  title,
    yrange: "[0:#{groups.map(&:last).max * 1.1}]",
    xtics:  "#{[bin_size, 5].max}",
    style:  "fill solid 0.5 border"
  })
    
  plot([{ x: groups.map(&:first), y: groups.map(&:last), style: "boxes" }], options)
end

.plot(data, options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/vienna_rna/modules/graphing.rb', line 94

def plot(data, options = {})
  Gnuplot.open do |gnuplot|
    Gnuplot::Plot.new(gnuplot) do |plot|
      plot.autoscale
      
      case options[:output]
      when /file/i then
        plot.output(options[:filename])
        plot.terminal("png size %s" % (options[:dimensions] || "800,600"))
      end
      
      (options[:plot] || {}).keys.each do |option|
        plot.send(option, options[:plot][option])
      end

      plot.data = data.map do |data_hash|
        Gnuplot::DataSet.new([data_hash[:x], data_hash[:y]]) do |dataset|
          dataset.with      = data_hash[:style] || "points"
          dataset.linecolor = "rgb '#{data_hash[:color]}'" if data_hash[:color]

          data_hash[:title] ? dataset.title = data_hash[:title] : dataset.notitle
        end
      end
    end
  end
end

.quick_overlay(data, title = "", options = {}) ⇒ Object



183
184
185
186
187
188
189
# File 'lib/vienna_rna/modules/graphing.rb', line 183

def quick_overlay(data, title = "", options = {})
  # [{ data: [[x_0, y_0], [x_1, y_1], ...], label: "Label" }, { data: [[x_0, y_0], [x_1, y_1], ...] }]
  options[:plot] = ((options[:plot] || {}).merge(title: title))
  options.merge!(output: "file") if options[:filename]
  
  plot(data.map { |hash| { title: hash[:label], x: hash[:data].map(&:first), y: hash[:data].map(&:last), style: "linespoints" }.merge(hash[:options] || {}) }, options)
end

.quick_plot(data, title = "", options = {}) ⇒ Object



179
180
181
# File 'lib/vienna_rna/modules/graphing.rb', line 179

def quick_plot(data, title = "", options = {})
  quick_overlay([{ data: data }], title, options)
end

.roc(data, title = "", options = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/vienna_rna/modules/graphing.rb', line 165

def roc(data, title = "", options = {})
  # data = [[true_score_1, true_score_2, ...], [false_score_1, false_score_2, ...]]~
  roc_curve = ROC.curve_points({ 1 => data[0], -1 => data[1] }.inject([]) { |data, (truth, values)| data.concat(values.map { |i| [i, truth] })})
  area      = roc_curve.each_cons(2).inject(0) do |sum, (a, b)| 
    delta_x, delta_y = b[0] - a[0], b[1] - a[1]
    sum + (delta_x * delta_y / 2 + delta_x * [a[1], b[1]].min)
  end
  
  options.merge!(output: "file") if options[:filename]
  options.merge!({ plot: { title: "%s %s %.4f" % [title, "AUC:", area] } })
    
  plot([{ x: roc_curve.map(&:first), y: roc_curve.map(&:last), style: "lines" }], options)
end

.splot(data, options = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/vienna_rna/modules/graphing.rb', line 121

def splot(data, options = {})
  # [[x_1, y_1, z_1], [x_2, y_2, z_2], ...]
  orthogonal_data = data.inject([[], [], []]) { |array, list| array.zip(list).map { |a, e| a << e } }
  
  Gnuplot.open do |gnuplot|
    Gnuplot::SPlot.new(gnuplot) do |plot|
      plot.autoscale
      
      case options[:output]
      when /file/i then
        plot.output(options[:filename])
        plot.terminal("png size 800,600")
      end
      
      (options[:plot] || {}).keys.each do |option|
        plot.send(option, options[:plot][option])
      end

      plot.data = [
        Gnuplot::DataSet.new(orthogonal_data) do |dataset|
          dataset.with = options[:style] || "lines"
        end
      ]
    end
  end
end