Complete E1-v2 workflow: stage A ladder + control + gate, stage B variants + baseline

- Snakefile: modules -> plain include: (single namespace; fixed cross-module
  helper resolution), rule all with stage-A/stage-B target sets driven by
  flat --config stage_b_arch/budget/stride switches.
- config/main.yaml: v2 run matrix (ladder 12-grid, control 1M cnn s4), store
  excludes (DCS spike-in, in-corpus escherichia trap reads), barcoding kit
  (SQK-RBK114-96 classified during dorado basecalls), removes v1 quota.
- baseline_pipeline: CUDA branch streams per-chunk dorado emit-sam through
  minimap2 (-N 10, tags preserved) and samtools view into per-batch BAMs,
  merged with samtools cat; ROCm/slorado fallback kept.
- ml_pipeline: label parquets -> signal stores (main / windows-ablation w4 /
  trap store) -> 12 ladder trainings + shuffled control -> custom_models test
  with the trap store (TP2) -> custom_models gate over the g11 runs; stage B
  adds windows ablation, holdout/core_clade(/bacillus_group) variants and the
  dorado-fast alignment baseline on the winner's identical test split.
- scripts: bam2annotation now emits source_run (barcoded -> barcodeNN from the
  dorado tags, dataset fallback); new label_qc.py (TP1 capture counts) and
  baseline_eval.py.
- custom_models submodule: bump to test_split.parquet export commit.
- README: two-stage run instructions (dev dry-run vs runner GPU).
This commit is contained in:
Tom Kasper
2026-10-15 19:54:51 +01:00
parent 5e95ea9476
commit 820863a9c7
10 changed files with 858 additions and 212 deletions
+154 -92
View File
@@ -1,70 +1,109 @@
def get_model_name(wildcards):
config_param=f'{wildcards.model}_model_name'
return config[config_param]
"""Data pipeline: pod5 download -> dorado basecall (+ barcode classification)
-> alignment to the panel -> merged BAM per (dataset, model).
def get_model_requirement(wildcards):
config_param=f'{wildcards.model}_model_name'
return config['dorado_model_dir']+'/'+config[config_param]
The 'hac' arm supplies the probe labels (stores); the 'fast' arm is the E1-v2
alignment baseline (dorad-fast + minimap2 + LCA). On CUDA the label job
threads dorado's classified SAM (per-read barcode tags from --kit-name)
straight into minimap2 (tags survive alignment, verified on minimap2 2.31)
and samtools view; per-batch BAMs are merged per (dataset, model). On ROCm
(slorado, non-barcode) the original fastq->align->bam chain is kept as the
fallback; barcodes are then unavailable and labels fall back to the dataset
name as source_run.
def get_benchmarking_file(wildcards):
config_param = f'{wildcards.model}_benchmarks_file'
if config[config_param]:
return f'--batchsize-benchmarks-file {config[config_param]}'
return ''
Host prerequisites: dorado on PATH, aws cli, curl; --resources gpu=N serialises
the GPU label jobs.
"""
def get_qscore(wildcards):
config_param = f'{wildcards.model}_min_q'
if config[config_param]:
return f'--min-qscore {config[config_param]}'
return ''
def get_batch_names(input_file):
with open(input_file,'rt') as ih:
return [line.strip().split('/')[-1].split('.')[0] for line in ih]
RUN_NAMES = {
"nomiss": "PBK98658_853a956f_57f83f46", # NO-MISS P2i flow cell
"trap": "ATCC_25922_202309", # public single-sample E. coli run
}
def run_name(dataset):
return RUN_NAMES[dataset]
def batch_names(dataset):
"""Pod5 chunk names per dataset (nomiss plain ints, trap 'batchN')."""
if dataset == "nomiss":
return list(range(1, config["pod5_dataset_size"] + 1, config["pod5_stride"]))
return [f"batch{x}" for x in range(1, config["trap_dataset_size"] + 1, config["trap_pod5_stride"])]
def get_run_name(wildcards):
if wildcards.dataset == 'nomiss':
return 'PBK98658_853a956f_57f83f46'
if wildcards.dataset == 'trap':
return 'ATCC_25922_202309'
return run_name(wildcards.dataset)
def get_batch_range(wildcards):
if wildcards.dataset == 'nomiss':
return range(1,config['pod5_dataset_size']+1,config['pod5_stride'])
if wildcards.dataset == 'trap':
return [f'batch{x}' for x in range(1,config['trap_dataset_size']+1,config['trap_pod5_stride'])]
return batch_names(wildcards.dataset)
def get_model_name(wildcards):
return config[f"{wildcards.model}_model_name"]
def get_model_requirement(wildcards):
return config["dorado_model_dir"] + "/" + config[f"{wildcards.model}_model_name"]
def get_benchmarking_file(wildcards):
bench = config.get(f"{wildcards.model}_benchmarks_file")
if bench:
return f"--batchsize-benchmarks-file {bench}"
return ""
def get_qscore(wildcards):
q = config.get(f"{wildcards.model}_min_q")
if q:
return f"--min-qscore {q}"
return ""
def get_kit_flag(wildcards):
kit = config["barcoding_kit"].get(wildcards.dataset)
if kit:
return f"--kit-name {kit}"
return ""
wildcard_constraints:
batch=r"(batch)?\d+",
model=r'hac|fast'
model="hac|fast",
dataset="nomiss|trap"
GPU_STACK = config.get('gpu_stack','cuda')
GPU_STACK = config.get("gpu_stack", "cuda")
# -------------------------------------------------------------------------
# pod5 acquisition
# -------------------------------------------------------------------------
rule download_nomiss_pod5:
output:
'../data/raw_pod5/nomiss/PBK98658_853a956f_57f83f46_{batch}.pod5'
"../data/raw_pod5/nomiss/PBK98658_853a956f_57f83f46_{batch}.pod5"
threads: 4
shell:
"""
aws s3 cp --no-sign-request s3://ont-open-data/nomiss_96BC_P2I_SUP_2026/raw/pod5/PBK98658_853a956f_57f83f46_{wildcards.batch}.pod5 {output}
"""
rule download_trap_tar:
output:
temp('../data/raw_pod5/trap/all.tar')
temp("../data/raw_pod5/trap/all.tar")
threads: 4
params:
tmp='../data/raw_pod5/trap/trap_all.tar'
shell:
"""
curl -L --http1.1 -A "Mozilla/5.0" "https://api.figshare.com/v2/file/download/45408628" -o {output}
"""
rule untar_trap_folder:
input:
'../data/raw_pod5/trap/all.tar'
"../data/raw_pod5/trap/all.tar"
output:
directory('../data/raw_pod5/trap/all')
directory("../data/raw_pod5/trap/all")
threads: 1
shell:
"""
@@ -72,45 +111,69 @@ rule untar_trap_folder:
tar xf {input} -C {output}
"""
rule extract_trap_pod5:
input:
'../data/raw_pod5/trap/all'
"../data/raw_pod5/trap/all"
output:
'../data/raw_pod5/trap/ATCC_25922_202309_{batch}.pod5'
"../data/raw_pod5/trap/ATCC_25922_202309_{batch}.pod5"
threads: 1
params:
input_pod5='{input[0]}/ATCC_25922__202309/ATCC_25922__202309_{batch}.pod5'
shell:
"""
mv {params.input_pod5} {output}
mv {input}/ATCC_25922__202309/ATCC_25922__202309_{wildcards.batch}.pod5 {output}
"""
if GPU_STACK == 'cuda':
rule basecall_pod5_dorado:
# -------------------------------------------------------------------------
# basecall + align (merged per dataset/model BAM; barcoding is classifier-side)
# -------------------------------------------------------------------------
if GPU_STACK == "cuda":
rule label_pod5_batch:
input:
pod5='../data/raw_pod5/{dataset}/{run}_{batch}.pod5',
model=get_model_requirement
pod5="../data/raw_pod5/{dataset}/{run}_{batch}.pod5",
model=get_model_requirement,
ref="../data/reference_genomes/full_reference.mmi"
output:
temp('../data/basecalled_reads/{dataset}/{model}/{run}_{batch}.fastq')
threads:
32
bam=temp("../data/aligned_reads/{dataset}/{model}/{run}_{batch}.bam")
threads: 32
resources:
gpu=1
params:
benchmarking=get_benchmarking_file,
min_qscore=get_qscore,
dorado_model=get_model_name,
benchmarking=get_benchmarking_file,
min_q=get_qscore,
kit_flag=get_kit_flag,
minimap_extra=config["minimap_extra"],
conda:
"../envs/minimap.yaml"
shell:
"""
dorado basecaller --models-directory {config[dorado_model_dir]} --emit-fastq {params.benchmarking} {params.min_qscore} {params.dorado_model} {input.pod5} > {output}
dorado basecaller --models-directory {config[dorado_model_dir]} --emit-sam {params.benchmarking} {params.min_q} {params.kit_flag} {params.dorado_model} {input.pod5} \
| minimap2 -a -x map-ont {params.minimap_extra} -t {threads} {input.ref} - \
| samtools view -b -@ {threads} > {output}
"""
elif GPU_STACK == 'rocm':
rule merge_aligned_reads:
input:
bams=expand("../data/aligned_reads/{{dataset}}/{{model}}/{run}_{batch}.bam", batch=get_batch_range, run=get_run_name),
output:
bam=temp("../data/aligned_reads/{dataset}_{model}_to_genome.bam"),
threads: 8
conda:
"../envs/samtools.yaml"
shell:
"""
samtools cat -@ {threads} -o {output.bam} {input.bams}
"""
elif GPU_STACK == "rocm":
# slorado emits fastq only: barcode classification is not available on this
# branch, so labels fall back to the dataset name as source_run.
rule basecall_pod5_slorado:
input:
pod5='../data/raw_pod5/{dataset}/{run}_{batch}.pod5',
pod5="../data/raw_pod5/{dataset}/{run}_{batch}.pod5",
model=get_model_requirement
output:
temp('../data/basecalled_reads/{dataset}/{model}/{run}_{batch}.fastq')
fastq=temp("../data/basecalled_reads/{dataset}/{model}/{run}_{batch}.fastq")
threads:
32
resources:
@@ -120,43 +183,42 @@ elif GPU_STACK == 'rocm':
pod5-slorado basecaller -o {output} -t {threads} --flash=yes {input.model} {input.pod5}
"""
rule concatenate_basecalled_fastq:
input:
expand('../data/basecalled_reads/{{dataset}}/{{model}}/{run}_{batch}.fastq',batch=get_batch_range,run=get_run_name)
output:
'../data/basecalled_reads/{dataset}_{model}.fastq.gz'
threads: 1
wildcard_constraints:
model="hac|fast"
shell:
"""
cat {input} | gzip -c > {output}
"""
rule concatenate_basecalled_fastq:
input:
expand("../data/basecalled_reads/{{dataset}}/{{model}}/{run}_{batch}.fastq", batch=get_batch_range, run=get_run_name)
output:
"../data/basecalled_reads/{dataset}_{model}.fastq.gz"
threads: 1
shell:
"""
cat {input} | gzip -c > {output}
"""
rule align_reads_to_reference:
input:
fastq='../data/basecalled_reads/{dataset}_{model}.fastq.gz',
ref='../data/reference_genomes/full_reference.mmi'
output:
'../data/aligned_reads/{dataset}_{model}_to_genome.sam'
threads: 32
conda:
'../envs/minimap.yaml'
shell:
"""
minimap2 -ax map-ont -t {threads} {input.ref} {input.fastq} > {output}
"""
rule align_reads_to_reference:
input:
fastq="../data/basecalled_reads/{dataset}_{model}.fastq.gz",
ref="../data/reference_genomes/full_reference.mmi"
output:
temp("../data/aligned_reads/{dataset}_{model}_to_genome.sam")
params:
minimap_extra=config["minimap_extra"],
threads: 32
conda:
"../envs/minimap.yaml"
shell:
"""
minimap2 -ax map-ont {params.minimap_extra} -t {threads} {input.ref} {input.fastq} > {output}
"""
rule convert_sam_to_bam:
input:
'../data/aligned_reads/{dataset}_{model}_to_genome.sam'
output:
'../data/aligned_reads/{dataset}_{model}_to_genome.sorted.bam'
threads: 32
conda:
'../envs/samtools.yaml'
shell:
"""
samtools view -b -@ {threads} {input} | samtools sort -@ {threads} > {output}
samtools index -@ {threads} {output}
"""
rule convert_sam_to_bam:
input:
"../data/aligned_reads/{dataset}_{model}_to_genome.sam"
output:
temp("../data/aligned_reads/{dataset}_{model}_to_genome.bam")
threads: 16
conda:
"../envs/samtools.yaml"
shell:
"""
samtools view -b -@ {threads} {input} > {output}
"""