Skip to content

Commit

Permalink
Merge pull request #538 from malariagen/536-z-score-fst
Browse files Browse the repository at this point in the history
Option to annotate upper-right corner of Fst plots with Z scores.
  • Loading branch information
leehart authored Jun 21, 2024
2 parents 54e7a45 + 1efc4bc commit 6ebd049
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
10 changes: 7 additions & 3 deletions malariagen_data/anoph/fst.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def pairwise_average_fst(
def plot_pairwise_average_fst(
self,
fst_df: fst_params.df_pairwise_fst,
annotate_se: bool = False,
annotation: fst_params.annotation = None,
zmin: Optional[plotly_params.zmin] = 0.0,
zmax: Optional[plotly_params.zmax] = None,
text_auto: plotly_params.text_auto = ".3f",
Expand All @@ -500,9 +500,13 @@ def plot_pairwise_average_fst(
index = fst_df.iloc[index_key]["cohort1"]
col = fst_df.iloc[index_key]["cohort2"]
fst = fst_df.iloc[index_key]["fst"]
if annotate_se is True:
fig_df[index][col] = fst
if annotation == "standard error":
se = fst_df.iloc[index_key]["se"]
fig_df.loc[index, col] = se
fig_df[col][index] = se
elif annotation == "Z score":
zs = fst_df.iloc[index_key]["fst"] / fst_df.iloc[index_key]["se"]
fig_df[col][index] = zs
else:
fig_df.loc[index, col] = fst

Expand Down
11 changes: 10 additions & 1 deletion malariagen_data/anoph/fst_params.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Parameter definitions for Fst functions."""

from typing import Optional
from typing import Optional, Literal

import pandas as pd
from typing_extensions import Annotated, TypeAlias
Expand All @@ -22,3 +22,12 @@
A dataframe of pairwise Fst and standard error values.
""",
]

annotation: TypeAlias = Annotated[
Optional[Literal["standard error", "Z score"]],
"""
How to annotate the upper-right corner of the plot. Default behaviour (None) is using Fst, other options
are using the standard error (if annotation is 'standard error') or the Z score of the two
cohorts being the same (if annotation is 'Z score').
""",
]
4 changes: 2 additions & 2 deletions notebooks/plot_pairwise_average_fst.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"metadata": {},
"outputs": [],
"source": [
"ag3.plot_pairwise_average_fst(pairwise_fst_df, annotate_se=True)"
"ag3.plot_pairwise_average_fst(pairwise_fst_df, annotation=\"standard error\")"
]
},
{
Expand All @@ -104,7 +104,7 @@
"metadata": {},
"outputs": [],
"source": [
"ag3.plot_pairwise_average_fst(pairwise_fst_df, annotate_se=False)"
"ag3.plot_pairwise_average_fst(pairwise_fst_df, annotation=\"Z score\")"
]
},
{
Expand Down
6 changes: 5 additions & 1 deletion tests/anoph/test_fst.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ def check_pairwise_average_fst(api: AnophelesFstAnalysis, fst_params):
if len(fst_df) > 0:
fig = api.plot_pairwise_average_fst(fst_df, show=False)
assert isinstance(fig, go.Figure)
fig = api.plot_pairwise_average_fst(fst_df, annotate_se=True, show=False)
fig = api.plot_pairwise_average_fst(
fst_df, annotation="standard error", show=False
)
assert isinstance(fig, go.Figure)
fig = api.plot_pairwise_average_fst(fst_df, annotation="Z score", show=False)
assert isinstance(fig, go.Figure)


Expand Down

0 comments on commit 6ebd049

Please sign in to comment.