From cbd85d69579354808ef3028a4145876e18ef5ffb Mon Sep 17 00:00:00 2001 From: Aljes Date: Thu, 26 Nov 2026 18:12:14 +0000 Subject: [PATCH 1/2] --feature-type takes a 10x input with no types as gene expression (#127) --feature-type and --split-feature-types were refused on a Cell Ranger 2 .h5 and on a folder whose features file has no type column, though both convert without them, so a pipeline had to inspect each 10x input before it chose its flags. Such an input holds gene expression alone (Cell Ranger 2, older STARsolo), so plan_targets takes every feature as 'Gene Expression' when the caller says why the types are missing: --feature-type 'Gene Expression' keeps every feature with a warning, any other type is refused naming gene expression, and --split-feature-types writes one OUT.Gene_Expression output. var gains no feature_types column, so the output is the one the plain conversion gives; the entry map stays the plain transpose. Filtering an h5ad input on its feature_types column, the issue's lower priority, is noted in STATUS.md as a follow-up. Co-Authored-By: Claude Opus 5.5 --- STATUS.md | 7 +++++ docs/reference/formats/10x-h5.md | 8 +++++ docs/reference/formats/10x-mtx.md | 6 +++- src/adata/conversion/features.py | 34 +++++++++++++++++++-- src/adata/conversion/mtx.py | 9 ++---- src/adata/conversion/tenx.py | 10 +----- tests/test_mtx.py | 51 +++++++++++++++++++++++++++++-- tests/test_tenx.py | 43 +++++++++++++++++++++++++- 8 files changed, 146 insertions(+), 22 deletions(-) diff --git a/STATUS.md b/STATUS.md index 9d14d59..96dbe93 100644 --- a/STATUS.md +++ b/STATUS.md @@ -28,6 +28,9 @@ The conversion epic's sub-issues (#21–#30) are all done and ticked in #20. - #124: `import dataframe --categorical` reads numeric-looking labels as text (#125), for 0.17.7. +- #127: `convert --feature-type` and `--split-feature-types` take a 10x + input that records no feature types (Cell Ranger 2, two-column features + files) as gene expression, for 0.17.7. The fixes from the October 2026 audit (epic #87, 164 findings in 16 batches) finished with 0.17.6; the sub-issues (#88–#103) are all done and @@ -36,6 +39,10 @@ theirs. ## Follow-ups noticed +- From #127: `convert` from h5ad refuses `--feature-type` and + `--split-feature-types`; they could filter on var's `feature_types` + column, so one h5ad converted from a multiome `.h5` splits as the `.h5` + would. - From #124 (`--categorical` now reads numeric-looking labels as text): - `import dataframe --keep-dtypes`, to keep the encodings the existing frame has for columns of the same name (category order, `ordered`, diff --git a/docs/reference/formats/10x-h5.md b/docs/reference/formats/10x-h5.md index 4c9db8a..a3d5ddb 100644 --- a/docs/reference/formats/10x-h5.md +++ b/docs/reference/formats/10x-h5.md @@ -70,6 +70,14 @@ entry whose feature index lies outside the features is refused as the matrix is copied, and the output is not kept. `--genome` with `--feature-type` is refused when that genome has no feature of the type. +A Cell Ranger 2 file records no feature types: Cell Ranger 2 measured gene +expression alone, so its features are taken as `Gene Expression`, and the +same flags work on any 10x input. `--feature-type "Gene Expression"` keeps +every feature, with a warning saying so, and any other type is refused; +`--split-feature-types` writes one `OUT.Gene_Expression.h5ad`. var gains no +`feature_types` column: the output is the one the conversion gives without +the flag. + The matrix is stored genes x cells in CSC, which is byte for byte cells x genes in CSR, so `data`, `indices` and `indptr` stream straight across; `--split-feature-types` reads them once for every output. diff --git a/docs/reference/formats/10x-mtx.md b/docs/reference/formats/10x-mtx.md index 8b25342..c6cead7 100644 --- a/docs/reference/formats/10x-mtx.md +++ b/docs/reference/formats/10x-mtx.md @@ -38,7 +38,11 @@ adata convert counts.h5ad -o counts_mtx --to 10x-mtx --layer counts The import flags are those of the [10x HDF5 format](10x-h5.md) (`--genome`, `--var-names`, `--make-unique`, `--feature-type`, `--split-feature-types`), plus `--compression` for the output store, which is gzip on h5ad unless -given. Features are named by gene id unless `--var-names gene_symbols` is +given. A folder whose features file has no type column (Cell Ranger 2's +`genes.tsv`, older STARsolo's two-column `features.tsv`) holds gene +expression alone, so `--feature-type "Gene Expression"` keeps every feature, +with a warning, any other type is refused, and `--split-feature-types` +writes one `OUT.Gene_Expression.h5ad`, as for a Cell Ranger 2 `.h5`. Features are named by gene id unless `--var-names gene_symbols` is given; with it, reading gives the var names and columns `scanpy.read_10x_mtx(gex_only=False)` gives, checked against scanpy in CI, except that integer counts stay integers (int32, int64 when a value needs diff --git a/src/adata/conversion/features.py b/src/adata/conversion/features.py index 98b0e60..777f963 100644 --- a/src/adata/conversion/features.py +++ b/src/adata/conversion/features.py @@ -50,6 +50,10 @@ VAR_NAMES = ("gene_symbols", "gene_ids") +#: The type of every feature of an input that records none: Cell Ranger 2 +#: and STARsolo's two-column files measured gene expression alone. +GENE_EXPRESSION = "Gene Expression" + #: Rows of a string column handled per read when streaming one. STRING_STEP = 1 << 18 @@ -137,13 +141,40 @@ def plan_targets( options: Options, *, noun: str, + untyped: Optional[str] = None, ) -> Tuple[List[Path], List[Target], List[str]]: """The outputs to write, the features each keeps, and any warnings. One output, unless `--split-feature-types` asks for one per type. `keep` is what survives filters the caller applied first, such as `--genome`; `noun` names the input in messages ("file", "folder"). + + `types` is None for an input that records no feature types; `untyped` + then says why ("genes.tsv records no feature types"). Such an input + holds gene expression alone, so the type flags work on it as on a v3 + input of that one type, and a pipeline can pass the same flags for any + 10x input (#127). Without `untyped` the flags are refused. """ + warnings: List[str] = [] + if types is None and (options.feature_type is not None or options.split_feature_types): + if untyped is None: + raise ValueError( + f"This {noun} records no feature types; --feature-type and " + "--split-feature-types need them." + ) + if options.feature_type is not None and options.feature_type != GENE_EXPRESSION: + raise ValueError( + f"No features of type {options.feature_type!r}; {untyped}, so " + f"this {noun} holds gene expression only." + ) + # Used only to select: var gains no feature_types column, so the + # output is the one the conversion gives without the flag. + types = np.full(len(names), GENE_EXPRESSION, dtype=object) + warnings.append( + f"{untyped}; every feature is taken as " + f"{GENE_EXPRESSION!r}, the only type such a {noun} holds." + ) + groups: List[Tuple[Optional[str], np.ndarray]] = [] if options.feature_type is not None: available = [t for t, _ in type_counts(types)] @@ -176,7 +207,6 @@ def plan_targets( stems = unique_names([label for label, _ in groups if label is not None]) outputs: List[Path] = [] targets: List[Target] = [] - warnings: List[str] = [] for label, mask in groups: path = output if options.split_feature_types: @@ -403,7 +433,7 @@ def requested(flag: str, name: Optional[str]) -> Optional[str]: types = column(types_col) else: missing.append("feature types (a feature_types column, or --feature-types-column)") - types = ["Gene Expression"] * n + types = [GENE_EXPRESSION] * n if missing: if not options.force: diff --git a/src/adata/conversion/mtx.py b/src/adata/conversion/mtx.py index a3dd12a..a180f5a 100644 --- a/src/adata/conversion/mtx.py +++ b/src/adata/conversion/mtx.py @@ -351,15 +351,10 @@ def plan( if width >= 3 and not folder.legacy: types = np.array([row[2] for row in rows], dtype=object) columns["feature_types"] = types - if types is None and (options.feature_type is not None or options.split_feature_types): - raise ValueError( - f"{folder.features.name} records no feature types; " - "--feature-type and --split-feature-types need a third column." - ) - keep = np.ones(n_features, dtype=bool) outputs, targets, warnings = plan_targets( - output, names, columns, types, keep, options, noun="folder" + output, names, columns, types, keep, options, noun="folder", + untyped=f"{folder.features.name} records no feature types", ) if width > 3: warnings.append( diff --git a/src/adata/conversion/tenx.py b/src/adata/conversion/tenx.py index 40a9041..0fe2ca8 100644 --- a/src/adata/conversion/tenx.py +++ b/src/adata/conversion/tenx.py @@ -375,14 +375,6 @@ def plan_matrix_import( check_import_options(options) matrix = _read_matrix(handle, options.genome) - if matrix.version == 2 and ( - options.feature_type is not None or options.split_feature_types - ): - raise ValueError( - "This is a Cell Ranger 2 file, which records no feature types; " - "--feature-type and --split-feature-types need a v3 file." - ) - keep = np.ones(matrix.n_features, dtype=bool) if options.genome is not None and matrix.version == 3: genomes = matrix.columns.get("genome") @@ -397,7 +389,7 @@ def plan_matrix_import( outputs, targets, warnings = plan_targets( output, matrix.names, matrix.columns, matrix.feature_types, keep, options, - noun="file", + noun="file", untyped="Cell Ranger 2 files record no feature types", ) return matrix, outputs, targets, warnings diff --git a/tests/test_mtx.py b/tests/test_mtx.py index efc83d5..7c7027e 100644 --- a/tests/test_mtx.py +++ b/tests/test_mtx.py @@ -364,8 +364,16 @@ def test_a_broken_folder_writes_nothing(tmp_path, counts, spoil, message): @pytest.mark.parametrize( "layout,args,message", [ - ("starsolo2col", ["--feature-type", "Gene Expression"], "records no feature types"), - ("cr2", ["--split-feature-types"], "records no feature types"), + ( + "starsolo2col", ["--feature-type", "Peaks"], + "No features of type 'Peaks'; features.tsv records no feature types, " + "so this folder holds gene expression only.", + ), + ( + "cr2", ["--feature-type", "Antibody Capture"], + "No features of type 'Antibody Capture'; genes.tsv records no feature " + "types, so this folder holds gene expression only.", + ), ("cr3", ["--genome", "hg19"], "no genome per feature"), ("cr3", ["--var-names", "symbols"], "--var-names must be one of"), ("cr3", ["--compression", "zstd"], "not available for h5ad"), @@ -383,6 +391,45 @@ def test_options_that_do_not_fit_are_refused(tmp_path, counts, layout, args, mes assert not (tmp_path / "out.h5ad").exists() +@pytest.mark.parametrize("layout, features", [("cr2", "genes.tsv"), ("starsolo2col", "features.tsv")]) +@pytest.mark.parametrize( + "flags, written", + [ + (["--feature-type", "Gene Expression"], "out.h5ad"), + (["--split-feature-types"], "out.Gene_Expression.h5ad"), + ], + ids=["feature-type", "split"], +) +def test_the_type_flags_take_an_untyped_folder_as_gene_expression( + tmp_path, counts, layout, features, flags, written +): + """#127: --feature-type and --split-feature-types were refused on a + folder whose features file has no type column, so a pipeline had to + look inside each 10x input before it chose its flags. Such a folder + (Cell Ranger 2, older STARsolo) holds gene expression alone.""" + folder = write_10x_mtx( + tmp_path / "in", counts, barcodes=_barcodes(), ids=IDS, names=NAMES, layout=layout + ) + plain = tmp_path / "plain.h5ad" + _ok(folder, "-o", plain) + + result = _ok(folder, "-o", tmp_path / "out.h5ad", *flags) + + assert sorted(p.name for p in tmp_path.glob("out*.h5ad")) == [written] + got, expected = _read(tmp_path / written), _read(plain) + assert list(got.obs_names) == list(expected.obs_names) + assert list(got.var_names) == list(expected.var_names) + assert list(got.var.columns) == list(expected.var.columns) + assert "feature_types" not in got.var.columns + assert got.X.dtype == expected.X.dtype + assert np.array_equal(got.X.toarray(), expected.X.toarray()) + assert ( + f"Warning: {features} records no feature types; every feature is taken " + "as 'Gene Expression', the only type such a folder holds." + ) in _out(result) + assert _debris(tmp_path) == [] + + def test_a_single_mtx_file_points_to_its_folder(tmp_path, counts): path = write_mtx_file(tmp_path / "matrix.mtx", counts) result = _convert(path, "-o", tmp_path / "out.h5ad") diff --git a/tests/test_tenx.py b/tests/test_tenx.py index f532f6e..a4815ec 100644 --- a/tests/test_tenx.py +++ b/tests/test_tenx.py @@ -258,13 +258,54 @@ def test_a_cell_ranger_2_file_with_one_genome_needs_no_flag(tmp_path): "extra,message", [ (["--genome", "dm6"], "No genome 'dm6' in this file; it has: hg19, mm10"), - (["--genome", "hg19", "--feature-type", "Gene Expression"], "records no feature types"), + ( + ["--genome", "hg19", "--feature-type", "Peaks"], + "No features of type 'Peaks'; Cell Ranger 2 files record no feature " + "types, so this file holds gene expression only.", + ), ], ) def test_bad_cell_ranger_2_options_are_refused(tmp_path, extra, message): result = _convert(_v2(tmp_path), "-o", tmp_path / "o.h5ad", *extra) assert result.exit_code == 1 assert message in _out(result) + assert not (tmp_path / "o.h5ad").exists() + + +def _same_conversion(got, expected) -> None: + assert list(got.obs_names) == list(expected.obs_names) + assert list(got.var_names) == list(expected.var_names) + assert list(got.var.columns) == list(expected.var.columns) + for name in expected.var.columns: + assert list(got.var[name]) == list(expected.var[name]), name + assert got.X.dtype == expected.X.dtype + assert np.array_equal(got.X.toarray(), expected.X.toarray()) + + +@pytest.mark.parametrize( + "flags, written", + [ + (["--feature-type", "Gene Expression"], "o.h5ad"), + (["--split-feature-types"], "o.Gene_Expression.h5ad"), + ], + ids=["feature-type", "split"], +) +def test_the_type_flags_take_a_cell_ranger_2_file_as_gene_expression(tmp_path, flags, written): + """#127: --feature-type and --split-feature-types were refused on a Cell + Ranger 2 file, so a pipeline had to know each input's 10x version before + it chose its flags. Cell Ranger 2 measured gene expression alone.""" + h5 = _v2(tmp_path) + plain = tmp_path / "plain.h5ad" + _ok(h5, "-o", plain, "--genome", "hg19") + + result = _ok(h5, "-o", tmp_path / "o.h5ad", "--genome", "hg19", *flags) + + assert sorted(p.name for p in tmp_path.glob("o*.h5ad")) == [written] + _same_conversion(_read(tmp_path / written), _read(plain)) + assert ( + "Warning: Cell Ranger 2 files record no feature types; every feature is " + "taken as 'Gene Expression', the only type such a file holds." + ) in _out(result) def test_an_inconsistent_matrix_is_refused_before_writing(tenx, tmp_path): From fee6d45702c752885781dbfce5571237dba19990 Mon Sep 17 00:00:00 2001 From: Aljes Date: Thu, 26 Nov 2026 19:33:24 +0000 Subject: [PATCH 2/2] Review: a three-column genes.tsv refuses the type flags; wording, tests - A genes.tsv with a third column was called untyped, so --feature-type 'Gene Expression' kept every feature of a Cell Ranger 3 features.tsv renamed genes.tsv (as GEO often serves them), antibodies included, and the warning said it had no types. Its third column is not read, as in scanpy, so the flags are refused there, saying to name the file features.tsv to filter on the types. - The h5 message says "Cell Ranger 2's layout records no feature types", true of CellBender 0.2 run on Cell Ranger 2 input as well. - Tests: values of var compared in the mtx test, a GEO-prefixed folder, CellBender on Cell Ranger 2 input, and a refusal checked to write no output of any name. - Docs: .zarr outputs, the three-column case, 10x-mtx.md reflowed. Co-Authored-By: Claude Opus 5.5 --- docs/reference/formats/10x-h5.md | 11 ++++---- docs/reference/formats/10x-mtx.md | 14 +++++----- src/adata/conversion/mtx.py | 12 +++++++++ src/adata/conversion/tenx.py | 2 +- tests/test_cellbender.py | 21 +++++++++++++++ tests/test_mtx.py | 45 ++++++++++++++++++++++++++++--- tests/test_tenx.py | 10 +++---- 7 files changed, 94 insertions(+), 21 deletions(-) diff --git a/docs/reference/formats/10x-h5.md b/docs/reference/formats/10x-h5.md index a3d5ddb..38c740f 100644 --- a/docs/reference/formats/10x-h5.md +++ b/docs/reference/formats/10x-h5.md @@ -70,11 +70,12 @@ entry whose feature index lies outside the features is refused as the matrix is copied, and the output is not kept. `--genome` with `--feature-type` is refused when that genome has no feature of the type. -A Cell Ranger 2 file records no feature types: Cell Ranger 2 measured gene -expression alone, so its features are taken as `Gene Expression`, and the -same flags work on any 10x input. `--feature-type "Gene Expression"` keeps -every feature, with a warning saying so, and any other type is refused; -`--split-feature-types` writes one `OUT.Gene_Expression.h5ad`. var gains no +A file in Cell Ranger 2's layout (CellBender run on one included) records +no feature types: Cell Ranger 2 measured gene expression alone, so its +features are taken as `Gene Expression`, and the same flags work on any 10x +input. `--feature-type "Gene Expression"` keeps every feature, with a +warning saying so, and any other type is refused; `--split-feature-types` +writes one `OUT.Gene_Expression.h5ad` (or `.zarr`). var gains no `feature_types` column: the output is the one the conversion gives without the flag. diff --git a/docs/reference/formats/10x-mtx.md b/docs/reference/formats/10x-mtx.md index c6cead7..157f014 100644 --- a/docs/reference/formats/10x-mtx.md +++ b/docs/reference/formats/10x-mtx.md @@ -42,12 +42,14 @@ given. A folder whose features file has no type column (Cell Ranger 2's `genes.tsv`, older STARsolo's two-column `features.tsv`) holds gene expression alone, so `--feature-type "Gene Expression"` keeps every feature, with a warning, any other type is refused, and `--split-feature-types` -writes one `OUT.Gene_Expression.h5ad`, as for a Cell Ranger 2 `.h5`. Features are named by gene id unless `--var-names gene_symbols` is -given; with it, reading gives the var names and columns -`scanpy.read_10x_mtx(gex_only=False)` gives, checked against scanpy in CI, -except that integer counts stay integers (int32, int64 when a value needs -it) where scanpy converts them to float32; a `real` matrix is read as -float64. Duplicate entries are summed, as scipy does. +writes one `OUT.Gene_Expression.h5ad` (or `.zarr`), as for a Cell Ranger 2 +`.h5`. A `genes.tsv` with a third column refuses them: Cell Ranger 2's +records no types, and scanpy ignores the column. Features are named by gene +id unless `--var-names gene_symbols` is given; with it, reading gives the +var names and columns `scanpy.read_10x_mtx(gex_only=False)` gives, checked +against scanpy in CI, except that integer counts stay integers (int32, int64 +when a value needs it) where scanpy converts them to float32; a `real` +matrix is read as float64. Duplicate entries are summed, as scipy does. The matrix is read in two streaming passes, so its size is not a limit: the first checks the whole folder -- barcode and feature counts against the diff --git a/src/adata/conversion/mtx.py b/src/adata/conversion/mtx.py index a180f5a..456b16d 100644 --- a/src/adata/conversion/mtx.py +++ b/src/adata/conversion/mtx.py @@ -351,6 +351,18 @@ def plan( if width >= 3 and not folder.legacy: types = np.array([row[2] for row in rows], dtype=object) columns["feature_types"] = types + if types is None and width >= 3 and ( + options.feature_type is not None or options.split_feature_types + ): + # Not untyped, though not read: GEO often serves a Cell Ranger 3 + # features.tsv renamed genes.tsv for old Seurat, antibodies and + # all, and taking it as gene expression would keep them. + raise ValueError( + f"{folder.features.name} has a third column, which is not read: " + "Cell Ranger 2's genes.tsv records no feature types, and scanpy " + "ignores it. If it holds Cell Ranger 3's feature types, name the " + "file features.tsv to filter on them." + ) keep = np.ones(n_features, dtype=bool) outputs, targets, warnings = plan_targets( output, names, columns, types, keep, options, noun="folder", diff --git a/src/adata/conversion/tenx.py b/src/adata/conversion/tenx.py index 0fe2ca8..02ec6af 100644 --- a/src/adata/conversion/tenx.py +++ b/src/adata/conversion/tenx.py @@ -389,7 +389,7 @@ def plan_matrix_import( outputs, targets, warnings = plan_targets( output, matrix.names, matrix.columns, matrix.feature_types, keep, options, - noun="file", untyped="Cell Ranger 2 files record no feature types", + noun="file", untyped="Cell Ranger 2's layout records no feature types", ) return matrix, outputs, targets, warnings diff --git a/tests/test_cellbender.py b/tests/test_cellbender.py index c673ceb..c2dddcb 100644 --- a/tests/test_cellbender.py +++ b/tests/test_cellbender.py @@ -192,6 +192,27 @@ def test_cellbender_0_2_on_cell_ranger_2_input(tmp_path): assert "genome" not in got.uns["cellranger"], "background_removed is not a genome" +def test_cellbender_on_cell_ranger_2_input_takes_the_type_flags_as_gene_expression(tmp_path): + """#127: the type flags work on any input in Cell Ranger 2's layout, + which records no feature types; CellBender 0.2 keeps that layout.""" + truth = _cellbender(tmp_path, version="0.2", cellranger_v2=True) + plain, typed = tmp_path / "plain.h5ad", tmp_path / "typed.h5ad" + _ok(truth.path, "-o", plain) + + result = _ok(truth.path, "-o", typed, "--feature-type", "Gene Expression") + + got, expected = _read(typed), _read(plain) + assert list(got.var_names) == list(expected.var_names) + assert np.array_equal(got.X.toarray(), expected.X.toarray()) + assert list(got.obs.columns) == list(expected.obs.columns) + assert "Cell Ranger 2's layout records no feature types" in _out(result) + + refused = _convert(truth.path, "-o", tmp_path / "peaks.h5ad", "--feature-type", "Peaks") + assert refused.exit_code == 1 + assert "so this file holds gene expression only" in _out(refused) + assert not (tmp_path / "peaks.h5ad").exists() + + # --------------------------------------------------------------------------- # sniffing diff --git a/tests/test_mtx.py b/tests/test_mtx.py index 7c7027e..7ec7536 100644 --- a/tests/test_mtx.py +++ b/tests/test_mtx.py @@ -388,10 +388,18 @@ def test_options_that_do_not_fit_are_refused(tmp_path, counts, layout, args, mes result = _convert(folder, "-o", tmp_path / "out.h5ad", *args) assert result.exit_code == 1 assert message in _out(result) - assert not (tmp_path / "out.h5ad").exists() + assert list(tmp_path.glob("out*")) == [] -@pytest.mark.parametrize("layout, features", [("cr2", "genes.tsv"), ("starsolo2col", "features.tsv")]) +@pytest.mark.parametrize( + "layout, prefix, features", + [ + ("cr2", "", "genes.tsv"), + ("starsolo2col", "", "features.tsv"), + ("cr2", "GSM1_", "GSM1_genes.tsv"), # as GEO serves them + ], + ids=["cr2", "starsolo2col", "geo-prefix"], +) @pytest.mark.parametrize( "flags, written", [ @@ -401,14 +409,15 @@ def test_options_that_do_not_fit_are_refused(tmp_path, counts, layout, args, mes ids=["feature-type", "split"], ) def test_the_type_flags_take_an_untyped_folder_as_gene_expression( - tmp_path, counts, layout, features, flags, written + tmp_path, counts, layout, prefix, features, flags, written ): """#127: --feature-type and --split-feature-types were refused on a folder whose features file has no type column, so a pipeline had to look inside each 10x input before it chose its flags. Such a folder (Cell Ranger 2, older STARsolo) holds gene expression alone.""" folder = write_10x_mtx( - tmp_path / "in", counts, barcodes=_barcodes(), ids=IDS, names=NAMES, layout=layout + tmp_path / "in", counts, barcodes=_barcodes(), ids=IDS, names=NAMES, + layout=layout, prefix=prefix, ) plain = tmp_path / "plain.h5ad" _ok(folder, "-o", plain) @@ -420,6 +429,8 @@ def test_the_type_flags_take_an_untyped_folder_as_gene_expression( assert list(got.obs_names) == list(expected.obs_names) assert list(got.var_names) == list(expected.var_names) assert list(got.var.columns) == list(expected.var.columns) + for name in expected.var.columns: + assert list(got.var[name]) == list(expected.var[name]), name assert "feature_types" not in got.var.columns assert got.X.dtype == expected.X.dtype assert np.array_equal(got.X.toarray(), expected.X.toarray()) @@ -430,6 +441,32 @@ def test_the_type_flags_take_an_untyped_folder_as_gene_expression( assert _debris(tmp_path) == [] +@pytest.mark.parametrize("flags", [["--feature-type", "Gene Expression"], ["--split-feature-types"]]) +def test_a_genes_tsv_with_a_third_column_refuses_the_type_flags(tmp_path, counts, flags): + """GEO often serves a Cell Ranger 3 features.tsv renamed genes.tsv, as + old Seurat wanted, antibodies and all. Its third column is not read (as + in scanpy), so taking the folder as gene expression would keep the + antibodies; the flags are refused, saying what to do.""" + folder = tmp_path / "in" + write_10x_mtx( + folder, counts, barcodes=_barcodes(), ids=IDS, names=NAMES, types=TYPES, + layout="starsolo", + ) + (folder / "features.tsv").rename(folder / "genes.tsv") + + result = _convert(folder, "-o", tmp_path / "out.h5ad", *flags) + + assert result.exit_code == 1 + assert ( + "genes.tsv has a third column, which is not read: Cell Ranger 2's " + "genes.tsv records no feature types, and scanpy ignores it. If it holds " + "Cell Ranger 3's feature types, name the file features.tsv to filter on them." + ) in _out(result) + assert list(tmp_path.glob("out*")) == [] + # Without the flags it converts as before, the third column unread. + _ok(folder, "-o", tmp_path / "plain.h5ad") + + def test_a_single_mtx_file_points_to_its_folder(tmp_path, counts): path = write_mtx_file(tmp_path / "matrix.mtx", counts) result = _convert(path, "-o", tmp_path / "out.h5ad") diff --git a/tests/test_tenx.py b/tests/test_tenx.py index a4815ec..63100bc 100644 --- a/tests/test_tenx.py +++ b/tests/test_tenx.py @@ -260,8 +260,8 @@ def test_a_cell_ranger_2_file_with_one_genome_needs_no_flag(tmp_path): (["--genome", "dm6"], "No genome 'dm6' in this file; it has: hg19, mm10"), ( ["--genome", "hg19", "--feature-type", "Peaks"], - "No features of type 'Peaks'; Cell Ranger 2 files record no feature " - "types, so this file holds gene expression only.", + "No features of type 'Peaks'; Cell Ranger 2's layout records no " + "feature types, so this file holds gene expression only.", ), ], ) @@ -269,7 +269,7 @@ def test_bad_cell_ranger_2_options_are_refused(tmp_path, extra, message): result = _convert(_v2(tmp_path), "-o", tmp_path / "o.h5ad", *extra) assert result.exit_code == 1 assert message in _out(result) - assert not (tmp_path / "o.h5ad").exists() + assert list(tmp_path.glob("o*")) == [] def _same_conversion(got, expected) -> None: @@ -303,8 +303,8 @@ def test_the_type_flags_take_a_cell_ranger_2_file_as_gene_expression(tmp_path, f assert sorted(p.name for p in tmp_path.glob("o*.h5ad")) == [written] _same_conversion(_read(tmp_path / written), _read(plain)) assert ( - "Warning: Cell Ranger 2 files record no feature types; every feature is " - "taken as 'Gene Expression', the only type such a file holds." + "Warning: Cell Ranger 2's layout records no feature types; every feature " + "is taken as 'Gene Expression', the only type such a file holds." ) in _out(result)