Class: BioDSL::WriteTable

Inherits:
Object
  • Object
show all
Defined in:
lib/BioDSL/commands/write_table.rb

Overview

Write tabular output from the stream.

Description

write_table writes tabular output from the stream.

Usage

write_table([keys: <string> | skip: <string>][, output: <file>[, force:
            <bool>[, header: <bool>[, pretty: <bool>[, commify: <bool>
            [, delimiter: <string>[, first: <uint> | last: <uint>
            [, gzip: <bool>, [bzip2: <bool>]]]]]]]]]

Options

  • keys <string> - Comma separated list of keys to print in that order.

  • skip <string> - Comma separated list of keys to skip printing.

  • output <file> - Output file.

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

  • header <bool> - Output header.

  • pretty <bool> - Pretty print table.

  • commify <bool> - Commify numbers when pretty printing.

  • delimiter <string> - Specify delimiter (default=“t”).

  • first <uint> - Only output first number of rows.

  • last <uint> - Only output last number of rows.

  • gzip <bool> - Write gzipped output file.

  • bzip2 <bool> - Write bzipped output file.

Examples

Consider the following records in the stream:

{ORGANISM: Human
 COUNT: 23524
 SEQ: ATACGTCAG},
{ORGANISM: Dog
 COUNT: 2442
 SEQ: AGCATGAC},
{ORGANISM: Mouse
 COUNT: 234
 SEQ: GACTG},
{ORGANISM: Cat
 COUNT: 2342
 SEQ: AAATGCA}

To write all records from the stream as a table, do:

write_table()

Human  23524 ATACGTCAG
Dog  2442  AGCATGAC
Mouse  234 GACTG
Cat  2342  AAATGCA

If you supply the header option, then the first row in the table will be a ‘header’ line prefixed with a ‘#’:

write_table(header: true)

#ORGANISM  COUNT SEQ
Human  23524 ATACGTCAG
Dog  2442  AGCATGAC
Mouse  234 GACTG
Cat  2342  AAATGCA

You can also change the delimiter from the default (tab) to e.g. ‘,’:

write_table(delimiter: ',')

Human,23524,ATACGTCAG
Dog,2442,AGCATGAC
Mouse,234,GACTG
Cat,2342,AAATGCA

If you want the values output in a specific order you have to supply a comma separated list using the keys option that will print only those keys in that order:

write_table(keys: [:SEQ, :COUNT])

ATACGTCAG  23524
AGCATGAC 2442
GACTG  234
AAATGCA  2342

Keys in the format V0, V1, V2 … Vn, is automagically sorted numerically.

Alternatively, if you have some keys that you don’t want in the tabular output, use the skip option. So to print all keys except SEQ and SEQ_TYPE do:

write_table(skip: [:SEQ])

Human  23524
Dog  2442
Mouse  234
Cat  2342

And if you want a pretty printed table use the pretty option and throw in the commify option if you want commified numbers:

write_tab(pretty: true, header: true, commify: true)

+----------+--------+-----------+
| ORGANISM | COUNT  | SEQ       |
+----------+--------+-----------+
| Human    | 23,524 | ATACGTCAG |
| Dog      |  2,442 | AGCATGAC  |
| Mouse    |    234 | GACTG     |
| Cat      |  2,342 | AAATGCA   |
+----------+--------+-----------+

To write a table to a file ‘test.tab’:

write_table(output: "test.tab")

To write a table to a file ‘test.tab’ with only the first 3 rows:

write_table(output: "test.tab", first: 3)

To write a table to a file ‘test.tab’ with only the last 3 rows:

write_table(output: "test.tab", last: 3)

To overwrite output file if this exists use the force option:

write_table(output: "test.tab", force: true)

To write gzipped output to a file ‘test.tab.gz’.

write_table(output: "test.tab.gz", gzip: true)

To write bzipped output to a file ‘test.tab.bz2’.

write_table(output: "test.tab.bz2", bzip2: true)

Constant Summary collapse

STATS =
%i(records_in records_out)

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WriteTable

Constructor for WriteTable.

Parameters:

  • options (Hash)

    Options hash.

Options Hash (options):

  • :keys (Array)
  • :skip (Array)
  • :output (String)
  • :force (Boolean)
  • :header (Boolean)
  • :pretty (Boolean)
  • :commify (Boolean)
  • :delimiter (String)
  • :first (Fixnum)
  • :last (Fixnum)
  • :gzip (Boolean)
  • :bzip2 (Boolean)


187
188
189
190
191
192
193
194
195
196
# File 'lib/BioDSL/commands/write_table.rb', line 187

def initialize(options)
  @options = options
  check_options
  @options[:delimiter] ||= "\t"
  @compress              = choose_compression
  @headings              = nil
  @header                = @options[:header] ? true : false
  @last                  = []
  @rows                  = []
end

Instance Method Details

#lmbProc

Return command lambda for write_table.

Returns:

  • (Proc)

    Command lambda.



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/BioDSL/commands/write_table.rb', line 201

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

    if @options[:output]
      Filesys.open(@options[:output], 'w', compress: @compress) do |tab_out|
        write_table(input, output, tab_out)
      end
    else
      write_table(input, output, $stdout)
    end
  end
end