import os
import sys

shell.prefix("export PATH=$PATH:$PWD; ")


# import config files
configfile: "../config/config.yml"

# SET UP PATH VARIABLES
cwd = os.getcwd()
project_path = cwd[:cwd.find('workflow') + len('workflow')]
input_dir = os.path.join(project_path, config['input_root'])
output_dir = os.path.join(project_path, config['output_root'])
vcf_filtered_dir = os.path.join(output_dir, "filtered_vcfs")
annotated_mutations_dir = os.path.join(output_dir, "annotated_vcfs")
final_dir = os.path.join(output_dir, "final_source")


# SET UP ALL NECESSARY DIRECTORIES
dirs = [input_dir, output_dir, vcf_filtered_dir, annotated_mutations_dir, final_dir]

for d in dirs:
    if not os.path.exists(d):
        print('Making '+ d + ':')
        os.mkdir(d)
        print('complete\n')

# SET samples EQUAL TO THE INPUT FILE WILDCARDS
samples, = glob_wildcards(os.path.join(input_dir, "{sample}.vcf"))


rule all:
    input:
        expand(os.path.join(final_dir, "{sample}.pdf"), sample=samples)

rule store_unique_vcf_lines:
    input:
        vcf=os.path.join(input_dir, "{sample}.vcf")
    output:
        temp(os.path.join(input_dir, "{sample}.stored"))
    shell:
        "python scripts/prepare_source/store_unique_vcf_lines.py {input.vcf} {output}"

rule filter_pass_only:
    input:
        temp(os.path.join(input_dir, "{sample}.stored")),
        vcf=os.path.join(input_dir, "{sample}.vcf")
    output:
        os.path.join(vcf_filtered_dir, "{sample}.recode.vcf")
    shell:
        "vcftools --vcf {input.vcf} --remove-filtered-all --recode --out " + os.path.join(vcf_filtered_dir, "{wildcards.sample}") + " 2> /dev/null"


rule reformat_for_annovar:
    input:
        vcf=os.path.join(vcf_filtered_dir, "{sample}.recode.vcf")
    output:
        os.path.join(vcf_filtered_dir, "{sample}.reformatted.vcf")
    shell:
        "python scripts/prepare_source/reformat_for_annovar.py {input.vcf}"


rule get_annotations:  #Replace with snpeff, vcfanno
    input:
        vcf=os.path.join(vcf_filtered_dir, "{sample}.reformatted.vcf")
    params:
        build = config['build'],
        out = os.path.join(annotated_mutations_dir, "{sample}.annotated"),
        annovar_script = config['annovar_script'],
        human_db = config['human_db']
    output:
        os.path.join(annotated_mutations_dir, "{sample}.annotated." + config['build'] + "_multianno.vcf")
    shell:
        "perl {params.annovar_script} "
        "-vcfinput {input.vcf} "
        "{params.human_db} "
        "-buildver {params.build} "
        "--out {params.out} "
        "-remove "
        "-protocol refGene "
        "-operation g "
        "-nastring . > /dev/null 2>&1"


rule remove_annovar_outputs:
    input:
        vcf=os.path.join(annotated_mutations_dir, "{sample}.annotated." + config['build'] + "_multianno.vcf")
    output:
        os.path.join(annotated_mutations_dir, "{sample}.annotated.vcf")
    shell:
        "python scripts/prepare_source/rename_annovar.py {input.vcf}"


rule format_source:
    input:
        vcf=os.path.join(annotated_mutations_dir, "{sample}.annotated.vcf"),
        targets=os.path.join(input_dir, "{sample}.bed")
    output:
        os.path.join(final_dir, "{sample}.csv")
    params:
        script=os.path.join(project_path, 'scripts', 'prepare_source','format_source.py')
    shell:
        "python {params.script} {input.vcf} {input.targets} {output}"


rule get_report:
    input:
        csv=os.path.join(final_dir, "{sample}.csv")
    output:
        os.path.join(final_dir, "{sample}.pdf")
    shell:
        "python scripts/jasper_api/request.py {output}"
