File: util.py

package info (click to toggle)
python-biom-format 2.1.5%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 51,844 kB
  • ctags: 1,341
  • sloc: python: 11,620; makefile: 128; sh: 65
file content (35 lines) | stat: -rw-r--r-- 1,062 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# ----------------------------------------------------------------------------
# Copyright (c) 2011-2015, The BIOM Format Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# ----------------------------------------------------------------------------

from __future__ import division

import biom.util
import biom.parse


def write_biom_table(table, fmt, filepath):
    """Write table in specified format to filepath"""

    if fmt not in ['hdf5', 'json', 'tsv']:
        raise ValueError("Unknown file format")

    if fmt == 'hdf5' and not biom.util.HAVE_H5PY:
        fmt = 'json'

    if fmt == 'json':
        with open(filepath, 'w') as f:
            f.write(table.to_json(biom.parse.generatedby()))
    elif fmt == 'tsv':
        with open(filepath, 'w') as f:
            f.write(table)
            f.write('\n')
    else:
        import h5py

        with h5py.File(filepath, 'w') as f:
            table.to_hdf5(f, biom.parse.generatedby())