# sctk Architecture

## Goals

1. Keep the package importable in a normal Python single-cell environment.
2. Isolate R/Bioconductor tools behind an explicit backend.
3. Use stable, shared argument names across preprocessing, tools, and plotting.
4. Prefer AnnData/scverse conventions over custom side effects.
5. Make each module small enough to test and replace independently.

## Package Layout

```text
src/sctk/
  pp/          preprocessing: QC, normalization, clustering, integration
  tl/          analysis tools: pseudobulk, enrichment, expression summaries
  pl/          plotting helpers; no heavy imports at package import time
  r/           optional R bridge and R-backed methods
  _core/       shared validation, matrix access, optional dependency helpers
```

## API Conventions

- Use `adata` for `AnnData`.
- Use `layer=None` to mean `.X`; use `"counts"` for raw-count layers.
- Use `groupby` for generic observation grouping.
- Use `sample_key`, `condition_key`, `batch_key`, and `cell_type_key` for common
  biological metadata.
- Use `key_added` for new `.obs`, `.var`, `.obsm`, `.obsp`, or `.uns` fields.
- Use `copy=False` for functions that can either mutate or return a copy.
- Never overwrite `.X` temporarily when a `layer` argument can be passed through.

## Optional Dependencies

Optional packages are imported inside the functions that need them. The package
root should not import `rpy2`, `decoupler`, `pydeseq2`, `matplotlib`, or
Bioconductor-backed code. This keeps environment problems local and debuggable.

Recommended extras:

- `sctk[enrichment]`: GSEApy enrichment plus decoupler-backed resources
- `sctk[r]`: R bridge via rpy2/anndata2ri
- `sctk[de]`: Python DESeq2 through pydeseq2
- `sctk[plot]`: plotting helpers
- `sctk[integration]`: optional Harmony and scVI integration backends
- `sctk[ccc]`: LIANA consensus cell-cell communication

Installation is intentionally simple:

- `environment.yml`: preferred conda/mamba environment for Python + R + Jupyter
- `requirements.txt`: pip fallback for Python-only installs
- `requirements-r.R`: fallback R/Bioconductor install script outside conda
- `requirements-dev.txt`: pip fallback for tests and packaging tools

## Migration Order

1. Move generic utilities into `_core`.
2. Rebuild `pp` functions with consistent layer and key handling.
3. Replace old decoupler calls with `dc.mt.*` and `dc.pp.*`.
4. Move R-backed methods into `sctk.r` and fail with actionable errors when R
   packages are missing.
5. Port plotting after data-producing functions stabilize.
6. Extract non-single-cell modules, especially survival analysis, to a separate
   package or notebook utilities folder.
