Skip to content

Commit

Permalink
IMP: AlphaDiversityFormat to Metadata transformer supports multi-colu…
Browse files Browse the repository at this point in the history
…mn (#193)
  • Loading branch information
gregcaporaso authored and ebolyen committed Aug 28, 2018
1 parent 52395ec commit 45b2949
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 15 additions & 5 deletions q2_types/sample_data/_transformer.py
Expand Up @@ -7,6 +7,7 @@
# ----------------------------------------------------------------------------

import pandas as pd
import numpy as np

import qiime2

Expand All @@ -20,7 +21,11 @@ def _read_alpha_diversity(fh):
df = pd.read_csv(fh, sep='\t', header=0, dtype=object)
df.set_index(df.columns[0], drop=True, append=False, inplace=True)
df.index.name = None
return pd.to_numeric(df.iloc[:, 0], errors='raise')
# casting of columns adapted from SO post:
# https://stackoverflow.com/a/36814203/3424666
cols = df.columns
df[cols] = df[cols].apply(pd.to_numeric, errors='ignore')
return df


@plugin.register_transformer
Expand All @@ -34,12 +39,17 @@ def _1(data: pd.Series) -> AlphaDiversityFormat:
@plugin.register_transformer
def _2(ff: AlphaDiversityFormat) -> pd.Series:
with ff.open() as fh:
return _read_alpha_diversity(fh)
df = _read_alpha_diversity(fh)
series = df.iloc[:, 0]
if not np.issubdtype(series, np.number):
raise ValueError('Non-numeric values detected in alpha diversity '
'estimates.')
return series


@plugin.register_transformer
def _3(ff: AlphaDiversityFormat) -> qiime2.Metadata:
with ff.open() as fh:
series = _read_alpha_diversity(fh)
series.index.name = 'Sample ID'
return qiime2.Metadata(series.to_frame())
df = _read_alpha_diversity(fh)
df.index.name = 'Sample ID'
return qiime2.Metadata(df)
2 changes: 1 addition & 1 deletion q2_types/sample_data/tests/test_transformer.py
Expand Up @@ -72,7 +72,7 @@ def test_alpha_diversity_format_to_metadata(self):

def test_non_alpha_diversity(self):
filename = 'also-not-alpha-diversity.tsv'
with self.assertRaisesRegex(ValueError, 'Unable to parse string'):
with self.assertRaisesRegex(ValueError, 'Non-numeric values '):
self.transform_format(AlphaDiversityFormat, pd.Series, filename)


Expand Down

0 comments on commit 45b2949

Please sign in to comment.