GOTDiff 0.1.0__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.
- gotdiff-0.1.0/LICENSE +21 -0
- gotdiff-0.1.0/PKG-INFO +188 -0
- gotdiff-0.1.0/README.md +147 -0
- gotdiff-0.1.0/examples/anndata_example.py +57 -0
- gotdiff-0.1.0/examples/basic_usage.py +60 -0
- gotdiff-0.1.0/examples/trajectory_viz.py +42 -0
- gotdiff-0.1.0/gotdiff/__init__.py +72 -0
- gotdiff-0.1.0/gotdiff/_adata.py +205 -0
- gotdiff-0.1.0/gotdiff/_branching.py +188 -0
- gotdiff-0.1.0/gotdiff/_core.py +365 -0
- gotdiff-0.1.0/gotdiff/_grassmann_dist.py +324 -0
- gotdiff-0.1.0/gotdiff/_plot.py +175 -0
- gotdiff-0.1.0/gotdiff/_pseudotime.py +196 -0
- gotdiff-0.1.0/gotdiff/_trajectory.py +293 -0
- gotdiff-0.1.0/pyproject.toml +46 -0
- gotdiff-0.1.0/tests/conftest.py +2 -0
- gotdiff-0.1.0/tests/test_anndata.py +63 -0
- gotdiff-0.1.0/tests/test_core.py +46 -0
- gotdiff-0.1.0/tests/test_grassmann_dist.py +67 -0
- gotdiff-0.1.0/tests/test_trajectory.py +58 -0
gotdiff-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 GOTDiff Developers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
gotdiff-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: GOTDiff
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Differentiation path / trajectory inference via geodesic distance on the Grassmann manifold, with AnnData support.
|
|
5
|
+
Project-URL: Homepage, https://github.com/ZengFLab/GOTDiff
|
|
6
|
+
Project-URL: Issues, https://github.com/ZengFLab/GOTDiff/issues
|
|
7
|
+
Author: GOTDiff Developers
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: differentiation,grassmann,optimal-transport,pseudotime,single-cell,trajectory-inference
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
20
|
+
Requires-Python: >=3.9
|
|
21
|
+
Requires-Dist: anndata>=0.9
|
|
22
|
+
Requires-Dist: grassmann-ot>=0.3.1
|
|
23
|
+
Requires-Dist: numpy>=1.24
|
|
24
|
+
Requires-Dist: scanpy>=1.9
|
|
25
|
+
Requires-Dist: scikit-learn>=1.3
|
|
26
|
+
Requires-Dist: scipy>=1.10
|
|
27
|
+
Requires-Dist: torch>=2.0
|
|
28
|
+
Provides-Extra: all
|
|
29
|
+
Requires-Dist: faiss-gpu; extra == 'all'
|
|
30
|
+
Requires-Dist: matplotlib>=3.7; extra == 'all'
|
|
31
|
+
Requires-Dist: plotly>=5.18; extra == 'all'
|
|
32
|
+
Requires-Dist: seaborn>=0.12; extra == 'all'
|
|
33
|
+
Provides-Extra: faiss
|
|
34
|
+
Requires-Dist: faiss-cpu; extra == 'faiss'
|
|
35
|
+
Provides-Extra: gpu
|
|
36
|
+
Requires-Dist: faiss-gpu; extra == 'gpu'
|
|
37
|
+
Provides-Extra: plot
|
|
38
|
+
Requires-Dist: matplotlib>=3.7; extra == 'plot'
|
|
39
|
+
Requires-Dist: seaborn>=0.12; extra == 'plot'
|
|
40
|
+
Description-Content-Type: text/markdown
|
|
41
|
+
|
|
42
|
+
# GOTDiff
|
|
43
|
+
|
|
44
|
+
> **G**rassmann-**O**ptimal-**T**ransport **Diff**erentiation inference
|
|
45
|
+
|
|
46
|
+
Single-cell trajectory inference based on the **geodesic distance accumulated
|
|
47
|
+
along a differentiation path on the Grassmann manifold**.
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Why?
|
|
52
|
+
|
|
53
|
+
Each cell can be represented by a low-dimensional *subspace* of active
|
|
54
|
+
regulatory programs (NMF components, scVI / Harmony loadings, ...). This
|
|
55
|
+
subspace is a point on a Grassmann manifold. A differentiation trajectory is
|
|
56
|
+
therefore a **curve on that manifold**, and the **geodesic distance
|
|
57
|
+
accumulated along the curve** is the total amount of regulatory re-wiring a
|
|
58
|
+
cell has undergone since the root (most primitive) state.
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
differentiation distance = Σ d_G( Γ(t), Γ(t + Δt) )
|
|
62
|
+
t
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
That scalar — the **total "调控变化量"** along the分化路程 — is the central
|
|
66
|
+
output of GOTDiff.
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## Install
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pip install GOTDiff
|
|
74
|
+
# with GPU / plotting extras
|
|
75
|
+
pip install "GOTDiff[all]"
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Quick start
|
|
81
|
+
|
|
82
|
+
### Raw arrays
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
import numpy as np
|
|
86
|
+
from gotdiff import GOTDiff
|
|
87
|
+
|
|
88
|
+
# H comes from any external tool: NMF, scVI, Harmony, ...
|
|
89
|
+
# shape: (n_gene_programs, n_cells)
|
|
90
|
+
H = np.random.rand(20, 500)
|
|
91
|
+
|
|
92
|
+
gd = GOTDiff(k=30, p=3, metric="geodesic", backend="faiss")
|
|
93
|
+
gd.fit(H)
|
|
94
|
+
|
|
95
|
+
adata.obs["gotdiff_pseudotime"] # 伪时间
|
|
96
|
+
adata.obs["gotdiff_diff_distance"] # 分化路程(核心输出)
|
|
97
|
+
adata.obsm["X_gotdiff_grassmann"] # 每个细胞的 Grassmann 坐标
|
|
98
|
+
adata.uns["gotdiff"] # 参数、分支点等元信息
|
|
99
|
+
|
|
100
|
+
# compare total differentiation "cost" of each lineage
|
|
101
|
+
lineage_costs = gd.branch_path_lengths(adata.obs["cell_type"])
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### AnnData (recommended)
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
import scanpy as sc
|
|
108
|
+
from gotdiff import GOTDiff
|
|
109
|
+
|
|
110
|
+
adata = sc.read_h5ad("hematopoiesis.h5ad")
|
|
111
|
+
|
|
112
|
+
# If you already have a factor matrix (NMF / scVI / Harmony), point GOTDiff at it:
|
|
113
|
+
gd = GOTDiff(k=30, p=3, backend="faiss")
|
|
114
|
+
gd.fit_anndata(adata, use_rep="X_pca") # or
|
|
115
|
+
|
|
116
|
+
# If you don't, GOTDiff runs NMF for you and stores adata.obsm['X_nmf']:
|
|
117
|
+
gd.fit_anndata(adata, n_programs=25)
|
|
118
|
+
|
|
119
|
+
# Results are written into adata in-place:
|
|
120
|
+
# adata.obs['gotdiff_pseudotime']
|
|
121
|
+
# adata.obs['gotdiff_diff_distance']
|
|
122
|
+
# adata.obsm['X_gotdiff_grassmann']
|
|
123
|
+
# adata.uns['gotdiff']
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Plotting
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
from gotdiff._plot import scatter, diffusion_landscape
|
|
130
|
+
|
|
131
|
+
scatter(adata, color="gotdiff_diff_distance", basis="X_umap", show_root=True)
|
|
132
|
+
diffusion_landscape(adata, color="cell_type")
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## Pipeline
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
expression matrix / factor matrix (H)
|
|
141
|
+
│
|
|
142
|
+
▼
|
|
143
|
+
cells_to_grassmann ──────────────── (from grassmann-ot)
|
|
144
|
+
→ U_list : (n_cells, d, p) each cell = a subspace
|
|
145
|
+
│
|
|
146
|
+
▼
|
|
147
|
+
grassmann_pseudotime ─────────────── intrinsic to Grassmann space
|
|
148
|
+
→ pseudotime, root
|
|
149
|
+
│
|
|
150
|
+
▼
|
|
151
|
+
accumulate_geodesic_distance ────── ★ the differentiation path length
|
|
152
|
+
→ diff_distance for every cell
|
|
153
|
+
│
|
|
154
|
+
▼
|
|
155
|
+
detect_branch_points ─────────────── curvature + local anisotropy
|
|
156
|
+
→ branch indices & scores
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## API summary
|
|
162
|
+
|
|
163
|
+
| Object | Purpose |
|
|
164
|
+
|--------|---------|
|
|
165
|
+
| `GOTDiff` | Main estimator — `fit(H)` or `fit_anndata(adata)` |
|
|
166
|
+
| `grassmann_geodesic_distance` | Pairwise geodesic distance on Gr(p, d) |
|
|
167
|
+
| `grassmann_chordal_distance` | Cheaper Frobenius projector distance |
|
|
168
|
+
| `accumulate_geodesic_distance` | ★ Differentiation path length |
|
|
169
|
+
| `grassmann_pseudotime` | Intrinsic Grassmann pseudotime |
|
|
170
|
+
| `fit_geodesic_curve` | Smooth curve through Grassmann points |
|
|
171
|
+
| `detect_branch_points` | Branch / lineage divergence points |
|
|
172
|
+
| `extract_H_from_adata` | Read a factor matrix from AnnData |
|
|
173
|
+
| `write_results_to_adata` | Persist every result to AnnData |
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## Relation to `grassmann-ot`
|
|
178
|
+
|
|
179
|
+
GOTDiff **builds on** [`grassmann-ot`](https://github.com/ZengFLab/grassmann-ot):
|
|
180
|
+
it reuses `cells_to_grassmann` to map cells to Grassmann points, then adds a
|
|
181
|
+
full **trajectory-inference layer** on top (pseudotime, path-length, branching,
|
|
182
|
+
AnnData I/O, plotting).
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
MIT
|
gotdiff-0.1.0/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# GOTDiff
|
|
2
|
+
|
|
3
|
+
> **G**rassmann-**O**ptimal-**T**ransport **Diff**erentiation inference
|
|
4
|
+
|
|
5
|
+
Single-cell trajectory inference based on the **geodesic distance accumulated
|
|
6
|
+
along a differentiation path on the Grassmann manifold**.
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Why?
|
|
11
|
+
|
|
12
|
+
Each cell can be represented by a low-dimensional *subspace* of active
|
|
13
|
+
regulatory programs (NMF components, scVI / Harmony loadings, ...). This
|
|
14
|
+
subspace is a point on a Grassmann manifold. A differentiation trajectory is
|
|
15
|
+
therefore a **curve on that manifold**, and the **geodesic distance
|
|
16
|
+
accumulated along the curve** is the total amount of regulatory re-wiring a
|
|
17
|
+
cell has undergone since the root (most primitive) state.
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
differentiation distance = Σ d_G( Γ(t), Γ(t + Δt) )
|
|
21
|
+
t
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
That scalar — the **total "调控变化量"** along the分化路程 — is the central
|
|
25
|
+
output of GOTDiff.
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install GOTDiff
|
|
33
|
+
# with GPU / plotting extras
|
|
34
|
+
pip install "GOTDiff[all]"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Quick start
|
|
40
|
+
|
|
41
|
+
### Raw arrays
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
import numpy as np
|
|
45
|
+
from gotdiff import GOTDiff
|
|
46
|
+
|
|
47
|
+
# H comes from any external tool: NMF, scVI, Harmony, ...
|
|
48
|
+
# shape: (n_gene_programs, n_cells)
|
|
49
|
+
H = np.random.rand(20, 500)
|
|
50
|
+
|
|
51
|
+
gd = GOTDiff(k=30, p=3, metric="geodesic", backend="faiss")
|
|
52
|
+
gd.fit(H)
|
|
53
|
+
|
|
54
|
+
adata.obs["gotdiff_pseudotime"] # 伪时间
|
|
55
|
+
adata.obs["gotdiff_diff_distance"] # 分化路程(核心输出)
|
|
56
|
+
adata.obsm["X_gotdiff_grassmann"] # 每个细胞的 Grassmann 坐标
|
|
57
|
+
adata.uns["gotdiff"] # 参数、分支点等元信息
|
|
58
|
+
|
|
59
|
+
# compare total differentiation "cost" of each lineage
|
|
60
|
+
lineage_costs = gd.branch_path_lengths(adata.obs["cell_type"])
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### AnnData (recommended)
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
import scanpy as sc
|
|
67
|
+
from gotdiff import GOTDiff
|
|
68
|
+
|
|
69
|
+
adata = sc.read_h5ad("hematopoiesis.h5ad")
|
|
70
|
+
|
|
71
|
+
# If you already have a factor matrix (NMF / scVI / Harmony), point GOTDiff at it:
|
|
72
|
+
gd = GOTDiff(k=30, p=3, backend="faiss")
|
|
73
|
+
gd.fit_anndata(adata, use_rep="X_pca") # or
|
|
74
|
+
|
|
75
|
+
# If you don't, GOTDiff runs NMF for you and stores adata.obsm['X_nmf']:
|
|
76
|
+
gd.fit_anndata(adata, n_programs=25)
|
|
77
|
+
|
|
78
|
+
# Results are written into adata in-place:
|
|
79
|
+
# adata.obs['gotdiff_pseudotime']
|
|
80
|
+
# adata.obs['gotdiff_diff_distance']
|
|
81
|
+
# adata.obsm['X_gotdiff_grassmann']
|
|
82
|
+
# adata.uns['gotdiff']
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Plotting
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from gotdiff._plot import scatter, diffusion_landscape
|
|
89
|
+
|
|
90
|
+
scatter(adata, color="gotdiff_diff_distance", basis="X_umap", show_root=True)
|
|
91
|
+
diffusion_landscape(adata, color="cell_type")
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## Pipeline
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
expression matrix / factor matrix (H)
|
|
100
|
+
│
|
|
101
|
+
▼
|
|
102
|
+
cells_to_grassmann ──────────────── (from grassmann-ot)
|
|
103
|
+
→ U_list : (n_cells, d, p) each cell = a subspace
|
|
104
|
+
│
|
|
105
|
+
▼
|
|
106
|
+
grassmann_pseudotime ─────────────── intrinsic to Grassmann space
|
|
107
|
+
→ pseudotime, root
|
|
108
|
+
│
|
|
109
|
+
▼
|
|
110
|
+
accumulate_geodesic_distance ────── ★ the differentiation path length
|
|
111
|
+
→ diff_distance for every cell
|
|
112
|
+
│
|
|
113
|
+
▼
|
|
114
|
+
detect_branch_points ─────────────── curvature + local anisotropy
|
|
115
|
+
→ branch indices & scores
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## API summary
|
|
121
|
+
|
|
122
|
+
| Object | Purpose |
|
|
123
|
+
|--------|---------|
|
|
124
|
+
| `GOTDiff` | Main estimator — `fit(H)` or `fit_anndata(adata)` |
|
|
125
|
+
| `grassmann_geodesic_distance` | Pairwise geodesic distance on Gr(p, d) |
|
|
126
|
+
| `grassmann_chordal_distance` | Cheaper Frobenius projector distance |
|
|
127
|
+
| `accumulate_geodesic_distance` | ★ Differentiation path length |
|
|
128
|
+
| `grassmann_pseudotime` | Intrinsic Grassmann pseudotime |
|
|
129
|
+
| `fit_geodesic_curve` | Smooth curve through Grassmann points |
|
|
130
|
+
| `detect_branch_points` | Branch / lineage divergence points |
|
|
131
|
+
| `extract_H_from_adata` | Read a factor matrix from AnnData |
|
|
132
|
+
| `write_results_to_adata` | Persist every result to AnnData |
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## Relation to `grassmann-ot`
|
|
137
|
+
|
|
138
|
+
GOTDiff **builds on** [`grassmann-ot`](https://github.com/ZengFLab/grassmann-ot):
|
|
139
|
+
it reuses `cells_to_grassmann` to map cells to Grassmann points, then adds a
|
|
140
|
+
full **trajectory-inference layer** on top (pseudotime, path-length, branching,
|
|
141
|
+
AnnData I/O, plotting).
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## License
|
|
146
|
+
|
|
147
|
+
MIT
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""
|
|
2
|
+
AnnData integration example.
|
|
3
|
+
|
|
4
|
+
Requires scanpy and a small synthetic AnnData object (no real dataset needed).
|
|
5
|
+
Demonstrates: automatic NMF, fitting, and reading the results back.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
import scanpy as sc
|
|
11
|
+
|
|
12
|
+
from gotdiff import GOTDiff
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def synth_anndata(n_cells=300, n_vars=200, n_programs=20, seed=0):
|
|
16
|
+
"""A noisy factorized expression matrix wrapped as AnnData."""
|
|
17
|
+
rng = np.random.default_rng(seed)
|
|
18
|
+
W = rng.standard_normal((n_programs, n_vars))
|
|
19
|
+
H = rng.standard_normal((n_programs, n_cells))
|
|
20
|
+
X = (W.T @ H).T # (n_cells, n_vars)
|
|
21
|
+
X = np.clip(X, 0.0, None) # non-negative counts
|
|
22
|
+
adata = sc.AnnData(X=X)
|
|
23
|
+
adata.obs["cell_type"] = np.random.choice(
|
|
24
|
+
["Stem", "Prog", "Term1", "Term2"], size=n_cells
|
|
25
|
+
)
|
|
26
|
+
sc.pp.normalize_total(adata)
|
|
27
|
+
sc.pp.log1p(adata)
|
|
28
|
+
sc.pp.pca(adata, n_comps=20)
|
|
29
|
+
sc.pp.neighbors(adata, n_neighbors=15)
|
|
30
|
+
sc.tl.umap(adata)
|
|
31
|
+
return adata
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def main():
|
|
35
|
+
adata = synth_anndata()
|
|
36
|
+
|
|
37
|
+
gd = GOTDiff(k=30, p=3, backend="pca", n_programs=20)
|
|
38
|
+
# automatic NMF fallback: no h_key / use_rep provided
|
|
39
|
+
gd.fit_anndata(adata)
|
|
40
|
+
|
|
41
|
+
print("AnnData fields written by GOTDiff:")
|
|
42
|
+
print(" ", list(adata.obs.columns & {
|
|
43
|
+
"gotdiff_pseudotime", "gotdiff_diff_distance", "gotdiff_branch_score"}))
|
|
44
|
+
print(" obsm keys:", [k for k in adata.obsm.keys() if "gotdiff" in k])
|
|
45
|
+
print(" uns keys :", list(adata.uns.get("gotdiff", {}).keys()))
|
|
46
|
+
|
|
47
|
+
print("\ntop 5 cells by differentiation distance:")
|
|
48
|
+
print(adata.obs.nlargest(5, "gotdiff_diff_distance")[
|
|
49
|
+
["gotdiff_pseudotime", "gotdiff_diff_distance"]
|
|
50
|
+
])
|
|
51
|
+
|
|
52
|
+
print("\nlineage path-length costs:")
|
|
53
|
+
print(gd.branch_path_lengths(adata.obs["cell_type"]))
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
if __name__ == "__main__":
|
|
57
|
+
main()
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Basic usage example.
|
|
3
|
+
|
|
4
|
+
Generates a synthetic differentiation trajectory (a path on the Grassmann
|
|
5
|
+
manifold), runs GOTDiff end-to-end, and prints the core output.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
from gotdiff import (
|
|
11
|
+
GOTDiff,
|
|
12
|
+
grassmann_geodesic_distance,
|
|
13
|
+
accumulate_geodesic_distance,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def synth_trajectory(n_cells=200, d=50, p=3, noise=0.02, seed=0):
|
|
18
|
+
"""A smooth random walk on Gr(p, d) approximating a分化 trajectory."""
|
|
19
|
+
rng = np.random.default_rng(seed)
|
|
20
|
+
U0, _ = np.linalg.qr(rng.standard_normal((d, p)))
|
|
21
|
+
bases = [U0]
|
|
22
|
+
U = U0.copy()
|
|
23
|
+
for _ in range(n_cells - 1):
|
|
24
|
+
# a small random tangent step
|
|
25
|
+
A = rng.standard_normal((d, p)) * noise
|
|
26
|
+
U = U + A
|
|
27
|
+
U, _ = np.linalg.qr(U)
|
|
28
|
+
bases.append(U)
|
|
29
|
+
return np.stack(bases, axis=0)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def main():
|
|
33
|
+
n_cells, d, p = 200, 50, 3
|
|
34
|
+
bases = synth_trajectory(n_cells=n_cells, d=d, p=p)
|
|
35
|
+
|
|
36
|
+
# ---- direct low-level API ---------------------------------------------
|
|
37
|
+
D = grassmann_geodesic_distance(bases)
|
|
38
|
+
print("pairwise distance matrix shape:", D.shape)
|
|
39
|
+
print("mean nearest-neighbour distance: %.4f" % np.mean(np.sort(D, axis=1)[:, 1]))
|
|
40
|
+
|
|
41
|
+
diff_len = accumulate_geodesic_distance(bases)
|
|
42
|
+
print("\ndifferentiation path length (root -> terminal): %.4f" % diff_len.max())
|
|
43
|
+
print("per-cell distances (first 5):", np.round(diff_len[:5], 4))
|
|
44
|
+
|
|
45
|
+
# ---- high-level estimator ---------------------------------------------
|
|
46
|
+
# Synthesize an H matrix (factors) for the estimator
|
|
47
|
+
rng = np.random.default_rng(1)
|
|
48
|
+
H = rng.standard_normal((20, n_cells)) # (n_programs, n_cells)
|
|
49
|
+
|
|
50
|
+
gd = GOTDiff(k=30, p=p, metric="geodesic", backend="pca")
|
|
51
|
+
gd.fit(H)
|
|
52
|
+
print("\nestimator pseudotime range: %.3f .. %.3f" % (
|
|
53
|
+
gd.pseudotime_.min(), gd.pseudotime_.max()))
|
|
54
|
+
print("estimator diff-distance max: %.4f" % gd.diff_distances_.max())
|
|
55
|
+
if len(gd.branch_points_) > 0:
|
|
56
|
+
print("detected branch points:", gd.branch_points_)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
if __name__ == "__main__":
|
|
60
|
+
main()
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Visualisation example.
|
|
3
|
+
|
|
4
|
+
Requires matplotlib. Produces two figures: a UMAP coloured by
|
|
5
|
+
differentiation distance, and the "diffusion landscape" (pseudotime vs
|
|
6
|
+
path length).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
import scanpy as sc
|
|
12
|
+
|
|
13
|
+
from gotdiff import GOTDiff
|
|
14
|
+
from gotdiff._plot import diffusion_landscape, scatter
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def main():
|
|
18
|
+
rng = np.random.default_rng(0)
|
|
19
|
+
n = 400
|
|
20
|
+
X = rng.standard_normal((n, 50))
|
|
21
|
+
adata = sc.AnnData(X=X)
|
|
22
|
+
sc.pp.pca(adata, n_comps=20)
|
|
23
|
+
sc.tl.umap(adata)
|
|
24
|
+
|
|
25
|
+
H = rng.standard_normal((20, n))
|
|
26
|
+
GOTDiff(k=30, p=3, backend="pca").fit_anndata(adata, use_rep="X_pca")
|
|
27
|
+
|
|
28
|
+
fig, axes = sc.pl.subplots(1, 2, figsize=(12, 5))
|
|
29
|
+
scatter(adata, color="gotdiff_diff_distance", basis="X_umap",
|
|
30
|
+
show_root=True, ax=axes[0])
|
|
31
|
+
axes[0].set_title("Differentiation distance on UMAP")
|
|
32
|
+
|
|
33
|
+
diffusion_landscape(adata, color="cell_type", ax=axes[1])
|
|
34
|
+
axes[1].set_title("Pseudotime vs accumulated geodesic distance")
|
|
35
|
+
|
|
36
|
+
fig.tight_layout()
|
|
37
|
+
fig.savefig("gotdiff_overview.png", dpi=150, bbox_inches="tight")
|
|
38
|
+
print("saved gotdiff_overview.png")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
if __name__ == "__main__":
|
|
42
|
+
main()
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""
|
|
2
|
+
GOTDiff — Differentiation path / trajectory inference on the Grassmann manifold.
|
|
3
|
+
|
|
4
|
+
Core idea
|
|
5
|
+
----------
|
|
6
|
+
Single-cell RNA-seq (or any matrix-factorization output) gives, for each cell,
|
|
7
|
+
a low-dimensional **subspace** of active regulatory programs (e.g. NMF components,
|
|
8
|
+
scVI / Harmony loadings). Each such subspace is a point on a Grassmann manifold.
|
|
9
|
+
A differentiation trajectory is therefore a *curve* on that manifold; the
|
|
10
|
+
**geodesic distance accumulated along the curve** is the total amount of
|
|
11
|
+
regulatory re-wiring a cell has undergone since the root (most primitive) state.
|
|
12
|
+
|
|
13
|
+
Public API
|
|
14
|
+
----------
|
|
15
|
+
- :class:`GOTDiff` — main estimator, supports both raw arrays and AnnData.
|
|
16
|
+
- :func:`grassmann_geodesic_distance` — pairwise geodesic distance between
|
|
17
|
+
Grassmann points.
|
|
18
|
+
- :func:`accumulate_geodesic_distance` — turn a pseudotime-ordered sequence of
|
|
19
|
+
Grassmann points into a vector of differentiation-path lengths.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from ._grassmann_dist import (
|
|
23
|
+
GrassmannDistance,
|
|
24
|
+
grassmann_geodesic_distance,
|
|
25
|
+
grassmann_chordal_distance,
|
|
26
|
+
principal_angles,
|
|
27
|
+
)
|
|
28
|
+
from ._trajectory import (
|
|
29
|
+
GrassmannTrajectory,
|
|
30
|
+
accumulate_geodesic_distance,
|
|
31
|
+
fit_geodesic_curve,
|
|
32
|
+
)
|
|
33
|
+
from ._pseudotime import (
|
|
34
|
+
grassmann_pseudotime,
|
|
35
|
+
infer_root_cell,
|
|
36
|
+
)
|
|
37
|
+
from ._branching import (
|
|
38
|
+
BranchAnalyzer,
|
|
39
|
+
detect_branch_points,
|
|
40
|
+
)
|
|
41
|
+
from ._adata import (
|
|
42
|
+
extract_H_from_adata,
|
|
43
|
+
write_results_to_adata,
|
|
44
|
+
)
|
|
45
|
+
from ._core import GOTDiff
|
|
46
|
+
|
|
47
|
+
__version__ = "0.1.0"
|
|
48
|
+
|
|
49
|
+
__all__ = [
|
|
50
|
+
# top-level estimator
|
|
51
|
+
"GOTDiff",
|
|
52
|
+
# Grassmann geometry
|
|
53
|
+
"GrassmannDistance",
|
|
54
|
+
"grassmann_geodesic_distance",
|
|
55
|
+
"grassmann_chordal_distance",
|
|
56
|
+
"principal_angles",
|
|
57
|
+
# trajectory / path-length
|
|
58
|
+
"GrassmannTrajectory",
|
|
59
|
+
"accumulate_geodesic_distance",
|
|
60
|
+
"fit_geodesic_curve",
|
|
61
|
+
# pseudotime
|
|
62
|
+
"grassmann_pseudotime",
|
|
63
|
+
"infer_root_cell",
|
|
64
|
+
# branching
|
|
65
|
+
"BranchAnalyzer",
|
|
66
|
+
"detect_branch_points",
|
|
67
|
+
# AnnData I/O
|
|
68
|
+
"extract_H_from_adata",
|
|
69
|
+
"write_results_to_adata",
|
|
70
|
+
# metadata
|
|
71
|
+
"__version__",
|
|
72
|
+
]
|