Skip to content

Commit

Permalink
Merge pull request #671 from jairideout/convert-finalization
Browse files Browse the repository at this point in the history
biom convert finalization
  • Loading branch information
wasade committed Oct 20, 2015
2 parents 4558caa + cf4408d commit 00d0bbd
Show file tree
Hide file tree
Showing 5 changed files with 354 additions and 41 deletions.
29 changes: 2 additions & 27 deletions biom/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,8 @@
from .metadata_adder import add_metadata
from .table_validator import validate_table
from .table_converter import convert

import biom.parse
import biom.util
from .util import write_biom_table

__all__ = ['validate_table', 'summarize_table', 'add_metadata',
'show_install_info', 'normalize_table', 'subset_table',
'convert']


def write_biom_table(table, fmt, filepath):
"""Write table in specified format to filepath"""

if fmt not in ['hdf5', 'json', 'tsv']:
raise ValueError("Unknown file format")

if fmt == 'hdf5' and not biom.util.HAVE_H5PY:
fmt = 'json'

if fmt == 'json':
with open(filepath, 'w') as f:
f.write(table.to_json(biom.parse.generatedby()))
elif fmt == 'tsv':
with open(filepath, 'w') as f:
f.write(table)
f.write('\n')
else:
import h5py

with h5py.File(filepath, 'w') as f:
table.to_hdf5(f, biom.parse.generatedby())
'convert', 'write_biom_table']
29 changes: 23 additions & 6 deletions biom/cli/table_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from __future__ import division

from biom.cli.util import write_biom_table

table_types = ["OTU table",
"Pathway table",
"Function table",
Expand All @@ -30,17 +32,26 @@
}


def convert(table, sample_metadata, observation_metadata,
to_json, to_hdf5, to_tsv, collapsed_samples,
collapsed_observations, header_key, output_metadata_id, table_type,
process_obs_metadata, tsv_metadata_formatter):
def convert(table, output_filepath, sample_metadata=None,
observation_metadata=None,
to_json=False, to_hdf5=False, to_tsv=False,
collapsed_samples=False, collapsed_observations=False,
header_key=None, output_metadata_id=None, table_type=None,
process_obs_metadata=None, tsv_metadata_formatter='sc_separated'):

if sum([to_tsv, to_hdf5, to_json]) == 0:
raise ValueError("Must specify an output format")
elif sum([to_tsv, to_hdf5, to_json]) > 1:
raise ValueError("Can only specify a single output format")

table.type = table_type
if table_type is None:
if table.type in [None, "None"]:
table.type = "Table"
else:
pass
else:
table.type = table_type

if tsv_metadata_formatter is not None:
obs_md_fmt_f = observation_metadata_formatters[tsv_metadata_formatter]

Expand Down Expand Up @@ -75,9 +86,14 @@ def convert(table, sample_metadata, observation_metadata,
result = table.to_tsv(header_key=header_key,
header_value=output_metadata_id,
metadata_formatter=obs_md_fmt_f)
with open(output_filepath, 'w') as f:
f.write(result)
return
elif to_json:
fmt = 'json'
result = table
elif to_hdf5:
fmt = 'hdf5'
result = table
if collapsed_observations:
metadata = [{'collapsed_ids': md.keys()}
Expand All @@ -91,5 +107,6 @@ def convert(table, sample_metadata, observation_metadata,
# We have changed the metadata, it is safer to make sure that
# it is correct
result._cast_metadata()
write_biom_table(result, fmt, output_filepath)

return result
return
33 changes: 33 additions & 0 deletions biom/cli/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# ----------------------------------------------------------------------------
# Copyright (c) 2011-2015, The BIOM Format Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# ----------------------------------------------------------------------------

import biom.util
import biom.parse


def write_biom_table(table, fmt, filepath):
"""Write table in specified format to filepath"""

if fmt not in ['hdf5', 'json', 'tsv']:
raise ValueError("Unknown file format")

if fmt == 'hdf5' and not biom.util.HAVE_H5PY:
fmt = 'json'

if fmt == 'json':
with open(filepath, 'w') as f:
f.write(table.to_json(biom.parse.generatedby()))
elif fmt == 'tsv':
with open(filepath, 'w') as f:
f.write(table)
f.write('\n')
else:
import h5py

with h5py.File(filepath, 'w') as f:
table.to_hdf5(f, biom.parse.generatedby())
15 changes: 7 additions & 8 deletions scripts/biom
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def add_metadata(input_fp, output_fp, sample_metadata_fp,
help='The name to be given to the observation metadata '
'column when creating a tsv table file if the column '
'should be renamed.')
@click.option('--table-type', required=False, default="Table",
@click.option('--table-type', required=False,
type=click.Choice(biom.cli.table_converter.table_types),
help='The type of the table.')
@click.option('--process-obs-metadata', required=False,
Expand Down Expand Up @@ -253,13 +253,12 @@ def convert(input_fp, output_fp, sample_metadata_fp, observation_metadata_fp,
else:
observation_metadata_f = None

result = biom.cli.convert(table, sample_metadata_f, observation_metadata_f,
to_json, to_hdf5, to_tsv, collapsed_samples,
collapsed_observations, header_key,
output_metadata_id, table_type,
process_obs_metadata, tsv_metadata_formatter)

biom.cli.write_biom_table(result, fmt, output_fp)
biom.cli.convert(table, output_fp, sample_metadata_f,
observation_metadata_f,
to_json, to_hdf5, to_tsv, collapsed_samples,
collapsed_observations, header_key,
output_metadata_id, table_type,
process_obs_metadata, tsv_metadata_formatter)

@cli.command(name='show-install-info')
def show_install_info():
Expand Down
Loading

0 comments on commit 00d0bbd

Please sign in to comment.