show top ranked genes

Show a heatmap of top ranked genes using P values.

Parameters

number_of_rows: int Number of rows to show.


cluster_key: str Obs value to cluster genes by.


method: str Statistical method to use.


Web view

show_top_ranked_genes_screenshot show_top_ranked_genes_screenshot

Python equivalent

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()