views:

229

answers:

3

how can I do a hierarchical clustering (in this case for gene expression data) in Python in a way that shows the matrix of gene expression values along with the dendrogram? What I mean is like the example here:

http://www.mathworks.cn/access/helpdesk/help/toolbox/bioinfo/ug/a1060813239b1.html

shown after bullet point 6 (Figure 1), where the dendrogram is plotted to the left of the gene expression matrix, where the rows have been reordered to reflect the clustering.

How can I do this in Python using numpy/scipy or other tools? Also, is it computationally practical to do this with a matrix of about 11,000 genes, using euclidean distance as a metric?

EDIT: Many have suggested clustering packages, but I am still unsure how to plot the kind of image I linked to above in Python. How can I overlay a dendrogram alongside a heatmap matrix, using Matplotlib for example?

thanks.

A: 

Did you try biopython ? Not sure it does exactly what you want but Biopython has tools for clustering. You can also check pychem that uses the biopython cluster module and provides of a GUI for data management and visualization. Another option is pycluster.

joaquin
I will look into biopython, my concern is that I don't know how to plot the results in matplotlib. Pychem is not maintained anymore.
@user248237, why do you say pychem is not maintained anymore? Pychem 3.0.5beta has been released 3 months ago... (Pychem releases are produced once per year more or less)... Source is available and you can get the ideas of how to represent your data from there.
joaquin
+1  A: 

You can do this with scipy's cluster.hierarchy module. The commands are actually even very similar. However, you will have to use correlation instead of corr as a parameter to pdist and rather than cluster the name of the function scipy's cluster module is fcluster. Also, for the dendrogram, the function is dendrogram in scipy as opposed to clustergram in Matlab.

You can definitely use a euclidean metric (think it is the default for pdist). I think that it should be feasible to do this with 11,000 genes because that will be 11000*(11000-1)/2 = 60494500 (11000 choose 2) distances to be computed. That's a large number, but certainly feasible I would think.

Justin Peel
Are there tools for plotting the resulting dendrogram in scipy?
You'll also want the `matplotlib` module. It might help to look at this document: http://www.cs.swarthmore.edu/~turnbull/cs67/s09/labs/lab05.pdf It uses the scipy-cluster package (hcluster) which I'm pretty certain is what has been put in the scipy.cluster.hierarchy module.
Justin Peel
A: 

Many clustering methods including scipy.cluster start by sorting all pairwise distances, ~ 60 million in your case, not too big.
How long does the following take for you ?

import scipy.cluster.hierarchy as hier
import pylab as pl

def fcluster( pts, ncluster, method="average", criterion="maxclust" ):
    """ -> (pts, Y pdist, Z linkage, T fcluster, clusterlists)   
        ncluster = n1 + n2 + ... (including n1 singletons)
        av cluster size = len(pts) / ncluster
    """
    pts = np.asarray(pts)
    Y = scipy.spatial.distance.pdist( pts )  # ~ N^2 / 2
    Z = hier.linkage( Y, method )  # N-1                         
    T = hier.fcluster( Z, ncluster, criterion=criterion )
        # clusters = clusterlists(T)
    return (pts, Y, Z, T)

hier.dendrogram( Z )

How to permute the matrix and plot nicely was asked here in So in March, with a partial answer.

Denis