|
| 1 | +# PATTERNS.md — Codebase Conventions and Run Patterns |
| 2 | + |
| 3 | +## Pipeline Architecture Patterns |
| 4 | + |
| 5 | +### 1. Branch-and-Merge Input Strategy |
| 6 | + |
| 7 | +The workflow uses `.branch {}` to split input into API vs external paths, then merges all JSON outputs via `.mix().collect()` before the benchmark pipeline. This pattern allows mixed-source runs in a single CSV. |
| 8 | + |
| 9 | +``` |
| 10 | +ids.branch { api: ...; external: ... } |
| 11 | + → separate processing paths |
| 12 | + → ch_api_jsons.mix(ch_tarball_jsons).mix(ch_external_dir_jsons).collect() |
| 13 | +``` |
| 14 | + |
| 15 | +### 2. In-Process API Fetch (No Task) |
| 16 | + |
| 17 | +`SeqeraApi.fetchRunData()` runs inside the Nextflow process (Groovy), NOT as a Nextflow task. This means: |
| 18 | + |
| 19 | +- No container, no retry directive, no task monitoring |
| 20 | +- Retry logic is manually coded with exponential backoff (3 attempts) |
| 21 | +- Token is read from environment at execution time |
| 22 | +- Results are written to temp files and emitted as channel values |
| 23 | + |
| 24 | +### 3. Three-Stage Python Pipeline |
| 25 | + |
| 26 | +The benchmark report follows a strict data pipeline: |
| 27 | + |
| 28 | +1. **Normalize** (raw JSON → JSONL bundle) — streaming, handles CUR parquet |
| 29 | +2. **Aggregate** (JSONL → report_data.json) — statistical rollups |
| 30 | +3. **Render** (report_data.json → HTML) — Jinja2 template + ECharts |
| 31 | + |
| 32 | +Each stage is a separate Nextflow process with the same container image. The boundary between stages is explicit (JSONL files, then JSON), making debugging straightforward. |
| 33 | + |
| 34 | +### 4. External Test Fixtures |
| 35 | + |
| 36 | +Tests use pre-exported tarball fixtures (`workflows/nf_aggregate/assets/log_dirs/`, referenced from `test_benchmark.csv`) rather than live API calls. This enables fully offline CI. |
| 37 | + |
| 38 | +## Coding Conventions |
| 39 | + |
| 40 | +### Nextflow |
| 41 | + |
| 42 | +- **DSL2** with `include {}` for all modules |
| 43 | +- **No strict syntax yet** — uses `Channel.empty()`, implicit closures in places |
| 44 | +- Processes use `conda` + `container` directives (Wave-compatible) |
| 45 | +- All processes emit `versions.yml` for software version tracking |
| 46 | +- `publishDir` configured per-process in `workflows/nf_aggregate/nextflow.config` |
| 47 | + |
| 48 | +### Python (bin/) |
| 49 | + |
| 50 | +- **Typer CLI** with subcommands for each pipeline stage |
| 51 | +- Modular: `benchmark_report.py` is a thin dispatcher; logic lives in `benchmark_report_normalize.py`, `benchmark_report_aggregate.py`, `benchmark_report_render.py`, `benchmark_report_fetch.py` |
| 52 | +- **JSONL** as intermediate format (streaming-friendly, Fusion-compatible) |
| 53 | +- **Jinja2** for HTML templating with ECharts for charts |
| 54 | +- **PyArrow** for CUR parquet reading (batched streaming for memory efficiency) |
| 55 | +- Test runner: `pytest` with tests colocated under each module's `tests/` directory |
| 56 | + |
| 57 | +### Testing |
| 58 | + |
| 59 | +- **nf-test** for pipeline integration (4 test suites: benchmark-tarball, benchmark-directory, api-only, mixed-no-benchmark) |
| 60 | +- **pytest** for Python unit tests (normalize, aggregate, render, fetch) |
| 61 | +- **[nft-utils@0.0.4](mailto:nft-utils@0.0.4)** plugin for snapshot assertions |
| 62 | +- Test config: `tests/nextflow.config` (intentionally empty — relies on profile `test`) |
| 63 | +- Snapshots verify task counts, output file lists, and software versions |
| 64 | + |
| 65 | +### Configuration |
| 66 | + |
| 67 | +- Container registry: `quay.io` (default for all runtimes) |
| 68 | +- Docker runs as current user: `-u $(id -u):$(id -g)` |
| 69 | +- Process defaults: 1 CPU, 6 GB memory, 4h time |
| 70 | +- Error strategy: retry on exit codes 130–145 and 104 (OOM/signal kills) |
| 71 | + |
| 72 | +## Run Patterns (from Platform observations) |
| 73 | + |
| 74 | +### Scheduling Overhead |
| 75 | + |
| 76 | +Both observed runs show consistent 4–8 minute submit→start latency. This is a characteristic of AWS Batch with spot instances: |
| 77 | + |
| 78 | +- Instance provisioning |
| 79 | +- Container image pull (Wave-built images) |
| 80 | +- S3 staging of inputs |
| 81 | + |
| 82 | +### Instance Type Selection |
| 83 | + |
| 84 | +AWS Batch auto-selects instance types based on resource requests: |
| 85 | + |
| 86 | +- 1 CPU / 6 GB → m5d.large or c5d.large |
| 87 | +- 2 CPU / 2 GB → c5d.large (compute-optimised) |
| 88 | +- All spot pricing |
| 89 | + |
| 90 | +### Cost Efficiency |
| 91 | + |
| 92 | +Early QC tasks cost $0.001–$0.007 each. The dominant cost will come from STAR genome generation and alignment tasks (not yet observed in these in-progress runs). |
| 93 | + |
| 94 | +## File Naming Conventions |
| 95 | + |
| 96 | +| Pattern | Convention | |
| 97 | +| ---------------------- | ------------------------------------------- | |
| 98 | +| Modules | `modules/local/<name>/main.nf` | |
| 99 | +| Module tests (Python) | `modules/local/<name>/tests/test_<name>.py` | |
| 100 | +| Module docs | `modules/local/<name>/AGENTS.md` | |
| 101 | +| Pipeline tests | `tests/<scenario>/main.nf.test` | |
| 102 | +| Pipeline test fixtures | `workflows/nf_aggregate/assets/` | |
| 103 | +| Bin scripts | `bin/benchmark_report_<stage>.py` | |
| 104 | +| Agent docs | `AGENTS.md` at each directory level | |
| 105 | + |
| 106 | +## Git Workflow |
| 107 | + |
| 108 | +- Feature branches named `<user>/<description>` |
| 109 | +- Conventional commits: `feat()`, `fix()`, `test()`, `refactor()`, `docs()`, `perf()` |
| 110 | +- Commit signing required (GPG via SSH/1Password) |
| 111 | +- Pre-commit hooks for linting |
0 commit comments