Skip to content

Commit 472d074

Browse files
committed
fix: raise ValueError when window_size exceeds available SNPs in fst_gwss
Signed-off-by: Suhrid Marwah <suhridmarwah07@gmail.com>
1 parent 79c38ce commit 472d074

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

malariagen_data/anoph/fst.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ def _fst_gwss(
8888
fst = np.clip(fst, a_min=clip_min, a_max=1)
8989
x = allel.moving_statistic(pos, statistic=np.mean, size=window_size)
9090

91+
if len(x) == 0:
92+
raise ValueError(
93+
f"No Fst windows could be computed: window_size={window_size!r} is "
94+
f"larger than the number of SNP sites available ({len(pos)}) in the "
95+
"selected region. Try reducing window_size or selecting a larger region."
96+
)
97+
9198
results = dict(x=x, fst=fst)
9299

93100
return results

tests/anoph/test_fst.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ def test_fst_gwss(fixture, api: AnophelesFstAnalysis):
140140
assert isinstance(fig, bokeh.models.GridPlot)
141141

142142

143+
@parametrize_with_cases("fixture,api", cases=".")
144+
def test_fst_gwss_window_size_too_large(fixture, api: AnophelesFstAnalysis):
145+
# Use a window_size larger than the number of available SNPs to trigger the
146+
# ValueError guard added in _fst_gwss.
147+
all_sample_sets = api.sample_sets()["sample_set"].to_list()
148+
all_countries = api.sample_metadata()["country"].dropna().unique().tolist()
149+
countries = random.sample(all_countries, 2)
150+
cohort1_query = f"country == {countries[0]!r}"
151+
cohort2_query = f"country == {countries[1]!r}"
152+
with pytest.raises(ValueError, match="window_size"):
153+
api.fst_gwss(
154+
contig=random.choice(api.contigs),
155+
sample_sets=all_sample_sets,
156+
cohort1_query=cohort1_query,
157+
cohort2_query=cohort2_query,
158+
site_mask=random.choice(api.site_mask_ids),
159+
window_size=10_000_000, # far larger than any fixture SNP count
160+
min_cohort_size=1,
161+
)
162+
163+
143164
@parametrize_with_cases("fixture,api", cases=".")
144165
def test_average_fst(fixture, api: AnophelesFstAnalysis):
145166
# Set up test parameters.

0 commit comments

Comments
 (0)