-
Notifications
You must be signed in to change notification settings - Fork 767
fix: keep FlyBase gene nan when reading 10x mtx #4329
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
Draft
joncarse
wants to merge
2
commits into
scverse:main
Choose a base branch
from
joncarse:fix/read-10x-na-like-gene-symbols
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+87
−0
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {func}`~scanpy.io.read_10x_mtx` no longer treats the gene symbol `nan` as missing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
|
|
||
| import scanpy as sc | ||
| from scanpy._compat import CSRBase | ||
| from scanpy.io._read import _PANDAS_STR_NA_VALUES | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pathlib import Path | ||
|
|
@@ -111,6 +112,60 @@ def test_read_10x_mtx_int( | |
| assert dict(adata.var.dtypes) == dict(feature_types=str_dt, **col_dtypes) | ||
|
|
||
|
|
||
| def _mtx_dir_with_symbol(tmp_path: Path, data_10x: Path, symbol: str) -> Path: | ||
| dest = tmp_path / "mtx" | ||
| shutil.copytree(data_10x / "int-ids", dest) | ||
| lines = (dest / "features.tsv").read_text().splitlines() | ||
| cols = lines[0].split("\t") | ||
| cols[0] = "FBgn0036414" | ||
| cols[1] = symbol | ||
| lines[0] = "\t".join(cols) | ||
| (dest / "features.tsv").write_text("\n".join(lines) + "\n") | ||
| return dest | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("var_names", ["gene_symbols", "gene_ids"]) | ||
| def test_read_10x_mtx_gene_symbol_nan( | ||
| tmp_path: Path, data_10x: Path, var_names: Literal["gene_symbols", "gene_ids"] | ||
| ) -> None: | ||
| mtx_path = _mtx_dir_with_symbol(tmp_path, data_10x, "nan") | ||
| adata = sc.io.read_10x_mtx(mtx_path, var_names=var_names, compressed=False) | ||
|
|
||
| if var_names == "gene_symbols": | ||
| assert adata.var_names[0] == "nan" | ||
| assert not pd.isna(adata.var_names[0]) | ||
| adata.var["mt"] = adata.var_names.str.startswith("mt:") | ||
| assert not adata.var["mt"].isna().any() | ||
| sc.pp.calculate_qc_metrics( | ||
| adata, qc_vars=["mt"], percent_top=None, log1p=False, inplace=True | ||
| ) | ||
| else: | ||
| assert adata.var["gene_symbols"].iloc[0] == "nan" | ||
| assert not adata.var["gene_symbols"].isna().iloc[0] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("symbol", ["NaN", "NA"]) | ||
| def test_read_10x_mtx_other_na_gene_symbols( | ||
| tmp_path: Path, data_10x: Path, symbol: str | ||
| ) -> None: | ||
| mtx_path = _mtx_dir_with_symbol(tmp_path, data_10x, symbol) | ||
| adata = sc.io.read_10x_mtx(mtx_path, var_names="gene_ids", compressed=False) | ||
| assert adata.var["gene_symbols"].isna().iloc[0] | ||
|
|
||
|
|
||
| def test_pandas_str_na_values_unchanged() -> None: | ||
| """Fail if pandas changes its default NA tokens. | ||
|
|
||
| Production copies ``STR_NA_VALUES`` as ``_PANDAS_STR_NA_VALUES`` and derives | ||
| ``_10X_FEATURE_NA_VALUES`` by dropping ``nan``. Importing pandas._libs only | ||
| here avoids a private import in ``read_10x_mtx``. On failure, update | ||
| ``_PANDAS_STR_NA_VALUES`` in ``scanpy.io._read``. | ||
| """ | ||
| from pandas._libs.parsers import STR_NA_VALUES | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to protect against drift in future pandas versions but didn't want production code to import a private ("_") pandas member. Thought putting it as a test is a good tradeoff |
||
|
|
||
| assert frozenset(STR_NA_VALUES) == _PANDAS_STR_NA_VALUES | ||
|
|
||
|
|
||
| def test_read_10x_h5_v1(data_10x: Path) -> None: | ||
| spec_genome_v1 = sc.io.read_10x_h5( | ||
| data_10x / "1.2.0" / "filtered_gene_bc_matrices_h5.h5", | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 137-141 specifically test regressions for cases such as #1259 , but might look out-of-context when reading these lines in the future. I can remove or extract to another unit test that specifically mentions #1259