Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: changes to add uns metadata for spatial #6658

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions backend/common/utils/cxg_generation_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import pickle

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -31,6 +32,32 @@
metadata_array.meta[key] = value


def convert_uns_to_cxg_group(cxg_container, metadata_dict, group_metadata_name="cxg_group_metadata", ctx=None):

array_name = f"{cxg_container}/{group_metadata_name}"

tiledb.from_numpy(array_name, np.zeros((1,)))

def iterate_over_dict(metadata_dict):
with tiledb.open(array_name, mode="w", ctx=ctx) as metadata_array:
for key, value in metadata_dict.items():
if not key.startswith("spatial"):
continue
print(f"key: {key}, type:{type(value)}, value: {value}")
if isinstance(value, dict):
try:
metadata_array.meta[key] = pickle.dumps(value)
except Exception as e:
logging.error(f"Error adding metadata {key} to {array_name}: {e}")

Check warning on line 51 in backend/common/utils/cxg_generation_utils.py

View check run for this annotation

Codecov / codecov/patch

backend/common/utils/cxg_generation_utils.py#L46-L51

Added lines #L46 - L51 were not covered by tests
else:
try:
metadata_array.meta[key] = value
except Exception as e:
logging.error(f"Error adding metadata {key} to {array_name}: {e}")

Check warning on line 56 in backend/common/utils/cxg_generation_utils.py

View check run for this annotation

Codecov / codecov/patch

backend/common/utils/cxg_generation_utils.py#L53-L56

Added lines #L53 - L56 were not covered by tests

iterate_over_dict(metadata_dict)


def convert_dataframe_to_cxg_array(cxg_container, dataframe_name, dataframe, index_column_name, ctx):
"""
Saves the contents of the dataframe to the CXG output directory specified.
Expand Down
4 changes: 4 additions & 0 deletions backend/layers/processing/h5ad_data_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
convert_dictionary_to_cxg_group,
convert_matrices_to_cxg_arrays,
convert_ndarray_to_cxg_dense_array,
convert_uns_to_cxg_group,
)
from backend.common.utils.matrix_utils import is_matrix_sparse
from backend.common.utils.tiledb import consolidation_buffer_size
Expand Down Expand Up @@ -79,6 +80,9 @@ def to_cxg(self, output_cxg_directory, sparse_threshold, convert_anndata_colors_
convert_dataframe_to_cxg_array(output_cxg_directory, "var", self.var, self.var_index_column_name, ctx)
logging.info("\t...dataset var dataframe saved")

convert_uns_to_cxg_group(output_cxg_directory, self.anndata.uns, "uns", ctx)
logging.info("\t...dataset uns dataframe saved")

self.write_anndata_embeddings_to_cxg(output_cxg_directory, ctx)
logging.info("\t...dataset embeddings saved")

Expand Down
61 changes: 61 additions & 0 deletions make_cxg.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from backend.layers.processing.h5ad_data_file import H5ADDataFile\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def make_cxg(local_filename):\n",
" \"\"\"\n",
" Convert the uploaded H5AD file to the CXG format servicing the cellxgene Explorer.\n",
" \"\"\"\n",
"\n",
" cxg_output_container = local_filename.replace(\".h5ad\", \".cxg\")\n",
" try:\n",
" h5ad_data_file = H5ADDataFile(local_filename, var_index_column_name=\"feature_name\")\n",
" h5ad_data_file.to_cxg(cxg_output_container, sparse_threshold=25.0)\n",
" except Exception as ex:\n",
" # TODO use a specialized exception\n",
" msg = \"CXG conversion failed.\"\n",
"\n",
" raise RuntimeError(msg) from ex\n",
" raise ex\n",
"\n",
" return cxg_output_container\n",
"\n",
"make_cxg(\"UXR_0bb15784-1cea-47e1-9a00-57dcd127746c.h5ad\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading