rule mapping_analysis:
    """
    Create a HTML report of sequencing coverage after bam processing. This
    report is dedicated for variant calling and de novo assembly pipeline.

    Required input:
        __mapping_analysis__input: bed file from bedtools genomecov
        __mapping_analysis__bam_input: bam file
        __mapping_analysis__quast_input: html file from quast (optional*)
        __mapping_analysis__ann_input: genbank annotation file (optional*)

    Required output:
        __mapping_analysis__output: html file
        __mapping_analysis__json: json file

    Required parameter:
        config["mapping_analysis"]["k"]: number of gaussians predicted in the
                gaussian pool.
        config["mapping_analysis"]["window_size"]: windows size for running
                median. This integer must be an odd number.
        config["mapping_analysis"]["high_threshold"]: high zscore threshold
        config["mapping_analysis"]["low_threshold"]: low zscore threshold
        config["mapping_analysis"]["circular"]: boolean if the genome is circular

    Contributors:
        Sequana consortium

    *: If you don't need the optional input. You must set the variable as an
       empty list. (i.e.:  __mapping_analysis__quast_input = [])
    """
    input:
        bed = __mapping_analysis__input,
        bam = __mapping_analysis__bam_input,
        quast = __mapping_analysis__quast_input,
        ann = __mapping_analysis__ann_input
    output:
        __mapping_analysis__output
    params:
        sample_dict = __mapping_analysis__sample_dict,
        outdir = __mapping_analysis__directory,
        sample = __mapping_analysis__sample,
        k = config["mapping_analysis"]["k"],
        size = config["mapping_analysis"]["window_size"],
        high = config["mapping_analysis"]["high_threshold"],
        low = config["mapping_analysis"]["low_threshold"],
        circular = config["mapping_analysis"]["circular"]
    run:
        import os

        from sequana import bedtools
        from sequana import BAM
        from sequana.tools import genbank_features_parser

        json_dict = {}

        bam = BAM(input.bam)
        json_dict["bam_stats"] = bam.get_stats()
