# In a RNA-Seq heatmap should you do Z-score standardisation before clustering the rows/columns or after?

I have made a heatmap using RPKM values from a RNA-Seq dataset using the pheatmap() function in R. I have log2-transformed the data before performing Z-score standardisation of the data. I have also clustered the rows and columns of the heatmap. I have the following code:

heatmap_data_log2 %>% pheatmap(color = colorRampPalette(c("blue2","white","red"))(100), scale = "column", cluster_cols = T, clustering_method = "ward.D2", angle_col = 45, fontsize_row = 7.5, fontsize_col = 8, border_color = NA, cutree_rows = 4)


I have seen in a video tutorial that the pheatmap() function does the Z-score scaling before doing the clustering of the rows and columns. However, in this online article, it says that "The Z-scores are computed after the clustering, so that it only affects the graphical aesthetics and the color visualization is improved".

As these two sources are giving contradictory information, I was wondering which is better. Should you do Z-score scaling of the gene expression values before doing the clustering or after? Any advice is appreciated.

## 1 Answer

Looking at the source for pheatmap, there is a function called scale_mat that is used to preprocess and normalize the input matrix, depending on the value of scale, which specifies one of either none, row, or column normalization options. Separate and similar functions are used for color value scaling downstream.

I have not watched the video or read the linked article, so I'm not sure this is contradictory, but it may be just small confusion about how normalization is applied (and to what).

Normalization is usually done before clustering to focus cluster construction on signal rows or columns that are not some multiple or scale factor of another — and in referring back to the source code, this is what pheatmap does, as well.