Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (58 sloc) 2.17 KB
title author date output
Metabolomics clustering
Russ Poldrack
November 1, 2014
html_document

Metabolomics clustering for the MyConnectome study.

Code available at: https://github.com/poldrack/myconnectome/blob/master/myconnectome/metabolomics/Metabolomics_clustering.Rmd


# First, load the functions that will provide the data:

basedir=Sys.getenv('MYCONNECTOME_DIR')

dataurl='http://web.stanford.edu/group/poldracklab/myconnectome-data/base/metabolomics'
save_data=TRUE

metab=load_metab_data(use_clustered_data=FALSE)
metab$date=NULL
metab=scale(metab)

#The annotation tool that we will use requires PubChem IDs, so load those:

cids=read.table(sprintf('%s/pubmed_cids.txt',dataurl))$V1


#Set up the clustering tool:

library(apcluster)
sim=corSimMat()

#Run the clustering:

ap=apcluster(sim,t(metab))

ap

We save the clustering data and pubchem IDs for each cluster, for later use in annotation with Impala (set save_data=TRUE and change directory location to enable this)


if (save_data) {
  for (i in 1:length(ap@clusters)) {
    print(i)
  	for (member in 1:length(ap@clusters[[i]])) {
  		m=cids[ap@clusters[[i]]]
  		outfilename=sprintf('%s/metabolomics/apclust_scaled_mod%02d.txt',basedir,i)
  		write.table(m,file=outfilename,col.names=FALSE,row.names=FALSE,quote=FALSE)
    	outfilename=sprintf('%s/metabolomics/apclust_scaled_mod%02d_names.txt',basedir,i)
  		write.table(names(ap@clusters[[i]]),file=outfilename,col.names=FALSE,row.names=FALSE,quote=FALSE)

  		}
  	}
  save(ap,file=sprintf('%s/metabolomics/apclust.Rdata',basedir))
}

#Compute eigenconcentrations for each cluster


eigenconc=c()
meanconc=c()
for (c in 1:length(ap@clusters)) {
  print(c)
	cdata=metab[,ap@clusters[[c]]]
	pca=prcomp(cdata,retx=TRUE)
  pc=pca$x[,1]
  if (cor(pc,apply(cdata,1,mean)) < 0) {
    pc=pc*-1
  }
	eigenconc=cbind(eigenconc,pc)
  meanconc=cbind(meanconc,apply(cdata,1,mean))

	}
if (save_data) {
  write.table(eigenconc,sprintf('%s/metabolomics/apclust_eigenconcentrations.txt',basedir))
}