Skip to content

Commit

Permalink
MAINT: use Table.partition
Browse files Browse the repository at this point in the history
  • Loading branch information
wasade authored and gregcaporaso committed Dec 17, 2024
1 parent 46a6f1c commit b3e8028
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions q2_feature_table/_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ def split(table: biom.Table,
filter_empty_features: bool = True) -> biom.Table:
metadata = metadata.filter_ids(table.ids(axis='sample'))
metadata_df = metadata.drop_missing_values().to_dataframe()
lookup = metadata_df[metadata.name].to_dict()

indices = metadata_df.reset_index(
).groupby(metadata.name)[metadata_df.index.name].apply(list).to_dict()
def partition_f(i, m):
return lookup.get(i)

unique_grps = sorted(set(lookup.values()))
try:
qiime2.sdk.util.validate_result_collection_keys(*indices.keys())
qiime2.sdk.util.validate_result_collection_keys(*unique_grps)
except KeyError as e:
raise KeyError(
"One or more invalid metadata column values identified during "
Expand All @@ -29,9 +31,11 @@ def split(table: biom.Table,
f"table. The original error message is as follows: {str(e)}")

result = {}
for group, sample_ids in indices.items():
t = table.filter(sample_ids, axis='sample', inplace=False)
for group, tab in table.partition(partition_f):
if group is None:
continue

if filter_empty_features:
t.remove_empty(axis='observation', inplace=True)
result[group] = t
tab.remove_empty(axis='observation', inplace=True)
result[group] = tab
return result

0 comments on commit b3e8028

Please sign in to comment.