-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdag.py
More file actions
552 lines (465 loc) · 20.1 KB
/
Copy pathdag.py
File metadata and controls
552 lines (465 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
"""Paradigm-aware analysis DAG for scAgent.
Generates an ordered, dependency-checked plan of analysis steps based
on the experiment context. The agent uses this to suggest next steps,
validate ordering, and track progress.
Usage::
from scagent.context import ExperimentContext
from scagent.dag import AnalysisDAG
ctx = ExperimentContext(Path(".scagent"))
dag = AnalysisDAG.from_context(ctx)
print(dag.summary())
step = dag.next_step()
"""
from __future__ import annotations
import json
from copy import deepcopy
from dataclasses import asdict, dataclass, field
from pathlib import Path
from typing import Any
from scagent.context import ExperimentContext
# ---------------------------------------------------------------------------
# Data model
# ---------------------------------------------------------------------------
@dataclass
class DAGStep:
"""A single step in the analysis DAG."""
id: str
name: str # human-readable
category: str # qc, normalization, clustering, de, trajectory, etc.
tool_id: str | None = None # maps to tool registry; None for meta-steps
depends_on: list[str] = field(default_factory=list)
status: str = "pending" # pending, done, skipped
skip_reason: str | None = None
required: bool = True
conditional: bool = False # True if step is included only when condition is met
# ---------------------------------------------------------------------------
# DAG definitions per paradigm
# ---------------------------------------------------------------------------
def _cell_atlas_steps(ctx: ExperimentContext) -> list[DAGStep]:
"""Cell atlas: single-tissue deep profiling."""
steps = [
DAGStep("load", "Load data", "loading", "load_10x_h5"),
DAGStep("qc_metrics", "QC metrics", "qc", "calculate_qc_metrics", ["load"]),
DAGStep("filter_cells", "Filter cells", "qc", "filter_cells", ["qc_metrics"]),
DAGStep("filter_genes", "Filter genes", "qc", "filter_genes", ["filter_cells"]),
DAGStep("doublet_detection", "Doublet detection", "qc", "scrublet_doublets", ["filter_genes"]),
DAGStep("normalize", "Normalize", "normalization", "log_normalize", ["doublet_detection"]),
DAGStep("hvg", "Highly variable genes", "feature_selection", "highly_variable_genes", ["normalize"]),
DAGStep("pca", "PCA", "dimensionality_reduction", "pca", ["hvg"]),
]
if ctx.needs_batch_correction():
steps.append(DAGStep(
"batch_correction", "Batch correction", "integration", "harmony",
["pca"], conditional=True,
))
neighbor_dep = "batch_correction"
else:
neighbor_dep = "pca"
steps.extend([
DAGStep("neighbors", "Neighbor graph", "neighbors", "neighbor_graph", [neighbor_dep]),
DAGStep("clustering", "Clustering", "clustering", "leiden_clustering", ["neighbors"]),
DAGStep("umap", "UMAP embedding", "embedding", "umap", ["neighbors"]),
DAGStep("markers", "Marker genes", "differential_expression", "wilcoxon_markers", ["clustering"]),
DAGStep("annotation", "Cell type annotation", "annotation", "celltypist_annotation", ["clustering", "markers"]),
])
return steps
def _disease_vs_healthy_steps(ctx: ExperimentContext) -> list[DAGStep]:
"""Disease vs. healthy: cross-condition comparison."""
steps = _cell_atlas_steps(ctx) # shared prefix
# Add DE + enrichment + composition
steps.extend([
DAGStep(
"pseudobulk_de", "Pseudobulk DE", "differential_expression",
"deseq2_pseudobulk", ["annotation"], required=True,
),
DAGStep(
"pathway_enrichment", "Pathway enrichment", "enrichment",
"gsea", ["pseudobulk_de"], required=False,
),
DAGStep(
"composition", "Composition analysis", "composition",
"sccoda", ["annotation"], required=False, conditional=True,
),
])
return steps
def _developmental_trajectory_steps(ctx: ExperimentContext) -> list[DAGStep]:
"""Developmental trajectory: pseudotime / lineage analysis.
Workflow: shared prefix → PAGA topology → DPT pseudotime → (optional) scVelo.
Best-practice refs: [BP-1] pp. 553-554, [BP-2] Ch. 14.
"""
steps = _cell_atlas_steps(ctx) # shared prefix
# Add trajectory-specific steps
# Best-practice workflow: PAGA → Palantir → (optional) scVelo → CellRank
# [BP-2] Ch. 14: Palantir preferred over DPT for pseudotime
# [BP-2] Ch. 15: CellRank preferred over raw velocity stream plots
steps.extend([
DAGStep(
"paga", "PAGA trajectory topology", "trajectory",
"paga", ["clustering"],
),
DAGStep(
"pseudotime", "Palantir pseudotime", "trajectory",
"palantir", ["paga", "annotation"],
),
DAGStep(
"rna_velocity", "RNA velocity", "trajectory",
"scvelo_velocity", ["neighbors"],
required=False, conditional=True,
),
DAGStep(
"fate_mapping", "CellRank fate mapping", "trajectory",
"cellrank", ["rna_velocity"],
required=False, conditional=True,
),
])
return steps
def _perturbation_screen_steps(ctx: ExperimentContext) -> list[DAGStep]:
"""Perturbation screen: CRISPR / Perturb-seq analysis.
Workflow: shared prefix → guide assignment → perturbation DE → enrichment.
Best-practice refs: [BP-1] p. 557, [BP-2] Ch. 20.
"""
steps = _cell_atlas_steps(ctx) # shared prefix
steps.extend([
DAGStep(
"guide_assignment", "Guide assignment", "perturbation",
"guide_assignment", ["annotation"],
),
DAGStep(
"perturbation_de", "Perturbation DE", "differential_expression",
"perturbation_de", ["guide_assignment"],
),
DAGStep(
"perturbation_enrichment", "Perturbation enrichment", "enrichment",
"gsea", ["perturbation_de"], required=False,
),
])
return steps
def _temporal_longitudinal_steps(ctx: ExperimentContext) -> list[DAGStep]:
"""Temporal / longitudinal: time-series cross-condition analysis.
Reuses existing tools: pseudobulk DE for pairwise timepoint contrasts,
composition for proportion trends, trajectory for temporal ordering.
Batch correction is critical — different timepoints = different batches.
Best-practice refs: [BP-1] pp. 552-553, 555, [BP-2] Ch. 18.
"""
steps = [
DAGStep("load", "Load data", "loading", "load_10x_h5"),
DAGStep("qc_metrics", "QC metrics", "qc", "calculate_qc_metrics", ["load"]),
DAGStep("filter_cells", "Filter cells", "qc", "filter_cells", ["qc_metrics"]),
DAGStep("filter_genes", "Filter genes", "qc", "filter_genes", ["filter_cells"]),
DAGStep("doublet_detection", "Doublet detection", "qc", "scrublet_doublets", ["filter_genes"]),
DAGStep("normalize", "Normalize", "normalization", "log_normalize", ["doublet_detection"]),
DAGStep("hvg", "Highly variable genes", "feature_selection", "highly_variable_genes", ["normalize"]),
DAGStep("pca", "PCA", "dimensionality_reduction", "pca", ["hvg"]),
]
# Batch correction is ALWAYS required for temporal data
# (different timepoints = different batches) [BP-1]
steps.append(DAGStep(
"batch_correction", "Batch correction", "integration", "harmony",
["pca"], required=True,
))
steps.extend([
DAGStep("neighbors", "Neighbor graph", "neighbors", "neighbor_graph", ["batch_correction"]),
DAGStep("clustering", "Clustering", "clustering", "leiden_clustering", ["neighbors"]),
DAGStep("umap", "UMAP embedding", "embedding", "umap", ["neighbors"]),
DAGStep("markers", "Marker genes", "differential_expression", "wilcoxon_markers", ["clustering"]),
DAGStep("annotation", "Cell type annotation", "annotation", "celltypist_annotation", ["clustering", "markers"]),
# Temporal-specific analysis steps
DAGStep(
"pseudobulk_de", "Pseudobulk DE (timepoints)", "differential_expression",
"deseq2_pseudobulk", ["annotation"], required=True,
),
DAGStep(
"composition", "Composition over time", "composition",
"sccoda", ["annotation"], required=False,
),
DAGStep(
"pathway_enrichment", "Pathway enrichment", "enrichment",
"gsea", ["pseudobulk_de"], required=False,
),
])
return steps
def _immune_repertoire_steps(ctx: ExperimentContext) -> list[DAGStep]:
"""Immune repertoire: V(D)J / TCR/BCR clonotype analysis.
Workflow: shared GEX prefix → load VDJ → clonotype analysis → overlap.
Best-practice refs: [BP-1] pp. 559-560, [BP-2] Ch. 38-39.
"""
steps = _cell_atlas_steps(ctx) # shared GEX prefix
steps.extend([
DAGStep(
"load_vdj", "Load V(D)J data", "loading",
"load_vdj", ["load"],
),
DAGStep(
"clonotype_analysis", "Clonotype analysis", "repertoire",
"clonotype_analysis", ["load_vdj", "annotation"],
),
DAGStep(
"repertoire_overlap", "Repertoire overlap", "repertoire",
"repertoire_overlap", ["clonotype_analysis"],
required=False,
),
])
return steps
def _multimodal_steps(ctx: ExperimentContext) -> list[DAGStep]:
"""Multimodal (CITE-seq): RNA + surface protein analysis.
Workflow: GEX prefix → load protein → normalize protein → WNN → cluster → annotate.
Best-practice refs: [BP-1] pp. 558-559, [BP-2] Ch. 32-37.
"""
steps = [
DAGStep("load", "Load data", "loading", "load_10x_h5"),
DAGStep("qc_metrics", "QC metrics", "qc", "calculate_qc_metrics", ["load"]),
DAGStep("filter_cells", "Filter cells", "qc", "filter_cells", ["qc_metrics"]),
DAGStep("filter_genes", "Filter genes", "qc", "filter_genes", ["filter_cells"]),
DAGStep("doublet_detection", "Doublet detection", "qc", "scrublet_doublets", ["filter_genes"]),
DAGStep("normalize", "Normalize", "normalization", "log_normalize", ["doublet_detection"]),
DAGStep("hvg", "Highly variable genes", "feature_selection", "highly_variable_genes", ["normalize"]),
DAGStep("pca", "PCA", "dimensionality_reduction", "pca", ["hvg"]),
]
# Protein modality
steps.extend([
DAGStep(
"load_protein", "Load protein data", "loading",
"load_protein", ["load"],
),
DAGStep(
"normalize_protein", "Normalize protein (CLR)", "normalization",
"normalize_protein", ["load_protein"],
),
DAGStep(
"wnn", "Weighted nearest neighbors", "neighbors",
"wnn", ["pca", "normalize_protein"],
),
DAGStep(
"clustering", "Clustering (WNN)", "clustering",
"leiden_clustering", ["wnn"],
),
DAGStep(
"umap", "UMAP embedding", "embedding",
"umap", ["wnn"],
),
DAGStep(
"markers", "Marker genes", "differential_expression",
"wilcoxon_markers", ["clustering"],
),
DAGStep(
"protein_markers", "Protein markers", "differential_expression",
"protein_markers", ["clustering", "normalize_protein"],
),
DAGStep(
"annotation", "Cell type annotation", "annotation",
"celltypist_annotation", ["clustering", "markers", "protein_markers"],
),
])
return steps
_PARADIGM_BUILDERS = {
"cell_atlas": _cell_atlas_steps,
"disease_vs_healthy": _disease_vs_healthy_steps,
"developmental_trajectory": _developmental_trajectory_steps,
"perturbation_screen": _perturbation_screen_steps,
"temporal_longitudinal": _temporal_longitudinal_steps,
"immune_repertoire": _immune_repertoire_steps,
"multimodal": _multimodal_steps,
}
# ---------------------------------------------------------------------------
# AnalysisDAG
# ---------------------------------------------------------------------------
class AnalysisDAG:
"""Paradigm-aware analysis DAG with progress tracking.
Parameters
----------
paradigm
Experiment paradigm (e.g., ``"cell_atlas"``).
steps
Ordered list of :class:`DAGStep` objects.
"""
FILENAME = "dag.json"
def __init__(self, paradigm: str, steps: list[DAGStep]) -> None:
self.paradigm = paradigm
self.steps = steps
self._step_index: dict[str, DAGStep] = {s.id: s for s in steps}
# ------------------------------------------------------------------
# Factory
# ------------------------------------------------------------------
@classmethod
def from_context(cls, ctx: ExperimentContext) -> "AnalysisDAG":
"""Generate a DAG appropriate for the experiment context."""
paradigm = ctx.paradigm
if paradigm is None:
raise ValueError("Cannot generate DAG: paradigm is not set in experiment context")
builder = _PARADIGM_BUILDERS.get(paradigm)
if builder is None:
raise ValueError(
f"No DAG definition for paradigm '{paradigm}'. "
f"Supported: {sorted(_PARADIGM_BUILDERS.keys())}"
)
steps = builder(ctx)
return cls(paradigm, steps)
# ------------------------------------------------------------------
# Navigation
# ------------------------------------------------------------------
def next_step(self) -> DAGStep | None:
"""Return the first pending step whose dependencies are all met."""
for step in self.steps:
if step.status != "pending":
continue
if self._deps_met(step):
return step
return None
def complete_step(self, step_id: str) -> None:
"""Mark a step as done."""
step = self._get(step_id)
step.status = "done"
def skip_step(self, step_id: str, reason: str = "") -> None:
"""Mark a step as skipped."""
step = self._get(step_id)
step.status = "skipped"
step.skip_reason = reason
def is_valid_step(self, step_id: str) -> bool:
"""Check if a step can be executed (all dependencies met)."""
step = self._get(step_id)
return self._deps_met(step)
def get_step(self, step_id: str) -> DAGStep | None:
"""Return a step by ID, or *None*."""
return self._step_index.get(step_id)
# ------------------------------------------------------------------
# Dynamic modification
# ------------------------------------------------------------------
def add_step(
self,
step: DAGStep,
after: str | None = None,
) -> None:
"""Add a step to the DAG dynamically.
Parameters
----------
step
The new step to add.
after
Insert after this step ID. If *None*, appends at the end.
Raises
------
ValueError
If *step.id* already exists or *after* is not found.
"""
if step.id in self._step_index:
raise ValueError(f"Step '{step.id}' already exists in DAG")
if after is not None:
if after not in self._step_index:
raise ValueError(f"Step '{after}' not found in DAG")
idx = next(i for i, s in enumerate(self.steps) if s.id == after)
self.steps.insert(idx + 1, step)
else:
self.steps.append(step)
self._step_index[step.id] = step
def mark_precomputed(self, step_id: str) -> None:
"""Mark a step as already done (e.g., detected by inspector).
Silently ignores unknown step IDs so callers can mark steps
that may or may not exist in this particular DAG.
"""
step = self._step_index.get(step_id)
if step is not None:
step.status = "done"
def mark_precomputed_from_state(self, state) -> int:
"""Mark DAG steps as done based on an :class:`AnnDataState`.
Parameters
----------
state
Inspector output (:class:`~scagent.inspector.AnnDataState`).
Returns
-------
Number of steps marked as precomputed.
"""
# Map state flags → DAG step IDs
_FLAG_TO_STEPS = {
"has_qc_metrics": ["load", "qc_metrics", "filter_cells", "filter_genes"],
"has_normalized": ["normalize"],
"has_hvg": ["hvg"],
"has_pca": ["pca"],
"has_neighbors": ["neighbors"],
"has_clusters": ["clustering"],
"has_umap": ["umap"],
"has_cell_types": ["annotation"],
"has_de_results": ["markers"],
}
count = 0
for flag, step_ids in _FLAG_TO_STEPS.items():
if getattr(state, flag, False):
for sid in step_ids:
step = self._step_index.get(sid)
if step is not None and step.status == "pending":
step.status = "done"
count += 1
return count
@property
def done_steps(self) -> list[DAGStep]:
return [s for s in self.steps if s.status == "done"]
@property
def pending_steps(self) -> list[DAGStep]:
return [s for s in self.steps if s.status == "pending"]
@property
def progress(self) -> tuple[int, int]:
"""Return (done_count, total_count)."""
total = sum(1 for s in self.steps if s.status != "skipped")
done = sum(1 for s in self.steps if s.status == "done")
return done, total
# ------------------------------------------------------------------
# Summary
# ------------------------------------------------------------------
def summary(self) -> str:
"""Human-readable Markdown progress table."""
done, total = self.progress
lines = [
f"## Analysis Plan — {self.paradigm} ({done}/{total} steps done)\n",
"| # | Step | Category | Status |",
"|---|------|----------|--------|",
]
for i, s in enumerate(self.steps):
status_icon = {"done": "✅", "skipped": "⏭️", "pending": "⬜"}[s.status]
extra = ""
if s.conditional:
extra = " _(conditional)_"
if s.skip_reason:
extra += f" — {s.skip_reason}"
lines.append(f"| {i} | {s.name} | {s.category} | {status_icon} {s.status}{extra} |")
nxt = self.next_step()
if nxt:
lines.append(f"\n**Next step:** {nxt.name} (`{nxt.id}`)")
return "\n".join(lines)
# ------------------------------------------------------------------
# Persistence
# ------------------------------------------------------------------
def save(self, project_dir: Path | str) -> Path:
"""Write the DAG to ``.scagent/dag.json``."""
p = Path(project_dir)
p.mkdir(parents=True, exist_ok=True)
path = p / self.FILENAME
data = {
"paradigm": self.paradigm,
"steps": [asdict(s) for s in self.steps],
}
path.write_text(json.dumps(data, indent=2), encoding="utf-8")
return path
@classmethod
def load(cls, project_dir: Path | str) -> "AnalysisDAG":
"""Load a DAG from ``.scagent/dag.json``."""
p = Path(project_dir) / cls.FILENAME
data = json.loads(p.read_text(encoding="utf-8"))
steps = [DAGStep(**s) for s in data["steps"]]
return cls(data["paradigm"], steps)
# ------------------------------------------------------------------
# Private
# ------------------------------------------------------------------
def _get(self, step_id: str) -> DAGStep:
step = self._step_index.get(step_id)
if step is None:
raise ValueError(f"No step with id '{step_id}' in DAG")
return step
def _deps_met(self, step: DAGStep) -> bool:
"""Check if all dependencies are done or skipped."""
for dep_id in step.depends_on:
dep = self._step_index.get(dep_id)
if dep is None:
return False
if dep.status not in ("done", "skipped"):
return False
return True