Skip to content

Commit

Permalink
Fix #268
Browse files Browse the repository at this point in the history
Allow "None" values to be present as cell-level attributes
during `merge_airr_chains`.
  • Loading branch information
grst committed Jun 30, 2021
1 parent 0f0b282 commit a2ee516
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion scirpy/io/_datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def __setitem__(self, k, v) -> None:
v = _is_true2(v)
try:
existing_value = self._cell_attrs[k]
if existing_value != v:
if existing_value != v and not _is_na2(existing_value):
raise ValueError(
"Cell-level attributes differ between different chains. "
f"Already present: `{existing_value}`. Tried to add `{v}`."
Expand Down
27 changes: 19 additions & 8 deletions scirpy/tests/test_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ def test_merge_airr_chains_concat():
)


def test_merge_airr_chains_no_ir():
@pytest.mark.parametrize("is_cell,result", [("None", None), ("foo", ValueError)])
def test_merge_airr_chains_no_ir(is_cell, result: BaseException):
"""Test that merging an IR anndata with a non-IR anndata
also works with `merge_airr_chains`."""
also works with `merge_airr_chains`.
When a cell-level attribute (`is_cell`) is present, and an inconsistent value
gets merged, the function should fail with a ValueError. However, if `is_cell` is
"None", merging should still be possible.
"""
cell_ids_anndata = np.array(
[
"AAACCTGAGATAGCAT-1",
Expand All @@ -65,16 +71,21 @@ def test_merge_airr_chains_no_ir():
adata = AnnData(X=np.ones((6, 2)))
adata.obs_names = cell_ids_anndata
adata.obs["foo"] = "bar"
adata.obs["is_cell"] = is_cell
adata_ir = read_10x_vdj(TESTDATA / "10x/filtered_contig_annotations.csv")
adata_ir.obs["foo_ir"] = "bar_ir"

merge_airr_chains(adata, adata_ir)
if result is not None:
with pytest.raises(result):
merge_airr_chains(adata, adata_ir)
else:
merge_airr_chains(adata, adata_ir)

npt.assert_array_equal(adata.obs.index, cell_ids_anndata)
assert np.all(np.isin(adata_ir.obs.columns, adata.obs.columns))
assert "foo" in adata.obs.columns
assert "foo_ir" in adata.obs.columns
assert list(adata.obs_names) == list(cell_ids_anndata)
npt.assert_array_equal(adata.obs.index, cell_ids_anndata)
assert np.all(np.isin(adata_ir.obs.columns, adata.obs.columns))
assert "foo" in adata.obs.columns
assert "foo_ir" in adata.obs.columns
assert list(adata.obs_names) == list(cell_ids_anndata)


def test_merge_adata():
Expand Down

0 comments on commit a2ee516

Please sign in to comment.