clone2vec 0.1.0__tar.gz → 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. {clone2vec-0.1.0 → clone2vec-0.1.1}/PKG-INFO +1 -1
  2. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec/__init__.py +1 -1
  3. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec/datasets.py +67 -5
  4. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec/plotting.py +74 -1
  5. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec/utils.py +107 -0
  6. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec.egg-info/PKG-INFO +1 -1
  7. {clone2vec-0.1.0 → clone2vec-0.1.1}/setup.cfg +1 -1
  8. {clone2vec-0.1.0 → clone2vec-0.1.1}/tests/test_associations.py +1 -1
  9. {clone2vec-0.1.0 → clone2vec-0.1.1}/tests/test_pl.py +12 -0
  10. {clone2vec-0.1.0 → clone2vec-0.1.1}/tests/test_utils.py +18 -1
  11. {clone2vec-0.1.0 → clone2vec-0.1.1}/README.md +0 -0
  12. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec/associations.py +0 -0
  13. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec/catboost.py +0 -0
  14. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec/eigenvalues.py +0 -0
  15. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec/embeddings.py +0 -0
  16. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec/integration.py +0 -0
  17. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec/preprocessing.py +0 -0
  18. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec/seurat.py +0 -0
  19. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec/tools.py +0 -0
  20. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec.egg-info/SOURCES.txt +0 -0
  21. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec.egg-info/dependency_links.txt +0 -0
  22. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec.egg-info/requires.txt +0 -0
  23. {clone2vec-0.1.0 → clone2vec-0.1.1}/clone2vec.egg-info/top_level.txt +0 -0
  24. {clone2vec-0.1.0 → clone2vec-0.1.1}/pyproject.toml +0 -0
  25. {clone2vec-0.1.0 → clone2vec-0.1.1}/tests/test_embeddings.py +0 -0
  26. {clone2vec-0.1.0 → clone2vec-0.1.1}/tests/test_integration.py +0 -0
  27. {clone2vec-0.1.0 → clone2vec-0.1.1}/tests/test_pp.py +0 -0
  28. {clone2vec-0.1.0 → clone2vec-0.1.1}/tests/test_tl.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: clone2vec
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: Package for an analysis of lineage-tracing scRNA-Seq data
5
5
  Home-page: https://github.com/kharchenkolab/clone2vec
6
6
  Author: Sergey Isaev
@@ -5,7 +5,7 @@ from . import preprocessing as pp
5
5
  from . import utils
6
6
  from . import seurat
7
7
 
8
- __version__ = "2.0.1"
8
+ __version__ = "0.1.1"
9
9
 
10
10
  # Restrict top-level tab-completion to submodules only
11
11
  __all__ = [
@@ -1,27 +1,89 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from scanpy import read, AnnData
4
+ from typing import Literal
4
5
  from pathlib import Path
5
6
 
7
+ import scanpy as sc
8
+
6
9
  zenodo_record_lt = "15334396"
10
+ zenodo_record_c2v = "19973466"
11
+
12
+ logg = sc.logging
13
+
14
+ __all__ = [
15
+ "Liu_NSCLC_CD8",
16
+ "Weinreb_in_vitro",
17
+ "Erickson_murine_development",
18
+ ]
19
+
20
+ def __dir__():
21
+ return sorted(__all__)
22
+
23
+ def Liu_NSCLC_CD8(
24
+ embedding_type: Literal["gex", "c2v"] = "gex",
25
+ file_path: str | Path | None = None,
26
+ ) -> AnnData:
27
+ """
28
+ Dataset from Liu et al. [PMID: 35121991] with CD8 T cells from NSCLC.
29
+
30
+ Parameters
31
+ ----------
32
+ embedding_type : Literal["gex", "c2v"], optional
33
+ Type of embedding to use, by default "gex".
34
+ file_path : str | Path | None, optional
35
+ Path where .h5ad-container will be stored, by default None.
36
+
37
+ Returns
38
+ -------
39
+ AnnData
40
+ Annotated data matrix with the dataset.
41
+ """
42
+ if embedding_type == "c2v":
43
+ logg.info("using clonal embedding (compositions in adata.X)")
44
+ elif embedding_type == "gex":
45
+ logg.info("using gene expression embedding")
46
+ else:
47
+ raise ValueError("embedding_type must be one of 'gex' or 'c2v'")
48
+
49
+ if file_path is None:
50
+ file_path = f"data/Liu_CD8_{embedding_type}.h5ad"
51
+
52
+ url = f"https://zenodo.org/records/{zenodo_record_c2v}/files/Liu_CD8_{embedding_type}.h5ad"
53
+
54
+ adata = read(file_path, backup_url=url, sparse=True, cache=True)
55
+ return adata
7
56
 
8
57
  def Weinreb_in_vitro(
9
- file_path: str | Path = "data/Weinreb_in_vitro.h5ad",
58
+ embedding_type: Literal["gex", "c2v"] = "gex",
59
+ file_path: str | Path | None = None,
10
60
  ) -> AnnData:
11
61
  """
12
- Dataset from [PMID: 31974159] with in vitro hematopoiesis.
62
+ Dataset from Weinreb et al. [PMID: 31974159] with in vitro hematopoiesis.
13
63
 
14
64
  Parameters
15
65
  ----------
16
- file_path : str | Path, optional
17
- Path where .h5ad-container will be stored, by default "data/Weinreb_in_vitro.h5ad".
66
+ embedding_type : Literal["gex", "c2v"], optional
67
+ Type of embedding to use, by default "gex".
68
+ file_path : str | Path | None, optional
69
+ Path where .h5ad-container will be stored, by default None.
18
70
 
19
71
  Returns
20
72
  -------
21
73
  AnnData
22
74
  Annotated data matrix with the dataset.
23
75
  """
24
- url = f"https://zenodo.org/records/{zenodo_record_lt}/files/Weinreb_in_vitro.h5ad"
76
+ if embedding_type == "c2v":
77
+ logg.info("using clonal embedding (compositions in adata.X)")
78
+ elif embedding_type == "gex":
79
+ logg.info("using gene expression embedding")
80
+ else:
81
+ raise ValueError("embedding_type must be one of 'gex' or 'c2v'")
82
+
83
+ if file_path is None:
84
+ file_path = f"data/Weinreb_in_vitro_{embedding_type}.h5ad"
85
+
86
+ url = f"https://zenodo.org/records/{zenodo_record_c2v}/files/Weinreb_in_vitro_{embedding_type}.h5ad"
25
87
 
26
88
  adata = read(file_path, backup_url=url, sparse=True, cache=True)
27
89
  return adata
@@ -35,11 +35,84 @@ __all__ = [
35
35
  "scaled_dotplot",
36
36
  "scatter2vars",
37
37
  "pca_loadings",
38
+ "embedding",
38
39
  ]
39
40
 
40
41
  def __dir__():
41
42
  return sorted(__all__)
42
43
 
44
+ def embedding(
45
+ adata: sc.AnnData,
46
+ obsm_name: str | None = None,
47
+ obsm_component: int | str | list[int] | list[str] | None = None,
48
+ basis: str = "X_umap",
49
+ **kwargs
50
+ ) -> matplotlib.axes.Axes | matplotlib.figure.Figure | None:
51
+ """
52
+ Wrapper around sc.pl.embedding that allows to plot embedding from obsm.
53
+
54
+ Parameters
55
+ ----------
56
+ adata: sc.AnnData
57
+ Annotated data matrix.
58
+ obsm_name: str | None
59
+ Key for embedding in `adata.obsm`.
60
+ obsm_component: int | str | list[int] | list[str] | None
61
+ Component(s) to plot. Can be:
62
+
63
+ - ``int``: column index (works for both arrays and DataFrames).
64
+ - ``str``: column name (only when ``adata.obsm[obsm_name]`` is a
65
+ :class:`~pandas.DataFrame`).
66
+ - ``list[int]`` or ``list[str]``: multiple components; each is
67
+ plotted as a separate colour channel via *sc.pl.embedding*.
68
+ - ``None`` (default): uses component ``0``.
69
+ basis: str
70
+ Key for embedding coordinates in `adata.obsm`.
71
+ **kwargs
72
+ Additional arguments to pass to `sc.pl.embedding`.
73
+
74
+ Returns
75
+ -------
76
+ res: matplotlib.axes.Axes | matplotlib.figure.Figure | None
77
+ Axes object with the plot.
78
+ """
79
+ tmp_keys: list[str] = [] # obs keys to clean up afterwards
80
+ if obsm_name:
81
+ if obsm_component is None:
82
+ obsm_component = 0
83
+
84
+ obsm_data = adata.obsm[obsm_name]
85
+ is_df = isinstance(obsm_data, pd.DataFrame)
86
+
87
+ # Normalise to a list so that the loop below works for both
88
+ # scalar and list inputs.
89
+ components = (
90
+ obsm_component
91
+ if isinstance(obsm_component, list)
92
+ else [obsm_component]
93
+ )
94
+
95
+ for comp in components:
96
+ obs_key = f"{obsm_name}_{comp}"
97
+ if is_df:
98
+ if isinstance(comp, str):
99
+ adata.obs[obs_key] = list(obsm_data[comp])
100
+ else:
101
+ adata.obs[obs_key] = list(obsm_data.iloc[:, comp])
102
+ else:
103
+ adata.obs[obs_key] = list(obsm_data[:, comp])
104
+ tmp_keys.append(obs_key)
105
+
106
+ kwargs["color"] = tmp_keys if len(tmp_keys) > 1 else tmp_keys[0]
107
+
108
+ if "ax" in kwargs:
109
+ kwargs["show"] = False
110
+ res = sc.pl.embedding(adata, basis=basis, **kwargs)
111
+ for key in tmp_keys:
112
+ del adata.obs[key]
113
+ return res
114
+
115
+
43
116
  def pca_loadings(
44
117
  adata: sc.AnnData,
45
118
  key: str = "gPCs",
@@ -509,7 +582,7 @@ def group_scatter(
509
582
  if kwargs_background is None:
510
583
  kwargs_background = [{} for i in range(len(groups))]
511
584
  if isinstance(kwargs_background, dict):
512
- kwargs_background = [kwargs_backgroundcopy() for i in range(len(groups))]
585
+ kwargs_background = [kwargs_background.copy() for i in range(len(groups))]
513
586
  elif len(kwargs_background) != len(groups):
514
587
  raise ValueError("kwargs_background must be a dict or a list of dict with length equal to the number of groups")
515
588
  if kwargs_group is None:
@@ -15,6 +15,8 @@ __all__ = [
15
15
  "get_connectivity_matrix",
16
16
  "gs",
17
17
  "regress_categories",
18
+ "impute",
19
+ "laplacian_eigenmaps",
18
20
  ]
19
21
 
20
22
  def __dir__():
@@ -507,6 +509,10 @@ def _nan_mask(X: np.ndarray | sp.sparse.csr_matrix) -> np.ndarray:
507
509
  mask = np.isnan(arr).any(axis=1)
508
510
  return mask
509
511
 
512
+
513
+
514
+
515
+
510
516
  def gs(
511
517
  adata: sc.AnnData,
512
518
  use_rep: str = "X_pca",
@@ -615,4 +621,105 @@ def gs(
615
621
  f" .obs['{obs_key}'] geometric sketching labels",
616
622
  " .uns['gs'] parameters",
617
623
  ]
624
+ logg.info(" finished ({time_passed})", deep="\n".join(lines), time=start)
625
+
626
+ def laplacian_eigenmaps(
627
+ adata: sc.AnnData,
628
+ obsp_key: str = "connectivities",
629
+ n_components: int = 20,
630
+ key_added: str = "X_laplacian",
631
+ norm_laplacian: bool = True,
632
+ uns_key: str = "laplacian_eigenmaps",
633
+ ) -> None:
634
+ """
635
+ Compute Laplacian eigenmaps from a sparse graph stored in ``adata.obsp``.
636
+
637
+ The function builds the (normalised) graph Laplacian from the adjacency
638
+ matrix in ``adata.obsp[obsp_key]``, computes the smallest non-trivial
639
+ eigenvectors, and stores them in ``adata.obsm[key_added]``.
640
+
641
+ Parameters
642
+ ----------
643
+ adata : sc.AnnData
644
+ Annotated data matrix. Must contain a sparse adjacency matrix in
645
+ ``adata.obsp[obsp_key]``.
646
+ obsp_key : str, optional
647
+ Key in ``adata.obsp`` for the adjacency / connectivity matrix.
648
+ Default is ``"connectivities"``.
649
+ n_components : int, optional
650
+ Number of eigenvectors to compute (excluding the trivial constant
651
+ eigenvector). Default is 20.
652
+ key_added : str, optional
653
+ Key in ``adata.obsm`` where the embedding is stored.
654
+ Default is ``"X_laplacian"``.
655
+ norm_laplacian : bool, optional
656
+ If ``True``, use the symmetric normalised Laplacian
657
+ :math:`I - D^{-1/2} W D^{-1/2}`. Otherwise use the combinatorial
658
+ Laplacian :math:`D - W`. Default is ``True``.
659
+ uns_key : str, optional
660
+ Name of the new column in ``adata.uns`` to store the Laplacian eigenmaps parameters,
661
+ by default "laplacian_eigenmaps".
662
+
663
+ Returns
664
+ -------
665
+ None
666
+ The embedding is stored in place in ``adata.obsm[key_added]``.
667
+ """
668
+ from scipy.sparse.linalg import eigsh
669
+
670
+ line_before = f"\n using .obsp['{obsp_key}'], n_components={n_components}"
671
+ start = logg.info("computing Laplacian eigenmaps", deep=line_before)
672
+
673
+ if obsp_key not in adata.obsp:
674
+ raise KeyError(f"'{obsp_key}' not found in adata.obsp")
675
+
676
+ W = adata.obsp[obsp_key]
677
+ if not sp.issparse(W):
678
+ W = sp.csr_matrix(W)
679
+ else:
680
+ W = W.tocsr()
681
+
682
+ if (W - W.T).nnz > 0:
683
+ W = (W + W.T) / 2.0
684
+
685
+ n = W.shape[0]
686
+ if n_components >= n:
687
+ raise ValueError(
688
+ f"n_components ({n_components}) must be less than the number of "
689
+ f"observations ({n})"
690
+ )
691
+
692
+ d = np.asarray(W.sum(axis=1)).ravel()
693
+
694
+ if norm_laplacian:
695
+ d_inv_sqrt = np.zeros_like(d)
696
+ mask = d > 0
697
+ d_inv_sqrt[mask] = 1.0 / np.sqrt(d[mask])
698
+ D_inv_sqrt = sp.diags(d_inv_sqrt)
699
+ L = sp.eye(n, format="csr") - D_inv_sqrt @ W @ D_inv_sqrt
700
+ else:
701
+ D = sp.diags(d)
702
+ L = D - W
703
+
704
+ eigenvalues, eigenvectors = eigsh(L, k=n_components + 1, which="SM")
705
+
706
+ order = np.argsort(eigenvalues)
707
+ eigenvalues = eigenvalues[order]
708
+ eigenvectors = eigenvectors[:, order]
709
+
710
+ embedding = eigenvectors[:, 1:]
711
+
712
+ adata.obsm[key_added] = embedding
713
+ adata.uns[uns_key] = {
714
+ "eigenvalues": eigenvalues[1:],
715
+ "components": n_components,
716
+ "norm_laplacian": norm_laplacian,
717
+ "key_added": key_added,
718
+ "obsp_key": obsp_key,
719
+ }
720
+ lines = [
721
+ "added",
722
+ f" .obsm['{key_added}'] Laplacian eigenmaps ({n_components} components)",
723
+ f" .uns['{uns_key}'] parameters",
724
+ ]
618
725
  logg.info(" finished ({time_passed})", deep="\n".join(lines), time=start)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: clone2vec
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: Package for an analysis of lineage-tracing scRNA-Seq data
5
5
  Home-page: https://github.com/kharchenkolab/clone2vec
6
6
  Author: Sergey Isaev
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = clone2vec
3
- version = 0.1.0
3
+ version = 0.1.1
4
4
  author = Sergey Isaev
5
5
  author_email = s@isaev.cc
6
6
  description = Package for an analysis of lineage-tracing scRNA-Seq data
@@ -340,7 +340,7 @@ def test_associations_binomial_gam(adata_binomial):
340
340
 
341
341
  def test_weighted_corr_vs_manual():
342
342
  """Verify that _fast_corr with weights matches a manual weighted Pearson."""
343
- from sclitr.associations import _fast_corr
343
+ from clone2vec.associations import _fast_corr
344
344
 
345
345
  rng = np.random.RandomState(42)
346
346
  n, p, q = 100, 5, 2
@@ -74,6 +74,18 @@ def test_group_kde_returns_figure(matrix_kind, adata_with_umap_dense, adata_with
74
74
  fig = c2v.pl.group_kde(ad, groupby="cell_type", groups=group, basis="X_umap", return_fig=True)
75
75
  assert isinstance(fig, plt.Figure)
76
76
 
77
+ # ===== embedding =====
78
+
79
+ def test_embedding_with_obsm(adata_with_umap_dense):
80
+ ad = adata_with_umap_dense.copy()
81
+ rng = np.random.RandomState(42)
82
+ ad.obsm["X_test_emb"] = rng.rand(ad.n_obs, 3)
83
+ ax = c2v.pl.embedding(ad, obsm_name="X_test_emb", obsm_component=1, basis="X_umap", show=False)
84
+ # temporary column should be removed after plotting
85
+ assert "X_test_emb_1" not in ad.obs.columns
86
+ plt.close("all")
87
+
88
+
77
89
  # ===== loss_history and clone_size =====
78
90
 
79
91
  def test_loss_history_plots_line(clones):
@@ -236,4 +236,21 @@ def test_impute_categorical(adata_dense):
236
236
  assert "ct_with_na_imp" in ad.obs
237
237
  imputed_vals = ad.obs["ct_with_na_imp"].astype(str).values
238
238
  assert np.sum(imputed_vals == "NA") == 0
239
- assert "impute_prob" in ad.obsm
239
+ assert "impute_prob" in ad.obsm
240
+
241
+
242
+ # ===== Tests for laplacian_eigenmaps =====
243
+
244
+ def test_laplacian_eigenmaps():
245
+ n, k = 50, 5
246
+ # ring graph
247
+ rows = list(range(n)) + list(range(1, n)) + [0]
248
+ cols = list(range(1, n)) + [0] + list(range(n))
249
+ data = [1.0] * len(rows)
250
+ W = sp.csr_matrix((data, (rows, cols)), shape=(n, n))
251
+ ad = sc.AnnData(X=np.zeros((n, 2)), obsp={"connectivities": W})
252
+ c2v.utils.laplacian_eigenmaps(ad, n_components=k)
253
+ assert "X_laplacian" in ad.obsm
254
+ assert ad.obsm["X_laplacian"].shape == (n, k)
255
+ assert "laplacian_eigenmaps" in ad.uns
256
+ assert len(ad.uns["laplacian_eigenmaps"]["eigenvalues"]) == k
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes