Debian Codegraph @

biom/cli/table_head.py

raw
1 # ----------------------------------------------------------------------------
2 # Copyright (c) 2011-2017, The BIOM Format Development Team.
3 #
4 # Distributed under the terms of the Modified BSD License.
5 #
6 # The full license is in the file COPYING.txt, distributed with this software.
7 # ----------------------------------------------------------------------------
8
9
10 import click
11
12 from biom import load_table
13 from biom.cli import cli
14
15
16 @cli.command()
17 @click.option('-i', '--input-fp', required=True,
18 type=click.Path(exists=True, dir_okay=False),
19 help='The input BIOM table')
20 @click.option('-o', '--output-fp', default=None,
21 type=click.Path(writable=True),
22 help='An output file-path', required=False)
23 @click.option('-n', '--n-obs', default=5, type=int,
24 help="The number of observations to show",
25 required=False)
26 @click.option('-m', '--n-samp', default=5, type=int,
27 help="The number of samples to show",
28 required=False)
29 def head(input_fp, output_fp, n_obs, n_samp):
30 """Dump the first bit of a table.
31
32 Example usage:
33
34 Print out the upper left corner of a BIOM table to standard out:
35
36 $ biom head -i table.biom
37
38 """
39 if n_obs == 0 or n_samp == 0:
40 raise ValueError("Sample and observation identifiers can be obtained "
41 "from 'biom table-ids'")
42
43 if n_obs < 0:
44 raise ValueError("-n/--n-obs must be > 0")
45
46 if n_samp < 0:
47 raise ValueError("-m/--m-samp must be > 0")
48
49 table = load_table(input_fp).head(n=n_obs, m=n_samp)
50
51 if output_fp is None:
52 click.echo(str(table))
53 else:
54 with open(output_fp, 'w') as fp:
55 fp.write(str(table))