import sequana
from sequana import snaketools as sm
from sequana.snaketools import SequanaConfig, FileFactory

# This must be defined before the include
configfile: "config.yaml"

cfg = SequanaConfig(config)
expected_output = []


rule kraken_translate:
    input:
        "kraken/kraken.out"
    output:
        summary="kraken/kraken.out.summary",
        #krona="kraken/krona.html"
    run:
        from sequana.kraken import KrakenContaminant
        k = KrakenContaminant(input[0])
        k.kraken_to_krona(mode="adapters")

rule kraken_to_krona:
    input: "kraken/kraken.out.summary"
    output: "kraken/krona.html"
    run:
        shell("ktImportText %s -o %s" % (input, output))


if cfg.paired:
    rule kraken_contaminant:
        input:
            file1=cfg.DATASET[0],
            file2=cfg.DATASET[1]
        output:
            classified="kraken/kraken_classified.fasta",
            unclassified="kraken/kraken_unclassified.fasta",
            kraken="kraken/kraken.out"
        params:
            database = config['kraken']['database']
        threads: 4
        run:
            shell("kraken -db {params.database}  {input.file1} {input.file2} --threads {threads} --quick --classified-out {output.classified} --unclassified-out {output.unclassified} --paired --out {output.kraken}")
else:
    rule kraken_contaminant:
        input:
            file1=cfg.DATASET[0]
        output:
            classified="kraken/kraken_classified.fasta",
            unclassified="kraken/kraken_unclassified.fasta",
            kraken="kraken/kraken.out"
        params:
            database = "/home/cokelaer/Downloads/minikraken_20141208/"
        threads: 4
        run:
            shell("kraken -db {params.database} {input.file1} "
                " --threads {threads} --quick --classified-out "
                " {output.classified} --unclassified-out {output.unclassified}"
                " --paired")

rule all:
    input: "kraken/krona.html",
        #expand("kraken/kraken_{tag}.fasta", tag=["classified", "unclassified"])



