3
$\begingroup$

I'm using the python API for the biom-format package. I read in two files in biom format and merged them, like so:

In [1]: import biom

In [2]: a = biom.load_table('./table-a.biom')

In [3]: b = biom.load_table('./table-b.biom')

In [4]: a
Out[4]: 51116 x 46 <class 'biom.table.Table'> with 185077 nonzero entries (7% dense)

In [5]: b
Out[5]: 37479 x 26 <class 'biom.table.Table'> with 100124 nonzero entries (10% dense)

In [6]: c = a.merge(b)

In [7]: c
Out[7]: 67022 x 47 <class 'biom.table.Table'> with 254555 nonzero entries (8% dense)

How do I now export this biom object (c) as a biom file? There are a few methods available, eg, .to_tsv(), but I don't see a way to save this object in biom format.

$\endgroup$
1
  • $\begingroup$ Are you using the biomformat package of bioconductor through Python? Or the biom-format package? Anyway in R the relevant function is write_biom so it might be the same in the python package $\endgroup$
    – llrs
    Oct 25 '17 at 12:42
4
$\begingroup$

You can use the functions to_hdf5 and to_json from the biom-format package:

to_hdf5

with biom.util.biom_open('output.biom', 'w') as f:
    merged_table.to_hdf5(f, "example")

to_json

with open('output.txt', 'w') as f:
    f.write(merged_table.to_json("example"))
$\endgroup$
1
$\begingroup$

There is a function in PiCRUST you can use

from picrust.util import write_biom_table

write_biom_table(c,'table-c.biom')

Note, however, that depending if you want json or hdf5 format and if your metadata are complex, you may need to use additional arguments. These are the arguments of the function:

def write_biom_table(biom_table, biom_table_fp, compress=True,
                 write_hdf5=HAVE_H5PY, format_fs=None):
"""Writes a BIOM table to the specified filepath

Parameters
----------
biom_table : biom.Table
    The table object to write out
biom_table_fp : str
    The path to the output file
compress : bool, optional
    Defaults to ``True``. If True, built-in compression on the output HDF5
    file will be enabled. This option is only relevant if ``write_hdf5`` is
    ``True``.
write_hdf5 : bool, optional
    Defaults to ``True`` if H5PY is installed and to ``False`` if H5PY is
    not installed. If ``True`` the output biom table will be written as an
    HDF5 binary file, otherwise it will be a JSON string.
format_fs : dict, optional
    Formatting functions to be passed to `Table.to_hdf5`

Notes
-----
This code was adapted from QIIME 1.9
"""

I have some problems to perform a simple operation and write the output of biom data generated with picrust, it might be helpful as an example of how to use the arguments.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.