Debian Codegraph @

biom/cli/util.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 biom.util
11 import biom.parse
12 import h5py
13
14
15 def write_biom_table(table, fmt, filepath):
16 """Write table in specified format to filepath"""
17
18 if fmt not in ['hdf5', 'json', 'tsv']:
19 raise ValueError("Unknown file format")
20
21 if fmt == 'json':
22 with open(filepath, 'w') as f:
23 f.write(table.to_json(biom.parse.generatedby()))
24 elif fmt == 'tsv':
25 with open(filepath, 'w') as f:
26 f.write(table)
27 f.write('\n')
28 else:
29
30 with h5py.File(filepath, 'w') as f:
31 table.to_hdf5(f, biom.parse.generatedby())