Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Improve detection of annotation when converting h5ad to loom. Integer…
Browse files Browse the repository at this point in the history
… in sample-based metadata file will be correctly saved as annotations
  • Loading branch information
dweemx committed May 8, 2020
1 parent c186f78 commit d599b9a
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/utils/bin/h5ad_to_loom.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,26 @@ def read_h5ad(file_path, backed='r'):
attrs_metadata["metrics"] = []
attrs_metadata["annotations"] = []


def is_annotation(adata, column_attr_key):
if type(adata.obs[column_attr_key].dtype) == pd.core.dtypes.dtypes.CategoricalDtype and column_attr_key != adata.uns["rank_genes_groups"]["params"]["groupby"]:
return True
if column_attr_key.startswith('n_') or column_attr_key.startswith('num_') or column_attr_key.startswith('pct_') or column_attr_key.startswith('percent_'):
return False
unique_values = np.unique(adata.obs[column_attr_key].values)
if len(unique_values) < 500:
return True
return False


# Populate
for column_attr_key in adata.obs.keys():
# Don't store the clustering as annotation
if type(adata.obs[column_attr_key].dtype) == pd.core.dtypes.dtypes.CategoricalDtype and column_attr_key != adata.uns["rank_genes_groups"]["params"]["groupby"]:
if is_annotation(adata=adata, column_attr_key=column_attr_key):
attrs_metadata["annotations"].append(
{
"name": column_attr_key,
"values": list(set(adata.obs[column_attr_key].values))
"values": np.unique(adata.obs[column_attr_key].values).astype(str).tolist()
}
)
else:
Expand Down

0 comments on commit d599b9a

Please sign in to comment.