Repository: hms-dbmi/scde Branch: main Commit: 37859b8f4df6 Files: 76 Total size: 656.3 KB Directory structure: gitextract_3p0tp7nj/ ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── DESCRIPTION ├── NAMESPACE ├── R/ │ └── functions.R ├── README.md ├── data/ │ ├── es.mef.small.rda │ ├── knn.rda │ ├── o.ifm.rda │ ├── pollen.rda │ └── scde.edff.rda ├── inst/ │ ├── NEWS │ ├── diffexp.Rmd │ ├── diffexp.md │ ├── experimental.Rmd │ ├── experimental.md │ ├── genesets.Rmd │ ├── genesets.md │ ├── pagoda.Rmd │ └── pagoda.md ├── license.txt ├── man/ │ ├── ViewPagodaApp-class.Rd │ ├── bwpca.Rd │ ├── clean.counts.Rd │ ├── clean.gos.Rd │ ├── es.mef.small.Rd │ ├── knn.Rd │ ├── knn.error.models.Rd │ ├── make.pagoda.app.Rd │ ├── o.ifm.Rd │ ├── pagoda.cluster.cells.Rd │ ├── pagoda.effective.cells.Rd │ ├── pagoda.gene.clusters.Rd │ ├── pagoda.pathway.wPCA.Rd │ ├── pagoda.reduce.loading.redundancy.Rd │ ├── pagoda.reduce.redundancy.Rd │ ├── pagoda.show.pathways.Rd │ ├── pagoda.subtract.aspect.Rd │ ├── pagoda.top.aspects.Rd │ ├── pagoda.varnorm.Rd │ ├── pagoda.view.aspects.Rd │ ├── papply.Rd │ ├── pollen.Rd │ ├── scde.Rd │ ├── scde.browse.diffexp.Rd │ ├── scde.edff.Rd │ ├── scde.error.models.Rd │ ├── scde.expression.difference.Rd │ ├── scde.expression.magnitude.Rd │ ├── scde.expression.prior.Rd │ ├── scde.failure.probability.Rd │ ├── scde.fit.models.to.reference.Rd │ ├── scde.posteriors.Rd │ ├── scde.test.gene.expression.difference.Rd │ ├── show.app.Rd │ ├── view.aspects.Rd │ └── winsorize.matrix.Rd ├── src/ │ ├── Makevars │ ├── Makevars.win │ ├── bwpca.cpp │ ├── bwpca.h │ ├── jpmatLogBoot.cpp │ ├── jpmatLogBoot.h │ ├── matSlideMult.cpp │ ├── matSlideMult.h │ ├── pagoda.cpp │ └── pagoda.h ├── tests/ │ └── tests.R ├── vignettes/ │ ├── diffexp.Rmd │ └── pagoda.Rmd └── web/ ├── additional.css ├── pathcl.css ├── pathcl.js ├── pathcl_canvas.js └── pathcl_canvas_1.1.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ ls *~ R/*~ .DS_Store .Rbuildignore .Rhistory .Rproj.user .git .Rproj.user src/*.o src/*.so scde.Rproj *_bak .*_bak ================================================ FILE: .travis.yml ================================================ # Use R language: r sudo: false cache: packages warnings_are_errors: false env: global: - BIOC_USE_DEVEL="FALSE" ## Use the current release version - _R_CHECK_FORCE_SUGGESTS_="FALSE" ## Some suggested packages not ## available for r-oldrel r: - oldrel - release - devel addons: apt: packages: - libnlopt-dev install: - R -e 'install.packages("devtools")' - R -e 'devtools::install_deps(dependencies = TRUE)' - R -e 'devtools::install_version("flexmix", "2.3-13")' # catch package installation issues sooner rather than later - R -e 'devtools::install_github("hms-dbmi/scde")' # do not build vignettes...takes too long and times out on travis script: - R CMD build --no-build-vignettes --no-manual . - R CMD check --no-build-vignettes --no-manual --timings *tar.gz # we need to install BiocInstaller for testing Bioconductor packages bioc_required: true # only report coverage for the release version after_success: - test $TRAVIS_R_VERSION_STRING = 'release' && Rscript -e 'covr::codecov()' notifications: email: on_success: change on_failure: change ================================================ FILE: CHANGELOG.md ================================================ ## Upcoming ## [2.26.2] - Version from Bioconductor, Packaged: 2023-January-19 Bioconductor version: Release (3.16) ================================================ FILE: DESCRIPTION ================================================ Package: scde Type: Package Title: Single Cell Differential Expression Version: 2.27.1 Description: The scde package implements a set of statistical methods for analyzing single-cell RNA-seq data. scde fits individual error models for single-cell RNA-seq measurements. These models can then be used for assessment of differential expression between groups of cells, as well as other types of analysis. The scde package also contains the pagoda framework which applies pathway and gene set overdispersion analysis to identify and characterize putative cell subpopulations based on transcriptional signatures. The overall approach to the differential expression analysis is detailed in the following publication: "Bayesian approach to single-cell differential expression analysis" (Kharchenko PV, Silberstein L, Scadden DT, Nature Methods, doi: 10.1038/nmeth.2967). The overall approach to subpopulation identification and characterization is detailed in the following pre-print: "Characterizing transcriptional heterogeneity through pathway and gene set overdispersion analysis" (Fan J, Salathia N, Liu R, Kaeser G, Yung Y, Herman J, Kaper F, Fan JB, Zhang K, Chun J, and Kharchenko PV, Nature Methods, doi:10.1038/nmeth.3734). Author: Peter Kharchenko [aut, cre], Jean Fan [aut], Evan Biederstedt [aut] Authors@R: c( person("Peter", "Kharchenko", role = c("aut", "cre"), email = "Peter_Kharchenko@hms.harvard.edu"), person("Jean", "Fan", role = "aut", email = "jeanfan@jhu.edu", comment = c(ORCID = "0000-0002-0212-5451")), person("Evan", "Biederstedt", role = "aut", email = "evan.biederstedt@gmail.com") ) Maintainer: Evan Biederstedt URL: http://pklab.med.harvard.edu/scde BugReports: https://github.com/hms-dbmi/scde/issues License: GPL-2 LazyData: true Depends: R (>= 3.0.0), flexmix Imports: Rcpp (>= 0.10.4), RcppArmadillo (>= 0.5.400.2.0), mgcv, Rook, rjson, MASS, Cairo, RColorBrewer, edgeR, quantreg, methods, nnet, RMTstat, extRemes, pcaMethods, BiocParallel, parallel Suggests: knitr, cba, fastcluster, WGCNA, GO.db, org.Hs.eg.db, rmarkdown biocViews: ImmunoOncology, RNASeq, StatisticalMethod, DifferentialExpression, Bayesian, Transcription, Software LinkingTo: Rcpp, RcppArmadillo VignetteBuilder: knitr Packaged: 2015-11-02 14:30:04 UTC; reyes RoxygenNote: 5.0.0 NeedsCompilation: yes ================================================ FILE: NAMESPACE ================================================ export(bwpca) export(knn.error.models) export(make.pagoda.app) export(pagoda.cluster.cells) export(pagoda.effective.cells) export(pagoda.gene.clusters) export(pagoda.pathway.wPCA) export(pagoda.reduce.loading.redundancy) export(pagoda.reduce.redundancy) export(pagoda.show.pathways) export(pagoda.subtract.aspect) export(pagoda.top.aspects) export(pagoda.varnorm) export(pagoda.view.aspects) export(scde.browse.diffexp) export(scde.error.models) export(scde.expression.difference) export(scde.expression.magnitude) export(scde.expression.prior) export(scde.failure.probability) export(scde.fit.models.to.reference) export(scde.posteriors) export(scde.test.gene.expression.difference) export(show.app) export(winsorize.matrix) export(clean.counts) export(clean.gos) useDynLib(scde) # custom additions importFrom(Cairo,CairoPNG) importFrom(RColorBrewer,brewer.pal) importFrom(parallel,mclapply,detectCores) importFrom(BiocParallel,bplapply,MulticoreParam) importFrom(edgeR,calcNormFactors) importFrom(MASS,theta.ml,rnegbin,negative.binomial) importFrom(rjson,fromJSON,toJSON) importFrom(Rcpp,evalCpp) importFrom(methods,setRefClass) importFrom(quantreg,rq,predict.rq) importFrom(nnet,multinom,nnet,nnet.default) importFrom(mgcv,gam) importFrom(RMTstat,WishartMaxPar) importFrom(pcaMethods, sDev, pca, scores, loadings) importFrom(extRemes, fevd, qevd, plot.fevd) import(RcppArmadillo) import(flexmix) import(Rook) ================================================ FILE: R/functions.R ================================================ ##' Single-cell Differential Expression (with Pathway And Gene set Overdispersion Analysis) ##' ##' The scde package implements a set of statistical methods for analyzing single-cell RNA-seq data. ##' scde fits individual error models for single-cell RNA-seq measurements. These models can then be used for ##' assessment of differential expression between groups of cells, as well as other types of analysis. ##' The scde package also contains the pagoda framework which applies pathway and gene set overdispersion analysis ##' to identify and characterize putative cell subpopulations based on transcriptional signatures. ##' See vignette("diffexp") for a brief tutorial on differential expression analysis. ##' See vignette("pagoda") for a brief tutorial on pathway and gene set overdispersion analysis to identify and characterize cell subpopulations. ##' More extensive tutorials are available at \url{http://pklab.med.harvard.edu/scde/index.html}. ##' (test) ##' @name scde ##' @docType package ##' @author Peter Kharchenko \email{Peter_Kharchenko@@hms.harvard.edu} ##' @author Jean Fan \email{jeanfan@@fas.harvard.edu} NULL ################################# Sample data ##' Sample data ##' ##' A subset of Saiful et al. 2011 dataset containing first 20 ES and 20 MEF cells. ##' ##' @name es.mef.small ##' @docType data ##' @references \url{http://www.ncbi.nlm.nih.gov/pubmed/21543516} ##' @export NULL ##' Sample data ##' ##' Single cell data from Pollen et al. 2014 dataset. ##' ##' @name pollen ##' @docType data ##' @references \url{www.ncbi.nlm.nih.gov/pubmed/25086649} ##' @export NULL ##' Sample error model ##' ##' SCDE error model generated from a subset of Saiful et al. 2011 dataset containing first 20 ES and 20 MEF cells. ##' ##' @name o.ifm ##' @docType data ##' @references \url{http://www.ncbi.nlm.nih.gov/pubmed/21543516} ##' @export NULL ##' Sample error model ##' ##' SCDE error model generated from the Pollen et al. 2014 dataset. ##' ##' @name knn ##' @docType data ##' @references \url{www.ncbi.nlm.nih.gov/pubmed/25086649} ##' @export NULL # Internal model data # # Numerically-derived correction for NB->chi squared approximation stored as an local regression model # # @name scde.edff ################################# Generic methods ##' Filter GOs list ##' ##' Filter GOs list and append GO names when appropriate ##' ##' @param go.env GO or gene set list ##' @param min.size Minimum size for number of genes in a gene set (default: 5) ##' @param max.size Maximum size for number of genes in a gene set (default: 5000) ##' @param annot Whether to append GO annotations for easier interpretation (default: FALSE) ##' ##' @return a filtered GO list ##' ##' @examples ##' \donttest{ ##' # 10 sample GOs ##' library(org.Hs.eg.db) ##' go.env <- mget(ls(org.Hs.egGO2ALLEGS)[1:10], org.Hs.egGO2ALLEGS) ##' # Filter this list and append names for easier interpretation ##' go.env <- clean.gos(go.env) ##' } ##' ##' @export clean.gos <- function(go.env, min.size = 5, max.size = 5000, annot = FALSE) { go.env <- as.list(go.env) size <- unlist(lapply(go.env, length)) go.env <- go.env[size > min.size & size < max.size] # If we have GO.db installed, then add the term to each GO code. if (annot && "GO.db" %in% installed.packages()[,1]) { desc <- MASS::select( GO.db, keys = names(go.env), columns = c("TERM"), multiVals = 'CharacterList' ) stopifnot(all(names(go.env) == desc$GOID)) names(go.env) <- paste(names(go.env), desc$TERM) } return(go.env) } ##' Filter counts matrix ##' ##' Filter counts matrix based on gene and cell requirements ##' ##' @param counts read count matrix. The rows correspond to genes, columns correspond to individual cells ##' @param min.lib.size Minimum number of genes detected in a cell. Cells with fewer genes will be removed (default: 1.8e3) ##' @param min.reads Minimum number of reads per gene. Genes with fewer reads will be removed (default: 10) ##' @param min.detected Minimum number of cells a gene must be seen in. Genes not seen in a sufficient number of cells will be removed (default: 5) ##' ##' @return a filtered read count matrix ##' ##' @examples ##' data(pollen) ##' dim(pollen) ##' cd <- clean.counts(pollen) ##' dim(cd) ##' ##' @export clean.counts <- function(counts, min.lib.size = 1.8e3, min.reads = 10, min.detected = 5) { # filter out low-gene cells counts <- counts[, colSums(counts>0)>min.lib.size] # remove genes that don't have many reads counts <- counts[rowSums(counts)>min.reads, ] # remove genes that are not seen in a sufficient number of cells counts <- counts[rowSums(counts>0)>min.detected, ] return(counts) } ################################# SCDE Methods ##' Fit single-cell error/regression models ##' ##' Fit error models given a set of single-cell data (counts) and an optional grouping factor (groups). The cells (within each group) are first cross-compared to determine a subset of genes showing consistent expression. The set of genes is then used to fit a mixture model (Poisson-NB mixture, with expression-dependent concomitant). ##' ##' Note: the default implementation has been changed to use linear-scale fit with expression-dependent NB size (overdispersion) fit. This represents an interative improvement on the originally published model. Use linear.fit=F to revert back to the original fitting procedure. ##' ##' @param counts read count matrix. The rows correspond to genes (should be named), columns correspond to individual cells. The matrix should contain integer counts ##' @param groups an optional factor describing grouping of different cells. If provided, the cross-fits and the expected expression magnitudes will be determined separately within each group. The factor should have the same length as ncol(counts). ##' @param min.nonfailed minimal number of non-failed observations required for a gene to be used in the final model fitting ##' @param threshold.segmentation use a fast threshold-based segmentation during cross-fit (default: TRUE) ##' @param min.count.threshold the number of reads to use to guess which genes may have "failed" to be detected in a given measurement during cross-cell comparison (default: 4) ##' @param zero.count.threshold threshold to guess the initial value (failed/non-failed) during error model fitting procedure (defaults to the min.count.threshold value) ##' @param zero.lambda the rate of the Poisson (failure) component (default: 0.1) ##' @param save.crossfit.plots whether png files showing cross-fit segmentations should be written out (default: FALSE) ##' @param save.model.plots whether pdf files showing model fits should be written out (default = TRUE) ##' @param n.cores number of cores to use ##' @param min.size.entries minimum number of genes to use when determining expected expression magnitude during model fitting ##' @param max.pairs maximum number of cross-fit comparisons that should be performed per group (default: 5000) ##' @param min.pairs.per.cell minimum number of pairs that each cell should be cross-compared with ##' @param verbose 1 for increased output ##' @param linear.fit Boolean of whether to use a linear fit in the regression (default: TRUE). ##' @param local.theta.fit Boolean of whether to fit the overdispersion parameter theta, ie. the negative binomial size parameter, based on local regression (default: set to be equal to the linear.fit parameter) ##' @param theta.fit.range Range of valid values for the overdispersion parameter theta, ie. the negative binomial size parameter (default: c(1e-2, 1e2)) ##' ##' @return a model matrix, with rows corresponding to different cells, and columns representing different parameters of the determined models ##' ##' @useDynLib scde ##' ##' @examples ##' data(es.mef.small) ##' cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) ##' sg <- factor(gsub("(MEF|ESC).*", "\\1", colnames(cd)), levels = c("ESC", "MEF")) ##' names(sg) <- colnames(cd) ##' \donttest{ ##' o.ifm <- scde.error.models(counts = cd, groups = sg, n.cores = 10, threshold.segmentation = TRUE) ##' } ##' ##' @export scde.error.models <- function(counts, groups = NULL, min.nonfailed = 3, threshold.segmentation = TRUE, min.count.threshold = 4, zero.count.threshold = min.count.threshold, zero.lambda = 0.1, save.crossfit.plots = FALSE, save.model.plots = TRUE, n.cores = 12, min.size.entries = 2e3, max.pairs = 5000, min.pairs.per.cell = 10, verbose = 0, linear.fit = TRUE, local.theta.fit = linear.fit, theta.fit.range = c(1e-2, 1e2)) { # default same group if(is.null(groups)) { groups <- as.factor(rep("cell", ncol(counts))) } # check for integer counts if(any(!unlist(lapply(counts,is.integer)))) { stop("Some of the supplied counts are not integer values (or stored as non-integer types). Aborting!\nThe method is designed to work on read counts - do not pass normalized read counts (e.g. FPKM values). If matrix contains read counts, but they are stored as numeric values, use counts<-apply(counts,2,function(x) {storage.mode(x) <- 'integer'; x}) to recast."); } # crossfit if(verbose) { cat("cross-fitting cells.\n") } cfm <- calculate.crossfit.models(counts, groups, n.cores = n.cores, threshold.segmentation = threshold.segmentation, min.count.threshold = min.count.threshold, zero.lambda = zero.lambda, max.pairs = max.pairs, save.plots = save.crossfit.plots, min.pairs.per.cell = min.pairs.per.cell, verbose = verbose) # error model for each cell if(verbose) { cat("building individual error models.\n") } ifm <- calculate.individual.models(counts, groups, cfm, min.nonfailed = min.nonfailed, zero.count.threshold = zero.count.threshold, n.cores = n.cores, save.plots = save.model.plots, linear.fit = linear.fit, return.compressed.models = TRUE, verbose = verbose, min.size.entries = min.size.entries, local.theta.fit = local.theta.fit, theta.fit.range = theta.fit.range) rm(cfm) gc() return(ifm) } ##' Estimate prior distribution for gene expression magnitudes ##' ##' Use existing count data to determine a prior distribution of genes in the dataset ##' ##' @param models models determined by \code{\link{scde.error.models}} ##' @param counts count matrix ##' @param length.out number of points (resolution) of the expression magnitude grid (default: 400). Note: larger numbers will linearly increase memory/CPU demands. ##' @param show.plot show the estimate posterior ##' @param pseudo.count pseudo-count value to use (default 1) ##' @param bw smoothing bandwidth to use in estimating the prior (default: 0.1) ##' @param max.quantile determine the maximum expression magnitude based on a quantile (default : 0.999) ##' @param max.value alternatively, specify the exact maximum expression magnitude value ##' ##' @return a structure describing expression magnitude grid ($x, on log10 scale) and prior ($y) ##' ##' @examples ##' data(es.mef.small) ##' cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) ##' data(o.ifm) # Load precomputed model. Use ?scde.error.models to see how o.ifm was generated ##' o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) ##' ##' @export scde.expression.prior <- function(models, counts, length.out = 400, show.plot = FALSE, pseudo.count = 1, bw = 0.1, max.quantile = 1-1e-3, max.value = NULL) { fpkm <- scde.expression.magnitude(models, counts) fail <- scde.failure.probability(models, counts = counts) fpkm <- log10(exp(as.matrix(fpkm))+1) wts <- as.numeric(as.matrix(1-fail[, colnames(fpkm)])) wts <- wts/sum(wts) # fit density on a mirror image if(is.null(max.value)) { x <- as.numeric(fpkm) max.value <- as.numeric(quantile(x[x 1) { correct.batch <- TRUE } else { if(verbose) { cat("WARNING: only one batch level detected. Nothing to correct for.") } } } # batch control if(correct.batch) { batch <- as.factor(batch) # check batch-group interactions bgti <- table(groups, batch) bgti.ft <- fisher.test(bgti) if(verbose) { cat("controlling for batch effects. interaction:\n") print(bgti) } #if(any(bgti == 0)) { # cat("ERROR: cannot control for batch effect, as some batches are found only in one group:\n") # print(bgti) #} if(bgti.ft$p.value < 1e-3) { cat("WARNING: strong interaction between groups and batches! Correction may be ineffective:\n") print(bgti.ft) } # calculate batch posterior if(verbose) { cat("calculating batch posteriors\n") } batch.jpl <- tapply(seq_len(nrow(models)), groups, function(ii) { scde.posteriors(models = batch.models, counts = counts, prior = prior, batch = batch, composition = table(batch[ii]), n.cores = n.cores, n.randomizations = n.randomizations, return.individual.posteriors = FALSE) }) if(verbose) { cat("calculating batch differences\n") } batch.bdiffp <- calculate.ratio.posterior(batch.jpl[[1]], batch.jpl[[2]], prior, n.cores = n.cores) batch.bdiffp.rep <- quick.distribution.summary(batch.bdiffp) } else { if(verbose) { cat("comparing groups:\n") print(table(as.character(groups))) } } # fit joint posteriors for each group jpl <- tapply(seq_len(nrow(models)), groups, function(ii) { scde.posteriors(models = models[ii, , drop = FALSE], counts = counts[, ii, drop = FALSE], prior = prior, n.cores = n.cores, n.randomizations = n.randomizations) }) if(verbose) { cat("calculating difference posterior\n") } # calculate difference posterior bdiffp <- calculate.ratio.posterior(jpl[[1]], jpl[[2]], prior, n.cores = n.cores) if(verbose) { cat("summarizing differences\n") } bdiffp.rep <- quick.distribution.summary(bdiffp) if(correct.batch) { if(verbose) { cat("adjusting for batch effects\n") } # adjust for batch effects a.bdiffp <- calculate.ratio.posterior(bdiffp, batch.bdiffp, prior = data.frame(x = as.numeric(colnames(bdiffp)), y = rep(1/ncol(bdiffp), ncol(bdiffp))), skip.prior.adjustment = TRUE, n.cores = n.cores) a.bdiffp.rep <- quick.distribution.summary(a.bdiffp) # return with batch correction info if(return.posteriors) { return(list(batch.adjusted = a.bdiffp.rep, results = bdiffp.rep, batch.effect = batch.bdiffp.rep, difference.posterior = bdiffp, batch.adjusted.difference.posterior = a.bdiffp, joint.posteriors = jpl)) } else { return(list(batch.adjusted = a.bdiffp.rep, results = bdiffp.rep, batch.effect = batch.bdiffp.rep)) } } else { # no batch correction return if(return.posteriors) { return(list(results = bdiffp.rep, difference.posterior = bdiffp, joint.posteriors = jpl)) } else { return(bdiffp.rep) } } } ##' View differential expression results in a browser ##' ##' Launches a browser app that shows the differential expression results, allowing to sort, filter, etc. ##' The arguments generally correspond to the \code{scde.expression.difference()} call, except that the results of that call are also passed here. Requires \code{Rook} and \code{rjson} packages to be installed. ##' ##' @param results result object returned by \code{scde.expression.difference()}. Note to browse group posterior levels, use \code{return.posteriors = TRUE} in the \code{scde.expression.difference()} call. ##' @param models model matrix ##' @param counts count matrix ##' @param prior prior ##' @param groups group information ##' @param batch batch information ##' @param geneLookupURL The URL that will be used to construct links to view more information on gene names. By default (if can't guess the organism) the links will forward to ENSEMBL site search, using \code{geneLookupURL = "http://useast.ensembl.org/Multi/Search/Results?q = {0}"}. The "{0}" in the end will be substituted with the gene name. For instance, to link to GeneCards, use \code{"http://www.genecards.org/cgi-bin/carddisp.pl?gene = {0}"}. ##' @param server optional previously returned instance of the server, if want to reuse it. ##' @param name app name (needs to be altered only if adding more than one app to the server using \code{server} parameter) ##' @param port Interactive browser port ##' ##' @return server instance, on which $stop() function can be called to kill the process. ##' ##' @examples ##' data(es.mef.small) ##' cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) ##' sg <- factor(gsub("(MEF|ESC).*", "\\1", colnames(cd)), levels = c("ESC", "MEF")) ##' names(sg) <- colnames(cd) ##' \donttest{ ##' o.ifm <- scde.error.models(counts = cd, groups = sg, n.cores = 10, threshold.segmentation = TRUE) ##' o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) ##' # make sure groups corresponds to the models (o.ifm) ##' groups <- factor(gsub("(MEF|ESC).*", "\\1", rownames(o.ifm)), levels = c("ESC", "MEF")) ##' names(groups) <- row.names(o.ifm) ##' ediff <- scde.expression.difference(o.ifm, cd, o.prior, groups = groups, n.randomizations = 100, n.cores = 10, verbose = 1) ##' scde.browse.diffexp(ediff, o.ifm, cd, o.prior, groups = groups, geneLookupURL="http://www.informatics.jax.org/searchtool/Search.do?query={0}") # creates browser ##' } ##' ##' @export scde.browse.diffexp <- function(results, models, counts, prior, groups = NULL, batch = NULL, geneLookupURL = NULL, server = NULL, name = "scde", port = NULL) { #require(Rook) #require(rjson) if(is.null(server)) { server <- get.scde.server(port) } sa <- ViewDiff$new(results, models, counts, prior, groups = groups, batch = batch, geneLookupURL = geneLookupURL) server$add(app = sa, name = name) browseURL(paste(server$full_url(name), "index.html", sep = "/")) return(server) } ##' View PAGODA application ##' ##' Installs a given pagoda app (or any other rook app) into a server, optionally ##' making a call to show it in the browser. ##' ##' @param app pagoda app (output of make.pagoda.app()) or another rook app ##' @param name URL path name for this app ##' @param browse whether a call should be made for browser to show the app ##' @param port optional port on which the server should be initiated ##' @param ip IP on which the server should listen (typically localhost) ##' @param server an (optional) Rook server instance (defaults to ___scde.server) ##' ##' @examples ##' \donttest{ ##' app <- make.pagoda.app(tamr2, tam, varinfo, go.env, pwpca, clpca, col.cols=col.cols, cell.clustering=hc, title="NPCs") ##' # show app in the browser (port 1468) ##' show.app(app, "pollen", browse = TRUE, port=1468) ##' } ##' ##' @return Rook server instance ##' ##' @export show.app <- function(app, name, browse = TRUE, port = NULL, ip = '127.0.0.1', server = NULL) { # replace special characters name <- gsub("[^[:alnum:]]", "_", name) if (tools:::httpdPort() !=0 && tools:::httpdPort() != port) { cat("ERROR: port is already being used. The PAGODA app is currently incompatible with RStudio. Please try running the interactive app in the R console.") } if(is.null(server)) { server <- get.scde.server(port) } server$add(app = app, name = name) if(browse) { browseURL(paste(server$full_url(name), "index.html", sep = "/")) } return(server) } # get SCDE server from saved session get.scde.server <- function(port = NULL, ip = '127.0.0.1') { if(exists("___scde.server", envir = globalenv())) { server <- get("___scde.server", envir = globalenv()) } else { require(Rook) server <- Rhttpd$new() assign("___scde.server", server, envir = globalenv()) server$start(listen = ip, port = port) } return(server) } # calculate individual and joint posterior information # models - all or a subset of models belonging to a particular group # ##' Calculate joint expression magnitude posteriors across a set of cells ##' ##' Calculates expression magnitude posteriors for the individual cells, and then uses bootstrap resampling to calculate a joint expression posterior for all the specified cells. Alternatively during batch-effect correction procedure, the joint posterior can be calculated for a random composition of cells of different groups (see \code{batch} and \code{composition} parameters). ##' ##' @param models models models determined by \code{\link{scde.error.models}} ##' @param counts read count matrix ##' @param prior gene expression prior as determined by \code{\link{scde.expression.prior}} ##' @param n.randomizations number of bootstrap iterations to perform ##' @param batch a factor describing which batch group each cell (i.e. each row of \code{models} matrix) belongs to ##' @param composition a vector describing the batch composition of a group to be sampled ##' @param return.individual.posteriors whether expression posteriors of each cell should be returned ##' @param return.individual.posterior.modes whether modes of expression posteriors of each cell should be returned ##' @param ensemble.posterior Boolean of whether to calculate the ensemble posterior (sum of individual posteriors) instead of a joint (product) posterior. (default: FALSE) ##' @param n.cores number of cores to utilize ##' ##' @return \subsection{default}{ a posterior probability matrix, with rows corresponding to genes, and columns to expression levels (as defined by \code{prior$x}) ##' } ##' \subsection{return.individual.posterior.modes}{ a list is returned, with the \code{$jp} slot giving the joint posterior matrix, as described above. The \code{$modes} slot gives a matrix of individual expression posterior mode values on log scale (rows - genes, columns -cells)} ##' \subsection{return.individual.posteriors}{ a list is returned, with the \code{$post} slot giving a list of individual posterior matrices, in a form analogous to the joint posterior matrix, but reported on log scale } ##' ##' @examples ##' data(es.mef.small) ##' cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) ##' data(o.ifm) # Load precomputed model. Use ?scde.error.models to see how o.ifm was generated ##' o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) ##' # calculate joint posteriors ##' jp <- scde.posteriors(o.ifm, cd, o.prior, n.cores = 1) ##' ##' @export scde.posteriors <- function(models, counts, prior, n.randomizations = 100, batch = NULL, composition = NULL, return.individual.posteriors = FALSE, return.individual.posterior.modes = FALSE, ensemble.posterior = FALSE, n.cores = 20) { if(!all(rownames(models) %in% colnames(counts))) { stop("ERROR: provided count data does not cover all of the cells specified in the model matrix") } if(!is.null(batch)) { # calculating batch-sampled posteriors instead of evenly sampled ones if(is.null(composition)) { stop("ERROR: group composition must be provided if the batch argument is passed") } batchil <- tapply(c(1:nrow(models))-1, batch, I) } # order counts according to the cells ci <- match(rownames(models), colnames(counts)) counts <- as.matrix(counts[, ci, drop = FALSE]) marginals <- 10^prior$x - 1 marginals[marginals<0] <- 0 marginals <- log(marginals) min.slope <- 1e-10 if(any(models$corr.a 1 && nrow(counts) > n.cores) { # split by genes xl <- papply(chunk(seq_len(nrow(counts)), n.cores), function(ii) { ucl <- lapply(seq_len(ncol(counts)), function(i) as.vector(unique(counts[ii, i, drop = FALSE]))) uci <- do.call(cbind, lapply(seq_len(ncol(counts)), function(i) match(counts[ii, i, drop = FALSE], ucl[[i]])-1)) #x <- logBootPosterior(models, ucl, uci, marginals, n.randomizations, 1, postflag) if(!is.null(batch)) { x <- .Call("logBootBatchPosterior", mm, ucl, uci, marginals, batchil, composition, n.randomizations, ii[1], postflag, localthetaflag, squarelogitconc, PACKAGE = "scde") } else { x <- .Call("logBootPosterior", mm, ucl, uci, marginals, n.randomizations, ii[1], postflag, localthetaflag, squarelogitconc, ensembleflag, PACKAGE = "scde") } }, n.cores = n.cores) if(postflag == 0) { x <- do.call(rbind, xl) } else if(postflag == 1) { x <- list(jp = do.call(rbind, lapply(xl, function(d) d$jp)), modes = do.call(rbind, lapply(xl, function(d) d$modes))) } else if(postflag == 2) { x <- list(jp = do.call(rbind, lapply(xl, function(d) d$jp)), post = lapply(seq_along(xl[[1]]$post), function(pi) { do.call(rbind, lapply(xl, function(d) d$post[[pi]])) })) } else if(postflag == 3) { x <- list(jp = do.call(rbind, lapply(xl, function(d) d$jp)), modes = do.call(rbind, lapply(xl, function(d) d$modes)), post = lapply(seq_along(xl[[1]]$post), function(pi) { do.call(rbind, lapply(xl, function(d) d$post[[pi]])) })) } rm(xl) gc() } else { # unique count lists with matching indices ucl <- lapply(seq_len(ncol(counts)), function(i) as.vector(unique(counts[, i, drop = FALSE]))) uci <- do.call(cbind, lapply(seq_len(ncol(counts)), function(i) match(counts[, i, drop = FALSE], ucl[[i]])-1)) #x <- logBootPosterior(models, ucl, uci, marginals, n.randomizations, 1, postflag) if(!is.null(batch)) { x <- .Call("logBootBatchPosterior", mm, ucl, uci, marginals, batchil, composition, n.randomizations, 1, postflag, localthetaflag, squarelogitconc, PACKAGE = "scde") } else { x <- .Call("logBootPosterior", mm, ucl, uci, marginals, n.randomizations, 1, postflag, localthetaflag, squarelogitconc, ensembleflag, PACKAGE = "scde") } } if(postflag == 0) { rownames(x) <- rownames(counts) colnames(x) <- as.character(exp(marginals)) } else if(postflag == 1) { rownames(x$jp) <- rownames(counts) colnames(x$jp) <- as.character(exp(marginals)) rownames(x$modes) <- rownames(counts) colnames(x$modes) <- rownames(models) } else if(postflag == 2) { rownames(x$jp) <- rownames(counts) colnames(x$jp) <- as.character(exp(marginals)) names(x$post) <- rownames(models) x$post <- lapply(x$post, function(d) { rownames(d) <- rownames(counts) colnames(d) <- as.character(exp(marginals)) return(d) }) } else if(postflag == 3) { rownames(x$jp) <- rownames(counts) colnames(x$jp) <- as.character(exp(marginals)) rownames(x$modes) <- rownames(counts) colnames(x$modes) <- rownames(models) names(x$post) <- rownames(models) x$post <- lapply(x$post, function(d) { rownames(d) <- rownames(counts) colnames(d) <- as.character(exp(marginals)) return(d) }) } return(x) } # get estimates of expression magnitude for a given set of models # models - entire model matrix, or a subset of cells (i.e. select rows) of the model matrix for which the estimates should be obtained # counts - count data that covers the desired set of genes (rows) and all specified cells (columns) # return - a matrix of log(FPM) estimates with genes as rows and cells as columns (in the model matrix order). ##' Return scaled expression magnitude estimates ##' ##' Return point estimates of expression magnitudes of each gene across a set of cells, based on the regression slopes determined during the model fitting procedure. ##' ##' @param models models determined by \code{\link{scde.error.models}} ##' @param counts count matrix ##' ##' @return a matrix of expression magnitudes on a log scale (rows - genes, columns - cells) ##' ##' @examples ##' data(es.mef.small) ##' cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) ##' data(o.ifm) # Load precomputed model. Use ?scde.error.models to see how o.ifm was generated ##' # get expression magnitude estimates ##' lfpm <- scde.expression.magnitude(o.ifm, cd) ##' ##' @export scde.expression.magnitude <- function(models, counts) { if(!all(rownames(models) %in% colnames(counts))) { stop("ERROR: provided count data does not cover all of the cells specified in the model matrix") } t((t(log(counts[, rownames(models), drop = FALSE]))-models$corr.b)/models$corr.a) } # calculate drop-out probability given either count data or magnitudes (log(FPM)) # magnitudes can either be a per-cell matrix or a single vector of values which will be evaluated for each cell # returns a probability of a drop out event for every gene (rows) for every cell (columns) ##' Calculate drop-out probabilities given a set of counts or expression magnitudes ##' ##' Returns estimated drop-out probability for each cell (row of \code{models} matrix), given either an expression magnitude ##' @param models models determined by \code{\link{scde.error.models}} ##' @param magnitudes a vector (\code{length(counts) == nrows(models)}) or a matrix (columns correspond to cells) of expression magnitudes, given on a log scale ##' @param counts a vector (\code{length(counts) == nrows(models)}) or a matrix (columns correspond to cells) of read counts from which the expression magnitude should be estimated ##' ##' @return a vector or a matrix of drop-out probabilities ##' ##' @examples ##' data(es.mef.small) ##' cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) ##' data(o.ifm) # Load precomputed model. Use ?scde.error.models to see how o.ifm was generated ##' o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) ##' # calculate probability of observing a drop out at a given set of magnitudes in different cells ##' mags <- c(1.0, 1.5, 2.0) ##' p <- scde.failure.probability(o.ifm, magnitudes = mags) ##' # calculate probability of observing the dropout at a magnitude corresponding to the ##' # number of reads actually observed in each cell ##' self.p <- scde.failure.probability(o.ifm, counts = cd) ##' ##' @export scde.failure.probability <- function(models, magnitudes = NULL, counts = NULL) { if(is.null(magnitudes)) { if(!is.null(counts)) { magnitudes <- scde.expression.magnitude(models, counts) } else { stop("ERROR: either magnitudes or counts should be provided") } } if(is.matrix(magnitudes)) { # a different vector for every cell if(!all(rownames(models) %in% colnames(magnitudes))) { stop("ERROR: provided magnitude data does not cover all of the cells specified in the model matrix") } if("conc.a2" %in% names(models)) { x <- t(1/(exp(t(magnitudes)*models$conc.a +t(magnitudes^2)*models$conc.a2 + models$conc.b)+1)) } else { x <- t(1/(exp(t(magnitudes)*models$conc.a + models$conc.b)+1)) } } else { # a common vector of magnitudes for all cells if("conc.a2" %in% names(models)) { x <- t(1/(exp((models$conc.a %*% t(magnitudes)) + (models$conc.a2 %*% t(magnitudes^2)) + models$conc.b)+1)) } else { x <- t(1/(exp((models$conc.a %*% t(magnitudes)) + models$conc.b)+1)) } } x[is.nan(x)] <- 0 colnames(x) <- rownames(models) x } ##' Test differential expression and plot posteriors for a particular gene ##' ##' The function performs differential expression test and optionally plots posteriors for a specified gene. ##' ##' @param gene name of the gene to be tested ##' @param models models ##' @param counts read count matrix (must contain the row corresponding to the specified gene) ##' @param prior expression magnitude prior ##' @param groups a two-level factor specifying between which cells (rows of the models matrix) the comparison should be made ##' @param batch optional multi-level factor assigning the cells (rows of the model matrix) to different batches that should be controlled for (e.g. two or more biological replicates). The expression difference estimate will then take into account the likely difference between the two groups that is explained solely by their difference in batch composition. Not all batch configuration may be corrected this way. ##' @param batch.models optional set of models for batch comparison (typically the same as models, but can be more extensive, or recalculated within each batch) ##' @param n.randomizations number of bootstrap/sampling iterations that should be performed ##' @param show.plots whether the plots should be shown ##' @param return.details whether the posterior should be returned ##' @param verbose set to T for some status output ##' @param ratio.range optionally specifies the range of the log2 expression ratio plot ##' @param show.individual.posteriors whether the individual cell expression posteriors should be plotted ##' @param n.cores number of cores to use (default = 1) ##' ##' @return by default returns MLE of log2 expression difference, 95% CI (upper, lower bound), and a Z-score testing for expression difference. If return.details = TRUE, a list is returned containing the above structure, as well as the expression fold difference posterior itself. ##' ##' @examples ##' data(es.mef.small) ##' cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) ##' data(o.ifm) # Load precomputed model. Use ?scde.error.models to see how o.ifm was generated ##' o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) ##' scde.test.gene.expression.difference("Tdh", models = o.ifm, counts = cd, prior = o.prior) ##' ##' @export scde.test.gene.expression.difference <- function(gene, models, counts, prior, groups = NULL, batch = NULL, batch.models = models, n.randomizations = 1e3, show.plots = TRUE, return.details = FALSE, verbose = FALSE, ratio.range = NULL, show.individual.posteriors = TRUE, n.cores = 1) { if(!gene %in% rownames(counts)) { stop("ERROR: specified gene (", gene, ") is not found in the count data") } ci <- match(rownames(models), colnames(counts)) counts <- as.matrix(counts[gene, ci, drop = FALSE]) if(is.null(groups)) { # recover groups from models groups <- as.factor(attr(models, "groups")) if(is.null(groups)) stop("ERROR: groups factor is not provided, and models structure is lacking groups attribute") names(groups) <- rownames(models) } if(length(levels(groups)) != 2) { stop(paste("ERROR: wrong number of levels in the grouping factor (", paste(levels(groups), collapse = " "), "), but must be two.", sep = "")) } if(verbose) { cat("comparing gene ", gene, " between groups:\n") print(table(as.character(groups))) } # calculate joint posteriors jpl <- tapply(seq_len(nrow(models)), groups, function(ii) { scde.posteriors(models = models[ii, , drop = FALSE], counts = counts[, ii, drop = FALSE], prior = prior, n.cores = n.cores, n.randomizations = n.randomizations, return.individual.posteriors = TRUE) }) bdiffp <- calculate.ratio.posterior(jpl[[1]]$jp, jpl[[2]]$jp, prior, n.cores = n.cores) bdiffp.rep <- quick.distribution.summary(bdiffp) nam1 <- levels(groups)[1] nam2 <- levels(groups)[2] # batch control correct.batch <- !is.null(batch) && length(levels(batch)) > 1 if(correct.batch) { batch <- as.factor(batch) # check batch-group interactions bgti <- table(groups, batch) bgti.ft <- fisher.test(bgti) if(verbose) { cat("controlling for batch effects. interaction:\n") } if(any(bgti == 0)) { cat("ERROR: cannot control for batch effect, as some batches are found only in one group:\n") print(bgti) } if(bgti.ft$p.value<1e-3) { cat("WARNING: strong interaction between groups and batches! Correction may be ineffective:\n") print(bgti) print(bgti.ft) } # calculate batch posterior batch.jpl <- tapply(seq_len(nrow(models)), groups, function(ii) { scde.posteriors(models = batch.models, counts = counts, prior = prior, batch = batch, composition = table(batch[ii]), n.cores = n.cores, n.randomizations = n.randomizations, return.individual.posteriors = FALSE) }) batch.bdiffp <- calculate.ratio.posterior(batch.jpl[[1]], batch.jpl[[2]], prior, n.cores = n.cores) a.bdiffp <- calculate.ratio.posterior(bdiffp, batch.bdiffp, prior = data.frame(x = as.numeric(colnames(bdiffp)), y = rep(1/ncol(bdiffp), ncol(bdiffp))), skip.prior.adjustment = TRUE) a.bdiffp.rep <- quick.distribution.summary(a.bdiffp) } if(show.plots) { # show each posterior layout(matrix(c(1:3), 3, 1, byrow = TRUE), heights = c(2, 1, 2), widths = c(1), FALSE) par(mar = c(2.5, 3.5, 2.5, 3.5), mgp = c(1.5, 0.65, 0), cex = 0.9) #par(mar = c(2.5, 3.5, 0.5, 3.5), mgp = c(1.5, 0.65, 0), cex = 0.9) pp <- exp(do.call(rbind, lapply(jpl[[1]]$post, as.numeric))) cols <- rainbow(nrow(pp), s = 0.8) plot(c(), c(), xlim = range(prior$x), ylim = range(c(0, pp)), xlab = "expression level", ylab = "individual posterior", main = nam1) if(show.individual.posteriors) { lapply(seq_len(nrow(pp)), function(i) lines(prior$x, pp[i, ], col = rgb(1, 0.5, 0, alpha = 0.25))) } #legend(x = ifelse(which.max(na.omit(pjpc)) > length(pjpc)/2, "topleft", "topright"), bty = "n", col = cols, legend = rownames(pp), lty = rep(1, nrow(pp))) if(correct.batch) { par(new = TRUE) plot(prior$x, batch.jpl[[1]][1, ], axes = FALSE, ylab = "", xlab = "", type = 'l', col = 8, lty = 1, lwd = 2) } pjpc <- jpl[[1]]$jp par(new = TRUE) jpr <- range(c(0, na.omit(pjpc))) plot(prior$x, pjpc, axes = FALSE, ylab = "", xlab = "", ylim = jpr, type = 'l', col = 1, lty = 1, lwd = 2) axis(4, pretty(jpr, 5), col = 1) mtext("joint posterior", side = 4, outer = FALSE, line = 2) # ratio plot if(is.null(ratio.range)) { ratio.range <- range(as.numeric(colnames(bdiffp))/log10(2)) } par(mar = c(2.5, 3.5, 0.5, 3.5), mgp = c(1.5, 0.65, 0), cex = 0.9) rv <- as.numeric(colnames(bdiffp))/log10(2) rp <- as.numeric(bdiffp[1, ]) plot(rv, rp, xlab = "log2 expression ratio", ylab = "ratio posterior", type = 'l', lwd = ifelse(correct.batch, 1, 2), main = "", axes = FALSE, xlim = ratio.range, ylim = c(0, max(bdiffp))) axis(1, pretty(ratio.range, 5), col = 1) abline(v = 0, lty = 2, col = 8) if(correct.batch) { # with batch correction # show batch difference par(new = TRUE) plot(as.numeric(colnames(batch.bdiffp))/log10(2), as.numeric(batch.bdiffp[1, ]), xlab = "", ylab = "", type = 'l', lwd = 1, main = "", axes = FALSE, xlim = ratio.range, col = 8, ylim = c(0, max(batch.bdiffp))) # fill out the a.bdiffp confidence interval par(new = TRUE) rv <- as.numeric(colnames(a.bdiffp))/log10(2) rp <- as.numeric(a.bdiffp[1, ]) plot(rv, rp, xlab = "", ylab = "", type = 'l', lwd = 2, main = "", axes = FALSE, xlim = ratio.range, col = 2, ylim = c(0, max(rp))) axis(2, pretty(c(0, max(a.bdiffp)), 2), col = 1) r.lb <- which.min(abs(rv-a.bdiffp.rep$lb)) r.ub <- which.min(abs(rv-a.bdiffp.rep$ub)) polygon(c(rv[r.lb], rv[r.lb:r.ub], rv[r.ub]), y = c(-10, rp[r.lb:r.ub], -10), col = rgb(1, 0, 0, alpha = 0.2), border = NA) abline(v = a.bdiffp.rep$mle, col = 2, lty = 2) abline(v = c(rv[r.ub], rv[r.lb]), col = 2, lty = 3) legend(x = ifelse(a.bdiffp.rep$mle > 0, "topleft", "topright"), legend = c(paste("MLE: ", round(a.bdiffp.rep$mle, 2), sep = ""), paste("95% CI: ", round(a.bdiffp.rep$lb, 2), " : ", round(a.bdiffp.rep$ub, 2), sep = ""), paste("Z = ", round(a.bdiffp.rep$Z, 2), sep = ""), paste("cZ = ", round(a.bdiffp.rep$cZ, 2), sep = "")), bty = "n") } else { # without batch correction # fill out the bdiffp confidence interval axis(2, pretty(c(0, max(bdiffp)), 2), col = 1) r.lb <- which.min(abs(rv-bdiffp.rep$lb)) r.ub <- which.min(abs(rv-bdiffp.rep$ub)) polygon(c(rv[r.lb], rv[r.lb:r.ub], rv[r.ub]), y = c(-10, rp[r.lb:r.ub], -10), col = rgb(1, 0, 0, alpha = 0.2), border = NA) abline(v = bdiffp.rep$mle, col = 2, lty = 2) abline(v = c(rv[r.ub], rv[r.lb]), col = 2, lty = 3) legend(x = ifelse(bdiffp.rep$mle > 0, "topleft", "topright"), legend = c(paste("MLE: ", round(bdiffp.rep$mle, 2), sep = ""), paste("95% CI: ", round(bdiffp.rep$lb, 2), " : ", round(bdiffp.rep$ub, 2), sep = ""), paste("Z = ", round(bdiffp.rep$Z, 2), sep = ""), paste("aZ = ", round(bdiffp.rep$cZ, 2), sep = "")), bty = "n") } # distal plot par(mar = c(2.5, 3.5, 2.5, 3.5), mgp = c(1.5, 0.65, 0), cex = 0.9) #par(mar = c(2.5, 3.5, 0.5, 3.5), mgp = c(1.5, 0.65, 0), cex = 0.9) dp <- exp(do.call(rbind, lapply(jpl[[2]]$post, as.numeric))) cols <- rainbow(nrow(dp), s = 0.8) plot(c(), c(), xlim = range(prior$x), ylim = range(c(0, dp)), xlab = "expression level", ylab = "individual posterior", main = nam2) if(show.individual.posteriors) { lapply(seq_len(nrow(dp)), function(i) lines(prior$x, dp[i, ], col = rgb(0, 0.5, 1, alpha = 0.25))) } if(correct.batch) { par(new = TRUE) plot(prior$x, batch.jpl[[2]][1, ], axes = FALSE, ylab = "", xlab = "", type = 'l', col = 8, lty = 1, lwd = 2) } djpc <- jpl[[2]]$jp #legend(x = ifelse(which.max(na.omit(djpc)) > length(djpc)/2, "topleft", "topright"), bty = "n", col = cols, legend = rownames(dp), lty = rep(1, nrow(dp))) par(new = TRUE) jpr <- range(c(0, na.omit(djpc))) plot(prior$x, djpc, axes = FALSE, ylab = "", xlab = "", ylim = jpr, type = 'l', col = 1, lty = 1, lwd = 2) axis(4, pretty(jpr, 5), col = 1) mtext("joint posterior", side = 4, outer = FALSE, line = 2) } if(return.details) { if(correct.batch) { # with batch correction return(list(results = a.bdiffp.rep, difference.posterior = a.bdiffp, results.nobatchcorrection = bdiffp.rep)) } else { return(list(results = bdiffp.rep, difference.posterior = bdiffp, posteriors = jpl)) } } else { if(correct.batch) { # with batch correction return(a.bdiffp.rep) } else { return(bdiffp.rep) } } } # fit models to external (bulk) reference ##' Fit scde models relative to provided set of expression magnitudes ##' ##' If group-average expression magnitudes are available (e.g. from bulk measurement), this method can be used ##' to fit individual cell error models relative to that reference ##' ##' @param counts count matrix ##' @param reference a vector of expression magnitudes (read counts) corresponding to the rows of the count matrix ##' @param min.fpm minimum reference fpm of genes that will be used to fit the models (defaults to 1). Note: fpm is calculated from the reference count vector as reference/sum(reference)*1e6 ##' @param n.cores number of cores to use ##' @param zero.count.threshold read count to use as an initial guess for the zero threshold ##' @param nrep number independent of mixture fit iterations to try (default = 1) ##' @param save.plots whether to write out a pdf file showing the model fits ##' @param plot.filename model fit pdf filename ##' @param verbose verbose level ##' ##' @return matrix of scde models ##' ##' @examples ##' data(es.mef.small) ##' cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) ##' \donttest{ ##' o.ifm <- scde.error.models(counts = cd, groups = sg, n.cores = 10, threshold.segmentation = TRUE) ##' o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) ##' # calculate joint posteriors across all cells ##' jp <- scde.posteriors(models = o.ifm, cd, o.prior, n.cores = 10, return.individual.posterior.modes = TRUE, n.randomizations = 100) ##' # use expected expression magnitude for each gene ##' av.mag <- as.numeric(jp$jp %*% as.numeric(colnames(jp$jp))) ##' # translate into counts ##' av.mag.counts <- as.integer(round(av.mag)) ##' # now, fit alternative models using av.mag as a reference (normally this would correspond to bulk RNA expression magnitude) ##' ref.models <- scde.fit.models.to.reference(cd, av.mag.counts, n.cores = 1) ##' } ##' ##' @export scde.fit.models.to.reference <- function(counts, reference, n.cores = 10, zero.count.threshold = 1, nrep = 1, save.plots = FALSE, plot.filename = "reference.model.fits.pdf", verbose = 0, min.fpm = 1) { return.compressed.models <- TRUE verbose <- 1 ids <- colnames(counts) ml <- papply(seq_along(ids), function(i) { df <- data.frame(count = counts[, ids[i]], fpm = reference/sum(reference)*1e6) df <- df[df$fpm > min.fpm, ] m1 <- fit.nb2.mixture.model(df, nrep = nrep, verbose = verbose, zero.count.threshold = zero.count.threshold) if(return.compressed.models) { v <- get.compressed.v1.model(m1) cl <- clusters(m1) rm(m1) gc() return(list(model = v, clusters = cl)) } else { return(m1) } }, n.cores = n.cores) names(ml) <- ids # check if there were errors in the multithreaded portion lapply(seq_along(ml), function(i) { if(class(ml[[i]]) == "try-error") { message("ERROR encountered in building a model for cell ", ids[i], ":") message(ml[[i]]) tryCatch(stop(paste("ERROR encountered in building a model for cell ", ids[i])), error = function(e) stop(e)) } }) if(save.plots) { # model fits #CairoPNG(file = paste(group, "model.fits.png", sep = "."), width = 1024, height = 300*length(ids)) pdf(file = plot.filename, width = 13, height = 4) #l <- layout(matrix(seq(1, 4*length(ids)), nrow = length(ids), byrow = TRUE), rep(c(1, 1, 1, 0.5), length(ids)), rep(1, 4*length(ids)), FALSE) l <- layout(matrix(seq(1, 4), nrow = 1, byrow = TRUE), rep(c(1, 1, 1, 0.5), 1), rep(1, 4), FALSE) par(mar = c(3.5, 3.5, 3.5, 0.5), mgp = c(2.0, 0.65, 0), cex = 0.9) invisible(lapply(seq_along(ids), function(i) { df <- data.frame(count = counts[, ids[i]], fpm = reference/sum(reference)*1e6) df <- df[df$fpm > min.fpm, ] plot.nb2.mixture.fit(ml[[i]], df, en = ids[i], do.par = FALSE, compressed.models = return.compressed.models) })) dev.off() } if(return.compressed.models) { # make a joint model matrix jmm <- data.frame(do.call(rbind, lapply(ml, function(m) m$model))) rownames(jmm) <- names(ml) jmm return(jmm) } else { return(ml) } } ##' Determine principal components of a matrix using per-observation/per-variable weights ##' ##' Implements a weighted PCA ##' ##' @param mat matrix of variables (columns) and observations (rows) ##' @param matw corresponding weights ##' @param npcs number of principal components to extract ##' @param nstarts number of random starts to use ##' @param smooth smoothing span ##' @param em.tol desired EM algorithm tolerance ##' @param em.maxiter maximum number of EM iterations ##' @param seed random seed ##' @param center whether mat should be centered (weighted centering) ##' @param n.shuffles optional number of per-observation randomizations that should be performed in addition to the main calculations to determine the lambda1 (PC1 eigenvalue) magnitude under such randomizations (returned in $randvar) ##' ##' @return a list containing eigenvector matrix ($rotation), projections ($scores), variance (weighted) explained by each component ($var), total (weighted) variance of the dataset ($totalvar) ##' ##' @examples ##' set.seed(0) ##' mat <- matrix( c(rnorm(5*10,mean=0,sd=1), rnorm(5*10,mean=5,sd=1)), 10, 10) # random matrix ##' base.pca <- bwpca(mat) # non-weighted pca, equal weights set automatically ##' matw <- matrix( c(rnorm(5*10,mean=0,sd=1), rnorm(5*10,mean=5,sd=1)), 10, 10) # random weight matrix ##' matw <- abs(matw)/max(matw) ##' base.pca.weighted <- bwpca(mat, matw) # weighted pca ##' ##' @export bwpca <- function(mat, matw = NULL, npcs = 2, nstarts = 1, smooth = 0, em.tol = 1e-6, em.maxiter = 25, seed = 1, center = TRUE, n.shuffles = 0) { if(smooth<4) { smooth <- 0 } if(any(is.nan(matw))) { stop("bwpca: weight matrix contains NaN values") } if(any(is.nan(mat))) { stop("bwpca: value matrix contains NaN values") } if(is.null(matw)) { matw <- matrix(1, nrow(mat), ncol(mat)) nstarts <- 1 } if(center) { mat <- t(t(mat)-colSums(mat*matw)/colSums(matw)) } res <- .Call("baileyWPCA", mat, matw, npcs, nstarts, smooth, em.tol, em.maxiter, seed, n.shuffles, PACKAGE = "scde") #res <- bailey.wpca(mat, matw, npcs, nstarts, smooth, em.tol, em.maxiter, seed) rownames(res$rotation) <- colnames(mat) rownames(res$scores) <- rownames(mat) colnames(res$rotation) <- paste("PC", seq(1:ncol(res$rotation)), sep = "") res$sd <- t(sqrt(res$var)) res } ##' Winsorize matrix ##' ##' Sets the ncol(mat)*trim top outliers in each row to the next lowest value same for the lowest outliers ##' ##' @param mat matrix ##' @param trim fraction of outliers (on each side) that should be Winsorized, or (if the value is >= 1) the number of outliers to be trimmed on each side ##' ##' @return Winsorized matrix ##' ##' @examples ##' set.seed(0) ##' mat <- matrix( c(rnorm(5*10,mean=0,sd=1), rnorm(5*10,mean=5,sd=1)), 10, 10) # random matrix ##' mat[1,1] <- 1000 # make outlier ##' range(mat) # look at range of values ##' win.mat <- winsorize.matrix(mat, 0.1) ##' range(win.mat) # note outliers removed ##' ##' @export winsorize.matrix <- function(mat, trim) { if(trim > 0.5) { trim <- trim/ncol(mat) } wm <- .Call("winsorizeMatrix", mat, trim, PACKAGE = "scde") rownames(wm) <- rownames(mat) colnames(wm) <- colnames(mat) return(wm) } ############################ PAGODA functions ##' Build error models for heterogeneous cell populations, based on K-nearest neighbor cells. ##' ##' Builds cell-specific error models assuming that there are multiple subpopulations present ##' among the measured cells. The models for each cell are based on average expression estimates ##' obtained from K closest cells within a given group (if groups = NULL, then within the entire ##' set of measured cells). The method implements fitting of both the original log-fit models ##' (when linear.fit = FALSE), or newer linear-fit models (linear.fit = TRUE, default) with locally ##' fit overdispersion coefficient (local.theta.fit = TRUE, default). ##' ##' @param counts count matrix (integer matrix, rows- genes, columns- cells) ##' @param groups optional groups partitioning known subpopulations ##' @param cor.method correlation measure to be used in determining k nearest cells ##' @param k number of nearest neighbor cells to use during fitting. If k is set sufficiently high, all of the cells within a given group will be used. ##' @param min.nonfailed minimum number of non-failed measurements (within the k nearest neighbor cells) required for a gene to be taken into account during error fitting procedure ##' @param min.size.entries minimum number of genes to use for model fitting ##' @param min.count.threshold minimum number of reads required for a measurement to be considered non-failed ##' @param save.model.plots whether model plots should be saved (file names are (group).models.pdf, or cell.models.pdf if no group was supplied) ##' @param max.model.plots maximum number of models to save plots for (saves time when there are too many cells) ##' @param n.cores number of cores to use through the calculations ##' @param min.fpm optional parameter to restrict model fitting to genes with group-average expression magnitude above a given value ##' @param verbose level of verbosity ##' @param fpm.estimate.trim trim fraction to be used in estimating group-average gene expression magnitude for model fitting (0.5 would be median, 0 would turn off trimming) ##' @param linear.fit whether newer linear model fit with zero intercept should be used (T), or the log-fit model published originally (F) ##' @param local.theta.fit whether local theta fitting should be used (only available for the linear fit models) ##' @param theta.fit.range allowed range of the theta values ##' @param alpha.weight.power 1/theta weight power used in fitting theta dependency on the expression magnitude ##' ##' @return a data frame with parameters of the fit error models (rows- cells, columns- fitted parameters) ##' ##' @examples ##' data(pollen) ##' cd <- clean.counts(pollen) ##' \donttest{ ##' knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) ##' } ##' ##' @export knn.error.models <- function(counts, groups = NULL, k = round(ncol(counts)/2), min.nonfailed = 5, min.count.threshold = 1, save.model.plots = TRUE, max.model.plots = 50, n.cores = parallel::detectCores(), min.size.entries = 2e3, min.fpm = 0, cor.method = "pearson", verbose = 0, fpm.estimate.trim = 0.25, linear.fit = TRUE, local.theta.fit = linear.fit, theta.fit.range = c(1e-2, 1e2), alpha.weight.power = 1/2) { threshold.prior = 1-1e-6 # check for integer counts if(any(!unlist(lapply(counts,is.integer)))) { stop("Some of the supplied counts are not integer values (or stored as non-integer types). Aborting!\nThe method is designed to work on read counts - do not pass normalized read counts (e.g. FPKM values). If matrix contains read counts, but they are stored as numeric values, use counts<-apply(counts,2,function(x) {storage.mode(x) <- 'integer'; x}) to recast."); } # TODO: # - implement check for k >= n.cells (to avoid correlation calculations) # - implement error reporting/handling for failed cell fits if(is.null(groups)) { groups <- as.factor(rep("cell", ncol(counts))) } names(groups) <- colnames(counts) if(k > ncol(counts)-1) { message("the value of k (", k, ") is too large, setting to ", (ncol(counts)-1)) k <- ncol(counts)-1 } ls <- estimate.library.sizes(counts, NULL, groups, min.size.entries, verbose = verbose, return.details = TRUE, vil = counts >= min.count.threshold) ca <- counts ca[ca 0) { cat(group, ": calculating cell-cell similarities ...") } #if(n.cores > 1) { allowWGCNAThreads(n.cores) } else { disableWGCNAThreads() } #celld <- WGCNA::cor(log10(matrix(as.numeric(as.matrix(ca)), nrow = nrow(ca), ncol = ncol(ca))+1), method = cor.method, use = "p", nThreads = n.cores) if(is.element("WGCNA", installed.packages()[, 1])) { celld <- WGCNA::cor(sqrt(matrix(as.numeric(as.matrix(ca[, ids])), nrow = nrow(ca), ncol = length(ids))), method = cor.method, use = "p", nThreads = n.cores) } else { celld <- stats::cor(sqrt(matrix(as.numeric(as.matrix(ca[, ids])), nrow = nrow(ca), ncol = length(ids))), method = cor.method, use = "p") } rownames(celld) <- colnames(celld) <- ids if(verbose > 0) { cat(" done\n") } # TODO: correct for batch effect in cell-cell similarity matrix if(FALSE) { # number batches 10^(seq(0, n)) compute matrix of id sums, NA the diagonal, bid <- 10^(as.integer(batch)-1) bm <- matrix(bid, byrow = TRUE, nrow = length(bid), ncol = length(bid))+bid diag(bm) <- NA # use tapply to calculate means shifts per combination reconstruct shift vector, matrix, subtract # select the upper triangle, tapply to it to correct celld vector directly } if(verbose) message(paste("fitting", group, "models:")) ml <- papply(seq_along(ids), function(i) { try({ if(verbose) message(paste(group, '.', i, " : ", ids[i], sep = "")) # determine k closest cells oc <- ids[-i][order(celld[ids[i], -i, drop = FALSE], decreasing = TRUE)[1:min(k, length(ids)-1)]] #set.seed(i) oc <- sample(ids[-i], k) # determine a subset of genes that show up sufficiently often #fpm <- rowMeans(t(t(counts[, oc, drop = FALSE])/(ls$ls[oc]))) fpm <- apply(t(ca[, oc, drop = FALSE])/(ls$ls[oc]), 2, mean, trim = fpm.estimate.trim, na.rm = TRUE) # rank genes by the number of non-zero occurrences, take top genes vi <- which(rowSums(counts[, oc] > min.count.threshold) >= min(ncol(oc)-1, min.nonfailed) & fpm > min.fpm) if(length(vi)<40) message("WARNING: only ", length(vi), " valid genes were found to fit ", ids[i], " model") df <- data.frame(count = counts[vi, ids[i]], fpm = fpm[vi]) # determine failed-component posteriors for each gene #fp <- ifelse(df$count <= min.count.threshold, threshold.prior, 1-threshold.prior) fp <- ifelse(df$count <= min.count.threshold & df$fpm >= median(df$fpm[df$count <= min.count.threshold]), threshold.prior, 1-threshold.prior) cp <- cbind(fp, 1-fp) if(linear.fit) { # use a linear fit (nb2gth) m1 <- fit.nb2gth.mixture.model(df, prior = cp, nrep = 1, verbose = verbose, zero.count.threshold = min.count.threshold, full.theta.range = theta.fit.range, theta.fit.range = theta.fit.range, use.constant.theta.fit = !local.theta.fit, alpha.weight.power = alpha.weight.power) } else { # mixture fit (the originally published method) m1 <- fit.nb2.mixture.model(df, prior = cp, nrep = 1, verbose = verbose, zero.count.threshold = min.count.threshold) } v <- get.compressed.v1.model(m1) cl <- clusters(m1) m1<-list(model = v, clusters = cl) #plot.nb2.mixture.fit(m1, df, en = ids[i], do.par = FALSE, compressed.models = TRUE) return(m1) #}) })}, n.cores = n.cores) vic <- which(unlist(lapply(seq_along(ml), function(i) { if(class(ml[[i]]) == "try-error") { message("ERROR encountered in building a model for cell ", ids[i], " - skipping the cell. Error:") message(ml[[i]]) #tryCatch(stop(paste("ERROR encountered in building a model for cell ", ids[i])), error = function(e) stop(e)) return(FALSE); } return(TRUE); }))) ml <- ml[vic]; names(ml) <- ids[vic]; if(length(vic) min.count.threshold) >= min(ncol(oc)-1, min.nonfailed) & fpm > min.fpm) df <- data.frame(count = counts[vi, ids[i]], fpm = fpm[vi]) plot.nb2.mixture.fit(ml[[ids[i]]], df, en = ids[i], do.par = FALSE, compressed.models = TRUE) })) dev.off() }, error = function(e) { message("ERROR encountered during model fit plot outputs:") message(e) dev.off() }) } return(ml) }) # make a joint model matrix jmm <- data.frame(do.call(rbind, lapply(mll, function(tl) do.call(rbind, lapply(tl, function(m) m$model))))) rownames(jmm) <- unlist(lapply(mll, names)) # reorder in the original cell order attr(jmm, "groups") <- rep(names(mll), unlist(lapply(mll, length))) return(jmm) } ##' Normalize gene expression variance relative to transcriptome-wide expectations ##' ##' Normalizes gene expression magnitudes to ensure that the variance follows chi-squared statistics ##' with respect to its ratio to the transcriptome-wide expectation as determined by local regression ##' on expression magnitude (and optionally gene length). Corrects for batch effects. ##' ##' @param models model matrix (select a subset of rows to normalize variance within a subset of cells) ##' @param counts read count matrix ##' @param batch measurement batch (optional) ##' @param trim trim value for Winsorization (optional, can be set to 1-3 to reduce the impact of outliers, can be as large as 5 or 10 for datasets with several thousand cells) ##' @param prior expression magnitude prior ##' @param fit.genes a vector of gene names which should be used to establish the variance fit (default is NULL: use all genes). This can be used to specify, for instance, a set spike-in control transcripts such as ERCC. ##' @param plot whether to plot the results ##' @param minimize.underdispersion whether underdispersion should be minimized (can increase sensitivity in datasets with high complexity of population, however cannot be effectively used in datasets where multiple batches are present) ##' @param n.cores number of cores to use ##' @param n.randomizations number of bootstrap sampling rounds to use in estimating average expression magnitude for each gene within the given set of cells ##' @param weight.k k value to use in the final weight matrix ##' @param verbose verbosity level ##' @param weight.df.power power factor to use in determining effective number of degrees of freedom (can be increased for datasets exhibiting particularly high levels of noise at low expression magnitudes) ##' @param smooth.df degrees of freedom to be used in calculating smoothed local regression between coefficient of variation and expression magnitude (and gene length, if provided). Leave at -1 for automated guess. ##' @param max.adj.var maximum value allowed for the estimated adjusted variance (capping of adjusted variance is recommended when scoring pathway overdispersion relative to randomly sampled gene sets) ##' @param theta.range valid theta range (should be the same as was set in knn.error.models() call ##' @param gene.length optional vector of gene lengths (corresponding to the rows of counts matrix) ##' ##' @examples ##' data(pollen) ##' cd <- clean.counts(pollen) ##' \donttest{ ##' knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) ##' varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) ##' } ##' ##' @return a list containing the following fields: ##' \itemize{ ##' \item{mat} {adjusted expression magnitude values} ##' \item{matw} { weight matrix corresponding to the expression matrix} ##' \item{arv} { a vector giving adjusted variance values for each gene} ##' \item{avmodes} {a vector estimated average expression magnitudes for each gene} ##' \item{modes} {a list of batch-specific average expression magnitudes for each gene} ##' \item{prior} {estimated (or supplied) expression magnitude prior} ##' \item{edf} { estimated effective degrees of freedom} ##' \item{fit.genes} { fit.genes parameter } ##' } ##' ##' @export pagoda.varnorm <- function(models, counts, batch = NULL, trim = 0, prior = NULL, fit.genes=NULL, plot = TRUE, minimize.underdispersion = FALSE, n.cores = detectCores(), n.randomizations = 100, weight.k = 0.9, verbose = 0, weight.df.power = 1, smooth.df = -1, max.adj.var = 10, theta.range = c(1e-2, 1e2), gene.length = NULL) { cd <- counts min.edf <- 1 weight.k.internal <- 1 use.mean.fpm <- FALSE use.expected.value <- TRUE cv.fit <- TRUE edf.damping <- 1 # load NB extensions data(scde.edff, envir = environment()) # subset cd to the cells occurring in the models if(verbose) { cat("checking counts ... ") } if(!all(rownames(models) %in% colnames(cd))) { stop(paste("supplied count matrix (cd) is missing data for the following cells:[", paste(rownames(models)[!rownames(models) %in% colnames(cd)], collapse = ", "), "]", sep = "")) } if(!length(rownames(models)) == length(colnames(cd)) || !all(rownames(models) == colnames(cd))) { cd <- cd[, match(rownames(models), colnames(cd))] } if(verbose) { cat("done\n") } # trim counts according to the extreme fpm values if(trim > 0) { if(verbose) { cat("Winsorizing count matrix ... ") } fpm <- t((t(log(cd))-models$corr.b)/models$corr.a) #tfpm <- log(winsorize.matrix(exp(fpm), trim = trim)) tfpm <- winsorize.matrix(fpm, trim) rn <- rownames(cd) cn <- colnames(cd) cd <- round(exp(t(t(tfpm)*models$corr.a+models$corr.b))) cd[cd<0] <- 0 rownames(cd) <- rn colnames(cd) <- cn rm(fpm, tfpm) cd <- cd[rowSums(cd) > 0, ] # omit genes without any data after Winsorization if(verbose) { cat("done\n") } } # check/fix batch vector if(verbose) { cat("checking batch ... ") } if(!is.null(batch)) { if(!is.factor(batch)) { batch <- as.factor(batch) } if(is.null(names(batch))) { if(length(batch) != nrow(models)) { stop("invalid batch vector supplied: length differs from nrow(models)!") } names(batch) <- rownames(models) } else { if(!all(rownames(models) %in% names(batch))) { stop(paste("invalid batch vector supplied: the following cell(s) are not present: [", paste(rownames(models)[!rownames(models) %in% names(batch)], collapse = ", "), "]", sep = "")) } batch <- batch[rownames(models)] } bt <- table(batch) min.batch.level <- 2 if(any(bt 1) { # calculate mode for each batch if(verbose) { cat("batch: [ ") } modes <- tapply(seq_len(nrow(models)), batch, function(ii) { if(verbose) { cat(as.character(batch[ii[1]]), " ") } if(use.mean.fpm) { # use mean fpm across cells modes <- rowMeans(exp(scde.expression.magnitude(models[ii, ], cd[, ii]))) } else { # use joint posterior mode jp <- scde.posteriors(models = models[ii, ], cd[, ii], prior, n.cores = n.cores, return.individual.posterior.modes = TRUE, n.randomizations = n.randomizations) if(use.expected.value) { modes <- (jp$jp %*% as.numeric(colnames(jp$jp)))[, 1] } else { # use mode modes <- (as.numeric(colnames(jp$jp)))[max.col(jp$jp)] } } }) # set dataset-wide mode #if(use.mean.fpm) { # use mean fpm across cells # avmodes <- colMeans(do.call(rbind, modes)*as.vector(unlist(tapply(1:length(batch), batch, length))))*length(levels(batch))/length(batch) #jp <- scde.posteriors(models = models, cd, prior, n.cores = n.cores, return.individual.posterior.modes = TRUE, n.randomizations = n.randomizations) if(verbose) { cat("] ") } } if(verbose) { cat("done\n") } # check/calculate weights if(verbose) { cat("calculating weight matrix ... ") } # calculate default weighting scheme if(verbose) { cat("calculating ... ") } # dataset-wide version of matw (disregarding batch) sfp <- do.call(cbind, lapply(seq_len(ncol(cd)), function(i) ppois(cd[, i]-1, exp(models[i, "fail.r"]), lower.tail = FALSE))) mfp <- scde.failure.probability(models = models, magnitudes = log(avmodes)) ofpT <- do.call(cbind, lapply(seq_len(ncol(cd)), function(i) { # for each cell lfpm <- log(avmodes) mu <- models$corr.b[i] + models$corr.a[i]*lfpm thetas <- get.corr.theta(models[i, ], lfpm, theta.range) pnbinom(1, size = thetas, mu = exp(mu), lower.tail = TRUE) })) matw <- 1-weight.k.internal*mfp*sfp # only mode failure probability # mode failure or NB failure #tmfp <- 1-(1-mfp)*(1-ofpT) #matw <- 1-weight.k.internal*tmfp*sfp # calculate batch-specific version of the weight matrix if needed if(!is.null(batch) && length(levels(batch)) > 1) { # with batch correction # save the dataset-wide one as avmatw # calculate mode for each batch if(verbose) { cat("batch: [ ") } bmatw <- do.call(cbind, tapply(seq_len(nrow(models)), batch, function(ii) { if(verbose) { cat(as.character(batch[ii[1]]), " ") } # set self-fail probability to p(count|background) # total mode failure (including overdispersion dropouts) #sfp <- do.call(cbind, lapply(ii, function(i) dpois(cd[, i], exp(models[i, "fail.r"]), log = FALSE))) sfp <- do.call(cbind, lapply(ii, function(i) ppois(cd[, i]-1, exp(models[i, "fail.r"]), lower.tail = FALSE))) mfp <- scde.failure.probability(models = models[ii, ], magnitudes = log(modes[[batch[ii[1]]]])) ofpT <- do.call(cbind, lapply(ii, function(i) { # for each cell lfpm <- log(modes[[batch[i]]]) mu <- models$corr.b[i] + models$corr.a[i]*lfpm thetas <- get.corr.theta(models[i, ], lfpm, theta.range) pnbinom(1, size = thetas, mu = exp(mu), lower.tail = TRUE) })) x <- 1-weight.k.internal*mfp*sfp # only mode failure probability # mode failure or NB failure #tmfp <- 1-(1-mfp)*(1-ofpT) #x <- 1-weight.k.internal*tmfp*sfp })) # reorder bmatw <- bmatw[, rownames(models)] if(verbose) { cat("] ") } } if(verbose) { cat("done\n") } # calculate effective degrees of freedom # total effective degrees of freedom per gene if(verbose) { cat("calculating effective degrees of freedom ..") } ids <- 1:ncol(cd) names(ids) <- colnames(cd) # dataset-wide version edf.mat <- do.call(cbind, papply(ids, function(i) { v <- models[i, ] lfpm <- log(avmodes) mu <- exp(lfpm*v$corr.a + v$corr.b) # adjust very low mu levels except for those that have 0 counts (to avoid inf values) thetas <- get.corr.theta(v, lfpm, theta.range) edf <- exp(predict(scde.edff, data.frame(lt = log(thetas)))) edf[thetas > 1e3] <- 1 edf }, n.cores = n.cores)) if(edf.damping != 1) { edf.mat <- ((edf.mat/ncol(edf.mat))^edf.damping) * ncol(edf.mat) } # incorporate weight into edf #edf.mat <- ((matw^weight.df.power)*edf.mat) edf.mat <- (matw*edf.mat)^weight.df.power #edf <- rowSums(matw*edf.mat)+1.5 # summarize eDF per gene edf <- rowSums(edf.mat)+1 # summarize eDF per gene if(verbose) { cat(".") } # batch-specific version if necessary if(!is.null(batch) && is.list(modes)) { # batch-specific mode bedf.mat <- do.call(cbind, papply(ids, function(i) { v <- models[i, ] lfpm <- log(modes[[batch[i]]]) mu <- exp(lfpm*v$corr.a + v$corr.b) # adjust very low mu levels except for those that have 0 counts (to avoid inf values) thetas <- get.corr.theta(v, lfpm, theta.range) edf <- exp(predict(scde.edff, data.frame(lt = log(thetas)))) edf[thetas > 1e3] <- 1 return(edf) }, n.cores = n.cores)) if(edf.damping != 1) { bedf.mat <- ((bedf.mat/ncol(bedf.mat))^edf.damping) * ncol(edf.mat) } # incorporate weight into edf #bedf.mat <- ((bmatw^weight.df.power)*bedf.mat) bedf.mat <- (bmatw*bedf.mat)^weight.df.power bedf <- rowSums(bedf.mat)+1 # summarize eDF per gene if(verbose) { cat(".") } } if(verbose) { cat(" done\n") } if(verbose) { cat("calculating normalized expression values ... ") } # evaluate negative binomial deviations and effective degrees of freedom ids <- 1:ncol(cd) names(ids) <- colnames(cd) mat <- do.call(cbind, papply(ids, function(i) { v <- models[i, ] lfpm <- log(avmodes) mu <- exp(lfpm*v$corr.a + v$corr.b) # adjust very low mu levels except for those that have 0 counts (to avoid inf values) thetas <- get.corr.theta(v, lfpm, theta.range) #matw[, i]*edf.mat[, i]*(cd[, i]-mu)^2/(mu+mu^2/thetas) #x <- (cd[, i]-mu)^2/(mu+mu^2/thetas) #edf.mat[, i]*(cd[, i]-mu)^2/(mu+mu^2/thetas) # considering Poisson-nb mixture fail.lambda <- exp(as.numeric(v["fail.r"])) #edf.mat[, i]*(cd[, i]-mu)^2/(matw[, i]*(mu+mu^2/thetas) + (1-matw[, i])*((mu-fail.lambda)^2 + fail.lambda)) edf.mat[, i]*(cd[, i]-mu)^2/(mu+mu^2/thetas + fail.lambda) #edf.mat[, i]*(cd[, i]-mu)^2/(matw[, i]*mu+(mu^2)*((1-matw[, i])+matw[, i]/thetas)) }, n.cores = n.cores)) rownames(mat) <- rownames(cd) if(verbose) { cat(".") } # batch-specific version of mat if(!is.null(batch) && is.list(modes)) { # batch-specific mode bmat <- do.call(cbind, papply(ids, function(i) { v <- models[i, ] lfpm <- log(modes[[batch[i]]]) mu <- exp(lfpm*v$corr.a + v$corr.b) # adjust very low mu levels except for those that have 0 counts (to avoid inf values) thetas <- get.corr.theta(v, lfpm, theta.range) #matw[, i]*edf.mat[, i]*(cd[, i]-mu)^2/(mu+mu^2/thetas) #x <- (cd[, i]-mu)^2/(mu+mu^2/thetas) #edf.mat[, i]*(cd[, i]-mu)^2/(mu+mu^2/thetas) #edf.mat[, i]*(cd[, i]-mu)^2/(matw[, i]*mu+(mu^2)*((1-matw[, i])+matw[, i]/thetas)) fail.lambda <- exp(as.numeric(v["fail.r"])) #edf.mat[, i]*(cd[, i]-mu)^2/(matw[, i]*(mu+mu^2/thetas) + (1-matw[, i])*((mu-fail.lambda)^2 + fail.lambda)) edf.mat[, i]*(cd[, i]-mu)^2/(mu+mu^2/thetas + fail.lambda) }, n.cores = n.cores)) rownames(bmat) <- rownames(cd) if(verbose) { cat(".") } } if(verbose) { cat(" done\n") } # do a model fit on the weighted standard deviation (as a function of the batch-average expression mode) wvar <- rowSums(mat)/rowSums(edf.mat) if(!is.null(batch) && is.list(modes)) { # batch-specific mode # estimate the ratio of the batch-specific variance to the total dataset variance bwvar <- rowSums(bmat)/rowSums(bedf.mat) bwvar.ratio <- bwvar/wvar wvar <- bwvar # replace wvar now that we have the ratio of matw <- bmatw # replace matw with the batch-specific one that will be used from here on # ALTERNATIVE: could adjust wvar for the bwvar.ratio here, before fitting expression dependency } fvi <- vi <- rowSums(matw) > 0 & is.finite(wvar) & wvar > 0 if(!is.null(fit.genes)) { fvi <- fvi & rownames(mat) %in% fit.genes } if(!any(fvi)) { stop("unable to find a set of valid genes to establish the variance fit") } # s = mgcv:::s s = mgcv::s if(cv.fit) { #x <- gam(as.formula("cv2 ~ s(lev)"), data = df[vi, ], weights = rowSums(matw[vi, ])) if(is.null(gene.length)) { df <- data.frame(lev = log10(avmodes), cv2 = log10(wvar/avmodes^2)) x <- mgcv::gam(cv2 ~ s(lev, k = smooth.df), data = df[fvi, ], weights = rowSums(matw[fvi, ])) } else { df <- data.frame(lev = log10(avmodes), cv2 = log10(wvar/avmodes^2), len = gene.length[rownames(cd)]) x <- mgcv::gam(cv2 ~ s(lev, k = smooth.df) + s(len, k = smooth.df), data = df[fvi, ], weights = rowSums(matw[fvi, ])) } #x <- lm(cv2~lev, data = df[vi, ], weights = rowSums(matw[vi, ])) zval.m <- 10^(df$cv2[vi]-predict(x, newdata = df[vi, ])) if(plot) { par(mfrow = c(1, 2), mar = c(3.5, 3.5, 1.0, 1.0), mgp = c(2, 0.65, 0)) #smoothScatter(df$lev[vi], log(wvar[vi]), nbin = 256, xlab = "expression magnitude (log10)", ylab = "wvar (log)") abline(h = 0, lty = 2, col = 2) #points(df[paste("g", diff.exp.gene.ids, sep = ""), "lev"], log(wvar[paste("g", diff.exp.gene.ids, sep = "")]), col = 2) smoothScatter(df$lev[vi], df$cv2[vi], nbin = 256, xlab = "expression magnitude (log10)", ylab = "cv^2 (log10)") lines(sort(df$lev[vi]), predict(x, newdata = df[vi, ])[order(df$lev[vi])], col = 2, pch = ".", cex = 1) if(!is.null(fit.genes)) { # show genes used for the fit points(df$lev[fvi],df$cv2[fvi],pch=".",col="green",cex=1) } #points(df[paste("g", diff.exp.gene.ids, sep = ""), "lev"], df[paste("g", diff.exp.gene.ids, sep = ""), "cv2"], col = 2) } # optional : re-weight to minimize the underdispersed points if(minimize.underdispersion) { pv <- pchisq(zval.m*(edf[vi]-1), edf[vi], log.p = FALSE, lower.tail = TRUE) pv[edf[vi]<= min.edf] <- 0 pv <- p.adjust(pv) #x <- gam(as.formula("cv2 ~ s(lev)"), data = df[vi, ], weights = (pmin(10, -log(pv))+1)*rowSums(matw[vi, ])) x <- mgcv::gam(cv2 ~ s(lev, k = smooth.df), data = df[fvi, ], weights = (pmin(10, -log(pv))+1)*rowSums(matw[fvi, ])) zval.m <- 10^(df$cv2[vi]-predict(x,newdata=df[vi,])) if(plot) { lines(sort(df$lev[vi]), predict(x, newdata = df[vi, ])[order(df$lev[vi])], col = 4, pch = ".", cex = 1) } } } else { df <- data.frame(lev = log10(avmodes), sd = sqrt(wvar)) #x <- gam(as.formula("sd ~ s(lev)"), data = df[vi, ], weights = rowSums(matw[vi, ])) x <- mgcv::gam(sd ~ s(lev, k = smooth.df), data = df[fvi, ], weights = rowSums(matw[fvi, ])) zval.m <- (as.numeric((df$sd[vi])/pmax(min.sd, predict(x,newdata=df[vi,]))))^2 if(plot) { par(mfrow = c(1, 2), mar = c(3.5, 3.5, 1.0, 1.0), mgp = c(2, 0.65, 0)) smoothScatter(df$lev[vi], df$sd[vi], nbin = 256, xlab = "expression magnitude", ylab = "weighted sdiv") lines(sort(df$lev[vi]), predict(x, newdata = df[vi, ])[order(df$lev[vi])], col = 2, pch = ".", cex = 1) if(!is.null(fit.genes)) { # show genes used for the fit points(df$lev[fvi],df$sd[fvi],pch=".",col="green",cex=1) } } # optional : re-weight to minimize the underdispersed points if(minimize.underdispersion) { pv <- pchisq(zval.m*(edf[vi]-1), edf[vi], log.p = FALSE, lower.tail = TRUE) pv[edf[vi]<= min.edf] <- 0 pv <- p.adjust(pv) #x <- gam(as.formula("sd ~ s(lev)"), data = df[vi, ], weights = (pmin(20, -log(pv))+1)*rowSums(matw[vi, ])) x <- mgcv::gam(sd ~ s(lev, k = smooth.df), data = df[fvi, ], weights = (pmin(20, -log(pv))+1)*rowSums(matw[fvi, ])) zval.m <- (as.numeric((df$sd[vi])/pmax(x$fitted.values, min.sd)))^2 if(plot) { lines(sort(df$lev[vi]), predict(x, newdata = df[vi, ])[order(df$lev[vi])], col = 4, pch = ".", cex = 1) } } } # adjust for inter-batch variance if(!is.null(batch) && is.list(modes)) { # batch-specific mode #zval.m <- zval.m*pmin(bwvar.ratio[vi], 1) # don't increase zval.m even if batch-specific specific variance is higher than the dataset-wide variance zval.m <- zval.m*pmin(bwvar.ratio[vi], 1/bwvar.ratio[vi]) # penalize for strong deviation in either direction } # calculate adjusted variance qv <- pchisq(zval.m*(edf[vi]-1), edf[vi], log.p = TRUE, lower.tail = FALSE) qv[edf[vi]<= min.edf] <- 0 qv[abs(qv)<1e-10] <- 0 arv <- rep(NA, length(vi)) arv[vi] <- qchisq(qv, ncol(matw)-1, lower.tail = FALSE, log.p = TRUE)/ncol(matw) arv <- pmin(max.adj.var, arv) names(arv) <- rownames(cd) if(plot) { smoothScatter(df$lev[vi], arv[vi], xlab = "expression magnitude (log10)", ylab = "adjusted variance (log10)", nbin = 256) abline(h = 1, lty = 2, col = 8) abline(h = max.adj.var, lty = 3, col = 2) if(!is.null(fit.genes)) { points(df$lev[fvi],arv[fvi],pch=".",col="green",cex=1) } #points(df[paste("g", diff.exp.gene.ids, sep = ""), "lev"], arv[paste("g", diff.exp.gene.ids, sep = "")], col = 2) #points(df$lev[vi], arv[vi], col = 2, pch = ".", cex = 2) } # Wilcox score upper bound wsu <- function(k, n, z = qnorm(0.975)) { p <- k/n pmin(1, (2*n*p+z^2+(z*sqrt(z^2-1/n+4*n*p*(1-p)-(4*p-2)) +1))/(2*(n+z^2))) } # use milder weight matrix #matw <- 1-0.9*((1-matw)^2) # milder weighting for the the PCA (1-0.9*sp*mf) matw <- 1-weight.k*(1-matw) # milder weighting for the the PCA (1-0.9*sp*mf) matw <- matw/rowSums(matw) mat <- log10(exp(scde.expression.magnitude(models, cd))+1) # estimate observed variance (for scaling) before batch adjustments #varm <- sqrt(arv/pmax(weightedMatVar(mat, matw, batch = batch), 1e-5)) varm[varm<1e-5] <- 1e-5 mat <- mat*varm ov <- weightedMatVar(mat, matw) vr <- arv/ov vr[ov <= 0] <- 0 if(!is.null(batch) && is.list(modes)) { # batch-specific mode # adjust proportion of zeros # determine lowest upper bound of non-zero measurement probability among the batch (for each gene) nbub <- apply(do.call(cbind, tapply(seq_len(ncol(mat)), batch, function(ii) { wsu(rowSums(mat[, ii] > 0), length(ii), z = qnorm(1-1e-2)) })), 1, min) # decrease the batch weights for each gene to match the total # expectation of the non-zero measurements nbo <- do.call(cbind, tapply(seq_len(ncol(mat)), batch, function(ii) { matw[, ii]*pmin(1, ceiling(nbub*length(ii))/rowSums(mat[, ii] > 0)) })) nbo <- nbo[, colnames(matw)] matw <- nbo ## # center 0 and non-0 observations between batches separately ## amat <- mat amat[amat == 0] <- NA ## amat.av <- rowMeans(amat, na.rm = TRUE) # dataset means ## # adjust each batch by the mean of its non-0 measurements ## amat <- do.call(cbind, tapply(1:ncol(amat), batch, function(ii) { ## amat[, ii]-rowMeans(amat[, ii], na.rm = TRUE) ## })) ## amat <- amat[, colnames(mat)] # fix the ordering ## # shift up each gene by the dataset mean ## amat <- amat+amat.av ## amat[is.na(amat)] <- 0 ## mat <- amat amat <- mat nr <- ncol(matw)/rowSums(matw) amat.av <- rowMeans(amat*matw)*nr # dataset means amat <- do.call(cbind, tapply(seq_len(ncol(amat)), batch, function(ii) { amat[, ii]-(rowMeans(amat[, ii]*matw[, ii]*nr, na.rm = TRUE)) })) amat <- amat[, colnames(matw)] mat <- amat+amat.av # alternative: actually zero-out entries in mat ## nbub <- rowMin(do.call(cbind, tapply(1:ncol(amat), batch, function(ii) { ## wsu(rowSums(amat[, ii] > 0), length(ii), z = qnorm(1-1e-2)) ## }))) ## set.seed(0) ## # decrease the batch weights for each gene to match the total ## # expectation of the non-zero measurements ## matm <- do.call(cbind, tapply(1:ncol(amat), batch, function(ii) { ## # number of entries to zero-out per gene ## nze <- rowSums(amat[, ii] > 0) - ceiling(nbub*length(ii)) ## # construct mat multiplier submatrix ## sa <- rep(1, length(ii)) ## smatm <- do.call(rbind, lapply(1:length(nze), function(ri) { ## if(nze[ri]<1) { return(sa) } ## vi <- which(mat[ri, ii] > 0) ## a <- sa a[vi[sample.int(length(vi), nze[ri])]] <- 0 ## a ## })) ## colnames(smatm) <- colnames(mat[, ii]) ## rownames(smatm) <- rownames(mat) ## smatm ## })) ## matm <- matm[, colnames(mat)] ## mat <- mat*matm ## matw <- matw*matm } # center (no batch) mat <- weightedMatCenter(mat, matw) mat <- mat*sqrt(vr) if(!is.null(batch) && is.list(modes)) { # batch-specific mode return(list(mat = mat, matw = matw, arv = arv, modes = modes, avmodes = avmodes, prior = prior, edf = edf, batch = batch, trim = trim, bwvar.ratio = bwvar.ratio)) } else { return(list(mat = mat, matw = matw, arv = arv, modes = modes, avmodes = avmodes, prior = prior, edf = edf, batch = batch, trim = trim)) } } ##' Control for a particular aspect of expression heterogeneity in a given population ##' ##' Similar to subtracting n-th principal component, the current procedure determines ##' (weighted) projection of the expression matrix onto a specified aspect (some pattern ##' across cells, for instance sequencing depth, or PC corresponding to an undesired process ##' such as ribosomal pathway variation) and subtracts it from the data so that it is controlled ##' for in the subsequent weighted PCA analysis. ##' ##' @param varinfo normalized variance info (from pagoda.varnorm()) ##' @param aspect a vector giving a cell-to-cell variation pattern that should be controlled for (length should be corresponding to ncol(varinfo$mat)) ##' @param center whether the matrix should be re-centered following pattern subtraction ##' ##' @return a modified varinfo object with adjusted expression matrix (varinfo$mat) ##' ##' @examples ##' data(pollen) ##' cd <- clean.counts(pollen) ##' \donttest{ ##' knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) ##' varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) ##' # create go environment ##' library(org.Hs.eg.db) ##' # translate gene names to ids ##' ids <- unlist(lapply(mget(rownames(cd), org.Hs.egALIAS2EG, ifnotfound = NA), function(x) x[1])) ##' rids <- names(ids); names(rids) <- ids ##' go.env <- lapply(mget(ls(org.Hs.egGO2ALLEGS), org.Hs.egGO2ALLEGS), function(x) as.character(na.omit(rids[x]))) ##' # clean GOs ##' go.env <- clean.gos(go.env) ##' # convert to an environment ##' go.env <- list2env(go.env) ##' # subtract the pattern ##' cc.pattern <- pagoda.show.pathways(ls(go.env)[1:2], varinfo, go.env, show.cell.dendrogram = TRUE, showRowLabels = TRUE) # Look at pattern from 2 GO annotations ##' varinfo.cc <- pagoda.subtract.aspect(varinfo, cc.pattern) ##' } ##' ##' @export pagoda.subtract.aspect <- function(varinfo, aspect, center = TRUE) { if(length(aspect) != ncol(varinfo$mat)) { stop("aspect should be a numeric vector of the same length as the number of cells (i.e. ncol(varinfo$mat))") } v <- aspect v <- v-mean(v) v <- v/sqrt(sum(v^2)) nr <- ((varinfo$mat * varinfo$matw) %*% v)/(varinfo$matw %*% v^2) mat.c <- varinfo$mat - t(v %*% t(nr)) if(center) { mat.c <- weightedMatCenter(mat.c, varinfo$matw) # this commonly re-introduces some background dependency because of the matw } varinfo$mat <- mat.c varinfo } ##' Run weighted PCA analysis on pre-annotated gene sets ##' ##' For each valid gene set (having appropriate number of genes) in the provided environment (setenv), ##' the method will run weighted PCA analysis, along with analogous analyses of random gene sets of the ##' same size, or shuffled expression magnitudes for the same gene set. ##' ##' @param varinfo adjusted variance info from pagoda.varinfo() (or pagoda.subtract.aspect()) ##' @param setenv environment listing gene sets (contains variables with names corresponding to gene set name, and values being vectors of gene names within each gene set) ##' @param n.components number of principal components to determine for each gene set ##' @param n.cores number of cores to use ##' @param min.pathway.size minimum number of observed genes that should be contained in a valid gene set ##' @param max.pathway.size maximum number of observed genes in a valid gene set ##' @param n.randomizations number of random gene sets (of the same size) to be evaluated in parallel with each gene set (can be kept at 5 or 10, but should be increased to 50-100 if the significance of pathway overdispersion will be determined relative to random gene set models) ##' @param n.internal.shuffles number of internal (independent row shuffles) randomizations of expression data that should be evaluated for each gene set (needed only if one is interested in gene set coherence P values, disabled by default; set to 10-30 to estimate) ##' @param n.starts number of random starts for the EM method in each evaluation ##' @param center whether the expression matrix should be recentered ##' @param batch.center whether batch-specific centering should be used ##' @param proper.gene.names alternative vector of gene names (replacing rownames(varinfo$mat)) to be used in cases when the provided setenv uses different gene names ##' @param verbose verbosity level ##' ##' @return a list of weighted PCA info for each valid gene set ##' ##' @examples ##' data(pollen) ##' cd <- clean.counts(pollen) ##' \donttest{ ##' knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) ##' varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) ##' # create go environment ##' library(org.Hs.eg.db) ##' # translate gene names to ids ##' ids <- unlist(lapply(mget(rownames(cd), org.Hs.egALIAS2EG, ifnotfound = NA), function(x) x[1])) ##' rids <- names(ids); names(rids) <- ids ##' go.env <- lapply(mget(ls(org.Hs.egGO2ALLEGS), org.Hs.egGO2ALLEGS), function(x) as.character(na.omit(rids[x]))) ##' # clean GOs ##' go.env <- clean.gos(go.env) ##' # convert to an environment ##' go.env <- list2env(go.env) ##' pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) ##' } ##' ##' @export pagoda.pathway.wPCA <- function(varinfo, setenv, n.components = 2, n.cores = detectCores(), min.pathway.size = 10, max.pathway.size = 1e3, n.randomizations = 10, n.internal.shuffles = 0, n.starts = 10, center = TRUE, batch.center = TRUE, proper.gene.names = NULL, verbose = 0) { mat <- varinfo$mat matw <- varinfo$matw gsl <- NULL return.gsl <- FALSE smooth <- 0 if(batch.center) { batch <- varinfo$batch } else { batch <- NULL } if(is.null(proper.gene.names)) { proper.gene.names <- rownames(mat) } if(center) { mat <- weightedMatCenter(mat, matw, batch = batch) } vi <- apply(mat, 1, function(x) sum(abs(diff(x))) > 0) vi[is.na(vi)] <- FALSE mat <- mat[vi, , drop = FALSE] # remove constant rows matw <- matw[vi, , drop = FALSE] proper.gene.names <- proper.gene.names[vi] if(is.null(gsl)) { gsl <- ls(envir = setenv) gsl.ng <- unlist(lapply(sn(gsl), function(go) sum(unique(get(go, envir = setenv)) %in% proper.gene.names))) gsl <- gsl[gsl.ng >= min.pathway.size & gsl.ng<= max.pathway.size] names(gsl) <- gsl } if(verbose) { message("processing ", length(gsl), " valid pathways") } if(return.gsl) return(gsl) # transpose mat to save a bit of calculations mat <- t(mat) matw <- t(matw) mcm.pc <- papply(gsl, function(x) { lab <- proper.gene.names %in% get(x, envir = setenv) if(sum(lab)<1) { return(NULL) } #smooth <- round(sum(lab)*smooth.fraction) #smooth <- max(sum(lab), smooth) #xp <- pca(d, nPcs = n.components, center = TRUE, scale = "none") #xp <- epca(mat[, lab], ncomp = n.components, center = FALSE, nstarts = n.starts) xp <- bwpca(mat[, lab, drop = FALSE], matw[, lab, drop = FALSE], npcs = n.components, center = FALSE, nstarts = n.starts, smooth = smooth, n.shuffles = n.internal.shuffles) # get standard deviations for the random samples ngenes <- sum(lab) z <- do.call(rbind, lapply(seq_len(n.randomizations), function(i) { si <- sample(1:ncol(mat), ngenes) #epca(mat[, si], ncomp = 1, center = FALSE, nstarts = n.starts)$sd xp <- bwpca(mat[, si, drop = FALSE], matw[, si, drop = FALSE], npcs = 1, center = FALSE, nstarts = n.starts, smooth = smooth)$sd })) # flip orientations to roughly correspond with the means cs <- unlist(lapply(seq_len(ncol(xp$scores)), function(i) sign(cor(xp$scores[, i], colMeans(t(mat[, lab, drop = FALSE])*abs(xp$rotation[, i])))))) xp$scores <- t(t(xp$scores)*cs) xp$rotation <- t(t(xp$rotation)*cs) # local normalization of each component relative to sampled PC1 sd avar <- pmax(0, (xp$sd^2-mean(z[, 1]^2))/sd(z[, 1]^2)) xv <- t(xp$scores) xv <- xv/apply(xv, 1, sd)*sqrt(avar) return(list(xv = xv, xp = xp, z = z, sd = xp$sd, n = ngenes)) }, n.cores = n.cores) } ##' Estimate effective number of cells based on lambda1 of random gene sets ##' ##' Examines the dependency between the amount of variance explained by the first principal component ##' of a gene set and the number of genes in a gene set to determine the effective number of cells ##' for the Tracy-Widom distribution ##' ##' @param pwpca result of the pagoda.pathway.wPCA() call with n.randomizations > 1 ##' @param start optional starting value for the optimization (if the NLS breaks, trying high starting values usually fixed the local gradient problem) ##' ##' @return effective number of cells ##' ##' @examples ##' data(pollen) ##' cd <- clean.counts(pollen) ##' \donttest{ ##' knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) ##' varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) ##' pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) ##' pagoda.effective.cells(pwpca) ##' } ##' ##' @export pagoda.effective.cells <- function(pwpca, start = NULL) { n.genes <- unlist(lapply(pwpca, function(x) rep(x$n, nrow(x$z)))) var <- unlist(lapply(pwpca, function(x) x$z[, 1]))^2 if(is.null(start)) { start <- nrow(pwpca[[1]]$xp$scores)*10 } n.cells <- nrow(pwpca[[1]]$xp$scores) of <- function(p, v, sp) { sn <- p[1] vfit <- (sn+sp)^2/(sn*sn+1/2) -1.2065335745820*(sn+sp)*((1/sn + 1/sp)^(1/3))/(sn*sn+1/2) residuals <- (v-vfit)^2 return(sum(residuals)) } x <- nlminb(objective = of, start = c(start), v = var, sp = sqrt(n.genes-1/2), lower = c(1), upper = c(n.cells)) return((x$par)^2+1/2) } ##' Determine de-novo gene clusters and associated overdispersion info ##' ##' Determine de-novo gene clusters, their weighted PCA lambda1 values, and random matrix expectation. ##' ##' @param varinfo varinfo adjusted variance info from pagoda.varinfo() (or pagoda.subtract.aspect()) ##' @param trim additional Winsorization trim value to be used in determining clusters (to remove clusters that group outliers occurring in a given cell). Use higher values (5-15) if the resulting clusters group outlier patterns ##' @param n.clusters number of clusters to be determined (recommended range is 100-200) ##' @param cor.method correlation method ("pearson", "spearman") to be used as a distance measure for clustering ##' @param n.samples number of randomly generated matrix samples to test the background distribution of lambda1 on ##' @param n.starts number of wPCA EM algorithm starts at each iteration ##' @param n.internal.shuffles number of internal shuffles to perform (only if interested in set coherence, which is quite high for clusters by definition, disabled by default; set to 10-30 shuffles to estimate) ##' @param n.cores number of cores to use ##' @param verbose verbosity level ##' @param plot whether a plot showing distribution of random lambda1 values should be shown (along with the extreme value distribution fit) ##' @param show.random whether the empirical random gene set values should be shown in addition to the Tracy-Widom analytical approximation ##' @param n.components number of PC to calculate (can be increased if the number of clusters is small and some contain strong secondary patterns - rarely the case) ##' @param method clustering method to be used in determining gene clusters ##' @param secondary.correlation whether clustering should be performed on the correlation of the correlation matrix instead ##' @param n.cells number of cells to use for the randomly generated cluster lambda1 model ##' @param old.results optionally, pass old results just to plot the model without recalculating the stats ##' ##' @return a list containing the following fields: ##' \itemize{ ##' \item{clusters} {a list of genes in each cluster values} ##' \item{xf} { extreme value distribution fit for the standardized lambda1 of a randomly generated pattern} ##' \item{tci} { index of a top cluster in each random iteration} ##' \item{cl.goc} {weighted PCA info for each real gene cluster} ##' \item{varm} {standardized lambda1 values for each randomly generated matrix cluster} ##' \item{clvlm} {a linear model describing dependency of the cluster lambda1 on a Tracy-Widom lambda1 expectation} ##' } ##' ##' @examples ##' data(pollen) ##' cd <- clean.counts(pollen) ##' \donttest{ ##' knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) ##' varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) ##' clpca <- pagoda.gene.clusters(varinfo, trim=7.1/ncol(varinfo$mat), n.clusters=150, n.cores=10, plot=FALSE) ##' } ##' ##' @export pagoda.gene.clusters <- function(varinfo, trim = 3.1/ncol(varinfo$mat), n.clusters = 150, n.samples = 60, cor.method = "p", n.internal.shuffles = 0, n.starts = 10, n.cores = detectCores(), verbose = 0, plot = FALSE, show.random = FALSE, n.components = 1, method = "ward.D", secondary.correlation = FALSE, n.cells = ncol(varinfo$mat), old.results = NULL) { smooth <- 0 mat <- varinfo$mat matw <- varinfo$matw batch = varinfo$batch if(trim > 0) { mat <- winsorize.matrix(mat, trim = trim) } if(!is.null(batch)) { # center mat by batch mat <- weightedMatCenter(mat, matw, batch) } if(!is.null(old.results)) { if(verbose) { cat ("reusing old results for the observed clusters\n")} gcls <- old.results$clusters cl.goc <- old.results$cl.goc } else { if(verbose) { cat ("determining gene clusters ...")} # actual clusters vi<-which(abs(apply(mat, 1, function(x) sum(abs(diff(x))))) > 0) if(is.element("WGCNA", installed.packages()[, 1])) { gd <- as.dist(1-WGCNA::cor(t(mat)[, vi], method = cor.method, nThreads = n.cores)) } else { gd <- as.dist(1-cor(t(mat)[, vi], method = cor.method)) } if(secondary.correlation) { if(is.element("WGCNA", installed.packages()[, 1])) { gd <- as.dist(1-WGCNA::cor(as.matrix(gd), method = "p", nThreads = n.cores)) } else { gd <- as.dist(1-cor(as.matrix(gd), method = "p")) } } if(is.element("fastcluster", installed.packages()[, 1])) { gcl <- fastcluster::hclust(gd, method = method) } else { gcl <- stats::hclust(gd, method = method) } gcll <- cutree(gcl, n.clusters) gcls <- tapply(rownames(mat)[vi], as.factor(gcll), I) names(gcls) <- paste("geneCluster", names(gcls), sep = ".") rm(gd, gcl) gc() # determine PC1 for the actual clusters if(verbose) { cat (" cluster PCA ...")} il <- tapply(vi, factor(gcll, levels = c(1:length(gcls))), I) cl.goc <- papply(il, function(ii) { xp <- bwpca(t(mat[ii, , drop = FALSE]), t(matw[ii, , drop = FALSE]), npcs = n.components, center = FALSE, nstarts = n.starts, smooth = smooth, n.shuffles = n.internal.shuffles) cs <- unlist(lapply(seq_len(ncol(xp$scores)), function(i) sign(cor(xp$scores[, i], colMeans(mat[ii, , drop = FALSE]*abs(xp$rotation[, i])))))) xp$scores <- t(t(xp$scores)*cs) xp$rotation <- t(t(xp$rotation)*cs) return(list(xp = xp, sd = xp$sd, n = length(ii))) }, n.cores = n.cores) names(cl.goc) <- paste("geneCluster", names(cl.goc), sep = ".") if(verbose) { cat ("done\n")} } # sampled variation if(!is.null(old.results) && !is.null(old.results$varm)) { if(verbose) { cat ("reusing old results for the sampled clusters\n")} varm <- old.results$varm } else { if(verbose) { cat ("generating", n.samples, "randomized samples ")} varm <- do.call(rbind, papply(seq_len(n.samples), function(i) { # each sampling iteration set.seed(i) # generate random normal matrix # TODO: use n.cells instead of ncol(matw) m <- matrix(rnorm(nrow(mat)*n.cells), nrow = nrow(mat), ncol = n.cells) #m <- weightedMatCenter(m, matw, batch = batch) if(show.random) { full.m <- t(m) # save untrimmed version of m for random gene set controls } if(trim > 0) { m <- winsorize.matrix(m, trim = trim) } vi<-which(abs(apply(m, 1, function(x) sum(diff(abs(x))))) > 0) if(is.element("WGCNA", installed.packages()[, 1])) { gd <- as.dist(1-WGCNA::cor(t(m[vi, ]), method = cor.method, nThreads = 1)) } else { gd <- as.dist(1-cor(t(m[vi, ]), method = cor.method)) } if(secondary.correlation) { if(is.element("WGCNA", installed.packages()[, 1])) { gd <- as.dist(1-WGCNA::cor(as.matrix(gd), method = "p", nThreads = 1)) } else { gd <- as.dist(1-cor(as.matrix(gd), method = "p")) } } if(is.element("fastcluster", installed.packages()[, 1])) { gcl <- fastcluster::hclust(gd, method = method) } else { gcl <- stats::hclust(gd, method = method) } gcll <- cutree(gcl, n.clusters) rm(gd, gcl) gc() # transpose to save time m <- t(m) # matw <- t(matw) sdv <- tapply(vi, gcll, function(ii) { #as.numeric(bwpca(m[, ii], matw[, ii], npcs = 1, center = FALSE, nstarts = n.starts, smooth = smooth)$sd)^2 pcaMethods::sDev(pcaMethods::pca(m[, ii], nPcs = 1, center = FALSE))^2 }) pathsizes <- unlist(tapply(vi, gcll, length)) names(pathsizes) <- pathsizes if(show.random) { rsdv <- unlist(lapply(names(pathsizes), function(s) { vi <- sample(1:ncol(full.m), as.integer(s)) pcaMethods::sDev(pcaMethods::pca(full.m[, vi], nPcs = 1, center = FALSE))^2 })) if(verbose) { cat (".")} return(data.frame(n = as.integer(pathsizes), var = unlist(sdv), round = i, rvar = rsdv)) } if(verbose) { cat (".")} data.frame(n = as.integer(pathsizes), var = unlist(sdv), round = i) }, n.cores = n.cores)) if(verbose) { cat ("done\n")} } # score relative to Tracey-Widom distribution #require(RMTstat) x <- RMTstat::WishartMaxPar(n.cells, varm$n) varm$pm <- x$centering-(1.2065335745820)*x$scaling # predicted mean of a random set varm$pv <- (1.607781034581)*x$scaling # predicted variance of a random set #clvlm <- lm(var~pm, data = varm) clvlm <- lm(var~0+pm+n, data = varm) varm$varst <- (varm$var-predict(clvlm))/sqrt(varm$pv) #varm$varst <- as.numeric(varm$var - (cbind(1, varm$pm) %*% coef(clvlm)))/sqrt(varm$pv) #varm$varst <- as.numeric(varm$var - (varm$pm* coef(clvlm)[2]))/sqrt(varm$pv) #varm$varst <- (varm$var-varm$pm)/sqrt(varm$pv) tci <- tapply(seq_len(nrow(varm)), as.factor(varm$round), function(ii) ii[which.max(varm$varst[ii])]) #xf <- fevd(varm$varst[tci], type = "Gumbel") # fit on top clusters xf <- extRemes::fevd(varm$varst, type = "Gumbel") # fit on all clusters if(plot) { require(extRemes) par(mfrow = c(1, 2), mar = c(3.5, 3.5, 3.5, 1.0), mgp = c(2, 0.65, 0), cex = 0.9) smoothScatter(varm$n, varm$var, main = "simulations", xlab = "cluster size", ylab = "PC1 variance") if(show.random) { points(varm$n, varm$rvar, pch = ".", col = "red") } #pv <- predict(rsm, newdata = data.frame(n = sort(varm$n)), se.fit = TRUE) on <- order(varm$n, decreasing = TRUE) lines(varm$n[on], predict(clvlm)[on], col = 4, lty = 3) lines(varm$n[on], varm$pm[on], col = 2) lines(varm$n[on], (varm$pm+1.96*sqrt(varm$pv))[on], col = 2, lty = 2) lines(varm$n[on], (varm$pm-1.96*sqrt(varm$pv))[on], col = 2, lty = 2) legend(x = "bottomright", pch = c(1, 19, 19), col = c(1, 4, 2), legend = c("top clusters", "clusters", "random"), bty = "n") points(varm$n[tci], varm$var[tci], col = 1) extRemes::plot.fevd(xf, type = "density", main = "Gumbel fit") abline(v = 0, lty = 3, col = 4) } #pevd(9, loc = xf$results$par[1], scale = xf$results$par[2], lower.tail = FALSE) #xf$results$par return(list(clusters = gcls, xf = xf, tci = tci, cl.goc = cl.goc, varm = varm, clvlm = clvlm, trim = trim)) } ##' Score statistical significance of gene set and cluster overdispersion ##' ##' Evaluates statistical significance of the gene set and cluster lambda1 values, returning ##' either a text table of Z scores, etc, a structure containing normalized values of significant ##' aspects, or a set of genes underlying the significant aspects. ##' ##' @param pwpca output of pagoda.pathway.wPCA() ##' @param clpca output of pagoda.gene.clusters() (optional) ##' @param n.cells effective number of cells (if not provided, will be determined using pagoda.effective.cells()) ##' @param z.score Z score to be used as a cutoff for statistically significant patterns (defaults to 0.05 P-value ##' @param return.table whether a text table showing ##' @param return.genes whether a set of genes driving significant aspects should be returned ##' @param plot whether to plot the cv/n vs. dataset size scatter showing significance models ##' @param adjust.scores whether the normalization of the aspect patterns should be based on the adjusted Z scores - qnorm(0.05/2, lower.tail = FALSE) ##' @param score.alpha significance level of the confidence interval for determining upper/lower bounds ##' @param use.oe.scale whether the variance of the returned aspect patterns should be normalized using observed/expected value instead of the default chi-squared derived variance corresponding to overdispersion Z score ##' @param effective.cells.start starting value for the pagoda.effective.cells() call ##' ##' @return if return.table = FALSE and return.genes = FALSE (default) returns a list structure containing the following items: ##' \itemize{ ##' \item{xv} {a matrix of normalized aspect patterns (rows- significant aspects, columns- cells} ##' \item{xvw} { corresponding weight matrix } ##' \item{gw} { set of genes driving the significant aspects } ##' \item{df} { text table with the significance testing results } ##' } ##' ##' @examples ##' data(pollen) ##' cd <- clean.counts(pollen) ##' \donttest{ ##' knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) ##' varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) ##' pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) ##' tam <- pagoda.top.aspects(pwpca, return.table = TRUE, plot=FALSE, z.score=1.96) # top aspects based on GO only ##' } ##' ##' @export pagoda.top.aspects <- function(pwpca, clpca = NULL, n.cells = NULL, z.score = qnorm(0.05/2, lower.tail = FALSE), return.table = FALSE, return.genes = FALSE, plot = FALSE, adjust.scores = TRUE, score.alpha = 0.05, use.oe.scale = FALSE, effective.cells.start = NULL) { basevar = 1 if(is.null(n.cells)) { n.cells <- pagoda.effective.cells(pwpca, start = effective.cells.start) } vdf <- data.frame(do.call(rbind, lapply(seq_along(pwpca), function(i) { vars <- as.numeric((pwpca[[i]]$sd)^2) shz <- NA if(!is.null(pwpca[[i]]$xp$randvar)) { shz <- (vars - mean(pwpca[[i]]$xp$randvar))/sd(pwpca[[i]]$xp$randvar) } cbind(i = i, var = vars, n = pwpca[[i]]$n, npc = seq(1:ncol(pwpca[[i]]$xp$scores)), shz = shz) }))) # fix p-to-q mistake in qWishartSpike qWishartSpikeFixed <- function (q, spike, ndf = NA, pdim = NA, var = 1, beta = 1, lower.tail = TRUE, log.p = FALSE) { params <- RMTstat::WishartSpikePar(spike, ndf, pdim, var, beta) qnorm(q, mean = params$centering, sd = params$scaling, lower.tail, log.p) } # add right tail approximation to ptw, which gives up quite early pWishartMaxFixed <- function (q, ndf, pdim, var = 1, beta = 1, lower.tail = TRUE) { params <- RMTstat::WishartMaxPar(ndf, pdim, var, beta) q.tw <- (q - params$centering)/(params$scaling) p <- RMTstat::ptw(q.tw, beta, lower.tail, log.p = TRUE) p[p == -Inf] <- pgamma((2/3)*q.tw[p == -Inf]^(3/2), 2/3, lower.tail = FALSE, log.p = TRUE) + lgamma(2/3) + log((2/3)^(1/3)) p } #bi <- which.max(unlist(lapply(pwpca, function(x) x$n))) #vshift <- mean(pwpca[[bi]]$z[, 1]^2)/pwpca[[bi]]$n #ev <- ifelse(spike > 0, qWishartSpikeFixed(0.5, spike, n.cells, pwpca[[bi]]$n, var = basevar, lower.tail = FALSE), RMTstat::qWishartMax(0.5, n.cells, pwpca[[bi]]$n, var = basevar, lower.tail = FALSE))/pwpca[[bi]]$n #cat("vshift = ", vshift) vshift <- 0 ev <- 0 vdf$var <- vdf$var-(vshift-ev)*vdf$n #vdf$var[vdf$npc == 1] <- vdf$var[vdf$npc == 1]-(vshift-ev)*vdf$n[vdf$npc == 1] vdf$exp <- RMTstat::qWishartMax(0.5, n.cells, vdf$n, var = basevar, lower.tail = FALSE) #vdf$z <- qnorm(pWishartMax(vdf$var, n.cells, vdf$n, log.p = TRUE, lower.tail = FALSE, var = basevar), lower.tail = FALSE, log.p = TRUE) vdf$z <- qnorm(pWishartMaxFixed(vdf$var, n.cells, vdf$n, lower.tail = FALSE, var = basevar), lower.tail = FALSE, log.p = TRUE) vdf$cz <- qnorm(bh.adjust(pnorm(as.numeric(vdf$z), lower.tail = FALSE, log.p = TRUE), log = TRUE), lower.tail = FALSE, log.p = TRUE) vdf$ub <- RMTstat::qWishartMax(score.alpha/2, n.cells, vdf$n, var = basevar, lower.tail = FALSE) vdf$ub.stringent <- RMTstat::qWishartMax(score.alpha/nrow(vdf)/2, n.cells, vdf$n, var = basevar, lower.tail = FALSE) if(!is.null(clpca)) { clpca$xf <- extRemes::fevd(varst, data = clpca$varm, type = "Gumbel") #clpca$xf <- fevd(clpca$varm$varst[clpca$tci], type = "Gumbel") clpca$xf$results$par <- c(clpca$xf$results$par, c(shape = 0)) #plot(xf) clvdf <- data.frame(do.call(rbind, lapply(seq_along(clpca$cl.goc), function(i) { vars <- as.numeric((clpca$cl.goc[[i]]$sd)^2) shz <- NA if(!is.null(clpca$cl.goc[[i]]$xp$randvar)) { shz <- (vars - mean(clpca$cl.goc[[i]]$xp$randvar))/sd(clpca$cl.goc[[i]]$xp$randvar) } cbind(i = i, var = vars, n = clpca$cl.goc[[i]]$n, npc = seq(1:ncol(clpca$cl.goc[[i]]$xp$scores)), shz = shz) }))) clvdf$var <- clvdf$var-(vshift-ev)*clvdf$n x <- RMTstat::WishartMaxPar(n.cells, clvdf$n) clvdf$pm <- x$centering-(1.2065335745820)*x$scaling # predicted mean of a random set clvdf$pv <- (1.607781034581)*x$scaling # predicted variance of a random set pvar <- predict(clpca$clvlm, newdata = clvdf) clvdf$varst <- (clvdf$var-pvar)/sqrt(clvdf$pv) clvdf$exp <- clpca$xf$results$par[1]*sqrt(clvdf$pv)+pvar #clvdf$varst <- (clvdf$var-clvdf$pm)/sqrt(clvdf$pv) #clvdf$exp <- clpca$xf$results$par[1]*sqrt(clvdf$pv)+clvdf$pm lp <- pgev.upper.log(clvdf$varst, clpca$xf$results$par[1], clpca$xf$results$par[2], rep(clpca$xf$results$par[3], nrow(clvdf))) clvdf$z <- qnorm(lp, lower.tail = FALSE, log.p = TRUE) clvdf$cz <- qnorm(bh.adjust(pnorm(as.numeric(clvdf$z), lower.tail = FALSE, log.p = TRUE), log = TRUE), lower.tail = FALSE, log.p = TRUE) # CI relative to the background clvdf$ub <- extRemes::qevd(score.alpha/2, loc = clpca$xf$results$par[1], scale = clpca$xf$results$par[2], shape = clpca$xf$results$par[3], lower.tail = FALSE)*sqrt(clvdf$pv) + pvar clvdf$ub.stringent <- extRemes::qevd(score.alpha/2/nrow(clvdf), loc = clpca$xf$results$par[1], scale = clpca$xf$results$par[2], shape = clpca$xf$results$par[3], lower.tail = FALSE)*sqrt(clvdf$pv) + pvar } if(plot) { par(mfrow = c(1, 1), mar = c(3.5, 3.5, 1.0, 1.0), mgp = c(2, 0.65, 0)) un <- sort(unique(vdf$n)) on <- order(vdf$n, decreasing = FALSE) pccol <- colorRampPalette(c("black", "grey70"), space = "Lab")(max(vdf$npc)) plot(vdf$n, vdf$var/vdf$n, xlab = "gene set size", ylab = "PC1 var/n", ylim = c(0, max(vdf$var/vdf$n)), col = pccol[vdf$npc]) lines(vdf$n[on], (vdf$exp/vdf$n)[on], col = 2, lty = 1) lines(vdf$n[on], (vdf$ub.stringent/vdf$n)[on], col = 2, lty = 2) if(!is.null(clpca)) { pccol <- colorRampPalette(c("darkgreen", "lightgreen"), space = "Lab")(max(clvdf$npc)) points(clvdf$n, clvdf$var/clvdf$n, col = pccol[clvdf$npc], pch = 1) #clvm <- clpca$xf$results$par[1]*sqrt(pmax(1e-3, predict(vm, data.frame(n = un)))) + predict(mm, data.frame(n = un)) on <- order(clvdf$n, decreasing = FALSE) lines(clvdf$n[on], (clvdf$exp/clvdf$n)[on], col = "darkgreen") lines(clvdf$n[on], (clvdf$ub.stringent/clvdf$n)[on], col = "darkgreen", lty = 2) } #mi<-which.max(vdf$n) sv<- (vdf$var/vdf$n)[mi] - (vdf$exp/vdf$n)[mi] #lines(vdf$n[on], (vdf$exp/vdf$n)[on]+sv, col = 2, lty = 3) #lines(vdf$n[on], (vdf$ub.stringent/vdf$n)[on]+sv, col = 2, lty = 2) } if(!is.null(clpca)) { # merge in cluster stats based on their own model # merge pwpca, psd and pm # all processing from here is common clvdf$i <- clvdf$i+length(pwpca) # shift cluster ids pwpca <- c(pwpca, clpca$cl.goc) vdf <- rbind(vdf, clvdf[, c("i", "var", "n", "npc", "exp", "cz", "z", "ub", "ub.stringent", "shz")]) } vdf$adj.shz <- qnorm(bh.adjust(pnorm(as.numeric(vdf$shz), lower.tail = FALSE, log.p = TRUE), log = TRUE), lower.tail = FALSE, log.p = TRUE) #vdf$oe <- vdf$var/vdf$exp rs <- (vshift-ev)*vdf$n #rs <- ifelse(vdf$npc == 1, (vshift-ev)*vdf$n, 0) vdf$oe <- (vdf$var+rs)/(vdf$exp+rs) #vdf$oe[vdf$oe<0] <- 0 #vdf$oec <- (vdf$var-vdf$ub.stringent+vdf$exp)/vdf$exp #vdf$oec <- (vdf$var-vdf$ub+vdf$exp)/vdf$exp #vdf$oec <- (vdf$var-vdf$ub+vdf$exp+rs)/(vdf$exp+rs) vdf$oec <- (vdf$var+rs)/(vdf$ub+rs) #vdf$oec[vdf$oec<0] <- 0 #vdf$z[vdf$z<0] <- 0 df <- data.frame(name = names(pwpca)[vdf$i], npc = vdf$npc, n = vdf$n, score = vdf$oe, z = vdf$z, adj.z = vdf$cz, sh.z = vdf$shz, adj.sh.z = vdf$adj.shz, stringsAsFactors = FALSE) if(adjust.scores) { vdf$valid <- vdf$cz >= z.score } else { vdf$valid <- vdf$z >= z.score } if(return.table) { df <- df[vdf$valid, ] df <- df[order(df$score, decreasing = TRUE), ] return(df) } # determine genes driving significant pathways # return genes within top 2/3rds of PC loading gl <- lapply(which(vdf$valid), function(i) { s <- abs(pwpca[[vdf[i, "i"]]]$xp$rotation[, vdf[i, "npc"]] ) s[s >= max(s)/3] }) gw <- tapply(abs(unlist(gl)), as.factor(unlist(lapply(gl, names))), max) if(return.genes) { return(gw) } # return combined data structure # weight xvw <- do.call(rbind, lapply(pwpca, function(x) { xm <- t(x$xp$scoreweights) })) vi <- vdf$valid xvw <- xvw[vi, ]/rowSums(xvw[vi, ]) # return scaled patterns xmv <- do.call(rbind, lapply(pwpca, function(x) { xm <- t(x$xp$scores) })) if(use.oe.scale) { xmv <- (xmv[vi, ] -rowMeans(xmv[vi, ]))* (as.numeric(vdf$oe[vi])/sqrt(apply(xmv[vi, ], 1, var))) } else { # chi-squared xmv <- (xmv[vi, ]-rowMeans(xmv[vi, ])) * sqrt((qchisq(pnorm(vdf$z[vi], lower.tail = FALSE, log.p = TRUE), n.cells, lower.tail = FALSE, log.p = TRUE)/n.cells)/apply(xmv[vi, ], 1, var)) } rownames(xmv) <- paste("#PC", vdf$npc[vi], "# ", names(pwpca)[vdf$i[vi]], sep = "") return(list(xv = xmv, xvw = xvw, gw = gw, df = df)) } ##' Collapse aspects driven by the same combinations of genes ##' ##' Examines PC loading vectors underlying the identified aspects and clusters aspects based ##' on a product of loading and score correlation (raised to corr.power). Clusters of aspects ##' driven by the same genes are determined based on the distance.threshold and collapsed. ##' ##' @param tam output of pagoda.top.aspects() ##' @param pwpca output of pagoda.pathway.wPCA() ##' @param clpca output of pagoda.gene.clusters() (optional) ##' @param plot whether to plot the resulting clustering ##' @param cluster.method one of the standard clustering methods to be used (fastcluster::hclust is used if available or stats::hclust) ##' @param distance.threshold similarity threshold for grouping interdependent aspects ##' @param corr.power power to which the product of loading and score correlation is raised ##' @param abs Boolean of whether to use absolute correlation ##' @param n.cores number of cores to use during processing ##' @param ... additional arguments are passed to the pagoda.view.aspects() method during plotting ##' ##' @return a list structure analogous to that returned by pagoda.top.aspects(), but with addition of a $cnam element containing a list of aspects summarized by each row of the new (reduced) $xv and $xvw ##' ##' @examples ##' data(pollen) ##' cd <- clean.counts(pollen) ##' \donttest{ ##' knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) ##' varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) ##' pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) ##' tam <- pagoda.top.aspects(pwpca, return.table = TRUE, plot=FALSE, z.score=1.96) # top aspects based on GO only ##' tamr <- pagoda.reduce.loading.redundancy(tam, pwpca) ##' } ##' ##' @export pagoda.reduce.loading.redundancy <- function(tam, pwpca, clpca = NULL, plot = FALSE, cluster.method = "complete", distance.threshold = 0.01, corr.power = 4, n.cores = detectCores(), abs = TRUE, ...) { pclc <- pathway.pc.correlation.distance(c(pwpca, clpca$cl.goc), tam$xv, target.ndf = 100, n.cores = n.cores) cda <- cor(t(tam$xv)) if(abs) { cda <- abs(cda) } else { cda[cda<0] <- 0 } cda <- as.dist(1-cda) cc <- (1-sqrt((1-pclc)*(1-cda)))^corr.power if(is.element("fastcluster", installed.packages()[, 1])) { y <- fastcluster::hclust(cc, method = cluster.method) } else { y <- stats::hclust(cc, method = cluster.method) } ct <- cutree(y, h = distance.threshold) ctf <- factor(ct, levels = sort(unique(ct))) xvl <- collapse.aspect.clusters(tam$xv, tam$xvw, ct, pick.top = FALSE, scale = TRUE) if(plot) { sc <- sample(colors(), length(levels(ctf)), replace = TRUE) view.aspects(tam$xv, row.clustering = y, row.cols = sc[as.integer(ctf)], ...) } # collapsed names if(!is.null(tam$cnam)) { # already has collapsed names cnam <- tapply(rownames(tam$xv), ctf, function(xn) unlist(tam$cnam[xn])) } else { cnam <- tapply(rownames(tam$xv), ctf, I) } names(cnam) <- rownames(xvl$d) tam$xv <- xvl$d tam$xvw <- xvl$w tam$cnam <- cnam return(tam) } ##' Collapse aspects driven by similar patterns (i.e. separate the same sets of cells) ##' ##' Examines PC loading vectors underlying the identified aspects and clusters aspects based on score correlation. Clusters of aspects driven by the same patterns are determined based on the distance.threshold. ##' ##' @param tamr output of pagoda.reduce.loading.redundancy() ##' @param distance.threshold similarity threshold for grouping interdependent aspects ##' @param cluster.method one of the standard clustering methods to be used (fastcluster::hclust is used if available or stats::hclust) ##' @param distance distance matrix ##' @param weighted.correlation Boolean of whether to use a weighted correlation in determining the similarity of patterns ##' @param plot Boolean of whether to show plot ##' @param top Restrict output to the top n aspects of heterogeneity ##' @param trim Winsorization trim to use prior to determining the top aspects ##' @param abs Boolean of whether to use absolute correlation ##' @param ... additional arguments are passed to the pagoda.view.aspects() method during plotting ##' ##' @return a list structure analogous to that returned by pagoda.top.aspects(), but with addition of a $cnam element containing a list of aspects summarized by each row of the new (reduced) $xv and $xvw ##' ##' @examples ##' data(pollen) ##' cd <- clean.counts(pollen) ##' \donttest{ ##' knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) ##' varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) ##' pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) ##' tam <- pagoda.top.aspects(pwpca, return.table = TRUE, plot=FALSE, z.score=1.96) # top aspects based on GO only ##' tamr <- pagoda.reduce.loading.redundancy(tam, pwpca) ##' tamr2 <- pagoda.reduce.redundancy(tamr, distance.threshold = 0.9, plot = TRUE, labRow = NA, labCol = NA, box = TRUE, margins = c(0.5, 0.5), trim = 0) ##' } ##' ##' @export pagoda.reduce.redundancy <- function(tamr, distance.threshold = 0.2, cluster.method = "complete", distance = NULL, weighted.correlation = TRUE, plot = FALSE, top = Inf, trim = 0, abs = FALSE, ...) { if(is.null(distance)) { if(weighted.correlation) { distance <- .Call("matWCorr", t(tamr$xv), t(tamr$xvw), PACKAGE = "scde") rownames(distance) <- colnames(distance) <- rownames(tamr$xv) if(abs) { distance <- stats::as.dist(1-abs(distance), upper = TRUE) } else { distance <- stats::as.dist(1-distance, upper = TRUE) } } else { if(abs) { distance <- stats::as.dist(1-abs(cor(t(tamr$xv)))) } else { distance <- stats::as.dist(1-cor(t(tamr$xv))) } } } if(is.element("fastcluster", installed.packages()[, 1])) { y <- fastcluster::hclust(distance, method = cluster.method) } else { y <- stats::hclust(distance, method = cluster.method) } ct <- cutree(y, h = distance.threshold) ctf <- factor(ct, levels = sort(unique(ct))) xvl <- collapse.aspect.clusters(tamr$xv, tamr$xvw, ct, pick.top = FALSE, scale = TRUE) if(plot) { sc <- sample(colors(), length(levels(ctf)), replace = TRUE) view.aspects(tamr$xv, row.clustering = y, row.cols = sc[as.integer(ctf)], ...) } # collapsed names if(!is.null(tamr$cnam)) { # already has collapsed names cnam <- tapply(rownames(tamr$xv), ctf, function(xn) unlist(tamr$cnam[xn])) } else { cnam <- tapply(rownames(tamr$xv), ctf, I) } names(cnam) <- rownames(xvl$d) if(trim > 0) { xvl$d <- winsorize.matrix(xvl$d, trim) } # trim prior to determining the top sets rcmvar <- apply(xvl$d, 1, var) vi <- order(rcmvar, decreasing = TRUE)[1:min(length(rcmvar), top)] tamr2 <- tamr tamr2$xv <- xvl$d[vi, ] tamr2$xvw <- xvl$w[vi, ] tamr2$cnam <- cnam[vi] return(tamr2) } ##' Determine optimal cell clustering based on the genes driving the significant aspects ##' ##' Determines cell clustering (hclust result) based on a weighted correlation of genes ##' underlying the top aspects of transcriptional heterogeneity. Branch orientation is optimized ##' if 'cba' package is installed. ##' ##' @param tam result of pagoda.top.aspects() call ##' @param varinfo result of pagoda.varnorm() call ##' @param method clustering method ('ward.D' by default) ##' @param verbose 0 or 1 depending on level of desired verbosity ##' @param include.aspects whether the aspect patterns themselves should be included alongside with the individual genes in calculating cell distance ##' @param return.details Boolean of whether to return just the hclust result or a list containing the hclust result plus the distance matrix and gene values ##' ##' @return hclust result ##' ##' @examples ##' data(pollen) ##' cd <- clean.counts(pollen) ##' \donttest{ ##' knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) ##' varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) ##' pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) ##' tam <- pagoda.top.aspects(pwpca, return.table = TRUE, plot=FALSE, z.score=1.96) # top aspects based on GO only ##' hc <- pagoda.cluster.cells(tam, varinfo) ##' plot(hc) ##' } ##' ##' @export pagoda.cluster.cells <- function(tam, varinfo, method = "ward.D", include.aspects = FALSE, verbose = 0, return.details = FALSE) { # gene clustering gw <- tam$gw gw <- gw[(rowSums(varinfo$matw)*varinfo$arv)[names(gw)] > 1] gw <- gw/gw mi <- match(names(gw), rownames(varinfo$mat)) wgm <- varinfo$mat[mi, ] wgm <- wgm*as.numeric(gw) wgwm <- varinfo$matw[mi, ] if(include.aspects) { if(verbose) { message("clustering cells based on ", nrow(wgm), " genes and ", nrow(tam$xv), " aspect patterns")} wgm <- rbind(wgm, tam$xv) wgwm <- rbind(wgwm, tam$xvw) } else { if(verbose) { message("clustering cells based on ", nrow(wgm), " genes")} } snam <- sample(colnames(wgm)) dm <- .Call("matWCorr", wgm, wgwm, PACKAGE = "scde") dm <- 1-dm rownames(dm) <- colnames(dm) <- colnames(wgm) wcord <- stats::as.dist(dm, upper = TRUE) hc <- hclust(wcord, method = method) if(is.element("cba", installed.packages()[, 1])) { co <- cba::order.optimal(wcord, hc$merge) hc$merge <- co$merge hc$order <- co$order } if(return.details) { return(list(clustering = hc, distance = wcord, genes = gw)) } else { return(hc) } } ##' View PAGODA output ##' ##' Create static image of PAGODA output visualizing cell hierarchy and top aspects of transcriptional heterogeneity ##' ##' @param tamr Combined pathways that show similar expression patterns. Output of \code{\link{pagoda.reduce.redundancy}} ##' @param row.clustering Dendrogram of combined pathways clustering ##' @param top Restrict output to the top n aspects of heterogeneity ##' @param ... additional arguments are passed to the \code{\link{view.aspects}} method during plotting ##' ##' @return PAGODA heatmap ##' ##' @examples ##' data(pollen) ##' cd <- clean.counts(pollen) ##' \donttest{ ##' knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) ##' varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) ##' pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) ##' tam <- pagoda.top.aspects(pwpca, return.table = TRUE, plot=FALSE, z.score=1.96) # top aspects based on GO only ##' pagoda.view.aspects(tam) ##' } ##' ##' @export pagoda.view.aspects <- function(tamr, row.clustering = hclust(dist(tamr$xv)), top = Inf, ...) { if(is.finite(top)) { rcmvar <- apply(tamr$xv, 1, var) vi <- order(rcmvar, decreasing = TRUE)[1:min(length(rcmvar), top)] tamr$xv <- tamr$xv[vi, ] tamr$xvw <- tamr$xvw[vi, ] tamr$cnam <- tamr$cnam[vi] } view.aspects(tamr$xv, row.clustering = row.clustering, ... ) } ##' View heatmap ##' ##' Internal function to visualize aspects of transcriptional heterogeneity as a heatmap. Used by \code{\link{pagoda.view.aspects}}. ##' ##' @param mat Numeric matrix ##' @param row.clustering Row dendrogram ##' @param cell.clustering Column dendrogram ##' @param zlim Range of the normalized gene expression levels, inputted as a list: c(lower_bound, upper_bound). Values outside this range will be Winsorized. Useful for increasing the contrast of the heatmap visualizations. Default, set to the 5th and 95th percentiles. ##' @param row.cols Matrix of row colors. ##' @param col.cols Matrix of column colors. Useful for visualizing cell annotations such as batch labels. ##' @param cols Heatmap colors ##' @param show.row.var.colors Boolean of whether to show row variance as a color track ##' @param top Restrict output to the top n aspects of heterogeneity ##' @param ... additional arguments for heatmap plotting ##' ##' @return A heatmap ##' view.aspects <- function(mat, row.clustering = NA, cell.clustering = NA, zlim = c(-1, 1)*quantile(mat, p = 0.95), row.cols = NULL, col.cols = NULL, cols = colorRampPalette(c("darkgreen", "white", "darkorange"), space = "Lab")(1024), show.row.var.colors = TRUE, top = Inf, ...) { #row.cols, col.cols are matrices for now rcmvar <- apply(mat, 1, var) mat[mat zlim[2]] <- zlim[2] if(class(row.clustering) == "hclust") { row.clustering <- as.dendrogram(row.clustering) } if(class(cell.clustering) == "hclust") { cell.clustering <- as.dendrogram(cell.clustering) } if(show.row.var.colors) { if(is.null(row.cols)) { icols <- colorRampPalette(c("white", "black"), space = "Lab")(1024)[1023*(rcmvar/max(rcmvar))+1] row.cols <- cbind(var = icols) } } my.heatmap2(mat, Rowv = row.clustering, Colv = cell.clustering, zlim = zlim, RowSideColors = row.cols, ColSideColors = col.cols, col = cols, ...) } ##' Make the PAGODA app ##' ##' Create an interactive user interface to explore output of PAGODA. ##' ##' @param tamr Combined pathways that show similar expression patterns. Output of \code{\link{pagoda.reduce.redundancy}} ##' @param tam Combined pathways that are driven by the same gene sets. Output of \code{\link{pagoda.reduce.loading.redundancy}} ##' @param varinfo Variance information. Output of \code{\link{pagoda.varnorm}} ##' @param env Gene sets as an environment variable. ##' @param pwpca Weighted PC magnitudes for each gene set provided in the \code{env}. Output of \code{\link{pagoda.pathway.wPCA}} ##' @param clpca Weighted PC magnitudes for de novo gene sets identified by clustering on expression. Output of \code{\link{pagoda.gene.clusters}} ##' @param col.cols Matrix of column colors. Useful for visualizing cell annotations such as batch labels. Default NULL. ##' @param cell.clustering Dendrogram of cell clustering. Output of \code{\link{pagoda.cluster.cells} } . Default NULL. ##' @param row.clustering Dendrogram of combined pathways clustering. Default NULL. ##' @param title Title text to be used in the browser label for the app. Default, set as 'pathway clustering' ##' @param zlim Range of the normalized gene expression levels, inputted as a list: c(lower_bound, upper_bound). Values outside this range will be Winsorized. Useful for increasing the contrast of the heatmap visualizations. Default, set to the 5th and 95th percentiles. ##' ##' @return PAGODA app ##' ##' @export make.pagoda.app <- function(tamr, tam, varinfo, env, pwpca, clpca = NULL, col.cols = NULL, cell.clustering = NULL, row.clustering = NULL, title = "pathway clustering", zlim = c(-1, 1)*quantile(tamr$xv, p = 0.95)) { # rcm - xv # matvar if(is.null(cell.clustering)) { cell.clustering <- pagoda.cluster.cells(tam, varinfo) } if(is.null(row.clustering)) { row.clustering <- hclust(dist(tamr$xv)) row.clustering$order <- rev(row.clustering$order) } #fct - which tam row in which tamr$xv cluster.. remap tamr$cnams cn <- tamr$cnam fct <- rep(1:length(cn), lapply(cn, length)) names(fct) <- unlist(cn) fct <- fct[rownames(tam$xv)] rcm <- tamr$xv rownames(rcm) <- as.character(1:nrow(rcm)) fres <- list(hvc = cell.clustering, tvc = row.clustering, rcm = rcm, zlim2 = zlim, matvar = apply(tam$xv, 1, sd), ct = fct, matrcmcor = rep(1, nrow(tam$xv)), cols = colorRampPalette(c("darkgreen", "white", "darkorange"), space = "Lab")(1024), colcol = col.cols) # gene df gene.df <- data.frame(var = varinfo$arv*rowSums(varinfo$matw)) gene.df$gene <- rownames(varinfo$mat) gene.df <- gene.df[order(gene.df$var, decreasing = TRUE), ] # prepare pathway df df <- tamr$df if(exists("myGOTERM", envir = globalenv())) { df$desc <- mget(df$name, get("myGOTERM", envir = globalenv()), ifnotfound = "") } else { df$desc <- "" } min.z <- -9 df$z[df$z length(pjpc)/2, "topleft", "topright"), bty = "n", col = cols, legend = colnames(pp), lty = rep(1, dim(pp)[2])) par(new = TRUE) plot(prior$x, pjpc, axes = FALSE, ylab = "", xlab = "", ylim = jpr, type = 'l', col = 1, lty = 1, lwd = 2) axis(4, pretty(jpr, 5), col = 1) mtext("joint posterior", side = 4, outer = FALSE, line = 2) # ratio plot par(mar = c(2.5, 3.5, 0.5, 3.5), mgp = c(1.5, 0.65, 0), cex = 0.9) plot(fcp$v, fcp$p, xlab = "log10 expression ratio", ylab = "ratio posterior", type = 'l', lwd = 2, main = "") r.mle <- fcp$v[which.max(fcp$p)] r.lb <- max(which(cumsum(fcp$p)<0.025)) r.ub <- min(which(cumsum(fcp$p) > (1-0.025))) polygon(c(fcp$v[r.lb], fcp$v[r.lb:r.ub], fcp$v[r.ub]), y = c(-10, fcp$p[r.lb:r.ub], -10), col = "grey90") abline(v = r.mle, col = 2, lty = 2) abline(v = c(fcp$v[r.ub], fcp$v[r.lb]), col = 2, lty = 3) box() legend(x = ifelse(r.mle > 0, "topleft", "topright"), legend = c(paste("MLE = ", round(10^(r.mle), 1), " (", round(r.mle, 2), " in log10)", sep = ""), paste("95% CI: ", round(10^(fcp$v[r.lb]), 1), " - ", round(10^(fcp$v[r.ub]), 1), sep = ""), paste(" log10: ", round(fcp$v[r.lb], 2), " - ", round(fcp$v[r.ub], 2), sep = "")), bty = "n") # distal plot dp <- exp(ldp) par(mar = c(2.5, 3.5, 2.5, 3.5), mgp = c(1.5, 0.65, 0), cex = 0.9) jpr <- range(c(0, djpc), na.rm = TRUE) cols <- rainbow(dim(dp)[2], s = 0.8) plot(c(), c(), xlim = range(prior$x), ylim = range(c(0, dp)), xlab = "expression level", ylab = "individual posterior", main = nam2) lapply(seq_len(ncol(dp)), function(i) lines(prior$x, dp[, i], col = cols[i])) legend(x = ifelse(which.max(na.omit(djpc)) > length(djpc)/2, "topleft", "topright"), bty = "n", col = cols, legend = colnames(dp), lty = rep(1, dim(dp)[2])) par(new = TRUE) plot(prior$x, djpc, axes = FALSE, ylab = "", xlab = "", ylim = jpr, type = 'l', col = 1, lty = 1, lwd = 2) axis(4, pretty(jpr, 5), col = 1) mtext("joint posterior", side = 4, outer = FALSE, line = 2) } lbf <- mpls/mpgr lbf <- (difference.prior*lbf)/(difference.prior*lbf+1-difference.prior) #return(c(equal = qnorm(ebf, lower.tail = TRUE), less = qnorm(lbf, lower.tail = TRUE))) if(return.both) { return(list(z = qnorm(lbf, lower.tail = TRUE), post = fcp)) } else if(return.posterior) { return(fcp) } else { return(qnorm(lbf, lower.tail = TRUE)) } } # counts - data frame with fragment counts (rows - fragments columns -experiments) # groups - a two-level factor describing grouping of columns. Use NA for observations that should be skipped # min.count.threshold - the number of reads used to make an initial guess for the failed component # threshold.segmentation - use min.count.threshold to perform very quick identification of the drop-outs # threshold.prior - prior that should be associated with threshold segmentation calculate.crossfit.models <- function(counts, groups, min.count.threshold = 4, nrep = 1, verbose = 0, min.prior = 1e-5, n.cores = 12, save.plots = TRUE, zero.lambda = 0.1, old.cfm = NULL, threshold.segmentation = FALSE, threshold.prior = 1-1e-6, max.pairs = 1000, min.pairs.per.cell = 10) { names(groups) <- colnames(counts) # enumerate cross-fit pairs within each group cl <- do.call(cbind, tapply(colnames(counts), groups, function(ids) { cl <- combn(ids, 2) min.pairs.per.cell <- min(length(ids)*(length(ids)-1)/2, min.pairs.per.cell) if(verbose) { cat("number of pairs: ", ncol(cl), "\n") } if(ncol(cl) > max.pairs) { if(verbose) { cat("reducing to a random sample of ", max.pairs, " pairs\n") } # make sure there's at least min.pairs.per.cell pairs for each cell cl <- cl[, unique(c(sample(1:ncol(cl), max.pairs), unlist(lapply(ids, function(id) sample(which(colSums(cl == id) > 0), min.pairs.per.cell)))))] } cl })) orl <- c() if(!is.null(old.cfm)) { # check which pairs have already been fitted in compared in old.cfm pn1 <- unlist(apply(cl, 2, function(ii) paste(ii, collapse = ".vs."))) pn2 <- unlist(apply(cl, 2, function(ii) paste(rev(ii), collapse = ".vs."))) ### %%% use rev() to revert element order vi <- (pn1 %in% names(old.cfm)) | (pn2 %in% names(old.cfm)) cl <- cl[, !vi, drop = FALSE] orl <- old.cfm[names(old.cfm) %in% c(pn1, pn2)] } if(verbose) { cat("total number of pairs: ", ncol(cl), "\n") } if(dim(cl)[2] > 0) { if(verbose) message(paste("cross-fitting", ncol(cl), "pairs:")) rl <- papply(seq_len(ncol(cl)), function(cii) { ii <- cl[, cii] df <- data.frame(c1 = counts[, ii[1]], c2 = counts[, ii[2]]) vi <- which(rowSums(df) > 0, ) if(!threshold.segmentation) { if(verbose) { message("fitting pair [", paste(ii, collapse = " "), "]") } mo1 <- FLXMRglmCf(c1~1, family = "poisson", components = c(1), mu = log(zero.lambda)) mo2 <- FLXMRnb2glmC(c1~1+I(log(c2+1)), components = c(2)) mo3 <- FLXMRnb2glmC(c2~1+I(log(c1+1)), components = c(2)) mo4 <- FLXMRglmCf(c2~1, family = "poisson", components = c(3), mu = log(zero.lambda)) m1 <- mc.stepFlexmix(c1~1, data = df[vi, ], k = 3, model = list(mo1, mo2, mo3, mo4), control = list(verbose = verbose, minprior = min.prior), concomitant = FLXPmultinom(~I((log(c1+1)+log(c2+1))/2)+1), cluster = cbind(df$c1[vi]<= min.count.threshold, df$c1[vi] > min.count.threshold & df$c2[vi] > min.count.threshold, df$c2[vi]<= min.count.threshold), nrep = nrep) # reduce return size m1@posterior <- lapply(m1@posterior, function(m) { rownames(m) <- NULL return(m) }) #rownames(m1@concomitant@x) <- NULL m1@concomitant@x <- matrix() m1@model <- lapply(m1@model, function(mod) { mod@x <- matrix() mod@y <- matrix() #rownames(mod@x) <- NULL #rownames(mod@y) <- NULL return(mod) }) #parent.env(environment(m1@components[[1]][[1]]@logLik)) <- globalenv() #parent.env(environment(m1@components[[1]][[2]]@logLik)) <- globalenv() #parent.env(environment(m1@components[[2]][[1]]@logLik)) <- globalenv() #parent.env(environment(m1@components[[2]][[2]]@logLik)) <- globalenv() names(vi) <- NULL pm <- posterior(m1)[, c(1, 3)] rownames(pm) <- NULL cl <- clusters(m1) names(cl) <- NULL gc() } else { # use min.count.threshold to quickly segment the points cl <- rep(2, length(vi)) cl[df[vi, 1] 0 h <- hist(x, plot = FALSE) breaks <- h$breaks nB <- length(breaks) y <- log10(h$counts) y <- y/max(y) rect(breaks[-nB], 0, breaks[-1], y, col = "gray60", ...) } t.pairs.smoothScatter.spearman <- function(x, y, i = NULL, j = NULL, cex = 0.8, ...) { vi <- x > 0 | y > 0 smoothScatter(x[vi], y[vi], add = TRUE, useRaster = TRUE, ...) legend(x = "bottomright", legend = paste("sr = ", round(cor(x[vi], y[vi], method = "spearman"), 2), sep = ""), bty = "n", cex = cex) } # component assignment scatter t.panel.component.scatter <- function(x, y, i, j, cex = 0.8, ...) { if(!is.null(rl[[paste(ids[i], "vs", ids[j], sep = ".")]])) { m1 <- rl[[paste(ids[i], "vs", ids[j], sep = ".")]] # forward plot vi <- which(x > 0 | y > 0) ci <- vi[m1$clusters == 1] if(length(ci) > 3) { points(x[ci], y[ci], pch = ".", col = densCols(x[ci], y[ci], colramp = colorRampPalette(brewer.pal(9, "Reds")[-(1:3)])), cex = 2) } ci <- vi[m1$clusters == 3] if(length(ci) > 3) { points(x[ci], y[ci], pch = ".", col = densCols(x[ci], y[ci], colramp = colorRampPalette(brewer.pal(9, "Greens")[-(1:3)])), cex = 2) } ci <- vi[m1$clusters == 2] if(length(ci) > 3) { points(x[ci], y[ci], pch = ".", col = densCols(x[ci], y[ci], colramp = colorRampPalette(brewer.pal(9, "Blues")[-(1:3)])), cex = 2) } legend(x = "topleft", pch = c(19), col = "blue", legend = paste("sr = ", round(cor(x[ci], y[ci], method = "spearman"), 2), sep = ""), bty = "n", cex = cex) legend(x = "bottomright", pch = c(rep(19, 3)), col = c("red", "blue", "green"), legend = paste(round(unlist(tapply(m1$clusters, factor(m1$clusters, levels = c(1, 2, 3)), length))*100/length(vi), 1), "%", sep = ""), bty = "n", cex = cex) } else if(!is.null(rl[[paste(ids[i], "vs", ids[j], sep = ".")]])) { m1 <- rl[[paste(ids[j], "vs", ids[i], sep = ".")]] # reverse plot vi <- which(x > 0 | y > 0) ci <- vi[m1$clusters == 3] if(length(ci) > 3) { points(x[ci], y[ci], pch = ".", col = densCols(x[ci], y[ci], colramp = colorRampPalette(brewer.pal(9, "Reds")[-(1:3)])), cex = 2) } ci <- vi[m1$clusters == 1] if(length(ci) > 3) { points(x[ci], y[ci], pch = ".", col = densCols(x[ci], y[ci], colramp = colorRampPalette(brewer.pal(9, "Greens")[-(1:3)])), cex = 2) } ci <- vi[m1$clusters == 2] if(length(ci) > 3) { points(x[ci], y[ci], pch = ".", col = densCols(x[ci], y[ci], colramp = colorRampPalette(brewer.pal(9, "Blues")[-(1:3)])), cex = 2) } legend(x = "topleft", pch = c(19), col = "blue", legend = paste("sr = ", round(cor(x[ci], y[ci], method = "spearman"), 2), sep = ""), bty = "n", cex = cex) legend(x = "bottomright", pch = c(rep(19, 3)), col = c("red", "blue", "green"), legend = paste(round(unlist(tapply(m1$clusters, factor(m1$clusters, levels = c(3, 2, 1)), length))*100/length(vi), 1), "%", sep = ""), bty = "n", cex = cex) } else { #message(paste("ERROR: unable to find model for i = ", i, "j = ", j)) message(paste("INFO: cross-fit plots: skipping model for i = ", i, "j = ", j, " (increase max.pairs parameter if needed")) } } #pdf(file = paste(group, "crossfits.pdf", sep = "."), width = 3*length(ids), height = 3*length(ids)) CairoPNG(filename = paste(group, "crossfits.png", sep = "."), width = 250*length(ids), height = 250*length(ids)) pairs.extended(log10(counts[, ids]+1), lower.panel = t.pairs.smoothScatter.spearman, upper.panel = t.panel.component.scatter, diag.panel = t.pairs.panel.hist, cex = 1.5) dev.off() }) } return(rl) } # estimates library sizes based on the correlated components # min.size.entries - minimal number of entries (genes) used to determine scaling factors for individual experiments # counts - data frame with fragment counts (rows - fragments columns -experiments) # groups - a two-level factor describing grouping of columns. Use NA for observations that should be skipped # cfm - cross-fit models (return of calculate.crossfit.models()) # vil - optional binary matrix (corresponding to counts) with 0s marking likely drop-out observations # return value - library size vector in millions of reads estimate.library.sizes <- function(counts, cfm, groups, min.size.entries = min(nrow(counts), 2e3), verbose = 0, return.details = FALSE, vil = NULL, ...) { #require(edgeR) names(groups) <- colnames(counts) # determine the set fragments that were not attributed to failure in any cross-comparison if(is.null(vil)) { #x <- lapply(cfm, function(d) { ll <- list(!(1:nrow(counts)) %in% d$vi[which(d$clusters != 1)], !(1:nrow(counts)) %in% d$vi[which(d$clusters != 3)]) names(ll) <- d$ii return(ll) }) x <- lapply(cfm, function(d) { ll <- list(!(1:nrow(counts)) %in% d$vi[which(d$clusters > 1)], !(1:nrow(counts)) %in% d$vi[which(d$clusters %% 3 != 0)]) names(ll) <- d$ii return(ll) }) vil <- do.call(cbind, tapply(unlist(x, recursive = FALSE), factor(unlist(lapply(x, names)), levels = colnames(counts)[!is.na(groups)]), function(l) { x <- rowSums(do.call(cbind, l), na.rm = FALSE) == 0 x[is.na(x)] <- FALSE return(x) })) } # order entries by the number of non-failed experiments, # select entries for library size estimation ni <- cbind(1:nrow(counts), rowSums(vil)) ni <- ni[order(ni[, 2], decreasing = TRUE), ] if(nrow(ni) 0) { if(verbose > 1) { cat(paste("WARNING: unable to find cross-fit models for the following pairs : ", paste(pn1[!vi], collapse = " ")), "\n") } else { cat("WARNING: unable to find cross-fit models for ", sum(!vi), " out of ", length(vi), " pairs. Using a subset.\n") } } # use a subset if(sum(vi) > 3) { pn1 <- pn1[vi] pn2 <- pn2[vi] vi <- vi[vi] } else { stop("less than 3 valid cross-fit pairs are available! giving up.") } } #rl <- cfm[vi] vi.names<-names(cfm)[names(cfm) %in% c(pn1, pn2)] ### a similar selection was done like this in calculate.crossfit.models() function rl <- cfm[vi.names] ### with this sub-selection we select only sample pairs within the current group (e.g. pairs of ES) # determine the set genes that were not attributed to failure in any cross-comparison x <- lapply(rl, function(d) { ll <- list(!(1:nrow(counts)) %in% d$vi[which(d$clusters > 1)], !(1:nrow(counts)) %in% d$vi[which(d$clusters %% 3 != 0)]) names(ll) <- d$ii return(ll) }) vil <- do.call(cbind, tapply(unlist(x, recursive = FALSE), factor(unlist(lapply(x, names)), levels = ids), function(l) { x <- rowSums(do.call(cbind, l), na.rm = FALSE) == 0 x[is.na(x)] <- FALSE return(x) })) #x <- lapply(rl, function(d) { ll <- list((d$failures == 1), (d$failures == 2)) names(ll) <- d$ii return(ll) }) #vil <- do.call(cbind, tapply(unlist(x, recursive = FALSE), factor(unlist(lapply(x, names)), levels = ids), function(l) { x <- rowSums(do.call(cbind, l), na.rm = FALSE) == 0 x[is.na(x)] <- FALSE return(x) })) t.ls <- ls$ls[ids] adjust <- NULL if(!is.null(ls$adjustments)) { ls$adjustments[[groups[ids[1]]]] } # fit two-NB2 mixture for each experiment if(verbose) { message(paste("fitting", group, "models:")) } gc() # pair cell name matrix nm <- do.call(rbind, lapply(rl, function(x) x$ii)) ml <- papply(seq_along(ids), function(i) { try({ if(verbose) message(paste(i, ":", ids[i])) # determine genes with sufficient number of non-failed observations in other experiments vi <- which(rowSums(vil[, -i, drop = FALSE]) >= min(length(ids)-1, min.nonfailed)) fpm <- rowMeans(t(t(counts[vi, ids[-i], drop = FALSE])/(t.ls[-i]))) if(!is.null(adjust)) { fpm <- adjust(fpm) } # adjust for between-group systematic differences df <- data.frame(count = counts[vi, ids[i]], fpm = fpm) # reconstruct failure prior for the cell by averaging across # cross-cell comparisons where the cell did participate cp <- exp(rowMeans(log(cbind( do.call(cbind, lapply(rl[which(nm[, 1] == ids[i])], function(d) { ivi <- rep(NA, nrow(counts)) ivi[d$vi] <- 1:length(d$vi) d$posterior[ivi[vi], 1] })), do.call(cbind, lapply(rl[which(nm[, 2] == ids[i])], function(d) { ivi <- rep(NA, nrow(counts)) ivi[d$vi] <- 1:length(d$vi) d$posterior[ivi[vi], 2] })) )), na.rm = TRUE)) cp <- cbind(cp, 1-cp) nai <- which(is.na(cp[, 1])) cp[nai, 1] <- 1-(1e-10) cp[nai, 2] <- (1e-10) if(linear.fit) { m1 <- fit.nb2gth.mixture.model(df, prior = cp, nrep = 1, verbose = verbose, zero.count.threshold = zero.count.threshold, full.theta.range = theta.fit.range, theta.fit.range = theta.fit.range, use.constant.theta.fit = !local.theta.fit, ...) } else { m1 <- fit.nb2.mixture.model(df, prior = cp, nrep = nrep, verbose = verbose, zero.count.threshold = zero.count.threshold, ...) } if(return.compressed.models) { v <- get.compressed.v1.model(m1) cl <- clusters(m1) rm(m1) gc() return(list(model = v, clusters = cl)) } # otherwise try to reduce the size of a full model # reduce return size #m1@posterior <- lapply(m1@posterior, function(m) { rownames(m) <- NULL return(m)}) m1@posterior <- NULL #rownames(m1@concomitant@x) <- NULL m1@concomitant@x <- matrix() m1@model <- lapply(m1@model, function(mod) { mod@x <- matrix() mod@y <- matrix() #rownames(mod@x) <- NULL #rownames(mod@y) <- NULL return(mod) }) # make a clean copy of the internal environment t.cleanenv <- function(comp) { el <- list2env(as.list(environment(comp@logLik), all.names = TRUE), parent = globalenv()) ep <- list2env(as.list(environment(comp@predict), all.names = TRUE), parent = globalenv()) pf <- get("predict", envir = el) environment(pf) <- ep assign("predict", pf, envir = el) pf <- get("predict", envir = ep) environment(pf) <- ep assign("predict", pf, envir = ep) pf <- get("logLik", envir = el) environment(pf) <- el assign("logLik", pf, envir = el) pf <- get("logLik", envir = ep) environment(pf) <- el assign("logLik", pf, envir = ep) environment(comp@logLik) <- el environment(comp@predict) <- ep comp } m1@components <- lapply(m1@components, function(cl) lapply(cl, t.cleanenv)) # clean up the formula environment (was causing multithreading problems) rm(list = ls(env = attr(m1@concomitant@formula, ".Environment")), envir = attr(m1@concomitant@formula, ".Environment")) gc() #rm(list = ls(env = attr(m1@formula, ".Environment")), envir = attr(m1@formula, ".Environment")) return(m1) })}, n.cores = n.cores) # end cell iteration # check if there were errors in the multithreaded portion vic <- which(unlist(lapply(seq_along(ml), function(i) { if(class(ml[[i]]) == "try-error") { message("ERROR encountered in building a model for cell ", ids[i], " - skipping the cell. Error:") message(ml[[i]]) #tryCatch(stop(paste("ERROR encountered in building a model for cell ", ids[i])), error = function(e) stop(e)) return(FALSE); } return(TRUE); }))) ml <- ml[vic]; names(ml) <- ids[vic]; if(length(vic)0) { # model fits #CairoPNG(filename = paste(group, "model.fits.png", sep = "."), width = 1024, height = 300*length(ids)) pdf(file = paste(group, "model.fits.pdf", sep = "."), width = ifelse(linear.fit, 15, 13), height = 4) #l <- layout(matrix(seq(1, 4*length(ids)), nrow = length(ids), byrow = TRUE), rep(c(1, 1, 1, 0.5), length(ids)), rep(1, 4*length(ids)), FALSE) l <- layout(matrix(seq(1, 4), nrow = 1, byrow = TRUE), rep(c(1, 1, 1, ifelse(linear.fit, 1, 0.5)), 1), rep(1, 4), FALSE) par(mar = c(3.5, 3.5, 3.5, 0.5), mgp = c(2.0, 0.65, 0), cex = 0.9) invisible(lapply(seq_along(vic), function(j) { i <- vic[j]; vi <- which(rowSums(vil[, -i, drop = FALSE]) >= min(length(ids)-1, min.nonfailed)) df <- data.frame(count = counts[vi, ids[i]], fpm = rowMeans(t(t(counts[vi, ids[-i], drop = FALSE])/(t.ls[-i])))) plot.nb2.mixture.fit(ml[[j]], df, en = ids[i], do.par = FALSE, compressed.models = return.compressed.models) })) dev.off() } return(ml) }) # end group iteration if(return.compressed.models) { # make a joint model matrix jmm <- data.frame(do.call(rbind, lapply(mll, function(tl) do.call(rbind, lapply(tl, function(m) m$model))))) rownames(jmm) <- unlist(lapply(mll, names)) # reorder in the original cell order attr(jmm, "groups") <- rep(names(mll), unlist(lapply(mll, length))) return(jmm) } else { return(mll) } } ####### ## V1 optimized methods ####### # gets an array summary of gam model structure (assumes a flat ifm list) get.compressed.v1.models <- function(ifml) { data.frame(do.call(rbind, lapply(ifml, get.compressed.v1.model))) } # get a vector representation of a given model get.compressed.v1.model <- function(m1) { if(class(m1@model[[2]]) == "FLXMRnb2gthC") { # linear fit model v <- c(m1@concomitant@coef[c(1:2), 2], get("coef", environment(m1@components[[1]][[1]]@predict))) names(v) <- c("conc.b", "conc.a", "fail.r") vth <- m1@components[[2]][[2]]@parameters$coef # translate mu regression from linear to log model v <- c(v, c("corr.b" = log(as.numeric(vth["corr.a"])), "corr.a" = 1), vth[-match("corr.a", names(vth))], "conc.a2" = m1@concomitant@coef[3, 2]) } else { # original publication model v <- c(m1@concomitant@coef[, 2], get("coef", environment(m1@components[[1]][[1]]@predict)), m1@components[[2]][[2]]@parameters$coef, get("theta", environment(m1@components[[2]][[2]]@predict))) names(v) <- c("conc.b", "conc.a", "fail.r", "corr.b", "corr.a", "corr.theta") } v } # calculates posterior matrices (log scale) for a set of ifm models calculate.posterior.matrices <- function(dat, ifm, prior, n.cores = 32, inner.cores = 4, outer.cores = round(n.cores/inner.cores)) { marginals <- data.frame(fpm = 10^prior$x - 1) marginals$fpm[marginals$fpm<0] <- 0 lapply(ifm, function(group.ifm) { papply(sn(names(group.ifm)), function(nam) { df <- get.exp.logposterior.matrix(group.ifm[[nam]], dat[, nam], marginals, n.cores = inner.cores, grid.weight = prior$grid.weight) rownames(df) <- rownames(dat) colnames(df) <- as.character(prior$x) return(df) }, n.cores = n.cores) }) } sample.posterior <- function(dat, ifm, prior, n.samples = 1, n.cores = 32) { marginals <- data.frame(fpm = 10^prior$x - 1) lapply(ifm, function(group.ifm) { papply(sn(names(group.ifm)), function(nam) { get.exp.sample(group.ifm[[nam]], dat[, nam], marginals, prior.x = prior$x, n = n.samples) }, n.cores = n.cores) }) } # calculate joint posterior matrix for a given group of experiments # lmatl - list of posterior matrices (log scale) for individual experiments calculate.joint.posterior.matrix <- function(lmatl, n.samples = 100, bootstrap = TRUE, n.cores = 15) { if(bootstrap) { jpl <- papply(seq_len(n.cores), function(i) jpmatLogBoot(Matl = lmatl, Nboot = ceiling(n.samples/n.cores), Seed = i), n.cores = n.cores) jpl <- Reduce("+", jpl) jpl <- jpl/rowSums(jpl) } else { jpl <- Reduce("+", lmatl) jpl <- exp(jpl-log.row.sums(jpl)) } rownames(jpl) <- rownames(lmatl[[1]]) colnames(jpl) <- colnames(lmatl[[1]]) jpl } # calculate joint posterior of a group defined by a composition vector # lmatll - list of posterior matrix lists (as obtained from calculate.posterior.matrices) # composition - a named vector, indicating the number of samples that should be drawn from each element of lmatll to compose a group calculate.batch.joint.posterior.matrix <- function(lmatll, composition, n.samples = 100, n.cores = 15) { # reorder composition vector to match lmatll names jpl <- papply(seq_len(n.cores), function(i) jpmatLogBatchBoot(lmatll, composition[names(lmatll)], ceiling(n.samples/n.cores), i), n.cores = n.cores) jpl <- Reduce("+", jpl) jpl <- jpl/rowSums(jpl) #jpl <- jpmatLogBatchBoot(lmatll, composition[names(lmatll)], n.samples, n.cores) rownames(jpl) <- rownames(lmatll[[1]][[1]]) colnames(jpl) <- colnames(lmatll[[1]][[1]]) jpl } # calculates the likelihood of expression difference based on # two posterior matrices (not adjusted for prior) calculate.ratio.posterior <- function(pmat1, pmat2, prior, n.cores = 15, skip.prior.adjustment = FALSE) { n <- length(prior$x) if(!skip.prior.adjustment) { pmat1 <- t(t(pmat1)*prior$y) pmat2 <- t(t(pmat2)*prior$y) } chunk <- function(x, n) split(x, sort(rank(x) %% n)) if(n.cores > 1) { x <- do.call(rbind, papply(chunk(1:nrow(pmat1), n.cores*5), function(ii) matSlideMult(pmat1[ii, , drop = FALSE], pmat2[ii, , drop = FALSE]), n.cores = n.cores)) } else { x <- matSlideMult(pmat1, pmat2) } x <- x/rowSums(x) rv <- seq(prior$x[1]-prior$x[length(prior$x)], prior$x[length(prior$x)]-prior$x[1], length = length(prior$x)*2-1) colnames(x) <- as.character(rv) rownames(x) <- rownames(pmat1) return(x) } # quick utility function to get the difference Z score from the ratio posterior get.ratio.posterior.Z.score <- function(rpost, min.p = 1e-15) { rpost <- rpost+min.p rpost <- rpost/rowSums(rpost) zi <- which.min(abs(as.numeric(colnames(rpost)))) gs <- rowSums(rpost[, 1:(zi-1), drop = FALSE]) zl <- pmin(0, qnorm(gs, lower.tail = FALSE)) zg <- pmax(0, qnorm(gs+rpost[, zi, drop = FALSE], lower.tail = FALSE)) z <- ifelse(abs(zl) > abs(zg), zl, zg) } # calculate a joint posterior matrix with bootstrap jpmatLogBoot <- function(Matl, Nboot, Seed) { .Call("jpmatLogBoot", Matl, Nboot, Seed, PACKAGE = "scde") } # similar to the above, but compiles joint by sampling a pre-set # number of different types (defined by Comp factor) jpmatLogBatchBoot <- function(Matll, Comp, Nboot, Seed) { .Call("jpmatLogBatchBoot", Matll, Comp, Nboot, Seed, PACKAGE = "scde") } matSlideMult <- function(Mat1, Mat2) { .Call("matSlideMult", Mat1, Mat2, PACKAGE = "scde") } calculate.failure.p <- function(dat, ifm, n.cores = 32) { lapply(ifm, function(group.ifm) { lapply(sn(names(group.ifm)), function(nam) { get.concomitant.prob(group.ifm[[nam]], counts = dat[, nam]) }) }) } # calculate failure probabilities across all cells for a given set # of levels (lfpm - log(fpm) vector for all genes calculate.failure.lfpm.p <- function(lfpm, ifm, n.cores = 32) { lapply(ifm, function(group.ifm) { lapply(sn(names(group.ifm)), function(nam) { get.concomitant.prob(group.ifm[[nam]], lfpm = lfpm) }) }) } # get expected fpm from counts get.fpm.estimates <- function(m1, counts) { if(class(m1@components[[2]][[2]]) == "FLXcomponentE") { # gam do inverse interpolation b1 <- get("b1", envir = environment(m1@components[[2]][[2]]@predict)) z <- approx(x = b1$fitted.values, y = b1$model$x, xout = counts, rule = 1:2)$y z[is.na(z)] <- -Inf z } else { # linear model par <- m1@components[[2]][[2]]@parameters if(!is.null(par[["linear"]])) { log((counts-par$coef[[1]])/par$coef[[2]]) } else { (log(counts)-par$coef[[1]])/par$coef[[2]] } } } ####### ## INTERNAL FUNCTIONS ####### # clean up stale web server reference .onAttach <- function(...) { if(exists("___scde.server", envir = globalenv())) { old.server <- get("___scde.server", envir = globalenv()) n.apps <- length(old.server$appList)-1 # TODO fix server rescue... packageStartupMessage("scde: found stale web server instance with ", n.apps, " apps. removing.") # remove rm("___scde.server", envir = globalenv()) return(TRUE) if(n.apps > 0) { require(Rook) require(rjson) packageStartupMessage("scde: found stale web server instance with ", n.apps, " apps. restarting.") rm("___scde.server", envir = globalenv()) # remove old instance (apparently saved Rook servers can't just be restarted ... we'll make a new one and re-add all of the apps tryCatch( { server <- get.scde.server(ip = old.server$listenAddr, port = old.server$listenPort) # launch a new server if(!is.null(server)) { lapply(old.server$appList[-1], function(sa) { server$add(app = sa$app, name = sa$name) }) } }, error = function(e) message(e)) } else { packageStartupMessage("scde: found stale web server instance with ", n.apps, " apps. removing.") # remove rm("___scde.server", envir = globalenv()) } } } .onUnload <- function(libpath) { library.dynam.unload("scde", libpath, verbose = TRUE) } # rdf : count/fpm data frame fit.nb2.mixture.model <- function(rdf, zero.count.threshold = 10, prior = cbind(rdf$count<= zero.count.threshold, rdf$count > zero.count.threshold), nrep = 3, iter = 50, verbose = 0, background.rate = 0.1, ...) { #mo1 <- FLXMRnb2glmC(count~1, components = c(1), theta.range = c(0.5, Inf)) #mo1 <- FLXMRglmCf(count~1, components = c(1), family = "poisson", mu = 0.01) #mo1 <- FLXMRglmC(count~1, components = c(1), family = "poisson") mo1 <- FLXMRglmCf(count~1, family = "poisson", components = c(1), mu = log(background.rate)) mo2 <- FLXMRnb2glmC(count~1+I(log(fpm)), components = c(2), theta.range = c(0.5, Inf)) m1 <- mc.stepFlexmix(count~1, data = rdf, k = 2, model = list(mo1, mo2), control = list(verbose = verbose, minprior = 0, iter = iter), concomitant = FLXPmultinom(~I(log(fpm))+1), cluster = prior, nrep = nrep, ...) # check if the theta was underfit if(get("theta", envir = environment(m1@components[[2]][[2]]@logLik)) == 0.5) { # refit theta sci <- clusters(m1) == 2 fit <- glm.nb.fit(m1@model[[2]]@x[sci, , drop = FALSE], m1@model[[2]]@y[sci], weights = rep(1, sum(sci)), offset = c(), init.theta = 0.5) assign("theta", value = fit$theta, envir = environment(m1@components[[2]][[2]]@logLik)) m1@components[[2]][[2]]@parameters$coef <- fit$coefficients assign("coef", value = fit$coefficients, envir = environment(m1@components[[2]][[2]]@logLik)) message("WARNING: theta was underfit, new theta = ", fit$theta) } return(m1) } fit.nb2gth.mixture.model <- function(rdf, zero.count.threshold = 10, prior = as.integer(rdf$count >= zero.count.threshold | rdf$fpm zero.count.threshold, 0.95, 0.05))) mo1 <- FLXMRglmCf(count~1, family = "poisson", components = c(1), mu = log(0.1)) mo2 <- FLXMRnb2gthC(count~0+fpm, components = c(2), full.theta.range = full.theta.range, theta.fit.range = theta.fit.range, theta.fit.sp = theta.sp, constant.theta = use.constant.theta.fit, alpha.weight.power = alpha.weight.power) m1 <- mc.stepFlexmix(count~1, data = rdf, k = 2, model = list(mo1, mo2), control = list(verbose = verbose, minprior = 0, iter = iter), concomitant = FLXPmultinom(~I(log(fpm))+I(log(fpm)^2)+1), cluster = prior, nrep = nrep) return(m1) } # rdf : count/fpm data frame # en : experiment name for plotting # n.zero.windows - number of windows to visualize failure model fit # m1 - fitted model plot.nb2.mixture.fit <- function(m1, rdf, en, do.par = TRUE, n.zero.windows = 50, compressed.models = FALSE, bandwidth = 0.05) { #require(Cairo) require(RColorBrewer) if(do.par) { CairoPNG(filename = paste(en, "model.fit.png", sep = "."), width = 800, height = 300) l <- layout(matrix(c(1:4), 1, 4, byrow = TRUE), c(1, 1, 1, 0.5), rep(1, 4), FALSE) par(mar = c(3.5, 3.5, 3.5, 0.5), mgp = c(2.0, 0.65, 0), cex = 0.9) } smoothScatter(log10(rdf$fpm+1), log10(rdf$count+1), xlab = "expected FPM", ylab = "observed counts", main = paste(en, "scatter", sep = " : "), bandwidth = bandwidth) plot(c(), c(), xlim = range(log10(rdf$fpm+1)), ylim = range(log10(rdf$count+1)), xlab = "expected FPM", ylab = "observed counts", main = paste(en, "components", sep = " : ")) if(compressed.models) { vpi <- m1$clusters == 1 } else { vpi <- clusters(m1) == 1 } if(sum(vpi) > 2){ points(log10(rdf$fpm[vpi]+1), log10(rdf$count[vpi]+1), pch = ".", col = densCols(log10(rdf$fpm[vpi]+1), log10(rdf$count[vpi]+1), colramp = colorRampPalette(brewer.pal(9, "Reds")[-(1:3)])), cex = 2) } if(sum(!vpi) > 2){ points(log10(rdf$fpm[!vpi]+1), log10(rdf$count[!vpi]+1), pch = ".", col = densCols(log10(rdf$fpm[!vpi]+1), log10(rdf$count[!vpi]+1), colramp = colorRampPalette(brewer.pal(9, "Blues")[-(1:3)])), cex = 2) # show fit fpmo <- order(rdf$fpm[!vpi], decreasing = FALSE) if(compressed.models) { #rf <- scde.failure.probability(data.frame(t(m1$model)), magnitudes = log(rdf$fpm)) lines(log10(rdf$fpm[!vpi]+1)[fpmo], log10(exp(m1$model[["corr.a"]]*log(rdf$fpm[!vpi])[fpmo]+m1$model[["corr.b"]])+1)) if("corr.ltheta.b" %in% names(m1$model)) { # show 95% CI for the non-constant theta fit xval <- range(log(rdf$fpm[!vpi])) xval <- seq(xval[1], xval[2], length.out = 100) thetas <- get.corr.theta(m1$model, xval) #thetas <- exp(m1$model[["corr.ltheta.i"]]+m1$model[["corr.ltheta.lfpm"]]*xval) #thetas <- (1+exp((m1$model["corr.ltheta.lfpm.m"] - xval)/m1$model["corr.ltheta.lfpm.s"]))/m1$model["corr.ltheta.a"] alpha <- 0.05 yval <- exp(m1$model[["corr.a"]]*xval + m1$model[["corr.b"]]) lines(log10(exp(xval)+1), log10(qnbinom(alpha/2, size = thetas, mu = yval)+1), col = 1, lty = 2) lines(log10(exp(xval)+1), log10(qnbinom(1-alpha/2, size = thetas, mu = yval)+1), col = 1, lty = 2) lines(log10(exp(xval)+1), log10(qnbinom(alpha/2, size = m1$model[["corr.theta"]], mu = yval)+1), col = 8, lty = 2) lines(log10(exp(xval)+1), log10(qnbinom(1-alpha/2, size = m1$model[["corr.theta"]], mu = yval)+1), col = 8, lty = 2) } } else { lines(log10(rdf$fpm[!vpi]+1)[fpmo], log10(m1@components[[2]][[2]]@predict(cbind(1, log(rdf$fpm[!vpi])))+1)[fpmo], col = 4) } } legend(x = "topleft", col = c("red", "blue"), pch = 19, legend = c("failure component", "correlated component"), bty = "n", cex = 0.9) # zero fit if(n.zero.windows > nrow(rdf)) { n.zero.windows <- nrow(rdf) } bw <- floor(nrow(rdf)/n.zero.windows) if(compressed.models) { rdf$cluster <- m1$clusters } else { rdf$cluster <- clusters(m1) } rdf <- rdf[order(rdf$fpm, decreasing = FALSE), ] fdf <- data.frame(y = rowMeans(matrix(log10(rdf$fpm[1:(n.zero.windows*bw)]+1), ncol = bw, byrow = TRUE)), zf = rowMeans(matrix(as.integer(rdf$cluster[1:(n.zero.windows*bw)] == 1), ncol = bw, byrow = TRUE))) plot(zf~y, fdf, ylim = c(0, 1), xlim = range(na.omit(log10(rdf$fpm+1))), xlab = "expected FPM", ylab = "fraction of failures", main = "failure model", pch = 16, cex = 0.5) ol <- order(rdf$fpm, decreasing = TRUE) if(compressed.models) { fp <- scde.failure.probability(data.frame(t(m1$model)), magnitudes = log(rdf$fpm)) lines(log10(rdf$fpm[ol]+1), fp[ol], col = 2) } else { mt <- terms(m1@concomitant@formula, data = rdf) mf <- model.frame(delete.response(mt), data = rdf, na.action = NULL) cm0 <- exp(model.matrix(mt, data = mf) %*% m1@concomitant@coef) cm0 <- cm0/rowSums(cm0) lines(log10(rdf$fpm[ol]+1), cm0[ol, 1], col = 2) } # show thetas #tl <- c(fail = get("theta", envir = environment(m1@components[[1]][[1]]@logLik)), corr = get("theta", envir = environment(m1@components[[2]][[2]]@logLik))) if(compressed.models) { if("corr.ltheta.b" %in% names(m1$model)) { p <- exp(m1$model[["corr.a"]]*log(rdf$fpm[!vpi])+m1$model[["corr.b"]]) alpha <- ((rdf$count[!vpi]/p-1)^2 - 1/p) trng <- log(range(c(m1$model[["corr.theta"]], thetas))) + 0.5*c(-1, 1) # restrict the alpha to the confines of the estimated theta values alpha[alpha > exp(-trng[1])] <- exp(-trng[1]) alpha[alpha 1) stop(paste("for the", family, "family y must be univariate")) x } z@defineComponent <- expression({ predict <- function(x, ...) { dotarg = list(...) #message("FLXRnb2glm:predict:nb2") if("offset" %in% names(dotarg)) offset <- dotarg$offset p <- x%*%coef if (!is.null(offset)) p <- p + offset negative.binomial(theta)$linkinv(p) } logLik <- function(x, y, ...) { r <- dnbinom(y, size = theta, mu = predict(x, ...), log = TRUE) #message(paste("FLXRnb2glm:loglik:nb2", theta)) return(r) } new("FLXcomponent", parameters = list(coef = coef), logLik = logLik, predict = predict, df = df) }) z@fit <- function(x, y, w){ #message("FLXRnb2glm:fit:nb2") w[y<= 1] <- w[y<= 1]/1e6 # focus the fit on non-failed genes fit <- glm.nb.fit(x, y, weights = w, offset = offset, init.theta = init.theta, theta.range = theta.range) # an ugly hack to restrict to non-negative slopes cf <- coef(fit) if(cf[2]<0) { cf <- c(mean(y*w)/sum(w), 0) } with(list(coef = cf, df = ncol(x), theta = fit$theta, offset = offset), eval(z@defineComponent)) } return(z) } # component-specific version of the nb2glm # nb2 glm implementation setClass("FLXMRnb2glmC", representation(vci = "ANY"), contains = "FLXMRnb2glm", package = "flexmix") # components is used to specify the indices of the components on which likelihood will be # evaluated. Others will return as loglik of 0 FLXMRnb2glmC <- function(... , components = NULL) { #require(MASS) z <- new("FLXMRnb2glmC", FLXMRnb2glm(...), vci = components) z } # nb2 glm implementation setClass("FLXMRnb2gam", contains = "FLXMRglm", package = "flexmix") setClass("FLXcomponentE", representation(refitTheta = "function", theta.fit = "ANY"), contains = "FLXcomponent", package = "flexmix") # nb2 implementation with a simple trimmed-mean/median slope, and a gam theta fit setClass("FLXMRnb2gth", contains = "FLXMRglm", package = "flexmix") # get values of theta for a given set of models and expression (log-scale) magnitudes get.corr.theta <- function(model, lfpm, theta.range = NULL) { if("corr.ltheta.b" %in% names(model)) { #th <- exp(-1*(model[["corr.ltheta.a"]]/(1+exp((model[["corr.ltheta.lfpm.m"]] - lfpm)/model[["corr.ltheta.lfpm.s"]])) + log(model[["corr.ltheta.b"]]))) th <- exp(-1*(model[["corr.ltheta.b"]]+(model[["corr.ltheta.t"]]-model[["corr.ltheta.b"]])/(1+10^((model[["corr.ltheta.m"]]-lfpm)*model[["corr.ltheta.s"]]))^model[["corr.ltheta.r"]])) } else { if(length(lfpm) > 1) { th <- rep(model[["corr.theta"]], length(lfpm)) } else { th <- model[["corr.theta"]] } } if(!is.null(theta.range)) { th[th theta.range[2]] <- theta.range[2] th[is.nan(th)] <- theta.range[1] } th } FLXMRnb2gth <- function(formula = . ~ ., offset = NULL, full.theta.range = c(1e-3, 1e3), theta.fit.range = full.theta.range*c(1e-1, 1e1), theta.fit.sp = c(-1), constant.theta = FALSE, slope.mean.trim = 0.4, alpha.weight.power = 1/2, ...) { if(slope.mean.trim<0) { slope.mean.trim <- 0 } if(slope.mean.trim > 0.5) { slope.mean.trim <- 0.5 } family <- "negative.binomial" glmrefit <- function(x, y, w) { message("ERROR: FLXRnb2gth:glmrefit: NOT IMPLEMENTED") return(NULL) } z <- new("FLXMRnb2gth", weighted = TRUE, formula = formula, name = "FLXMRnb2gth", offset = offset, family = family, refit = glmrefit) z@preproc.y <- function(x) { if (ncol(x) > 1) stop(paste("for the", family, "family y must be univariate")) x } z@defineComponent <- expression({ predict <- function(x, ...) { dotarg = list(...) #message("FLXRnb2gth:predict:nb2") coef["corr.a"]*x } logLik <- function(x, y, ...) { dotarg = list(...) #message("FLXRnb2gth:logLik") if(constant.theta) { th <- coef["corr.theta"] } else { #th <- exp(coef["corr.ltheta.i"] + coef["corr.ltheta.lfpm"]*log(x)) #th <- exp(-1*(coef["corr.ltheta.a"]/(1+exp((coef["corr.ltheta.lfpm.m"] - log(x))/coef["corr.ltheta.lfpm.s"])) + log(coef["corr.ltheta.b"]))) th <- get.corr.theta(coef, log(x)) } # restrict theta to the pre-defined range th[th > full.theta.range[2]] <- full.theta.range[2] th[th < full.theta.range[1]] <- full.theta.range[1] # evaluate NB r <- dnbinom(y, size = th, mu = coef["corr.a"]*x, log = TRUE) } new("FLXcomponent", parameters = list(coef = coef, linear = TRUE), logLik = logLik, predict = predict, df = df) }) z@fit <- function(x, y, w){ # message("FLXRnb2gth:fit") # estimate slope using weighted trimmed mean #w[y == 0] <- w[y == 0]/1e6 #r <- y/x #ro <- order(r) ## cumulative weight sum along the ratio order (to figure out where to trim) #cs <- cumsum(w[ro])/sum(w) #lb <- min(which(cs > slope.mean.trim)) #ub <- max(which(cs<(1-slope.mean.trim))) #ro <- ro[lb:ub] ## slope fit #a <- weighted.mean(r[ro], w[ro]) a <- as.numeric(coef(glm(y~0+x, family = poisson(link = "identity"), start = weighted.mean(y/x, w), weights = w))[1]) # predicted values p <- a*x #te <- p^2/((y-p)^2 - p) # theta point estimates #te[te theta.fit.range[2]] <- theta.fit.range[2] alpha <- ((y/p-1)^2 - 1/p) alpha[alpha<1/theta.fit.range[2]] <- 1/theta.fit.range[2] alpha[alpha > 1/theta.fit.range[1]] <- 1/theta.fit.range[1] #theta <- MASS::theta.ml(y, p, sum(w), w) theta <- MASS::theta.md(y, p, sum(w)-1, w) theta <- pmin(pmax(theta.fit.range[1], theta), theta.fit.range[2]) if(constant.theta) { v <- c("corr.a" = a, "corr.theta" = theta) } else { # fit theta linear model in the log space #theta.l <- glm(log(te)~log(x), weights = w) #ac <- tryCatch( { mw <- w*(as.numeric(alpha)^(alpha.weight.power)) lx <- log(x) lx.rng <- range(lx) mid.s <- (sum(lx.rng))/2 low <- log(x) < mid.s lalpha <- log(alpha) bottom.s <- quantile(lalpha[low], 0.025, na.rm = TRUE) top.s <- quantile(lalpha[!low], 0.975, na.rm = TRUE) wsr <- function(p, x, y, w = rep(1, length(y))) { # borrowing nplr approach here bottom <- p[1] top <- p[2] xmid <- p[3] scal <- p[4] r <- p[5] yfit <- bottom+(top-bottom)/(1+10^((xmid-x)*scal))^r #exp(-1*(model[["corr.ltheta.b"]]+(model[["corr.ltheta.t"]]-model[["corr.ltheta.b"]])/(1+10^((model[["corr.ltheta.m"]]-lfpm)*model[["corr.ltheta.s"]]))^model[["corr.ltheta.r"]])) residuals <- (y - yfit)^2 return(sum(w*residuals)) } #po <- nlm(f = wsr, p = c(bottom.s, top.s, mid.s, s = -1, r = 0.5), x = log(x), y = lalpha, w = w*(as.numeric(alpha)^(1/2))) #ac <- po$estimate #po <- nlminb(objective = wsr, start = c(bottom.s, top.s, mid.s, s = -1, r = 0.5), x = log(x), y = lalpha, w = mw) po <- nlminb(objective = wsr, start = c(bottom.s, top.s, mid.s, s = -1, r = 0.5), x = log(x), y = lalpha, w = mw, lower = c(-100, -10, -100, -100, 0.1), upper = c(10, 100, 100, 0, 20)) ac <- po$par #smoothScatter(log(x), log(alpha)) #p <- ac points(log(x), p[1]+(p[2]-p[1])/(1+10^((p[3]-log(x))*p[4]))^p[5], col = 2, pch = ".") #browser() #}, error = function(e) { # message("encountered error trying to fit logistic model with guessed parameters.") # # fit with fewer parameters? #}) v <- c(a, theta, ac) names(v) <- c("corr.a", "corr.theta", "corr.ltheta.b", "corr.ltheta.t", "corr.ltheta.m", "corr.ltheta.s", "corr.ltheta.r") } with(list(coef = v, full.theta.range = full.theta.range, df = ncol(x), offset = offset), eval(z@defineComponent)) } return(z) } # component-specific version of the nb2gth # nb2 gam implementation setClass("FLXMRnb2gthC", representation(vci = "ANY"), contains = "FLXMRnb2gth", package = "flexmix") # components is used to specify the indices of the components on which likelihood will be # evaluated. Others will return as loglik of 0 FLXMRnb2gthC <- function(... , components = NULL) { #require(mgcv) z <- new("FLXMRnb2gthC", FLXMRnb2gth(...), vci = components) z } setMethod("FLXdeterminePostunscaled", signature(model = "FLXMRnb2glmC"), function(model, components, ...) { if(is.null(model@vci)) { #message("FLXMRnb2glmC:FLXdeterminePostunscaled - applying to all components") m <- matrix(sapply(components, function(x) x@logLik(model@x, model@y)), nrow = nrow(model@y)) } else { #message(paste("FLXMRnb2glmC:FLXdeterminePostunscaled - applying to components", paste(model@vci, collapse = " "))) m <- matrix(do.call(cbind, lapply(seq_along(components), function(i) { if(i %in% model@vci) { components[[i]]@logLik(model@x, model@y) } else { rep(0, nrow(model@y)) } })), nrow = nrow(model@y)) } }) setMethod("FLXmstep", signature(model = "FLXMRnb2glmC"), function(model, weights, ...) { # make up a dummy component return coef <- rep(0, ncol(model@x)) names(coef) <- colnames(model@x) control <- eval(formals(glm.fit)$control) comp.1 <- with(list(coef = coef, df = 0, offset = NULL, family = model@family), eval(model@defineComponent)) # iterate over components unlist(lapply(seq_len(ncol(weights)), function(i) { if(i %in% model@vci) { #message(paste("FLXMRnb2glmC:FLXmstep - running m-step for component", i)) FLXmstep(as(model, "FLXMRnb2glm"), weights[, i, drop = FALSE]) } else { #message(paste("FLXMRnb2glmC:FLXmstep - dummy return for component", i)) list(comp.1) } }), recursive = FALSE) }) # same for gth setMethod("FLXdeterminePostunscaled", signature(model = "FLXMRnb2gthC"), function(model, components, ...) { if(is.null(model@vci)) { #message("FLXMRnb2gthC:FLXdeterminePostunscaled - applying to all components") m <- matrix(sapply(components, function(x) x@logLik(model@x, model@y)), nrow = nrow(model@y)) } else { #message(paste("FLXMRnb2gthC:FLXdeterminePostunscaled - applying to components", paste(model@vci, collapse = " "))) m <- matrix(do.call(cbind, lapply(seq_along(components), function(i) { if(i %in% model@vci) { components[[i]]@logLik(model@x, model@y) } else { rep(0, nrow(model@y)) } })), nrow = nrow(model@y)) } }) setMethod("FLXmstep", signature(model = "FLXMRnb2gthC"), function(model, weights, ...) { # make up a dummy component return coef <- rep(0, ncol(model@x)) names(coef) <- colnames(model@x) control <- eval(formals(glm.fit)$control) comp.1 <- with(list(q1 = list(coefficients = c(1)), coef = coef, df = 0, offset = NULL, family = model@family), eval(model@defineComponent)) # iterate over components unlist(lapply(seq_len(ncol(weights)), function(i) { if(i %in% model@vci) { #message(paste("FLXMRnb2gthC:FLXmstep - running m-step for component", i)) FLXmstep(as(model, "FLXMRnb2gth"), weights[, i, drop = FALSE]) } else { #message(paste("FLXMRnb2gthC:FLXmstep - dummy return for component", i)) list(comp.1) } }), recursive = FALSE) }) # component-specific version of the nb2glm # nb2 glm implementation setClass("FLXMRglmC", representation(vci = "ANY"), contains = "FLXMRglm", package = "flexmix") # components is used to specify the indices of the components on which likelihood will be # evaluated. Others will return as loglik of 0 FLXMRglmC <- function(... , components = NULL) { #require(MASS) z <- new("FLXMRglmC", FLXMRglm(...), vci = components) z } setMethod("FLXdeterminePostunscaled", signature(model = "FLXMRglmC"), function(model, components, ...) { if(is.null(model@vci)) { #message("FLXMRnb2glmC:FLXdeterminePostunscaled - applying to all components") m <- matrix(sapply(components, function(x) x@logLik(model@x, model@y)), nrow = nrow(model@y)) } else { #message(paste("FLXMRnb2glmC:FLXdeterminePostunscaled - applying to components", paste(model@vci, collapse = " "))) m <- matrix(do.call(cbind, lapply(seq_along(components), function(i) { if(i %in% model@vci) { components[[i]]@logLik(model@x, model@y) } else { rep(0, nrow(model@y)) } })), nrow = nrow(model@y)) } #message("FLXMRnb2glmC:FLXdeterminePostunscaled : ") #message(m) #browser() m }) setMethod("FLXmstep", signature(model = "FLXMRglmC"), function(model, weights, ...) { # make up a dummy component return coef <- rep(0, ncol(model@x)) names(coef) <- colnames(model@x) control <- eval(formals(glm.fit)$control) comp.1 <- with(list(coef = coef, df = 0, offset = NULL, family = model@family), eval(model@defineComponent)) # iterate over components unlist(lapply(seq_len(ncol(weights)), function(i) { if(i %in% model@vci) { #message(paste("FLXMRglmC:FLXmstep - running m-step for component", i)) FLXmstep(as(model, "FLXMRglm"), weights[, i, drop = FALSE]) } else { #message(paste("FLXMRglmC:FLXmstep - dummy return for component", i)) list(comp.1) } }), recursive = FALSE) }) # mu-fixed version setClass("FLXMRglmCf", representation(mu = "numeric"), contains = "FLXMRglmC", package = "flexmix") FLXMRglmCf <- function(... , family = c("binomial", "poisson"), mu = 0) { #require(MASS) family <- match.arg(family) z <- new("FLXMRglmCf", FLXMRglmC(..., family = family), mu = mu) z } setMethod("FLXmstep", signature(model = "FLXMRglmCf"), function(model, weights, ...) { # make up a dummy component return coef <- c(model@mu, rep(0, ncol(model@x)-1)) names(coef) <- colnames(model@x) control <- eval(formals(glm.fit)$control) comp.1 <- with(list(coef = coef, df = 0, offset = NULL, family = model@family), eval(model@defineComponent)) # iterate over components unlist(lapply(seq_len(ncol(weights)), function(i) { list(comp.1) }), recursive = FALSE) }) # a magnitude-weighted version of the FLXPmultinom (to down-weight low-fpkm points during concomitant fit) # alternatively: some kind of non-decreasing function could be used setClass("FLXPmultinomW", contains = "FLXPmultinom") FLXPmultinomW <- function(formula = ~1) { z <- new("FLXPmultinom", name = "FLXPmultinom", formula = formula) multinom.fit <- function(x, y, w, ...) { r <- ncol(x) p <- ncol(y) if (p < 2) stop("Multinom requires at least two components.") mask <- c(rep(0, r + 1), rep(c(0, rep(1, r)), p - 1)) #if(missing(w)) w <- rep(1, nrow(y)) #w <- round(exp(x[, 2])) nnet::nnet.default(x, y, w, mask = mask, size = 0, skip = TRUE, softmax = TRUE, censored = FALSE, rang = 0, trace = FALSE, ...) } z@fit <- function(x, y, w, ...) multinom.fit(x, y, w, ...)$fitted.values z@refit <- function(x, y, w, ...) { if (missing(w) || is.null(w)) w <- rep(1, nrow(y)) #w <- round(exp(x[, 2])) fit <- nnet::multinom(y ~ 0 + x, weights = w, data = list(y = y, x = x), Hess = TRUE, trace = FALSE) fit$coefnames <- colnames(x) fit$vcoefnames <- fit$coefnames[seq_along(fit$coefnames)] dimnames(fit$Hessian) <- lapply(dim(fit$Hessian) / ncol(x), function(i) paste(rep(seq_len(i) + 1, each = ncol(x)), colnames(x), sep = ":")) fit } z } # variation of negative.binomial family that keeps theta value accessible negbin.th <- function (theta = stop("'theta' must be specified"), link = "log") { linktemp <- substitute(link) if (!is.character(linktemp)) linktemp <- deparse(linktemp) if (linktemp %in% c("log", "identity", "sqrt")) stats <- make.link(linktemp) else if (is.character(link)) { stats <- make.link(link) linktemp <- link } else { if (inherits(link, "link-glm")) { stats <- link if (!is.null(stats$name)) linktemp <- stats$name } else stop(linktemp, " link not available for negative binomial family available links are \"identity\", \"log\" and \"sqrt\"") } .Theta <- theta env <- new.env(parent = .GlobalEnv) assign(".Theta", theta, envir = env) variance <- function(mu) mu + mu^2/.Theta validmu <- function(mu) all(mu > 0) dev.resids <- function(y, mu, wt) 2 * wt * (y * log(pmax(1, y)/mu) - (y + .Theta) * log((y + .Theta)/(mu + .Theta))) aic <- function(y, n, mu, wt, dev) { term <- (y + .Theta) * log(mu + .Theta) - y * log(mu) + lgamma(y + 1) - .Theta * log(.Theta) + lgamma(.Theta) - lgamma(.Theta + y) 2 * sum(term * wt) } initialize <- expression({ if (any(y < 0)) stop("negative values not allowed for the negative binomial family") n <- rep(1, nobs) mustart <- y + (y == 0)/6 }) simfun <- function(object, nsim) { ftd <- fitted(object) val <- rnegbin(nsim * length(ftd), ftd, .Theta) } environment(variance) <- environment(validmu) <- environment(dev.resids) <- environment(aic) <- environment(simfun) <- env famname <- paste("Negative Binomial(", format(round(theta, 4)), ")", sep = "") structure(list(family = famname, link = linktemp, linkfun = stats$linkfun, linkinv = stats$linkinv, variance = variance, dev.resids = dev.resids, aic = aic, mu.eta = stats$mu.eta, initialize = initialize, validmu = validmu, valideta = stats$valideta, simulate = simfun, theta = theta), class = "family") } # .fit version of the glm.nb glm.nb.fit <- function(x, y, weights = rep(1, nobs), control = list(trace = 0, maxit = 20), offset = rep(0, nobs), etastart = NULL, start = NULL, mustart = NULL, init.theta = NULL, link = "log", method = "glm.fit", intercept = TRUE, theta.range = c(0, 1e5), ...) { method <- "custom.glm.fit" #require(MASS) loglik <- function(n, th, mu, y, w) sum(w * (lgamma(th + y) - lgamma(th) - lgamma(y + 1) + th * log(th) + y * log(mu + (y == 0)) - (th + y) * log(th + mu))) # link <- substitute(link) Call <- match.call() control <- do.call("glm.control", control) n <- length(y) # family for the initial guess fam0 <- if (missing(init.theta) | is.null(init.theta)) do.call("poisson", list(link = link)) else do.call("negative.binomial", list(theta = init.theta, link = link)) # fit function if (!missing(method)) { #message(paste("glm.nb.fit: method = ", method)) if (!exists(method, mode = "function")) stop("unimplemented method: ", sQuote(method)) glm.fitter <- get(method) } else { #message("glm.nb.fit: using default glm.fit") method <- "glm.fit" glm.fitter <- stats::glm.fit #glm.fitter <- custom.glm.fit } if (control$trace > 1) { message("Initial fit:") } fit <- glm.fitter(x = x, y = y, weights = weights, start = start, etastart = etastart, mustart = mustart, offset = offset, family = fam0, control = control, intercept = intercept) class(fit) <- c("glm", "lm") mu <- fit$fitted.values th <- as.vector(theta.ml(y, mu, sum(weights), weights, limit = control$maxit, trace = control$trace > 2)) if(!is.null(theta.range)) { if(th 1) message("adjusting theta from ", signif(th), " to ", signif(theta.range[1]), " to fit the specified range") th <- theta.range[1] } else if(th > theta.range[2]) { if (control$trace > 1) message("adjusting theta from ", signif(th), " to ", signif(theta.range[2]), " to fit the specified range") th <- theta.range[2] } } if (control$trace > 1) message("Initial value for theta:", signif(th)) fam <- do.call("negative.binomial", list(theta = th, link = link)) iter <- 0 d1 <- sqrt(2 * max(1, fit$df.residual)) d2 <- del <- 1 g <- fam$linkfun Lm <- loglik(n, th, mu, y, weights) Lm0 <- Lm + 2 * d1 while ((iter <- iter + 1) <= control$maxit && (abs(Lm0 - Lm)/d1 + abs(del)/d2) > control$epsilon) { eta <- g(mu) fit <- glm.fitter(x = x, y = y, weights = weights, etastart = eta, offset = offset, family = fam, control = list(maxit = control$maxit*10, epsilon = control$epsilon, trace = control$trace > 1), intercept = intercept) t0 <- th th <- theta.ml(y, mu, sum(weights), weights, limit = control$maxit, trace = control$trace > 2) if(!is.null(theta.range)) { if(th 1) message("adjusting theta from ", signif(th), " to ", signif(theta.range[1]), " to fit the specified range") th <- theta.range[1] } else if(th > theta.range[2]) { if (control$trace > 1) message("adjusting theta from ", signif(th), " to ", signif(theta.range[2]), " to fit the specified range") th <- theta.range[2] } } fam <- do.call("negative.binomial", list(theta = th, link = link)) mu <- fit$fitted.values del <- t0 - th Lm0 <- Lm Lm <- loglik(n, th, mu, y, weights) if (control$trace) { Ls <- loglik(n, th, y, y, weights) Dev <- 2 * (Ls - Lm) message("Theta(", iter, ") = ", signif(th), ", 2(Ls - Lm) = ", signif(Dev)) } } if (!is.null(attr(th, "warn"))) fit$th.warn <- attr(th, "warn") if (iter > control$maxit) { warning("alternation limit reached") fit$th.warn <- gettext("alternation limit reached") } if (length(offset) && intercept) { null.deviance <- if ("(Intercept)" %in% colnames(x)) glm.fitter(x[, "(Intercept)", drop = FALSE], y, weights = weights, offset = offset, family = fam, control = list(maxit = control$maxit*10, epsilon = control$epsilon, trace = control$trace > 1), intercept = TRUE)$deviance else fit$deviance fit$null.deviance <- null.deviance } class(fit) <- c("negbin.th", "glm", "lm") Call$init.theta <- signif(as.vector(th), 10) Call$link <- link fit$call <- Call fit$x <- x fit$y <- y fit$theta <- as.vector(th) fit$SE.theta <- attr(th, "SE") fit$twologlik <- as.vector(2 * Lm) fit$aic <- -fit$twologlik + 2 * fit$rank + 2 fit$method <- method fit$control <- control fit$offset <- offset fit } custom.glm.fit <- function (x, y, weights = rep(1, nobs), start = NULL, etastart = NULL, mustart = NULL, offset = rep(0, nobs), family = gaussian(), control = list(), intercept = TRUE, alpha = 0) { control <- do.call("glm.control", control) x <- as.matrix(x) xnames <- dimnames(x)[[2L]] ynames <- if (is.matrix(y)) rownames(y) else names(y) conv <- FALSE nobs <- NROW(y) nvars <- ncol(x) EMPTY <- nvars == 0 if (is.null(weights)) weights <- rep.int(1, nobs) if (is.null(offset)) offset <- rep.int(0, nobs) variance <- family$variance linkinv <- family$linkinv if (!is.function(variance) || !is.function(linkinv)) stop("'family' argument seems not to be a valid family object", call. = FALSE) dev.resids <- family$dev.resids aic <- family$aic mu.eta <- family$mu.eta unless.null <- function(x, if.null) if (is.null(x)) if.null else x valideta <- unless.null(family$valideta, function(eta) TRUE) validmu <- unless.null(family$validmu, function(mu) TRUE) if (is.null(mustart)) { eval(family$initialize) } else { mukeep <- mustart eval(family$initialize) mustart <- mukeep } if (EMPTY) { eta <- rep.int(0, nobs) + offset if (!valideta(eta)) stop("invalid linear predictor values in empty model", call. = FALSE) mu <- linkinv(eta) if (!validmu(mu)) stop("invalid fitted means in empty model", call. = FALSE) dev <- sum(dev.resids(y, mu, weights)) w <- ((weights * mu.eta(eta)^2)/variance(mu))^0.5 residuals <- (y - mu)/mu.eta(eta) good <- rep(TRUE, length(residuals)) boundary <- conv <- TRUE coef <- numeric(0L) iter <- 0L } else { coefold <- NULL eta <- if (!is.null(etastart)) etastart else if (!is.null(start)) if (length(start) != nvars) stop(gettextf("length of 'start' should equal %d and correspond to initial coefs for %s", nvars, paste(deparse(xnames), collapse = ", ")), domain = NA) else { coefold <- start offset + as.vector(if (NCOL(x) == 1) x * start else x %*% start) } else family$linkfun(mustart) mu <- linkinv(eta) if (!(validmu(mu) && valideta(eta))) stop("cannot find valid starting values: please specify some", call. = FALSE) devold <- sum(dev.resids(y, mu, weights)) boundary <- conv <- FALSE for (iter in 1L:control$maxit) { good <- weights > 0 varmu <- variance(mu)[good] if (any(is.na(varmu))) stop("NAs in V(mu)") if (any(varmu == 0)) stop("0s in V(mu)") mu.eta.val <- mu.eta(eta) if (any(is.na(mu.eta.val[good]))) stop("NAs in d(mu)/d(eta)") good <- (weights > 0) & (mu.eta.val != 0) if (all(!good)) { conv <- FALSE warning("no observations informative at iteration ", iter) break } z <- (eta - offset)[good] + (y - mu)[good]/mu.eta.val[good] #z <- (eta - offset)[good] + (family$linkfun(y[good]) - family$linkfun(mu[good]))/family$linkfun(mu.eta.val[good]) # attempting to be robust here, trowing out fraction with highest abs(z) if(alpha > 0) { qv <- quantile(abs(z), probs = c(alpha/2, 1.0-alpha/2)) gvi <- which(good)[which(abs(z) qv[2])] good[gvi] <- FALSE if (all(!good)) { conv <- FALSE warning("no observations informative at iteration ", iter) break } z <- (eta - offset)[good] + (y - mu)[good]/mu.eta.val[good] } w <- mu.eta.val[good]*sqrt(weights[good]/variance(mu)[good]) ngoodobs <- as.integer(nobs - sum(!good)) fit <- .Fortran("dqrls", qr = x[good, ] * w, n = ngoodobs, p = nvars, y = w * z, ny = 1L, tol = min(1e-07, control$epsilon/1000), coefficients = double(nvars), residuals = double(ngoodobs), effects = double(ngoodobs), rank = integer(1L), pivot = 1L:nvars, qraux = double(nvars), work = double(2 * nvars)) # , PACKAGE = "base" #browser() if (any(!is.finite(fit$coefficients))) { conv <- FALSE warning(gettextf("non-finite coefficients at iteration %d", iter), domain = NA) break } if (nobs < fit$rank) stop(gettextf("X matrix has rank %d, but only %d observations", fit$rank, nobs), domain = NA) start[fit$pivot] <- fit$coefficients eta <- drop(x %*% start) mu <- linkinv(eta <- eta + offset) dev <- sum(dev.resids(y, mu, weights)) if (control$trace) cat("Deviance = ", dev, "Iterations -", iter, "\n") boundary <- FALSE if (!is.finite(dev)) { if (is.null(coefold)) stop("no valid set of coefficients has been found: please supply starting values", call. = FALSE) warning("step size truncated due to divergence", call. = FALSE) ii <- 1 while (!is.finite(dev)) { if (ii > control$maxit) stop("inner loop 1 cannot correct step size", call. = FALSE) ii <- ii + 1 start <- (start + coefold)/2 eta <- drop(x %*% start) mu <- linkinv(eta <- eta + offset) dev <- sum(dev.resids(y, mu, weights)) } boundary <- TRUE if (control$trace) cat("Step halved: new deviance = ", dev, "\n") } # require deviance to go down if ((!is.null(coefold)) & (dev - devold)/(0.1 + abs(dev)) > 3*control$epsilon) { warning("step size truncated due to increasing divergence", call. = FALSE) ii <- 1 while ((dev - devold)/(0.1 + abs(dev)) > 3*control$epsilon) { if (ii > control$maxit) { warning("inner loop 1 cannot correct step size", call. = FALSE) break } ii <- ii + 1 start <- (start + coefold)/2 eta <- drop(x %*% start) mu <- linkinv(eta <- eta + offset) dev <- sum(dev.resids(y, mu, weights)) } boundary <- TRUE if (control$trace) cat("Step halved: new deviance = ", dev, "\n") } if (!(valideta(eta) && validmu(mu))) { if (is.null(coefold)) stop("no valid set of coefficients has been found: please supply starting values", call. = FALSE) warning("step size truncated: out of bounds", call. = FALSE) ii <- 1 while (!(valideta(eta) && validmu(mu))) { if (ii > control$maxit) stop("inner loop 2 cannot correct step size", call. = FALSE) ii <- ii + 1 start <- (start + coefold)/2 eta <- drop(x %*% start) mu <- linkinv(eta <- eta + offset) } boundary <- TRUE dev <- sum(dev.resids(y, mu, weights)) if (control$trace) cat("Step halved: new deviance = ", dev, "\n") } if (abs(dev - devold)/(0.1 + abs(dev)) < control$epsilon) { conv <- TRUE coef <- start break } devold <- dev coef <- coefold <- start } if (!conv) warning("glm.fit: algorithm did not converge", call. = FALSE) if (boundary) warning("glm.fit: algorithm stopped at boundary value", call. = FALSE) eps <- 10 * .Machine$double.eps if (family$family == "binomial") { if (any(mu > 1 - eps) || any(mu < eps)) warning("glm.fit: fitted probabilities numerically 0 or 1 occurred", call. = FALSE) } if (family$family == "poisson") { if (any(mu < eps)) warning("glm.fit: fitted rates numerically 0 occurred", call. = FALSE) } if (fit$rank < nvars) coef[fit$pivot][seq.int(fit$rank + 1, nvars)] <- NA xxnames <- xnames[fit$pivot] residuals <- (y - mu)/mu.eta(eta) fit$qr <- as.matrix(fit$qr) nr <- min(sum(good), nvars) if (nr < nvars) { Rmat <- diag(nvars) Rmat[1L:nr, 1L:nvars] <- fit$qr[1L:nr, 1L:nvars] } else Rmat <- fit$qr[1L:nvars, 1L:nvars] Rmat <- as.matrix(Rmat) Rmat[row(Rmat) > col(Rmat)] <- 0 names(coef) <- xnames colnames(fit$qr) <- xxnames dimnames(Rmat) <- list(xxnames, xxnames) } names(residuals) <- ynames names(mu) <- ynames names(eta) <- ynames wt <- rep.int(0, nobs) wt[good] <- w^2 names(wt) <- ynames names(weights) <- ynames names(y) <- ynames if (!EMPTY) names(fit$effects) <- c(xxnames[seq_len(fit$rank)], rep.int("", sum(good) - fit$rank)) wtdmu <- if (intercept) sum(weights * y)/sum(weights) else linkinv(offset) nulldev <- sum(dev.resids(y, wtdmu, weights)) n.ok <- nobs - sum(weights == 0) nulldf <- n.ok - as.integer(intercept) rank <- if (EMPTY) 0 else fit$rank resdf <- n.ok - rank aic.model <- aic(y, length(y), mu, weights, dev) + 2 * rank list(coefficients = coef, residuals = residuals, fitted.values = mu, effects = if (!EMPTY) fit$effects, R = if (!EMPTY) Rmat, rank = rank, qr = if (!EMPTY) structure(fit[c("qr", "rank", "qraux", "pivot", "tol")], class = "qr"), family = family, linear.predictors = eta, deviance = dev, aic = aic.model, null.deviance = nulldev, iter = iter, weights = wt, prior.weights = weights, df.residual = resdf, df.null = nulldf, y = y, converged = conv, boundary = boundary) } # copied from limma weighted.median.scde <- function (x, w, na.rm = FALSE) # Weighted median # Gordon Smyth # 30 June 2005 { if (missing(w)) w <- rep.int(1, length(x)) else { if(length(w) != length(x)) stop("'x' and 'w' must have the same length") if(any(is.na(w))) stop("NA weights not allowed") if(any(w<0)) stop("Negative weights not allowed") } if(is.integer(w)) w <- as.numeric(w) if(na.rm) { w <- w[i <- !is.na(x)] x <- x[i] } if(all(w == 0)) { warning("All weights are zero") return(NA) } o <- order(x) x <- x[o] w <- w[o] p <- cumsum(w)/sum(w) n <- sum(p<0.5) if(p[n+1] > 0.5) x[n+1] else (x[n+1]+x[n+2])/2 } # FROM common.r sn <- function(x) { names(x) <- x return(x) } # panel routines for pairs() pairs.panel.hist <- function(x, i = NULL, ...) { usr <- par("usr") on.exit(par(usr)) par(usr = c(usr[1:2], 0, 1.5) ) h <- hist(x, plot = FALSE) breaks <- h$breaks nB <- length(breaks) y <- h$counts y <- y/max(y) rect(breaks[-nB], 0, breaks[-1], y, col = "gray70", ...) } pairs.panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, i = NULL, j = NULL) { usr <- par("usr") on.exit(par(usr)) par(usr = c(0, 1, 0, 1)) r <- abs(cor(x, y, method = "pearson")) #r <- abs(cor(x, y, method = "spearman")) txt <- format(c(r, 0.123456789), digits = digits)[1] txt <- paste(prefix, txt, sep = "") if(missing(cex.cor)) { cex <- 0.6/strwidth(txt) } #text(0.5, 0.5, txt, cex = cex * r) text(0.5, 0.5, txt, cex = cex) } pairs.panel.scatter <- function(x, y, i = NULL, j = NULL, ...) { vi <- x > 0 | y > 0 points(x[vi], y[vi], pch = ".", col = densCols(x[vi], y[vi], colramp = colorRampPalette(brewer.pal(9, "Blues")[-(1:2)])), cex = 2) } pairs.panel.smoothScatter <- function(x, y, i = NULL, j = NULL, ...) { vi <- x > 0 | y > 0 smoothScatter(x[vi], y[vi], add = TRUE, ...) } # a slight modification of pairs that passes i/j indices to the panel methods pairs.extended <- function (x, labels, panel = points, ..., lower.panel = panel, upper.panel = panel, diag.panel = NULL, text.panel = textPanel, label.pos = 0.5 + has.diag/3, cex.labels = NULL, font.labels = 1, row1attop = TRUE, gap = 1) { textPanel <- function(x = 0.5, y = 0.5, txt, cex, font) { text(x, y, txt, cex = cex, font = font) } localAxis <- function(side, x, y, xpd, bg, col = NULL, main, oma, ...) { ## Explicitly ignore any color argument passed in as ## it was most likely meant for the data points and ## not for the axis. if(side %%2 == 1) { Axis(x, side = side, xpd = NA, ...) } else { Axis(y, side = side, xpd = NA, ...) } } localPlot <- function(..., main, oma, font.main, cex.main) { plot(...) } localLowerPanel <- function(..., main, oma, font.main, cex.main) { lower.panel(...) } localUpperPanel <- function(..., main, oma, font.main, cex.main) { upper.panel(...) } localDiagPanel <- function(..., main, oma, font.main, cex.main) { diag.panel(...) } dots <- list(...) nmdots <- names(dots) if (!is.matrix(x)) { x <- as.data.frame(x) for(i in seq_along(names(x))) { if(is.factor(x[[i]]) || is.logical(x[[i]])) { x[[i]] <- as.numeric(x[[i]]) } if(!is.numeric(unclass(x[[i]]))) { stop("non-numeric argument to 'pairs'") } } } else if(!is.numeric(x)) { stop("non-numeric argument to 'pairs'") } panel <- match.fun(panel) if((has.lower <- !is.null(lower.panel)) && !missing(lower.panel)) { lower.panel <- match.fun(lower.panel) } if((has.upper <- !is.null(upper.panel)) && !missing(upper.panel)) { upper.panel <- match.fun(upper.panel) } if((has.diag <- !is.null( diag.panel)) && !missing( diag.panel)) { diag.panel <- match.fun( diag.panel) } if(row1attop) { tmp <- lower.panel lower.panel <- upper.panel upper.panel <- tmp tmp <- has.lower has.lower <- has.upper has.upper <- tmp } nc <- ncol(x) if (nc < 2) stop("only one column in the argument to 'pairs'") has.labs <- TRUE if (missing(labels)) { labels <- colnames(x) if (is.null(labels)) { labels <- paste("var", 1L:nc) } } else if(is.null(labels)) { has.labs <- FALSE } oma <- if("oma" %in% nmdots) { dots$oma } else { NULL } main <- if("main" %in% nmdots) { dots$main } else { NULL } if (is.null(oma)) { oma <- c(4, 4, 4, 4) if (!is.null(main)) { oma[3L] <- 6 } } opar <- par(mfrow = c(nc, nc), mar = rep.int(gap/2, 4), oma = oma) on.exit(par(opar)) for (i in if(row1attop) 1L:nc else nc:1L) for (j in 1L:nc) { localPlot(x[, j], x[, i], xlab = "", ylab = "", axes = FALSE, type = "n", ...) if(i == j || (i < j && has.lower) || (i > j && has.upper) ) { box() if(i == 1 && (!(j %% 2) || !has.upper || !has.lower )) localAxis(1 + 2*row1attop, x[, j], x[, i], ...) if(i == nc && ( j %% 2 || !has.upper || !has.lower )) localAxis(3 - 2*row1attop, x[, j], x[, i], ...) if(j == 1 && (!(i %% 2) || !has.upper || !has.lower )) localAxis(2, x[, j], x[, i], ...) if(j == nc && ( i %% 2 || !has.upper || !has.lower )) localAxis(4, x[, j], x[, i], ...) mfg <- par("mfg") if(i == j) { if (has.diag) localDiagPanel(as.vector(x[, i]), i = i, ...) if (has.labs) { par(usr = c(0, 1, 0, 1)) if(is.null(cex.labels)) { l.wid <- strwidth(labels, "user") cex.labels <- max(0.8, min(2, .9 / max(l.wid))) } text.panel(0.5, label.pos, labels[i], cex = cex.labels, font = font.labels) } } else if(i < j) localLowerPanel(as.vector(x[, j]), as.vector(x[, i]), i = i, j = j, ...) else localUpperPanel(as.vector(x[, j]), as.vector(x[, i]), i = i, j = j, ...) if (any(par("mfg") != mfg)) stop("the 'panel' function made a new plot") } else { par(new = FALSE) } } if (!is.null(main)) { font.main <- if("font.main" %in% nmdots) { dots$font.main } else { par("font.main") } cex.main <- if("cex.main" %in% nmdots) { dots$cex.main } else { par("cex.main") } mtext(main, 3, 3, TRUE, 0.5, cex = cex.main, font = font.main) } invisible(NULL) } # given a set of pdfs (columns), calculate summary statistics (mle, 95% CI, Z-score deviations from 0) quick.distribution.summary <- function(s.bdiffp) { diffv <- as.numeric(colnames(s.bdiffp)) dq <- t(apply(s.bdiffp, 1, function(p) { mle <- which.max(p) p <- cumsum(p) return(diffv[c(lb = max(c(1, which(p<0.025))), mle, min(c(length(p), which(p > (1-0.025)))))]) }))/log10(2) colnames(dq) <- c("lb", "mle", "ub") cq <- rep(0, nrow(dq)) cq[dq[, 1] > 0] <- dq[dq[, 1] > 0, 1] cq[dq[, 3]<0] <- dq[dq[, 3]<0, 3] z <- get.ratio.posterior.Z.score(s.bdiffp) za <- sign(z)*qnorm(p.adjust(pnorm(abs(z), lower.tail = FALSE), method = "BH"), lower.tail = FALSE) data.frame(dq, "ce" = as.numeric(cq), "Z" = as.numeric(z), "cZ" = as.numeric(za)) } ####### ## INTERNAL PAGODA ROUTINES ####### # performs weighted centering of mat rows (mat - rowSums(mat*weights)/rowSums(weights)) # possibly accounting for batch effects (i.e. centering each batch separately weightedMatCenter <- function(mat, matw, batch = NULL) { if(is.null(batch)) { return(mat-rowSums(mat*matw)/rowSums(matw)) } else { cmat <- mat invisible(tapply(seq_len(ncol(mat)), as.factor(batch), function(ii) { cmat[, ii] <<- cmat[, ii, drop = FALSE] - rowSums(cmat[, ii, drop = FALSE]*matw[, ii, drop = FALSE])/rowSums(matw[, ii, drop = FALSE]) })) return(cmat) } } # per-experiment/per-gene weighted variance estimate # weight matrix should have the same dimensions as the data matrix weightedMatVar <- function(mat, matw, batch = NULL, center = TRUE, min.weight = 0, normalize.weights = TRUE) { # normalize weights #matw <- matw/rowSums(matw) #matw <- matw/rowSums(matw)*ncol(matw) if(center) { mat <- weightedMatCenter(mat, matw, batch) } #weightedMatVar.Rcpp(mat, matw) #return(rowSums(mat*mat*matw) / (1-rowSums(matw*matw))) #return(rowSums(mat*mat*matw)) #return(rowSums(mat*mat*matw) * rowSums(matw) /pmax(rowSums(matw)^2 - rowSums(matw*matw), rep(min.weight, nrow(matw)))) v<- rowSums(mat*mat*matw) if(normalize.weights) { v <- v/rowSums(matw) } v } # GEV t() function gev.t <- function(x, loc, scale, shape = rep(0, length(loc)), log = FALSE) { if(log) { pmin(0, ifelse(shape == 0, -(x-loc)/scale, (-1/shape)*log(pmax(0, 1+shape*(x-loc)/scale)))) } else { pmin(1, ifelse(shape == 0, exp(-(x-loc)/scale), ((pmax(0, 1+shape*(x-loc)/scale))^(-1/shape)))) } } # returns upper tail of GEV in log scale pgev.upper.log <- function(x, loc, scale, shape = rep(0, length(loc))) { tv <- gev.t(x, loc, scale, shape, log = TRUE) tv[tv > -5 & tv<0] <- log(-expm1(-exp(tv[tv > -5 & tv<0]))) tv } # BH P-value adjustment with a log option bh.adjust <- function(x, log = FALSE) { nai <- which(!is.na(x)) ox <- x x<-x[nai] id <- order(x, decreasing = FALSE) if(log) { q <- x[id] + log(length(x)/seq_along(x)) } else { q <- x[id]*length(x)/seq_along(x) } a <- rev(cummin(rev(q)))[order(id)] ox[nai]<-a ox } pathway.pc.correlation.distance <- function(pcc, xv, n.cores = 10, target.ndf = NULL) { # all relevant gene names rotn <- unique(unlist(lapply(pcc[gsub("^#PC\\d+# ", "", rownames(xv))], function(d) rownames(d$xp$rotation)))) # prepare an ordered (in terms of genes) and centered version of each component pl <- papply(rownames(xv), function(nam) { pnam <- gsub("^#PC\\d+# ", "", nam) pn <- as.integer(gsub("^#PC(\\d+)# .*", "\\1", nam)) rt <- pcc[[pnam]]$xp$rotation[, pn] # order names/values according to increasing name match index mi <- match(names(rt), rotn) mo <- order(mi, decreasing = FALSE) rt <- as.numeric(rt)-mean(rt) return(list(i = mi[mo], v = rt[mo])) }, n.cores = n.cores) x <- .Call("plSemicompleteCor2", pl, PACKAGE = "scde") if(!is.null(target.ndf)) { r <- x$r[upper.tri(x$r)] n <- x$n[upper.tri(x$n)] suppressWarnings(tv <- r*sqrt((n-2)/(1-r^2))) z <- pt(tv, df = n-2, lower.tail = FALSE, log.p = TRUE) nr <- qt(z, df = target.ndf-2, lower.tail = FALSE, log.p = TRUE) nr <- nr/sqrt(target.ndf-2+nr^2) nr[is.nan(nr)] <- r[is.nan(nr)] cr <- x$r cr[upper.tri(cr)] <- nr cr[lower.tri(cr)] <- t(cr)[lower.tri(cr)] } else { cr <- x$r } rownames(cr) <- colnames(cr) <- rownames(xv) d <- stats::as.dist(1-abs(cr)) d[d<0] <- 0 d } collapse.aspect.clusters <- function(d, dw, ct, scale = TRUE, pick.top = FALSE) { xvm <- do.call(rbind, tapply(seq_len(nrow(d)), factor(ct, levels = sort(unique(ct))), function(ii) { if(length(ii) == 1) return(d[ii, ]) if(pick.top) { return(d[ii[which.max(apply(d[ii, ], 1, var))], ]) } xp <- pcaMethods::pca(t(d[ii, ]), nPcs = 1, center = TRUE, scale = "none") xv <- pcaMethods::scores(xp)[, 1] if(sum(abs(diff(xv))) > 0 && cor(xv, colMeans(d[ii, ]*abs(pcaMethods::loadings(xp)[, 1])))<0) { xv <- -1*xv } #set scale at top pathway? if(sum(abs(diff(xv))) > 0) { if(scale) { xv <- xv*sqrt(max(apply(d[ii, ], 1, var)))/sqrt(var(xv)) } if(sum(abs(xv)) == 0) { xv <- abs(rnorm(length(xv), sd = 1e-6)) } } else { xv <- abs(rnorm(length(xv), sd = 1e-6)) } #xv <- xv/sqrt(length(ii)) xv })) rownames(xvm) <- unlist(tapply(seq_len(nrow(d)), factor(ct, levels = sort(unique(ct))), function(ii) { if(length(ii) == 1) return(rownames(d)[ii]) return(rownames(d)[ii[which.max(apply(d[ii, ], 1, var))]]) })) xvmw <- do.call(rbind, tapply(seq_len(nrow(d)), factor(ct, levels = sort(unique(ct))), function(ii) { w <- colSums(dw[ii, , drop = FALSE]*apply(d[ii, , drop = FALSE], 1, sd)) w <- w/sum(w) })) return(list(d = xvm, w = xvmw)) } # convert R color to a web hex representation col2hex <- function(col) { unlist(lapply(col, function(c) { c <- col2rgb(c) sprintf("#%02X%02X%02X", c[1], c[2], c[3]) })) } my.heatmap2 <- function (x, Rowv = NULL, Colv = if(symm)"Rowv" else NULL, distfun = dist, hclustfun = hclust, reorderfun = function(d, w) reorder(d, w), add.expr, symm = FALSE, revC = identical(Colv, "Rowv"), scale = c("none", "row", "column"), na.rm = TRUE, margins = c(5, 5), internal.margin = 0.5, ColSideColors, RowSideColors, cexRow = 0.2 + 1/log10(nr), cexCol = 0.2 + 1/log10(nc), labRow = NULL, labCol = NULL, xlab = NULL, ylab = NULL, keep.dendro = FALSE, grid = FALSE, grid.col = 1, grid.lwd = 1, verbose = getOption("verbose"), Colv.vsize = 0.15, Rowv.hsize = 0.15, ColSideColors.unit.vsize = "0.08", RowSideColors.hsize = 0.03, lasCol = 2, lasRow = 2, respect = FALSE, box = FALSE, zlim = NULL, ...) { scale <- if(symm && missing(scale)) "none" else match.arg(scale) if(length(di <- dim(x)) != 2 || !is.numeric(x)) stop("'x' must be a numeric matrix") nr <- di[1] nc <- di[2] if(nr < 1 || nc <= 1) stop("'x' must have at least one row and 2 columns") if(!is.numeric(margins) || length(margins) != 2) stop("'margins' must be a numeric vector of length 2") if(is.null(zlim)) { zlim <- range(x[is.finite(x)]) } else { x[x zlim[2]] <- zlim[2] } doRdend <- !identical(Rowv, NA) doCdend <- !identical(Colv, NA) ## by default order by row/col means if(is.null(Rowv)) { Rowv <- rowMeans(x, na.rm = na.rm) } if(is.null(Colv)) { Colv <- colMeans(x, na.rm = na.rm) } ## get the dendrograms and reordering indices if(doRdend) { if(inherits(Rowv, "dendrogram")) ddr <- Rowv else { hcr <- hclustfun(distfun(x)) ddr <- as.dendrogram(hcr) if(!is.logical(Rowv) || Rowv) ddr <- reorderfun(ddr, Rowv) } if(nr != length(rowInd <- order.dendrogram(ddr))) stop("row dendrogram ordering gave index of wrong length") } else rowInd <- 1:nr if(doCdend) { if(inherits(Colv, "dendrogram")) ddc <- Colv else if(identical(Colv, "Rowv")) { if(nr != nc) stop('Colv = "Rowv" but nrow(x) != ncol(x)') ddc <- ddr } else { hcc <- hclustfun(distfun(if(symm)x else t(x))) ddc <- as.dendrogram(hcc) if(!is.logical(Colv) || Colv) ddc <- reorderfun(ddc, Colv) } if(nc != length(colInd <- order.dendrogram(ddc))) stop("column dendrogram ordering gave index of wrong length") } else colInd <- 1:nc ## reorder x x <- x[rowInd, colInd, drop = FALSE] labRow <- if(is.null(labRow)) if(is.null(rownames(x))) (1:nr)[rowInd] else rownames(x) else labRow[rowInd] labCol <- if(is.null(labCol)) if(is.null(colnames(x))) (1:nc)[colInd] else colnames(x) else labCol[colInd] if(scale == "row") { x <- sweep(x, 1, rowMeans(x, na.rm = na.rm)) sx <- apply(x, 1, sd, na.rm = na.rm) x <- sweep(x, 1, sx, "/") } else if(scale == "column") { x <- sweep(x, 2, colMeans(x, na.rm = na.rm)) sx <- apply(x, 2, sd, na.rm = na.rm) x <- sweep(x, 2, sx, "/") } ## Calculate the plot layout ds <- dev.size(units = "cm") lmat <- rbind(c(NA, 3), 2:1) if(doRdend) { lwid <- c(if(is.character(Rowv.hsize)) Rowv.hsize else lcm(Rowv.hsize*ds[1]), 1) } else { lmat[2, 1] <- NA lmat[1, 2] <- 2 lwid <- c(0, 1) } if(doCdend) { lhei <- c(if(is.character(Colv.vsize)) Colv.vsize else lcm(Colv.vsize*ds[2]), 1) } else { lmat[1, 2] <- NA lhei <- c(0, 1) } #lwid <- c(if(doRdend) lcm(Rowv.hsize*ds[1]) else "0.5 cm", 1) #lhei <- c((if(doCdend) lcm(Colv.vsize*ds[2]) else "0.5 cm"), 1) if(!missing(ColSideColors) && !is.null(ColSideColors)) { ## add middle row to layout if(is.matrix(ColSideColors)) { if(ncol(ColSideColors) != nc) stop("'ColSideColors' matrix must have the same number of columns as length ncol(x)") if(is.character(ColSideColors.unit.vsize)) { ww <- paste(as.numeric(gsub("(\\d+\\.?\\d*)(.*)", "\\1", ColSideColors.unit.vsize, perl = TRUE))*nrow(ColSideColors), gsub("(\\d+\\.?\\d*)(.*)", "\\2", ColSideColors.unit.vsize, perl = TRUE), sep = "") } else { ww <- lcm(ColSideColors.unit.vsize*ds[2]*nrow(ColSideColors)) } lmat <- rbind(lmat[1, ]+1, c(NA, 1), lmat[2, ]+1) lhei <- c(lhei[1], ww, lhei[2]) } else { if(!is.character(ColSideColors) || length(ColSideColors) != nc) stop("'ColSideColors' must be a character vector of length ncol(x)") if(is.character(ColSideColors.unit.vsize)) { ww <- paste(as.numeric(gsub("(\\d+\\.?\\d*)(.*)", "\\1", ColSideColors.unit.vsize, perl = TRUE)), gsub("(\\d+\\.?\\d*)(.*)", "\\2", ColSideColors.unit.vsize, perl = TRUE), sep = "") } else { ww <- lcm(ColSideColors.unit.vsize*ds[2]) } lmat <- rbind(lmat[1, ]+1, c(NA, 1), lmat[2, ]+1) lhei <- c(lhei[1], ww, lhei[2]) } } if(!missing(RowSideColors) && !is.null(RowSideColors)) { ## add middle column to layout if(!is.character(RowSideColors) || length(RowSideColors) != nr) stop("'RowSideColors' must be a character vector of length nrow(x)") lmat <- cbind(lmat[, 1]+1, c(rep(NA, nrow(lmat)-1), 1), lmat[, 2]+1) lwid <- c(lwid[1], if(is.character(RowSideColors.hsize)) RowSideColors.hsize else lcm(RowSideColors.hsize*ds[1]), lwid[2]) } lmat[is.na(lmat)] <- 0 if(verbose) { cat("layout: widths = ", lwid, ", heights = ", lhei, " lmat = \n") print(lmat) } ## Graphics `output' ----------------------- op <- par(no.readonly = TRUE) #on.exit(par(op)) layout(lmat, widths = lwid, heights = lhei, respect = respect) ## draw the side bars if(!missing(RowSideColors) && !is.null(RowSideColors)) { par(mar = c(margins[1], 0, 0, internal.margin)) image(rbind(1:nr), col = RowSideColors[rowInd], axes = FALSE) if(box) { box() } } if(!missing(ColSideColors) && !is.null(ColSideColors)) { par(mar = c(internal.margin, 0, 0, margins[2])) if(is.matrix(ColSideColors)) { image(t(matrix(1:length(ColSideColors), byrow = TRUE, nrow = nrow(ColSideColors), ncol = ncol(ColSideColors))), col = as.vector(t(ColSideColors[, colInd, drop = FALSE])), axes = FALSE) if(box) { box() } } else { image(cbind(1:nc), col = ColSideColors[colInd], axes = FALSE) if(box) { box() } } } ## draw the main carpet par(mar = c(margins[1], 0, 0, margins[2])) if(!symm || scale != "none") x <- t(x) if(revC) { # x columns reversed iy <- nr:1 ddr <- rev(ddr) x <- x[, iy, drop = FALSE] } else iy <- 1:nr image(1:nc, 1:nr, x, xlim = 0.5+ c(0, nc), ylim = 0.5+ c(0, nr), axes = FALSE, xlab = "", ylab = "", zlim = zlim, ...) if(box) { box() } axis(1, 1:nc, labels = labCol, las = lasCol, line = -0.5, tick = 0, cex.axis = cexCol) if(!is.null(xlab)) mtext(xlab, side = 1, line = margins[1] - 1.25) axis(4, iy, labels = labRow, las = lasRow, line = -0.5, tick = 0, cex.axis = cexRow) if(!is.null(ylab)) mtext(ylab, side = 4, line = margins[2] - 1.25, las = lasRow) if (!missing(add.expr)) eval(substitute(add.expr)) if(grid) { abline(v = c(1:nc)-0.5, col = grid.col, lwd = grid.lwd) abline(h = c(1:nr)-0.5, col = grid.col, lwd = grid.lwd) box(col = grid.col, lwd = grid.lwd) } ## the two dendrograms : if(doRdend) { par(mar = c(margins[1], internal.margin, 0, 0)) plot(ddr, horiz = TRUE, axes = FALSE, yaxs = "i", leaflab = "none", xaxs = "i") } if(doCdend) { par(mar = c(0, 0, internal.margin, margins[2])) plot(ddc, axes = FALSE, xaxs = "i", leaflab = "none", yaxs = "i") } invisible(list(rowInd = rowInd, colInd = colInd, Rowv = if(keep.dendro && doRdend) ddr, Colv = if(keep.dendro && doCdend) ddc )) } # rook class for browsing differential expression results ViewDiff <- setRefClass( 'ViewDiff', fields = c('gt', 'models', 'counts', 'prior', 'groups', 'batch', 'geneLookupURL'), methods = list( initialize = function(results, models, counts, prior, groups = NULL, batch = NULL, geneLookupURL = NULL) { if(!is.null(results$results)) { gt <<- results$results } else { gt <<- results } # add raw names if this wasn't a batch-corrected sample if("mle" %in% colnames(gt)) { colnames(gt) <<- paste(colnames(gt), "raw", sep = "_") } if(!is.null(results$batch.adjusted)) { df <- results$batch.adjusted colnames(df) <- paste(colnames(df), "cor", sep = "_") gt <<- cbind(gt, df) } if(!is.null(results$batch.effect)) { df <- results$batch.effect colnames(df) <- paste(colnames(df), "bat", sep = "_") gt <<- cbind(gt, df) } colnames(gt) <<- tolower(colnames(gt)) # append expression levels to the results if(!is.null(results$joint.posteriors)) { gt$lev1 <<- log10(as.numeric(colnames(results$joint.posteriors[[1]]))[max.col(results$joint.posteriors[[1]])]+1) gt$lev2 <<- log10(as.numeric(colnames(results$joint.posteriors[[2]]))[max.col(results$joint.posteriors[[2]])]+1) } gt$gene <<- rownames(gt) gt <<- data.frame(gt) # guess gene lookup for common cases if(is.null(geneLookupURL)) { # human if( any(grepl("ENSG\\d+", gt$gene[1])) || any(c("CCLU1", "C22orf45") %in% gt$gene)) { geneLookupURL <<- "http://useast.ensembl.org/Homo_sapiens/Gene/Summary?g = {0}" } else if( any(grepl("ENSMUSG\\d+", gt$gene[1]))) { geneLookupURL <<- "http://useast.ensembl.org/Mus_musculus/Gene/Summary?g = {0}" } else if( any(c("Foxp2", "Sept1", "Lrrc34") %in% gt$gene)) { # mouse MGI geneLookupURL <<- "http://www.informatics.jax.org/searchtool/Search.do?query = {0}" } else if( any(grepl("FBgn\\d+", gt$gene[1])) || any(c("CG3680", "CG8290") %in% gt$gene)) { # flybase geneLookupURL <<- "http://flybase.org/cgi-bin/uniq.html?db = fbgn&GeneSearch = {0}&context = {1}&species = Dmel&cs = yes&caller = genejump" } else { # default, forward to ensemble search geneLookupURL <<- "http://useast.ensembl.org/Multi/Search/Results?q = {0}site = ensembl" } } else { geneLookupURL <<- geneLookupURL } gt <<- gt[gt$z_raw != "NA", ] gt <<- gt[!is.na(gt$z_raw), ] models <<- models counts <<- counts prior <<- prior if(is.null(groups)) { # recover groups from models groups <<- as.factor(attr(models, "groups")) if(is.null(groups)) stop("ERROR: groups factor is not provided, and models structure is lacking groups attribute") names(groups) <<- rownames(models) } else { groups <<- groups } if(length(levels(groups)) != 2) { stop(paste("ERROR: wrong number of levels in the grouping factor (", paste(levels(groups), collapse = " "), "), but must be two.", sep = "")) } batch <<- batch callSuper() }, call = function(env){ path <- env[['PATH_INFO']] req <- Request$new(env) res <- Response$new() switch(path, # INDEX '/index.html' = { body <- paste(' SCDE: ', paste(levels(groups), collapse = " vs. "), ' The resulting embedding can be passed into the PAGODA app to visualize individual aspects, genes, etc. within this embedding: ``` r app <- make.pagoda.app(tamr2, tam, varinfo, go.env, pwpca, clpca, col.cols = col.cols, cell.clustering = hc, title = "NPCs", embedding = tSNE.pagoda$Y) # show app in the browser (port 1468) show.app(app, "pollen", browse = TRUE, port = 1468) ``` Controlling for undesired aspects of heterogeneity -------------------------------------------------- Depending on the biological setting, certain dominant aspects of transcriptional heterogeneity may not be of interest. To explicitly control for these aspects of heterogeneity that are not of interest, we will use `pagoda.subtract.aspect` method that we've previously used to control for residual patterns associated with sequencing depth differences. Here, we illustrate how to control for the mitotic cell cycle pattern ( nuclear division and mitotic nuclear division) which showed up as one of the four significant aspects in the analysis above. ``` r # get cell cycle signature and view the top genes cc.pattern <- pagoda.show.pathways(c("GO:0000280", "GO:0007067"), varinfo, go.env, show.cell.dendrogram = TRUE, cell.clustering = hc, showRowLabels = TRUE) # subtract the pattern varinfo.cc <- pagoda.subtract.aspect(varinfo, cc.pattern) ``` ![](figures/pagoda-controlForCellCycle-1.png) Now we can go through the same analysis as shown above, starting with the `pagoda.pathway.wPCA()` call, using `varinfo.cc` instead of `varinfo`, which will control for the cell cycle heterogeneity between the cells. ================================================ FILE: license.txt ================================================ Copyright (c) 2015. All Rights Reserved. Created by Jean Fan and Peter Kharchenko. Harvard Medical School, Department of Biomedical Informatics (Regents). Permission to use, copy, modify, and distribute this software and its documentation for educational and research not-for-profit purposes, without fee and without a signed licensing agreement, is hereby granted, provided that attribution to authors and Regents, as appeared in the paragraph above appears in all copies, modifications, and distributions. Contact The authors for commercial licensing opportunities. IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. ================================================ FILE: man/ViewPagodaApp-class.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \docType{class} \name{ViewPagodaApp-class} \alias{ViewPagodaApp} \alias{ViewPagodaApp-class} \title{A Reference Class to represent the PAGODA application} \description{ This ROOK application class enables communication with the client-side ExtJS framework and Inchlib HTML5 canvas libraries to create the graphical user interface for PAGODA Refer to the code in \code{\link{make.pagoda.app}} for usage example } \section{Fields}{ \describe{ \item{\code{results}}{Output of the pathway clustering and redundancy reduction} \item{\code{genes}}{List of genes to display in the Detailed clustering panel} \item{\code{mat}}{Matrix of posterior mode count estimates} \item{\code{matw}}{Matrix of weights associated with each estimate in \code{mat}} \item{\code{goenv}}{Gene set list as an environment} \item{\code{renv}}{Global environment} \item{\code{name}}{Name of the application page; for display as the page title} \item{\code{trim}}{Trim quantity used for Winsorization for visualization} \item{\code{batch}}{Any batch or other known confounders to be included in the visualization as a column color track} }} ================================================ FILE: man/bwpca.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{bwpca} \alias{bwpca} \title{Determine principal components of a matrix using per-observation/per-variable weights} \usage{ bwpca(mat, matw = NULL, npcs = 2, nstarts = 1, smooth = 0, em.tol = 1e-06, em.maxiter = 25, seed = 1, center = TRUE, n.shuffles = 0) } \arguments{ \item{mat}{matrix of variables (columns) and observations (rows)} \item{matw}{corresponding weights} \item{npcs}{number of principal components to extract} \item{nstarts}{number of random starts to use} \item{smooth}{smoothing span} \item{em.tol}{desired EM algorithm tolerance} \item{em.maxiter}{maximum number of EM iterations} \item{seed}{random seed} \item{center}{whether mat should be centered (weighted centering)} \item{n.shuffles}{optional number of per-observation randomizations that should be performed in addition to the main calculations to determine the lambda1 (PC1 eigenvalue) magnitude under such randomizations (returned in $randvar)} } \value{ a list containing eigenvector matrix ($rotation), projections ($scores), variance (weighted) explained by each component ($var), total (weighted) variance of the dataset ($totalvar) } \description{ Implements a weighted PCA } \examples{ set.seed(0) mat <- matrix( c(rnorm(5*10,mean=0,sd=1), rnorm(5*10,mean=5,sd=1)), 10, 10) # random matrix base.pca <- bwpca(mat) # non-weighted pca, equal weights set automatically matw <- matrix( c(rnorm(5*10,mean=0,sd=1), rnorm(5*10,mean=5,sd=1)), 10, 10) # random weight matrix matw <- abs(matw)/max(matw) base.pca.weighted <- bwpca(mat, matw) # weighted pca } ================================================ FILE: man/clean.counts.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{clean.counts} \alias{clean.counts} \title{Filter counts matrix} \usage{ clean.counts(counts, min.lib.size = 1800, min.reads = 10, min.detected = 5) } \arguments{ \item{counts}{read count matrix. The rows correspond to genes, columns correspond to individual cells} \item{min.lib.size}{Minimum number of genes detected in a cell. Cells with fewer genes will be removed (default: 1.8e3)} \item{min.reads}{Minimum number of reads per gene. Genes with fewer reads will be removed (default: 10)} \item{min.detected}{Minimum number of cells a gene must be seen in. Genes not seen in a sufficient number of cells will be removed (default: 5)} } \value{ a filtered read count matrix } \description{ Filter counts matrix based on gene and cell requirements } \examples{ data(pollen) dim(pollen) cd <- clean.counts(pollen) dim(cd) } ================================================ FILE: man/clean.gos.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{clean.gos} \alias{clean.gos} \title{Filter GOs list} \usage{ clean.gos(go.env, min.size = 5, max.size = 5000, annot = FALSE) } \arguments{ \item{go.env}{GO or gene set list} \item{min.size}{Minimum size for number of genes in a gene set (default: 5)} \item{max.size}{Maximum size for number of genes in a gene set (default: 5000)} \item{annot}{Whether to append GO annotations for easier interpretation (default: FALSE)} } \value{ a filtered GO list } \description{ Filter GOs list and append GO names when appropriate } \examples{ \donttest{ # 10 sample GOs library(org.Hs.eg.db) go.env <- mget(ls(org.Hs.egGO2ALLEGS)[1:10], org.Hs.egGO2ALLEGS) # Filter this list and append names for easier interpretation go.env <- clean.gos(go.env) } } ================================================ FILE: man/es.mef.small.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \docType{data} \name{es.mef.small} \alias{es.mef.small} \title{Sample data} \description{ A subset of Saiful et al. 2011 dataset containing first 20 ES and 20 MEF cells. } \references{ \url{http://www.ncbi.nlm.nih.gov/pubmed/21543516} } ================================================ FILE: man/knn.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \docType{data} \name{knn} \alias{knn} \title{Sample error model} \description{ SCDE error model generated from the Pollen et al. 2014 dataset. } \references{ \url{www.ncbi.nlm.nih.gov/pubmed/25086649} } ================================================ FILE: man/knn.error.models.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{knn.error.models} \alias{knn.error.models} \title{Build error models for heterogeneous cell populations, based on K-nearest neighbor cells.} \usage{ knn.error.models(counts, groups = NULL, k = round(ncol(counts)/2), min.nonfailed = 5, min.count.threshold = 1, save.model.plots = TRUE, max.model.plots = 50, n.cores = parallel::detectCores(), min.size.entries = 2000, min.fpm = 0, cor.method = "pearson", verbose = 0, fpm.estimate.trim = 0.25, linear.fit = TRUE, local.theta.fit = linear.fit, theta.fit.range = c(0.01, 100), alpha.weight.power = 1/2) } \arguments{ \item{counts}{count matrix (integer matrix, rows- genes, columns- cells)} \item{groups}{optional groups partitioning known subpopulations} \item{k}{number of nearest neighbor cells to use during fitting. If k is set sufficiently high, all of the cells within a given group will be used.} \item{min.nonfailed}{minimum number of non-failed measurements (within the k nearest neighbor cells) required for a gene to be taken into account during error fitting procedure} \item{min.count.threshold}{minimum number of reads required for a measurement to be considered non-failed} \item{save.model.plots}{whether model plots should be saved (file names are (group).models.pdf, or cell.models.pdf if no group was supplied)} \item{max.model.plots}{maximum number of models to save plots for (saves time when there are too many cells)} \item{n.cores}{number of cores to use through the calculations} \item{min.size.entries}{minimum number of genes to use for model fitting} \item{min.fpm}{optional parameter to restrict model fitting to genes with group-average expression magnitude above a given value} \item{cor.method}{correlation measure to be used in determining k nearest cells} \item{verbose}{level of verbosity} \item{fpm.estimate.trim}{trim fraction to be used in estimating group-average gene expression magnitude for model fitting (0.5 would be median, 0 would turn off trimming)} \item{linear.fit}{whether newer linear model fit with zero intercept should be used (T), or the log-fit model published originally (F)} \item{local.theta.fit}{whether local theta fitting should be used (only available for the linear fit models)} \item{theta.fit.range}{allowed range of the theta values} \item{alpha.weight.power}{1/theta weight power used in fitting theta dependency on the expression magnitude} } \value{ a data frame with parameters of the fit error models (rows- cells, columns- fitted parameters) } \description{ Builds cell-specific error models assuming that there are multiple subpopulations present among the measured cells. The models for each cell are based on average expression estimates obtained from K closest cells within a given group (if groups = NULL, then within the entire set of measured cells). The method implements fitting of both the original log-fit models (when linear.fit = FALSE), or newer linear-fit models (linear.fit = TRUE, default) with locally fit overdispersion coefficient (local.theta.fit = TRUE, default). } \examples{ data(pollen) cd <- clean.counts(pollen) \donttest{ knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) } } ================================================ FILE: man/make.pagoda.app.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{make.pagoda.app} \alias{make.pagoda.app} \title{Make the PAGODA app} \usage{ make.pagoda.app(tamr, tam, varinfo, env, pwpca, clpca = NULL, col.cols = NULL, cell.clustering = NULL, row.clustering = NULL, title = "pathway clustering", zlim = c(-1, 1) * quantile(tamr$xv, p = 0.95)) } \arguments{ \item{tamr}{Combined pathways that show similar expression patterns. Output of \code{\link{pagoda.reduce.redundancy}}} \item{tam}{Combined pathways that are driven by the same gene sets. Output of \code{\link{pagoda.reduce.loading.redundancy}}} \item{varinfo}{Variance information. Output of \code{\link{pagoda.varnorm}}} \item{env}{Gene sets as an environment variable.} \item{pwpca}{Weighted PC magnitudes for each gene set provided in the \code{env}. Output of \code{\link{pagoda.pathway.wPCA}}} \item{clpca}{Weighted PC magnitudes for de novo gene sets identified by clustering on expression. Output of \code{\link{pagoda.gene.clusters}}} \item{col.cols}{Matrix of column colors. Useful for visualizing cell annotations such as batch labels. Default NULL.} \item{cell.clustering}{Dendrogram of cell clustering. Output of \code{\link{pagoda.cluster.cells} } . Default NULL.} \item{row.clustering}{Dendrogram of combined pathways clustering. Default NULL.} \item{title}{Title text to be used in the browser label for the app. Default, set as 'pathway clustering'} \item{zlim}{Range of the normalized gene expression levels, inputted as a list: c(lower_bound, upper_bound). Values outside this range will be Winsorized. Useful for increasing the contrast of the heatmap visualizations. Default, set to the 5th and 95th percentiles.} } \value{ PAGODA app } \description{ Create an interactive user interface to explore output of PAGODA. } ================================================ FILE: man/o.ifm.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \docType{data} \name{o.ifm} \alias{o.ifm} \title{Sample error model} \description{ SCDE error model generated from a subset of Saiful et al. 2011 dataset containing first 20 ES and 20 MEF cells. } \references{ \url{http://www.ncbi.nlm.nih.gov/pubmed/21543516} } ================================================ FILE: man/pagoda.cluster.cells.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{pagoda.cluster.cells} \alias{pagoda.cluster.cells} \title{Determine optimal cell clustering based on the genes driving the significant aspects} \usage{ pagoda.cluster.cells(tam, varinfo, method = "ward.D", include.aspects = FALSE, verbose = 0, return.details = FALSE) } \arguments{ \item{tam}{result of pagoda.top.aspects() call} \item{varinfo}{result of pagoda.varnorm() call} \item{method}{clustering method ('ward.D' by default)} \item{include.aspects}{whether the aspect patterns themselves should be included alongside with the individual genes in calculating cell distance} \item{verbose}{0 or 1 depending on level of desired verbosity} \item{return.details}{Boolean of whether to return just the hclust result or a list containing the hclust result plus the distance matrix and gene values} } \value{ hclust result } \description{ Determines cell clustering (hclust result) based on a weighted correlation of genes underlying the top aspects of transcriptional heterogeneity. Branch orientation is optimized if 'cba' package is installed. } \examples{ data(pollen) cd <- clean.counts(pollen) \donttest{ knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) tam <- pagoda.top.aspects(pwpca, return.table = TRUE, plot=FALSE, z.score=1.96) # top aspects based on GO only hc <- pagoda.cluster.cells(tam, varinfo) plot(hc) } } ================================================ FILE: man/pagoda.effective.cells.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{pagoda.effective.cells} \alias{pagoda.effective.cells} \title{Estimate effective number of cells based on lambda1 of random gene sets} \usage{ pagoda.effective.cells(pwpca, start = NULL) } \arguments{ \item{pwpca}{result of the pagoda.pathway.wPCA() call with n.randomizations > 1} \item{start}{optional starting value for the optimization (if the NLS breaks, trying high starting values usually fixed the local gradient problem)} } \value{ effective number of cells } \description{ Examines the dependency between the amount of variance explained by the first principal component of a gene set and the number of genes in a gene set to determine the effective number of cells for the Tracy-Widom distribution } \examples{ data(pollen) cd <- clean.counts(pollen) \donttest{ knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) pagoda.effective.cells(pwpca) } } ================================================ FILE: man/pagoda.gene.clusters.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{pagoda.gene.clusters} \alias{pagoda.gene.clusters} \title{Determine de-novo gene clusters and associated overdispersion info} \usage{ pagoda.gene.clusters(varinfo, trim = 3.1/ncol(varinfo$mat), n.clusters = 150, n.samples = 60, cor.method = "p", n.internal.shuffles = 0, n.starts = 10, n.cores = detectCores(), verbose = 0, plot = FALSE, show.random = FALSE, n.components = 1, method = "ward.D", secondary.correlation = FALSE, n.cells = ncol(varinfo$mat), old.results = NULL) } \arguments{ \item{varinfo}{varinfo adjusted variance info from pagoda.varinfo() (or pagoda.subtract.aspect())} \item{trim}{additional Winsorization trim value to be used in determining clusters (to remove clusters that group outliers occurring in a given cell). Use higher values (5-15) if the resulting clusters group outlier patterns} \item{n.clusters}{number of clusters to be determined (recommended range is 100-200)} \item{n.samples}{number of randomly generated matrix samples to test the background distribution of lambda1 on} \item{cor.method}{correlation method ("pearson", "spearman") to be used as a distance measure for clustering} \item{n.internal.shuffles}{number of internal shuffles to perform (only if interested in set coherence, which is quite high for clusters by definition, disabled by default; set to 10-30 shuffles to estimate)} \item{n.starts}{number of wPCA EM algorithm starts at each iteration} \item{n.cores}{number of cores to use} \item{verbose}{verbosity level} \item{plot}{whether a plot showing distribution of random lambda1 values should be shown (along with the extreme value distribution fit)} \item{show.random}{whether the empirical random gene set values should be shown in addition to the Tracy-Widom analytical approximation} \item{n.components}{number of PC to calculate (can be increased if the number of clusters is small and some contain strong secondary patterns - rarely the case)} \item{method}{clustering method to be used in determining gene clusters} \item{secondary.correlation}{whether clustering should be performed on the correlation of the correlation matrix instead} \item{n.cells}{number of cells to use for the randomly generated cluster lambda1 model} \item{old.results}{optionally, pass old results just to plot the model without recalculating the stats} } \value{ a list containing the following fields: \itemize{ \item{clusters} {a list of genes in each cluster values} \item{xf} { extreme value distribution fit for the standardized lambda1 of a randomly generated pattern} \item{tci} { index of a top cluster in each random iteration} \item{cl.goc} {weighted PCA info for each real gene cluster} \item{varm} {standardized lambda1 values for each randomly generated matrix cluster} \item{clvlm} {a linear model describing dependency of the cluster lambda1 on a Tracy-Widom lambda1 expectation} } } \description{ Determine de-novo gene clusters, their weighted PCA lambda1 values, and random matrix expectation. } \examples{ data(pollen) cd <- clean.counts(pollen) \donttest{ knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) clpca <- pagoda.gene.clusters(varinfo, trim=7.1/ncol(varinfo$mat), n.clusters=150, n.cores=10, plot=FALSE) } } ================================================ FILE: man/pagoda.pathway.wPCA.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{pagoda.pathway.wPCA} \alias{pagoda.pathway.wPCA} \title{Run weighted PCA analysis on pre-annotated gene sets} \usage{ pagoda.pathway.wPCA(varinfo, setenv, n.components = 2, n.cores = detectCores(), min.pathway.size = 10, max.pathway.size = 1000, n.randomizations = 10, n.internal.shuffles = 0, n.starts = 10, center = TRUE, batch.center = TRUE, proper.gene.names = NULL, verbose = 0) } \arguments{ \item{varinfo}{adjusted variance info from pagoda.varinfo() (or pagoda.subtract.aspect())} \item{setenv}{environment listing gene sets (contains variables with names corresponding to gene set name, and values being vectors of gene names within each gene set)} \item{n.components}{number of principal components to determine for each gene set} \item{n.cores}{number of cores to use} \item{min.pathway.size}{minimum number of observed genes that should be contained in a valid gene set} \item{max.pathway.size}{maximum number of observed genes in a valid gene set} \item{n.randomizations}{number of random gene sets (of the same size) to be evaluated in parallel with each gene set (can be kept at 5 or 10, but should be increased to 50-100 if the significance of pathway overdispersion will be determined relative to random gene set models)} \item{n.internal.shuffles}{number of internal (independent row shuffles) randomizations of expression data that should be evaluated for each gene set (needed only if one is interested in gene set coherence P values, disabled by default; set to 10-30 to estimate)} \item{n.starts}{number of random starts for the EM method in each evaluation} \item{center}{whether the expression matrix should be recentered} \item{batch.center}{whether batch-specific centering should be used} \item{proper.gene.names}{alternative vector of gene names (replacing rownames(varinfo$mat)) to be used in cases when the provided setenv uses different gene names} \item{verbose}{verbosity level} } \value{ a list of weighted PCA info for each valid gene set } \description{ For each valid gene set (having appropriate number of genes) in the provided environment (setenv), the method will run weighted PCA analysis, along with analogous analyses of random gene sets of the same size, or shuffled expression magnitudes for the same gene set. } \examples{ data(pollen) cd <- clean.counts(pollen) \donttest{ knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) # create go environment library(org.Hs.eg.db) # translate gene names to ids ids <- unlist(lapply(mget(rownames(cd), org.Hs.egALIAS2EG, ifnotfound = NA), function(x) x[1])) rids <- names(ids); names(rids) <- ids go.env <- lapply(mget(ls(org.Hs.egGO2ALLEGS), org.Hs.egGO2ALLEGS), function(x) as.character(na.omit(rids[x]))) # clean GOs go.env <- clean.gos(go.env) # convert to an environment go.env <- list2env(go.env) pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) } } ================================================ FILE: man/pagoda.reduce.loading.redundancy.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{pagoda.reduce.loading.redundancy} \alias{pagoda.reduce.loading.redundancy} \title{Collapse aspects driven by the same combinations of genes} \usage{ pagoda.reduce.loading.redundancy(tam, pwpca, clpca = NULL, plot = FALSE, cluster.method = "complete", distance.threshold = 0.01, corr.power = 4, n.cores = detectCores(), abs = TRUE, ...) } \arguments{ \item{tam}{output of pagoda.top.aspects()} \item{pwpca}{output of pagoda.pathway.wPCA()} \item{clpca}{output of pagoda.gene.clusters() (optional)} \item{plot}{whether to plot the resulting clustering} \item{cluster.method}{one of the standard clustering methods to be used (fastcluster::hclust is used if available or stats::hclust)} \item{distance.threshold}{similarity threshold for grouping interdependent aspects} \item{corr.power}{power to which the product of loading and score correlation is raised} \item{n.cores}{number of cores to use during processing} \item{abs}{Boolean of whether to use absolute correlation} \item{...}{additional arguments are passed to the pagoda.view.aspects() method during plotting} } \value{ a list structure analogous to that returned by pagoda.top.aspects(), but with addition of a $cnam element containing a list of aspects summarized by each row of the new (reduced) $xv and $xvw } \description{ Examines PC loading vectors underlying the identified aspects and clusters aspects based on a product of loading and score correlation (raised to corr.power). Clusters of aspects driven by the same genes are determined based on the distance.threshold and collapsed. } \examples{ data(pollen) cd <- clean.counts(pollen) \donttest{ knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) tam <- pagoda.top.aspects(pwpca, return.table = TRUE, plot=FALSE, z.score=1.96) # top aspects based on GO only tamr <- pagoda.reduce.loading.redundancy(tam, pwpca) } } ================================================ FILE: man/pagoda.reduce.redundancy.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{pagoda.reduce.redundancy} \alias{pagoda.reduce.redundancy} \title{Collapse aspects driven by similar patterns (i.e. separate the same sets of cells)} \usage{ pagoda.reduce.redundancy(tamr, distance.threshold = 0.2, cluster.method = "complete", distance = NULL, weighted.correlation = TRUE, plot = FALSE, top = Inf, trim = 0, abs = FALSE, ...) } \arguments{ \item{tamr}{output of pagoda.reduce.loading.redundancy()} \item{distance.threshold}{similarity threshold for grouping interdependent aspects} \item{cluster.method}{one of the standard clustering methods to be used (fastcluster::hclust is used if available or stats::hclust)} \item{distance}{distance matrix} \item{weighted.correlation}{Boolean of whether to use a weighted correlation in determining the similarity of patterns} \item{plot}{Boolean of whether to show plot} \item{top}{Restrict output to the top n aspects of heterogeneity} \item{trim}{Winsorization trim to use prior to determining the top aspects} \item{abs}{Boolean of whether to use absolute correlation} \item{...}{additional arguments are passed to the pagoda.view.aspects() method during plotting} } \value{ a list structure analogous to that returned by pagoda.top.aspects(), but with addition of a $cnam element containing a list of aspects summarized by each row of the new (reduced) $xv and $xvw } \description{ Examines PC loading vectors underlying the identified aspects and clusters aspects based on score correlation. Clusters of aspects driven by the same patterns are determined based on the distance.threshold. } \examples{ data(pollen) cd <- clean.counts(pollen) \donttest{ knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) tam <- pagoda.top.aspects(pwpca, return.table = TRUE, plot=FALSE, z.score=1.96) # top aspects based on GO only tamr <- pagoda.reduce.loading.redundancy(tam, pwpca) tamr2 <- pagoda.reduce.redundancy(tamr, distance.threshold = 0.9, plot = TRUE, labRow = NA, labCol = NA, box = TRUE, margins = c(0.5, 0.5), trim = 0) } } ================================================ FILE: man/pagoda.show.pathways.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{pagoda.show.pathways} \alias{pagoda.show.pathways} \title{View pathway or gene weighted PCA} \usage{ pagoda.show.pathways(pathways, varinfo, goenv = NULL, n.genes = 20, two.sided = FALSE, n.pc = rep(1, length(pathways)), colcols = NULL, zlim = NULL, showRowLabels = FALSE, cexCol = 1, cexRow = 1, nstarts = 10, cell.clustering = NULL, show.cell.dendrogram = TRUE, plot = TRUE, box = TRUE, trim = 0, return.details = FALSE, ...) } \arguments{ \item{pathways}{character vector of pathway or gene names} \item{varinfo}{output of pagoda.varnorm()} \item{goenv}{environment mapping pathways to genes} \item{n.genes}{number of genes to show} \item{two.sided}{whether the set of shown genes should be split among highest and lowest loading (T) or if genes with highest absolute loading (F) should be shown} \item{n.pc}{optional integer vector giving the number of principal component to show for each listed pathway} \item{colcols}{optional column color matrix} \item{zlim}{optional z color limit} \item{showRowLabels}{controls whether row labels are shown in the plot} \item{cexCol}{column label size (cex)} \item{cexRow}{row label size (cex)} \item{nstarts}{number of random starts for the wPCA} \item{cell.clustering}{cell clustering} \item{show.cell.dendrogram}{whether cell dendrogram should be shown} \item{plot}{whether the plot should be shown} \item{box}{whether to draw a box around the plotted matrix} \item{trim}{optional Winsorization trim that should be applied} \item{return.details}{whether the function should return the matrix as well as full PCA info instead of just PC1 vector} \item{...}{additional arguments are passed to the \code{c.view.pathways}} } \value{ cell scores along the first principal component of shown genes (returned as invisible) } \description{ Takes in a list of pathways (or a list of genes), runs weighted PCA, optionally showing the result. } ================================================ FILE: man/pagoda.subtract.aspect.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{pagoda.subtract.aspect} \alias{pagoda.subtract.aspect} \title{Control for a particular aspect of expression heterogeneity in a given population} \usage{ pagoda.subtract.aspect(varinfo, aspect, center = TRUE) } \arguments{ \item{varinfo}{normalized variance info (from pagoda.varnorm())} \item{aspect}{a vector giving a cell-to-cell variation pattern that should be controlled for (length should be corresponding to ncol(varinfo$mat))} \item{center}{whether the matrix should be re-centered following pattern subtraction} } \value{ a modified varinfo object with adjusted expression matrix (varinfo$mat) } \description{ Similar to subtracting n-th principal component, the current procedure determines (weighted) projection of the expression matrix onto a specified aspect (some pattern across cells, for instance sequencing depth, or PC corresponding to an undesired process such as ribosomal pathway variation) and subtracts it from the data so that it is controlled for in the subsequent weighted PCA analysis. } \examples{ data(pollen) cd <- clean.counts(pollen) \donttest{ knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) # create go environment library(org.Hs.eg.db) # translate gene names to ids ids <- unlist(lapply(mget(rownames(cd), org.Hs.egALIAS2EG, ifnotfound = NA), function(x) x[1])) rids <- names(ids); names(rids) <- ids go.env <- lapply(mget(ls(org.Hs.egGO2ALLEGS), org.Hs.egGO2ALLEGS), function(x) as.character(na.omit(rids[x]))) # clean GOs go.env <- clean.gos(go.env) # convert to an environment go.env <- list2env(go.env) # subtract the pattern cc.pattern <- pagoda.show.pathways(ls(go.env)[1:2], varinfo, go.env, show.cell.dendrogram = TRUE, showRowLabels = TRUE) # Look at pattern from 2 GO annotations varinfo.cc <- pagoda.subtract.aspect(varinfo, cc.pattern) } } ================================================ FILE: man/pagoda.top.aspects.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{pagoda.top.aspects} \alias{pagoda.top.aspects} \title{Score statistical significance of gene set and cluster overdispersion} \usage{ pagoda.top.aspects(pwpca, clpca = NULL, n.cells = NULL, z.score = qnorm(0.05/2, lower.tail = FALSE), return.table = FALSE, return.genes = FALSE, plot = FALSE, adjust.scores = TRUE, score.alpha = 0.05, use.oe.scale = FALSE, effective.cells.start = NULL) } \arguments{ \item{pwpca}{output of pagoda.pathway.wPCA()} \item{clpca}{output of pagoda.gene.clusters() (optional)} \item{n.cells}{effective number of cells (if not provided, will be determined using pagoda.effective.cells())} \item{z.score}{Z score to be used as a cutoff for statistically significant patterns (defaults to 0.05 P-value} \item{return.table}{whether a text table showing} \item{return.genes}{whether a set of genes driving significant aspects should be returned} \item{plot}{whether to plot the cv/n vs. dataset size scatter showing significance models} \item{adjust.scores}{whether the normalization of the aspect patterns should be based on the adjusted Z scores - qnorm(0.05/2, lower.tail = FALSE)} \item{score.alpha}{significance level of the confidence interval for determining upper/lower bounds} \item{use.oe.scale}{whether the variance of the returned aspect patterns should be normalized using observed/expected value instead of the default chi-squared derived variance corresponding to overdispersion Z score} \item{effective.cells.start}{starting value for the pagoda.effective.cells() call} } \value{ if return.table = FALSE and return.genes = FALSE (default) returns a list structure containing the following items: \itemize{ \item{xv} {a matrix of normalized aspect patterns (rows- significant aspects, columns- cells} \item{xvw} { corresponding weight matrix } \item{gw} { set of genes driving the significant aspects } \item{df} { text table with the significance testing results } } } \description{ Evaluates statistical significance of the gene set and cluster lambda1 values, returning either a text table of Z scores, etc, a structure containing normalized values of significant aspects, or a set of genes underlying the significant aspects. } \examples{ data(pollen) cd <- clean.counts(pollen) \donttest{ knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) tam <- pagoda.top.aspects(pwpca, return.table = TRUE, plot=FALSE, z.score=1.96) # top aspects based on GO only } } ================================================ FILE: man/pagoda.varnorm.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{pagoda.varnorm} \alias{pagoda.varnorm} \title{Normalize gene expression variance relative to transcriptome-wide expectations} \usage{ pagoda.varnorm(models, counts, batch = NULL, trim = 0, prior = NULL, fit.genes = NULL, plot = TRUE, minimize.underdispersion = FALSE, n.cores = detectCores(), n.randomizations = 100, weight.k = 0.9, verbose = 0, weight.df.power = 1, smooth.df = -1, max.adj.var = 10, theta.range = c(0.01, 100), gene.length = NULL) } \arguments{ \item{models}{model matrix (select a subset of rows to normalize variance within a subset of cells)} \item{counts}{read count matrix} \item{batch}{measurement batch (optional)} \item{trim}{trim value for Winsorization (optional, can be set to 1-3 to reduce the impact of outliers, can be as large as 5 or 10 for datasets with several thousand cells)} \item{prior}{expression magnitude prior} \item{fit.genes}{a vector of gene names which should be used to establish the variance fit (default is NULL: use all genes). This can be used to specify, for instance, a set spike-in control transcripts such as ERCC.} \item{plot}{whether to plot the results} \item{minimize.underdispersion}{whether underdispersion should be minimized (can increase sensitivity in datasets with high complexity of population, however cannot be effectively used in datasets where multiple batches are present)} \item{n.cores}{number of cores to use} \item{n.randomizations}{number of bootstrap sampling rounds to use in estimating average expression magnitude for each gene within the given set of cells} \item{weight.k}{k value to use in the final weight matrix} \item{verbose}{verbosity level} \item{weight.df.power}{power factor to use in determining effective number of degrees of freedom (can be increased for datasets exhibiting particularly high levels of noise at low expression magnitudes)} \item{smooth.df}{degrees of freedom to be used in calculating smoothed local regression between coefficient of variation and expression magnitude (and gene length, if provided). Leave at -1 for automated guess.} \item{max.adj.var}{maximum value allowed for the estimated adjusted variance (capping of adjusted variance is recommended when scoring pathway overdispersion relative to randomly sampled gene sets)} \item{theta.range}{valid theta range (should be the same as was set in knn.error.models() call} \item{gene.length}{optional vector of gene lengths (corresponding to the rows of counts matrix)} } \value{ a list containing the following fields: \itemize{ \item{mat} {adjusted expression magnitude values} \item{matw} { weight matrix corresponding to the expression matrix} \item{arv} { a vector giving adjusted variance values for each gene} \item{avmodes} {a vector estimated average expression magnitudes for each gene} \item{modes} {a list of batch-specific average expression magnitudes for each gene} \item{prior} {estimated (or supplied) expression magnitude prior} \item{edf} { estimated effective degrees of freedom} \item{fit.genes} { fit.genes parameter } } } \description{ Normalizes gene expression magnitudes to ensure that the variance follows chi-squared statistics with respect to its ratio to the transcriptome-wide expectation as determined by local regression on expression magnitude (and optionally gene length). Corrects for batch effects. } \examples{ data(pollen) cd <- clean.counts(pollen) \donttest{ knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) } } ================================================ FILE: man/pagoda.view.aspects.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{pagoda.view.aspects} \alias{pagoda.view.aspects} \title{View PAGODA output} \usage{ pagoda.view.aspects(tamr, row.clustering = hclust(dist(tamr$xv)), top = Inf, ...) } \arguments{ \item{tamr}{Combined pathways that show similar expression patterns. Output of \code{\link{pagoda.reduce.redundancy}}} \item{row.clustering}{Dendrogram of combined pathways clustering} \item{top}{Restrict output to the top n aspects of heterogeneity} \item{...}{additional arguments are passed to the \code{\link{view.aspects}} method during plotting} } \value{ PAGODA heatmap } \description{ Create static image of PAGODA output visualizing cell hierarchy and top aspects of transcriptional heterogeneity } \examples{ data(pollen) cd <- clean.counts(pollen) \donttest{ knn <- knn.error.models(cd, k=ncol(cd)/4, n.cores=10, min.count.threshold=2, min.nonfailed=5, max.model.plots=10) varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = FALSE) pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components=1, n.cores=10, n.internal.shuffles=50) tam <- pagoda.top.aspects(pwpca, return.table = TRUE, plot=FALSE, z.score=1.96) # top aspects based on GO only pagoda.view.aspects(tam) } } ================================================ FILE: man/papply.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{papply} \alias{papply} \title{wrapper around different mclapply mechanisms} \usage{ papply(..., n.cores = n) } \arguments{ \item{...}{parameters to pass to lapply, mclapply, bplapply, etc.} \item{n.cores}{number of cores. If 1 core is requested, will default to lapply} } \description{ Abstracts out mclapply implementation, and defaults to lapply when only one core is requested (helps with debugging) } ================================================ FILE: man/pollen.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \docType{data} \name{pollen} \alias{pollen} \title{Sample data} \description{ Single cell data from Pollen et al. 2014 dataset. } \references{ \url{www.ncbi.nlm.nih.gov/pubmed/25086649} } ================================================ FILE: man/scde.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \docType{package} \name{scde} \alias{scde} \alias{scde-package} \title{Single-cell Differential Expression (with Pathway And Gene set Overdispersion Analysis)} \description{ The scde package implements a set of statistical methods for analyzing single-cell RNA-seq data. scde fits individual error models for single-cell RNA-seq measurements. These models can then be used for assessment of differential expression between groups of cells, as well as other types of analysis. The scde package also contains the pagoda framework which applies pathway and gene set overdispersion analysis to identify and characterize putative cell subpopulations based on transcriptional signatures. See vignette("diffexp") for a brief tutorial on differential expression analysis. See vignette("pagoda") for a brief tutorial on pathway and gene set overdispersion analysis to identify and characterize cell subpopulations. More extensive tutorials are available at \url{http://pklab.med.harvard.edu/scde/index.html}. (test) } \author{ Peter Kharchenko \email{Peter_Kharchenko@hms.harvard.edu} Jean Fan \email{jeanfan@fas.harvard.edu} } ================================================ FILE: man/scde.browse.diffexp.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{scde.browse.diffexp} \alias{scde.browse.diffexp} \title{View differential expression results in a browser} \usage{ scde.browse.diffexp(results, models, counts, prior, groups = NULL, batch = NULL, geneLookupURL = NULL, server = NULL, name = "scde", port = NULL) } \arguments{ \item{results}{result object returned by \code{scde.expression.difference()}. Note to browse group posterior levels, use \code{return.posteriors = TRUE} in the \code{scde.expression.difference()} call.} \item{models}{model matrix} \item{counts}{count matrix} \item{prior}{prior} \item{groups}{group information} \item{batch}{batch information} \item{geneLookupURL}{The URL that will be used to construct links to view more information on gene names. By default (if can't guess the organism) the links will forward to ENSEMBL site search, using \code{geneLookupURL = "http://useast.ensembl.org/Multi/Search/Results?q = {0}"}. The "{0}" in the end will be substituted with the gene name. For instance, to link to GeneCards, use \code{"http://www.genecards.org/cgi-bin/carddisp.pl?gene = {0}"}.} \item{server}{optional previously returned instance of the server, if want to reuse it.} \item{name}{app name (needs to be altered only if adding more than one app to the server using \code{server} parameter)} \item{port}{Interactive browser port} } \value{ server instance, on which $stop() function can be called to kill the process. } \description{ Launches a browser app that shows the differential expression results, allowing to sort, filter, etc. The arguments generally correspond to the \code{scde.expression.difference()} call, except that the results of that call are also passed here. Requires \code{Rook} and \code{rjson} packages to be installed. } \examples{ data(es.mef.small) cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) sg <- factor(gsub("(MEF|ESC).*", "\\\\1", colnames(cd)), levels = c("ESC", "MEF")) names(sg) <- colnames(cd) \donttest{ o.ifm <- scde.error.models(counts = cd, groups = sg, n.cores = 10, threshold.segmentation = TRUE) o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) # make sure groups corresponds to the models (o.ifm) groups <- factor(gsub("(MEF|ESC).*", "\\\\1", rownames(o.ifm)), levels = c("ESC", "MEF")) names(groups) <- row.names(o.ifm) ediff <- scde.expression.difference(o.ifm, cd, o.prior, groups = groups, n.randomizations = 100, n.cores = 10, verbose = 1) scde.browse.diffexp(ediff, o.ifm, cd, o.prior, groups = groups, geneLookupURL="http://www.informatics.jax.org/searchtool/Search.do?query={0}") # creates browser } } ================================================ FILE: man/scde.edff.Rd ================================================ % Generated by roxygen2 (5.0.0): do not edit by hand % Please edit documentation in R/functions.R \docType{data} \name{scde.edff} \alias{scde.edff} \title{Internal model data} \description{ Numerically-derived correction for NB->chi squared approximation stored as an local regression model } ================================================ FILE: man/scde.error.models.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{scde.error.models} \alias{scde.error.models} \title{Fit single-cell error/regression models} \usage{ scde.error.models(counts, groups = NULL, min.nonfailed = 3, threshold.segmentation = TRUE, min.count.threshold = 4, zero.count.threshold = min.count.threshold, zero.lambda = 0.1, save.crossfit.plots = FALSE, save.model.plots = TRUE, n.cores = 12, min.size.entries = 2000, max.pairs = 5000, min.pairs.per.cell = 10, verbose = 0, linear.fit = TRUE, local.theta.fit = linear.fit, theta.fit.range = c(0.01, 100)) } \arguments{ \item{counts}{read count matrix. The rows correspond to genes (should be named), columns correspond to individual cells. The matrix should contain integer counts} \item{groups}{an optional factor describing grouping of different cells. If provided, the cross-fits and the expected expression magnitudes will be determined separately within each group. The factor should have the same length as ncol(counts).} \item{min.nonfailed}{minimal number of non-failed observations required for a gene to be used in the final model fitting} \item{threshold.segmentation}{use a fast threshold-based segmentation during cross-fit (default: TRUE)} \item{min.count.threshold}{the number of reads to use to guess which genes may have "failed" to be detected in a given measurement during cross-cell comparison (default: 4)} \item{zero.count.threshold}{threshold to guess the initial value (failed/non-failed) during error model fitting procedure (defaults to the min.count.threshold value)} \item{zero.lambda}{the rate of the Poisson (failure) component (default: 0.1)} \item{save.crossfit.plots}{whether png files showing cross-fit segmentations should be written out (default: FALSE)} \item{save.model.plots}{whether pdf files showing model fits should be written out (default = TRUE)} \item{n.cores}{number of cores to use} \item{min.size.entries}{minimum number of genes to use when determining expected expression magnitude during model fitting} \item{max.pairs}{maximum number of cross-fit comparisons that should be performed per group (default: 5000)} \item{min.pairs.per.cell}{minimum number of pairs that each cell should be cross-compared with} \item{verbose}{1 for increased output} \item{linear.fit}{Boolean of whether to use a linear fit in the regression (default: TRUE).} \item{local.theta.fit}{Boolean of whether to fit the overdispersion parameter theta, ie. the negative binomial size parameter, based on local regression (default: set to be equal to the linear.fit parameter)} \item{theta.fit.range}{Range of valid values for the overdispersion parameter theta, ie. the negative binomial size parameter (default: c(1e-2, 1e2))} } \value{ a model matrix, with rows corresponding to different cells, and columns representing different parameters of the determined models } \description{ Fit error models given a set of single-cell data (counts) and an optional grouping factor (groups). The cells (within each group) are first cross-compared to determine a subset of genes showing consistent expression. The set of genes is then used to fit a mixture model (Poisson-NB mixture, with expression-dependent concomitant). } \details{ Note: the default implementation has been changed to use linear-scale fit with expression-dependent NB size (overdispersion) fit. This represents an interative improvement on the originally published model. Use linear.fit=F to revert back to the original fitting procedure. } \examples{ data(es.mef.small) cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) sg <- factor(gsub("(MEF|ESC).*", "\\\\1", colnames(cd)), levels = c("ESC", "MEF")) names(sg) <- colnames(cd) \donttest{ o.ifm <- scde.error.models(counts = cd, groups = sg, n.cores = 10, threshold.segmentation = TRUE) } } ================================================ FILE: man/scde.expression.difference.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{scde.expression.difference} \alias{scde.expression.difference} \title{Test for expression differences between two sets of cells} \usage{ scde.expression.difference(models, counts, prior, groups = NULL, batch = NULL, n.randomizations = 150, n.cores = 10, batch.models = models, return.posteriors = FALSE, verbose = 0) } \arguments{ \item{models}{models determined by \code{\link{scde.error.models}}} \item{counts}{read count matrix} \item{prior}{gene expression prior as determined by \code{\link{scde.expression.prior}}} \item{groups}{a factor determining the two groups of cells being compared. The factor entries should correspond to the rows of the model matrix. The factor should have two levels. NAs are allowed (cells will be omitted from comparison).} \item{batch}{a factor (corresponding to rows of the model matrix) specifying batch assignment of each cell, to perform batch correction} \item{n.randomizations}{number of bootstrap randomizations to be performed} \item{n.cores}{number of cores to utilize} \item{batch.models}{(optional) separate models for the batch data (if generated using batch-specific group argument). Normally the same models are used.} \item{return.posteriors}{whether joint posterior matrices should be returned} \item{verbose}{integer verbose level (1 for verbose)} } \value{ \subsection{default}{ a data frame with the following fields: \itemize{ \item{lb, mle, ub} {lower bound, maximum likelihood estimate, and upper bound of the 95% confidence interval for the expression fold change on log2 scale.} \item{ce} { conservative estimate of expression-fold change (equals to the min(abs(c(lb, ub))), or 0 if the CI crosses the 0} \item{Z} { uncorrected Z-score of expression difference} \item{cZ} {expression difference Z-score corrected for multiple hypothesis testing using Holm procedure} } If batch correction has been performed (\code{batch} has been supplied), analogous data frames are returned in slots \code{$batch.adjusted} for batch-corrected results, and \code{$batch.effect} for the differences explained by batch effects alone. }} \subsection{return.posteriors = TRUE}{ A list is returned, with the default results data frame given in the \code{$results} slot. \code{difference.posterior} returns a matrix of estimated expression difference posteriors (rows - genes, columns correspond to different magnitudes of fold-change - log2 values are given in the column names) \code{joint.posteriors} a list of two joint posterior matrices (rows - genes, columns correspond to the expression levels, given by prior$x grid) } } \description{ Use the individual cell error models to test for differential expression between two groups of cells. } \examples{ data(es.mef.small) cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) sg <- factor(gsub("(MEF|ESC).*", "\\\\1", colnames(cd)), levels = c("ESC", "MEF")) names(sg) <- colnames(cd) \donttest{ o.ifm <- scde.error.models(counts = cd, groups = sg, n.cores = 10, threshold.segmentation = TRUE) o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) # make sure groups corresponds to the models (o.ifm) groups <- factor(gsub("(MEF|ESC).*", "\\\\1", rownames(o.ifm)), levels = c("ESC", "MEF")) names(groups) <- row.names(o.ifm) ediff <- scde.expression.difference(o.ifm, cd, o.prior, groups = groups, n.randomizations = 100, n.cores = n.cores, verbose = 1) } } ================================================ FILE: man/scde.expression.magnitude.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{scde.expression.magnitude} \alias{scde.expression.magnitude} \title{Return scaled expression magnitude estimates} \usage{ scde.expression.magnitude(models, counts) } \arguments{ \item{models}{models determined by \code{\link{scde.error.models}}} \item{counts}{count matrix} } \value{ a matrix of expression magnitudes on a log scale (rows - genes, columns - cells) } \description{ Return point estimates of expression magnitudes of each gene across a set of cells, based on the regression slopes determined during the model fitting procedure. } \examples{ data(es.mef.small) cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) data(o.ifm) # Load precomputed model. Use ?scde.error.models to see how o.ifm was generated # get expression magnitude estimates lfpm <- scde.expression.magnitude(o.ifm, cd) } ================================================ FILE: man/scde.expression.prior.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{scde.expression.prior} \alias{scde.expression.prior} \title{Estimate prior distribution for gene expression magnitudes} \usage{ scde.expression.prior(models, counts, length.out = 400, show.plot = FALSE, pseudo.count = 1, bw = 0.1, max.quantile = 1 - 0.001, max.value = NULL) } \arguments{ \item{models}{models determined by \code{\link{scde.error.models}}} \item{counts}{count matrix} \item{length.out}{number of points (resolution) of the expression magnitude grid (default: 400). Note: larger numbers will linearly increase memory/CPU demands.} \item{show.plot}{show the estimate posterior} \item{pseudo.count}{pseudo-count value to use (default 1)} \item{bw}{smoothing bandwidth to use in estimating the prior (default: 0.1)} \item{max.quantile}{determine the maximum expression magnitude based on a quantile (default : 0.999)} \item{max.value}{alternatively, specify the exact maximum expression magnitude value} } \value{ a structure describing expression magnitude grid ($x, on log10 scale) and prior ($y) } \description{ Use existing count data to determine a prior distribution of genes in the dataset } \examples{ data(es.mef.small) cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) data(o.ifm) # Load precomputed model. Use ?scde.error.models to see how o.ifm was generated o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) } ================================================ FILE: man/scde.failure.probability.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{scde.failure.probability} \alias{scde.failure.probability} \title{Calculate drop-out probabilities given a set of counts or expression magnitudes} \usage{ scde.failure.probability(models, magnitudes = NULL, counts = NULL) } \arguments{ \item{models}{models determined by \code{\link{scde.error.models}}} \item{magnitudes}{a vector (\code{length(counts) == nrows(models)}) or a matrix (columns correspond to cells) of expression magnitudes, given on a log scale} \item{counts}{a vector (\code{length(counts) == nrows(models)}) or a matrix (columns correspond to cells) of read counts from which the expression magnitude should be estimated} } \value{ a vector or a matrix of drop-out probabilities } \description{ Returns estimated drop-out probability for each cell (row of \code{models} matrix), given either an expression magnitude } \examples{ data(es.mef.small) cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) data(o.ifm) # Load precomputed model. Use ?scde.error.models to see how o.ifm was generated o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) # calculate probability of observing a drop out at a given set of magnitudes in different cells mags <- c(1.0, 1.5, 2.0) p <- scde.failure.probability(o.ifm, magnitudes = mags) # calculate probability of observing the dropout at a magnitude corresponding to the # number of reads actually observed in each cell self.p <- scde.failure.probability(o.ifm, counts = cd) } ================================================ FILE: man/scde.fit.models.to.reference.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{scde.fit.models.to.reference} \alias{scde.fit.models.to.reference} \title{Fit scde models relative to provided set of expression magnitudes} \usage{ scde.fit.models.to.reference(counts, reference, n.cores = 10, zero.count.threshold = 1, nrep = 1, save.plots = FALSE, plot.filename = "reference.model.fits.pdf", verbose = 0, min.fpm = 1) } \arguments{ \item{counts}{count matrix} \item{reference}{a vector of expression magnitudes (read counts) corresponding to the rows of the count matrix} \item{n.cores}{number of cores to use} \item{zero.count.threshold}{read count to use as an initial guess for the zero threshold} \item{nrep}{number independent of mixture fit iterations to try (default = 1)} \item{save.plots}{whether to write out a pdf file showing the model fits} \item{plot.filename}{model fit pdf filename} \item{verbose}{verbose level} \item{min.fpm}{minimum reference fpm of genes that will be used to fit the models (defaults to 1). Note: fpm is calculated from the reference count vector as reference/sum(reference)*1e6} } \value{ matrix of scde models } \description{ If group-average expression magnitudes are available (e.g. from bulk measurement), this method can be used to fit individual cell error models relative to that reference } \examples{ data(es.mef.small) cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) \donttest{ o.ifm <- scde.error.models(counts = cd, groups = sg, n.cores = 10, threshold.segmentation = TRUE) o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) # calculate joint posteriors across all cells jp <- scde.posteriors(models = o.ifm, cd, o.prior, n.cores = 10, return.individual.posterior.modes = TRUE, n.randomizations = 100) # use expected expression magnitude for each gene av.mag <- as.numeric(jp$jp \%*\% as.numeric(colnames(jp$jp))) # translate into counts av.mag.counts <- as.integer(round(av.mag)) # now, fit alternative models using av.mag as a reference (normally this would correspond to bulk RNA expression magnitude) ref.models <- scde.fit.models.to.reference(cd, av.mag.counts, n.cores = 1) } } ================================================ FILE: man/scde.posteriors.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{scde.posteriors} \alias{scde.posteriors} \title{Calculate joint expression magnitude posteriors across a set of cells} \usage{ scde.posteriors(models, counts, prior, n.randomizations = 100, batch = NULL, composition = NULL, return.individual.posteriors = FALSE, return.individual.posterior.modes = FALSE, ensemble.posterior = FALSE, n.cores = 20) } \arguments{ \item{models}{models models determined by \code{\link{scde.error.models}}} \item{counts}{read count matrix} \item{prior}{gene expression prior as determined by \code{\link{scde.expression.prior}}} \item{n.randomizations}{number of bootstrap iterations to perform} \item{batch}{a factor describing which batch group each cell (i.e. each row of \code{models} matrix) belongs to} \item{composition}{a vector describing the batch composition of a group to be sampled} \item{return.individual.posteriors}{whether expression posteriors of each cell should be returned} \item{return.individual.posterior.modes}{whether modes of expression posteriors of each cell should be returned} \item{ensemble.posterior}{Boolean of whether to calculate the ensemble posterior (sum of individual posteriors) instead of a joint (product) posterior. (default: FALSE)} \item{n.cores}{number of cores to utilize} } \value{ \subsection{default}{ a posterior probability matrix, with rows corresponding to genes, and columns to expression levels (as defined by \code{prior$x}) } \subsection{return.individual.posterior.modes}{ a list is returned, with the \code{$jp} slot giving the joint posterior matrix, as described above. The \code{$modes} slot gives a matrix of individual expression posterior mode values on log scale (rows - genes, columns -cells)} \subsection{return.individual.posteriors}{ a list is returned, with the \code{$post} slot giving a list of individual posterior matrices, in a form analogous to the joint posterior matrix, but reported on log scale } } \description{ Calculates expression magnitude posteriors for the individual cells, and then uses bootstrap resampling to calculate a joint expression posterior for all the specified cells. Alternatively during batch-effect correction procedure, the joint posterior can be calculated for a random composition of cells of different groups (see \code{batch} and \code{composition} parameters). } \examples{ data(es.mef.small) cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) data(o.ifm) # Load precomputed model. Use ?scde.error.models to see how o.ifm was generated o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) # calculate joint posteriors jp <- scde.posteriors(o.ifm, cd, o.prior, n.cores = 1) } ================================================ FILE: man/scde.test.gene.expression.difference.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{scde.test.gene.expression.difference} \alias{scde.test.gene.expression.difference} \title{Test differential expression and plot posteriors for a particular gene} \usage{ scde.test.gene.expression.difference(gene, models, counts, prior, groups = NULL, batch = NULL, batch.models = models, n.randomizations = 1000, show.plots = TRUE, return.details = FALSE, verbose = FALSE, ratio.range = NULL, show.individual.posteriors = TRUE, n.cores = 1) } \arguments{ \item{gene}{name of the gene to be tested} \item{models}{models} \item{counts}{read count matrix (must contain the row corresponding to the specified gene)} \item{prior}{expression magnitude prior} \item{groups}{a two-level factor specifying between which cells (rows of the models matrix) the comparison should be made} \item{batch}{optional multi-level factor assigning the cells (rows of the model matrix) to different batches that should be controlled for (e.g. two or more biological replicates). The expression difference estimate will then take into account the likely difference between the two groups that is explained solely by their difference in batch composition. Not all batch configuration may be corrected this way.} \item{batch.models}{optional set of models for batch comparison (typically the same as models, but can be more extensive, or recalculated within each batch)} \item{n.randomizations}{number of bootstrap/sampling iterations that should be performed} \item{show.plots}{whether the plots should be shown} \item{return.details}{whether the posterior should be returned} \item{verbose}{set to T for some status output} \item{ratio.range}{optionally specifies the range of the log2 expression ratio plot} \item{show.individual.posteriors}{whether the individual cell expression posteriors should be plotted} \item{n.cores}{number of cores to use (default = 1)} } \value{ by default returns MLE of log2 expression difference, 95% CI (upper, lower bound), and a Z-score testing for expression difference. If return.details = TRUE, a list is returned containing the above structure, as well as the expression fold difference posterior itself. } \description{ The function performs differential expression test and optionally plots posteriors for a specified gene. } \examples{ data(es.mef.small) cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) data(o.ifm) # Load precomputed model. Use ?scde.error.models to see how o.ifm was generated o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) scde.test.gene.expression.difference("Tdh", models = o.ifm, counts = cd, prior = o.prior) } ================================================ FILE: man/show.app.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{show.app} \alias{show.app} \title{View PAGODA application} \usage{ show.app(app, name, browse = TRUE, port = NULL, ip = "127.0.0.1", server = NULL) } \arguments{ \item{app}{pagoda app (output of make.pagoda.app()) or another rook app} \item{name}{URL path name for this app} \item{browse}{whether a call should be made for browser to show the app} \item{port}{optional port on which the server should be initiated} \item{ip}{IP on which the server should listen (typically localhost)} \item{server}{an (optional) Rook server instance (defaults to ___scde.server)} } \value{ Rook server instance } \description{ Installs a given pagoda app (or any other rook app) into a server, optionally making a call to show it in the browser. } \examples{ \donttest{ app <- make.pagoda.app(tamr2, tam, varinfo, go.env, pwpca, clpca, col.cols=col.cols, cell.clustering=hc, title="NPCs") # show app in the browser (port 1468) show.app(app, "pollen", browse = TRUE, port=1468) } } ================================================ FILE: man/view.aspects.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{view.aspects} \alias{view.aspects} \title{View heatmap} \usage{ view.aspects(mat, row.clustering = NA, cell.clustering = NA, zlim = c(-1, 1) * quantile(mat, p = 0.95), row.cols = NULL, col.cols = NULL, cols = colorRampPalette(c("darkgreen", "white", "darkorange"), space = "Lab")(1024), show.row.var.colors = TRUE, top = Inf, ...) } \arguments{ \item{mat}{Numeric matrix} \item{row.clustering}{Row dendrogram} \item{cell.clustering}{Column dendrogram} \item{zlim}{Range of the normalized gene expression levels, inputted as a list: c(lower_bound, upper_bound). Values outside this range will be Winsorized. Useful for increasing the contrast of the heatmap visualizations. Default, set to the 5th and 95th percentiles.} \item{row.cols}{Matrix of row colors.} \item{col.cols}{Matrix of column colors. Useful for visualizing cell annotations such as batch labels.} \item{cols}{Heatmap colors} \item{show.row.var.colors}{Boolean of whether to show row variance as a color track} \item{top}{Restrict output to the top n aspects of heterogeneity} \item{...}{additional arguments for heatmap plotting} } \value{ A heatmap } \description{ Internal function to visualize aspects of transcriptional heterogeneity as a heatmap. Used by \code{\link{pagoda.view.aspects}}. } ================================================ FILE: man/winsorize.matrix.Rd ================================================ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/functions.R \name{winsorize.matrix} \alias{winsorize.matrix} \title{Winsorize matrix} \usage{ winsorize.matrix(mat, trim) } \arguments{ \item{mat}{matrix} \item{trim}{fraction of outliers (on each side) that should be Winsorized, or (if the value is >= 1) the number of outliers to be trimmed on each side} } \value{ Winsorized matrix } \description{ Sets the ncol(mat)*trim top outliers in each row to the next lowest value same for the lowest outliers } \examples{ set.seed(0) mat <- matrix( c(rnorm(5*10,mean=0,sd=1), rnorm(5*10,mean=5,sd=1)), 10, 10) # random matrix mat[1,1] <- 1000 # make outlier range(mat) # look at range of values win.mat <- winsorize.matrix(mat, 0.1) range(win.mat) # note outliers removed } ================================================ FILE: src/Makevars ================================================ PKG_CXXFLAGS=$(SHLIB_OPENMP_CXXFLAGS) PKG_LIBS=-L/usr/lib/ -L"." -lpthread -lm `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) $(SHLIB_OPENMP_CXXFLAGS) ================================================ FILE: src/Makevars.win ================================================ PKG_CXXFLAGS=$(SHLIB_OPENMP_CXXFLAGS) PKG_LIBS=-L/usr/lib/ -L"." -lpthread -lm $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "Rcpp:::LdFlags()") $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) $(SHLIB_OPENMP_CXXFLAGS) ================================================ FILE: src/bwpca.cpp ================================================ #include "bwpca.h" #include #include #include #include #include #include using namespace Rcpp ; #undef DEBUG // compare a and b positions of a data vector bool compare_on_other(int a, int b, arma::vec& data) { return data[a] ind(target.n_rows); for(int j=0;j ind(target1.n_rows); for(int j=0;j(Mat); // can avoid copy here arma::mat mw=Rcpp::as(Matw); // can avoid copy here double tol=Rcpp::as(EMtol); int maxiter=Rcpp::as(EMmaxiter); int nstarts=Rcpp::as(Nstarts); int smooth=Rcpp::as(Smooth); int npcs=Rcpp::as(Npcs); int seed=Rcpp::as(Seed); int nshuffles=Rcpp::as(Nshuffles); int d=m.n_cols; // genes int n=m.n_rows; // cells if(npcs>d) { npcs=d; } // limit the number of PCs to the number of genes //arma::mat mwsq_colsum=sum(mw % mw); #ifdef DEBUG std::cout<<"starting up"<0) { int np=smooth/2; int pol_degree=3; int diff_order=0; arma::colvec x(2*np+1); for(int i=0;i<2*np+1;i++) { x[i]=i-np; }; arma::mat A(2*np+1,pol_degree+1); for(int j=0; j0) { arma::vec rvars(nshuffles); arma::mat rm(n,d); arma::mat rmw(n,d); arma::mat reigenv,rcoef; for(int i=0;i(d,npcs); arma::mat start,eigenv, R; arma::qr_econ(start,R,X); eigenv=start; //std::cout<<"starting eigenv"<::max()); double bpres(std::numeric_limits::max()); arma::mat beigenv,bcoef; // best models of the current run int ii=0; // iteration counter while(ii0) { // convolve with smoothing coefficients int n=(smoothc.n_elem-1)/2; arma::vec res=conv(eigenv.col(k),smoothc); eigenv.col(k)=res.subvec(n,res.n_elem-n-1); //std::cout<<"smooth res[]:"; eigenv.col(k).print(); } // subtract current eigenvector vector from the data if(k!= npcs-1) { dat -= coef.col(k) * eigenv.col(k).t() ; //std::cout<<"dat:"; dat.print(); } } //std::cout<<"updated eigenvectors"<0 && ii>0 && (pres-npres)/npres < tol) { if(pres>npres) { #ifdef DEBUG std::cout<<", reached required tolerance"< // outer function for performing Bailey's weighted PCA // Nshuffles - number of internal row-specific randomizations to recalculate the lambda1 (PC1 variance) on RcppExport SEXP baileyWPCA(SEXP Mat, SEXP Matw, SEXP Npcs, SEXP Nstarts, SEXP Smooth, SEXP EMtol, SEXP EMmaxiter, SEXP Seed, SEXP Nshuffles); void baileyWPCAround(arma::mat& m,arma::mat& mw,int nstarts,int npcs,int seed,int maxiter,double tol,int smooth,arma::colvec& smoothc,arma::mat& bestcoef, arma::mat& besteigenv); #endif ================================================ FILE: src/jpmatLogBoot.cpp ================================================ #include "jpmatLogBoot.h" #include using namespace Rcpp ; // maximum and minimum values of negative binomial theta (size) that will be considered #define MIN_THETA 1.0e-2 #define MAX_THETA 1.0e+3 SEXP jpmatLogBoot(SEXP Matl, SEXP Nboot, SEXP Seed){ Rcpp::List matl(Matl); int nrows=Rcpp::as(matl[0]).nrow(); int ncols=Rcpp::as(matl[0]).ncol(); int nmat=matl.size(); int nboot=as(Nboot); arma::mat jp(nrows,ncols); // joint posterior across boostraps jp.zeros(); arma::mat tjp(nrows,ncols); // for current boostrap posterior int seed=Rcpp::as(Seed); srand(seed); for(int i=0;i(matl[rj]).begin(),nrows,ncols,false,true); tjp +=am; //R_CheckUserInterrupt(); } arma::colvec m=max(tjp,1); tjp.each_col() -= m; // shift for stability tjp=exp(tjp); arma::colvec s=sum(tjp,1); tjp.each_col() /= s; jp+=tjp; //R_CheckUserInterrupt(); } return wrap(jp); } // similar to above, however the joint is built not over boostrap samples // of the same pool, but by sampling a pre-defined (by Comp vector) composition // of several different pools (in Matll) SEXP jpmatLogBatchBoot(SEXP Matll, SEXP Comp, SEXP Nboot, SEXP Seed){ Rcpp::IntegerVector comp=as(Comp); Rcpp::NumericMatrix nm0=as(VECTOR_ELT( VECTOR_ELT(Matll,0) ,0)); int nrows=nm0.nrow(); int ncols=nm0.ncol(); int nboot=as(Nboot); arma::mat jp(nrows,ncols); // joint posterior across boostraps jp.zeros(); arma::mat tjp(nrows,ncols); // for current boostrap posterior int seed=Rcpp::as(Seed); srand(seed); for(int i=0;i0) { int nmat=LENGTH( VECTOR_ELT(Matll, k) ); for(int j=0;j(VECTOR_ELT( VECTOR_ELT(Matll,k) ,rj)).begin(),nrows,ncols,false,true); tjp +=am; //R_CheckUserInterrupt(); } } //R_CheckUserInterrupt(); } arma::colvec m=max(tjp,1); tjp.each_col() -= m; // shift for stability tjp=exp(tjp); arma::colvec s=sum(tjp,1); tjp.each_col() /= s; jp+=tjp; //R_CheckUserInterrupt(); } return wrap(jp); } //calculate log joint posteriors from models and unique count lists/integer on magniude grid // count columns must match model rows // Models - model matrix // Ucl - unique count list // Uci - unique count index // Magnitudes - marginals - (expression magnitudes) // Nboot - number of bootstrap iterations // Seed - random seed // ReturnIndividualPosterior: 0 - nothing, 1 - maxima, 2 - full posterior matrices // LocalThetaFit: 0 - constant theta; 1 - theta(fpm) fit // SquareLogitConc: 0 - concomitant is a function of magnitude ; 1 - of magnitude and magnitude^2 // EnsembleProbability: 1 - calculate ensemble (i.e. sum) joint probability instead of a joint (i.e product) RcppExport SEXP logBootPosterior(SEXP Models, SEXP Ucl, SEXP CountsI, SEXP Magnitudes, SEXP Nboot, SEXP Seed, SEXP ReturnIndividualPosteriors, SEXP LocalThetaFit, SEXP SquareLogitConc, SEXP EnsembleProbability) { #define CONCB_I 0 #define CONCA_I 1 #define FAILR_I 2 #define CORRB_I 3 #define CORRA_I 4 #define CORRT_I 5 #define CORRlTB_I 6 #define CORRlTT_I 7 #define CORRlTM_I 8 #define CORRlTS_I 9 #define CORRlTR_I 10 #define CONCA2_I 11 Rcpp::IntegerMatrix counti=Rcpp::as(CountsI); Rcpp::List ucl(Ucl); int ncells=ucl.size(); int returnpost=Rcpp::as(ReturnIndividualPosteriors); int localtheta=Rcpp::as(LocalThetaFit); int squarelogitconc=Rcpp::as(SquareLogitConc); int ensemblep=Rcpp::as(EnsembleProbability); arma::mat models=Rcpp::as(Models); arma::colvec magnitudes=Rcpp::as(Magnitudes); std::vector< arma::mat > ucposteriors; std::vector< std::vector < arma::uword > > ucmaxi; // calculate individual posteriors for each cell, for each unique count value //std::cout<<"individual posteriors "<::max()/ncells/1.1; for(int i=0;i(ucl[i])); int ncounts=uc.size(); arma::mat pm(magnitudes.n_elem,ncounts); std::vector< arma::uword > maxi; arma::vec mu=magnitudes * models(i,CORRA_I); mu+=models(i,CORRB_I); mu=exp(mu); arma::vec cfp; if(squarelogitconc) { cfp=models(i,CONCA_I) + magnitudes*models(i,CONCA2_I); cfp%=magnitudes; } else { cfp=magnitudes * models(i,CONCA_I); } cfp+=models(i,CONCB_I); cfp=1/(exp(cfp)+1); arma::vec cfpr=1-cfp; cfp=log(cfp); cfpr=log(cfpr); double maxcfp=max(cfp); arma::colvec thetas; if(localtheta) { // non-constant theta model - prepare theta values thetas=-1*magnitudes + models(i,CORRlTM_I); thetas*=models(i,CORRlTS_I); thetas=exp10(thetas)+1; thetas=pow(thetas,models(i,CORRlTR_I)); thetas=(models(i,CORRlTT_I) - models(i,CORRlTB_I))/thetas; thetas+=models(i,CORRlTB_I); thetas=exp(-1*thetas); for(unsigned int k=0;kMAX_THETA) { thetas[k]=MAX_THETA;} //R_CheckUserInterrupt(); } } //std::cout<<"cfp=["; std::copy(cfp.begin(),cfp.end(),std::ostream_iterator(std::cout," ")); std::cout<<"]"<(std::cout," ")); std::cout<<"]"<muv && xmuv)) { muv=x; } nbp[k]=Rf_dnbinom(x,thetas[k],thetas[k]/(thetas[k]+muv),true); //R_CheckUserInterrupt(); } } else { // constant theta double theta=models(i,CORRT_I); for(unsigned int k=0;kmuv && xmuv)) { muv=x; } nbp[k]=Rf_dnbinom(x,theta,theta/(theta+muv),true); //R_CheckUserInterrupt(); } } //std::cout<<"nbp1=["; copy(nbp.begin(),nbp.end(),std::ostream_iterator(std::cout," ")); std::cout<<"]"<(Seed); srand(seed); int nboot=as(Nboot); if(ensemblep) { for(int j=0;j(CountsI); Rcpp::List ucl(Ucl); int ncells=ucl.size(); int returnpost=Rcpp::as(ReturnIndividualPosteriors); int localtheta=Rcpp::as(LocalThetaFit); int squarelogitconc=Rcpp::as(SquareLogitConc); arma::mat models=Rcpp::as(Models); arma::colvec magnitudes=Rcpp::as(Magnitudes); Rcpp::List batchil(BatchIL); Rcpp::IntegerVector comp=as(Composition); std::vector< arma::mat > ucposteriors; std::vector< std::vector < arma::uword > > ucmaxi; // calculate individual posteriors for each cell, for each unique count value //std::cout<<"individual posteriors "<::max()/ncells/1.1; for(int i=0;i(ucl[i])); int ncounts=uc.size(); arma::mat pm(magnitudes.n_elem,ncounts); std::vector< arma::uword > maxi; arma::vec mu=magnitudes * models(i,CORRA_I); mu+=models(i,CORRB_I); mu=exp(mu); arma::vec cfp; if(squarelogitconc) { cfp=models(i,CONCA_I) + magnitudes*models(i,CONCA2_I); cfp%=magnitudes; } else { cfp=magnitudes * models(i,CONCA_I); } cfp+=models(i,CONCB_I); cfp=1/(exp(cfp)+1); arma::vec cfpr=1-cfp; cfp=log(cfp); cfpr=log(cfpr); double maxcfp=max(cfp); arma::colvec thetas; if(localtheta) { // linear theta model - prepare theta values thetas=-1*magnitudes + models(i,CORRlTM_I); thetas*=models(i,CORRlTS_I); thetas=exp10(thetas)+1; thetas=pow(thetas,models(i,CORRlTR_I)); thetas=(models(i,CORRlTT_I) - models(i,CORRlTB_I))/thetas; thetas+=models(i,CORRlTB_I); thetas=exp(-1*thetas); for(unsigned int k=0;kMAX_THETA) { thetas[k]=MAX_THETA;} //R_CheckUserInterrupt(); } } //R_CheckUserInterrupt(); for(int j=0;jmuv && xmuv)) { muv=x; } nbp[k]=Rf_dnbinom(x,thetas[k],thetas[k]/(thetas[k]+muv),true); //R_CheckUserInterrupt(); } } else { // constant theta double theta=models(i,CORRT_I); for(unsigned int k=0;kmuv && xmuv)) { muv=x; } nbp[k]=Rf_dnbinom(x,theta,theta/(theta+muv),true); } } nbp+=cfpr; // failure probability double fp=Rf_dpois(uc[j],exp(models(i,FAILR_I)),true); // max logp to shift by double maxp=max(nbp); if(maxp<(maxcfp+fp)) { maxp=maxcfp+fp; } nbp=(exp(nbp-maxp) + exp(cfp+fp-maxp)); nbp/=sum(nbp); nbp=log(nbp); // find max point if(returnpost==1) { arma::uword maxij; double maxv=nbp.max(maxij); maxi.push_back(maxij); } // set the lower bound to min/n.cells for(unsigned int k=0;k(Seed); srand(seed); int nboot=as(Nboot); for(int i=0;i0) { // sample cells from k-th batch Rcpp::IntegerVector bi(Rcpp::as(batchil[k])); // indecies of cells within the current batch int ncells=bi.size(); for(int j=0;j RcppExport SEXP jpmatLogBoot(SEXP Matl, SEXP Nboot, SEXP Seed) ; RcppExport SEXP jpmatLogBatchBoot(SEXP Matll, SEXP Comp, SEXP Nboot, SEXP Seed) ; RcppExport SEXP logBootPosterior(SEXP Models, SEXP Ucl, SEXP CountsI, SEXP Magnitudes, SEXP Nboot, SEXP Seed, SEXP ReturnIndividualPosteriors,SEXP LocalThetaFit, SEXP SquareLogitConc, SEXP EnsembleProbability) ; RcppExport SEXP logBootBatchPosterior(SEXP Models, SEXP Ucl, SEXP CountsI, SEXP Magnitudes, SEXP BatchIL, SEXP Composition, SEXP Nboot, SEXP Seed, SEXP ReturnIndividualPosteriors, SEXP LocalThetaFit, SEXP SquareLogitConc); #endif ================================================ FILE: src/matSlideMult.cpp ================================================ #include "matSlideMult.h" using namespace Rcpp ; SEXP matSlideMult(SEXP Mat1, SEXP Mat2){ arma::mat m1=Rcpp::as(Mat1); arma::mat m2=Rcpp::as(Mat2); int n=m1.n_cols; arma::mat rm(m1.n_rows,2*n-1); // left half for(int i=n;i>1;i--) { rm.col(n-i)=sum(m1.cols(0,n-i) % m2.cols(i-1,n-1),1); R_CheckUserInterrupt(); } // right half for(int i=1;i<=n;i++) { rm.col(n-2+i)=sum(m1.cols(i-1,n-1) % m2.cols(0,n-i),1); R_CheckUserInterrupt(); } return wrap(rm); } ================================================ FILE: src/matSlideMult.h ================================================ #ifndef _scde_MATSLIDEMULT_H #define _scde_MATSLIDEMULT_H #include RcppExport SEXP matSlideMult(SEXP Mat1, SEXP Mat2) ; #endif ================================================ FILE: src/pagoda.cpp ================================================ #include "pagoda.h" #include using namespace Rcpp; SEXP winsorizeMatrix(SEXP Mat, SEXP Trim){ arma::mat m=Rcpp::as(Mat); int n=m.n_cols; int k=m.n_rows; int ntr=round(n * Rcpp::as(Trim)); // number of positions to trim (from each side) if(ntr==0) { return wrap(m); } // nothing needs to be done for(int i=0;i(X); arma::mat y=Rcpp::as(Y); arma::mat c=arma::cor(x,y); return wrap(c); } SEXP matWCorr(SEXP Mat, SEXP Matw){ arma::mat m=Rcpp::as(Mat); arma::mat w=Rcpp::as(Matw); int n=m.n_cols; // int k=m.n_rows; arma::mat c(n,n,arma::fill::eye); for(int i=0;i<(n-1);i++) { for(int j=i+1;j(Rcpp::as(pl[i])[1]); Rcpp::NumericVector i1=Rcpp::as(Rcpp::as(pl[i])[0]); int v1s=v1.size(); for(int j=i+1;j(Rcpp::as(pl[j])[1]); Rcpp::NumericVector i2=Rcpp::as(Rcpp::as(pl[j])[0]); int v2s=v2.size(); int sgc=0; // "same gene" counter double l12=0; // covar double l11=0; // var1 double l22=0; // var2 int k2=0; for(int k1=0;k10) { cv=l12/sqrt(cv); } cm(i,j)=cv; cm(j,i)=cv; sgc=v1s+v2s-sgc; cn(i,j)=sgc; cn(j,i)=sgc; } R_CheckUserInterrupt(); }; return List::create(Named("r") = wrap(cm), Named("n") = wrap(cn)); } ================================================ FILE: src/pagoda.h ================================================ #ifndef _scde_PAGODA_H #define _scde_PAGODA_H #include RcppExport SEXP winsorizeMatrix(SEXP Mat, SEXP Trim); RcppExport SEXP matWCorr(SEXP Mat, SEXP Matw); RcppExport SEXP plSemicompleteCor2(SEXP Pl); RcppExport SEXP matCorr(SEXP X, SEXP Y); #endif ================================================ FILE: tests/tests.R ================================================ # tests for travis.ci library(scde) ###### # Basic diff exp and batch correction tests ###### # load example dataset data(es.mef.small) # factor determining cell types sg <- factor(gsub("(MEF|ESC).*", "\\1", colnames(es.mef.small)), levels=c("ESC", "MEF")) # the group factor should be named accordingly names(sg) <- colnames(es.mef.small) table(sg) # clean up the dataset cd <- es.mef.small # omit genes that are never detected cd <- cd[rowSums(cd)>0, ] # omit cells with very poor coverage cd <- cd[, colSums(cd)>1e4] # calculate models # takes too long to run on travis... # o.ifm <- scde.error.models(counts=cd, groups=sg, n.cores=1, threshold.segmentation=T, save.crossfit.plots=F, save.model.plots=F, verbose=1) # devtools::use_data(o.ifm) # save for later since this step takes a long time data(o.ifm) # filter out cells that don't show positive correlation with # the expected expression magnitudes (very poor fits) valid.cells <- o.ifm$corr.a > 0 table(valid.cells) o.ifm <- o.ifm[valid.cells, ] # estimate gene expression prior o.prior <- scde.expression.prior(models=o.ifm, counts=cd, length.out=400, show.plot=F) # define two groups of cells groups <- factor(gsub("(MEF|ESC).*", "\\1", rownames(o.ifm)), levels=c("ESC", "MEF")) names(groups) <- row.names(o.ifm) # run differential expression tests on all genes. ediff <- scde.expression.difference(o.ifm, cd, o.prior, groups=groups, n.randomizations=100, n.cores=1, verbose=1) # top upregulated genes (tail would show top downregulated ones) scde.test.gene.expression.difference("Tdh", models=o.ifm, counts=cd, prior=o.prior) batch <- as.factor(ifelse(rbinom(nrow(o.ifm), 1, 0.5)==1, "batch1", "batch2")) # check the interaction between batches and cell types (shouldn't be any) table(groups, batch) # test the Tdh gene again scde.test.gene.expression.difference("Tdh", models=o.ifm, counts=cd, prior=o.prior, batch=batch) # test for all of the genes ediff.batch <- scde.expression.difference(o.ifm, cd, o.prior, groups=groups, batch=batch, n.randomizations=100, n.cores=1, return.posteriors=T, verbose=1) ================================================ FILE: vignettes/diffexp.Rmd ================================================ --- title: "Getting Started with `scde`" author: "Peter Kharchenko, Jean Fan" date: '`r Sys.Date()`' output: pdf_document --- # Single-Cell Differential Expression Analysis In this vignette, we show you how perform single cell differential expression analysis using single cell RNA-seq data with the `scde` package. The `scde` package implements routines for fitting individual error models for single-cell RNA-seq measurements. Briefly, the read counts observed for each gene are modeled using a mixture of a negative binomial (NB) distribution (for the amplified/detected transcripts) and low-level Poisson distribution (for the unobserved or background-level signal of genes that failed to amplify or were not detected for other reasons). These models can then be used to identify robustly differentially expressed genes between groups of cells. For more information, please refer to the original manuscript by [_Kharchenko et al._](http://www.ncbi.nlm.nih.gov/pubmed/24836921). ## Preparing data The analysis starts with a matrix of read counts. Depending on the protocol, these may be raw numbers of reads mapped to each gene, or count values adjusted for potential biases (sequence dependency, splice variant coverage, etc. - the values must be integers). The `scde` package includes a subset of the ES/MEF cell dataset published by [_Islam et al._](http://www.ncbi.nlm.nih.gov/pubmed/24363023). The subset includes first 20 ES and MEF cells. Here we load the cells and define a factor separating ES and MEF cell types: ```{r, include = FALSE} library(knitr) opts_chunk$set( warning = FALSE, message = FALSE, fig.show = 'hold', fig.path = 'figures/scde-', cache.path = 'cache/scde-', cache = TRUE ) ``` ```{r, data} library(scde) # load example dataset data(es.mef.small) # factor determining cell types sg <- factor(gsub("(MEF|ESC).*", "\\1", colnames(es.mef.small)), levels = c("ESC", "MEF")) # the group factor should be named accordingly names(sg) <- colnames(es.mef.small) table(sg) # clean up the dataset cd <- clean.counts(es.mef.small, min.lib.size=1000, min.reads = 1, min.detected = 1) ``` ## Fitting error models As a next step we fit the error models on which all subsequent calculations will rely. The fitting process relies on a subset of robust genes that are detected in multiple cross-cell comparisons. Here we supply the `groups = sg` argument, so that the error models for the two cell types are fit independently (using two different sets of "robust" genes). If the `groups` argument is omitted, the models will be fit using a common set. Note this step takes a considerable amount of time unless multiple cores are used. ```{r, fit, eval = FALSE} # EVALUATION NOT NEEDED # calculate models o.ifm <- scde.error.models(counts = cd, groups = sg, n.cores = 1, threshold.segmentation = TRUE, save.crossfit.plots = FALSE, save.model.plots = FALSE, verbose = 1) ``` For the purposes of this vignette, the model has been precomputed and can simply be loaded. ```{r, fit2, results = 'hide'} data(o.ifm) ``` The `o.ifm` is a dataframe with error model coefficients for each cell (rows). ```{r, fit3, results = 'hide'} head(o.ifm) ``` Here, `corr.a` and `corr.b` are slope and intercept of the correlated component fit, `conc.*` refer to the concomitant fit, `corr.theta` is the NB over-dispersion, and `fail.r` is the background Poisson rate (fixed). Particularly poor cells may result in abnormal fits, most commonly showing negative `corr.a`, and should be removed. ```{r, fit4} # filter out cells that don't show positive correlation with # the expected expression magnitudes (very poor fits) valid.cells <- o.ifm$corr.a > 0 table(valid.cells) o.ifm <- o.ifm[valid.cells, ] ``` Here, all the fits were valid. Finally, we need to define an expression magnitude prior for the genes. Its main function, however, is to define a grid of expression magnitude values on which the numerical calculations will be carried out. ```{r, prior} # estimate gene expression prior o.prior <- scde.expression.prior(models = o.ifm, counts = cd, length.out = 400, show.plot = FALSE) ``` Here we used a grid of 400 points, and let the maximum expression magnitude be determined by the default 0.999 quantile (use `max.value` parameter to specify the maximum expression magnitude explicitly - on log10 scale). ## Testing for differential expression To test for differential expression, we first define a factor that specifies which two groups of cells are to be compared. The factor elements correspond to the rows of the model matrix (`o.ifm`), and can contain `NA` values (i.e. cells that won't be included in either group). Here we key off the the ES and MEF names. ```{r, diffexp} # define two groups of cells groups <- factor(gsub("(MEF|ESC).*", "\\1", rownames(o.ifm)), levels = c("ESC", "MEF")) names(groups) <- row.names(o.ifm) # run differential expression tests on all genes. ediff <- scde.expression.difference(o.ifm, cd, o.prior, groups = groups, n.randomizations = 100, n.cores = 1, verbose = 1) # top upregulated genes (tail would show top downregulated ones) head(ediff[order(ediff$Z, decreasing = TRUE), ]) ``` ```{r, diffexp2, eval = FALSE} # write out a table with all the results, showing most significantly different genes (in both directions) on top write.table(ediff[order(abs(ediff$Z), decreasing = TRUE), ], file = "results.txt", row.names = TRUE, col.names = TRUE, sep = "\t", quote = FALSE) ``` Alternatively we can run the differential expression on a single gene, and visualize the results: ```{r, diffexp3, cache = TRUE, fig.height = 9, fig.width = 6, fig.show='hold', fig.path='figures/scde-'} scde.test.gene.expression.difference("Tdh", models = o.ifm, counts = cd, prior = o.prior) ``` The top and the bottom plots show expression posteriors derived from individual cells (colored lines) and joint posteriors (black lines). The middle plot shows posterior of the expression fold difference between the two cell groups, highlighting the 95% credible interval by the red shading. ## Correcting for batch effects When the data combines cells that were measured in different batches, it is sometimes necessary to explicitly account for the expression differences that could be explained by the batch composition of the cell groups being compared. The example below makes up a random batch composition for the ES/MEF cells, and re-test the expression difference. ```{r, seed, include = FALSE} set.seed(1) ``` ```{r, batch, fig.height = 9, fig.width = 6} batch <- as.factor(ifelse(rbinom(nrow(o.ifm), 1, 0.5) == 1, "batch1", "batch2")) # check the interaction between batches and cell types (shouldn't be any) table(groups, batch) # test the Tdh gene again scde.test.gene.expression.difference("Tdh", models = o.ifm, counts = cd, prior = o.prior, batch = batch) ``` In the plot above, the grey lines are used to show posterior distributions based on the batch composition alone. The expression magnitude posteriors (top and bottom plots) look very similar, and as a result the log2 expression ratio posterior is close to 0. The thin black line shows log2 expression ratio posterior before correction. The batch correction doesn't shift the location, but increases uncertainty in the ratio estimate (since we're controlling for another factor). Similarly, batch correction can be performed when calculating expression differences for the entire dataset: ```{r, batch2} # test for all of the genes ediff.batch <- scde.expression.difference(o.ifm, cd, o.prior, groups = groups, batch = batch, n.randomizations = 100, n.cores = 1, return.posteriors = TRUE, verbose = 1) ``` ### More detailed functions The `scde.expression.difference` method can return a more extensive set of results, including joint posteriors and the expression fold difference posteriors for all of the exam ined genes: ```{r, detailed1, include=FALSE, eval=FALSE} # recalculate difference and return with joint posteriors and difference posterior ediff.details <- scde.expression.difference(o.ifm, cd, o.prior, n.randomizations = 100, n.cores = 1, verbose = 1, return.posteriors = TRUE) ``` The joint posteriors can also be obtained explicitly for a particular set of cells: ```{r, detailed2, eval=FALSE} # calculate joint posterior for ESCs (set return.individual.posterior.modes=T if you need p.modes) jp <- scde.posteriors(models = o.ifm[grep("ESC",rownames(o.ifm)), ], cd, o.prior, n.cores = 1) ``` The error models fit the intercept and the slope of the NB "correlated" component, providing more consistent expression magnitude estimates among the cells. These can be obtain ed with a quick helper function: ```{r, detailed3} # get expression magntiude estimates o.fpm <- scde.expression.magnitude(o.ifm, counts = cd) ``` Drop-out probabilities (as a function of expression magnitudes) for different cells are useful for assessing the quality of the measurements: ```{r, detailed4, fig.width=4, fig.height=4} # get failure probabilities on the expresison range o.fail.curves <- scde.failure.probability(o.ifm, magnitudes = log((10^o.prior$x)-1)) par(mfrow = c(1,1), mar = c(3.5,3.5,0.5,0.5), mgp = c(2.0,0.65,0), cex = 1) plot(c(), c(), xlim=range(o.prior$x), ylim=c(0,1), xlab="expression magnitude (log10)", ylab="drop-out probability") invisible(apply(o.fail.curves[, grep("ES",colnames(o.fail.curves))], 2, function(y) lines(x = o.prior$x, y = y,col = "orange"))) invisible(apply(o.fail.curves[, grep("MEF", colnames(o.fail.curves))], 2, function(y) lines(x = o.prior$x, y = y, col = "dodgerblue"))) ``` The drop-out probabilities (at a given expression magnitude, or at an observed count) can be useful in subsequent analysis ```{r, detailed5} # get failure probabilities on the expresison range o.fail.curves <- scde.failure.probability(o.ifm, magnitudes = log((10^o.prior$x)-1)) # get self-fail probabilities (at a given observed count) p.self.fail <- scde.failure.probability(models = o.ifm, counts = cd) ``` ## Adjusted distance meaures The dependency of drop-out probability on the average expression magntiude captured by the cell-speicifc models can be used to adjust cell-to-cell similarity measures, for insta nce in the context of cell clustering. Several such measures are explored below. ### Direct drop-out Direct weighting downweights the contribution of a given gene to the cell-to-cell distance based on the probability that the given measurement is a drop-out event (i.e. belongs to the drop-out component) - the "self-fail" probability shown in the previous section. To estimate the adjusted distance, we will simulate the drop-out events, replacing them with `NA` values, and calculating correlation using the remaining points: ```{r, adjusted1, results='hide', eval=FALSE} p.self.fail <- scde.failure.probability(models = o.ifm, counts = cd) # simulate drop-outs # note: using 10 sampling rounds for illustration here. ~500 or more should be used. n.simulations <- 10; k <- 0.9; cell.names <- colnames(cd); names(cell.names) <- cell.names; dl <- mclapply(1:n.simulations,function(i) { scd1 <- do.call(cbind,lapply(cell.names,function(nam) { x <- cd[,nam]; # replace predicted drop outs with NAs x[!as.logical(rbinom(length(x),1,1-p.self.fail[,nam]*k))] <- NA; x; })) rownames(scd1) <- rownames(cd); # calculate correlation on the complete observation pairs cor(log10(scd1+1),use="pairwise.complete.obs"); }, mc.cores = 1) # calculate average distance across sampling rounds direct.dist <- as.dist(1-Reduce("+",dl)/length(dl)) ``` ### Reciprocal weighting The reciprocal weighting of the Pearson correlation will give increased weight to pairs of observations where a gene expressed (on average) at a level x1 observed in a cell c1 would not be likely to fail in a cell c2, and vice versa: ```{r, adjusted2, results='hide', eval=FALSE} # load boot package for the weighted correlation implementation require(boot) k <- 0.95; reciprocal.dist <- as.dist(1 - do.call(rbind, mclapply(cell.names, function(nam1) { unlist(lapply(cell.names, function(nam2) { # reciprocal probabilities f1 <- scde.failure.probability(models = o.ifm[nam1,,drop = FALSE], magnitudes = o.fpm[, nam2]) f2 <- scde.failure.probability(models = o.ifm[nam2,,drop = FALSE], magnitudes = o.fpm[, nam1]) # weight factor pnf <- sqrt((1-f1)*(1-f2))*k +(1-k); boot::corr(log10(cbind(cd[, nam1], cd[, nam2])+1), w = pnf) })) },mc.cores = 1)), upper = FALSE) ``` ### Mode-relative weighting A more reliable reference magnitude against which drop-out likelihood could be assessed would be an estimate of the average expression magnitude, such as joint posterior mode. Below we estimate `p.mode.fail`, a probability that a drop-out event could be observed at the level of average expression magntiude in a given cell. For each measurement we then reduce it weight if it indeed dropped out in a cell where we expect it to drop-out given its average expression magnitude `(p.self.fail*p.mode.fail)`. However we do want to give high weight to measurements where the drop-out was not observed, even though it was exected based on the average expression magnitude, so the overall weight expression is `(1-p.self.fail*sqrt(p.self.fail*p.mode.fail))` (other formulations are clearly possible here). ```{r, adjusted3, results='hide', eval=FALSE} # reclculate posteriors with the individual posterior modes jp <- scde.posteriors(models = o.ifm, cd, o.prior, return.individual.posterior.modes = TRUE, n.cores = 1) # find joint posterior modes for each gene - a measure of MLE of group-average expression jp$jp.modes <- log(as.numeric(colnames(jp$jp)))[max.col(jp$jp)] p.mode.fail <- scde.failure.probability(models = o.ifm, magnitudes = jp$jp.modes) # weight matrix matw <- 1-sqrt(p.self.fail*sqrt(p.self.fail*p.mode.fail)) # magnitude matrix (using individual posterior modes here) mat <- log10(exp(jp$modes)+1); # weighted distance mode.fail.dist <- as.dist(1-do.call(rbind,mclapply(cell.names,function(nam1) { unlist(lapply(cell.names,function(nam2) { corr(cbind(mat[, nam1], mat[, nam2]), w = sqrt(sqrt(matw[, nam1]*matw[, nam2]))) })) }, mc.cores = 1)), upper = FALSE) ``` ================================================ FILE: vignettes/pagoda.Rmd ================================================ --- title: "Getting Started with `pagoda` Routines" author: "Peter Kharchenko, Jean Fan" date: '`r Sys.Date()`' output: html_document --- # Pathway and Gene Set Overdispersion Analysis In this vignette, we show you how to use `pagoda` routines in the `scde` package to characterize aspects of transcriptional heterogeneity in populations of single cells. The `pagoda` routines implemented in the `scde` resolves multiple, potentially overlapping aspects of transcriptional heterogeneity by identifying known pathways or novel gene sets that show significant excess of coordinated variability among the measured cells. Briefly, cell-specific error models derived from `scde` are used to estimate residual gene expression variance, and identify pathways and gene sets that exhibit statistically significant excess of coordinated variability (overdispersion). `pagoda` can be used to effectively recover known subpopulations and discover putative new subpopulations and their corresponding functional characteristics in single-cell samples. For more information, please refer to the original manuscript by [_Fan et al._](http://biorxiv.org/content/early/2015/09/16/026948). ```{r, include = FALSE} library(knitr) opts_chunk$set( warning = FALSE, message = FALSE, fig.show = 'hold', fig.path = 'figures/pagoda-', cache.path = 'cache/pagoda-', cache = TRUE ) ``` ## Preparing data The analysis starts with a matrix of read counts. Here, we use the read count table and cell group annotations from [_Pollen et al._](www.ncbi.nlm.nih.gov/pubmed/25086649) can be loaded using the `data("pollen")` call. Some additional filters are also applied. ```{r, data} library(scde) data(pollen) # remove poor cells and genes cd <- clean.counts(pollen) # check the final dimensions of the read count matrix dim(cd) ``` Next, we'll translate group and sample source data from [_Pollen et al._](www.ncbi.nlm.nih.gov/pubmed/25086649) into color codes. These will be used later to compare [_Pollen et al._](www.ncbi.nlm.nih.gov/pubmed/25086649)'s derived annotation with subpopulations identified by `pagoda`: ```{r, colorcodes} x <- gsub("^Hi_(.*)_.*", "\\1", colnames(cd)) l2cols <- c("coral4", "olivedrab3", "skyblue2", "slateblue3")[as.integer(factor(x, levels = c("NPC", "GW16", "GW21", "GW21+3")))] ``` ## Fitting error models Next, we'll construct error models for individual cells. Here, we use k-nearest neighbor model fitting procedure implemented by `knn.error.models()` method. This is a relatively noisy dataset (non-UMI), so we raise the `min.count.threshold` to 2 (minimum number of reads for the gene to be initially classified as a non-failed measurement), requiring at least 5 non-failed measurements per gene. We're providing a rough guess to the complexity of the population, by fitting the error models based on 1/4 of most similar cells (i.e. guessing there might be ~4 subpopulations). Note this step takes a considerable amount of time unless multiple cores are used. We highly recommend use of multiple cores. You can check the number of available cores available using `detectCores()`. ```{r, models, eval = FALSE, hide = TRUE} # EVALUATION NOT NEEDED knn <- knn.error.models(cd, k = ncol(cd)/4, n.cores = 1, min.count.threshold = 2, min.nonfailed = 5, max.model.plots = 10) ``` For the purposes of this vignette, the model has been precomputed and can simply be loaded. ```{r, models2, results = 'hide'} data(knn) ``` The fitting process above wrote out `cell.models.pdf` file in the current directory showing model fits for the first 10 cells (see `max.model.plots` argument). The fitting process above wrote out `cell.models.pdf` file in the current directory showing model fits for the first 10 cells (see `max.model.plots` argument). Here's an example of such plot: ![cell 3 model](https://github.com/hms-dbmi/scde/raw/master/vignettes/figures/pagoda-cell.model.fits-0.png) The two scatter plots on the left show observed (in a given cell) vs. expected (from k similar cells) expression magnitudes for each gene that is being used for model fitting. The second (from the left) scatter plot shows genes belonging to the drop-out component in red. The black dashed lines show 95% confidence band for the amplified genes (the grey dashed lines show confidence band for an alternative constant-theta model). The third plot shows drop-out probability as a function of magnitude, and the fourth plot shows negative binomial theta local regression fit as a function of magnitude (for the amplified component). ## Normalizing variance In order to accurately quantify excess variance or overdispersion, we must normalize out expected levels of technical and intrinsic biological noise. Briefly, variance of the NB/Poisson mixture processes derived from the error modeling step are modeled as a chi-squared distribution using adjusted degrees of freedom and observation weights based on the drop-out probability of a given gene. Here, we normalize variance, trimming 3 most extreme cells and limiting maximum adjusted variance to 5. ```{r varnorm, fig.height = 3, fig.width = 6} varinfo <- pagoda.varnorm(knn, counts = cd, trim = 3/ncol(cd), max.adj.var = 5, n.cores = 1, plot = TRUE) ``` The plot on the left shows coefficient of variance squared (on log10 scale) as a function of expression magnitude (log10 FPM). The red line shows local regression model for the genome-wide average dependency. The plot on the right shows adjusted variance (derived based on chi-squared probability of observed/genomewide expected ratio for each gene, with degrees of freedom adjusted for each gene). The adjusted variance of 1 means that a given gene exhibits as much variance as expected for a gene of such population average expression magnitude. Genes with high adjusted variance are overdispersed within the measured population and most likely show subpopulation-specific expression: ```{r, varnorm2} # list top overdispersed genes sort(varinfo$arv, decreasing = TRUE)[1:10] ``` ## Controlling for sequencing depth Even with all the corrections, sequencing depth or gene coverage is typically still a major aspects of variability. In most studies, we would want to control for that as a technical artifact (exceptions are cell mixtures where subtypes significantly differ in the amount of total mRNA). Below we will control for the gene coverage (estimated as a number of genes with non-zero magnitude per cell) and normalize out that aspect of cell heterogeneity: ```{r, varnorm3} varinfo <- pagoda.subtract.aspect(varinfo, colSums(cd[, rownames(knn)]>0)) ``` ## Evaluate overdispersion of pre-defined gene sets In order to detect significant aspects of heterogeneity across the population of single cells, 'pagoda' identifies pathways and gene sets that exhibit statistically significant excess of coordinated variability. Specifically, for each gene set, we tested whether the amount of variance explained by the first principal component significantly exceed the background expectation. We can test both pre-defined gene sets as well as 'de novo' gene sets whose expression profiles are well-correlated within the given dataset. For pre-defined gene sets, we'll use GO annotations. For the purposes of this vignette, in order to make calculations faster, we will only consider the first 100 GO terms plus a few that we care about. Additional tutorials on how to create and use your own gene sets can be found in [a separate tutorial](http://hms-dbmi.github.io/scde/genesets.html). ```{r, goenv} library(org.Hs.eg.db) # translate gene names to ids ids <- unlist(lapply(mget(rownames(cd), org.Hs.egALIAS2EG, ifnotfound = NA), function(x) x[1])) rids <- names(ids); names(rids) <- ids # convert GO lists from ids to gene names gos.interest <- unique(c(ls(org.Hs.egGO2ALLEGS)[1:100],"GO:0022008","GO:0048699", "GO:0000280", "GO:0007067")) go.env <- lapply(mget(gos.interest, org.Hs.egGO2ALLEGS), function(x) as.character(na.omit(rids[x]))) go.env <- clean.gos(go.env) # remove GOs with too few or too many genes go.env <- list2env(go.env) # convert to an environment ``` Now, we can calculate weighted first principal component magnitudes for each GO gene set in the provided environment. ```{r, pathwaySig} pwpca <- pagoda.pathway.wPCA(varinfo, go.env, n.components = 1, n.cores = 1) ``` We can now evaluate the statistical significance of the observed overdispersion for each GO gene set. ```{r, topPathways, fig.height = 4, fig.width = 5} df <- pagoda.top.aspects(pwpca, return.table = TRUE, plot = TRUE, z.score = 1.96) ``` Each point on the plot shows the PC1 variance (lambda1) magnitude (normalized by set size) as a function of set size. The red lines show expected (solid) and 95% upper bound (dashed) magnitudes based on the Tracey-Widom model. ```{r, df} head(df) ``` * The z column gives the Z-score of pathway over-dispersion relative to the genome-wide model (Z-score of 1.96 corresponds to P-value of 5%, etc.). * "z.adj" column shows the Z-score adjusted for multiple hypothesis (using Benjamini-Hochberg correction). * "score" gives observed/expected variance ratio * "sh.z" and "adj.sh.z" columns give the raw and adjusted Z-scores of "pathway cohesion", which compares the observed PC1 magnitude to the magnitudes obtained when the observations for each gene are randomized with respect to cells. When such Z-score is high (e.g. for GO:0008009) then multiple genes within the pathway contribute to the coordinated pattern. ## Evaluate overdispersion of 'de novo' gene sets We can also test 'de novo' gene sets whose expression profiles are well-correlated within the given dataset. The following procedure will determine 'de novo' gene clusters in the data, and build a background model for the expectation of the gene cluster weighted principal component magnitudes. Note the higher trim values for the clusters, as we want to avoid clusters that are formed by outlier cells. ```{r, clusterPCA, fig.height = 3, fig.width = 6} clpca <- pagoda.gene.clusters(varinfo, trim = 7.1/ncol(varinfo$mat), n.clusters = 50, n.cores = 1, plot = TRUE) ``` The plot above shows background distribution of the first principal component (`PC1`) variance (`lambda1`) magnitude. The blue scatterplot on the left shows `lambda1` magnitude vs. cluster size for clusters determined based on randomly-generated matrices of the same size. The black circles show top cluster in each simulation. The red lines show expected magnitude and 95% confidence interval based on Tracy-Widom distribution. The right plot shows extreme value distribution fit of residual cluster `PC1` variance magnitude relative to the Gumbel (extreme value) distribution. Now the set of top aspects can be recalculated taking these `de novo` gene clusters into account: ```{r, topPathways2, fig.height = 4, fig.width = 5} df <- pagoda.top.aspects(pwpca, clpca, return.table = TRUE, plot = TRUE, z.score = 1.96) head(df) ``` The gene clusters and their corresponding model expected value and 95% upper bound are shown in green. ## Visualize significant aspects of heterogeneity To view top heterogeneity aspects, we will first obtain information on all the significant aspects of transcriptional heterogeneity. We will also determine the overall cell clustering based on this full information: ```{r, celclust} # get full info on the top aspects tam <- pagoda.top.aspects(pwpca, clpca, n.cells = NULL, z.score = qnorm(0.01/2, lower.tail = FALSE)) # determine overall cell clustering hc <- pagoda.cluster.cells(tam, varinfo) ``` Next, we will reduce redundant aspects in two steps. First we will combine pathways that are driven by the same sets of genes: ```{r, loadingCollapse} tamr <- pagoda.reduce.loading.redundancy(tam, pwpca, clpca) ``` In the second step we will combine aspects that show similar patterns (i.e. separate the same sets of cells). Here we will plot the cells using the overall cell clustering determined above: ```{r, correlatedCollapse, fig.height = 6, fig.width = 10} tamr2 <- pagoda.reduce.redundancy(tamr, distance.threshold = 0.9, plot = TRUE, cell.clustering = hc, labRow = NA, labCol = NA, box = TRUE, margins = c(0.5, 0.5), trim = 0) ``` In the plot above, the columns are cells, rows are different significant aspects, clustered by their similarity pattern.The green-to-orange color scheme shows low-to-high weighted PCA scores (aspect patterns), where generally orange indicates higher expression. Blocks of color on the left margin show which aspects have been combined by the command above. Here the number of resulting aspects is relatively small. "top" argument (i.e. top = 10) can be used to limit further analysis to top N aspects. We will view the top aspects, clustering them by pattern similarity (note, to view aspects in the order of increasing `lambda1` magnitude, use `row.clustering = NA`). ```{r, viewAspects, fig.height = 3.5, fig.width = 8} col.cols <- rbind(groups = cutree(hc, 3)) pagoda.view.aspects(tamr2, cell.clustering = hc, box = TRUE, labCol = NA, margins = c(0.5, 20), col.cols = rbind(l2cols)) ``` While each row here represents a cluster of pathways, the row names are assigned to be the top overdispersed aspect in each cluster. To interactively browse and explore the output, we can create a `pagoda` app: ```{r, pagodaApp, eval = FALSE} # compile a browsable app, showing top three clusters with the top color bar app <- make.pagoda.app(tamr2, tam, varinfo, go.env, pwpca, clpca, col.cols = col.cols, cell.clustering = hc, title = "NPCs") # show app in the browser (port 1468) show.app(app, "pollen", browse = TRUE, port = 1468) ``` The `pagoda` app allows you to view the gene sets grouped within each aspect (row), as well as genes underlying the detected heterogeneity patterns. A screenshot of the app is provided below: ![pagoda app](https://github.com/hms-dbmi/scde/raw/master/vignettes/figures/pagoda-Screen_Shot_2015-06-07_at_4.53.46_PM.png) An interactive version of the full run (all GO terms tested) can also be found here: [http://pklab.med.harvard.edu/cgi-bin/R/rook/pollen.npc/index.html](http://pklab.med.harvard.edu/cgi-bin/R/rook/pollen.npc/index.html) Similar views can be obtained in the R session itself. For instance, here we'll view top 10 genes associated with the top two pathways in the neurogenesis cluster: "neurogenesis" (GO:0022008) and "generation of neurons" (GO:0048699) ```{r, showTopPathwayGenes, fig.height = 3.5, fig.width = 8} pagoda.show.pathways(c("GO:0022008","GO:0048699"), varinfo, go.env, cell.clustering = hc, margins = c(1,5), show.cell.dendrogram = TRUE, showRowLabels = TRUE, showPC = TRUE) ``` ## Controlling for undesired aspects of heterogeneity Depending on the biological setting, certain dominant aspects of transcriptional heterogeneity may not be of interest. To explicitly control for these aspects of heterogeneity that are not of interest, we will use `pagoda.subtract.aspect` method that we've previously used to control for residual patterns associated with sequencing depth differences. Here, we illustrate how to control for the mitotic cell cycle pattern (GO:0000280 nuclear division and GO:0007067 mitotic nuclear division) which showed up as one of the four significant aspects in the analysis above. ```{r, controlForCellCycle} # get cell cycle signature and view the top genes cc.pattern <- pagoda.show.pathways(c("GO:0000280", "GO:0007067"), varinfo, go.env, show.cell.dendrogram = TRUE, cell.clustering = hc, showRowLabels = TRUE) # subtract the pattern varinfo.cc <- pagoda.subtract.aspect(varinfo, cc.pattern) ``` Now we can go through the same analysis as shown above, starting with the `pagoda.pathway.wPCA()` call, using `varinfo.cc` instead of `varinfo`, which will control for the cell cycle heterogeneity between the cells. ================================================ FILE: web/additional.css ================================================ body { margin: 0px; margin-top: 0; } table#elevels { //background-color:#FFFFFF; //border: solid #000 2px; width: 500px; } table#elevels th { padding: 5px; text-align: center; } table#elevels td { padding: 5px; text-align: center; border: solid #000 1px; } ================================================ FILE: web/pathcl.css ================================================ .tab-cont { float: left; margin: 0 15px 15px 0; } .tab-icon { background-image: url(extjs/examples/shared/icons/fam/application_view_list.png); } .pathcl-icon { background-image: url(extjs/examples/shared/icons/fam/text_list_bullets.png); } .colormap { shape-rendering: crispEdges; } .rowLabel { text-anchor: start; dominant-baseline: middle; font: 18px sans-serif; } .geneRowLabel { text-anchor: start; dominant-baseline: middle; font: 12px sans-serif; } /* cross hairs */ .crosshair line { display:none; pointer-events:none; } .highlighting .crosshair line { display:inline; stroke-dasharray: 5,5; stroke: black; stroke-width: 1px; pointer-events:none; } line.highlighting.crosshair { display:inline; stroke-dasharray: 5,5; stroke: black; stroke-width: 1px; pointer-events:none; } g.crosshair text { display:none; pointer-events:none; } .highlighting g.crosshair text { display:inline; fill: black; font: 18px sans-serif; font-weight: bold; } div.positive .x-progress-bar.x-progress-bar-default{ background-color: #FFABAB; background-image: none; } div.x-progress .x-progress-text.x-progress-text-back { color:black; } div.x-progress.x-progress-default { border-color: black; background-color: #EEEEEE; } div.negative .x-progress-bar.x-progress-bar-default{ background-color: #85C2FF; background-image: none; } div.x-progress .x-progress-text { text-align: left; color: black; font-weight: normal; padding-left: 5px; } #pathclsvg path { vector-effect: non-scaling-stroke; } rect.datapt { pointer-events: all; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } circle { stroke: black; stroke-width: 0.5; fill-opacity: 0.7; } circle.selected { stroke: black; stroke-width: 2.0; fill-opacity:1; } ================================================ FILE: web/pathcl.js ================================================ Ext.require(['*']); Ext.onReady(function() { var cw; var currentPathCl=-1; // currently selected pathway cluster Ext.tip.QuickTipManager.init(); //Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider')); var clusterToolbar = Ext.create('Ext.toolbar.Toolbar', { items: [ { // xtype: 'button', // default for Toolbars text: 'Show' },'->',{ xtype : 'textfield', icon: 'preview.png', cls: 'x-btn-text-icon', fieldLabel: 'number of genes', labelStyle: 'white-space: nowrap;', name : 'nGenes', emptyText: 'number of genes to show' }] }); var tutorialWindow = new Ext.Window({ id:'tutorial-window', title: 'Video Tutorial', layout:'fit', activeItem: 0, defaults: {border:false}, bbar: Ext.create('Ext.toolbar.Toolbar', { padding: 5, items : [{ xtype: 'checkbox', boxLabel: 'do not automatically show this tutorial video on startup when I visit next time', checked: Ext.util.Cookies.get("hidetutorial")!=null, name: 'dontshowtutorial', listeners: { change: function(field, value) { if(value) { var now = new Date(); var expiry = new Date(now.getTime() + 365 * 24 * 60 * 60 * 1000); Ext.util.Cookies.set("hidetutorial",true,expiry) } else { console.log("clearing hidetutorial"); } } } },'->',{ xtype: 'button', text: 'Close', handler: function() { tutorialWindow.hide(); } } ] }), items : [{ id: "video", html: '' }] }); /* DETAILED CLUSTERING VIEW */ var detailMode=1; // 1: gene 2: pathway var detailItemList={}; // pathway or gene list var detailNGenes=20; // max genes to show var detailPanel = Ext.create('Ext.panel.Panel', { bodyPadding: 5, autoScroll: true, showGenes: function(ids) { if(ids === undefined || ids.length==0) return; detailPanel.setLoading(true); Ext.Ajax.request({ url: 'genecl.json', method: 'POST', waitTitle: 'Connecting', waitMsg: 'Sending data...', params: { "genes" : encodeURIComponent(JSON.stringify(ids)) }, scope:this, failure: function(r,o){console.log('failure:'); console.log(r);}, success: function(response) { var data = Ext.JSON.decode(response.responseText) detailPanel.ids=ids; detailPanel.genecldata=data; detailPanel.mode=1; Ext.getCmp('expressionDetailsPane').setTitle("Expression Details: selected genes"); detailPanel.redraw(detailPanel.genecldata) detailPanelGearMenu.getComponent(0).disable() //detailPanelGearMenu.getComponent(1).disable() detailPanelGearMenu.getComponent(2).disable() detailPanelGearMenu.getComponent(4).disable() detailPanel.setLoading(false); } }); }, showPathways: function(ids) { if(ids === undefined || ids.length==0) return; detailPanel.setLoading(true); var ngenes=detailPanelGearMenu.getComponent(0).getValue(); var twosided=detailPanelGearMenu.getComponent(4).checked; Ext.Ajax.request({ url: 'pathwaygenes.json', method: 'POST', waitTitle: 'Connecting', waitMsg: 'Sending data...', params: { "ngenes" : ngenes, "twosided" : twosided, "genes" : encodeURIComponent(JSON.stringify(ids)), "trim" : hc.trim }, scope:this, failure: function(r,o){console.log('failure:'); console.log(r);}, success: function(response) { var data = Ext.JSON.decode(response.responseText) detailPanel.ids=ids; detailPanel.genecldata=data; detailPanel.mode=2; Ext.getCmp('expressionDetailsPane').setTitle("Expression Details: top genes in specified pathways"); detailPanel.redraw(detailPanel.genecldata) detailPanelGearMenu.getComponent(0).enable() //detailPanelGearMenu.getComponent(1).enable() detailPanelGearMenu.getComponent(2).enable() detailPanelGearMenu.getComponent(4).enable() detailPanel.setLoading(false); } }); }, searchSimilar: function(pattern) { // request genes most closely matching current data.colcol if(!detailPanel.hasOwnProperty('genecldata')) return; detailPanel.setLoading(true); if(pattern === undefined) { // determine the colcol or single gene if(detailPanel.genecldata.hasOwnProperty('colcols')) { pattern=detailPanel.genecldata.colcols.data; } else { pattern=detailPanel.genecldata.matrix.data.slice(0,detailPanel.genecldata.matrix.dim[1]); } } var ngenes=detailPanelGearMenu.getComponent(0).getValue(); var twosided=detailPanelGearMenu.getComponent(4).checked; Ext.Ajax.request({ url: 'patterngenes.json', method: 'POST', waitTitle: 'Connecting', waitMsg: 'Sending data...', params: { "ngenes" : ngenes, "twosided" : twosided, "pattern" : encodeURIComponent(JSON.stringify(pattern)), "trim" : hc.trim }, scope:this, failure: function(r,o){console.log('failure:'); console.log(r);}, success: function(response) { var data = Ext.JSON.decode(response.responseText) detailPanel.pattern=pattern; detailPanel.genecldata=data; detailPanel.mode=3; Ext.getCmp('expressionDetailsPane').setTitle("Expression Details: genes matching specified pattern"); detailPanel.redraw(detailPanel.genecldata) detailPanelGearMenu.getComponent(0).enable() //detailPanelGearMenu.getComponent(1).enable() detailPanelGearMenu.getComponent(2).enable() detailPanelGearMenu.getComponent(4).enable() detailPanel.setLoading(false); } }); }, reload: function() { // goes back to the server to redraw the same ids if(!detailPanel.hasOwnProperty('mode')) return; switch(detailPanel.mode) { case 1: detailPanel.showGenes(detailPanel.ids); break; case 2: detailPanel.showPathways(detailPanel.ids);break; case 3: detailPanel.searchSimilar(detailPanel.pattern);break; default: console.log("reload requested with an undefined detailPanel mode"); } }, redraw: function(data) { // redraws the panels without going back to the server if(data === undefined) { if(detailPanel.hasOwnProperty('genecldata')) { data=detailPanel.genecldata; } else { return; } } //$('.datapt').remove(); $('#geneclsvg .datapt').remove(); $('#geneclsvg').remove(); detailPanel.update(""); s=detailPanel.getSize(); s.height=hc.geneUnitHeight*data.matrix.dim[0]+20; if(data.hasOwnProperty('colcols')) { s.height=s.height+hc.ccheight(data); } var el = d3.select(detailPanel.getLayout().getElementTarget().dom) var svg = el.append("svg").attr("id","geneclsvg").attr("width",(s.width-hc.padding.width)+"px").attr("height",(s.height-hc.padding.height)+"px").attr('xmlns','http://www.w3.org/2000/svg'); if(data.hasOwnProperty('colcols')) { var colcol = colcolmap(svg.append("g").attr("transform","translate("+hc.hmleft()+","+hc.cctop(data)+")"), data.colcols, hc.hmwidth(s.width), hc.ccheight(data)); colcol.append("title").text("1st principal component (PC) of the selected gene set expression: green - negative, white - neutral, orange - positive") } var cmap = colormap(svg.append("g").attr("id","gmapg").attr("transform","translate("+hc.hmleft()+","+hc.hmtop(data)+")"),data.matrix,hc.hmwidth(s.width),hc.hmheight(data,s.height)); if(data.hasOwnProperty('rowcols')) { var rowcols = sidecolormap(svg.append("g").attr("transform","translate("+hc.margins.left+","+hc.hmtop(data)+")"), data.rowcols, hc.rowcolWidth, hc.hmheight(data,s.height)); rowcols.append("title").text("contribution of a gene to the 1st principal component: green - negative, white - neutral, orange - positive") } var rowLabelStep = hc.hmheight(data,s.height)/data.matrix.dim[0]; var rowlabg = svg.append("g") .attr("transform","translate("+hc.rowlableft(s.width)+","+hc.hmtop(data)+")") .append("g") if(data.matrix.dim[0]==1) data.matrix.rows=[data.matrix.rows] var rowlab = rowlabg .selectAll(".rowLabelg") .data(data.matrix.rows) .enter() .append("text") .text(function(d) { return(d); }) .attr("x",0) .attr("y",function(d,i) { return Math.floor(i*rowLabelStep + rowLabelStep/2); }) .classed("geneRowLabel",true)[0] var focus = svg.append("g") .attr("class","crosshair"); var focushl = focus.append("line").classed("crosshair",true).attr({"x1":hc.margins.left,"y1":Math.round(hc.hmtop(data)+cmap.y(1)/2),"x2":(hc.hmleft()+hc.hmwidth(s.width)),"y2":Math.round(hc.hmtop(data)+cmap.y(1)/2)}); var focusvl = focus.append("line").classed("crosshair",true).attr("id","genefocusvl").attr({"x1":Math.round(hc.hmleft()+cmap.x(1)/2),"y1":hc.cctop(data),"x2":Math.round(hc.hmleft()+cmap.x(1)/2),"y2":(hc.hmtop(data)+hc.hmheight(data,s.height))}); var focustx = focus.append("text").text("").attr("x",Math.round(hc.hmleft()+cmap.x(1)/2)).attr("y",Math.round(hc.hmtop(data)+cmap.y(1)/2)); cmap.g.append("rect").attr('x',0).attr('y',0).attr('width',hc.hmwidth(s.width)).attr('height',hc.hmheight(data,s.height)).attr('style','fill:white;fill-opacity:0.0;stroke:black;stroke-width:0.5;pointer-events:all;').attr("id","gmapev"); var evg = d3.select("#gmapev"); evg.on("mousemove", function() { var coord=d3.mouse(evg.node()); // figure out row and column index var bbox=evg.node().getBBox(); var colIndex=Math.floor(coord[0]/bbox.width*data.matrix.dim[1]) var rowIndex=Math.floor(coord[1]/bbox.height*data.matrix.dim[0]) d3.select(rowlab[rowIndex]).classed('active', true); var newy=cmap.y(rowIndex); var newx=cmap.x(colIndex); focushl.attr("transform","translate(0,"+newy+")"); focusvl.attr("transform","translate("+newx+",0)"); focustx.text("cell: "+data.matrix.cols[colIndex]); //focustx.attr("transform","translate("+coord[0]+","+(heatmapPos[1]+heatmapHeight-10)+")"); if(newy>bbox.height/2) { newy=newy-5; } else { newy=newy+15; } if(newx>bbox.width/2) { focustx.attr("transform","translate("+(newx-5)+","+newy+")"); focustx.attr("text-anchor","end") } else { focustx.attr("transform","translate("+(newx+5)+","+newy+")"); focustx.attr("text-anchor","start") } d3.select('#pathclfocusvl').attr("transform","translate("+newx+",0)"); }).on("mouseenter",function() { el.classed('highlighting', true); d3.select('#pathclfocusvl').classed("highlighting",true) }).on("mouseleave",function() { el.classed('highlighting', false); d3.select('#pathclfocusvl').classed("highlighting",false) }) }, listeners: { resize: function(cmp,width,height,oldWidth,oldHeight,opts) { cmp.redraw(); } } }) // clear filter filed trigger button Ext.define('Ext.ux.CustomTrigger', { extend: 'Ext.form.field.Trigger', alias: 'widget.customtrigger', initComponent: function () { var me = this; me.triggerCls = 'x-form-clear-trigger'; me.callParent(arguments); }, // override onTriggerClick onTriggerClick: function() { if(this.getValue()!='') { this.setRawValue(''); this.fireEvent('change',this,''); } } }); /* PATHWAY CLUSTER INFO */ Ext.define('clinfo',{ extend: 'Ext.data.Model', fields: [ 'name', 'id', {name: 'od',type: 'float'},{name: 'npc', type: 'integer'},{name: 'sign', type: 'integer'},{name: 'initsel', type: 'integer'} ], idProperty: 'id' }); var clinfostore = Ext.create('Ext.data.Store', { id: 'clinfostore', model: 'clinfo', remoteSort: true, proxy: { type: 'jsonp', url: 'clinfo.json', extraParams: { pathcl: currentPathCl }, reader: { root: 'genes', totalProperty: 'totalCount' }, simpleSortMode: true, }, autoLoad: false, pageSize: 50, remoteFilter: true, listeners: { load: function(r) { // use the supplied 'initsel' to set the initial selection clSelectModel.suspendEvent('selectionchange') for(var i=0;i0) detailPanel.showPathways(ids); } } }); var clInfoGrid = Ext.create('Ext.grid.Panel', { store: clinfostore, id: "clinfo", selModel: clSelectModel, height:'100%', columnLines:true, emptyText: 'No Matching Pathways', tbar: Ext.create('Ext.PagingToolbar', { store: clinfostore, displayInfo: false, //displayMsg: 'Displaying genes {0} - {1} of {2}', emptyMsg: "No pathways to display", items:[ { flex:1, width: 500, minWidth: 50, xtype: 'customtrigger', emptyText: 'filter by name...', listeners: { change: {buffer: 200, fn: function(field, value) { if (value.length>0) { clinfostore.clearFilter(true); clinfostore.filter({property: 'name', value: value}); } else { clinfostore.clearFilter(false); } }} } }], listeners: { afterrender: function() { this.down('#refresh').hide(); } } }), columns: [ //{text: "name", flex: 1, dataIndex: 'name', sortable: true}, { text: 'overdispersion', dataIndex: 'od', flex:1, renderer: function (v, m, r) { //m.tdAttr='data-qtip="'+r.data.name+'"'; var id = Ext.id(); Ext.defer(function () { Ext.widget('progressbar', { renderTo: id, text: r.data.name, value: v / 1, }); }, 50); if(r.data.sign=="1") { return Ext.String.format('
', id); } else { return Ext.String.format('
', id); } } }, {text: "PC", width: 30, dataIndex: 'npc', sortable: true}, /*{text: "overdispersion", width: 100, dataIndex: 'od', sortable: true}*/ ] }) /* GENE SET ENRICHMENT INFO */ Ext.define('geinfo',{ extend: 'Ext.data.Model', fields: [ 'name', 'id', {name: 'fe',type: 'float'},{name: 'o', type: 'integer'},{name: 'u', type: 'integer'},{name: 'Z', type: 'float'},{name: 'Za', type: 'float'} ], idProperty: 'id' }); var geinfostore = Ext.create('Ext.data.Store', { id: 'geinfostore', model: 'geinfo', remoteSort: true, proxy: { type: 'ajax', url: 'testenr.json', actionMethods: {create: 'POST', read: 'POST', update: 'POST', destroy: 'POST'}, reader: { root: 'genes', totalProperty: 'totalCount' }, simpleSortMode: true, }, autoLoad: false, pageSize: 50, remoteFilter: true, }); var geSelectModel = Ext.create('Ext.selection.CheckboxModel', { listeners: { selectionchange: function(sm, selections) { var ids = $.map(selections,function(val,i) { return(val.data.id) }) if(ids.length>0) detailPanel.showPathways(ids); } } }); var geInfoGrid = Ext.create('Ext.grid.Panel', { store: geinfostore, id: "geinfo", selModel: geSelectModel, height:'100%', columnLines:true, emptyText: 'No Enriched Pathways', tbar: Ext.create('Ext.PagingToolbar', { store: geinfostore, displayInfo: false, //displayMsg: 'Displaying genes {0} - {1} of {2}', emptyMsg: "No pathways to display", items:[ { flex:1, width: 500, minWidth: 50, xtype: 'customtrigger', emptyText: 'filter by name...', listeners: { change: {buffer: 200, fn: function(field, value) { if (value.length>0) { geinfostore.clearFilter(true); geinfostore.filter({property: 'name', value: value}); } else { geinfostore.clearFilter(false); } }} } }], listeners: { afterrender: function() { this.down('#refresh').hide(); } } }), columns: [ {text: "Pathway", flex: 1, dataIndex: 'name', sortable: true }, {text: "FE", width: 60, dataIndex: 'fe', sortable: true, tooltip: "fold enrichment"}, {text: "Z", width: 60, dataIndex: 'Z', sortable: true, tooltip: "enrichment Z-score"}, {text: "cZ", width: 60, dataIndex: 'Za', sortable: true, tooltip: "enrichment Z-score, corrected for multiple hypothesis testing"}, {text: "n", width: 50, dataIndex: 'n', sortable: true, hidden: true, tooltip: "number of genes found in this pathway"}, {text: "u", width: 50, dataIndex: 'u', sortable: true, hidden: true, tooltip: "total number of genes annotated for this pathway"} ] }) /* gene info tab */ Ext.define('ginfo',{ extend: 'Ext.data.Model', fields: [ 'gene', {name: 'var',type: 'float'},{name: 'svar', type: 'float'} ], idProperty: 'gene' }); var ginfostore = Ext.create('Ext.data.Store', { id: 'ginfostore', model: 'ginfo', remoteSort: true, proxy: { type: 'jsonp', url: 'genes.json', reader: { root: 'genes', totalProperty: 'totalCount' }, simpleSortMode: true, }, sorters: [{ property: 'var', direction: 'DESC' }], pageSize: 100, remoteFilter: true, autoLoad: true }); var geneSelModel = Ext.create('Ext.selection.CheckboxModel', { listeners: { selectionchange: function(sm, selections) { var ids = $.map(selections,function(val,i) { return(val.data.gene) }) detailPanel.showGenes(ids); //grid4.down('#removeButton').setDisabled(selections.length === 0); } } }); var geneGrid = Ext.create('Ext.grid.Panel', { store: ginfostore, selModel: geneSelModel, columns: [ {text: "Gene", flex: 1, dataIndex: 'gene', sortable: true, renderer: function(value) { return Ext.String.format('{1}',value,value) } }, {text: "Variance", width: 80, dataIndex: 'var', sortable: true} ], //features: [filters], height:'100%', columnLines:true, emptyText: 'No Matching Genes', //forceFit: true //renderTo:'example-grid', //width: 800, //height: 300 // paging bar on the bottom tbar: Ext.create('Ext.PagingToolbar', { store: ginfostore, displayInfo: false, //displayMsg: 'Displaying genes {0} - {1} of {2}', emptyMsg: "No genes to display", items:[ { flex:1, width: 500, minWidth: 50, xtype: 'customtrigger', emptyText: 'filter by gene name...', listeners: { change: {buffer: 200, fn: function(field, value) { if (value.length>0) { ginfostore.clearFilter(true); ginfostore.filter({property: 'gene', value: value}); } else { ginfostore.clearFilter(false); } }} } }], listeners: { afterrender: function() { this.down('#refresh').hide(); } } }), listeners: { viewready: function() { // select top 20 genes this.selModel.suspendEvent('selectionchange') for(var i=0;i0) { pinfostore.clearFilter(true); pinfostore.filter({property: 'name', value: value}); } else { pinfostore.clearFilter(false); } }} } }], listeners: { afterrender: function() { this.down('#refresh').hide(); } } }) }); var infotab = Ext.create('Ext.tab.Panel', { //tabPosition: 'right', defaults: { bodyPadding: 0, layout: 'fit', iconCls: 'tab-icon' }, items: [ { title: 'Pathways', items:[pathwayGrid] }, { title: 'Genes', items:[geneGrid] }, { title: 'Cluster', items:[clInfoGrid], itemId: 'clustertab', hidden:true }, { title: 'Enrichment', items:[geInfoGrid], itemId: 'enrichmenttab', hidden:true } ] }); function colcolmap(svg, data, width, height) { // Check for no data if (data.length === 0) return function() {}; var cols = data.dim[1] var rows = data.dim[0]; var merged = data.data; var x = d3.scale.linear().domain([0, cols]).range([0, width]); var y = d3.scale.linear().domain([0, rows]).range([0, height]); var g = svg.append("g").classed("colormap",true); var rect = g.selectAll("rect").data(merged); rect.enter().append("rect").classed("colcoldatapt", true); //rect.exit().remove(); var cccmap= function(d) { return d; }; if(data.hasOwnProperty('domain')) { var color = d3.scale.linear() .domain(data.domain) .range(data.colors); cccmap= function(d) { return color(d); }; }; rect.attr("x", function(d, i) { return x(i % cols); }) .attr("y", function(d, i) { return y(Math.floor(i / cols)); }) .attr("width", x(1)) .attr("height", y(1)) .attr("fill", cccmap); svg.append("rect").attr('x',0).attr('y',0).attr('width',width).attr('height',height).attr('style','fill:none;stroke:black;stroke-width:0.5;'); return g; }; function sidecolormap(svg, data, width, height) { // Check for no data if(data === undefined) return function() {}; var cols = data.dim[1] var rows = data.dim[0]; var merged = data.data; var x = d3.scale.linear().domain([0, cols]).range([0, width]); var y = d3.scale.linear().domain([0, rows]).range([0, height]); var color = d3.scale.linear() .domain(data.domain) .range(data.colors); var g = svg.append("g").classed("colormap",true); var rect = g.selectAll("rect").data(merged); rect.enter().append("rect").classed("datapt", true); //rect.exit().remove(); rect.property("colIndex", function(d, i) { return i % cols; }) .property("rowIndex", function(d, i) { return Math.floor(i / cols); }) .attr("x", function(d, i) { return x(i % cols); }) .attr("y", function(d, i) { return y(Math.floor(i / cols)); }) .attr("width", x(1)) .attr("height", y(1)) .attr("fill", function(d) { return color(d); }); //.append("title").text(function(d) { return d + ""; }); svg.append("rect").attr('x',0).attr('y',0).attr('width',width).attr('height',height).attr('style','fill:none;stroke:black;stroke-width:0.5;'); return g; }; function colormap(svg, data, width, height) { // Check for no data if (data.length === 0) return function() {}; var cols = data.dim[1] var rows = data.dim[0]; var merged = data.data; var x = d3.scale.linear().domain([0, cols]).range([0, width]); var y = d3.scale.linear().domain([0, rows]).range([0, height]); var color = d3.scale.linear() .domain(data.domain) .range(data.colors); var g = svg.append("g").classed("colormap",true); //g.append("rect").attr('x',0).attr('y',0).attr('width',width).attr('height',height).attr('style','fill:white;stroke:none;'); //svg.append("rect").attr('x',0).attr('y',0).attr('width',width).attr('height',height).attr('style','fill-opacity:1;fill:black;stroke:none;pointer-events:all;').classed("mapcatcher",true); var rect = g.selectAll("rect").data(merged); rect.enter().append("rect").classed("datapt", true); //rect.exit().remove(); rect.attr("x", function(d, i) { return x(i % cols); }) .attr("y", function(d, i) { return y(Math.floor(i / cols)); }) .attr("width", x(1)) .attr("height", y(1)) .attr("fill", function(d) { return color(d); }); //.append("title").text(function(d) { return d + ""; }); svg.append("rect").attr('x',0).attr('y',0).attr('width',width).attr('height',height).attr('style','fill:none;stroke:black;stroke-width:0.5;'); return { x: x, y: y, g: g }; }; function updatePathclInfo(pathcl) { if(pathcl == currentPathCl) return; clinfostore.getProxy().setExtraParam("pathcl",pathcl) clinfostore.load(); infotab.child("#clustertab").tab.show(); infotab.setActiveTab(2); infotab.getActiveTab().setTitle("Cluster "+pathcl) currentPathCl=pathcl; } /* heatmap config */ var hc = { spacing: 2, geneUnitHeight: 15, margins: {top:2,right:60,bottom:2,left:1}, padding: {width:28,height:10}, colcolUnitHeight: 10, rowcolWidth: 10, colDendHeight: 50, trim: 0, // heatmap left position hmleft: function() { return(this.margins.left+this.rowcolWidth+this.spacing)}, // colcol top position cctop: function(data) { return(data.hasOwnProperty('coldend')? this.margins.top+this.colDendHeight+this.spacing : this.margins.top) }, // colcol height ccheight: function(data) { return(this.colcolUnitHeight*data.colcols.dim[0]) }, // heatmap top position hmtop: function(data) { if(data.hasOwnProperty('colcols')) { return(data.hasOwnProperty('coldend')? this.margins.top+this.colDendHeight+this.spacing*2 + this.ccheight(data) : this.margins.top+this.spacing*2 + this.ccheight(data)) } else { return(data.hasOwnProperty('coldend')? this.margins.top+this.colDendHeight+this.spacing : this.margins.top) } }, // heatmap hight hmheight: function(data,totalHeight) { return((totalHeight-this.padding.height*2) - this.hmtop(data)); }, // heatmap width hmwidth: function(totalWidth) { return(totalWidth-this.margins.left-this.rowcolWidth-this.spacing-this.margins.right-this.padding.width*2)}, rowlableft: function(totalWidth) { return(this.hmleft()+this.hmwidth(totalWidth)+this.spacing); }, rowlabelsize: function(data,totalHeight) { return(Math.round(this.hmheight(data,totalHeight)/data.matrix.dim[0]*0.97*72/96)) //return(18); } }; var clusterPanel = Ext.create('Ext.panel.Panel', { layout: 'fit', bodyPadding: 5, //ohtml: "Pathway Clustering", reload: function() { clusterPanel.setLoading(true); d3.json('pathcl.json',function(error,data) { if(error) { console.log(error); return;} clusterPanel.pathcldata=data; clusterPanelGearMenu.getComponent(0).suspendEvents(); clusterPanelGearMenu.getComponent(0).setValue(data.matrix.domain[data.matrix.domain.length-1]); clusterPanelGearMenu.getComponent(0).setMaxValue(data.matrix.range[1]); clusterPanelGearMenu.getComponent(0).resumeEvents() if(data.hasOwnProperty('trim')) { hc.trim=data.trim; detailPanelGearMenu.getComponent(2).suspendEvents(); detailPanelGearMenu.getComponent(2).setValue(data.trim); detailPanelGearMenu.getComponent(2).resumeEvents(); } clusterPanel.redraw(clusterPanel.pathcldata); clusterPanel.setLoading(false); }) }, redraw: function(data) { if(data === undefined) { if(clusterPanel.hasOwnProperty('pathcldata')) { data=clusterPanel.pathcldata; } else { clusterPanel.reload(); return; } } $('#pathclsvg .datapt').remove(); $('#pathclsvg').remove(); clusterPanel.update(""); s=clusterPanel.getSize(); var el = d3.select(clusterPanel.getLayout().getElementTarget().dom) var svg = el.append("svg").attr("id","pathclsvg").attr("width",(s.width-hc.padding.width)+"px").attr("height",(s.height-hc.padding.height)+"px").attr('xmlns','http://www.w3.org/2000/svg'); var dg = svg.append("g").attr("id","coldend").attr("transform", "translate(" + hc.hmleft() + " " + hc.margins.top + ") " +"scale(" + (hc.hmwidth(s.width)/72) + " " + (hc.colDendHeight/72) + ")"); // a workaround to append SVG elements into DOM $(el.node()).append(''); $(dg.node()).append($("#dummy g")); $("#dummy").remove(); // colcol var colcol = colcolmap(svg.append("g").attr("transform","translate("+hc.hmleft()+","+hc.cctop(data)+")"), data.colcols, hc.hmwidth(s.width), hc.ccheight(data)); colcol.append("title").text("custom cell classification colors") // main heatmap var cmap = colormap(svg.append("g").attr("id","cmapg").attr("transform","translate("+hc.hmleft()+","+hc.hmtop(data)+")"),data.matrix,hc.hmwidth(s.width),hc.hmheight(data,s.height)); // side colors var rowcols = sidecolormap(svg.append("g").attr("transform","translate("+hc.margins.left+","+hc.hmtop(data)+")"), data.rowcols, hc.rowcolWidth, hc.hmheight(data,s.height)); rowcols.append("title").text("Overdispersion: white - low, black - high") // row labels var rowlabelsize=hc.rowlabelsize(data,s.height); var rowLabelStep = hc.hmheight(data,s.height)/data.matrix.dim[0]; var rowlabg = svg.append("g") .attr("transform","translate("+hc.rowlableft(s.width)+","+hc.hmtop(data)+")") .append("g") var rowlab = rowlabg .selectAll(".rowLabelg") .data(data.matrix.rows) .enter() .append("text") .text(function(d) { return(d); }) .style("font-size",rowlabelsize+"pt") .attr("x",0) .attr("y",function(d,i) { return Math.floor(i*rowLabelStep + rowLabelStep/2); }) .classed("rowLabel",true)[0] var focus = svg.append("g").attr("class","crosshair"); var focushl = focus.append("line").classed("crosshair",true).attr({"x1":hc.margins.left,"y1":Math.round(hc.hmtop(data)+cmap.y(1)/2),"x2":(hc.hmleft()+hc.hmwidth(s.width)),"y2":Math.round(hc.hmtop(data)+cmap.y(1)/2)}); var focusvl = focus.append("line").classed("crosshair",true).attr("id","pathclfocusvl").attr({"x1":Math.round(hc.hmleft()+cmap.x(1)/2),"y1":hc.cctop(data),"x2":Math.round(hc.hmleft()+cmap.x(1)/2),"y2":(hc.hmtop(data)+hc.hmheight(data,s.height))}); var focustx = focus.attr("id","pathclfocustx").append("text").text("").attr("x",Math.round(hc.hmleft()+cmap.x(1)/2)).attr("y",Math.round(hc.hmtop(data)+cmap.y(1)/2)); cmap.g.append("rect").attr('x',0).attr('y',0).attr('width',hc.hmwidth(s.width)).attr('height',hc.hmheight(data,s.height)).attr('style','fill:white;fill-opacity:0.0;stroke:black;stroke-width:0.5;pointer-events:all;').attr("id","cmapev"); var evg = d3.select("#cmapev"); evg.on("click", function() { var coord=d3.mouse(evg.node()); var bbox=evg.node().getBBox(); var colIndex=Math.floor(coord[0]/bbox.width*data.matrix.dim[1]) var rowIndex=Math.floor(coord[1]/bbox.height*data.matrix.dim[0]) updatePathclInfo(rowlab[rowIndex].textContent) }).on("mousemove", function() { var coord=d3.mouse(evg.node()); // figure out row and column index var bbox=evg.node().getBBox(); var colIndex=Math.floor(coord[0]/bbox.width*data.matrix.dim[1]) var rowIndex=Math.floor(coord[1]/bbox.height*data.matrix.dim[0]) d3.select(rowlab[rowIndex]).classed('active', true); var newy=cmap.y(rowIndex); var newx=cmap.x(colIndex); focushl.attr("transform","translate(0,"+newy+")"); focusvl.attr("transform","translate("+newx+",0)"); focustx.text("cell: "+data.matrix.cols[colIndex]); //focustx.attr("transform","translate("+coord[0]+","+(heatmapPos[1]+heatmapHeight-10)+")"); if(newy>bbox.height/2) { newy=newy-5; } else { newy=newy+15; } if(newx>bbox.width/2) { focustx.attr("transform","translate("+(newx-5)+","+newy+")"); focustx.attr("text-anchor","end") } else { focustx.attr("transform","translate("+(newx+5)+","+newy+")"); focustx.attr("text-anchor","start") } d3.select('#genefocusvl').attr("transform","translate("+newx+",0)"); }).on("mouseenter",function() { el.classed('highlighting', true); d3.select('#genefocusvl').classed("highlighting",true) }).on("mouseleave",function() { el.classed('highlighting', false); d3.select('#genefocusvl').classed("highlighting",false) }) }, listeners: { resize: function(cmp,width,height,oldWidth,oldHeight,opts) { cmp.redraw(cmp.pathcldata); } } }); var clusterPanelGearMenu = Ext.create('Ext.menu.Menu', { id: 'clusterGearMenu', style: { overflow: 'visible' // For the Combo popup }, items: [{ fieldLabel: 'Z limit', name: 'zlim', xtype: 'numberfield', value: -1, decimalPrecision: 3, minValue: 0.0, maxValue: 100, width: 200, disabled: false, tooltip: 'Set the range of overdispersion scores illustrated by colors', listeners : { change : {buffer: 800, fn:function(f,v) { clusterPanel.pathcldata.matrix.domain=$.map($(Array(clusterPanel.pathcldata.matrix.domain.length)),function(val, i) { return (2*i*v/clusterPanel.pathcldata.matrix.domain.length - v); }) clusterPanel.redraw() }} } } ], listeners:{ 'mouseleave': {buffer: 1000, fn:function( menu, e, eOpts){ menu.hide(); }} } }); var detailPanelGearMenu = Ext.create('Ext.menu.Menu', { id: 'detailGearMenu', style: { overflow: 'visible' // For the Combo popup }, items: [{ fieldLabel: 'N genes', xtype: 'numberfield', tooltip: 'Number of genes to show in the Expression Details panel', label: 'N genes', value: 20, minValue: 1, maxValue: 1000, disabled: true, listeners : { change : {buffer: 800, fn:function(f,v) {detailPanel.reload()}} } /* }, '-',{ text: 'Color Z-limit:', tooltip: 'Maximum Z score to determine the color range', canActivate:false },{ xtype: 'slider', label: 'N genes', value: 0.5, increment: 1, minValue: 0, maxValue: 100, tipText: function(thumb){ return Ext.String.format('{0}', (thumb.value/100*3.6).toFixed(2)); }, */ }, { fieldLabel: 'Row height', name: 'rowheight', xtype: 'numberfield', value: hc.geneUnitHeight, minValue: 3, width: 200, maxValue: 100, tooltip: 'Number of pixels to use for each gene row', listeners : { change : {buffer: 800, fn:function(f,v) {hc.geneUnitHeight=v; detailPanel.redraw()}} } },{ fieldLabel: 'Trim', name: 'trim', xtype: 'numberfield', value: hc.trim, decimalPrecision: 5, minValue: 0.0, maxValue: 0.5, width: 200, maxValue: 100, disabled: true, tooltip: 'Winsorization trim fraction', listeners : { change : {buffer: 800, fn:function(f,v) {hc.trim=v; detailPanel.reload()}} } }, '-', { text: 'High/low genes', checked: false, tooltip: 'Whether to include genes from both sides of the PC loading (true) or just high magnitude (false)', disabled: true, listeners : { checkchange : function() {detailPanel.reload()} } }], listeners:{ 'mouseleave': {buffer: 1000, fn:function( menu, e, eOpts){ menu.hide(); }} } }); var ngenesSlider = Ext.create('Ext.slider.Single', { label: 'N genes', tip: 'number of genes to show', tipText: function(thumb){ return Ext.String.format('show {0} genes', thumb.value); }, width: 100, value: 20, increment: 1, minValue: 0, maxValue: 500, }); var viewport = Ext.create('Ext.Viewport', { layout: { type: 'border', padding: 5 }, defaults: { split: true }, items: [{ region: 'center', layout: 'border', items: [{ region: 'north', layout: 'fit', id: 'clusterPanel', title: 'Pathway Clustering', tools: [ { type:'gear', tooltip: 'Settings', handler: function(e, el,o,t) { clusterPanelGearMenu.showBy(t); } }, { type:'help', tooltip: 'Tutorial', handler: function(e, el,o,t) { tutorialWindow.show(); } }, { type:'save', tooltip: 'Save image as SVG file', handler: function(e,el,o,t) { var svg=d3.select("#pathclsvg"); if(!svg.empty()) { // update some visual attributes svg.selectAll("#coldend * path").style("vector-effect","non-scaling-stroke"); svg.selectAll(".rowLabel").style("dominant-baseline","middle").style("font","18px sans-serif"); svg.select("#pathclfocustx").text(""); var b64 = window.btoa(svg.node().parentNode.innerHTML); writeAndClickLink('data:application/octet-stream;base64,\n'+b64,'pathway_clusters.svg') } } } ], minHeight: 200, height: 300, bodyPadding: 0, split: true, items:[clusterPanel] },{ region: 'center', layout: 'fit', id: 'expressionDetailsPane', minHeight: 100, collapsible: false, // headerPosition: 'bottom', title: 'Expression Details', tools: [ { type:'search', tooltip: 'Search for genes matching the current consensus pattern', handler: function(e, el,o,t) { detailPanel.searchSimilar(); } },{ type:'collapse', tooltip: 'Run GO enrichment analysis on the current gene set', handler: function(e, el,o,t) { if(detailPanel.hasOwnProperty('genecldata')) { // write out current gene set geinfostore.getProxy().setExtraParam("genes",JSON.stringify(detailPanel.genecldata.matrix.rows)) geinfostore.load(); infotab.child("#enrichmenttab").tab.show(); // show the info tab infotab.setActiveTab(3); } } },{ type:'gear', tooltip: 'Settings', handler: function(e, el,o,t) { detailPanelGearMenu.showBy(t); } }, { type:'save', tooltip: 'Save image as SVG file', handler: function(e,el,o,t) { var svg=d3.select("#geneclsvg"); if(!svg.empty()) { svg.selectAll(".geneRowLabel").style("dominant-baseline","middle").style("font","12px sans-serif");; //var b64 = Base64.encode(el.html()); var b64 = window.btoa(svg.node().parentNode.innerHTML); writeAndClickLink('data:application/octet-stream;base64,\n'+b64,'genes.svg') } } } ], header: true, items:[detailPanel], autoScroll: true, autoShow: true, /*listeners: { afterrender: function(panel) { console.log("boo"); var header=panel.getHeader(); header.insert(1,[ngenesSlider]); } }*/ }] },{ region: 'east', collapsible: true, title: 'Info', split: true, layout: 'fit', width: '30%', minWidth: 100, minHeight: 140, bodyPadding: 0, items:[infotab] }] }); if(Ext.util.Cookies.get("hidetutorial")==null) { tutorialWindow.show(); } }); // quick helper function to provide an internal download link function writeAndClickLink(url,download) { var link=d3.select("body").append('a'); link.attr('href',url).attr('download',download); var evObj = document.createEvent('MouseEvents'); evObj.initMouseEvent('click', true, true); link.node().dispatchEvent(evObj); $(link.node()).remove(); } ================================================ FILE: web/pathcl_canvas.js ================================================ Ext.require(['*']); Ext.onReady(function() { var cw; var currentPathCl=-1; // currently selected pathway cluster Ext.tip.QuickTipManager.init(); //Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider')); var clusterToolbar = Ext.create('Ext.toolbar.Toolbar', { items: [ { // xtype: 'button', // default for Toolbars text: 'Show' },'->',{ xtype : 'textfield', icon: 'preview.png', cls: 'x-btn-text-icon', fieldLabel: 'number of genes', labelStyle: 'white-space: nowrap;', name : 'nGenes', emptyText: 'number of genes to show' }] }); var tutorialWindow = new Ext.Window({ id:'tutorial-window', title: 'Video Tutorial', layout:'fit', activeItem: 0, defaults: {border:false}, bbar: Ext.create('Ext.toolbar.Toolbar', { padding: 5, items : [{ xtype: 'checkbox', boxLabel: 'do not automatically show this tutorial video on startup when I visit next time', checked: Ext.util.Cookies.get("hidetutorial")!=null, name: 'dontshowtutorial', listeners: { change: function(field, value) { if(value) { var now = new Date(); var expiry = new Date(now.getTime() + 365 * 24 * 60 * 60 * 1000); Ext.util.Cookies.set("hidetutorial",true,expiry) } else { console.log("clearing hidetutorial"); } } } },'->',{ xtype: 'button', text: 'Close', handler: function() { tutorialWindow.hide(); } } ] }), items : [{ id: "video", html: '' }] }); /* DETAILED CLUSTERING VIEW */ var detailMode=1; // 1: gene 2: pathway var detailItemList={}; // pathway or gene list var detailNGenes=20; // max genes to show var detailPanel = Ext.create('Ext.panel.Panel', { layout: 'fit', bodyPadding: 5, autoScroll: false, showGenes: function(ids) { if(ids === undefined || ids.length==0) return; detailPanel.setLoading(true); Ext.Ajax.request({ url: 'genecl.json', method: 'POST', waitTitle: 'Connecting', waitMsg: 'Sending data...', params: { "genes" : encodeURIComponent(JSON.stringify(ids)) }, scope:this, failure: function(r,o){console.log('failure:'); console.log(r);}, success: function(response) { var data = Ext.JSON.decode(response.responseText) detailPanel.ids=ids; if(data.matrix.dim[0]==1) data.matrix.rows=[data.matrix.rows] detailPanel.genecldata=data; detailPanel.mode=1; Ext.getCmp('expressionDetailsPane').setTitle("Expression Details: selected genes"); detailPanel.redraw(detailPanel.genecldata) detailPanelGearMenu.getComponent(0).disable() detailPanelGearMenu.getComponent(1).enable() detailPanelGearMenu.getComponent(3).disable() detailPanel.setLoading(false); } }); }, showPathways: function(ids) { if(ids === undefined || ids.length==0) return; detailPanel.setLoading(true); var ngenes=detailPanelGearMenu.getComponent(0).getValue(); var twosided=detailPanelGearMenu.getComponent(3).checked; Ext.Ajax.request({ url: 'pathwaygenes.json', method: 'POST', waitTitle: 'Connecting', waitMsg: 'Sending data...', params: { "ngenes" : ngenes, "twosided" : twosided, "genes" : encodeURIComponent(JSON.stringify(ids)), "trim" : hc.trim }, scope:this, failure: function(r,o){console.log('failure:'); console.log(r);}, success: function(response) { var data = Ext.JSON.decode(response.responseText) detailPanel.ids=ids; if(data.matrix.dim[0]==1) data.matrix.rows=[data.matrix.rows] detailPanel.genecldata=data; detailPanel.mode=2; Ext.getCmp('expressionDetailsPane').setTitle("Expression Details: top genes in specified pathways"); detailPanel.redraw(detailPanel.genecldata) detailPanelGearMenu.getComponent(0).enable() detailPanelGearMenu.getComponent(1).enable() detailPanelGearMenu.getComponent(3).enable() detailPanel.setLoading(false); } }); }, searchSimilar: function(pattern) { // request genes most closely matching current data.colcol if(!detailPanel.hasOwnProperty('genecldata')) return; detailPanel.setLoading(true); if(pattern === undefined) { // determine the colcol or single gene if(detailPanel.genecldata.hasOwnProperty('colcols')) { pattern=detailPanel.genecldata.colcols.data; } else { pattern=detailPanel.genecldata.matrix.data.slice(0,detailPanel.genecldata.matrix.dim[1]); } } var ngenes=detailPanelGearMenu.getComponent(0).getValue(); var twosided=detailPanelGearMenu.getComponent(3).checked; Ext.Ajax.request({ url: 'patterngenes.json', method: 'POST', waitTitle: 'Connecting', waitMsg: 'Sending data...', params: { "ngenes" : ngenes, "twosided" : twosided, "pattern" : encodeURIComponent(JSON.stringify(pattern)), "trim" : hc.trim }, scope:this, failure: function(r,o){console.log('failure:'); console.log(r);}, success: function(response) { var data = Ext.JSON.decode(response.responseText) detailPanel.pattern=pattern; if(data.matrix.dim[0]==1) data.matrix.rows=[data.matrix.rows] detailPanel.genecldata=data; detailPanel.mode=3; Ext.getCmp('expressionDetailsPane').setTitle("Expression Details: genes matching specified pattern"); detailPanel.redraw(detailPanel.genecldata) detailPanelGearMenu.getComponent(0).enable() detailPanelGearMenu.getComponent(1).enable() detailPanelGearMenu.getComponent(3).enable() detailPanel.setLoading(false); } }); }, reload: function() { // goes back to the server to redraw the same ids if(!detailPanel.hasOwnProperty('mode')) return; switch(detailPanel.mode) { case 1: detailPanel.showGenes(detailPanel.ids); break; case 2: detailPanel.showPathways(detailPanel.ids);break; case 3: detailPanel.searchSimilar(detailPanel.pattern);break; default: console.log("reload requested with an undefined detailPanel mode"); } }, redraw: function(data) { // redraws the panels without going back to the server if(data === undefined) { if(detailPanel.hasOwnProperty('genecldata')) { data=detailPanel.genecldata; } else { return; } } $('#genecl').remove(); delete detailPanel.evctx; $('#geneclev').remove(); $('#gclCD').remove(); var s=detailPanel.getSize(); // adjust height to match maxRowHeight (=15) if needed if((s.height-hc.hmtop(data)-hc.margins.bottom)/data.matrix.dim[0] > 15) { s.height=15*data.matrix.dim[0]+hc.margins.bottom+hc.hmtop(data); } detailPanel.s=s; detailPanel.body.update('
') var gctx= $('#genecl')[0].getContext('2d'); if(data.hasOwnProperty('colcols')) { //colcols drawHeatmap(gctx,data.colcols,hc.hmleft(),hc.cctop(data),hc.hmwidth(s.width), hc.ccheight(data)); //rowcols drawHeatmap(gctx,data.rowcols,hc.margins.left,hc.hmtop(data),hc.rowcolWidth,hc.hmheight(data,s.height),false) } //main heatmap drawHeatmap(gctx,data.matrix,hc.hmleft(),hc.hmtop(data),hc.hmwidth(s.width),hc.hmheight(data,s.height),true) // event handling var evc=document.getElementById("geneclev"); var evctx= evc.getContext('2d'); detailPanel.evctx=evctx; try { evctx.setLineDash([5]); } catch (err) {} evctx.fillStyle='black'; evctx.font="bold 15px Arial"; var evtRect = evc.getBoundingClientRect(); evc.addEventListener('mousemove', function(evt) { var mx=evt.clientX-evtRect.left; var my=evt.clientY-evtRect.top; evctx.clearRect(0,0,s.width,s.height); var v=clusterPanel.s; clusterPanel.evctx.clearRect(0,0,v.width,v.height); if(my<=s.height-hc.margins.bottom && mx<=s.width-hc.margins.right && mx>=hc.hmleft() && my>=hc.hmtop(data)) { //mx=hc.hmleft()+(rx+0.5)*hc.hmwidth(s.width)/data.matrix.dim[1]; evctx.beginPath(); evctx.moveTo(mx,hc.cctop(data)); evctx.lineTo(mx,s.height-hc.margins.bottom); var ry=Math.floor((my-hc.hmtop(data))/hc.hmheight(data,s.height)*data.matrix.dim[0]); //my=hc.hmtop(data)+(ry+0.5)*hc.hmheight(data,s.height)/data.matrix.dim[0]; evctx.moveTo(hc.margins.left,my); evctx.lineTo(s.width-hc.margins.right,my); var rx=Math.floor((mx-hc.hmleft())/hc.hmwidth(s.width)*data.matrix.dim[1]); // update the line in the cluster panel clusterPanel.evctx.beginPath(); clusterPanel.evctx.moveTo(mx,hc.cctop(clusterPanel.pathcldata)) clusterPanel.evctx.lineTo(mx,v.height-hc.margins.bottom) clusterPanel.evctx.stroke(); if(mx>s.width/2) { evctx.textAlign="end"; mx-=5; } else { evctx.textAlign="start"; mx+=5; } evctx.textBaseline="bottom"; evctx.fillText("cell: "+data.matrix.cols[rx],mx,my-3); evctx.textBaseline="top"; evctx.fillText("gene: "+data.matrix.rows[ry],mx,my+3); evctx.stroke(); } }, false); evc.addEventListener('mouseout', function(evt) { evctx.clearRect(0,0,s.width,s.height); var v=clusterPanel.s; clusterPanel.evctx.clearRect(0,0,v.width,v.height); }); }, listeners: { resize: function(cmp,width,height,oldWidth,oldHeight,opts) { cmp.redraw(); } } }) // clear filter filed trigger button Ext.define('Ext.ux.CustomTrigger', { extend: 'Ext.form.field.Trigger', alias: 'widget.customtrigger', initComponent: function () { var me = this; me.triggerCls = 'x-form-clear-trigger'; me.callParent(arguments); }, // override onTriggerClick onTriggerClick: function() { if(this.getValue()!='') { this.setRawValue(''); this.fireEvent('change',this,''); } } }); /* PATHWAY CLUSTER INFO */ Ext.define('clinfo',{ extend: 'Ext.data.Model', fields: [ 'name', 'id', {name: 'od',type: 'float'},{name: 'npc', type: 'integer'},{name: 'sign', type: 'integer'},{name: 'initsel', type: 'integer'} ], idProperty: 'id' }); var clinfostore = Ext.create('Ext.data.Store', { id: 'clinfostore', model: 'clinfo', remoteSort: true, proxy: { type: 'jsonp', url: 'clinfo.json', extraParams: { pathcl: currentPathCl }, reader: { root: 'genes', totalProperty: 'totalCount' }, simpleSortMode: true, }, autoLoad: false, pageSize: 50, remoteFilter: true, listeners: { load: function(r) { // use the supplied 'initsel' to set the initial selection clSelectModel.suspendEvent('selectionchange') for(var i=0;i0) detailPanel.showPathways(ids); } } }); var clInfoGrid = Ext.create('Ext.grid.Panel', { store: clinfostore, id: "clinfo", selModel: clSelectModel, height:'100%', columnLines:true, emptyText: 'No Matching Pathways', tbar: Ext.create('Ext.PagingToolbar', { store: clinfostore, displayInfo: false, //displayMsg: 'Displaying genes {0} - {1} of {2}', emptyMsg: "No pathways to display", items:[ { flex:1, width: 500, minWidth: 50, xtype: 'customtrigger', emptyText: 'filter by name...', listeners: { change: {buffer: 200, fn: function(field, value) { if (value.length>0) { clinfostore.clearFilter(true); clinfostore.filter({property: 'name', value: value}); } else { clinfostore.clearFilter(false); } }} } }], listeners: { afterrender: function() { this.down('#refresh').hide(); } } }), columns: [ //{text: "name", flex: 1, dataIndex: 'name', sortable: true}, { text: 'overdispersion', dataIndex: 'od', flex:1, renderer: function (v, m, r) { //m.tdAttr='data-qtip="'+r.data.name+'"'; var id = Ext.id(); Ext.defer(function () { Ext.widget('progressbar', { renderTo: id, text: r.data.name, value: v / 1, }); }, 50); if(r.data.sign=="1") { return Ext.String.format('
', id); } else { return Ext.String.format('
', id); } } }, {text: "PC", width: 30, dataIndex: 'npc', sortable: true}, /*{text: "overdispersion", width: 100, dataIndex: 'od', sortable: true}*/ ] }) /* GENE SET ENRICHMENT INFO */ Ext.define('geinfo',{ extend: 'Ext.data.Model', fields: [ 'name', 'id', {name: 'fe',type: 'float'},{name: 'o', type: 'integer'},{name: 'u', type: 'integer'},{name: 'Z', type: 'float'},{name: 'Za', type: 'float'} ], idProperty: 'id' }); var geinfostore = Ext.create('Ext.data.Store', { id: 'geinfostore', model: 'geinfo', remoteSort: true, proxy: { type: 'ajax', url: 'testenr.json', actionMethods: {create: 'POST', read: 'POST', update: 'POST', destroy: 'POST'}, reader: { root: 'genes', totalProperty: 'totalCount' }, simpleSortMode: true, }, autoLoad: false, pageSize: 50, remoteFilter: true, }); var geSelectModel = Ext.create('Ext.selection.CheckboxModel', { listeners: { selectionchange: function(sm, selections) { var ids = $.map(selections,function(val,i) { return(val.data.id) }) if(ids.length>0) detailPanel.showPathways(ids); } } }); var geInfoGrid = Ext.create('Ext.grid.Panel', { store: geinfostore, id: "geinfo", selModel: geSelectModel, height:'100%', columnLines:true, emptyText: 'No Enriched Pathways', tbar: Ext.create('Ext.PagingToolbar', { store: geinfostore, displayInfo: false, //displayMsg: 'Displaying genes {0} - {1} of {2}', emptyMsg: "No pathways to display", items:[ { flex:1, width: 500, minWidth: 50, xtype: 'customtrigger', emptyText: 'filter by name...', listeners: { change: {buffer: 200, fn: function(field, value) { if (value.length>0) { geinfostore.clearFilter(true); geinfostore.filter({property: 'name', value: value}); } else { geinfostore.clearFilter(false); } }} } }], listeners: { afterrender: function() { this.down('#refresh').hide(); } } }), columns: [ {text: "Pathway", flex: 1, dataIndex: 'name', sortable: true }, {text: "FE", width: 60, dataIndex: 'fe', sortable: true, tooltip: "fold enrichment"}, {text: "Z", width: 60, dataIndex: 'Z', sortable: true, tooltip: "enrichment Z-score"}, {text: "cZ", width: 60, dataIndex: 'Za', sortable: true, tooltip: "enrichment Z-score, corrected for multiple hypothesis testing"}, {text: "n", width: 50, dataIndex: 'n', sortable: true, hidden: true, tooltip: "number of genes found in this pathway"}, {text: "u", width: 50, dataIndex: 'u', sortable: true, hidden: true, tooltip: "total number of genes annotated for this pathway"} ] }) /* gene info tab */ Ext.define('ginfo',{ extend: 'Ext.data.Model', fields: [ 'gene', {name: 'var',type: 'float'},{name: 'svar', type: 'float'} ], idProperty: 'gene' }); var ginfostore = Ext.create('Ext.data.Store', { id: 'ginfostore', model: 'ginfo', remoteSort: true, proxy: { type: 'jsonp', url: 'genes.json', reader: { root: 'genes', totalProperty: 'totalCount' }, simpleSortMode: true, }, sorters: [{ property: 'var', direction: 'DESC' }], pageSize: 100, remoteFilter: true, autoLoad: true }); var geneSelModel = Ext.create('Ext.selection.CheckboxModel', { listeners: { selectionchange: function(sm, selections) { var ids = $.map(selections,function(val,i) { return(val.data.gene) }) detailPanel.showGenes(ids); //grid4.down('#removeButton').setDisabled(selections.length === 0); } } }); var geneGrid = Ext.create('Ext.grid.Panel', { store: ginfostore, selModel: geneSelModel, columns: [ {text: "Gene", flex: 1, dataIndex: 'gene', sortable: true, renderer: function(value) { return Ext.String.format('{1}',value,value) } }, {text: "Variance", width: 100, dataIndex: 'var', sortable: true} ], //features: [filters], height:'100%', columnLines:true, emptyText: 'No Matching Genes', //forceFit: true //renderTo:'example-grid', //width: 800, //height: 300 // paging bar on the bottom tbar: Ext.create('Ext.PagingToolbar', { store: ginfostore, displayInfo: false, //displayMsg: 'Displaying genes {0} - {1} of {2}', emptyMsg: "No genes to display", items:[ { flex:1, width: 500, minWidth: 50, xtype: 'customtrigger', emptyText: 'filter by gene name...', listeners: { change: {buffer: 600, fn: function(field, value) { if (value.length>0) { ginfostore.clearFilter(true); ginfostore.filter({property: 'gene', value: value}); } else { ginfostore.clearFilter(false); } }} } }], listeners: { afterrender: function() { this.down('#refresh').hide(); } } }), listeners: { viewready: function() { // select top 20 genes this.selModel.suspendEvent('selectionchange') for(var i=0;i0) { pinfostore.clearFilter(true); pinfostore.filter({property: 'name', value: value}); } else { pinfostore.clearFilter(false); } }} } }], listeners: { afterrender: function() { this.down('#refresh').hide(); } } }) }); var infotab = Ext.create('Ext.tab.Panel', { //tabPosition: 'right', defaults: { bodyPadding: 0, layout: 'fit', iconCls: 'tab-icon' }, items: [ { title: 'Pathways', items:[pathwayGrid] }, { title: 'Genes', items:[geneGrid] }, { title: 'Cluster', items:[clInfoGrid], itemId: 'clustertab', hidden:true }, { title: 'Enrichment', items:[geInfoGrid], itemId: 'enrichmenttab', hidden:true } ] }); // draw dendrogram in a given context // ctx - canvas 2d context // d - dendrogram structure (t(merge), order, height) // x,y,width,height - placement function drawDendrogram(ctx, d, x, y, width, height) { var nmerges=d.merge.length; var xstep=width/d.order.length; var lp,lpy,rp,rpy,jx,jy; var maxHeight=Math.max.apply(null, d.height); var heightScale=height/maxHeight; ctx.beginPath(); for(var i=0, mergex=[]; id.dim[0]*maxRowHeight) { height=d.dim[0]*maxRowHeight;} var rowHeight=height/d.dim[0]; var colWidth=width/d.dim[1]; var mC=d.hasOwnProperty('colors'); // perform color mapping on the fly if(rowNames && !d.hasOwnProperty('rows')) { rowNames=false; } if(rowNames) { var fontSize=(rowHeight*0.95); if(fontSize>maxFontSize) { fontSize=maxFontSize; }; if(fontSize>=minFontSize) { ctx.font=fontSize+"px Arial"; ctx.textAlign='left'; ctx.textBaseline="middle"; } else { rowNames=false; } } var zlim=[]; if(mC) { if(d.hasOwnProperty('zlim')) { zlim=d.zlim; } else { zlim.push(-1*Math.max.apply(null,d.data.map(Math.abs))); zlim.push(-1*zlim[0]); } zlim.push(d.colors.length/(zlim[1]-zlim[0])); // zlim step } var val=0; for(var i=0; i=d.colors.length) { val=d.colors.length-1; } val=d.colors[Math.floor(val)]; } else { // interpret values as colors directly val=d.data[i*d.dim[1]+j] } ctx.fillStyle=val; ctx.fillRect(x+j*colWidth,y+i*rowHeight,colWidth,rowHeight); } if(rowNames) { ctx.fillStyle='black'; ctx.fillText(d.rows[i],x+width+3,y+(i+0.5)*rowHeight); } } ctx.lineWidth=1; ctx.strokeStyle='black'; ctx.strokeRect(x,y,width,height); //if(zlim!==undefined) {return(zlim[1])}; } function updatePathclInfo(pathcl) { if(pathcl == currentPathCl) return; clinfostore.getProxy().setExtraParam("pathcl",pathcl) clinfostore.load(); infotab.child("#clustertab").tab.show(); infotab.setActiveTab(2); infotab.getActiveTab().setTitle("Aspect "+pathcl) currentPathCl=pathcl; } /* heatmap config */ var hc = { spacing: 5, dendSpacing: 1, geneUnitHeight: 15, margins: {top:2,right:80,bottom:15,left:1}, colcolUnitHeight: 10, rowcolWidth: 10, colDendHeight: 50, trim: 0, // heatmap left position hmleft: function() { return(this.margins.left+this.rowcolWidth+this.spacing)}, // colcol top position cctop: function(data) { return(data.hasOwnProperty('coldend')? this.margins.top+this.colDendHeight+this.dendSpacing : this.margins.top) }, // colcol height ccheight: function(data) { return(this.colcolUnitHeight*data.colcols.dim[0]) }, // heatmap top position hmtop: function(data) { if(data.hasOwnProperty('colcols')) { return(data.hasOwnProperty('coldend')? this.margins.top+this.colDendHeight+this.spacing+this.dendSpacing + this.ccheight(data) : this.margins.top+this.spacing+this.dendSpacing + this.ccheight(data)) } else { return(data.hasOwnProperty('coldend')? this.margins.top+this.colDendHeight+this.spacing : this.margins.top) } }, // heatmap hight hmheight: function(data,totalHeight) { return((totalHeight-this.margins.bottom) - this.hmtop(data)); }, // heatmap width hmwidth: function(totalWidth) { return(totalWidth-this.margins.left-this.rowcolWidth-this.spacing-this.margins.right)}, rowlableft: function(totalWidth) { return(this.hmleft()+this.hmwidth(totalWidth)+this.spacing); }, rowlabelsize: function(data,totalHeight) { return(Math.round(this.hmheight(data,totalHeight)/data.matrix.dim[0]*0.97*72/96)) //return(18); } }; var clusterPanel = Ext.create('Ext.panel.Panel', { layout: 'fit', bodyPadding: 5, //ohtml: "Pathway Clustering", reload: function() { clusterPanel.setLoading(true); Ext.Ajax.request({ url: 'pathcl.json', method: 'POST', waitTitle: 'Connecting', waitMsg: 'Sending data...', scope:this, failure: function(r,o){console.log('failure:'); console.log(r);}, success: function(response) { var data = Ext.JSON.decode(response.responseText) if(data.matrix.dim[0]==1) data.matrix.rows=[data.matrix.rows] if(data.colcols.dim[0]==1) data.colcols.rows=[data.colcols.rows] clusterPanel.pathcldata=data; clusterPanelGearMenu.getComponent(0).suspendEvents(); clusterPanelGearMenu.getComponent(0).setValue(data.matrix.zlim[1]); //clusterPanelGearMenu.getComponent(0).setMaxValue(data.matrix.range[1]); clusterPanelGearMenu.getComponent(0).resumeEvents() if(data.hasOwnProperty('trim')) { hc.trim=data.trim; detailPanelGearMenu.getComponent(1).suspendEvents(); detailPanelGearMenu.getComponent(1).setValue(data.trim); detailPanelGearMenu.getComponent(1).resumeEvents(); } clusterPanel.redraw(clusterPanel.pathcldata); clusterPanel.setLoading(false); } }) }, redraw: function(data) { if(data === undefined) { if(clusterPanel.hasOwnProperty('pathcldata')) { data=clusterPanel.pathcldata; } else { clusterPanel.reload(); return; } } $('#pathcl').remove(); $('#pathclev').remove(); delete clusterPanel.evctx; $('#pclCD').remove(); //clusterPanel.update(""); var s=clusterPanel.getSize(); clusterPanel.s=s; clusterPanel.body.update('
') var ctx= $('#pathcl')[0].getContext('2d'); drawDendrogram(ctx,data.coldend,hc.hmleft(),hc.margins.top,hc.hmwidth(s.width),hc.colDendHeight); //colcols drawHeatmap(ctx,data.colcols,hc.hmleft(),hc.cctop(data),hc.hmwidth(s.width), hc.ccheight(data)); //rowcols drawHeatmap(ctx,data.rowcols,hc.margins.left,hc.hmtop(data),hc.rowcolWidth,hc.hmheight(data,s.height)) //main heatmap drawHeatmap(ctx,data.matrix,hc.hmleft(),hc.hmtop(data),hc.hmwidth(s.width),hc.hmheight(data,s.height),rowNames=true) // event handling var evc=document.getElementById("pathclev"); var evctx= evc.getContext('2d'); clusterPanel.evctx=evctx; //evctx.strokeRect(0,0,s.width-10,s.height-10); try { evctx.setLineDash([5]); } catch (err) {} evctx.fillStyle='black'; evctx.font="bold 15px Arial"; var evtRect = evc.getBoundingClientRect(); evc.addEventListener('click', function(evt) { var mx=evt.clientX-evtRect.left; var my=evt.clientY-evtRect.top; if(my<=s.height-hc.margins.bottom && mx<=s.width-hc.margins.right && mx>=hc.hmleft()) { if(my>=hc.hmtop(data)) { var ry=Math.floor((my-hc.hmtop(data))/hc.hmheight(data,s.height)*data.matrix.dim[0]); evctx.strokeStyle='red'; evctx.lineWidth=2; try { evctx.setLineDash([0]); } catch(err) {} //evctx.strokeRect(hc.hmleft(),hc.hmtop(data)+ry*hc.hmheight(data,s.height)/data.matrix.dim[0],hc.hmwidth(s.width),hc.hmheight(data,s.height)/data.matrix.dim[0]); evctx.strokeRect(0,hc.hmtop(data)+ry*hc.hmheight(data,s.height)/data.matrix.dim[0],s.width,hc.hmheight(data,s.height)/data.matrix.dim[0]); evctx.strokeStyle='black'; evctx.lineWidth=1; try { evctx.setLineDash([5]); } catch(err) {} updatePathclInfo(data.matrix.rows[ry]) } } }, false); evc.addEventListener('mousemove', function(evt) { var mx=evt.clientX-evtRect.left; var my=evt.clientY-evtRect.top; evctx.clearRect(0,0,s.width,s.height); if(detailPanel.hasOwnProperty('evctx')) { var v=detailPanel.s; detailPanel.evctx.clearRect(0,0,v.width,v.height); } if(my<=s.height-hc.margins.bottom && mx<=s.width-hc.margins.right && mx>=hc.hmleft() && my>=hc.cctop(data)) { //mx=hc.hmleft()+(rx+0.5)*hc.hmwidth(s.width)/data.matrix.dim[1]; evctx.beginPath(); evctx.moveTo(mx,hc.cctop(data)); evctx.lineTo(mx,s.height-hc.margins.bottom); // update the line in the gene panel if(detailPanel.hasOwnProperty('evctx')) { detailPanel.evctx.beginPath(); detailPanel.evctx.moveTo(mx,hc.cctop(detailPanel.genecldata)) detailPanel.evctx.lineTo(mx,v.height-hc.margins.bottom) detailPanel.evctx.stroke(); } var rx=Math.floor((mx-hc.hmleft())/hc.hmwidth(s.width)*data.matrix.dim[1]); if(mx>s.width/2) { evctx.textAlign="end"; mx-=5; } else { evctx.textAlign="start"; mx+=5; } evctx.textBaseline="bottom"; evctx.fillText("cell: "+data.matrix.cols[rx],mx,my-3); if(my>=hc.hmtop(data)) { var ry=Math.floor((my-hc.hmtop(data))/hc.hmheight(data,s.height)*data.matrix.dim[0]); //my=hc.hmtop(data)+(ry+0.5)*hc.hmheight(data,s.height)/data.matrix.dim[0]; evctx.moveTo(hc.margins.left,my); evctx.lineTo(s.width-hc.margins.right,my); //evctx.fillText("cell: "+data.matrix.cols[rx],mx+5,my-5); evctx.textBaseline="top"; evctx.fillText("aspect: "+data.matrix.rows[ry],mx,my+3); } else { evctx.moveTo(hc.hmleft(),my); evctx.lineTo(s.width-hc.margins.right,my); var ry=Math.floor((my-hc.cctop(data))/hc.ccheight(data)*data.colcols.dim[0]); var val=data.colcols.rows[ry]; if(val!==undefined) { evctx.textBaseline="top"; evctx.fillText("metadata: "+val,mx,my+3); } } evctx.stroke(); } }, false); evc.addEventListener('mouseout', function(evt) { evctx.clearRect(0,0,s.width,s.height); var v=detailPanel.getSize(); if(detailPanel.hasOwnProperty('evctx')) { detailPanel.evctx.clearRect(0,0,v.width,v.height); } }); }, listeners: { resize: function(cmp,width,height,oldWidth,oldHeight,opts) { cmp.redraw(cmp.pathcldata); } } }); var clusterPanelGearMenu = Ext.create('Ext.menu.Menu', { id: 'clusterGearMenu', style: { overflow: 'visible' // For the Combo popup }, items: [{ fieldLabel: 'Z limit', name: 'zlim', xtype: 'numberfield', value: -1, decimalPrecision: 3, minValue: 0.0, maxValue: 100, width: 200, disabled: false, tooltip: 'Set the range of overdispersion scores illustrated by colors', listeners : { change : {buffer: 800, fn:function(f,v) { clusterPanel.pathcldata.matrix.zlim=[-1*v,v]; clusterPanel.redraw() }} } } ], listeners:{ 'mouseleave': {buffer: 1000, fn:function( menu, e, eOpts){ menu.hide(); }} } }); var detailPanelGearMenu = Ext.create('Ext.menu.Menu', { id: 'detailGearMenu', style: { overflow: 'visible' // For the Combo popup }, items: [{ fieldLabel: 'N genes', xtype: 'numberfield', tooltip: 'Number of genes to show in the Expression Details panel', label: 'N genes', value: 20, minValue: 1, maxValue: 1000, disabled: true, listeners : { change : {buffer: 800, fn:function(f,v) {detailPanel.reload()}} } },{ fieldLabel: 'Trim', name: 'trim', xtype: 'numberfield', value: hc.trim, decimalPrecision: 5, minValue: 0.0, maxValue: 0.5, width: 200, maxValue: 100, disabled: true, tooltip: 'Winsorization trim fraction', listeners : { change : {buffer: 800, fn:function(f,v) {hc.trim=v; detailPanel.reload()}} } }, '-', { text: 'High/low genes', checked: false, tooltip: 'Whether to include genes from both sides of the PC loading (true) or just high magnitude (false)', disabled: true, listeners : { checkchange : function() {detailPanel.reload()} } }], listeners:{ 'mouseleave': {buffer: 1000, fn:function( menu, e, eOpts){ menu.hide(); }} } }); var ngenesSlider = Ext.create('Ext.slider.Single', { label: 'N genes', tip: 'number of genes to show', tipText: function(thumb){ return Ext.String.format('show {0} genes', thumb.value); }, width: 100, value: 20, increment: 1, minValue: 0, maxValue: 500, }); var viewport = Ext.create('Ext.Viewport', { layout: { type: 'border', padding: 5 }, defaults: { split: true }, items: [{ region: 'center', layout: 'border', items: [{ region: 'north', layout: 'fit', id: 'clusterPanel', title: 'Pathway Overdispersion', tools: [ { type:'gear', tooltip: 'Settings', handler: function(e, el,o,t) { clusterPanelGearMenu.showBy(t); } }, { type:'help', tooltip: 'Tutorial', handler: function(e, el,o,t) { tutorialWindow.show(); } }, { type:'save', tooltip: 'Save image', handler: function(e,el,o,t) { if($('#pathcl').length==0) { return; } var changingImage = Ext.create('Ext.Img', { src: $('#pathcl')[0].toDataURL("image/png",1.0) }); win = new Ext.Window({ title: 'exported image: use right click to save the image', layout: 'fit', autoScroll: true, modal: true, closeAction: 'hide', items:[changingImage] }); win.show(); } } ], minHeight: 200, height: 300, bodyPadding: 0, split: true, items:[clusterPanel] },{ region: 'center', layout: 'fit', id: 'expressionDetailsPane', minHeight: 100, collapsible: false, // headerPosition: 'bottom', title: 'Expression Details', tools: [ { type:'search', tooltip: 'Search for genes matching the current consensus pattern', handler: function(e, el,o,t) { detailPanel.searchSimilar(); } },{ type:'collapse', tooltip: 'Run GO enrichment analysis on the current gene set', handler: function(e, el,o,t) { if(detailPanel.hasOwnProperty('genecldata')) { // write out current gene set geinfostore.getProxy().setExtraParam("genes",JSON.stringify(detailPanel.genecldata.matrix.rows)) geinfostore.load(); infotab.child("#enrichmenttab").tab.show(); // show the info tab infotab.setActiveTab(3); } } },{ type:'gear', tooltip: 'Settings', handler: function(e, el,o,t) { detailPanelGearMenu.showBy(t); } }, { type:'save', tooltip: 'Save image', handler: function(e,el,o,t) { if($('#genecl').length==0) { return; } var changingImage = Ext.create('Ext.Img', { src: $('#genecl')[0].toDataURL("image/png",1.0) }); win = new Ext.Window({ title: 'exported image: use right click to save the image', layout: 'fit', autoScroll: true, modal: true, closeAction: 'hide', items:[changingImage] }); win.show(); } } ], header: true, items:[detailPanel], autoScroll: true, autoShow: true, /*listeners: { afterrender: function(panel) { console.log("boo"); var header=panel.getHeader(); header.insert(1,[ngenesSlider]); } }*/ }] },{ region: 'east', collapsible: true, title: 'Info', split: true, layout: 'fit', width: '30%', minWidth: 100, minHeight: 140, bodyPadding: 0, items:[infotab] }] }); if(Ext.util.Cookies.get("hidetutorial")==null) { tutorialWindow.show(); } }); ================================================ FILE: web/pathcl_canvas_1.1.js ================================================ Ext.require(['*']); d3.selection.prototype.moveToFront = function() { return this.each(function(){ this.parentNode.appendChild(this); }); }; Ext.onReady(function() { var cw; var currentPathCl=-1; // currently selected pathway cluster Ext.tip.QuickTipManager.init(); //Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider')); var clusterToolbar = Ext.create('Ext.toolbar.Toolbar', { items: [ { // xtype: 'button', // default for Toolbars text: 'Show' },'->',{ xtype : 'textfield', icon: 'preview.png', cls: 'x-btn-text-icon', fieldLabel: 'number of genes', labelStyle: 'white-space: nowrap;', name : 'nGenes', emptyText: 'number of genes to show' }] }); var tutorialWindow = new Ext.Window({ id:'tutorial-window', title: 'Video Tutorial', layout:'fit', activeItem: 0, defaults: {border:false}, bbar: Ext.create('Ext.toolbar.Toolbar', { padding: 5, items : [{ xtype: 'checkbox', boxLabel: 'do not automatically show this tutorial video on startup when I visit next time', checked: Ext.util.Cookies.get("hidetutorial")!=null, name: 'dontshowtutorial', listeners: { change: function(field, value) { if(value) { var now = new Date(); var expiry = new Date(now.getTime() + 365 * 24 * 60 * 60 * 1000); Ext.util.Cookies.set("hidetutorial",true,expiry) } else { console.log("clearing hidetutorial"); } } } },'->',{ xtype: 'button', text: 'Close', handler: function() { tutorialWindow.hide(); } } ] }), items : [{ id: "video", html: '' }] }); /* 2D embedding panel */ var embeddingPanel = Ext.create('Ext.panel.Panel', { layout: 'fit', bodyPadding:0, autoScroll: false, draw: function(cols) { if(!clusterPanel.pathcldata.hasOwnProperty('embedding')) { return; } if(cols===undefined) { // use last colcol row for colors cols=[]; if(clusterPanel.pathcldata.hasOwnProperty('colcols')) { var i=clusterPanel.pathcldata.colcols.dim[0]-1; for(var j=0; j 15) { s.height=15*data.matrix.dim[0]+hc.margins.bottom+hc.hmtop(data); } detailPanel.s=s; detailPanel.body.update('
') var gctx= $('#genecl')[0].getContext('2d'); if(data.hasOwnProperty('colcols')) { //colcols drawHeatmap(gctx,data.colcols,hc.hmleft(),hc.cctop(data),hc.hmwidth(s.width), hc.ccheight(data)); //rowcols drawHeatmap(gctx,data.rowcols,hc.margins.left,hc.hmtop(data),hc.rowcolWidth,hc.hmheight(data,s.height),false) } //main heatmap drawHeatmap(gctx,data.matrix,hc.hmleft(),hc.hmtop(data),hc.hmwidth(s.width),hc.hmheight(data,s.height),true) // event handling var evc=document.getElementById("geneclev"); var evctx= evc.getContext('2d'); detailPanel.evctx=evctx; try { evctx.setLineDash([5]); } catch (err) {} evctx.fillStyle='black'; evctx.font="bold 15px Arial"; var evtRect = evc.getBoundingClientRect(); evc.addEventListener('click', function(evt) { if(!clusterPanel.hasOwnProperty("pathcldata") || !clusterPanel.pathcldata.hasOwnProperty('embedding')) { return; } var mx=evt.clientX-evtRect.left; var my=evt.clientY-evtRect.top; if(my<=s.height-hc.margins.bottom && mx<=s.width-hc.margins.right && mx>=hc.hmleft() && my>=hc.cctop(data)) { if(my>=hc.hmtop(data)) { var ry=Math.floor((my-hc.hmtop(data))/hc.hmheight(data,s.height)*data.matrix.dim[0]); // update embedding embeddingPanel.recolor(heatmapRowColors(data.matrix,ry)); Ext.getCmp("embeddingDiv").setTitle("2D Embedding: gene "+data.matrix.rows[ry]) } else { var ry=Math.floor((my-hc.cctop(data))/hc.ccheight(data)*data.colcols.dim[0]); embeddingPanel.recolor(heatmapRowColors(data.colcols,ry)); Ext.getCmp("embeddingDiv").setTitle("2D Embedding: consensus pattern") } } }, false); evc.addEventListener('mousemove', function(evt) { var mx=evt.clientX-evtRect.left; var my=evt.clientY-evtRect.top; evctx.clearRect(0,0,s.width,s.height); var v=clusterPanel.s; clusterPanel.evctx.clearRect(0,0,v.width,v.height); if(my<=s.height-hc.margins.bottom && mx<=s.width-hc.margins.right && mx>=hc.hmleft() && my>=hc.cctop(data)) { //mx=hc.hmleft()+(rx+0.5)*hc.hmwidth(s.width)/data.matrix.dim[1]; evctx.beginPath(); evctx.moveTo(mx,hc.cctop(data)); evctx.lineTo(mx,s.height-hc.margins.bottom); var rx=Math.floor((mx-hc.hmleft())/hc.hmwidth(s.width)*data.matrix.dim[1]); // update the line in the cluster panel clusterPanel.evctx.beginPath(); clusterPanel.evctx.moveTo(mx,hc.cctop(clusterPanel.pathcldata)) clusterPanel.evctx.lineTo(mx,v.height-hc.margins.bottom) clusterPanel.evctx.stroke(); if(mx>s.width/2) { evctx.textAlign="end"; mx-=5; } else { evctx.textAlign="start"; mx+=5; } evctx.textBaseline="bottom"; evctx.fillText("cell: "+data.matrix.cols[rx],mx,my-3); d3.selectAll("#embedding circle.selected").classed("selected",false); d3.select("#cell"+rx).classed("selected",true).moveToFront(); if(my>=hc.hmtop(data)) { var ry=Math.floor((my-hc.hmtop(data))/hc.hmheight(data,s.height)*data.matrix.dim[0]); //my=hc.hmtop(data)+(ry+0.5)*hc.hmheight(data,s.height)/data.matrix.dim[0]; evctx.moveTo(hc.margins.left,my); evctx.lineTo(s.width-hc.margins.right,my); evctx.textBaseline="top"; evctx.fillText("gene: "+data.matrix.rows[ry],mx,my+3); } else { evctx.moveTo(hc.hmleft(),my); evctx.lineTo(s.width-hc.margins.right,my); } evctx.stroke(); } }, false); evc.addEventListener('mouseout', function(evt) { evctx.clearRect(0,0,s.width,s.height); var v=clusterPanel.s; clusterPanel.evctx.clearRect(0,0,v.width,v.height); d3.selectAll("#embedding circle.selected").classed("selected",false); }); }, listeners: { resize: function(cmp,width,height,oldWidth,oldHeight,opts) { cmp.redraw(); } } }) // clear filter filed trigger button Ext.define('Ext.ux.CustomTrigger', { extend: 'Ext.form.field.Trigger', alias: 'widget.customtrigger', initComponent: function () { var me = this; me.triggerCls = 'x-form-clear-trigger'; me.callParent(arguments); }, // override onTriggerClick onTriggerClick: function() { if(this.getValue()!='') { this.setRawValue(''); this.fireEvent('change',this,''); } } }); /* PATHWAY CLUSTER INFO */ Ext.define('clinfo',{ extend: 'Ext.data.Model', fields: [ 'name', 'id', {name: 'od',type: 'float'},{name: 'npc', type: 'integer'},{name: 'sign', type: 'integer'},{name: 'initsel', type: 'integer'} ], idProperty: 'id' }); var clinfostore = Ext.create('Ext.data.Store', { id: 'clinfostore', model: 'clinfo', remoteSort: true, proxy: { type: 'jsonp', url: 'clinfo.json', extraParams: { pathcl: currentPathCl }, reader: { root: 'genes', totalProperty: 'totalCount' }, simpleSortMode: true, }, autoLoad: false, pageSize: 50, remoteFilter: true, listeners: { load: function(r) { // use the supplied 'initsel' to set the initial selection clSelectModel.suspendEvent('selectionchange') for(var i=0;i0) detailPanel.showPathways(ids); } } }); var clInfoGrid = Ext.create('Ext.grid.Panel', { store: clinfostore, id: "clinfo", selModel: clSelectModel, height:'100%', columnLines:true, emptyText: 'No Matching Pathways', tbar: Ext.create('Ext.PagingToolbar', { store: clinfostore, displayInfo: false, //displayMsg: 'Displaying genes {0} - {1} of {2}', emptyMsg: "No pathways to display", items:[ { flex:1, width: 500, minWidth: 50, xtype: 'customtrigger', emptyText: 'filter by name...', listeners: { change: {buffer: 200, fn: function(field, value) { if (value.length>0) { clinfostore.clearFilter(true); clinfostore.filter({property: 'name', value: value}); } else { clinfostore.clearFilter(false); } }} } }], listeners: { afterrender: function() { this.down('#refresh').hide(); } } }), columns: [ //{text: "name", flex: 1, dataIndex: 'name', sortable: true}, { text: 'overdispersion', dataIndex: 'od', flex:1, renderer: function (v, m, r) { //m.tdAttr='data-qtip="'+r.data.name+'"'; var id = Ext.id(); Ext.defer(function () { Ext.widget('progressbar', { renderTo: id, text: r.data.name, value: v / 1, }); }, 50); if(r.data.sign=="1") { return Ext.String.format('
', id); } else { return Ext.String.format('
', id); } } }, {text: "PC", width: 30, dataIndex: 'npc', sortable: true}, /*{text: "overdispersion", width: 100, dataIndex: 'od', sortable: true}*/ ] }) /* GENE SET ENRICHMENT INFO */ Ext.define('geinfo',{ extend: 'Ext.data.Model', fields: [ 'name', 'id', {name: 'fe',type: 'float'},{name: 'o', type: 'integer'},{name: 'u', type: 'integer'},{name: 'Z', type: 'float'},{name: 'Za', type: 'float'} ], idProperty: 'id' }); var geinfostore = Ext.create('Ext.data.Store', { id: 'geinfostore', model: 'geinfo', remoteSort: true, proxy: { type: 'ajax', url: 'testenr.json', actionMethods: {create: 'POST', read: 'POST', update: 'POST', destroy: 'POST'}, reader: { root: 'genes', totalProperty: 'totalCount' }, simpleSortMode: true, }, autoLoad: false, pageSize: 50, remoteFilter: true, }); var geSelectModel = Ext.create('Ext.selection.CheckboxModel', { listeners: { selectionchange: function(sm, selections) { var ids = $.map(selections,function(val,i) { return(val.data.id) }) if(ids.length>0) detailPanel.showPathways(ids); } } }); var geInfoGrid = Ext.create('Ext.grid.Panel', { store: geinfostore, id: "geinfo", selModel: geSelectModel, height:'100%', columnLines:true, emptyText: 'No Enriched Pathways', tbar: Ext.create('Ext.PagingToolbar', { store: geinfostore, displayInfo: false, //displayMsg: 'Displaying genes {0} - {1} of {2}', emptyMsg: "No pathways to display", items:[ { flex:1, width: 500, minWidth: 50, xtype: 'customtrigger', emptyText: 'filter by name...', listeners: { change: {buffer: 200, fn: function(field, value) { if (value.length>0) { geinfostore.clearFilter(true); geinfostore.filter({property: 'name', value: value}); } else { geinfostore.clearFilter(false); } }} } }], listeners: { afterrender: function() { this.down('#refresh').hide(); } } }), columns: [ {text: "Pathway", flex: 1, dataIndex: 'name', sortable: true }, {text: "FE", width: 60, dataIndex: 'fe', sortable: true, tooltip: "fold enrichment"}, {text: "Z", width: 60, dataIndex: 'Z', sortable: true, tooltip: "enrichment Z-score"}, {text: "cZ", width: 60, dataIndex: 'Za', sortable: true, tooltip: "enrichment Z-score, corrected for multiple hypothesis testing"}, {text: "n", width: 50, dataIndex: 'n', sortable: true, hidden: true, tooltip: "number of genes found in this pathway"}, {text: "u", width: 50, dataIndex: 'u', sortable: true, hidden: true, tooltip: "total number of genes annotated for this pathway"} ] }) /* gene info tab */ Ext.define('ginfo',{ extend: 'Ext.data.Model', fields: [ 'gene', {name: 'var',type: 'float'},{name: 'svar', type: 'float'} ], idProperty: 'gene' }); var ginfostore = Ext.create('Ext.data.Store', { id: 'ginfostore', model: 'ginfo', remoteSort: true, proxy: { type: 'jsonp', url: 'genes.json', reader: { root: 'genes', totalProperty: 'totalCount' }, simpleSortMode: true, }, sorters: [{ property: 'var', direction: 'DESC' }], pageSize: 100, remoteFilter: true, autoLoad: true }); var geneSelModel = Ext.create('Ext.selection.CheckboxModel', { listeners: { selectionchange: function(sm, selections) { var ids = $.map(selections,function(val,i) { return(val.data.gene) }) detailPanel.showGenes(ids); //grid4.down('#removeButton').setDisabled(selections.length === 0); } } }); var geneGrid = Ext.create('Ext.grid.Panel', { store: ginfostore, selModel: geneSelModel, columns: [ {text: "Gene", flex: 1, dataIndex: 'gene', sortable: true, renderer: function(value) { return Ext.String.format('{1}',value,value) } }, {text: "Variance", width: 100, dataIndex: 'var', sortable: true} ], //features: [filters], height:'100%', columnLines:true, emptyText: 'No Matching Genes', //forceFit: true //renderTo:'example-grid', //width: 800, //height: 300 // paging bar on the bottom tbar: Ext.create('Ext.PagingToolbar', { store: ginfostore, displayInfo: false, //displayMsg: 'Displaying genes {0} - {1} of {2}', emptyMsg: "No genes to display", items:[ { flex:1, width: 500, minWidth: 50, xtype: 'customtrigger', emptyText: 'filter by gene name...', listeners: { change: {buffer: 600, fn: function(field, value) { if (value.length>0) { ginfostore.clearFilter(true); ginfostore.filter({property: 'gene', value: value}); } else { ginfostore.clearFilter(false); } }} } }], listeners: { afterrender: function() { this.down('#refresh').hide(); } } }), listeners: { viewready: function() { // select top 20 genes this.selModel.suspendEvent('selectionchange') for(var i=0;i0) { pinfostore.clearFilter(true); pinfostore.filter({property: 'name', value: value}); } else { pinfostore.clearFilter(false); } }} } }], listeners: { afterrender: function() { this.down('#refresh').hide(); } } }) }); var infotab = Ext.create('Ext.tab.Panel', { //tabPosition: 'right', defaults: { bodyPadding: 0, layout: 'fit', iconCls: 'tab-icon' }, items: [ { title: 'Pathways', items:[pathwayGrid] }, { title: 'Genes', items:[geneGrid] }, { title: 'Cluster', items:[clInfoGrid], itemId: 'clustertab', hidden:true }, { title: 'Enrichment', items:[geInfoGrid], itemId: 'enrichmenttab', hidden:true } ] }); // draw dendrogram in a given context // ctx - canvas 2d context // d - dendrogram structure (t(merge), order, height) // x,y,width,height - placement function drawDendrogram(ctx, d, x, y, width, height) { var nmerges=d.merge.length; var xstep=width/d.order.length; var lp,lpy,rp,rpy,jx,jy; var maxHeight=Math.max.apply(null, d.height); var heightScale=height/maxHeight; ctx.beginPath(); for(var i=0, mergex=[]; i=d.colors.length) { val=d.colors.length-1; } val=d.colors[Math.floor(val)]; cols.push(val) } } else { // interpret the values as colors for(var j=0; jd.dim[0]*maxRowHeight) { height=d.dim[0]*maxRowHeight;} var rowHeight=height/d.dim[0]; var colWidth=width/d.dim[1]; var mC=d.hasOwnProperty('colors'); // perform color mapping on the fly if(rowNames && !d.hasOwnProperty('rows')) { rowNames=false; } if(rowNames) { var fontSize=(rowHeight*0.95); if(fontSize>maxFontSize) { fontSize=maxFontSize; }; if(fontSize>=minFontSize) { ctx.font=fontSize+"px Arial"; ctx.textAlign='left'; ctx.textBaseline="middle"; } else { rowNames=false; } } var zlim=[]; if(mC) { if(d.hasOwnProperty('zlim')) { zlim=d.zlim; } else { zlim.push(-1*Math.max.apply(null,d.data.map(Math.abs))); zlim.push(-1*zlim[0]); } zlim.push(d.colors.length/(zlim[1]-zlim[0])); // zlim step } var val=0; for(var i=0; i=d.colors.length) { val=d.colors.length-1; } val=d.colors[Math.floor(val)]; } else { // interpret values as colors directly val=d.data[i*d.dim[1]+j] } ctx.fillStyle=val; ctx.fillRect(x+j*colWidth,y+i*rowHeight,colWidth,rowHeight); } if(rowNames) { ctx.fillStyle='black'; ctx.fillText(d.rows[i],x+width+3,y+(i+0.5)*rowHeight); } } ctx.lineWidth=1; ctx.strokeStyle='black'; ctx.strokeRect(x,y,width,height); //if(zlim!==undefined) {return(zlim[1])}; } function updatePathclInfo(pathcl) { if(pathcl == currentPathCl) return; clinfostore.getProxy().setExtraParam("pathcl",pathcl) clinfostore.load(); infotab.child("#clustertab").tab.show(); infotab.setActiveTab(2); infotab.getActiveTab().setTitle("Aspect "+pathcl) currentPathCl=pathcl; } /* heatmap config */ var hc = { spacing: 5, dendSpacing: 1, geneUnitHeight: 15, margins: {top:2,right:80,bottom:15,left:1}, colcolUnitHeight: 10, rowcolWidth: 10, colDendHeight: 50, trim: 0, // heatmap left position hmleft: function() { return(this.margins.left+this.rowcolWidth+this.spacing)}, // colcol top position cctop: function(data) { return(data.hasOwnProperty('coldend')? this.margins.top+this.colDendHeight+this.dendSpacing : this.margins.top) }, // colcol height ccheight: function(data) { return(this.colcolUnitHeight*data.colcols.dim[0]) }, // heatmap top position hmtop: function(data) { if(data.hasOwnProperty('colcols')) { return(data.hasOwnProperty('coldend')? this.margins.top+this.colDendHeight+this.spacing+this.dendSpacing + this.ccheight(data) : this.margins.top+this.spacing+this.dendSpacing + this.ccheight(data)) } else { return(data.hasOwnProperty('coldend')? this.margins.top+this.colDendHeight+this.spacing : this.margins.top) } }, // heatmap hight hmheight: function(data,totalHeight) { return((totalHeight-this.margins.bottom) - this.hmtop(data)); }, // heatmap width hmwidth: function(totalWidth) { return(totalWidth-this.margins.left-this.rowcolWidth-this.spacing-this.margins.right)}, rowlableft: function(totalWidth) { return(this.hmleft()+this.hmwidth(totalWidth)+this.spacing); }, rowlabelsize: function(data,totalHeight) { return(Math.round(this.hmheight(data,totalHeight)/data.matrix.dim[0]*0.97*72/96)) //return(18); } }; var clusterPanel = Ext.create('Ext.panel.Panel', { layout: 'fit', bodyPadding: 5, //ohtml: "Pathway Clustering", reload: function() { clusterPanel.setLoading(true); Ext.Ajax.request({ url: 'pathcl.json', method: 'POST', waitTitle: 'Connecting', waitMsg: 'Sending data...', scope:this, failure: function(r,o){console.log('failure:'); console.log(r);}, success: function(response) { var data = Ext.JSON.decode(response.responseText) if(data.matrix.dim[0]==1) data.matrix.rows=[data.matrix.rows] if(data.colcols.dim[0]==1) data.colcols.rows=[data.colcols.rows] clusterPanel.pathcldata=data; clusterPanelGearMenu.getComponent(0).suspendEvents(); clusterPanelGearMenu.getComponent(0).setValue(data.matrix.zlim[1]); //clusterPanelGearMenu.getComponent(0).setMaxValue(data.matrix.range[1]); clusterPanelGearMenu.getComponent(0).resumeEvents() if(data.hasOwnProperty('trim')) { hc.trim=data.trim; detailPanelGearMenu.getComponent(1).suspendEvents(); detailPanelGearMenu.getComponent(1).setValue(data.trim); detailPanelGearMenu.getComponent(1).resumeEvents(); } clusterPanel.redraw(clusterPanel.pathcldata); if(data.hasOwnProperty('embedding')) { Ext.getCmp("embeddingDiv").show(); embeddingPanel.draw(); } clusterPanel.setLoading(false); } }) }, redraw: function(data) { if(data === undefined) { if(clusterPanel.hasOwnProperty('pathcldata')) { data=clusterPanel.pathcldata; } else { clusterPanel.reload(); return; } } $('#pathcl').remove(); $('#pathclev').remove(); delete clusterPanel.evctx; $('#pclCD').remove(); //clusterPanel.update(""); var s=clusterPanel.getSize(); clusterPanel.s=s; clusterPanel.body.update('
') var ctx= $('#pathcl')[0].getContext('2d'); drawDendrogram(ctx,data.coldend,hc.hmleft(),hc.margins.top,hc.hmwidth(s.width),hc.colDendHeight); //colcols drawHeatmap(ctx,data.colcols,hc.hmleft(),hc.cctop(data),hc.hmwidth(s.width), hc.ccheight(data)); //rowcols drawHeatmap(ctx,data.rowcols,hc.margins.left,hc.hmtop(data),hc.rowcolWidth,hc.hmheight(data,s.height)) //main heatmap drawHeatmap(ctx,data.matrix,hc.hmleft(),hc.hmtop(data),hc.hmwidth(s.width),hc.hmheight(data,s.height),rowNames=true) // event handling var evc=document.getElementById("pathclev"); var evctx= evc.getContext('2d'); clusterPanel.evctx=evctx; //evctx.strokeRect(0,0,s.width-10,s.height-10); try { evctx.setLineDash([5]); } catch (err) {} evctx.fillStyle='black'; evctx.font="bold 15px Arial"; var evtRect = evc.getBoundingClientRect(); evc.addEventListener('click', function(evt) { var mx=evt.clientX-evtRect.left; var my=evt.clientY-evtRect.top; if(my<=s.height-hc.margins.bottom && mx<=s.width-hc.margins.right && mx>=hc.hmleft() && my>=hc.cctop(data)) { //if(my<=s.height-hc.margins.bottom && mx<=s.width-hc.margins.right && mx>=hc.hmleft()) { if(my>=hc.hmtop(data)) { var ry=Math.floor((my-hc.hmtop(data))/hc.hmheight(data,s.height)*data.matrix.dim[0]); evctx.strokeStyle='red'; evctx.lineWidth=2; try { evctx.setLineDash([0]); } catch(err) {} //evctx.strokeRect(hc.hmleft(),hc.hmtop(data)+ry*hc.hmheight(data,s.height)/data.matrix.dim[0],hc.hmwidth(s.width),hc.hmheight(data,s.height)/data.matrix.dim[0]); evctx.strokeRect(0,hc.hmtop(data)+ry*hc.hmheight(data,s.height)/data.matrix.dim[0],s.width,hc.hmheight(data,s.height)/data.matrix.dim[0]); evctx.strokeStyle='black'; evctx.lineWidth=1; try { evctx.setLineDash([5]); } catch(err) {} updatePathclInfo(data.matrix.rows[ry]) // update embedding if(data.hasOwnProperty('embedding')) { embeddingPanel.recolor(heatmapRowColors(data.matrix,ry)); Ext.getCmp("embeddingDiv").setTitle("2D Embedding: aspect "+data.matrix.rows[ry]); } } else { if(data.hasOwnProperty('embedding')) { var ry=Math.floor((my-hc.cctop(data))/hc.ccheight(data)*data.colcols.dim[0]); embeddingPanel.recolor(heatmapRowColors(data.colcols,ry)); Ext.getCmp("embeddingDiv").setTitle("2D Embedding: metadata "+data.colcols.rows[ry]) } } } }, false); evc.addEventListener('mousemove', function(evt) { var mx=evt.clientX-evtRect.left; var my=evt.clientY-evtRect.top; evctx.clearRect(0,0,s.width,s.height); if(detailPanel.hasOwnProperty('evctx')) { var v=detailPanel.s; detailPanel.evctx.clearRect(0,0,v.width,v.height); } if(my<=s.height-hc.margins.bottom && mx<=s.width-hc.margins.right && mx>=hc.hmleft() && my>=hc.cctop(data)) { //mx=hc.hmleft()+(rx+0.5)*hc.hmwidth(s.width)/data.matrix.dim[1]; evctx.beginPath(); evctx.moveTo(mx,hc.cctop(data)); evctx.lineTo(mx,s.height-hc.margins.bottom); // update the line in the gene panel if(detailPanel.hasOwnProperty('evctx')) { detailPanel.evctx.beginPath(); detailPanel.evctx.moveTo(mx,hc.cctop(detailPanel.genecldata)) detailPanel.evctx.lineTo(mx,v.height-hc.margins.bottom) detailPanel.evctx.stroke(); } var rx=Math.floor((mx-hc.hmleft())/hc.hmwidth(s.width)*data.matrix.dim[1]); if(mx>s.width/2) { evctx.textAlign="end"; mx-=5; } else { evctx.textAlign="start"; mx+=5; } evctx.textBaseline="bottom"; evctx.fillText("cell: "+data.matrix.cols[rx],mx,my-3); if(data.hasOwnProperty('embedding')) { d3.selectAll("#embedding circle.selected").classed("selected",false); d3.select("#cell"+rx).classed("selected",true).moveToFront(); } if(my>=hc.hmtop(data)) { var ry=Math.floor((my-hc.hmtop(data))/hc.hmheight(data,s.height)*data.matrix.dim[0]); //my=hc.hmtop(data)+(ry+0.5)*hc.hmheight(data,s.height)/data.matrix.dim[0]; evctx.moveTo(hc.margins.left,my); evctx.lineTo(s.width-hc.margins.right,my); //evctx.fillText("cell: "+data.matrix.cols[rx],mx+5,my-5); evctx.textBaseline="top"; evctx.fillText("aspect: "+data.matrix.rows[ry],mx,my+3); } else { evctx.moveTo(hc.hmleft(),my); evctx.lineTo(s.width-hc.margins.right,my); var ry=Math.floor((my-hc.cctop(data))/hc.ccheight(data)*data.colcols.dim[0]); var val=data.colcols.rows[ry]; if(val!==undefined) { evctx.textBaseline="top"; evctx.fillText("metadata: "+val,mx,my+3); } } evctx.stroke(); } }, false); evc.addEventListener('mouseout', function(evt) { evctx.clearRect(0,0,s.width,s.height); var v=detailPanel.getSize(); if(detailPanel.hasOwnProperty('evctx')) { detailPanel.evctx.clearRect(0,0,v.width,v.height); } d3.selectAll("#embedding circle.selected").classed("selected",false); }); }, listeners: { resize: function(cmp,width,height,oldWidth,oldHeight,opts) { cmp.redraw(cmp.pathcldata); } } }); var clusterPanelGearMenu = Ext.create('Ext.menu.Menu', { id: 'clusterGearMenu', style: { overflow: 'visible' // For the Combo popup }, items: [{ fieldLabel: 'Z limit', name: 'zlim', xtype: 'numberfield', value: -1, decimalPrecision: 3, minValue: 0.0, maxValue: 100, width: 200, disabled: false, tooltip: 'Set the range of overdispersion scores illustrated by colors', listeners : { change : {buffer: 800, fn:function(f,v) { clusterPanel.pathcldata.matrix.zlim=[-1*v,v]; clusterPanel.redraw() }} } } ], listeners:{ 'mouseleave': {buffer: 1000, fn:function( menu, e, eOpts){ menu.hide(); }} } }); var detailPanelGearMenu = Ext.create('Ext.menu.Menu', { id: 'detailGearMenu', style: { overflow: 'visible' // For the Combo popup }, items: [{ fieldLabel: 'N genes', xtype: 'numberfield', tooltip: 'Number of genes to show in the Expression Details panel', label: 'N genes', value: 20, minValue: 1, maxValue: 1000, disabled: true, listeners : { change : {buffer: 800, fn:function(f,v) {detailPanel.reload()}} } },{ fieldLabel: 'Trim', name: 'trim', xtype: 'numberfield', value: hc.trim, decimalPrecision: 5, minValue: 0.0, maxValue: 0.5, width: 200, maxValue: 100, disabled: true, tooltip: 'Winsorization trim fraction', listeners : { change : {buffer: 800, fn:function(f,v) {hc.trim=v; detailPanel.reload()}} } }, '-', { text: 'High/low genes', checked: false, tooltip: 'Whether to include genes from both sides of the PC loading (true) or just high magnitude (false)', disabled: true, listeners : { checkchange : function() {detailPanel.reload()} } }], listeners:{ 'mouseleave': {buffer: 1000, fn:function( menu, e, eOpts){ menu.hide(); }} } }); var ngenesSlider = Ext.create('Ext.slider.Single', { label: 'N genes', tip: 'number of genes to show', tipText: function(thumb){ return Ext.String.format('show {0} genes', thumb.value); }, width: 100, value: 20, increment: 1, minValue: 0, maxValue: 500, }); var viewport = Ext.create('Ext.Viewport', { layout: { type: 'border', padding: 5 }, defaults: { split: true }, items: [{ region: 'center', layout: 'border', items: [{ region: 'north', layout: 'fit', id: 'clusterPanel', title: 'Pathway Overdispersion', tools: [ { type:'gear', tooltip: 'Settings', handler: function(e, el,o,t) { clusterPanelGearMenu.showBy(t); } }, { type:'help', tooltip: 'Tutorial', handler: function(e, el,o,t) { tutorialWindow.show(); } }, { type:'save', tooltip: 'Save image', handler: function(e,el,o,t) { if($('#pathcl').length==0) { return; } var changingImage = Ext.create('Ext.Img', { src: $('#pathcl')[0].toDataURL("image/png",1.0) }); win = new Ext.Window({ title: 'exported image: use right click to save the image', layout: 'fit', autoScroll: true, modal: true, closeAction: 'hide', items:[changingImage] }); win.show(); } } ], minHeight: 200, height: 300, bodyPadding: 0, split: true, items:[clusterPanel] },{ region: 'center', layout: 'fit', id: 'expressionDetailsPane', minHeight: 100, collapsible: false, // headerPosition: 'bottom', title: 'Expression Details', tools: [ { type:'search', tooltip: 'Search for genes matching the current consensus pattern', handler: function(e, el,o,t) { detailPanel.searchSimilar(); } },{ type:'collapse', tooltip: 'Run GO enrichment analysis on the current gene set', handler: function(e, el,o,t) { if(detailPanel.hasOwnProperty('genecldata')) { // write out current gene set geinfostore.getProxy().setExtraParam("genes",JSON.stringify(detailPanel.genecldata.matrix.rows)) geinfostore.load(); infotab.child("#enrichmenttab").tab.show(); // show the info tab infotab.setActiveTab(3); } } },{ type:'gear', tooltip: 'Settings', handler: function(e, el,o,t) { detailPanelGearMenu.showBy(t); } }, { type:'save', tooltip: 'Save image', handler: function(e,el,o,t) { if($('#genecl').length==0) { return; } var changingImage = Ext.create('Ext.Img', { src: $('#genecl')[0].toDataURL("image/png",1.0) }); win = new Ext.Window({ title: 'exported image: use right click to save the image', layout: 'fit', autoScroll: true, modal: true, closeAction: 'hide', items:[changingImage] }); win.show(); } } ], header: true, items:[detailPanel], autoScroll: true, autoShow: true, /*listeners: { afterrender: function(panel) { console.log("boo"); var header=panel.getHeader(); header.insert(1,[ngenesSlider]); } }*/ }] },{ region: 'east', collapsible: true, split: true, layout: 'border', width: '30%', title:'Info', bodyPadding: 0, items:[{ region:'center', layout:'fit', minWidth: 100, minHeight: 140, bodyPadding: 0, items:[infotab] },{ title:'2D Embedding', id:'embeddingDiv', region:'south', layout: 'fit', minWidth: 100, minHeight: 100, height:'40%', hidden:true, collapsible: true, split:true, bodyPadding: 0, tools:[{ type:'save', tooltip: 'Save image', handler: function(e,el,o,t) { var svge=d3.select("#embedding"); if(!svge.empty()) { var html=svge.attr("version", 1.1) .attr("xmlns", "http://www.w3.org/2000/svg") .node().parentNode.innerHTML; var imgsrc = 'data:image/svg+xml;base64,'+ btoa(html); win = new Ext.Window({ title: 'exported image: use the link below save the image', layout: 'fit', html: 'link' }); win.show(); } } }], items:[embeddingPanel] }] }] }); if(Ext.util.Cookies.get("hidetutorial")==null) { tutorialWindow.show(); } }); // google analytics code (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-33018606-2', 'auto'); ga('send', 'pageview');