Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions grapp/assoc/bolt_inf_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,10 @@ def _locate(m: int) -> Tuple[Any, BoltVariantStatsArray, int]:
pos = ops._local_idx_to_pos[chrom][int(arr.local_idx[j])]
grammar_score = float(grammar_scores_by_chrom[chrom][pos])
x_norm2 = float(arr.x_norm2[j])
if x_norm2 <= 0.0:
# Mirrors BoltVariantStats.is_model_variant, whose other
# conditions imply x_norm2 > 0.
continue
retro_stat = (grammar_score**2) / all_hinv_norm2 / x_norm2 * float(ops.dim)
if retro_stat < 5.0:
# Build the per-variant object only for the selected SNP.
Expand Down
23 changes: 18 additions & 5 deletions grapp/assoc/bolt_lmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ def bolt_lmm_inf(
sample_filter = None
y_miss = np.isnan(_y_arr)
if y_miss.any():
if covariates.covar_cols or covariates.q_covar_cols or covariates.cindep != 1:
raise NotImplementedError(
"missing phenotypes are only supported for the intercept-only model"
)
nm = np.flatnonzero(~y_miss)
logger.info(
"Dropping %d/%d individuals with missing phenotype; Nused=%d",
Expand All @@ -152,7 +148,24 @@ def bolt_lmm_inf(
nm.size,
)
_y_arr = _y_arr[nm]
covariates = CovariateBasis.intercept_only(nm.size)
# Restrict the covariate basis to the retained individuals. For the
# intercept-only model this is just a smaller all-ones basis; with real
# covariates we re-orthonormalize basis[nm, :] via from_matrix (its SVD
# preserves the covariate column span and drops any column that becomes
# constant/collinear on the subset), matching C++ BOLT's approach.
if (
covariates.cindep == 1
and not covariates.covar_cols
and not covariates.q_covar_cols
):
covariates = CovariateBasis.intercept_only(nm.size)
else:
covariates = CovariateBasis.from_matrix(
covariates.basis[nm, :],
covar_cols=covariates.covar_cols,
q_covar_cols=covariates.q_covar_cols,
covar_max_levels=covariates.covar_max_levels,
)
sample_filter = nm.tolist()

y = _y_arr
Expand Down
2 changes: 2 additions & 0 deletions grapp/grg_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ def matmul(
init=mm_init,
miss=mm_miss,
)
if miss is not None and mm_miss is not miss:
miss[...] = cupy.asnumpy(mm_miss).astype(miss.dtype, copy=False)
cupy.cuda.Device().synchronize()
result = cupy.asnumpy(result)
else:
Expand Down
31 changes: 29 additions & 2 deletions test/assoc/test_bolt_lmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def setUpClass(cls):
cls.chrom_grgs = list(zip(CHROMS, cls.grgs))
cls.n = cls.grgs[0].num_individuals

def _run_against_truth(self, pheno_file, truth_file, expected_missing):
def _run_against_truth(self, pheno_file, truth_file, expected_missing, cov=None):
# read_pheno maps the NA missing token to NaN; the driver drops those
# individuals (Nused = n - expected_missing) and rebuilds the intercept basis.
y = read_pheno(os.path.join(INPUT_DIR, pheno_file))
Expand All @@ -67,7 +67,8 @@ def _run_against_truth(self, pheno_file, truth_file, expected_missing):
expected_missing,
f"{pheno_file}: expected {expected_missing} missing, got {int(np.isnan(y).sum())}",
)
cov = CovariateBasis.intercept_only(self.n)
if cov is None:
cov = CovariateBasis.intercept_only(self.n)

fit, cal, _, stats = bolt_lmm_inf(
self.chrom_grgs,
Expand Down Expand Up @@ -118,6 +119,32 @@ def test_missing(self):
"bolt.miss.pheno.txt", "bolt.miss.truth.tsv", expected_missing=55
)

def test_missing_with_real_covar_runs(self):
# End-to-end smoke for the bolt runs with covariate and missingness
y = read_pheno(os.path.join(INPUT_DIR, "bolt.miss.pheno.txt"))
self.assertEqual(int(np.isnan(y).sum()), 55)
rng = np.random.default_rng(SEED)
C_full = np.column_stack([np.ones(self.n), rng.standard_normal((self.n, 2))])
cov = CovariateBasis.from_matrix(
C_full, covar_cols=(), q_covar_cols=("x", "y"), covar_max_levels=10
)
self.assertEqual(cov.cindep, 3)

fit, cal, _, stats = bolt_lmm_inf(self.chrom_grgs, y, cov, seed=SEED, threads=1)
df = lmm_inf_stats_to_dataframe(stats, self.chrom_grgs)

self.assertGreater(len(df), 0)
beta = df["BETA"].astype(float).to_numpy()
chisq = df["CHISQ_BOLT_LMM_INF"].astype(float).to_numpy()
self.assertTrue(
np.isfinite(beta).all(), "non-finite BETA with covariates+missing"
)
self.assertTrue(
np.isfinite(chisq).all(), "non-finite CHISQ with covariates+missing"
)
self.assertTrue(0.0 <= fit.h2 <= 1.0, f"h2 out of range: {fit.h2}")
self.assertGreater(cal.factor, 0.0, f"calibration not positive: {cal.factor}")


if __name__ == "__main__":
unittest.main()
Loading