rule freebayes_vcf_filter:
    """
    Variant filter rules dedicated to VCF files generated by freebayes.
    It filters with freebayes quality score, coverage depth, frequency and
    strand ratio.

    Required input:
        - __freebayes_vcf_filter__input: VCF file from freebayes.

    Required output:
        - __freebayes_vcf_filter__output: Filtered VCF file.
        - __freebayes_vcf_filter__csv: CSV file of filtered variants.
        - __freebayes_vcf_filter__html: HTML report.

    Required parameters:
        - __freebayes_vcf_filter__report_dir: Report directory to copy JS/CSS.

    Required configuration:
        .. code-block:: yaml

            freebayes_vcf_filter:
                freebayes_score: 20 # threshold for minimum freebayes quality score.
                frequency: 0.7 # threshold for minimum alternative allele frequency.
                min_depth: 10 # threshold for minimum coverage depth.
                forward_depth: 3 # threshold for minimum coverage depth of forward strand.
                reverse_depth: 3 # threshold for minimum coverage depth of reverse strand.
                strand_ratio: 0.2 # threshold for minimum strand ratio between 0 and 0.5.
    """
    input:
        vcf = __freebayes_vcf_filter__input
    output:
        vcf = __freebayes_vcf_filter__output,
        csv = __freebayes_vcf_filter__csv,
        html = __freebayes_vcf_filter__html
    params:
        filter_dict = config["freebayes_vcf_filter"],
        report_dir = __freebayes_vcf_filter__report_dir
    run:
        from sequana.freebayes_vcf_filter import VCF_freebayes
        from sequana.modules_report.variant_calling import VariantCallingModule
        from sequana.utils import config
        
        v = VCF_freebayes(input["vcf"])
        filter_v = v.filter_vcf(params["filter_dict"])
        filter_v.to_vcf(output["vcf"])
        filter_v.to_csv(output["csv"])
        config.output_dir = params['report_dir']
        VariantCallingModule(filter_v)
