| 1 |
# ----------------------------------------------------------------------------- |
| 2 |
# Copyright (c) 2011-2017, The BIOM Format Development Team. |
| 3 |
# |
| 4 |
# Distributed under the terms of the Modified BSD License. |
| 5 |
# |
| 6 |
# The full license is in the file COPYING.txt, distributed with this software. |
| 7 |
# ----------------------------------------------------------------------------- |
| 8 |
|
| 9 |
|
| 10 |
from operator import itemgetter |
| 11 |
import locale |
| 12 |
|
| 13 |
import click |
| 14 |
from numpy import std |
| 15 |
|
| 16 |
from biom import load_table |
| 17 |
from biom.cli import cli |
| 18 |
from biom.util import compute_counts_per_sample_stats |
| 19 |
|
| 20 |
|
| 21 |
@cli.command(name='summarize-table') |
| 22 |
@click.option('-i', '--input-fp', required=True, |
| 23 |
type=click.Path(exists=True, dir_okay=False), |
| 24 |
help='The input BIOM table') |
| 25 |
@click.option('-o', '--output-fp', default=None, |
| 26 |
type=click.Path(writable=True, dir_okay=False), |
| 27 |
help='An output file-path') |
| 28 |
@click.option('--qualitative', default=False, is_flag=True, |
| 29 |
help="Present counts as number of unique observation ids per" |
| 30 |
" sample, rather than counts of observations per sample.") |
| 31 |
@click.option('--observations', default=False, is_flag=True, |
| 32 |
help="Summarize over observations") |
| 33 |
def summarize_table(input_fp, output_fp, qualitative, observations): |
| 34 |
"""Summarize sample or observation data in a BIOM table. |
| 35 |
|
| 36 |
Provides details on the observation counts per sample, including summary |
| 37 |
statistics, as well as metadata categories associated with samples and |
| 38 |
observations. |
| 39 |
|
| 40 |
Example usage: |
| 41 |
|
| 42 |
Write a summary of table.biom to table_summary.txt: |
| 43 |
|
| 44 |
$ biom summarize-table -i table.biom -o table_summary.txt |
| 45 |
|
| 46 |
""" |
| 47 |
table = load_table(input_fp) |
| 48 |
result = _summarize_table(table, qualitative, observations) |
| 49 |
if output_fp: |
| 50 |
with open(output_fp, 'w') as fh: |
| 51 |
fh.write(result) |
| 52 |
else: |
| 53 |
click.echo(result) |
| 54 |
|
| 55 |
|
| 56 |
def _summarize_table(table, qualitative=False, observations=False): |
| 57 |
lines = [] |
| 58 |
locale.setlocale(locale.LC_ALL, '') |
| 59 |
|
| 60 |
if observations: |
| 61 |
table = table.transpose() |
| 62 |
|
| 63 |
min_counts, max_counts, median_counts, mean_counts, counts_per_samp =\ |
| 64 |
compute_counts_per_sample_stats(table, qualitative) |
| 65 |
num_observations = len(table.ids(axis='observation')) |
| 66 |
|
| 67 |
counts_per_sample_values = list(counts_per_samp.values()) |
| 68 |
|
| 69 |
if table.metadata() is None: |
| 70 |
sample_md_keys = ["None provided"] |
| 71 |
else: |
| 72 |
sample_md_keys = table.metadata()[0].keys() |
| 73 |
|
| 74 |
if table.metadata(axis='observation') is None: |
| 75 |
observation_md_keys = ["None provided"] |
| 76 |
else: |
| 77 |
observation_md_keys = table.metadata(axis='observation')[0].keys() |
| 78 |
|
| 79 |
num_samples = len(table.ids()) |
| 80 |
|
| 81 |
if observations: |
| 82 |
# as this is a transpose of the original table... |
| 83 |
lines.append('Num samples: ' + locale.format_string('%d', |
| 84 |
num_observations, grouping=True)) |
| 85 |
lines.append('Num observations: ' + locale.format_string('%d', |
| 86 |
num_samples, grouping=True)) |
| 87 |
else: |
| 88 |
lines.append('Num samples: ' + locale.format_string('%d', |
| 89 |
num_samples, grouping=True)) |
| 90 |
lines.append('Num observations: ' + locale.format_string('%d', |
| 91 |
num_observations, grouping=True)) |
| 92 |
|
| 93 |
if not qualitative: |
| 94 |
total_count = sum(counts_per_sample_values) |
| 95 |
lines.append('Total count: ' + locale.format_string('%d', |
| 96 |
total_count, grouping=True)) |
| 97 |
lines.append('Table density (fraction of non-zero values): %1.3f' % |
| 98 |
table.get_table_density()) |
| 99 |
|
| 100 |
lines.append('') |
| 101 |
|
| 102 |
if qualitative: |
| 103 |
if observations: |
| 104 |
lines.append('Sample/observations summary:') |
| 105 |
else: |
| 106 |
lines.append('Observations/sample summary:') |
| 107 |
else: |
| 108 |
lines.append('Counts/sample summary:') |
| 109 |
|
| 110 |
lines.append(' Min: ' + locale.format_string('%1.3f', |
| 111 |
min_counts, grouping=True)) |
| 112 |
lines.append(' Max: ' + locale.format_string('%1.3f', |
| 113 |
max_counts, grouping=True)) |
| 114 |
lines.append(' Median: ' + locale.format_string('%1.3f', |
| 115 |
median_counts, grouping=True)) |
| 116 |
lines.append(' Mean: ' + locale.format_string('%1.3f', |
| 117 |
mean_counts, grouping=True)) |
| 118 |
lines.append(' Std. dev.: ' + locale.format_string('%1.3f', |
| 119 |
std(counts_per_sample_values), grouping=True)) |
| 120 |
|
| 121 |
if observations: |
| 122 |
# since this is a transpose... |
| 123 |
lines.append( |
| 124 |
' Sample Metadata Categories: %s' % |
| 125 |
'; '.join(observation_md_keys)) |
| 126 |
lines.append( |
| 127 |
' Observation Metadata Categories: %s' % |
| 128 |
'; '.join(sample_md_keys)) |
| 129 |
lines.append('') |
| 130 |
else: |
| 131 |
lines.append( |
| 132 |
' Sample Metadata Categories: %s' % |
| 133 |
'; '.join(sample_md_keys)) |
| 134 |
lines.append( |
| 135 |
' Observation Metadata Categories: %s' % |
| 136 |
'; '.join(observation_md_keys)) |
| 137 |
lines.append('') |
| 138 |
|
| 139 |
if qualitative: |
| 140 |
lines.append('Observations/sample detail:') |
| 141 |
else: |
| 142 |
lines.append('Counts/sample detail:') |
| 143 |
|
| 144 |
for k, v in sorted(counts_per_samp.items(), key=itemgetter(1)): |
| 145 |
lines.append('%s: ' % k + locale.format_string('%1.3f', |
| 146 |
v, grouping=True)) |
| 147 |
|
| 148 |
return "\n".join(lines) |