File: _normalize.py

package info (click to toggle)
q2-feature-table 2022.11.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,576 kB
  • sloc: javascript: 34,249; python: 5,605; makefile: 35; sh: 25
file content (25 lines) | stat: -rw-r--r-- 1,026 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
# ----------------------------------------------------------------------------
# Copyright (c) 2016-2022, QIIME 2 development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------

import biom


def rarefy(table: biom.Table, sampling_depth: int,
           with_replacement: bool = False) -> biom.Table:
    if with_replacement:
        table = table.filter(lambda v, i, m: v.sum() >= sampling_depth,
                             inplace=False, axis='sample')
    table = table.subsample(sampling_depth, axis='sample', by_id=False,
                            with_replacement=with_replacement)

    if table.is_empty():
        raise ValueError('The rarefied table contains no samples or features. '
                         'Verify your table is valid and that you provided a '
                         'shallow enough sampling depth.')

    return table