From 3120a9d97729e9cb346de0aa389fd9732aa232b0 Mon Sep 17 00:00:00 2001 From: Gregor Sturm Date: Fri, 10 Nov 2023 18:33:54 +0100 Subject: [PATCH] Fix #454 (#465) * Fix #454 * Update changelog --- CHANGELOG.md | 1 + src/scirpy/io/_io.py | 6 +++--- src/scirpy/tests/test_io.py | 9 +++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca342e0c5..4cc1900ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning][]. ### Fixes - Fix that `define_clonotype_clusters` could not retreive `within_group` columns from MuData ([#459](https://github.com/scverse/scirpy/pull/459)) +- Fix that AIRR Rearrangment fields of integer types could not be written when their value was None ([#465](https://github.com/scverse/scirpy/pull/465)) ## v0.13.1 diff --git a/src/scirpy/io/_io.py b/src/scirpy/io/_io.py index bc36a44cd..f368bc2c5 100644 --- a/src/scirpy/io/_io.py +++ b/src/scirpy/io/_io.py @@ -584,9 +584,9 @@ def write_airr(adata: DataHandler.TYPE, filename: Union[str, Path], **kwargs) -> for tmp_cell in airr_cells: for chain in tmp_cell.to_airr_records(): # workaround for AIRR library writing out int field as floats (if it happens to be a float) - for f in chain: - if RearrangementSchema.type(f) == "integer": - chain[f] = int(chain[f]) + for field, value in chain.items(): + if RearrangementSchema.type(field) == "integer" and value is not None: + chain[field] = int(value) writer.write(chain) writer.close() diff --git a/src/scirpy/tests/test_io.py b/src/scirpy/tests/test_io.py index 6cfb60495..92919fe3c 100644 --- a/src/scirpy/tests/test_io.py +++ b/src/scirpy/tests/test_io.py @@ -236,6 +236,15 @@ def test_airr_roundtrip_conversion(anndata_from_10x_sample, tmp_path): pdt.assert_frame_equal(anndata.obs, anndata2.obs, check_dtype=False, check_categorical=False) +def test_write_airr_none_field_issue_454(tmp_path): + cell = AirrCell("cell1") + chain = cell.empty_chain_dict() + chain["d_sequence_end"] = None + cell.add_chain(chain) + adata = from_airr_cells([cell]) + write_airr(adata, tmp_path / "test.airr.tsv") + + @pytest.mark.extra @pytest.mark.parametrize( "anndata_from_10x_sample",