"""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

[((295, 310), 'click.command', 'click.command', ([], {}), '()\n', (308, 310), False, 'import click\n'), ((312, 397), 'click.option', 'click.option', (['"""--rerun"""'], {'help': '"""Whether to overwrite cached files"""', 'default': '(False)'}), "('--rerun', help='Whether to overwrite cached files', default=False\n    )\n", (324, 397), False, 'import click\n'), ((452, 487), 'hail.init', 'hl.init', ([], {'default_reference': '"""GRCh38"""'}), "(default_reference='GRCh38')\n", (459, 487), True, 'import hail as hl\n'), ((510, 537), 'analysis_runner.output_path', 'output_path', (['"""sample_qc.mt"""'], {}), "('sample_qc.mt')\n", (521, 537), False, 'from analysis_runner import output_path\n'), ((765, 801), 'hail.read_matrix_table', 'hl.read_matrix_table', (['sample_qc_path'], {}), '(sample_qc_path)\n', (785, 801), True, 'import hail as hl\n'), ((823, 863), 'analysis_runner.output_path', 'output_path', (['"""call_rate_plot.png"""', '"""web"""'], {}), "('call_rate_plot.png', 'web')\n", (834, 863), False, 'from analysis_runner import output_path\n'), ((605, 645), 'hail.read_matrix_table', 'hl.read_matrix_table', (['GNOMAD_HGDP_1KG_MT'], {}), '(GNOMAD_HGDP_1KG_MT)\n', (625, 645), True, 'import hail as hl\n'), ((700, 716), 'hail.sample_qc', 'hl.sample_qc', (['mt'], {}), '(mt)\n', (712, 716), True, 'import hail as hl\n'), ((942, 1020), 'hail.plot.histogram', 'hl.plot.histogram', (['mt_qc.sample_qc.call_rate'], {'range': '(0, 1)', 'legend': '"""Call rate"""'}), "(mt_qc.sample_qc.call_rate, range=(0, 1), legend='Call rate')\n", (959, 1020), True, 'import hail as hl\n'), ((558, 590), 'hail.hadoop_exists', 'hl.hadoop_exists', (['sample_qc_path'], {}), '(sample_qc_path)\n', (574, 590), True, 'import hail as hl\n'), ((884, 915), 'hail.hadoop_exists', 'hl.hadoop_exists', (['plot_filename'], {}), '(plot_filename)\n', (900, 915), True, 'import hail as hl\n'), ((1056, 1091), 'hail.hadoop_open', 'hl.hadoop_open', (['plot_filename', '"""wb"""'], {}), "(plot_filename, 'wb')\n", (1070, 1091), True, 'import hail as hl\n'), ((1110, 1147), 'bokeh.io.export.get_screenshot_as_png', 'get_screenshot_as_png', (['call_rate_plot'], {}), '(call_rate_plot)\n', (1131, 1147), False, 'from bokeh.io.export import get_screenshot_as_png\n')]