Debian Codegraph @

biom/cli/metadata_exporter.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 import click
10
11 from biom import load_table
12 from biom.cli import cli
13
14
15 @cli.command(name='export-metadata')
16 @click.option('-i', '--input-fp', required=True,
17 type=click.Path(exists=True, dir_okay=False),
18 help='The input BIOM table')
19 @click.option('-m', '--sample-metadata-fp', required=False,
20 type=click.Path(exists=False, dir_okay=False),
21 help='The sample metadata output file.')
22 @click.option('--observation-metadata-fp', required=False,
23 type=click.Path(exists=False, dir_okay=False),
24 help='The observation metadata output file.')
25 def export_metadata(input_fp, sample_metadata_fp, observation_metadata_fp):
26 """Export metadata as TSV.
27
28 Example usage:
29
30 Export metadata as TSV:
31
32 $ biom export-metadata -i otu_table.biom
33 --sample-metadata-fp sample.tsv
34 --observation-metadata-fp observation.tsv
35 """
36 table = load_table(input_fp)
37
38 if sample_metadata_fp:
39 _export_metadata(table, 'sample', input_fp, sample_metadata_fp)
40 if observation_metadata_fp:
41 _export_metadata(table, 'observation', input_fp,
42 observation_metadata_fp)
43
44
45 def _export_metadata(table, axis, input_fp, output_fp):
46 try:
47 metadata = table.metadata_to_dataframe(axis)
48 metadata.to_csv(output_fp, sep='\t')
49 except KeyError:
50 click.echo('File {} does not contain {} metadata'.format(input_fp,
51 axis))