This example shows how to visualize a matrix of gene expression data saved as a tab-separated-file (e.g. rc_two_cats.txt) using the Clustergrammer interactive widget (see the Clustergrammer Jupyter Widget Documentation for more information).
In [1]:
# import widget classes and instantiate Network instance
from clustergrammer_widget import *
net = Network(clustergrammer_widget)
In [2]:
# load matrix file
net.load_file('rc_two_cats.txt')
# cluster using default parameters
net.cluster(enrichrgram=True)
# make interactive widget
net.widget()
Clustergrammer widget has biology-specific features that are activated when rows are given as official gene symbols:
Clustergrammer can also be used as a general purpose Pandas dataframe viewer. This example generates a dataframe with random data and visualizes it with Clustergrammer widget:
In [3]:
import numpy as np
import pandas as pd
# generate random matrix
num_rows = 500
num_cols = 10
np.random.seed(seed=100)
mat = np.random.rand(num_rows, num_cols)
# make row and col labels
rows = range(num_rows)
cols = range(num_cols)
rows = [str(i) for i in rows]
cols = [str(i) for i in cols]
# make dataframe
df = pd.DataFrame(data=mat, columns=cols, index=rows)
Initialize the network object, load the dataframe, hierarchically cluster the rows and columns using default parameters, and finally visualize using clustergrammer_widget.
In [4]:
net.load_df(df)
net.cluster(enrichrgram=False)
net.widget()
Clustergrammer widget is built using the ipywidgets framework (using the cookie cutter template) and can be installed (with pip) and enabled using the following commands:
pip install clustergrammer_widget
jupyter nbextension enable --py --sys-prefix widgetsnbextension
jupyter nbextension enable --py --sys-prefix clustergrammer_widget
See the documentation and clustergrammer_widget GitHub for more information.
Interactive widgets can also be rendered using Jupyter's nbviewer by using the 'Save Notebook with Widgets' action from the Widgets menu in the notebook (see ipywidgets documents). This notebook is being rendered by nbviewer using the Github repo.
The Clustergrammer project can also be used through: