Skip to content

Instantly share code, notes, and snippets.

@al2na
Created September 27, 2014 17:34
Show Gist options
  • Save al2na/2e51791cca5ea93311e6 to your computer and use it in GitHub Desktop.
Save al2na/2e51791cca5ea93311e6 to your computer and use it in GitHub Desktop.
get mean methylation per group from a methylBase object
# returns a matrix of mean methylation values for groups defined
# by treatment vector in the methylBase obj
meanMethGroup<-function(methylBase.obj,weighted=TRUE ){
data=getData(methylBase.obj) # get data frame part of the object
treatment=methylBase.obj@treatment # get the treatment vector from the object
# create the the empty resulting matrix
result=matrix(0,ncol=length(unique(methylBase.obj@treatment)) ,nrow=nrow(methylBase.obj) )
colnames(result)=paste('group',unique(treatment),sep='') # column names are from treatmet vector
for(i in 1:length(unique(treatment)) ){
# get C index
[email protected][methylBase.obj@treatment==unique(treatment)[i]]
if(weighted){
result[,i]=100*rowSums(data[,Cind,drop=FALSE],na.rm=TRUE)/rowSums(data[,Cind-1,drop=FALSE],na.rm=TRUE) # get weigthed means
}
else{
result[,i]=100*rowMeans(data[,Cind,drop=FALSE]/data[,Cind-1,drop=FALSE],na.rm=TRUE) # get means of percent methylation values
}
}
result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment