<p>You can use the functions <code>to_hdf5</code> and <code>to_json</code> from the <a href="http://biom-format.org/documentation/generated/biom.table.Table.html" rel="nofollow noreferrer">biom-format package</a>:</p>

<p><code>to_hdf5</code></p>

<pre><code>with biom.util.biom_open('output.biom', 'w') as f:
    merged_table.to_hdf5(f, "example")
</code></pre>

<p><code>to_json</code></p>

<pre><code>with open('output.txt', 'w') as f:
    f.write(merged_table.to_json("example"))
</code></pre>

CC BY-SA 4.0
2018-02-05T11:04:18.407Z
2018-08-02T20:50:44.240Z
2018-08-02T20:50:44.240Z
user3648523
<p>There is a function in <a href="http://picrust.github.io/" rel="nofollow noreferrer">PiCRUST</a> you can use</p>

<pre><code>from picrust.util import write_biom_table

write_biom_table(c,'table-c.biom')
</code></pre>

<p>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:</p>

<pre><code>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
"""
</code></pre>

<p>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 <a href="https://github.com/biocore/qiime/issues/2205" rel="nofollow noreferrer">example</a> of how to use the arguments.</p>

CC BY-SA 3.0
2017-10-26T22:18:03.483Z
2017-10-26T22:18:03.483Z
Alf Pascu
<p>I'm using the python API for the biom-format package. I read in two files in biom format and merged them, like so:</p>

<pre><code>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 &lt;class 'biom.table.Table'&gt; with 185077 nonzero entries (7% dense)

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

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

In [7]: c
Out[7]: 67022 x 47 &lt;class 'biom.table.Table'&gt; with 254555 nonzero entries (8% dense)
</code></pre>

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

CC BY-SA 3.0
2017-10-25T12:42:12.667Z
Are you using the [biomformat](https://bioconductor.org/packages/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
llrs
<python>
How do I export my biom object to a file in biom format?
# How do I export my biom object to a file in biom format?

(Asked by: username_0 on 2017-10-24)

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  with 185077 nonzero entries (7% dense)

In [5]: b
Out[5]: 37479 x 26  with 100124 nonzero entries (10% dense)

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

In [7]: c
Out[7]: 67022 x 47  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.

**Tags:** python
> **Comments:**
> **username_1**: Are you using the [biomformat](https://bioconductor.org/packages/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  

---

## 2 answers

### Answer by username_2 on 2018-02-05. Score: 4

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"))


---

### Answer by username_3 on 2017-10-26. Score: 1

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.


---

