# Snakefile WGBS #
##################
# Last modified 15/01/2021
# Really last modified 22-11-2022

# Collect samples, function returns tuples, that is reason comma
# We have forward and reverse read, so build better wild
# This stores the sample names
# Get only the names as WGBS01, WGBS02, ...
#SAMPLES = ["WGBS31"]
SAMPLES, = glob_wildcards("raw_data/{sample}_R1_001.fastq.gz")
#READS = ["1", "2"]
CONTEXTS = ["CpG", "CHH", "CHG"]

rule all:
    input:
        expand("bismark_methylation/{context}_context_{sample}_R1_001_val_1_bismark_bt2_pe.deduplicated.txt.gz", sample=SAMPLES, context=CONTEXTS),
        expand("bismark_methylation/{context}_coverage_{sample}.gz.bismark.cov.gz", sample=SAMPLES, context=CONTEXTS), 
        "used_tools.info"

rule trim_galore:
    input:
        R1 = "raw_data/{sample}_R1_001.fastq.gz",
        R2 = "raw_data/{sample}_R2_001.fastq.gz"
    output:
        "trimmed_data/{sample}_R1_001_val_1.fq.gz",
        "trimmed_data/{sample}_R2_001_val_2.fq.gz"
    threads:
        4
    log:
        log = "log/trim_galore/{sample}.log",
        err = "log/trim_galore/{sample}.err"
    shell:
        """
        trim_galore --paired --cores {threads} -o trimmed_data \
        {input.R1} {input.R2} > {log.log} 2> {log.err}
        """

# Prepare genome
rule bismark_genome:
    output:
        "genome.ready"
    shell:
        """
        bismark_genome_preparation --genomic_composition genome/

        touch genome.ready 
        """

# According to the author:
# Do not change -N useless you have a very good reason
# Adjust score_min to L,0,-0.4 (for not so polished genomes as this one)
# Output rule has to be so because you have a forward and reverse read
rule bismark_map:
    input:
        R1 = "trimmed_data/{sample}_R1_001_val_1.fq.gz",
        R2 = "trimmed_data/{sample}_R2_001_val_2.fq.gz",
        ready = "genome.ready"
    output:
        "bismark_mapping/{sample}_R1_001_val_1_bismark_bt2_pe.bam"
    threads:
        4
    log:
        log = "log/bismark_mapping/{sample}.log",
        err = "log/bismark_mapping/{sample}.err"
    shell:
        """
        bismark --parallel {threads} --score_min L,0,-0.4 \
        -o bismark_mapping --genome genome --temp_dir bismark_temp  \
        -1 {input.R1} -2 {input.R2} > {log.log} 2> {log.err}
        """

rule bismark_dedup:
    input:
        "bismark_mapping/{sample}_R1_001_val_1_bismark_bt2_pe.bam"
    output:
        "bismark_dedup/{sample}_R1_001_val_1_bismark_bt2_pe.deduplicated.bam"
    log:
        log = "log/bismark_dedup/{sample}.log",
        err = "log/bismark_dedup/{sample}.err"
    shell:
        """
        deduplicate_bismark --output_dir bismark_dedup {input} > {log.log} 2> {log.err}
        """


# comprehensive means pooling per context, as I want
# no bedGraph, run separately
# Problem seemed to be with multipled outputs. I think this rule was applied three times, while we need it only once
rule bismark_methylation:
    input:
        "bismark_dedup/{sample}_R1_001_val_1_bismark_bt2_pe.deduplicated.bam"
    output:
        "bismark_methylation/CpG_context_{sample}_R1_001_val_1_bismark_bt2_pe.deduplicated.txt.gz",
        "bismark_methylation/CHG_context_{sample}_R1_001_val_1_bismark_bt2_pe.deduplicated.txt.gz",
        "bismark_methylation/CHH_context_{sample}_R1_001_val_1_bismark_bt2_pe.deduplicated.txt.gz"
    log:
        log = "log/bismark_methylation/{sample}.log",
        err = "log/bismark_methylation/{sample}.err",
    threads:
        4
    shell:
        """
        bismark_methylation_extractor --multicore {threads} --comprehensive --report \
        --gzip --output bismark_methylation {input} > {log.log} 2> {log.err}
        """

# Call this on the individual CpG, CHH and CHH_context_ files
# Just add --CX to process everything. This does not influence the results of CpG context.
# Try trick with load to restrict number of instances. Otherwise too many open files
rule bismark_bedgraph:
    input:
        "bismark_methylation/{context}_context_{sample}_R1_001_val_1_bismark_bt2_pe.deduplicated.txt.gz"
    output:
        "bismark_methylation/{context}_coverage_{sample}.gz.bismark.cov.gz"
    log:
        log = "log/bismark_bedgraph/{context}_{sample}.log",
        err = "log/bismark_bedgraph/{context}_{sample}.err"
    params:
        prefix = "{context}_coverage_{sample}"
    shell:
        """
        bismark2bedGraph --scaffolds --buffer_size 12G --CX --dir bismark_methylation -o {params.prefix}  {input} > {log.log} 2> {log.err}
        """

# Used tools
rule used_tools:
    input:
        expand("bismark_methylation/{context}_coverage_{sample}.gz.bismark.cov.gz", sample=SAMPLES, context=CONTEXTS)
    output:
        "used_tools.info"
    shell:
        """
        trim_galore --version >> {output}

        bismark --version >> {output}
        """
