from snakemake.utils import min_version

min_version("6.3.0")

import pandas as pd
import yaml
import os.path
from os import path

configfile: "config/config.yaml"


# assign directory containing sequencing reads as specified in config.yaml
reads_dir = config["reads_dir"]

include: "rules/common_utils.smk"
include: "rules/common.smk"
include: "rules/ref_utils.smk"
include: "rules/ref.smk"
include: "rules/quality_control.smk"
include: "rules/trimming.smk"
include: "rules/mapping.smk"
include: "rules/duplicate_removal.smk"
include: "rules/indexes_bam2sam.smk"
include: "rules/insertion_identification.smk"
include: "rules/insertion_annotation.smk"


#### ONLY FOR GRID DESIGN (STOCK MATRIX) BASED ANALYSIS ####
if config["approach"] == "GRID":
    rule all:
        input:
#OLD pipeline
#        "multiqc/multiqc.html",
#        expand("sorted_alignments/{sample}.sorted.bam.bai", sample=SAMPLES),
#        expand("removed_duplicates_alignments/{sample}.dedup.bam.bai", sample=SAMPLES),
#        "MuSeq_table_final/Mu_single_GeneIds_gene_lengths_and_stock.csv",
#        "MuSeq_table_final/Mu_single_TranscriptIds_transcript_lengths_and_stock.csv"
#        "works.txt",
#        Checkpoint_GetReadType("checks/determine_read_type_check.{rt}.txt"),
#        Checkpoint_GetReadType("{rt}.works.txt"),
        #ref - fasta, annotation, (report)
        #"resources/annotation",
        #"resources/assembly_report.txt",
        #"resources/annotation.gff3",
        #"resources/split_annotation.gff3",
        #"resources/split_genome.fa",
        #"resources/renaming_scheme.txt",
        # final otput for both reference download paths 
        #"resources/genome.fa",
        #"resources/annotation",
            # final annotation table reagrdless of rule path for annotation file
            "resources/final_annotation_table",
        #cutadapt
        #Checkpoint_ReadSampleSheet_GRID("cut_reads/{sample}.fq1.gz"),
        #Checkpoint_ReadSampleSheet_GRID("cut_reads/{sample}.qc.txt"),
        #trimmomatic
        #Checkpoint_ReadSampleSheet_GRID("trimmed_reads/{sample}.forward_paired.fq.gz"),
        #fastqc
        #Checkpoint_ReadSampleSheet_GRID("results/fastqc/raw/{sample}_1_fastqc.html"),
        #Checkpoint_ReadSampleSheet_GRID("results/fastqc/raw/{sample}_1_fastqc.zip"),
        #trimmed fastqc 
        #Checkpoint_ReadSampleSheet_GRID("results/fastqc/trimmed/{sample}_forward_paired_fastqc.html"),
        #Checkpoint_ReadSampleSheet_GRID("results/fastqc/trimmed/{sample}_forward_paired_fastqc.zip"),
        #bowtie 2 index
        #multiext(
        #    "resources/genome",
        #    ".1.bt2", ".2.bt2", ".3.bt2", ".4.bt2", ".rev.1.bt2", ".rev.2.bt2",
        #),
        #bowtie2 align & convert to sorted bam
        #Checkpoint_ReadSampleSheet_GRID("results/mapped/{sample}.sam"),
        #Checkpoint_ReadSampleSheet_GRID("results/mapped/{sample}.sorted.bam"),
            Checkpoint_ReadSampleSheet_GRID("results/mapped/{sample}.sorted.bam.bai"),
        # picard + samtools view conversion to SAM format
        #Checkpoint_ReadSampleSheet_GRID("results/dedup/{sample}.dedup.bam"),
            Checkpoint_ReadSampleSheet_GRID("results/dedup/{sample}.dedup.bam.bai"),
            Checkpoint_ReadSampleSheet_GRID("results/dedup_sam/{sample}.dedup.sam"),           
            #multiqc
            "results/multiqc/multiqc.html",
            #insertion identification identification results
            "results/insertions_table_final/all_identified_insertions.csv",
            "results/insertions_table_final/germinal_identified_insertions.csv",
            #insertion annotation
            "results/insertions_table_final/all_identified_insertions_annotated.csv",
            "results/insertions_table_final/germinal_identified_insertions_annotated.csv",


#### ONLY FOR GENERIC ANALYSIS - NO STOCK MATRIX ####
elif config["approach"] == "GENERIC":

    if not is_single_end_GENERIC_experiment(SAMPLES):

        rule all:
            input:
                #fastqc
                expand("results/fastqc/raw/{sample}_1_fastqc.html", sample=SAMPLES),
                expand("results/fastqc/raw/{sample}_1_fastqc.zip", sample=SAMPLES),
                expand("results/fastqc/raw/{sample}_2_fastqc.html", sample=SAMPLES),
                expand("results/fastqc/raw/{sample}_2_fastqc.zip", sample=SAMPLES),
                #cutadapt
#                expand("results/cut_reads/{sample}.1.fq.gz", sample=SAMPLES),
#                expand("results/cut_reads/{sample}.2.fq.gz", sample=SAMPLES),
#                expand("results/cut_reads/{sample}.qc.txt", sample=SAMPLES),
                #trimmomatic
                expand("results/trimmed_reads/{sample}.1.fq.gz", sample=SAMPLES),
                expand("results/trimmed_reads/{sample}.2.fq.gz", sample=SAMPLES),
                #fastqc trimmed
                expand("results/fastqc/trimmed/{sample}_1_fastqc.html", sample=SAMPLES),
                expand("results/fastqc/trimmed/{sample}_1_fastqc.zip", sample=SAMPLES),
                expand("results/fastqc/trimmed/{sample}_2_fastqc.html", sample=SAMPLES),
                expand("results/fastqc/trimmed/{sample}_2_fastqc.zip", sample=SAMPLES),
                #bowtie2 align
                expand("results/mapped/{sample}.sam", sample=SAMPLES),
                #samtools; SAM to sorted BAM
                expand("results/mapped/{sample}.sorted.bam", sample=SAMPLES),
                #picard + samtools view conversion to SAM format
                expand("results/dedup_sam/{sample}.dedup.sam", sample=SAMPLES),
                expand("results/dedup/{sample}.dedup.bam", sample=SAMPLES),
                #indexes
                expand("results/mapped/{sample}.sorted.bam.bai", sample=SAMPLES),
                expand("results/dedup/{sample}.dedup.bam.bai", sample=SAMPLES),
                #multiqc
                "results/multiqc/multiqc.html",
                #insertion identification
                "results/insertions_table_final/all_identified_insertions.csv",
                #insertion annotation
                "results/insertions_table_final/all_identified_insertions_annotated.csv",


    elif is_single_end_GENERIC_experiment(SAMPLES):

        rule all:
            input:
                #fastqc
                expand("results/fastqc/raw/{sample}_1_fastqc.html", sample=SAMPLES),
                expand("results/fastqc/raw/{sample}_1_fastqc.zip", sample=SAMPLES),
                #cutadapt
#                expand("results/cut_reads/{sample}.1.fq.gz", sample=SAMPLES),
#                expand("results/cut_reads/{sample}.qc.txt", sample=SAMPLES),
                #trimmomatic
                expand("results/trimmed_reads/{sample}.1.fq.gz", sample=SAMPLES),
                #trimmed fastqc
                expand("results/fastqc/trimmed/{sample}_1_fastqc.html", sample=SAMPLES),
                expand("results/fastqc/trimmed/{sample}_1_fastqc.zip", sample=SAMPLES),
                #bowtie2 align
                expand("results/mapped/{sample}.sam", sample=SAMPLES),
                #samtools; SAM to sorted BAM
                expand("results/mapped/{sample}.sorted.bam", sample=SAMPLES),
                #picard + samtools view conversion to SAM format
                expand("results/dedup_sam/{sample}.dedup.sam", sample=SAMPLES),
                expand("results/dedup/{sample}.dedup.bam", sample=SAMPLES),
                #indexes
                expand("results/mapped/{sample}.sorted.bam.bai", sample=SAMPLES),
                expand("results/dedup/{sample}.dedup.bam.bai", sample=SAMPLES),
                #multiqc
                "results/multiqc/multiqc.html",
                #insertion identification
                "results/insertions_table_final/all_identified_insertions.csv",
                #insertion annotation
                "results/insertions_table_final/all_identified_insertions_annotated.csv",

       
