#!/usr/bin/env python3

import json
import click
from snpScore import tableFromVcf
from snpScore import snp_ann_pipe, sample_and_group_for_web


@click.command()
@click.option('--gene_bed',
              type=click.Path(exists=True, dir_okay=False),
              help='gene position bedfile.',
              required=True)
@click.option('--vcf_dir',
              help=('vcf table directory, can be more than one '
                    '[--vcf_dir dir1 --vcf_dir dir2].'),
              type=click.Path(exists=True, file_okay=False),
              multiple=True)
@click.option('--vcf_ann_file',
              help='snp annotation file.',
              type=click.Path(exists=True, dir_okay=False),
              required=True)
@click.option('-p',
              '--parameters',
              help='snpScore parameters json string.',
              required=True,
              type=click.STRING)
@click.option('--outdir',
              type=click.Path(),
              help='output directory.',
              required=True)
@click.option('--logger_file',
              type=click.Path(dir_okay=False),
              help='logger file Path.',
              default=None)
@click.option('--min_depth',
              help='minimal snp depth',
              default=1)
def main(gene_bed, vcf_dir, vcf_ann_file, outdir, parameters, logger_file, min_depth):
    parameters_obj = json.loads(parameters)
    gene_list = parameters_obj.get('gene_id', '')
    genes = ','.join(gene_list)
    chrom = parameters_obj.get('chrom', '')
    chrom_start = parameters_obj.get('chrom_start', '')
    chrom_end = parameters_obj.get('chrom_end', '')
    if chrom:
        position = f'{chrom}:{chrom_start}-{chrom_end}'
    else:
        position = ''
    group_names = parameters_obj.get('group_names')
    sample_list, group_list = sample_and_group_for_web(parameters_obj)

    snp_ann_pipe(gene_bed, vcf_ann_file, outdir, genes, position, vcf_dir,
                 sample_list, group_list, min_depth, outfmt='string',
                 logger_file=logger_file)


if __name__ == "__main__":
    main()
