if config["genome"] == "":
    pass
else:
    rule star_create_index:
        """
        Create Genome Index for reference genome (STAR).
        """
        input:
            fasta=config["genome"]
        output:
            "%s/chrName.txt" % (config["genome-star-index"])
        log:
            "%s/logs/star_create_index.log" % (config["project-folder"])
        benchmark:
            "%s/benchmark/star_create_index.tsv" % (config["project-folder"])
        singularity: config["singularity"]["star"]
        threads: cluster["star_create_index"]["cpus-per-task"]
        resources:
            time=cluster["star_create_index"]["time"],
            mem=cluster["star_create_index"]["mem-per-cpu"]
        params: index=config["genome-star-index"]
        shell:"""
                mkdir -p {params.index}
                STAR --runThreadN {threads} --runMode genomeGenerate --genomeDir {params.index} --genomeFastaFiles {input.fasta} 2> {log}
        """
        
if config["genome"] == "":
    pass
else:
    rule bwa_create_insilico_index:
        """
        Create Genome Index for in-silico prediction (BWA).
        """
        input:
            full="%s/References/full_inSilico_reference.fa" % (config["project-folder"]),
            selected="%s/References/sizeSelected_inSilico_reference.fa" % (config["project-folder"])
        output:
            full=config["genome-bwa-full_insilico-index"],
            selected=config["genome-bwa-selected_insilico-index"]
        log:
            full="%s/logs/bwa_create_full_insilico_index.log" % (config["project-folder"]),
            selected="%s/logs/bwa_create_full_insilico_index.log" % (config["project-folder"])
        benchmark:
            "%s/benchmark/bwa_create_insilico_index.tsv" % (config["project-folder"])
        singularity: config["singularity"]["gbs"]
        threads: cluster["bwa_create_insilico_index"]["cpus-per-task"]
        resources:
            time=cluster["bwa_create_insilico_index"]["time"],
            mem=cluster["bwa_create_insilico_index"]["mem-per-cpu"]
        shell:"""
            bwa index -a bwtsw {input.full} 2> {log.full}
            samtools faidx {input.full} 2>> {log.full}
                
            bwa index -a bwtsw {input.selected} 2> {log.selected}
            samtools faidx {input.selected} 2>> {log.selected}
        """
        
rule Concatenate_lanes:
    """
    Concatenate the demultiplexed fastq files (BASH).
    """
    input:
        R1=get_fastq_for_concatenating_read1,
        R2=get_fastq_for_concatenating_read2
    output:
        R1=temp("%s/FASTQ/CONCATENATED/{samples}_R1_001.merged.fastq.gz" % (config["project-folder"])),
        R1Report="%s/FASTQ/CONCATENATED/{samples}_R1_001.merged.fastq.gz.report" % (config["project-folder"]),
        R2=temp("%s/FASTQ/CONCATENATED/{samples}_R2_001.merged.fastq.gz" % (config["project-folder"])),
        R2Report="%s/FASTQ/CONCATENATED/{samples}_R2_001.merged.fastq.gz.report" % (config["project-folder"])
    log:
        "%s/logs/SHELL/catFastq_{samples}.log" % (config["project-folder"])
    benchmark:
        "%s/benchmark/Concatenate_lanes.{samples}.tsv" % (config["project-folder"])
    threads: cluster["Concatenate_lanes"]["cpus-per-task"]
    resources:
        time=cluster["Concatenate_lanes"]["time"],
        mem=cluster["Concatenate_lanes"]["mem-per-cpu"]
    params:
        outfolder="%s/FASTQ/CONCATENATED" % (config["project-folder"])
    shell:"""
        mkdir -p {params.outfolder} &> {log}
        cat {input.R1} > {output.R1} 2>> {log}
        ls {input.R1} > {output.R1Report} 2>> {log}
        cat {input.R2} > {output.R2} 2>> {log}
        ls {input.R2} > {output.R2Report} 2>> {log}
  	"""

if config["genome"] == "":
    pass
else:  	
    rule bwa_create_index:
        """
        Index the Reference Genome (BWA).
        """
        input:
            config["genome"]
        output:
            config["genome-bwa-index"]
        log:
            "%s/logs/bwa_create_index.log" % (config["project-folder"])
        benchmark:
            "%s/benchmark/bwa_create_index.tsv" % (config["project-folder"])
        threads: cluster["bwa_create_index"]["cpus-per-task"]
        resources:
            time=cluster["bwa_create_index"]["time"],
            mem=cluster["bwa_create_index"]["mem-per-cpu"]
        singularity: config["singularity"]["gbs"]
        shell:"""
                bwa index -a bwtsw {input} 2> {log}
                samtools faidx {input} 2> {log}
      	"""

rule IndexMockReferenceBWA:
    """
    Index the Mock Reference Genome (BWA).
    """
    input:
        "%s/FASTQ/TRIMMED/GSC.MR.Genome.fa" % (config["project-folder"]),
    output:
        "%s/FASTQ/TRIMMED/GSC.MR.Genome.fa.bwt" % (config["project-folder"])
    log:
        "%s/IndexMockReferenceBWA.log" % (config["project-folder"])
    benchmark:
        "%s/benchmark/IndexMockReferenceBWA.tsv" % (config["project-folder"])
    threads: cluster["IndexMockReferenceBWA"]["cpus-per-task"]
    resources:
        time=cluster["IndexMockReferenceBWA"]["time"],
        mem=cluster["IndexMockReferenceBWA"]["mem-per-cpu"]
    singularity: config["singularity"]["gbs"]
    shell:"""
        bwa index -a bwtsw {input}
  	"""

rule IndexMockReferenceSamtools:
    """
    Index the Mock Reference Genome (samtools).
    """
    input:
        "%s/FASTQ/TRIMMED/GSC.MR.Genome.fa" % (config["project-folder"]),
    output:
        "%s/FASTQ/TRIMMED/GSC.MR.Genome.fa.fai" % (config["project-folder"])
    log:
        "%s/logs/IndexMockReferenceSamtools.log" % (config["project-folder"])
    benchmark:
        "%s/benchmark/IndexMockReferenceSamtools.tsv" % (config["project-folder"])
    singularity: config["singularity"]["gbs"]
    threads: cluster["IndexMockReferenceSamtools"]["cpus-per-task"]
    resources:
        time=cluster["IndexMockReferenceSamtools"]["time"],
        mem=cluster["IndexMockReferenceSamtools"]["mem-per-cpu"]
    shell:"""
        samtools faidx {input}
  	"""

rule IndexClustersBWA:
    """
    Index the Mock Reference Genome Clusters (BWA).
    """
    input:
        "%s/FASTQ/TRIMMED/GSC.MR.Clusters.fa" % (config["project-folder"]),
    output:
        "%s/FASTQ/TRIMMED/GSC.MR.Clusters.fa.bwt" % (config["project-folder"])
    log:
        "%s/logs/IndexClustersBWA.log" % (config["project-folder"])
    benchmark:
        "%s/benchmark/IndexClustersBWA.tsv" % (config["project-folder"])
    singularity: config["singularity"]["gbs"]
    threads: cluster["IndexClustersBWA"]["cpus-per-task"]
    resources:
        time=cluster["IndexClustersBWA"]["time"],
        mem=cluster["IndexClustersBWA"]["mem-per-cpu"]
    shell:"""
        bwa index -a bwtsw {input}
  	"""

rule IndexClustersSamtools:
    """
    Index the Mock Reference Genome Clusters (samtools).
    """
    input:
        "%s/FASTQ/TRIMMED/GSC.MR.Clusters.fa" % (config["project-folder"]),
    output:
        "%s/FASTQ/TRIMMED/GSC.MR.Clusters.fa.fai" % (config["project-folder"])
    log:
        "%s/logs/IndexClustersSamtools.log" % (config["project-folder"])
    benchmark:
        "%s/benchmark/IndexClustersSamtools.tsv" % (config["project-folder"])
    singularity: config["singularity"]["gbs"]
    threads: cluster["IndexClustersSamtools"]["cpus-per-task"]
    resources:
        time=cluster["IndexClustersSamtools"]["time"],
        mem=cluster["IndexClustersSamtools"]["mem-per-cpu"]
    shell:"""
        samtools faidx {input}
  	"""
  	
rule IndexFinalMockBWA:
    """
    Index the final Mock Reference Genome (BWA).
    """
    input:
        "%s/MockReference/MockReference.fa" % (config["project-folder"])
    output:
        "%s/MockReference/MockReference.fa.bwt" % (config["project-folder"])
    log:
        "%s/logs/IndexFinalMockBWA.log" % (config["project-folder"])
    benchmark:
        "%s/benchmark/IndexFinalMockBWA.tsv" % (config["project-folder"])
    singularity: config["singularity"]["gbs"]
    threads: cluster["IndexFinalMockBWA"]["cpus-per-task"]
    resources:
        time=cluster["IndexFinalMockBWA"]["time"],
        mem=cluster["IndexFinalMockBWA"]["mem-per-cpu"]
    shell:"""
        bwa index -a bwtsw {input}
  	"""

rule IndexFinalMockSamtools:
    """
    Index the final Mock Reference Genome Clusters (samtools).
    """
    input:
        "%s/MockReference/MockReference.fa" % (config["project-folder"])
    output:
        "%s/MockReference/MockReference.fa.fai" % (config["project-folder"])
    log:
        "%s/logs/IndexFinalMockSamtools.log" % (config["project-folder"])
    benchmark:
        "%s/benchmark/IndexFinalMockSamtools.tsv" % (config["project-folder"])
    singularity: config["singularity"]["gbs"]
    threads: cluster["IndexFinalMockSamtools"]["cpus-per-task"]
    resources:
        time=cluster["IndexFinalMockSamtools"]["time"],
        mem=cluster["IndexFinalMockSamtools"]["mem-per-cpu"]
    shell:"""
        samtools faidx {input}
  	"""
  	
if config["mockreference"] == "":
    pass
else:  	
    if os.path.isfile(config["mockref-bwa-index"]):
        pass
    else:
        rule bwa_create_index_existingMock:
            """
            Index the existing mock reference Genome (BWA).
            """
            input:
                config["mockreference"]
            output:
                config["mockref-bwa-index"]
            log:
                "%s/logs/bwa_create_index_existingMock.log" % (config["project-folder"])
            benchmark:
                "%s/benchmark/bwa_create_index_existingMock.tsv" % (config["project-folder"])
            singularity: config["singularity"]["gbs"]
            threads: cluster["bwa_create_index_existingMock"]["cpus-per-task"]
            resources:
                time=cluster["bwa_create_index_existingMock"]["time"],
                mem=cluster["bwa_create_index_existingMock"]["mem-per-cpu"]
            shell:"""
                    bwa index -a bwtsw {input} 2> {log}
                    samtools faidx {input} 2> {log}
          	"""
          	
rule R_createBarcodesID_file:
    """
    Create the file barcodesID.txt (R).
    """
    input:
        script=config["barcodes-script"],
        samplesheet=config["samplesheet-file"]
    output:
        config["barcodes-file"]
    log:
        "%s/logs/R_createBarcodesID_file.log" % (config["project-folder"])
    benchmark:
        "%s/benchmark/R_createBarcodesID_file.tsv" % (config["project-folder"])
    singularity:
        config["singularity"]["r-gbs"]
    threads: cluster["R_createBarcodesID_file"]["cpus-per-task"]
    resources:
        time=cluster["R_createBarcodesID_file"]["time"],
        mem=cluster["R_createBarcodesID_file"]["mem-per-cpu"]
    params:
       projFolder=config["project-folder"],
       pipeFolder=config["pipeline-folder"],
       pipeConfig=config["pipeline-config"],
       refGenome=config["genome"]
    shell:"""
       Rscript {input.script} {input.samplesheet} {output} &> {log}
    """
