Skip to content

Commit 500a203

Browse files
author
artuurC
committed
Add first version of FlowSOMmary
1 parent effe116 commit 500a203

File tree

1 file changed

+134
-3
lines changed

1 file changed

+134
-3
lines changed

src/FlowSOM/plotting/plot_functions.py

Lines changed: 134 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import numpy as np
22
import matplotlib.pyplot as plt
33
import pandas as pd
4-
from scanpy.plotting import embedding
5-
from scanpy.tools import tsne, pca
4+
import seaborn as sns
5+
import matplotlib.colors
6+
import matplotlib.backends.backend_pdf
67

78
import matplotlib.colors
89

@@ -11,6 +12,8 @@
1112
from matplotlib import gridspec
1213
from ._plot_helper_functions import *
1314
from scipy.stats import gaussian_kde
15+
from scanpy.tools import umap
16+
from scanpy.preprocessing import neighbors
1417

1518

1619
def plot_2D_scatters(
@@ -396,4 +399,132 @@ def FlowSOMmary(fsom, plot_file="./FlowSOMmary.pdf"):
396399
:param plot_file: File name of the plot
397400
:type plot_file: str
398401
"""
399-
pass
402+
# Initializing
403+
metacluster_present = "cluster_data" in fsom.mudata.mod.keys()
404+
if metacluster_present:
405+
mfis = fsom.get_cluster_data().uns["metacluster_MFIs"]
406+
metaclusters = fsom.get_cell_data().obs["metaclustering"]
407+
n_metaclusters = fsom.get_cell_data().uns["n_metaclusters"]
408+
clusters = fsom.get_cell_data().obs["clustering"]
409+
n_clusters = np.arange(fsom.get_cell_data().uns["n_nodes"])
410+
file_present = False
411+
plot_dict = dict()
412+
413+
# Plot fsom trees and grids
414+
for view in ["MST", "grid"]:
415+
if metacluster_present:
416+
plot_dict["stars_" + view] = plot_stars(
417+
fsom,
418+
background_values=fsom.get_cluster_data().obs.metaclustering,
419+
title="FlowSOM " + view,
420+
view=view,
421+
)
422+
423+
plot_dict["mcl_labels_" + view] = plot_variable(
424+
fsom,
425+
variable=fsom.get_cluster_data().obs.metaclustering,
426+
equal_node_size=True,
427+
view=view,
428+
labels=fsom.get_cluster_data().obs.metaclustering,
429+
cmap=gg_color_hue(),
430+
title="FlowSOM Metaclusters",
431+
)
432+
433+
plot_dict["cl_labels_" + view] = plot_variable(
434+
fsom,
435+
variable=fsom.get_cluster_data().obs.metaclustering,
436+
equal_node_size=True,
437+
view=view,
438+
labels=np.arange(fsom.get_cell_data().uns["n_nodes"]),
439+
cmap=gg_color_hue(),
440+
title="FlowSOM Clusters",
441+
)
442+
else:
443+
plot_dict["stars_" + view] = plot_stars(fsom, title="FlowSOM" + view, view=view)
444+
plot_dict["labels_" + view] = plot_numbers(fsom, view=view, title="FlowSOM Clusters", equal_node_size=True)
445+
446+
# Plot Markers
447+
ref_markers_bool = fsom.get_cell_data().var["cols_used"]
448+
ref_markers = fsom.get_cell_data().var_names[ref_markers_bool]
449+
for marker in ref_markers:
450+
plot_dict["marker_" + marker] = plot_marker(
451+
fsom,
452+
[marker],
453+
ref_markers=ref_markers,
454+
equal_node_size=True,
455+
title=list(get_markers(fsom, [marker]).keys())[0],
456+
)
457+
458+
# File distribution
459+
if file_present:
460+
pass
461+
462+
# t-SNE
463+
subset_fsom = fsom.get_cell_data()[
464+
np.random.choice(range(fsom.get_cell_data().shape[0]), 5000, replace=False),
465+
fsom.get_cell_data().var_names[ref_markers_bool],
466+
]
467+
subset_fsom
468+
neighbors(subset_fsom)
469+
umap(subset_fsom)
470+
471+
for marker in ref_markers:
472+
fig, ax = plt.subplots()
473+
ax.scatter(
474+
x=subset_fsom.obsm["X_umap"][:, 0],
475+
y=subset_fsom.obsm["X_umap"][:, 1],
476+
c=subset_fsom.to_df().loc[:, marker],
477+
)
478+
ax.set_title("UMAP: " + list(get_markers(fsom, [marker]).keys())[0])
479+
plot_dict["umap_" + marker] = fig
480+
if metacluster_present:
481+
subset_fsom.obs["metaclustering"] = subset_fsom.obs["metaclustering"].astype(str)
482+
colors = {
483+
i: j
484+
for i, j in zip(
485+
np.unique(subset_fsom.obs["metaclustering"]),
486+
gg_color_hue()(np.linspace(0, 1, n_metaclusters)),
487+
)
488+
}
489+
fig, ax = plt.subplots()
490+
ax.scatter(
491+
x=subset_fsom.obsm["X_umap"][:, 0],
492+
y=subset_fsom.obsm["X_umap"][:, 1],
493+
c=subset_fsom.obs["metaclustering"].map(colors),
494+
)
495+
ax.set_title("UMAP: metaclustering")
496+
plot_dict["umap_metaclustering"] = fig
497+
498+
# Heatmap
499+
data_heatmap = fsom.get_cluster_data().uns["metacluster_MFIs"]
500+
data_heatmap.columns = fsom.get_cluster_data().var_names
501+
data_heatmap_subset = data_heatmap.loc[:, fsom.get_cell_data().var.cols_used.to_list()]
502+
plot_dict["heatmap"] = sns.clustermap(data_heatmap_subset, z_score=1, annot=data_heatmap_subset, cbar=True).fig
503+
504+
# Tables
505+
table1 = pd.DataFrame(
506+
{
507+
"Total number of cells": [fsom.get_cell_data().shape[0]],
508+
"Total number of metaclusters": [n_metaclusters],
509+
"Total number of clusters": len(n_clusters),
510+
"Markers used for FlowSOM": ", ".join(
511+
fsom.get_cell_data().var["pretty_colnames"][ref_markers_bool].to_list()
512+
),
513+
}
514+
).transpose()
515+
fig, ax = plt.subplots(1, 1)
516+
ax.axis("tight")
517+
ax.axis("off")
518+
ax.table(
519+
cellText=table1.values,
520+
rowLabels=table1.index,
521+
colLabels=["FlowSOMmary"],
522+
loc="center",
523+
)
524+
plot_dict["table1"] = fig
525+
526+
# Plot
527+
pdf = matplotlib.backends.backend_pdf.PdfPages(plot_file)
528+
for fig in plot_dict.keys():
529+
pdf.savefig(plot_dict[fig])
530+
pdf.close()

0 commit comments

Comments
 (0)