Clustered heatmaps
Last updated
Last updated
The required packages for this section are pandas and seaborn. These can be installed with the following command in the command window (Windows) / terminal (Mac).
Like in the other sections we will use the lipidomics demo dataset:
The first step is to normalise the data such that the features (lipids) have zero mean and unit variance, this can easily be done with the StandardScaler from sklearn. By indexing the dataframe with df.iloc[:,1:] we'll select all the data in de dataframe, except for the first column, which contains the labels:
To obtain a clustered heatmap, we can sue the clustermap function from seaborn:
We can save the heatmap to a file with:
Note: depending on the size of the dataset, you may get a warning stating that "Installing fastcluster may give better performance". This warning can be ignored, or alternatively if you do experience the clustering to be too slow you can install the fastcluster package with "pip install fastcluster".
We can also add an extra color map to indicate the Labels of the samples:
A more compact and visually more interesting heatmap can be obtained by filtering for more interesting lipid species, like for example species that are significantly different between the groups. Here we will do an ANOVA test and only keep species with p < 0.0001.
We adapt our previous ANOVA code test to run a loop and perform the ANOVA for each species. First we'll replace any special character with an underscore, since statsmodels does not currently accept these. During this loop we'll store the p-values in a Pandas.Series object named p_values. Next we select all p-values smaller than 0.0001 and use this to index our original DataFrame.
Like previously, we'll preform standard scaling on this data and we use the clustermap from Seaborn to draw the heatmap:
We set the figsize to values that result in the labels being shown for each row and colunm, with method we configure how the distance between clusters is calculated, and with metric how the distance between 2 n-dimensional vectors is calculated. All possible options are described in the Scipy documentation for and . Row or column clustering can optionally bet set to False.