"""Data pipeline: pod5 download -> dorado basecall (+ barcode classification, native alignment via --reference) -> label-ready alignment artifacts. 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 + --mm2-opts) and each pod5 chunk is written straight to CRAM (space-efficient; no merge step — the label job consumes the per-batch CRAMs directly, so no dataset-sized intermediate exists at all). 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. 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.fasta" output: alignments=temp("../data/aligned_reads/{dataset}/{model}/{run}_{batch}.cram") 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=f"-x {config['align_preset']} {config['minimap_extra']}", shell: """ dorado basecaller --models-directory {config[dorado_model_dir]} --emit-cram {params.benchmarking} {params.min_q} {params.kit_flag} --reference {input.ref} --mm2-opts "{params.mm2_opts}" {params.dorado_model} {input.pod5} > {output} """ 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 -a -x {config[align_preset]} {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} """