Files
eDNA_Stream_E01/workflow/rules/baseline_pipeline.smk
T
Tom Kasper 2ce1435a96 Align with dorado's native --reference aligner instead of piping SAM to minimap2
htslib rejected minimap2's re-emitted dorado SAM records on the runner
('CIGAR and query sequence are of different length'), killing the
label_pod5_batch pipe. dorado aligns during basecalling (--reference
<mmi> + --mm2-opts '-x map-ont -N 10'), is documented to cost no
basecalling throughput, and its own writer preserves the per-record
BC/RG barcode tags that bam2annotation needs for source_run. The rocm
fallback keeps the fastq->minimap2 chain.
2026-10-16 20:03:34 +01:00

226 lines
7.2 KiB
Plaintext

"""Data pipeline: pod5 download -> dorado basecall (+ barcode classification,
native alignment via --reference) -> merged BAM per (dataset, model).
The 'hac' arm supplies the probe labels (stores); the 'fast' arm is the E1-v2
alignment baseline (dorado-fast + minimap2 + LCA). On CUDA, alignment runs
inside dorado (--reference <mmi> + --mm2-opts): the external
minimap2-over-SAM pipe was dropped because htslib rejected minimap2's
re-emitted dorado records ("CIGAR and query sequence are of different
length"); dorado's own aligned writer keeps the per-record BC/RG barcode
tags that bam2annotation needs for source_run. Per-batch BAMs are merged
per (dataset, model) with samtools cat. On ROCm (slorado, non-barcode) the
fastq->minimap2->bam chain is kept as the fallback; barcodes are then
unavailable and labels fall back to the dataset name as source_run.
Host prerequisites: dorado on PATH, aws cli, curl; --resources gpu=N serialises
the GPU label jobs.
"""
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):
return run_name(wildcards.dataset)
def get_batch_range(wildcards):
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="hac|fast",
dataset="nomiss|trap"
GPU_STACK = config.get("gpu_stack", "cuda")
# -------------------------------------------------------------------------
# pod5 acquisition
# -------------------------------------------------------------------------
rule download_nomiss_pod5:
output:
"../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")
threads: 4
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"
output:
directory("../data/raw_pod5/trap/all")
threads: 1
shell:
"""
mkdir -p {output}
tar xf {input} -C {output}
"""
rule extract_trap_pod5:
input:
"../data/raw_pod5/trap/all"
output:
"../data/raw_pod5/trap/ATCC_25922_202309_{batch}.pod5"
threads: 1
shell:
"""
mv {input}/ATCC_25922__202309/ATCC_25922__202309_{wildcards.batch}.pod5 {output}
"""
# -------------------------------------------------------------------------
# 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,
ref="../data/reference_genomes/full_reference.mmi"
output:
bam=temp("../data/aligned_reads/{dataset}/{model}/{run}_{batch}.bam")
threads: 32
resources:
gpu=1
params:
dorado_model=get_model_name,
benchmarking=get_benchmarking_file,
min_q=get_qscore,
kit_flag=get_kit_flag,
mm2_opts=config["dorado_mm2_opts"],
conda:
"../envs/minimap.yaml"
shell:
"""
dorado basecaller --models-directory {config[dorado_model_dir]} --emit-sam {params.benchmarking} {params.min_q} {params.kit_flag} --reference {input.ref} --mm2-opts "{params.mm2_opts}" {params.dorado_model} {input.pod5} \
| samtools view -b -@ {threads} > {output}
"""
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",
model=get_model_requirement
output:
fastq=temp("../data/basecalled_reads/{dataset}/{model}/{run}_{batch}.fastq")
threads:
32
resources:
gpu=1
shell:
"""
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
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:
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:
temp("../data/aligned_reads/{dataset}_{model}_to_genome.bam")
threads: 16
conda:
"../envs/samtools.yaml"
shell:
"""
samtools view -b -@ {threads} {input} > {output}
"""