| 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 |
import click |
| 11 |
import h5py |
| 12 |
|
| 13 |
from biom.cli import cli |
| 14 |
from biom.parse import (get_axis_indices, direct_slice_data, direct_parse_key, |
| 15 |
generatedby) |
| 16 |
from biom.table import Table |
| 17 |
from biom.util import biom_open |
| 18 |
|
| 19 |
|
| 20 |
@cli.command(name='subset-table') |
| 21 |
@click.option('-i', '--input-hdf5-fp', default=None, |
| 22 |
type=click.Path(exists=True, dir_okay=False), |
| 23 |
help='the input hdf5 BIOM table filepath to subset') |
| 24 |
@click.option('-j', '--input-json-fp', default=None, |
| 25 |
type=click.Path(exists=True, dir_okay=False), |
| 26 |
help='the input json BIOM table filepath to subset') |
| 27 |
@click.option('-a', '--axis', required=True, |
| 28 |
type=click.Choice(['sample', 'observation']), |
| 29 |
help='the axis to subset over, either sample or observation') |
| 30 |
@click.option('-s', '--ids', required=True, |
| 31 |
type=click.Path(exists=True, dir_okay=False), |
| 32 |
help='a file containing a single column of IDs to retain ' |
| 33 |
'(either sample IDs or observation IDs, depending on the ' |
| 34 |
'axis)') |
| 35 |
@click.option('-o', '--output-fp', required=True, |
| 36 |
type=click.Path(writable=True, dir_okay=False), |
| 37 |
help='the output BIOM table filepath') |
| 38 |
def subset_table(input_hdf5_fp, input_json_fp, axis, ids, output_fp): |
| 39 |
"""Subset a BIOM table. |
| 40 |
|
| 41 |
Subset a BIOM table, over either observations or samples, without fully |
| 42 |
parsing it. This command is intended to assist in working with very large |
| 43 |
tables when tight on memory, or as a lightweight way to subset a full |
| 44 |
table. Currently, it is possible to produce tables with rows or columns |
| 45 |
(observations or samples) that are fully zeroed. |
| 46 |
|
| 47 |
Example usage: |
| 48 |
|
| 49 |
Choose a subset of the observations in table.biom (JSON) and write them to |
| 50 |
subset.biom: |
| 51 |
|
| 52 |
$ biom subset-table -j table.biom -a observations -s observation_ids.txt \ |
| 53 |
-o subset.biom |
| 54 |
|
| 55 |
Choose a subset of the observations in table.biom (HDF5) and write them to |
| 56 |
subset.biom: |
| 57 |
|
| 58 |
$ biom subset-table -i table.biom -a observations -s observation_ids.txt \ |
| 59 |
-o subset.biom |
| 60 |
|
| 61 |
""" |
| 62 |
if input_json_fp is not None: |
| 63 |
with open(input_json_fp) as f: |
| 64 |
input_json_fp = f.read() |
| 65 |
|
| 66 |
with open(ids) as f: |
| 67 |
ids = [] |
| 68 |
for line in f: |
| 69 |
if not line.startswith('#'): |
| 70 |
ids.append(line.strip().split('\t')[0]) |
| 71 |
|
| 72 |
table, format_ = _subset_table(input_hdf5_fp, input_json_fp, axis, ids) |
| 73 |
|
| 74 |
if format_ == 'json': |
| 75 |
with open(output_fp, 'w') as f: |
| 76 |
for line in table: |
| 77 |
f.write(line) |
| 78 |
f.write('\n') |
| 79 |
else: |
| 80 |
with h5py.File(output_fp, 'w') as f: |
| 81 |
table.to_hdf5(f, generatedby()) |
| 82 |
|
| 83 |
|
| 84 |
def _subset_table(hdf5_biom, json_table_str, axis, ids): |
| 85 |
if axis not in ['sample', 'observation']: |
| 86 |
raise ValueError("Invalid axis '%s'. Must be either 'sample' or " |
| 87 |
"'observation'." % axis) |
| 88 |
|
| 89 |
if hdf5_biom is None and json_table_str is None: |
| 90 |
raise ValueError("Must specify an input table") |
| 91 |
elif hdf5_biom is not None and json_table_str is not None: |
| 92 |
raise ValueError("Can only specify one input table") |
| 93 |
|
| 94 |
if json_table_str is not None: |
| 95 |
idxs, new_axis_md = get_axis_indices(json_table_str, ids, axis) |
| 96 |
new_data = direct_slice_data(json_table_str, idxs, axis) |
| 97 |
|
| 98 |
# multiple walks over the string. bad form, but easy right now |
| 99 |
# ...should add a yield_and_ignore parser or something. |
| 100 |
def subset_generator(): |
| 101 |
yield "{" |
| 102 |
yield direct_parse_key(json_table_str, "id") |
| 103 |
yield "," |
| 104 |
yield direct_parse_key(json_table_str, "format") |
| 105 |
yield "," |
| 106 |
yield direct_parse_key(json_table_str, "format_url") |
| 107 |
yield "," |
| 108 |
yield direct_parse_key(json_table_str, "type") |
| 109 |
yield "," |
| 110 |
yield direct_parse_key(json_table_str, "generated_by") |
| 111 |
yield "," |
| 112 |
yield direct_parse_key(json_table_str, "date") |
| 113 |
yield "," |
| 114 |
yield direct_parse_key(json_table_str, "matrix_type") |
| 115 |
yield "," |
| 116 |
yield direct_parse_key(json_table_str, "matrix_element_type") |
| 117 |
yield "," |
| 118 |
yield new_data |
| 119 |
yield "," |
| 120 |
yield new_axis_md |
| 121 |
yield "," |
| 122 |
|
| 123 |
if axis == "observation": |
| 124 |
yield direct_parse_key(json_table_str, "columns") |
| 125 |
else: |
| 126 |
yield direct_parse_key(json_table_str, "rows") |
| 127 |
yield "}" |
| 128 |
|
| 129 |
format_ = 'json' |
| 130 |
table = subset_generator() |
| 131 |
else: |
| 132 |
with biom_open(hdf5_biom) as f: |
| 133 |
table = Table.from_hdf5(f, ids=ids, axis=axis) |
| 134 |
format_ = 'hdf5' |
| 135 |
|
| 136 |
return table, format_ |