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
|
#!/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"
import json
from biom.util import biom_open
from biom.parse import MetadataMap, parse_biom_table
from pyqi.core.interfaces.optparse.input_handler import load_file_contents
def biom_load_file_contents(fp):
if fp is None:
return fp
return load_file_contents(fp)
def load_hdf5_or_json(fp):
"""Return a parsed JSON object or an HDF5 object"""
with biom_open(fp) as f:
if hasattr(f, 'seek'):
return json.load(f)
else:
return f
def load_biom_table(biom_fp):
"""Return a parsed BIOM table."""
with biom_open(biom_fp, 'U') as table_f:
return parse_biom_table(table_f)
def load_biom_table_with_file_contents(biom_fp):
"""Return a BIOM table and the original open filehandle as a tuple.
Useful when additional computation needs to be performed on the file
contents, such as an MD5 sum.
WARNING: this function does not close the open filehandle that it returns.
Users of this function are responsible for closing the filehandle when done
using it!
"""
with biom_open(biom_fp, 'U') as biom_f:
table = parse_biom_table(biom_f)
if hasattr(biom_f, 'seek'):
biom_f.seek(0)
return table, biom_f
def load_json_document(fp):
"""Return a parsed JSON object."""
with biom_open(fp, 'U') as f:
return json.load(f)
def load_metadata(fp):
"""Parse a sample/observation metadata file, return a ``MetadataMap``.
If ``fp`` is ``None``, this function will return ``None``.
"""
if fp is None:
return None
else:
with open(fp, 'U') as f:
return MetadataMap.from_file(f)
|