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

    Required input:
        - __joint_freebayes_vcf_filter__input: VCF file from freebayes

    Required output:
        - __joint_freebayes_vcf_filter__output: Filtered VCF file.
        - __joint_freebayes_vcf_filter__html: HTML report of detected variants.

    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.
    """
    input:
        vcf = __joint_freebayes_vcf_filter__input
    output:
        vcf = __joint_freebayes_vcf_filter__output,
        html = __joint_freebayes_vcf_filter__html
    params:
        filter_dict = config["joint_freebayes_vcf_filter"],
        report_dir = __joint_freebayes_vcf_filter__report_dir
    run:
        from sequana.freebayes_vcf_filter import VCF_freebayes
        from sequana.modules_report.joint_calling import JointCallingModule
        from sequana.utils import config
        
        v = VCF_freebayes(input['vcf'])
        filter_v = v.filter_vcf(params['filter_dict'])
        filter_v.to_vcf(output['vcf'])
        config.output_dir = params['report_dir']
        JointCallingModule(filter_v)
