Skip to content

Commit

Permalink
fix: handle both string and category encoded "assay_ontology_term_id" (
Browse files Browse the repository at this point in the history
…#1142)

Co-authored-by: Evan Molinelli <[email protected]>
  • Loading branch information
ejmolinelli and Evan Molinelli authored Dec 3, 2024
1 parent 0a166c4 commit 5293502
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
18 changes: 10 additions & 8 deletions cellxgene_schema_cli/cellxgene_schema/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1648,8 +1648,10 @@ def _validate_spatial_cell_type_ontology_term_id(self):
return

# Validate all out of tissue (in_tissue==0) spatial spots have unknown cell ontology term
is_spatial = self.adata.obs["assay_ontology_term_id"].apply(
lambda assay: is_ontological_descendant_of(ONTOLOGY_PARSER, assay, ASSAY_VISIUM, True)
is_spatial = (
self.adata.obs["assay_ontology_term_id"]
.apply(lambda assay: is_ontological_descendant_of(ONTOLOGY_PARSER, assay, ASSAY_VISIUM, True))
.astype(bool)
)
is_not_tissue = self.adata.obs["in_tissue"] == 0
is_not_unknown = self.adata.obs["cell_type_ontology_term_id"] != "unknown"
Expand All @@ -1676,9 +1678,9 @@ def _validate_spatial_tissue_position(self, tissue_position_name: str, min: int,
not (self.is_visium_and_is_single_true)
or (
~(
self.adata.obs["assay_ontology_term_id"].apply(
lambda t: is_ontological_descendant_of(ONTOLOGY_PARSER, t, ASSAY_VISIUM, True)
)
self.adata.obs["assay_ontology_term_id"]
.apply(lambda t: is_ontological_descendant_of(ONTOLOGY_PARSER, t, ASSAY_VISIUM, True))
.astype(bool)
)
& (self.adata.obs[tissue_position_name].notnull())
).any()
Expand All @@ -1697,9 +1699,9 @@ def _validate_spatial_tissue_position(self, tissue_position_name: str, min: int,
tissue_position_name not in self.adata.obs
or (
(
self.adata.obs["assay_ontology_term_id"].apply(
lambda t: is_ontological_descendant_of(ONTOLOGY_PARSER, t, ASSAY_VISIUM, True)
)
self.adata.obs["assay_ontology_term_id"]
.apply(lambda t: is_ontological_descendant_of(ONTOLOGY_PARSER, t, ASSAY_VISIUM, True))
.astype(bool)
)
& (self.adata.obs[tissue_position_name].isnull())
).any()
Expand Down
17 changes: 17 additions & 0 deletions cellxgene_schema_cli/tests/test_schema_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ def test_column_presence_in_tissue(self, validator_with_visium_assay, assay_onto
# reset and test
validator.reset()
validator.adata.obs["assay_ontology_term_id"] = assay_ontology_term_id
validator.adata.obs["assay_ontology_term_id"] = validator.adata.obs["assay_ontology_term_id"].astype("category")
validator._validate_spatial_tissue_position("in_tissue", 0, 1)
if is_descendant:
assert validator.errors == []
Expand Down Expand Up @@ -632,6 +633,22 @@ def test_assay_ontology_term_id(self, validator_with_adata, assay_ontology_term_
]
assert validator.errors == [self.get_format_error_message(error_message_suffix, error)]

def test_assay_ontology_term_id__as_categorical(self, validator_with_visium_assay):
"""
Formally, assay_ontology_term_id is expected to be a categorical variable of type string. However, it should work for categorical dtypes as well.
"""
validator: Validator = validator_with_visium_assay

# check encoding as string
validator._check_spatial_obs()
assert validator.errors == []
validator.reset()

# force encoding as 'categorical'
validator.adata.obs["assay_ontology_term_id"] = validator.adata.obs["assay_ontology_term_id"].astype("category")
validator._check_spatial_obs()
assert validator.errors == []

def test_cell_type_ontology_term_id_invalid_term(self, validator_with_adata):
validator = validator_with_adata
validator.adata.obs.loc[validator.adata.obs.index[0], "cell_type_ontology_term_id"] = "EFO:0000001"
Expand Down

0 comments on commit 5293502

Please sign in to comment.