import os
import pandas as pd
from pling.utils import get_pling_root_dir

configfile: "../config.yaml"

FASTAFILES = [el[0] for el in pd.read_csv(config["genomes_list"], header=None).values]
FASTAEXT = {os.path.splitext(os.path.basename(el))[0]:os.path.splitext(os.path.basename(el))[1] for el in FASTAFILES}
GENOMES = list(FASTAEXT.keys())
FASTAPATH = os.path.dirname(FASTAFILES[0])
OUTPUTPATH = config["output_dir"]
COMMUNITIESPATH = config["communities"] + "/objects/communities.txt"
PREFIX = config["prefix"]

def get_community_to_plasmid():
    community_to_plasmid = {}
    communities = []
    with open(COMMUNITIESPATH) as communities_fh:
        for community_index, line in enumerate(communities_fh):
            plasmids = line.strip().split()
            community_to_plasmid[str(community_index)] = plasmids
            if len(plasmids)>1:
                communities.append(community_index)
    return community_to_plasmid, communities

COMMUNITY_TO_PLASMID, COMMUNITIES = get_community_to_plasmid()

def blocks_input(community):
    if config.get("dedup", False):
        files = [f"{OUTPUTPATH}/unimogs/relabelled/dedup/{community}_dedup.unimog", f"{OUTPUTPATH}/unimogs/relabelled/dedup/{community}_map_dedup.txt"]
    else:
        files = [f"{OUTPUTPATH}/unimogs/{community}_anno.unimog", f"{OUTPUTPATH}/unimogs/{{community}}_map.txt"]
    return files

rule all:
    input:
        blocks_unimog = expand(f"{OUTPUTPATH}/unimogs/relabelled/blocks/{{community}}_blocks.unimog", community=COMMUNITIES),
        blocks_map = expand(f"{OUTPUTPATH}/unimogs/relabelled/blocks/{{community}}_map_blocks.txt", community=COMMUNITIES)

rule bakta:
    input:
        genome = lambda wildcards: f"{FASTAPATH}/{wildcards.genome}{FASTAEXT[wildcards.genome]}"
    output:
        ann_dir = directory(f"{OUTPUTPATH}/annotation/{{community}}/bakta/{{genome}}")
    conda:
        "../envs/bakta.yaml"
    threads: config["bakta_threads"]
    resources:
        mem_mb=lambda wildcards, attempt: attempt * config["bakta_mem"]
    params:
        DB = directory(f"{config['bakta_db']}")
    log: f"{OUTPUTPATH}/logs/bakta/{{community}}/{{genome}}.log"
    shell:
        "bakta {input.genome} --skip-plot --db {params.DB} --prefix {wildcards.genome} --translation-table 11 --threads {threads} --output {output.ann_dir} >{log} 2>&1"

rule panaroo:
    input:
        directories = lambda wildcards: expand(f"{OUTPUTPATH}/annotation/{wildcards.community}/bakta/{{genome}}", genome=COMMUNITY_TO_PLASMID[wildcards.community])
    output:
        pangenome = directory(f"{OUTPUTPATH}/annotation/{{community}}/panaroo"),
        aln = directory(f"{OUTPUTPATH}/annotation/{{community}}/panaroo/aligned_gene_sequences")
    conda:
        "../envs/panaroo.yaml"
    threads: config["panaroo_threads"]
    resources:
        mem_mb=lambda wildcards, attempt: attempt * config["panaroo_mem"]
    params:
        files = lambda wildcards: [f"{OUTPUTPATH}/annotation/{wildcards.community}/bakta/{genome}/{genome}.gff3" for genome in COMMUNITY_TO_PLASMID[wildcards.community]]
    shell:
        "panaroo -i {params.files} -o {output.pangenome} --clean-mode moderate -a pan --merge_paralogs -t {threads}"

rule gen_minimap_input:
    input:
        align_dir=f"{OUTPUTPATH}/annotation/{{community}}/panaroo/aligned_gene_sequences"
    output:
        f"{OUTPUTPATH}/tmp_files/minimap/{{community}}/input/{{genome}}.fna"
    conda:
        "../envs/minimap_input.yaml"
    threads: 1
    resources:
        mem_mb = lambda wildcards, attempt: attempt * 4000
    params:
        out_dir=f"{OUTPUTPATH}/tmp_files/minimap/{{community}}/input"
    script:
        "gen_minimap_input.py"

rule minimap:
    input:
        reads=f"{OUTPUTPATH}/tmp_files/minimap/{{community}}/input/{{genome}}.fna",
        dir_ref = FASTAPATH
    output:
        f"{OUTPUTPATH}/tmp_files/minimap/{{community}}/output/{{genome}}.paf"
    conda:
        "../envs/minimap2.yaml"
    threads: config["minimap_threads"]
    resources:
        mem_mb = lambda wildcards, attempt: attempt * config["minimap_mem"]
    shell:
        "minimap2 -t {threads} -c {input.dir_ref}/{wildcards.genome}.fna {input.reads} > {output}"

rule consolidation:
    input:
        lambda wildcards: expand(f"{OUTPUTPATH}/tmp_files/minimap/{wildcards.community}/output/{{genome}}.paf", genome=COMMUNITY_TO_PLASMID[wildcards.community]),
    output:
        unimog = f"{OUTPUTPATH}/unimogs/{{community}}_anno.unimog",
        map = f"{OUTPUTPATH}/unimogs/{{community}}_map.txt"
    conda:
        "../envs/consolidation.yaml"
    threads: 1
    resources:
        mem_mb = lambda wildcards, attempt: attempt * config["consolidation_mem"]
    params:
        pafs=f"{OUTPUTPATH}/tmp_files/minimap/{{community}}/output",
        genomes = lambda wildcards: COMMUNITY_TO_PLASMID[wildcards.community],
        outputpath = OUTPUTPATH,
        pling_root_dir = get_pling_root_dir()
    shell: """
        /opt/gensoft/exe/pling/1.0.3/venv/bin/python3.8  {params.pling_root_dir}/pling/anno_snakemake/consolidation.py {params.pafs} "{params.genomes}" {output.unimog} {output.map}
    """


if config.get("dedup", False):
    rule gen_dedup_input:
        input:
            lambda wildcards: [f"{FASTAPATH}/{genome}{FASTAEXT[genome]}" for genome in COMMUNITY_TO_PLASMID[wildcards.community]]
        output:
            f"{OUTPUTPATH}/tmp_files/community_fastas/{{community}}.fna"
        threads: 1
        resources:
            mem_mb = lambda wildcards, attempt: attempt * 4000
        shell:
            "cat {input} > {output}"

    rule deduplication:
        input:
            fasta = f"{OUTPUTPATH}/tmp_files/community_fastas/{{community}}.fna",
            unimog = f"{OUTPUTPATH}/unimogs/{{community}}_anno.unimog",
            map = f"{OUTPUTPATH}/unimogs/{{community}}_map.txt"
        output:
            nucmer = f"{OUTPUTPATH}/tmp_files/nucmer/{{community}}.nucmer",
            relabelled_unimog = f"{OUTPUTPATH}/unimogs/relabelled/dedup/{{community}}_dedup.unimog",
            dedup_map = f"{OUTPUTPATH}/unimogs/relabelled/dedup/{{community}}_map_dedup.txt"
        conda:
            "../envs/dedup.yaml"
        threads: config["deduplication_threads"]
        resources:
            mem_mb = lambda wildcards, attempt: attempt * config["deduplication_mem"]
        params:
            pafs=f"{OUTPUTPATH}/tmp_files/minimap/{{community}}/output",
            nucmer_threshold=config["dedup_threshold"],
            genomes = lambda wildcards: COMMUNITY_TO_PLASMID[wildcards.community],
            pling_root_dir = get_pling_root_dir()
        shell: """
            /opt/gensoft/exe/pling/1.0.3/venv/bin/python3.8  {params.pling_root_dir}/pling/anno_snakemake/multipartite.py \
                {input.fasta} {input.unimog} {input.map} \
                {params.pafs} {params.nucmer_threshold} "{params.genomes}" \
                {output.nucmer} {output.relabelled_unimog} {output.dedup_map}
        """

rule blocks:
    input:
        lambda wildcards: blocks_input(wildcards.community)
    output:
        relabelled_unimog = f"{OUTPUTPATH}/unimogs/relabelled/blocks/{{community}}_blocks.unimog",
        blocks_map = f"{OUTPUTPATH}/unimogs/relabelled/blocks/{{community}}_map_blocks.txt"
    conda:
        "../envs/blocks.yaml"
    threads: 1
    resources:
        mem_mb = lambda wildcards, attempt: attempt * config["blocks_mem"]
    params:
        genomes = lambda wildcards: COMMUNITY_TO_PLASMID[wildcards.community],
        relabelled_dir = f"{OUTPUTPATH}/unimogs/relabelled/blocks",
        pling_root_dir = get_pling_root_dir()
    shell: """
        /opt/gensoft/exe/pling/1.0.3/venv/bin/python3.8  {params.pling_root_dir}/pling/anno_snakemake/blocks.py \
            {input} \
            "{params.genomes}" {params.relabelled_dir} \
            {output.relabelled_unimog} {output.blocks_map} \
    """
