Skip to content

Instantly share code, notes, and snippets.

@jenzopr
Last active April 11, 2016 13:43
Show Gist options
  • Save jenzopr/02bc8205e85e51dfbfc80f4373194466 to your computer and use it in GitHub Desktop.
Save jenzopr/02bc8205e85e51dfbfc80f4373194466 to your computer and use it in GitHub Desktop.
The calc_dm function implements the distance-to-the-median measurement from Kolodziejczyk et. al. (Cell Stem Cell, 2015).
library(zoo)
calc_dm = function(expr, func=median) {
if( is.null(rownames(expr)) ) { rownames(expr) = 1:nrow(expr) }
# Calculate log10 mean of the rows
m = log10(rowMeans(expr))
names(m) = rownames(expr)
# Calculate the squared cv
cv2 = apply(expr, 1, function(x) { (sd(x)/mean(x))^2 })
# Sort the means
s = names(sort(m))
# Calculate median squared cv in a window of 50 genes
r = rollapply(data = log10(cv2[s]), width = 50, FUN=func, fill="extend")
names(r) = s
# Return distance to the median
log10(cv2) - r[names(cv2)]
}
@jenzopr
Copy link
Author

jenzopr commented Apr 11, 2016

Application on data from mouse single ES cells from Mantsoki et. al. (Comp. Biol. Chem, 2016)

cv-mean-dep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment