"""Simple Hail query example."""

import click
import hail as hl
from bokeh.io.export import get_screenshot_as_png
from analysis_runner import output_path


GNOMAD_HGDP_1KG_MT = (
    'gs://gcp-public-data--gnomad/release/3.1/mt/genomes/'
    'gnomad.genomes.v3.1.hgdp_1kg_subset_dense.mt'
)


@click.command()
@click.option('--rerun', help='Whether to overwrite cached files', default=False)
def query(rerun):
    """Query script entry point."""

    hl.init(default_reference='GRCh38')

    sample_qc_path = output_path('sample_qc.mt')
    if rerun or not hl.hadoop_exists(sample_qc_path):
        mt = hl.read_matrix_table(GNOMAD_HGDP_1KG_MT)
        mt = mt.head(100, n_cols=100)
        mt_qc = hl.sample_qc(mt)
        mt_qc.write(sample_qc_path)
    mt_qc = hl.read_matrix_table(sample_qc_path)

    plot_filename = output_path('call_rate_plot.png', 'web')
    if rerun or not hl.hadoop_exists(plot_filename):
        call_rate_plot = hl.plot.histogram(
            mt_qc.sample_qc.call_rate, range=(0, 1), legend='Call rate'
        )
        with hl.hadoop_open(plot_filename, 'wb') as f:
            get_screenshot_as_png(call_rate_plot).save(f, format='PNG')


if __name__ == '__main__':
    query()  # pylint: disable=no-value-for-parameter

[(15, 'click.command', 'click.command', False, 'import click\n'), (16, 'click.option', 'click.option', False, 'import click\n'), (20, 'hail.init', 'hl.init', True, 'import hail as hl\n'), (22, 'analysis_runner.output_path', 'output_path', False, 'from analysis_runner import output_path\n'), (28, 'hail.read_matrix_table', 'hl.read_matrix_table', True, 'import hail as hl\n'), (30, 'analysis_runner.output_path', 'output_path', False, 'from analysis_runner import output_path\n'), (24, 'hail.read_matrix_table', 'hl.read_matrix_table', True, 'import hail as hl\n'), (26, 'hail.sample_qc', 'hl.sample_qc', True, 'import hail as hl\n'), (32, 'hail.plot.histogram', 'hl.plot.histogram', True, 'import hail as hl\n'), (23, 'hail.hadoop_exists', 'hl.hadoop_exists', True, 'import hail as hl\n'), (31, 'hail.hadoop_exists', 'hl.hadoop_exists', True, 'import hail as hl\n'), (35, 'hail.hadoop_open', 'hl.hadoop_open', True, 'import hail as hl\n'), (36, 'bokeh.io.export.get_screenshot_as_png', 'get_screenshot_as_png', False, 'from bokeh.io.export import get_screenshot_as_png\n')]