cluster_key: str
Obs value to cluster genes by.
method: str
Statistical method to use.
import scanpy as sc
import pandas as pd
import numpy as np
import plotly.graph_objects as go
cluster_name = "leiden"
method = "wilcoxon"
n_rows = 10
sc.tl.rank_genes_groups(adata, groupby=cluster_name, method=method, key_added=method)
names = pd.DataFrame(adata.uns[method]['names']).head(n_rows)
pvalues = pd.DataFrame(adata.uns[method]['pvals']).head(n_rows)
clusters = names.columns
#reshape for heatmap. Heatmap plots from botttom up so reverse
names = reversed(names.values.reshape(n_rows,len(clusters)))
pvalues = reversed(pvalues.values.reshape(n_rows, len(clusters)))
fig = go.Figure(data=go.Heatmap(
z=pvalues,
text=names,
x=clusters,
y=np.arange(0, n_rows - 1),
hovertemplate = "%{text}: <br>P score: %{z} </br> Cluster: %{x}",
texttemplate="%{text}",
textfont={"size":18}))
fig.update_layout(title="Top ranked genes with p values",
yaxis={"title": 'Row'},
xaxis={"title": cluster_name})
fig.show()