Class: BioDSL::PlotMatches

Inherits:
Object
  • Object
show all
Includes:
AuxHelper
Defined in:
lib/BioDSL/commands/plot_matches.rb

Overview

Plot matches from the stream as a dotplot.

plot_matches is used to create dotplots of matches in the stream. plot_matches uses Q_BEG, Q_END, S_BEG, S_END from the stream. If strand information is available either by a STRAND key with the value ‘+’ or ‘-’, or by a DIRECTION key with the value ‘forward’ or ‘reverse’ then forward matches will be output in green and reverse matches in red (in all terminals, but dumb).

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_matches to work. Read more here:

www.gnuplot.info/

Usage

plot_matches([direction: <string>[, output: <file>[, force: <bool>
             [, terminal: <string>[, title: <string>[, xlabel: <string>
             [, ylabel: <string>[, test: <bool>]]]]]]]])

Options

  • direction: <string> - Plot matches from forward|reverse|both direction(s)

    (default=both).
    
  • output: <file> - Output file.

  • force: <bool> - Force overwrite existing output file.

  • terminal: <string> - Terminal for output: dumb|post|svg|x11|aqua|png|pdf

    (default=dumb).
    
  • title: <string> - Plot title (default=“Matches”).

  • xlabel: <string> - X-axis label (default=“x”).

  • ylabel: <string> - Y-axis label (default=“y”).

  • test: <bool> - Output Gnuplot script instead of plot.

Examples

Here we plot two matches from a table. The vector records are shown in the dump output:

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

{:Q_BEG=>0, :Q_END=>10, :S_BEG=>0, :S_END=>10, :STRAND=>"+"}
{:Q_BEG=>0, :Q_END=>10, :S_BEG=>0, :S_END=>10, :STRAND=>"-"}

                                     Matches
      +             +             +            +             +             +
  10 +>>>-----------+-------------+------------+-------------+----------->>>+
      |  >>>>       :             :            :             :       >>>>  |
      |      >>>>   :             :            :             :   >>>>      |
   8 ++..........>>>>>......................................>>>>>..........++
      |             : >>>>        :            :        >>>> :             |
      |             :     >>>>    :            :    >>>>     :             |
   6 ++.......................>>>>>............>>>>>.......................++
      |             :             :>>>>    >>>>:             :             |
      |             :             :    >>>>    :             :             |
      |             :             :>>>>    >>>>:             :             |
   4 ++.......................>>>>>............>>>>>.......................++
      |             :     >>>>    :            :    >>>>     :             |
      |             : >>>>        :            :        >>>> :             |
   2 ++..........>>>>>......................................>>>>>..........++
      |      >>>>   :             :            :             :   >>>>      |
      |  >>>>       :             :            :             :       >>>>  |
   0 +>>>-----------+-------------+------------+-------------+----------->>>+
      +             +             +            +             +             +
      0             2             4            6             8             10
                                        x

To render X11 output (i.e. instant view) use the terminal option:

plot_matches(terminal: :x11).run

To generate a PNG image and save to file:

plot_matches(terminal: :png, output: "plot.png").run

rubocop:disable ClassLength rubocop:enable LineLength

Constant Summary collapse

STATS =
%i(records_in records_out matches_in)

Instance Method Summary collapse

Methods included from AuxHelper

#aux_exist

Constructor Details

#initialize(options) ⇒ PlotMatches

Constructor for PlotMatches.

Parameters:

  • options (Hash)

    Options hash.

Options Hash (options):

  • :direction (Symbol)
  • :output (String)
  • :force (Boolean)
  • :terminal (Symbol)
  • :title (String)
  • :xlabel (String)
  • :ylabel (String)
  • :test (Boolean)


130
131
132
133
134
135
136
137
138
139
# File 'lib/BioDSL/commands/plot_matches.rb', line 130

def initialize(options)
  @options  = options
  @gp       = nil
  @style1   = {using: '1:2:3:4', with: 'vectors nohead ls 1'}
  @style2   = {using: '1:2:3:4', with: 'vectors nohead ls 2'}

  aux_exist('gnuplot')
  check_options
  defaults
end

Instance Method Details

#lmbProc

Return lambda for command plot_matches.

Returns:

  • (Proc)

    Command lambda.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/BioDSL/commands/plot_matches.rb', line 144

def lmb
  lambda do |input, output, status|
    status_init(status, STATS)

    @gp = GnuPlotter.new
    plot_defaults

    @gp.add_dataset(@style1) do |forward|
      @gp.add_dataset(@style2) do |reverse|
        input.each do |record|
          @status[:records_in] += 1

          plot_match(forward, reverse, record)

          process_output(output, record)
        end
      end
    end

    plot_output
  end
end