Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better formatted error message for ancestral alleles #887

Merged
merged 1 commit into from
Jan 23, 2024
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
20 changes: 9 additions & 11 deletions tests/test_sgkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ def test_ancestral_missingness(tmp_path):
ancestral_allele = ds.variant_ancestral_allele.values
ancestral_allele[0] = "N"
ancestral_allele[11] = "-"
ancestral_allele[12] = "💩"
ancestral_allele[15] = "💩"
ds = ds.drop_vars(["variant_ancestral_allele"])
sgkit.save_dataset(ds, str(zarr_path) + ".tmp")
Expand All @@ -538,19 +539,16 @@ def test_ancestral_missingness(tmp_path):
)
ds = sgkit.load_dataset(str(zarr_path) + ".tmp")
sd = tsinfer.SgkitSampleData(str(zarr_path) + ".tmp")
with pytest.warns(UserWarning, match="The following alleles were not found"):
with pytest.warns(
UserWarning,
match=r"not found in the variant_allele array for the 4 [\s\S]*'💩': 2",
):
inf_ts = tsinfer.infer(sd)
for i, (
inf_var,
var,
) in enumerate(zip(inf_ts.variants(), ts.variants())):
assert inf_var.site.ancestral_state == var.site.ancestral_state or i in [
0,
11,
15,
]
if i in [0, 11, 15]:
for i, (inf_var, var) in enumerate(zip(inf_ts.variants(), ts.variants())):
if i in [0, 11, 12, 15]:
assert inf_var.site.metadata == {"inference_type": "parsimony"}
else:
assert inf_var.site.ancestral_state == var.site.ancestral_state


@pytest.mark.skipif(sys.platform == "win32", reason="File permission errors on Windows")
Expand Down
17 changes: 13 additions & 4 deletions tsinfer/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2405,11 +2405,20 @@ def sites_ancestral_allele(self):
except IndexError:
unknown_alleles[allele] += 1
ret[i] = allele_index
if sum(unknown_alleles.values()) > 0:
tot = sum(unknown_alleles.values())
if tot > 0:
num_sites = len(string_allele)
frac_bad = tot / num_sites
frac_bad_per_type = [v / num_sites for v in unknown_alleles.values()]
summarise_unknown = [
f"'{k}': {v} ({frac * 100:.2f}% of sites)" # Summarise per allele type
for (k, v), frac in zip(unknown_alleles.items(), frac_bad_per_type)
]
warnings.warn(
"The following alleles were not found in the variant_allele array "
"and will be treated as unknown:\n"
f"{unknown_alleles}"
"An ancestral allele was not found in the variant_allele array for "
+ f"the {tot} sites ({frac_bad * 100 :.2f}%) listed below. "
+ "They will be treated as of unknown ancestral state:\n "
+ "\n ".join(summarise_unknown)
)
return ret

Expand Down