biom.table.Table.partition

Table.partition(f, axis='sample', remove_empty=False, ignore_none=False)

Yields partitions

Parameters:
ffunction, dict

f is given the ID and metadata of the vector and must return what partition the vector is part of. If dict, a mapping of either ID -> group, or group -> [list, of, ID] must be provided.

axis{‘sample’, ‘observation’}, optional

The axis to iterate over

remove_emptybool, optional

If True, remove empty vectors from a partition. Default is False.

ignore_nonebool, optional

If True, ignore partitions with the label None. Default is False.

Returns:
GeneratorType

A generator that yields (partition, Table)

Examples

>>> import numpy as np
>>> from biom.table import Table
>>> from biom.util import unzip

Create a 2x3 BIOM table, with observation metadata and sample metadata:

>>> data = np.asarray([[0, 0, 1], [1, 3, 42]])
>>> table = Table(data, ['O1', 'O2'], ['S1', 'S2', 'S3'],
...               [{'full_genome_available': True},
...                {'full_genome_available': False}],
...               [{'sample_type': 'a'}, {'sample_type': 'a'},
...                {'sample_type': 'b'}])

Define a function to bin by sample_type

>>> f = lambda id_, md: md['sample_type']

Partition the table and view results

>>> bins, tables = table.partition(f)
>>> print(bins[1]) 
# Constructed from biom file
#OTU ID S1  S2
O1  0.0 0.0
O2  1.0 3.0
>>> print(tables[1]) 
# Constructed from biom file
#OTU ID S3
O1  1.0
O2  42.0