Bazel rules for reproducible bioinformatics pipelines.
⚠️ Early development. APIs will change.
A typical sequencing analysis is a chain of command-line tools: raw reads come off the instrument as FASTQ, get quality-checked and trimmed, then flow into classification, alignment, or assembly, and finally into per-sample summary tables. Each step reads the previous step's files and writes new ones.
Most pipelines wire these steps together with shell scripts or a workflow manager and rely on convention to keep inputs, tool versions, and outputs in sync. That breaks down in familiar ways: a reference database gets silently updated, one sample is re-run with a different trimmer version, or a tweak to a late step forces every sample to be re-processed from scratch.
Bazel treats the pipeline as a build graph instead. Every step is an action with declared inputs and outputs, and the whole graph is hashed.
- Reproducible: every tool and reference is pinned by hash. Re-running a year later gives byte-identical results.
- Incremental: change one step and only that step, and the steps downstream of it, re-run. Fix a bug in the abundance table and the classification results are reused as-is.
- Cached: share results across a team with a remote cache like
NativeLink. If a colleague has already run QC on
sample1.fastq, your build fetches the report instead of recomputing it. - Hermetic: actions see only their declared inputs, so an output cannot depend on a file that is not in the graph.
The first pipeline in rules_bio identifies which bacteria are present in
a sample, and in what proportions, from DNA sequencing reads. This is the
core question in microbiome studies: what lives in this gut, soil, water
or clinical swab, and how does that community differ between conditions?
Sequencing every genome in a mixed bacterial community is expensive and hard to interpret. Instead, most studies sequence a single marker gene that every bacterium carries: the 16S ribosomal RNA gene. It is about 1,500 base pairs long and has two useful properties.
- Conserved regions are nearly identical across all bacteria. PCR primers designed against them amplify the gene from every species in the sample at once, without knowing in advance what is there.
- Hypervariable regions between them (named V1 through V9) differ between species. Reading those regions and matching them against a reference database of known 16S sequences tells you which bacterium each read came from.
A typical run amplifies one or two hypervariable regions, for example V3-V4 on Illumina or the full gene on Nanopore, and sequences the resulting fragments. The output is a FASTQ file per sample containing tens of thousands to millions of reads, each a noisy snapshot of one bacterium's 16S gene.
The pipeline turns those FASTQ files into a table of species counts per sample in four steps.
| Step | Rule | What happens | Why it matters |
|---|---|---|---|
| 1. Quality control | fastq_qc |
Count reads and bases, compute mean quality and GC content | Catches failed runs and contaminated samples before any compute is spent on them |
| 2. Trimming | trim_reads |
Remove PCR primer sequences and low-quality bases from read ends | Primer bases are synthetic and would mismatch every reference. Low-quality tails cause false species calls |
| 3. Classification | classify_16s |
Align each read to a 16S reference database such as SILVA using minimap2 and assign it a taxon | This is the identification step. A read that matches a reference at high identity is called as that species or genus |
| 4. Aggregation | abundance_table |
Count reads per taxon for every sample and assemble a samples-by-species matrix | Downstream statistics, plots and comparisons all start from this table |
The result is the standard input for ecological analysis: alpha diversity within a sample, beta diversity between samples, and differential abundance between groups.
- The reference database is the single largest source of
irreproducibility in 16S studies. Pinning it by hash as a
TaxonomyDbInfodependency means two labs running the same BUILD file get the same species calls. - Classification is the expensive step. With a remote cache, adding a new sample to a study re-runs classification only for that sample.
FastqInfocarries the sequencing platform, so trimming and classification can pick parameters suited to short accurate Illumina reads or long error-prone Nanopore reads without the user repeating that choice at every step.
The repository is organised in four layers. Lower layers know nothing about the ones above them.
flowchart TB
subgraph L1["1. Foundations"]
direction LR
tc["Toolchains<br/>pinned minimap2, seqkit, python"]
data["Data fetching<br/>SRA reads + 16S database, pinned by hash"]
test["Test helpers<br/>golden_output_test, tiny mock data"]
end
subgraph L2["2. Providers (envelopes between rules)"]
direction LR
fi["FastqInfo<br/>reads, sample name, platform"]
ci["ClassificationInfo<br/>per-read taxa"]
ti["TaxonomyDbInfo<br/>reference database"]
end
subgraph L3["3. Analysis rules"]
qc["fastq_qc ✅ done"] --> trim["trim_reads<br/>remove primers + low quality"]
trim --> cls["classify_16s<br/>reads → species per read"]
cls --> abund["abundance_table<br/>samples × species counts"]
end
subgraph L4["4. Pipelines (user-facing macros)"]
pipe["microbiome_16s_pipeline(name, samples, db)"]
end
L1 --> L2
L2 --> L3
L3 --> L4
| Layer | Contents | Purpose |
|---|---|---|
| Foundations | Toolchains for minimap2, seqkit and Python; repository rules that fetch SRA reads and a 16S reference database pinned by hash; golden_output_test and tiny mock FASTQ data |
Everything a rule needs to run hermetically and be tested |
| Providers | FastqInfo, ClassificationInfo, TaxonomyDbInfo |
Typed envelopes that carry files plus metadata between rules, so a rule declares what it accepts rather than guessing from filenames |
| Analysis rules | fastq_qc, trim_reads, classify_16s, abundance_table |
One rule per pipeline step. Each consumes providers from the layer below and emits new ones |
| Pipelines | microbiome_16s_pipeline |
Macros that wire the analysis rules together for a common study design, so most users never write a rule-by-rule BUILD file |
| Rule | What it does | Status |
|---|---|---|
fastq_qc |
Read count, bases, mean quality, GC% for a FASTQ | merged (#1) |
FastqInfo |
Provider carrying reads, sample and platform | merged (#2) |
golden_output_test |
Compares an output file to an expected file | merged (#6) |
trim_reads |
Adapter and quality trimming | planned |
classify_16s |
Taxonomic classification of 16S reads | planned |
abundance_table |
Per-sample taxon abundance table | planned |
ClassificationInfo |
Provider carrying per-read taxonomic assignments | planned |
TaxonomyDbInfo |
Provider carrying a pinned 16S reference database | planned |
microbiome_16s_pipeline |
Macro wiring QC, trimming, classification and abundance for many samples | planned |
Runs basic quality control over one FASTQ file and writes a tab-separated report with four metrics:
| Metric | Meaning |
|---|---|
reads |
Number of sequencing reads in the file |
bases |
Total number of bases across all reads |
mean_quality |
Mean Phred quality score (Phred+33 encoding) |
gc_percent |
Percentage of bases that are G or C |
These are the first numbers you look at after a run. Read and base counts confirm the sequencing depth you paid for. Mean quality below roughly 30 suggests the run or a subset of cycles had problems. GC content far from the expected value for your organism hints at contamination or adapter read-through.
load("@rules_bio//bio:defs.bzl", "fastq_qc")
fastq_qc(
name = "sample1_qc",
fastq = "sample1.fastq",
sample = "sample1", # defaults to the target name
platform = "illumina", # or "nanopore"
)The output file is sample1_qc_qc.txt. The target also returns a
FastqInfo provider so downstream rules can consume the reads without
re-declaring them.
A Starlark provider describing one sample's reads. It is the glue that lets rules in this repository be chained.
| Field | Type | Meaning |
|---|---|---|
reads |
File |
The FASTQ file |
sample |
string |
Sample name |
platform |
string |
"illumina" or "nanopore" |
Any rule that emits FastqInfo can feed any rule that accepts it. Rules
can also branch on platform, for example to choose different trimming
parameters for long noisy nanopore reads than for short accurate Illumina
reads.
To write your own rule that consumes it:
load("@rules_bio//bio:defs.bzl", "FastqInfo")
def _my_rule_impl(ctx):
info = ctx.attr.src[FastqInfo]
# info.reads is the FASTQ File, info.sample and info.platform are strings
...
my_rule = rule(
implementation = _my_rule_impl,
attrs = {"src": attr.label(providers = [FastqInfo])},
)A test rule that diffs a target's single output file against a checked-in expected file. Bioinformatics tools are usually deterministic given fixed inputs, so a golden file is a cheap and strict regression test. If the output drifts, the test fails and prints a unified diff.
load("@rules_bio//bio:defs.bzl", "golden_output_test")
golden_output_test(
name = "sample1_qc_test",
actual = ":sample1_qc",
expected = "sample1_expected_qc.txt",
)The actual target must produce exactly one file.
Rules pass data to each other through providers. DefaultInfo carries
the output files, which is how a test or a downstream genrule sees
them. The typed providers carry files plus metadata, which is how the
sequencing-aware rules see them:
| Provider | Emitted by | Consumed by |
|---|---|---|
FastqInfo |
fastq_qc, trim_reads |
trim_reads, classify_16s |
TaxonomyDbInfo |
database fetch rules | classify_16s |
ClassificationInfo |
classify_16s |
abundance_table |
Here is the intended shape of a 16S amplicon pipeline once the planned rules land. Each arrow is a provider dependency, and Bazel re-runs only the steps whose inputs changed.
sample1.fastq
│
├──▶ fastq_qc ──────────────▶ sample1_qc.txt ──▶ golden_output_test
│
└──▶ trim_reads ──(FastqInfo)──▶ classify_16s ──(ClassificationInfo)──▶ abundance_table
▲ ▲
@silva (TaxonomyDbInfo) │
│
sample2.fastq ──▶ trim_reads ──(FastqInfo)──▶ classify_16s ──(ClassificationInfo)┘
In BUILD form:
load("@rules_bio//bio:defs.bzl",
"fastq_qc", "trim_reads", "classify_16s", "abundance_table")
fastq_qc(
name = "sample1_qc",
fastq = "sample1.fastq",
platform = "illumina",
)
trim_reads( # planned
name = "sample1_trimmed",
src = ":sample1_qc", # consumes FastqInfo, inherits sample + platform
)
classify_16s( # planned
name = "sample1_taxa",
src = ":sample1_trimmed", # FastqInfo
database = "@silva//:138", # TaxonomyDbInfo, pinned by hash in MODULE.bazel
)
abundance_table( # planned
name = "all_samples",
srcs = [":sample1_taxa", ":sample2_taxa"], # ClassificationInfo
)Because trim_reads takes a FastqInfo rather than a raw file, it knows
the sample name and platform without you repeating them. Because the
reference database is a pinned external dependency, swapping to a new
SILVA release changes exactly one line, and Bazel re-runs classification
for every sample while leaving QC and trimming untouched.
For the common case, the planned microbiome_16s_pipeline macro expands
to all of the above:
load("@rules_bio//bio:defs.bzl", "microbiome_16s_pipeline")
microbiome_16s_pipeline( # planned
name = "study",
samples = glob(["reads/*.fastq"]),
db = "@silva//:138",
)In MODULE.bazel:
bazel_dep(name = "rules_bio", version = "0.0.1")In a BUILD.bazel file:
load("@rules_bio//bio:defs.bzl", "fastq_qc")
fastq_qc(
name = "sample1_qc",
fastq = "sample1.fastq",
platform = "illumina",
)Then:
bazel build //:sample1_qc
cat bazel-bin/sample1_qc_qc.txt
bazel test //tests/...
The tests/ directory runs fastq_qc on a tiny two-read FASTQ and checks
the report against a golden file with golden_output_test.
Apache 2.0