# Snakefile

# Define input files
SAMPLES = ["ERR10627364", "ERR10627365", "ERR10627366"]
GENOME = "ref/Plasmodium_falciparum.ASM276v2.dna_sm.toplevel.fa.gz"

rule all:
    input:
        expand("results/{sample}.bam", sample=SAMPLES)

rule trim_reads:
    input:
        r1 = "fastq/{sample}_R1.fastq.gz",
        r2 = "fastq/{sample}_R2.fastq.gz"
    output:
        r1 = "trimmed/{sample}_R1_trimmed.fastq.gz",
        r2 = "trimmed/{sample}_R2_trimmed.fastq.gz"
    resources:
        mem_mb = 4000,
        time = "01:00:00"
    threads: 4
    shell:
        "apptainer run --writable-tmpfs trimmomatic.sif PE -threads {threads} {input.r1} {input.r2} "
        "{output.r1} /dev/null {output.r2} /dev/null "
        "ILLUMINACLIP:TruSeq3-PE.fa:2:30:10 LEADING:3 TRAILING:3 SLIDINGWINDOW:4:15 MINLEN:36"

rule align_reads:
    input:
        r1 = "trimmed/{sample}_R1_trimmed.fastq.gz",
        r2 = "trimmed/{sample}_R2_trimmed.fastq.gz",
        ref = GENOME
    output:
        "results/{sample}.bam"
    resources:
        mem_mb = 8000,
        time = "04:00:00"
    threads: 8
    shell:
        "apptainer run --writable-tmpfs bwa.sif mem -t {threads} {input.ref} {input.r1} {input.r2} | "
        "apptainer run --writable-tmpfs samtools.sif view -bS - |"
        "apptainer run --writable-tmpfs samtools.sif sort -o {output}"
