Skip to content
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

Adding filter for mtRNA #110

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/grinch/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import numpy as np
from anndata import AnnData
from pydantic import NonNegativeFloat, NonNegativeInt, validate_call
from sklearn.utils.validation import check_non_negative
from sklearn.utils.validation import check_non_negative, column_or_1d

from .aliases import OBS, VAR
from .base import StorageMixin
from .conf import BaseConfigurable
from .processors import ReadKey
from .utils import any_not_None, true_inside
from .utils.decorators import plt_interactive
from .utils.plotting import plot1d
Expand Down Expand Up @@ -132,6 +134,8 @@ class Config(BaseFilter.Config):
max_counts: NonNegativeFloat | None = None
min_cells: NonNegativeInt | None = None
max_cells: NonNegativeInt | None = None
remove_MT_genes: bool = False
gene_names_key: ReadKey = "var_names" # only used if remove_MT_genes

cfg: Config

Expand Down Expand Up @@ -170,6 +174,11 @@ def _filter(self, adata: AnnData) -> None:
self.cfg.max_cells,
)

if self.cfg.remove_MT_genes:
gene_names = StorageMixin.read(adata, self.cfg.gene_names_key)
gene_names = np.char.upper(column_or_1d(gene_names).astype(str))
to_keep &= ~np.char.startswith(gene_names, 'MT-')

if to_keep.sum() < 1:
raise ValueError(
"Filtering options are too stringent. "
Expand Down
21 changes: 21 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,13 @@ def test_gene_all(X):
"min_counts": 6,
"min_cells": 1,
"max_cells": 2,
"remove_MT_genes": True,
}
)
cfg = instantiate(cfg)
filter_genes = cfg.create()
adata = AnnData(X)
adata.var_names = ["G1", "MT2", "MT-3", "G4"]
X_original = adata.X.copy()
filter_genes(adata)
assert_allclose(X_original, X)
Expand All @@ -249,6 +251,25 @@ def test_gene_all(X):
assert_allclose(adata.var[VAR.N_CELLS], [2])


@pytest.mark.parametrize("X", X_mods)
def test_gene_MT(X):
cfg = OmegaConf.create(
{
"_target_": "src.grinch.FilterGenes.Config",
"remove_MT_genes": True,
}
)
cfg = instantiate(cfg)
filter_genes = cfg.create()
adata = AnnData(X)
adata.var_names = ["G1", "MT2", "MT-3", "G4"]
X_original = adata.X.copy()
filter_genes(adata)
assert_allclose(X_original, X)
X_filtered = X[:, [0, 1, 3]]
assert_allclose(X_filtered, adata.X)


@pytest.mark.parametrize("X", X_mods)
def test_gene_inplace(X):
cfg = OmegaConf.create(
Expand Down