mkatari-bioinformatics-august-2013-clustering
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
mkatari-bioinformatics-august-2013-clustering [2013/10/09 13:30] – mkatari | mkatari-bioinformatics-august-2013-clustering [2015/06/17 13:26] (current) – mkatari | ||
---|---|---|---|
Line 2: | Line 2: | ||
- | ====== Clustering rna-seq data, continuation from [[mkatari-bioinformatics-august-2013-deseq|DESeq]] | + | ====== Clustering rna-seq data ====== |
+ | continuation from [[mkatari-bioinformatics-august-2013-deseq|DESeq]] | ||
+ | [[https:// | ||
+ | [[https:// | ||
+ | |||
+ | In case you didn't get DESeq to work download and load the files above | ||
+ | |||
+ | < | ||
+ | resSig = read.table(" | ||
+ | normalized = read.table(" | ||
+ | |||
+ | </ | ||
Get the significant genes | Get the significant genes | ||
Line 12: | Line 23: | ||
Get the normalized values for the significant genes | Get the normalized values for the significant genes | ||
< | < | ||
- | sigGenes.normalized = normalized[sigGenes, | + | sigGenes.normalized = normalized[as.character(sigGenes),] |
</ | </ | ||
Line 36: | Line 47: | ||
< | < | ||
sigGenes.hclust.k2< | sigGenes.hclust.k2< | ||
+ | </ | ||
+ | |||
+ | Now to get all the genes that are in cluster 2 simply type. | ||
+ | |||
+ | < | ||
+ | hclust.k2.cluster2=names(which(sigGenes.hclust.k2==2)) | ||
+ | </ | ||
+ | |||
+ | Now we can create a new matrix/data frame with just these genes. This new matrix can be used to plot a heatmap to make it easier to see a expression profile of the cluster (see below). | ||
+ | |||
+ | < | ||
+ | hclust.k2.cluster2.normalized = sigGenes.normalized[hclust.k2.cluster2, | ||
</ | </ | ||
Line 53: | Line 76: | ||
plot(sigGenes.hclust.k2.sil) | plot(sigGenes.hclust.k2.sil) | ||
</ | </ | ||
+ | |||
+ | ====== K-means ====== | ||
+ | The K-means method uses euclidean distance to measure distance. Since in biology we are more interested in gene expression profiles instead of magnitude of expression levels, let's scale our data so that the mean of the expression values is 0 and the expression values will be the standard deviations away from the mean. | ||
+ | |||
+ | < | ||
+ | # this function takes a vector of gene expression values. | ||
+ | scaleData <- function(x) { | ||
+ | x = as.numeric(x) | ||
+ | meanx = mean(x) | ||
+ | sdx = sd(x) | ||
+ | y = (x-meanx)/ | ||
+ | return(y) | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | we need to transpose it because apply function returns the genes as different columns. | ||
+ | |||
+ | < | ||
+ | scaledSigGenes = t(apply(sigGenes.normalized, | ||
+ | colnames(scaledSigGenes)=colnames(sigGenes.normalized) | ||
+ | </ | ||
+ | |||
+ | now to run k-means, in this case we are starting with 2 cluster. | ||
+ | |||
+ | < | ||
+ | SigGenes.kmeans.2 = kmeans(scaledSigGenes, | ||
+ | </ | ||
+ | |||
+ | To obtain the measure of how well the clustering has performed, we can look at the sum of squares between members of the outside group and sum of squares total. Higher the better. | ||
+ | |||
+ | < | ||
+ | SigGenes.kmeans.2$betweenss/ | ||
+ | </ | ||
+ | |||
+ | In order to determine the ideal number of k, we can try many different K's and look to see how well they performed. | ||
+ | |||
+ | < | ||
+ | getBestK <- function(x) { | ||
+ | kmeans_ss=numeric() | ||
+ | kmeans_ss[1]=0 | ||
+ | | ||
+ | for (i in 2:20) { | ||
+ | | ||
+ | # | ||
+ | # | ||
+ | |||
+ | # | ||
+ | | ||
+ | | ||
+ | |||
+ | |||
+ | } | ||
+ | return(kmeans_ss) | ||
+ | } | ||
+ | |||
+ | kmeans_ss=getBestK(scaledSigGenes) | ||
+ | plot(kmeans_ss) | ||
+ | |||
+ | </ | ||
+ | To get the genes in the different clusters | ||
+ | < | ||
+ | SigGenes.kmeans.2.group1 = names(which(SigGenes.kmeans.2$cluster==1)) | ||
+ | SigGenes.kmeans.2.group2 = names(which(SigGenes.kmeans.2$cluster==2)) | ||
+ | </ | ||
+ | |||
+ | |||
+ | The code below plots k-means clustering results. You simply have to provide the k-means output and the labels. | ||
+ | |||
+ | < | ||
+ | plotClusterCenters< | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | mycolors=c(" | ||
+ | centersdim = dim(kmeansres$centers) | ||
+ | plot(kmeansres$centers[1, | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | axis(1, at=c(1: | ||
+ | | ||
+ | for (i in 2: | ||
+ | lines(kmeansres$centers[i, | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | |||
+ | plotClusterCenters(SigGenes.kmeans.2) | ||
+ | </ | ||
+ | |||
====== Heatmap ====== | ====== Heatmap ====== | ||
Line 58: | Line 179: | ||
< | < | ||
+ | install.packages(" | ||
library(gplots) | library(gplots) | ||
</ | </ | ||
Line 73: | Line 195: | ||
</ | </ | ||
- | Create heatmap. We can save it to a pdf file | + | Create heatmap. We can save it to a pdf file. Note that sigGenes.normalized is just a matrix. Here we can provide any matrix of values, for example hclust.k2.cluster2.normalized which is the expression values of genes in cluster 2 (see above) |
< | < | ||
pdf(" | pdf(" | ||
- | heatmap.2(sigGenes.normalized, | + | heatmap.2(as.matrix(sigGenes.normalized), |
col=redgreen(75), | col=redgreen(75), | ||
hclustfun=hclust2, | hclustfun=hclust2, |
mkatari-bioinformatics-august-2013-clustering.txt · Last modified: 2015/06/17 13:26 by mkatari