"""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, 1, 15, 16), 'click.command', 'click.command', ({}, {}), '()', False, 'import click\n'), ((16, 1, 16, 81), 'click.option', 'click.option', (), '', False, 'import click\n'), ((20, 4, 20, 39), 'hail.init', 'hl.init', (), '', True, 'import hail as hl\n'), ((22, 21, 22, 48), 'analysis_runner.output_path', 'output_path', ({(22, 33, 22, 47): '"""sample_qc.mt"""'}, {}), "('sample_qc.mt')", False, 'from analysis_runner import output_path\n'), ((28, 12, 28, 48), 'hail.read_matrix_table', 'hl.read_matrix_table', ({(28, 33, 28, 47): 'sample_qc_path'}, {}), '(sample_qc_path)', True, 'import hail as hl\n'), ((30, 20, 30, 60), 'analysis_runner.output_path', 'output_path', ({(30, 32, 30, 52): '"""call_rate_plot.png"""', (30, 54, 30, 59): '"""web"""'}, {}), "('call_rate_plot.png', 'web')", False, 'from analysis_runner import output_path\n'), ((24, 13, 24, 53), 'hail.read_matrix_table', 'hl.read_matrix_table', ({(24, 34, 24, 52): 'GNOMAD_HGDP_1KG_MT'}, {}), '(GNOMAD_HGDP_1KG_MT)', True, 'import hail as hl\n'), ((26, 16, 26, 32), 'hail.sample_qc', 'hl.sample_qc', ({(26, 29, 26, 31): 'mt'}, {}), '(mt)', True, 'import hail as hl\n'), ((32, 25, 34, 9), 'hail.plot.histogram', 'hl.plot.histogram', (), '', True, 'import hail as hl\n'), ((23, 20, 23, 52), 'hail.hadoop_exists', 'hl.hadoop_exists', ({(23, 37, 23, 51): 'sample_qc_path'}, {}), '(sample_qc_path)', True, 'import hail as hl\n'), ((31, 20, 31, 51), 'hail.hadoop_exists', 'hl.hadoop_exists', ({(31, 37, 31, 50): 'plot_filename'}, {}), '(plot_filename)', True, 'import hail as hl\n'), ((35, 13, 35, 48), 'hail.hadoop_open', 'hl.hadoop_open', ({(35, 28, 35, 41): 'plot_filename', (35, 43, 35, 47): '"""wb"""'}, {}), "(plot_filename, 'wb')", True, 'import hail as hl\n'), ((36, 12, 36, 49), 'bokeh.io.export.get_screenshot_as_png', 'get_screenshot_as_png', ({(36, 34, 36, 48): 'call_rate_plot'}, {}), '(call_rate_plot)', False, 'from bokeh.io.export import get_screenshot_as_png\n')]