# sulkysubject37/qwd architecture

> Architecture diagram of the GitHub repository sulkysubject37/qwd, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sulkysubject37/qwd
- Repository: https://github.com/sulkysubject37/qwd

## Overview

1. **Repository shape**
   QwD is organized around a single Zig analysis engine, with multiple frontends layered on top: a command-line app, Python and R bindings, and a macOS SwiftUI dashboard. The engine is the source of truth; the other components are thin adapters around it.

2. **Core engine boundary**
   The `core/` tree contains the reusable runtime: parsing, decompression, scheduling, pipeline orchestration, SIMD helpers, memory management, metrics, and structured output. This is the architectural center of the repo and is what all consumers ultimately depend on.

3. **Input formats and ingestion**
   Input support is centered on FASTQ and BAM, with BGZF/GZIP handling for compressed streams. The `io/` and `core/io/` modules provide readers and decompression primitives, while `core/parser/`, `core/bam`, and `io/fastq` handle format-specific decoding. The data flow starts with file/stream readers and moves into normalized read records for downstream analysis.

4. **Decompression and streaming pipeline**
   A major design point is streaming decompression overlapped with analysis. `core/io/` includes native BGZF/GZIP and fallback deflate implementations, plus libdeflate-backed paths where available. This is a performance boundary: compressed bytes are turned into records as a streaming pipeline rather than via full-file materialization.

5. **Vectorized data representation**
   The engine emphasizes SIMD and columnar processing. `core/columnar/`, `core/vector/`, `core/simd/`, and `core/encoding/` implement the internal representations and transforms used to make read-level analytics fast. The architecture is designed so many QC operations can run on compact bitplanes or columnar buffers instead of per-base scalar loops.

6. **Pipeline, scheduler, and stages**
   `core/pipeline/`, `core/scheduler/`, `core/stage/`, and `core/parallel/` define the execution model, while `stages/` contains the actual analytics modules. Each stage corresponds to a specific QC or statistics task such as adapter detection, GC distribution, k-mer spectrum, duplication, quality decay, N statistics, trimming, and taxonomic screening. The pipeline assembles these stages into ordered work units, and the scheduler distributes them across threads.

7. **Analytics and reporting**
   Results are accumulated through `core/metrics/`, `core/api/metrics_stream.zig`, and `core/output/structured_output.zig`. This suggests a streaming metrics model rather than a batch-only report writer. The output boundary is important because the same structured results are consumed by the CLI, bindings, and dashboard.

8. **CLI frontend**
   `apps/cli/main.zig` is the minimal native entrypoint for users who want command-line execution. It likely wires arguments into the core pipeline configuration and prints or emits the structured report. The CLI is the simplest consumer and serves as the operational baseline for the engine.

9. **Language bindings**
   The `bindings/` tree exposes the same core engine to Python and R, with a Zig C API layer under `bindings/c/`. The Python package (`bindings/python/`) and R package (`bindings/r/`) are wrappers over that C-facing interface, so the binding boundary is intentionally narrow: native engine on one side, language-specific ergonomics on the other.

10. **macOS dashboard architecture**
   `apps/dashboard/` is a separate SwiftUI application that embeds the native engine via a C shim and XCFramework (`QwD.xcframework`). The SwiftUI views (`DashboardView`, `ReportView`, `SettingsView`, `QualityHeatmapView`, `KmerSpectrumView`, `TaxonomyProfileView`) present analysis results and expose interactive controls. The README indicates this dashboard is not just a viewer; it can inject analysis parameters such as trimming and filtering into the engine.

11. **Build and packaging boundaries**
   The repo uses Zig’s build system at the top level (`build.zig`), plus a Swift Package/Xcode project for the dashboard. The dashboard also has scripts to build the XCFramework and wrap the native library for Apple platforms. This means the project has two main packaging surfaces: native Zig artifacts and Apple app artifacts.

12. **Validation, performance, and reproducibility**
   `tests/` is broad and mirrors the architecture: fastq, bam, pipeline, stages, bindings, fuzz, stress, reproducibility, and interface tests. `benchmarks/` measures columnar, SIMD, decomposition, and parallel scaling behavior. Together these form the nonfunctional boundary of the system: correctness, cross-language compatibility, and performance are all treated as first-class concerns.

13. **Auxiliary tooling**
   `tools/` and `scripts/` provide generation and debugging helpers such as synthetic FASTQ generation, stress data generation, gzip debugging, and BGZF tooling. These are not runtime dependencies, but they support test data creation and performance exploration around the core engine.

14. **Overall data flow**
   The main flow is: input file or stream → format reader/decompressor → parsed reads → columnar/SIMD-friendly internal structures → scheduled QC/analysis stages → structured metrics/reporting → consumed by CLI, Python/R bindings, or the SwiftUI dashboard. The important architectural boundary is that all presentation and language-specific layers sit on top of the same Zig engine.

Main entry points: [`core/pipeline/pipeline.zig`](https://github.com/sulkysubject37/qwd/blob/main/core/pipeline/pipeline.zig) (Pipeline), [`core/output/structured_output.zig`](https://github.com/sulkysubject37/qwd/blob/main/core/output/structured_output.zig) (Structured output), [`core/parser/parser.zig`](https://github.com/sulkysubject37/qwd/blob/main/core/parser/parser.zig) (Parser), [`core/io/gzip_reader.zig`](https://github.com/sulkysubject37/qwd/blob/main/core/io/gzip_reader.zig) (IO core)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core_engine["Core engine"]
  node_pipeline["Pipeline<br/>orchestration<br/>[pipeline.zig]"]
  node_scheduler["Scheduler<br/>thread scheduling<br/>[scheduler.zig]"]
  node_parallel["Parallel runtime<br/>concurrency primitives"]
  node_parser["Parser<br/>record parsing<br/>[parser.zig]"]
  node_columnar["Columnar<br/>data representation<br/>[column_builder.zig]"]
  node_simd["SIMD<br/>vectorized kernels<br/>[simd_ops.zig]"]
  node_metrics["Metrics<br/>streaming stats<br/>[metrics.zig]"]
  node_output["Structured output<br/>report format"]
end

subgraph group_frontends["Frontends"]
  node_cli_app["CLI<br/>native entrypoint<br/>[main.zig]"]
  node_dashboard_app["Dashboard<br/>SwiftUI app"]
  node_dashboard_bridge["C shim<br/>native bridge<br/>[qwd.c]"]
  node_bindings_c["C API<br/>ffi layer<br/>[qwd_api.zig]"]
  node_bindings_python["Python<br/>package wrapper<br/>[qwd.py]"]
  node_bindings_r["R<br/>[qwd.R]"]
end

subgraph group_analysis["Analysis stages"]
  node_qc_stages["QC stages<br/>analysis modules<br/>[qc_stage.zig]"]
  node_alignment_stages["Alignment stages<br/>analysis modules"]
  node_kmer_stage["K-mer stage<br/>analysis module<br/>[kmer_stage.zig]"]
  node_gc_stage["GC stage<br/>analysis module<br/>[gc_stage.zig]"]
  node_trim_stage["Trim stage<br/>analysis module<br/>[trim_stage.zig]"]
end

subgraph group_inputs["Input handling"]
  node_io_core["IO core<br/>stream decompression<br/>[gzip_reader.zig]"]
  node_fastq_io["FASTQ reader<br/>format reader<br/>[fastq_reader.zig]"]
  node_bam_io["BAM reader<br/>format reader<br/>[bam_reader.zig]"]
end

node_cli_app -->|"configures"| node_pipeline
node_dashboard_app -->|"calls"| node_dashboard_bridge
node_dashboard_bridge -->|"wraps"| node_bindings_c
node_bindings_python -->|"uses"| node_bindings_c
node_bindings_r -->|"uses"| node_bindings_c
node_pipeline -->|"dispatches"| node_scheduler
node_scheduler -->|"runs on"| node_parallel
node_pipeline -->|"feeds"| node_parser
node_io_core -->|"reads"| node_fastq_io
node_io_core -->|"reads"| node_bam_io
node_io_core -->|"normalizes"| node_parser
node_fastq_io -->|"emits"| node_parser
node_bam_io -->|"emits"| node_parser
node_parser -->|"packs"| node_columnar
node_columnar -->|"accelerates"| node_simd
node_simd -->|"feeds"| node_qc_stages
node_pipeline -->|"assembles"| node_qc_stages
node_pipeline -->|"assembles"| node_alignment_stages
node_pipeline -->|"assembles"| node_kmer_stage
node_pipeline -->|"assembles"| node_gc_stage
node_pipeline -->|"assembles"| node_trim_stage
node_qc_stages -->|"records"| node_metrics
node_alignment_stages -->|"records"| node_metrics
node_metrics -->|"serializes"| node_output
node_output -->|"renders"| node_cli_app
node_output -.->|"consumed by"| node_bindings_python
node_output -.->|"consumed by"| node_bindings_r
node_output -->|"renders"| node_dashboard_app

click node_cli_app "https://github.com/sulkysubject37/qwd/blob/main/apps/cli/main.zig"
click node_dashboard_app "https://github.com/sulkysubject37/qwd/blob/main/apps/dashboard/Sources/QwDDashboard/QwDDashboardApp.swift"
click node_dashboard_bridge "https://github.com/sulkysubject37/qwd/blob/main/apps/dashboard/Sources/CQwD/qwd.c"
click node_bindings_c "https://github.com/sulkysubject37/qwd/blob/main/bindings/c/qwd_api.zig"
click node_bindings_python "https://github.com/sulkysubject37/qwd/blob/main/bindings/python/qwd.py"
click node_bindings_r "https://github.com/sulkysubject37/qwd/blob/main/bindings/r/R/qwd.R"
click node_pipeline "https://github.com/sulkysubject37/qwd/blob/main/core/pipeline/pipeline.zig"
click node_scheduler "https://github.com/sulkysubject37/qwd/blob/main/core/scheduler/scheduler.zig"
click node_parallel "https://github.com/sulkysubject37/qwd/blob/main/core/parallel/parallel_scheduler.zig"
click node_io_core "https://github.com/sulkysubject37/qwd/blob/main/core/io/gzip_reader.zig"
click node_fastq_io "https://github.com/sulkysubject37/qwd/blob/main/io/fastq/fastq_reader.zig"
click node_bam_io "https://github.com/sulkysubject37/qwd/blob/main/io/bam/bam_reader.zig"
click node_parser "https://github.com/sulkysubject37/qwd/blob/main/core/parser/parser.zig"
click node_columnar "https://github.com/sulkysubject37/qwd/blob/main/core/columnar/column_builder.zig"
click node_simd "https://github.com/sulkysubject37/qwd/blob/main/core/simd/simd_ops.zig"
click node_metrics "https://github.com/sulkysubject37/qwd/blob/main/core/metrics/metrics.zig"
click node_output "https://github.com/sulkysubject37/qwd/blob/main/core/output/structured_output.zig"
click node_qc_stages "https://github.com/sulkysubject37/qwd/blob/main/stages/qc/qc_stage.zig"
click node_alignment_stages "https://github.com/sulkysubject37/qwd/blob/main/stages/alignment/alignment_stats_stage.zig"
click node_kmer_stage "https://github.com/sulkysubject37/qwd/blob/main/stages/kmer/kmer_stage.zig"
click node_gc_stage "https://github.com/sulkysubject37/qwd/blob/main/stages/gc/gc_stage.zig"
click node_trim_stage "https://github.com/sulkysubject37/qwd/blob/main/stages/trim/trim_stage.zig"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pipeline,node_scheduler,node_parallel,node_parser,node_columnar,node_simd,node_metrics,node_output toneBlue
class node_cli_app,node_dashboard_app,node_dashboard_bridge,node_bindings_c,node_bindings_python,node_bindings_r toneAmber
class node_qc_stages,node_alignment_stages,node_kmer_stage,node_gc_stage,node_trim_stage toneMint
class node_io_core,node_fastq_io,node_bam_io toneRose
```

## Components

### Core engine

Reusable Zig runtime and analysis pipeline

- [Pipeline](https://github.com/sulkysubject37/qwd/blob/main/core/pipeline/pipeline.zig): orchestration · Builds ordered analysis work units and execution flow · `core/pipeline/pipeline.zig`
- [Scheduler](https://github.com/sulkysubject37/qwd/blob/main/core/scheduler/scheduler.zig): thread scheduling · Distributes pipeline work across threads · `core/scheduler/scheduler.zig`
- [Parallel runtime](https://github.com/sulkysubject37/qwd/blob/main/core/parallel/parallel_scheduler.zig): concurrency primitives · Parallel execution helpers and buffers · `core/parallel/parallel_scheduler.zig`
- [Parser](https://github.com/sulkysubject37/qwd/blob/main/core/parser/parser.zig): record parsing · Normalized read and record parsing boundary · `core/parser/parser.zig`
- [Columnar](https://github.com/sulkysubject37/qwd/blob/main/core/columnar/column_builder.zig): data representation · Compact read-level buffers and bitplane structures · `core/columnar/column_builder.zig`
- [SIMD](https://github.com/sulkysubject37/qwd/blob/main/core/simd/simd_ops.zig): vectorized kernels · Vectorized decode, scan, and transpose operations · `core/simd/simd_ops.zig`
- [Metrics](https://github.com/sulkysubject37/qwd/blob/main/core/metrics/metrics.zig): streaming stats · Runtime metrics and streaming metric accumulation · `core/metrics/metrics.zig`
- [Structured output](https://github.com/sulkysubject37/qwd/blob/main/core/output/structured_output.zig): report format · Structured report emission consumed by all frontends · `core/output/structured_output.zig`

### Frontends

CLI, language bindings, and dashboard consumers

- [CLI](https://github.com/sulkysubject37/qwd/blob/main/apps/cli/main.zig): native entrypoint · Command-line app that wires arguments into the engine · `apps/cli/main.zig`
- [Dashboard](https://github.com/sulkysubject37/qwd/blob/main/apps/dashboard/Sources/QwDDashboard/QwDDashboardApp.swift): SwiftUI app · macOS UI that embeds the native engine and renders reports · `apps/dashboard/Sources/QwDDashboard/QwDDashboardApp.swift`
- [C shim](https://github.com/sulkysubject37/qwd/blob/main/apps/dashboard/Sources/CQwD/qwd.c): native bridge · Swift-facing C wrapper and packaged XCFramework boundary · `apps/dashboard/Sources/CQwD/qwd.c`
- [C API](https://github.com/sulkysubject37/qwd/blob/main/bindings/c/qwd_api.zig): ffi layer · Stable C-facing API exported from Zig · `bindings/c/qwd_api.zig`
- [Python](https://github.com/sulkysubject37/qwd/blob/main/bindings/python/qwd.py): package wrapper · Python package built on the C API · `bindings/python/qwd.py`
- [R](https://github.com/sulkysubject37/qwd/blob/main/bindings/r/R/qwd.R): package wrapper · R package built on the C API · `bindings/r/R/qwd.R`

### Analysis stages

Domain analytics assembled into pipeline stages

- [QC stages](https://github.com/sulkysubject37/qwd/blob/main/stages/qc/qc_stage.zig): analysis modules · Primary quality-control and statistics stages · `stages/qc/qc_stage.zig`
- [Alignment stages](https://github.com/sulkysubject37/qwd/blob/main/stages/alignment/alignment_stats_stage.zig): analysis modules · Alignment-derived analytics such as coverage and insert size · `stages/alignment/alignment_stats_stage.zig`
- [K-mer stage](https://github.com/sulkysubject37/qwd/blob/main/stages/kmer/kmer_stage.zig): analysis module · K-mer spectrum and related sequence composition analytics · `stages/kmer/kmer_stage.zig`
- [GC stage](https://github.com/sulkysubject37/qwd/blob/main/stages/gc/gc_stage.zig): analysis module · GC distribution analysis · `stages/gc/gc_stage.zig`
- [Trim stage](https://github.com/sulkysubject37/qwd/blob/main/stages/trim/trim_stage.zig): analysis module · Trimming and filtering analysis/control stage · `stages/trim/trim_stage.zig`

### Input handling

File readers and compressed stream ingestion

- [IO core](https://github.com/sulkysubject37/qwd/blob/main/core/io/gzip_reader.zig): stream decompression · BGZF/GZIP readers and deflate implementations · `core/io/gzip_reader.zig`
- [FASTQ reader](https://github.com/sulkysubject37/qwd/blob/main/io/fastq/fastq_reader.zig): format reader · FASTQ ingestion and record decoding · `io/fastq/fastq_reader.zig`
- [BAM reader](https://github.com/sulkysubject37/qwd/blob/main/io/bam/bam_reader.zig): format reader · BAM ingestion and alignment-aware decoding · `io/bam/bam_reader.zig`

## Connections

- CLI → Pipeline: configures · CLI translates arguments into pipeline execution
- Dashboard → C shim: calls · SwiftUI app uses the C shim for native execution
- C shim → C API: wraps · Dashboard bridge sits on the same native API boundary
- Python → C API: uses · Python package delegates to the C API
- R → C API: uses · R package delegates to the C API
- Pipeline → Scheduler: dispatches · Pipeline hands work to the scheduler
- Scheduler → Parallel runtime: runs on · Scheduler uses shared parallel runtime primitives
- Pipeline → Parser: feeds · Pipeline receives parsed records
- IO core → FASTQ reader: reads · Core IO supports FASTQ streams
- IO core → BAM reader: reads · Core IO supports BAM streams
- IO core → Parser: normalizes · Compressed input is turned into parseable records
- FASTQ reader → Parser: emits · FASTQ reader produces normalized records
- BAM reader → Parser: emits · BAM reader produces normalized records
- Parser → Columnar: packs · Parsed reads are packed into compact internal buffers
- Columnar → SIMD: accelerates · Columnar layout is optimized for SIMD kernels
- SIMD → QC stages: feeds · Vectorized primitives support core QC work
- Pipeline → QC stages: assembles · Pipeline composes QC stages into ordered work
- Pipeline → Alignment stages: assembles · Pipeline composes alignment analytics when BAM is present
- Pipeline → K-mer stage: assembles · Pipeline may include k-mer analysis
- Pipeline → GC stage: assembles · Pipeline may include GC analysis
- Pipeline → Trim stage: assembles · Pipeline may include trimming and filtering stages
- QC stages → Metrics: records · Stages stream results into metrics collection
- Alignment stages → Metrics: records · Alignment analysis contributes metrics
- Metrics → Structured output: serializes · Structured output is built from streamed metrics
- Structured output → CLI: renders · CLI prints or emits structured reports
- Structured output → Python: consumed by · Bindings expose structured results to Python
- Structured output → R: consumed by · Bindings expose structured results to R
- Structured output → Dashboard: renders · Dashboard displays structured analysis results
