class BioDSL::PlotHeatmap

Plot tabular numerical data in a heatmap.

A heatmap can be plotted with plot_heatmap using numerical data (Non- numerical data is ignored). Data should be tabular with records as rows and keys as columns - the data cells plotted will be the values.

Default graphics are crufty ASCII and you probably want high resolution postscript or SVG output instead with is easy using the terminal option. Plotting is done using GNUplot which allows for different types of output.

GNUplot must be installed for plot_heatmap to work. Read more here:

www.gnuplot.info/

Usage

plot_heatmap([keys: <list> | skip: <list>[, output: <file>
             [, force: <bool> [, terminal: <string>
             [, title: <string>[, xlabel: <string>[, ylabel: <string>
             [, test: <bool>]]]]]]])

Options

Examples

Here we plot a heatmap of data a table:

BD.new.read_table(input: "test.tab").plot_heatmap.run

Constants

STATS

Public Class Methods

new(options) click to toggle source

Constructor for PlotHeatmap.

@param options [Hash] Options hash. @option options [Array] :keys List of keys to plot as column. @option options [Array] :skip List of keys to skip as column. @option options [String] :output Path to output file. @option options [Boolean] :forcea Flag to force overwrite output file. @option options [Symbol] :terminal Set plot terminal type. @option options [String] :title Set plot title. @option options [String] :xlabel Set plot xlabel. @option options [String] :ylabel Set plot ylabel @option options [Boolean] :logscale Logscale Z-axis. @option options [Boolean] :test Output gnuplot script.

@return [PlotHeatmap] Class instance.

# File lib/BioDSL/commands/plot_heatmap.rb, line 93
def initialize(options)
  @options   = options
  @headings  = nil
  @skip_keys = determine_skip_keys

  aux_exist('gnuplot')
  check_options
  defaults
end

Public Instance Methods

lmb() click to toggle source

Return command lambda for plot_histogram.

@return [Proc] Command lambda.

# File lib/BioDSL/commands/plot_heatmap.rb, line 106
def lmb
  lambda do |input, output, status|
    status_init(status, STATS)

    gp = GnuPlotter.new

    plot_options(gp)
    plot_dataset(gp, input, output)
    plot_output(gp)
  end
end

Private Instance Methods

check_options() click to toggle source

Check options.

# File lib/BioDSL/commands/plot_heatmap.rb, line 121
def check_options
  options_allowed(@options, :keys, :skip, :output, :force, :terminal,
                  :title, :xlabel, :ylabel, :logscale, :test)
  options_unique(@options, :keys, :skip)
  options_allowed_values(@options, terminal: [:dumb, :post, :svg, :x11,
                                              :aqua, :png, :pdf])
  options_allowed_values(@options, test: [nil, true, false])
  options_allowed_values(@options, logscale: [nil, true, false])
  options_files_exist_force(@options, :output)
end
defaults() click to toggle source

Set default options.

# File lib/BioDSL/commands/plot_heatmap.rb, line 133
def defaults
  @options[:terminal] ||= :dumb
  @options[:title] ||= 'Heatmap'
  @options[:xlabel] ||= 'x'
  @options[:ylabel] ||= 'y'
end
determine_headings(record) click to toggle source

Determine the headings.

@param record [Hash] BioDSL record.

# File lib/BioDSL/commands/plot_heatmap.rb, line 151
def determine_headings(record)
  @headings =
    if @options[:keys]
      @options[:keys].map(&:to_sym)
    elsif record.keys.first =~ /^V\d+$/
      sort_keys(record)
    else
      record.keys
    end

  @headings.reject! { |r| @skip_keys.include? r } if @options[:skip]
end
determine_skip_keys() click to toggle source

Compile a set of keys to skip.

@return [Set] Set of keys to skip.

# File lib/BioDSL/commands/plot_heatmap.rb, line 143
def determine_skip_keys
  return unless @options[:skip]
  @options[:skip].each_with_object(Set.new) { |e, a| a << e.to_sym }
end
plot_dataset(gp, input, output) click to toggle source

Plot relevant data from the input stream.

@param gp [GnuPlotter] GnuPlotter object. @param input [Enumerator] Input stream. @param output [Enumerator::Yielder] Output stream.

# File lib/BioDSL/commands/plot_heatmap.rb, line 201
def plot_dataset(gp, input, output)
  gp.add_dataset(matrix: :true, with: 'image') do |plotter|
    input.each do |record|
      @status[:records_in] += 1

      determine_headings(record) unless @headings

      plotter << record.values_at(*@headings)

      next unless output

      output << record

      @status[:records_out] += 1
    end
  end
end
plot_options(gp) click to toggle source

Set options for plot.

@param gp [GnuPlotter] GnuPlotter object.

# File lib/BioDSL/commands/plot_heatmap.rb, line 179
def plot_options(gp)
  gp.set terminal:  @options[:terminal].to_s
  gp.set title:     @options[:title]
  gp.set xlabel:    @options[:xlabel]
  gp.set ylabel:    @options[:ylabel]
  gp.set output:    @options[:output] if @options[:output]
  gp.set view:      'map'
  gp.set autoscale: 'xfix'
  gp.set autoscale: 'yfix'
  gp.set nokey:     true
  gp.set tic:       'scale 0'
  gp.set palette:   'rgbformulae 22,13,10'
  gp.set logscale:  'cb' if @options[:logscale]
  gp.unset xtics:   true
  gp.unset ytics:   true
end
plot_output(gp) click to toggle source

Output plot data according to options.

@param gp [GnuPlotter] GnuPlotter object.

# File lib/BioDSL/commands/plot_heatmap.rb, line 222
def plot_output(gp)
  if @options[:test]
    $stderr.puts gp.to_gp
  elsif @options[:terminal] == :dumb
    puts gp.splot
  else
    gp.splot
  end
end
sort_keys(record) click to toggle source

Sort records keys numerically, when the keys are in the format Vn, where n is an Integer.

@param record [Hash] BioDSL record.

@return [Array] List of sorted keys.

# File lib/BioDSL/commands/plot_heatmap.rb, line 170
def sort_keys(record)
  record.keys.sort do |a, b|
    a.to_s[1..a.to_s.size].to_i <=> b.to_s[1..a.to_s.size].to_i
  end
end