Skip to content

Instantly share code, notes, and snippets.

@rpietro
Created August 1, 2015 22:25
Show Gist options
  • Save rpietro/ddcb756f5483d8676986 to your computer and use it in GitHub Desktop.
Save rpietro/ddcb756f5483d8676986 to your computer and use it in GitHub Desktop.
Script for NMF heatmap vignette
# http://nmf.r-forge.r-project.org/vignettes/heatmaps.pdf
# random data that follow an 3-rank NMF model (with quite some noise:
# sd=2)
X <- syntheticNMF(100, 3, 20, noise = 2)
# row annotations and covariates
n <- nrow(X)
d <- rnorm(n)
e <- unlist(mapply(rep, c("X", "Y", "Z"), 10))
e <- c(e, rep(NA, n - length(e)))
rdata <- data.frame(Var = d, Type = e)
# column annotations and covariates
p <- ncol(X)
a <- sample(c("alpha", "beta", "gamma"), p, replace = TRUE)
# define covariates: true groups and some numeric variable
c <- rnorm(p)
# gather them in a data.frame
covariates <- data.frame(a, X$pData, c)
par(mfrow = c(1, 2))
aheatmap(X, annCol = covariates, annRow = X$fData)
aheatmap(X)
res <- nmf(X, 3, nrun = 10)
res
## <Object of class: NMFfitX1 >
## Method: brunet
## Runs: 10
## RNG:
## 407L, 1296956629L, -635484270L, -1992246805L, -332345296L, 1842947697L, 608732542L
## Total timing:
## user system elapsed
## 4.416 0.208 3.121
opar <- par(mfrow = c(1, 2))
# coefmap from multiple run fit: includes a consensus track
coefmap(res)
# coefmap of a single run fit: no consensus track
coefmap(minfit(res))
par(opar)
Rowv = NA
Colv = TRUE
scale = "c1"
color = "YlOrRd:50"
annCol = predict(object) + predict(object, "consensus")
opar <- par(mfrow=c(1,2))
# removing all automatic annotation tracks
coefmap(res, tracks=NA)
# customized plot
coefmap(res, Colv = ✬euclidean✬
, main = "Metagene contributions in each sample", labCol = NULL
, annRow = list(Metagene=✬:basis✬), annCol = list(✬:basis✬, Class=a, Index=c)
, annColors = list(Metagene=✬Set2✬)
, info = TRUE)
par(opar)
opar <- par(mfrow = c(1, 2))
# default plot
basismap(res)
# customized plot: only use row special annotation track.
basismap(res, main = "Metagenes", annRow = list(d, e), tracks = c(Metagene = ":basis"))
par(opar)
Colv = NA
scale = "r1"
color = "YlOrRd:50"
annRow = predict(object, "features")
opar <- par(mfrow = c(1, 2))
# default plot
consensusmap(res)
# customized plot
consensusmap(res, annCol = covariates, annColors = list(c = "blue"), labCol = "sample ",
main = "Cluster stability", sub = "Consensus matrix and all covariates")
distfun = function(x) as.dist(1 - x) # x being the consensus matrix
hclustfun = "average"
Rowv = TRUE
Colv = "Rowv"
color = "-RdYlBu"
res2_7 <- nmf(X, 2:7, nrun = 10, .options = "v")
## Compute NMF rank= 2 ... + measures ... OK
## Compute NMF rank= 3 ... + measures ... OK
## Compute NMF rank= 4 ... + measures ... OK
## Compute NMF rank= 5 ... + measures ... OK
## Compute NMF rank= 6 ... + measures ... OK
## Compute NMF rank= 7 ... + measures ... OK
class(res2_7)
consensusmap(res2_7)
res_methods <- nmf(X, 3, list("lee", "brunet", "nsNMF"), nrun = 10)
## Compute NMF method ✬lee✬ [1/3] ... OK
## Compute NMF method ✬brunet✬ [2/3] ... OK
## Compute NMF method ✬nsNMF✬ [3/3] ... OK
class(res_methods)
## [1] "NMFList"
## attr(,"package")
## [1] "NMF"
consensusmap(res_methods)
demo("aheatmap")
# or
demo("heatmaps")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment