File: output_handler.py

package info (click to toggle)
python-biom-format 2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,548 kB
  • ctags: 1,051
  • sloc: python: 6,257; makefile: 137; sh: 52
file content (80 lines) | stat: -rw-r--r-- 2,674 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python

# ----------------------------------------------------------------------------
# Copyright (c) 2011-2013, 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.
# ----------------------------------------------------------------------------

__author__ = "Greg Caporaso"
__copyright__ = "Copyright 2011-2013, The BIOM Format Development Team"
__credits__ = ["Greg Caporaso", "Jai Ram Rideout"]
__license__ = "BSD"
__maintainer__ = "Greg Caporaso"
__email__ = "gregcaporaso@gmail.com"

from os.path import exists
from pyqi.core.exception import IncompetentDeveloperError
from pyqi.core.interfaces.optparse.output_handler import write_list_of_strings
from biom.parse import generatedby
from biom.util import HAVE_H5PY


def write_subsetted_biom_table(result_key, data, option_value=None):
    """Write a string to a file"""
    if option_value is None:
        raise IncompetentDeveloperError("Cannot write output without a "
                                        "filepath.")

    if exists(option_value):
        raise IOError("Output path '%s' already exists." % option_value)

    table, fmt = data

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

    if fmt == 'json':
        write_list_of_strings(result_key, table, option_value)
    else:
        if HAVE_H5PY:
            import h5py
        else:
            # This should never be raised here
            raise ImportError("h5py is not available, cannot write HDF5!")

        with h5py.File(option_value, 'w') as f:
            table.to_hdf5(f, generatedby())


def write_biom_table(result_key, data, option_value=None):
    """Write a string to a file"""
    if option_value is None:
        raise IncompetentDeveloperError("Cannot write output without a "
                                        "filepath.")

    if exists(option_value):
        raise IOError("Output path '%s' already exists." % option_value)

    table, fmt = data

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

    if fmt == 'json':
        with open(option_value, 'w') as f:
            f.write(table.to_json(generatedby()))
    elif fmt == 'tsv':
        with open(option_value, 'w') as f:
            f.write(table)
            f.write('\n')
    else:
        if HAVE_H5PY:
            import h5py
        else:
            raise ImportError("h5py is not available, cannot write HDF5!")

        with h5py.File(option_value, 'w') as f:
            table.to_hdf5(f, generatedby())