#!/usr/bin/python3 -u
"""
Exercise the GenotypeIndexer, the module used by tensorqtl to index
and fetch cis-window genotypes during QTL mapping.
"""

import numpy as np
import pandas as pd

from qtl.genotype import GenotypeIndexer

genotype_df = pd.DataFrame(
    [[0, 1], [1, 1], [2, 0]],
    columns=["s1", "s2"],
    index=["rs1", "rs2", "rs3"],
)
variant_df = pd.DataFrame(
    {"chrom": ["1", "1", "2"], "pos": [100, 200, 300]},
    index=["rs1", "rs2", "rs3"],
)

gi = GenotypeIndexer(genotype_df, variant_df)
assert list(gi.sample_ids) == ["s1", "s2"]

# Fetch by variant id
g = gi.get_genotypes(["rs1", "rs3"])
np.testing.assert_array_equal(g, [[0, 1], [2, 0]])

# Fetch a cis window on a chromosome
w = gi.get_genotype_window("1:50-250")
np.testing.assert_array_equal(w.values, [[0, 1], [1, 1]])

# Restricted sample subset
gi2 = GenotypeIndexer(genotype_df, variant_df, sample_ids=["s2"])
np.testing.assert_array_equal(gi2.get_genotypes(["rs2"]), [[1]])

print("pyqtl (python3-qtl) run-unit-test: OK")
