Skip to content

Commit

Permalink
Fix pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
grst committed Jun 13, 2024
1 parent 34a9ac3 commit acbda4f
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 48 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ filterwarnings = [
[tool.ruff]
src = ["src"]
line-length = 120
extent-include = ["*.ipynb"]
extend-include = ["*.ipynb"]

[tool.ruff.format]
docstring-code-format = true
Expand Down
10 changes: 5 additions & 5 deletions src/infercnvpy/io/_genepos.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Literal, Union
from typing import Literal

import gtfparse
import numpy as np
Expand All @@ -9,13 +9,13 @@


def genomic_position_from_gtf(
gtf_file: Union[Path, str],
adata: Union[AnnData, None] = None,
gtf_file: Path | str,
adata: AnnData | None = None,
*,
gtf_gene_id: Literal["gene_id", "gene_name"] = "gene_name",
adata_gene_id: Union[str, None] = None,
adata_gene_id: str | None = None,
inplace: bool = True,
) -> Union[pd.DataFrame, None]:
) -> pd.DataFrame | None:
"""Get genomic gene positions from a GTF file.
The GTF file needs to match the genome annotation used for your single cell dataset.
Expand Down
7 changes: 3 additions & 4 deletions src/infercnvpy/io/_scevan.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Read in result files from scevan."""

from pathlib import Path
from typing import Optional, Union

import numpy as np
import pandas as pd
Expand All @@ -26,14 +25,14 @@ def _get_chr_pos_from_array(chr_pos_array):

def read_scevan(
adata: AnnData,
scevan_res_dir: Union[str, Path],
scevan_res_table: Optional[Union[str, Path]] = None,
scevan_res_dir: str | Path,
scevan_res_table: str | Path | None = None,
*,
subclones: bool = True,
inplace: bool = True,
subset: bool = True,
key_added: str = "scevan",
) -> Optional[AnnData]:
) -> AnnData | None:
"""Load results from SCEVAN :cite:`DeFalco2021` for downstream analysis with infercnvpy.
Requires that the cell barcodes used for SCEVAN and `adata.obs_names` match,
Expand Down
22 changes: 10 additions & 12 deletions src/infercnvpy/pl/_chromosome_heatmap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional, Union

import matplotlib.axes
import numpy as np
import pandas as pd
Expand All @@ -15,12 +13,12 @@ def chromosome_heatmap(
*,
groupby: str = "cnv_leiden",
use_rep: str = "cnv",
cmap: Union[str, Colormap] = "bwr",
cmap: str | Colormap = "bwr",
figsize: tuple[int, int] = (16, 10),
show: Optional[bool] = None,
save: Union[str, bool, None] = None,
show: bool | None = None,
save: str | bool | None = None,
**kwargs,
) -> Optional[dict[str, matplotlib.axes.Axes]]:
) -> dict[str, matplotlib.axes.Axes] | None:
"""Plot a heatmap of smoothed gene expression by chromosome.
Wrapper around :func:`scanpy.pl.heatmap`.
Expand Down Expand Up @@ -71,7 +69,7 @@ def chromosome_heatmap(
kwargs["norm"] = TwoSlopeNorm(0, vmin=vmin, vmax=vmax)

# add chromosome annotations
var_group_positions = list(zip(chr_pos, chr_pos[1:] + [tmp_adata.shape[1]]))
var_group_positions = list(zip(chr_pos, chr_pos[1:] + [tmp_adata.shape[1]], strict=False))

return_ax_dic = sc.pl.heatmap(
tmp_adata,
Expand Down Expand Up @@ -99,12 +97,12 @@ def chromosome_heatmap_summary(
*,
groupby: str = "cnv_leiden",
use_rep: str = "cnv",
cmap: Union[str, Colormap] = "bwr",
cmap: str | Colormap = "bwr",
figsize: tuple[int, int] = (16, 10),
show: Optional[bool] = None,
save: Union[str, bool, None] = None,
show: bool | None = None,
save: str | bool | None = None,
**kwargs,
) -> Optional[dict[str, matplotlib.axes.Axes]]:
) -> dict[str, matplotlib.axes.Axes] | None:
"""Plot a heatmap of average of the smoothed gene expression by chromosome per category in groupby.
Wrapper around :func:`scanpy.pl.heatmap`.
Expand Down Expand Up @@ -172,7 +170,7 @@ def _get_group_mean(group):
kwargs["norm"] = TwoSlopeNorm(0, vmin=vmin, vmax=vmax)

# add chromosome annotations
var_group_positions = list(zip(chr_pos, chr_pos[1:] + [tmp_adata.shape[1]]))
var_group_positions = list(zip(chr_pos, chr_pos[1:] + [tmp_adata.shape[1]], strict=False))

return_ax_dic = sc.pl.heatmap(
tmp_adata,
Expand Down
2 changes: 1 addition & 1 deletion src/infercnvpy/tl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def pca(
use_rep: str = "cnv",
key_added: str = "cnv_pca",
**kwargs,
) -> Union[np.ndarray, None]:
) -> np.ndarray | None:
"""Compute the PCA on the result of :func:`infercnvpy.tl.infercnv`.
Thin wrapper around :func:`scanpy.pp.pca`.
Expand Down
7 changes: 3 additions & 4 deletions src/infercnvpy/tl/_copykat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
from multiprocessing import cpu_count
from typing import Optional

import pandas as pd
from anndata import AnnData
Expand All @@ -18,8 +17,8 @@ def copykat(
min_genes_chr: int = 5,
key_added: str = "cnv",
inplace: bool = True,
layer: Optional[str] = None,
n_jobs: Optional[int] = None,
layer: str | None = None,
n_jobs: int | None = None,
norm_cell_names: str = "",
cell_line="no",
window_size=25,
Expand Down Expand Up @@ -172,7 +171,7 @@ def copykat(

if inplace:
adata.uns[key_added] = chrom_pos
adata.obsm["X_%s" % key_added] = new_cpkat_trans
adata.obsm[f"X_{key_added}"] = new_cpkat_trans
adata.obs[key_added] = copyKAT_pred
else:
return new_cpkat_trans, copyKAT_pred
30 changes: 15 additions & 15 deletions src/infercnvpy/tl/_infercnv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import re
from collections.abc import Sequence
from multiprocessing import cpu_count
from typing import Union

import numpy as np
import scipy.ndimage
Expand All @@ -18,20 +17,20 @@
def infercnv(
adata: AnnData,
*,
reference_key: Union[str, None] = None,
reference_cat: Union[None, str, Sequence[str]] = None,
reference: Union[np.ndarray, None] = None,
reference_key: str | None = None,
reference_cat: None | str | Sequence[str] = None,
reference: np.ndarray | None = None,
lfc_clip: float = 3,
window_size: int = 100,
step: int = 10,
dynamic_threshold: Union[float, None] = 1.5,
exclude_chromosomes: Union[Sequence[str], None] = ("chrX", "chrY"),
dynamic_threshold: float | None = 1.5,
exclude_chromosomes: Sequence[str] | None = ("chrX", "chrY"),
chunksize: int = 5000,
n_jobs: Union[int, None] = None,
n_jobs: int | None = None,
inplace: bool = True,
layer: Union[str, None] = None,
layer: str | None = None,
key_added: str = "cnv",
) -> Union[None, tuple[dict, scipy.sparse.csr_matrix]]:
) -> None | tuple[dict, scipy.sparse.csr_matrix]:
"""Infer Copy Number Variation (CNV) by averaging gene expression over genomic regions.
This method is heavily inspired by `infercnv <https://github.com/broadinstitute/inferCNV/>`_
Expand Down Expand Up @@ -121,7 +120,8 @@ def infercnv(
itertools.repeat(dynamic_threshold),
tqdm_class=tqdm,
max_workers=cpu_count() if n_jobs is None else n_jobs,
)
),
strict=False,
)
res = scipy.sparse.vstack(chunks)

Expand All @@ -148,7 +148,7 @@ def alphanum_key(key):
return sorted(l, key=alphanum_key)


def _running_mean(x: Union[np.ndarray, scipy.sparse.spmatrix], n: int = 50, step: int = 10) -> np.ndarray:
def _running_mean(x: np.ndarray | scipy.sparse.spmatrix, n: int = 50, step: int = 10) -> np.ndarray:
"""Compute a pyramidially weighted running mean.
Densifies the matrix. Use `step` and `chunksize` to save memory.
Expand Down Expand Up @@ -213,16 +213,16 @@ def _running_mean_for_chromosome(chr):

running_means = [_running_mean_for_chromosome(chr) for chr in chromosomes]

chr_start_pos = dict(zip(chromosomes, np.cumsum([0] + [x.shape[1] for x in running_means])))
chr_start_pos = dict(zip(chromosomes, np.cumsum([0] + [x.shape[1] for x in running_means]), strict=False))

return chr_start_pos, np.hstack(running_means)


def _get_reference(
adata: AnnData,
reference_key: Union[str, None],
reference_cat: Union[None, str, Sequence[str]],
reference: Union[np.ndarray, None],
reference_key: str | None,
reference_cat: None | str | Sequence[str],
reference: np.ndarray | None,
) -> np.ndarray:
"""Parameter validation extraction of reference gene expression.
Expand Down
12 changes: 6 additions & 6 deletions src/infercnvpy/tl/_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import warnings
from collections.abc import Mapping
from typing import Any, Optional
from typing import Any

import numpy as np
import scipy.sparse as sp
Expand All @@ -19,7 +19,7 @@ def cnv_score(
key_added: str = "cnv_score",
inplace: bool = True,
obs_key=None,
) -> Optional[Mapping[Any, np.number]]:
) -> Mapping[Any, np.number] | None:
"""Assign each cnv cluster a CNV score.
Clusters with a high score are likely affected by copy number abberations.
Expand Down Expand Up @@ -78,11 +78,11 @@ def ithgex(
adata: AnnData,
groupby: str,
*,
use_raw: Optional[bool] = None,
layer: Optional[str] = None,
use_raw: bool | None = None,
layer: str | None = None,
inplace: bool = True,
key_added: str = "ithgex",
) -> Optional[Mapping[str, float]]:
) -> Mapping[str, float] | None:
"""Compute the ITHGEX diversity score based on gene expression cite:`Wu2021`.
A high score indicates a high diversity of gene expression profiles of cells
Expand Down Expand Up @@ -158,7 +158,7 @@ def ithcna(
use_rep: str = "X_cnv",
key_added: str = "ithcna",
inplace: bool = True,
) -> Optional[Mapping[str, float]]:
) -> Mapping[str, float] | None:
"""Compute the ITHCNA diversity score based on copy number variation :cite:`Wu2021`.
A high score indicates a high diversity of CNV profiles of cells
Expand Down

0 comments on commit acbda4f

Please sign in to comment.