Skip to content

Commit b9add08

Browse files
committed
Refactor FC histogram plotting func
1 parent 974d61d commit b9add08

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

ezplot/plot_cytometry.py

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
import pandas as pd
77
import seaborn as sns
88

9-
from ezplot.process import combine_rcg, load_fcs
109
from ezplot.style import PALETTE, STYLE
1110

1211

13-
def plot_fc_hist(fig_fp, y_csv_fp, gateline, class_labels, group_labels, xlabel, figsize=(32, 16), palette=PALETTE, rc_params=STYLE):
12+
def plot_fc_hist(fig_fp, y_df, gateline, class_labels, group_labels, xlabel, figsize=(32, 16), palette=PALETTE, rc_params=STYLE):
1413
fig_fp = Path(fig_fp)
1514
fig_fp.parent.mkdir(parents=True, exist_ok=True)
1615
with sns.axes_style("whitegrid"), mpl.rc_context(rc_params):
@@ -28,55 +27,52 @@ def plot_fc_hist(fig_fp, y_csv_fp, gateline, class_labels, group_labels, xlabel,
2827
"legend.fontsize": 36,
2928
},
3029
)
31-
y_df = pd.read_csv(Path(y_csv_fp))
3230
y_df = y_df.loc[(y_df["response"] > 0)]
3331
fg = sns.FacetGrid(y_df, palette=palette, row="group", hue="repeat", aspect=7, height=2)
3432
fg.map_dataframe(sns.kdeplot, x="response", hue="class", fill=True, alpha=0.25, log_scale=True, palette=palette, linewidth=0)
3533
fg.map_dataframe(sns.kdeplot, x="response", hue="class", fill=False, alpha=1, log_scale=True, palette=palette, linewidth=1)
3634
fg.refline(y=0, linewidth=1, linestyle="-", color="#212121", clip_on=False)
3735
fg.refline(x=gateline, color="#212121", clip_on=False)
38-
for g, ax in enumerate(fg.axes.ravel()):
39-
yg_c0_df = y_df.loc[(y_df["group"] == g) & (y_df["class"] == 0)]
40-
yg_c1_df = y_df.loc[(y_df["group"] == g) & (y_df["class"] == 1)]
41-
tot_c0 = len(yg_c0_df)
42-
tot_c1 = len(yg_c1_df)
43-
pos_c0 = np.sum(yg_c0_df["response"] > gateline)
44-
pos_c1 = np.sum(yg_c1_df["response"] > gateline)
45-
pct_c0 = np.around(100 * (pos_c0 / tot_c0), 2)
46-
pct_c1 = np.around(100 * (pos_c1 / tot_c1), 2)
47-
ax.text(1, 0.30, f"{pct_c0}%", color=palette[0], fontsize=36, ha="right", transform=ax.transAxes)
48-
ax.text(1, 0.02, f"{pct_c1}%", color=palette[1], fontsize=36, ha="right", transform=ax.transAxes)
49-
ax.text(0, 0.02, f"{group_labels[g]}", color="#212121", fontsize=36, ha="left", transform=ax.transAxes)
50-
if g >= 2:
51-
ax.text(0, 0.30, "Decoder +", color="#212121", fontsize=36, ha="left", transform=ax.transAxes)
52-
handles = [
53-
mpl.lines.Line2D([], [], color=palette[0], lw=6),
54-
mpl.lines.Line2D([], [], color=palette[1], lw=6),
55-
]
56-
fg.axes.ravel()[0].legend(handles, class_labels, loc=(0, 0.4))
36+
handles = []
37+
for c in range(y_df["class"].nunique()):
38+
for g, ax in enumerate(fg.axes.ravel()):
39+
ycg_df = y_df.loc[(y_df["group"] == g) & (y_df["class"] == c)]
40+
tot_ycg = len(ycg_df)
41+
pos_ycg = np.sum(ycg_df["response"] > gateline)
42+
pct_ycg = np.around(100 * (pos_ycg / tot_ycg), 2)
43+
ax.text(1, c * 0.30 + 0.02, f"{pct_ycg}%", color=palette[c], fontsize=36, ha="right", transform=ax.transAxes)
44+
ax.text(0, 0.02, f"{group_labels[g]}", color="#212121", fontsize=36, ha="left", transform=ax.transAxes)
45+
handles.append(mpl.lines.Line2D([], [], color=palette[c], lw=6))
46+
fg.axes.ravel()[0].legend(handles, class_labels, loc=(0, 0.62))
5747
fg.fig.subplots_adjust(hspace=0)
5848
fg.set_titles("")
59-
fg.set(yticks=[], ylabel="", xlim=(1, 1e7))
49+
fg.set(yticks=[], ylabel="", xlim=(1, None))
6050
fg.set_xlabels(xlabel, fontsize=48, fontweight="bold")
6151
fg.despine(left=True, bottom=True)
6252
fg.fig.savefig(fig_fp)
6353
plt.close("all")
6454

6555

6656
def main():
67-
save_csv_fp = "/home/phuong/data/phd-project/3--antigen/0--K562-fc-staining/y.csv"
68-
data_dp = "/home/phuong/data/phd-project/3--antigen/0--K562-fc-staining/data/"
69-
combine_rcg(data_dp, load_fnc=load_fcs, load_fnc_kwargs={"channel": "FL4_A"}, save_csv_fp=save_csv_fp)
57+
# save_csv_fp = "/home/phuong/data/phd-project/3--antigen/0--K562-fc-staining/y.csv"
58+
# data_dp = "/home/phuong/data/phd-project/3--antigen/0--K562-fc-staining/data/"
59+
# combine_rcg(data_dp, load_fnc=load_fcs, load_fnc_kwargs={"channel": "FL4_A"}, save_csv_fp=save_csv_fp)
7060

71-
# fig_fp = "/home/phuong/data/phd-project/3--antigen/0--K562-fc-staining/y.png"
72-
# y_csv_fp = "/home/phuong/data/phd-project/3--antigen/0--K562-fc-staining/y.csv"
73-
# group_labels = [r"$\mathdefault{Plain\ K562}$", r"$\mathdefault{Constitutive}$", r"$\mathdefault{None\ Input}$", r"$\mathdefault{Sparse\ Input}$", r"$\mathdefault{Dense\ Input}$"]
74-
# class_labels = [r"$\alpha$-CD19 AF647", r"$\alpha$-PSMA APC"]
75-
# palette = ["#8069EC", "#EA822C"]
76-
# xlabel = "Fluorescence (AU)"
77-
# fig_fp = "/home/phuong/data/phd-project/figures/fig_4b.png"
78-
# gateline = 3e3
79-
# plot_fc_hist(fig_fp, y_csv_fp, gateline, class_labels, group_labels, xlabel, figsize=(32, 16), palette=palette)
61+
fig_fp = "/home/phuong/data/phd-project/figures/fig_4b.png"
62+
y_csv_fp = "/home/phuong/data/phd-project/3--antigen/0--K562-fc-staining/y.csv"
63+
y_df = pd.read_csv(y_csv_fp)
64+
group_labels = [
65+
"Plain K562",
66+
"Constitutive",
67+
"Decoder + \nNone Input",
68+
"Decoder + \nSparse Input",
69+
"Decoder + \nDense Input",
70+
]
71+
class_labels = [r"$\alpha$-CD19 AF647", r"$\alpha$-PSMA APC"]
72+
palette = ["#8069EC", "#EA822C"]
73+
xlabel = "Fluorescence (AU)"
74+
gateline = 3e3
75+
plot_fc_hist(fig_fp, y_df, gateline, class_labels, group_labels, xlabel, figsize=(32, 16), palette=palette)
8076

8177

8278
if __name__ == "__main__":

0 commit comments

Comments
 (0)