diff --git a/DESCRIPTION b/DESCRIPTION index 59210a4..247a5ba 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,15 +15,34 @@ Authors@R: c(person("Vladimir", "Kiselev", role=c("aut"))) Description: A tool for unsupervised clustering and analysis of single cell RNA-Seq data. License: GPL-3 -Imports: graphics, stats, utils, methods, e1071, parallel, foreach, - doParallel, doRNG, shiny, ggplot2, pheatmap (>= 1.0.8), - ROCR, robustbase, rrcov, cluster, WriteXLS, - Rcpp (>= 0.11.1), SummarizedExperiment, SingleCellExperiment, - BiocGenerics, S4Vectors +Imports: + graphics, + stats, + utils, + methods, + e1071, + parallel, + foreach, + doParallel, + doRNG, + shiny, + ggplot2, + pheatmap (>= 1.0.8), + ROCR, + robustbase, + rrcov, + cluster, + WriteXLS, + Rcpp (>= 0.11.1), + SummarizedExperiment, + SingleCellExperiment, + BiocGenerics, + S4Vectors, + Matrix Depends: R(>= 3.3) LinkingTo: Rcpp, RcppArmadillo LazyData: TRUE -RoxygenNote: 6.0.1 +RoxygenNote: 6.1.1 Suggests: knitr, rmarkdown, mclust, scater VignetteBuilder: knitr biocViews: SingleCell, Software, Classification, Clustering, DimensionReduction, diff --git a/NAMESPACE b/NAMESPACE index 3b908c9..a0b7bb8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(get_marker_genes) export(get_outl_cells) export(get_processed_dataset) export(norm_laplacian) +export(organise_marker_genes) export(reindex_clusters) export(sc3) export(sc3_calc_biology) @@ -54,6 +55,7 @@ importFrom(ggplot2,ylim) importFrom(graphics,plot) importFrom(methods,as) importFrom(methods,new) +importFrom(parallel,clusterCall) importFrom(parallel,detectCores) importFrom(parallel,makeCluster) importFrom(parallel,stopCluster) diff --git a/R/CoreFunctions.R b/R/CoreFunctions.R index a099d82..2a0c48d 100644 --- a/R/CoreFunctions.R +++ b/R/CoreFunctions.R @@ -30,6 +30,9 @@ #' @importFrom Rcpp sourceCpp #' calculate_distance <- function(data, method) { + if(class(data) == "dgCMatrix"){ + data = as.matrix(data) + } return(if (method == "spearman") { as.matrix(1 - cor(data, method = "spearman")) } else if (method == "pearson") { diff --git a/R/CoreMethods.R b/R/CoreMethods.R index 7729610..812ad3e 100644 --- a/R/CoreMethods.R +++ b/R/CoreMethods.R @@ -149,7 +149,7 @@ sc3_prepare.SingleCellExperiment <- function(object, gene_filter, pct_dropout_mi f_data <- rowData(object) f_data$sc3_gene_filter <- TRUE if (gene_filter) { - dropouts <- rowSums(counts(object) == 0)/ncol(object)*100 + dropouts <- Matrix::rowSums(counts(object) == 0)/ncol(object)*100 if(!is.null(isSpike(object))) { f_data$sc3_gene_filter <- dropouts < pct_dropout_max & dropouts > pct_dropout_min & !isSpike(object) } else { @@ -291,7 +291,7 @@ setMethod("sc3_estimate_k", signature(object = "SingleCellExperiment"), sc3_esti #' #' @importFrom doRNG %dorng% #' @importFrom foreach foreach %dopar% -#' @importFrom parallel makeCluster stopCluster +#' @importFrom parallel makeCluster stopCluster clusterCall #' @importFrom doParallel registerDoParallel sc3_calc_dists.SingleCellExperiment <- function(object) { dataset <- get_processed_dataset(object) @@ -317,6 +317,9 @@ sc3_calc_dists.SingleCellExperiment <- function(object) { cl <- parallel::makeCluster(n_cores, outfile = "") doParallel::registerDoParallel(cl, cores = n_cores) + ## pass .libPaths to workers + parallel::clusterCall(cl, function(x) {.libPaths(.Library); .libPaths(x)}, .libPaths()) + # calculate distances in parallel dists <- foreach::foreach(i = distances) %dorng% { try({ @@ -358,7 +361,7 @@ setMethod("sc3_calc_dists", signature(object = "SingleCellExperiment"), sc3_calc #' #' @importFrom doRNG %dorng% #' @importFrom foreach foreach -#' @importFrom parallel makeCluster stopCluster +#' @importFrom parallel makeCluster stopCluster clusterCall #' @importFrom doParallel registerDoParallel sc3_calc_transfs.SingleCellExperiment <- function(object) { dists <- metadata(object)$sc3$distances @@ -388,6 +391,9 @@ sc3_calc_transfs.SingleCellExperiment <- function(object) { cl <- parallel::makeCluster(n_cores, outfile = "") doParallel::registerDoParallel(cl, cores = n_cores) + ## pass .libPaths to workers + parallel::clusterCall(cl, function(x) {.libPaths(.Library); .libPaths(x)}, .libPaths()) + # calculate the 6 distinct transformations in parallel transfs <- foreach::foreach(i = 1:nrow(hash.table)) %dorng% { try({ @@ -431,7 +437,7 @@ setMethod("sc3_calc_transfs", signature(object = "SingleCellExperiment"), sc3_ca #' #' @importFrom doRNG %dorng% #' @importFrom foreach foreach -#' @importFrom parallel makeCluster stopCluster +#' @importFrom parallel makeCluster stopCluster clusterCall #' @importFrom doParallel registerDoParallel #' @importFrom utils setTxtProgressBar txtProgressBar #' @importFrom stats kmeans @@ -464,6 +470,9 @@ sc3_kmeans.SingleCellExperiment <- function(object, ks) { cl <- parallel::makeCluster(n_cores, outfile = "") doParallel::registerDoParallel(cl, cores = n_cores) + ## pass .libPaths to workers + parallel::clusterCall(cl, function(x) {.libPaths(.Library); .libPaths(x)}, .libPaths()) + pb <- utils::txtProgressBar(min = 1, max = nrow(hash.table), style = 3) # calculate the 6 distinct transformations in parallel @@ -515,7 +524,7 @@ setMethod("sc3_kmeans", signature(object = "SingleCellExperiment"), sc3_kmeans.S #' #' @importFrom doRNG %dorng% #' @importFrom foreach foreach -#' @importFrom parallel makeCluster stopCluster +#' @importFrom parallel makeCluster stopCluster clusterCall #' @importFrom doParallel registerDoParallel #' @import cluster #' @importFrom stats hclust dist as.dist @@ -545,6 +554,9 @@ sc3_calc_consens.SingleCellExperiment <- function(object) { cl <- parallel::makeCluster(n_cores, outfile = "") doParallel::registerDoParallel(cl, cores = n_cores) + ## pass .libPaths to workers + parallel::clusterCall(cl, function(x) {.libPaths(.Library); .libPaths(x)}, .libPaths()) + cons <- foreach::foreach(i = ks) %dorng% { try({ d <- k.means[grep(paste0("_", i, "_"), names(k.means))] @@ -644,7 +656,7 @@ setMethod("sc3_calc_consens", signature(object = "SingleCellExperiment"), sc3_ca #' #' @importFrom doRNG %dorng% #' @importFrom foreach foreach -#' @importFrom parallel makeCluster stopCluster +#' @importFrom parallel makeCluster stopCluster clusterCall #' @importFrom doParallel registerDoParallel #' @importFrom methods as sc3_calc_biology.SingleCellExperiment <- function(object, ks, regime) { @@ -695,6 +707,9 @@ sc3_calc_biology.SingleCellExperiment <- function(object, ks, regime) { cl <- parallel::makeCluster(n_cores, outfile = "") doParallel::registerDoParallel(cl, cores = n_cores) + ## pass .libPaths to workers + parallel::clusterCall(cl, function(x) {.libPaths(.Library); .libPaths(x)}, .libPaths()) + biol <- foreach::foreach(i = 1:nrow(hash.table)) %dorng% { try({ get_biolgy(dataset, clusts[, paste0("sc3_", hash.table[i, 1], "_clusters")], hash.table[i, 2]) diff --git a/man/prepare_for_svm.Rd b/man/prepare_for_svm.Rd index ba7ac69..f997bba 100644 --- a/man/prepare_for_svm.Rd +++ b/man/prepare_for_svm.Rd @@ -4,7 +4,8 @@ \alias{prepare_for_svm} \title{A helper function for the SVM analysis} \usage{ -prepare_for_svm(N, svm_num_cells = NULL, svm_train_inds = NULL, svm_max) +prepare_for_svm(N, svm_num_cells = NULL, svm_train_inds = NULL, + svm_max) } \arguments{ \item{N}{number of cells in the input dataset} diff --git a/man/sc3.Rd b/man/sc3.Rd index ca4c06f..4d8f812 100644 --- a/man/sc3.Rd +++ b/man/sc3.Rd @@ -5,20 +5,19 @@ \alias{sc3} \alias{sc3.SingleCellExperiment} \alias{sc3,SingleCellExperiment-method} -\alias{sc3} \title{Run all steps of \code{SC3} in one go} \usage{ sc3.SingleCellExperiment(object, ks, gene_filter, pct_dropout_min, - pct_dropout_max, d_region_min, d_region_max, svm_num_cells, svm_train_inds, - svm_max, n_cores, kmeans_nstart, kmeans_iter_max, k_estimator, biology, - rand_seed) - -\S4method{sc3}{SingleCellExperiment}(object, ks = NULL, gene_filter = TRUE, - pct_dropout_min = 10, pct_dropout_max = 90, d_region_min = 0.04, - d_region_max = 0.07, svm_num_cells = NULL, svm_train_inds = NULL, - svm_max = 5000, n_cores = NULL, kmeans_nstart = NULL, - kmeans_iter_max = 1e+09, k_estimator = FALSE, biology = FALSE, - rand_seed = 1) + pct_dropout_max, d_region_min, d_region_max, svm_num_cells, + svm_train_inds, svm_max, n_cores, kmeans_nstart, kmeans_iter_max, + k_estimator, biology, rand_seed) + +\S4method{sc3}{SingleCellExperiment}(object, ks = NULL, + gene_filter = TRUE, pct_dropout_min = 10, pct_dropout_max = 90, + d_region_min = 0.04, d_region_max = 0.07, svm_num_cells = NULL, + svm_train_inds = NULL, svm_max = 5000, n_cores = NULL, + kmeans_nstart = NULL, kmeans_iter_max = 1e+09, k_estimator = FALSE, + biology = FALSE, rand_seed = 1) } \arguments{ \item{object}{an object of \code{SingleCellExperiment} class.} diff --git a/man/sc3_calc_biology.Rd b/man/sc3_calc_biology.Rd index ee02197..57da9f2 100644 --- a/man/sc3_calc_biology.Rd +++ b/man/sc3_calc_biology.Rd @@ -6,8 +6,6 @@ \alias{sc3_calc_biology.SingleCellExperiment} \alias{sc3_calc_biology,} \alias{sc3_calc_biology,SingleCellExperiment-method} -\alias{sc3_calc_biology,SingleCellExperiment-method} -\alias{sc3_calc_biology} \title{Calculate DE genes, marker genes and cell outliers.} \usage{ sc3_calc_biology.SingleCellExperiment(object, ks, regime) diff --git a/man/sc3_calc_consens.Rd b/man/sc3_calc_consens.Rd index 7a7c4b1..9d98189 100644 --- a/man/sc3_calc_consens.Rd +++ b/man/sc3_calc_consens.Rd @@ -6,8 +6,6 @@ \alias{sc3_calc_consens.SingleCellExperiment} \alias{sc3_calc_consens,} \alias{sc3_calc_consens,SingleCellExperiment-method} -\alias{sc3_calc_consens,SingleCellExperiment-method} -\alias{sc3_calc_consens} \title{Calculate consensus matrix.} \usage{ sc3_calc_consens.SingleCellExperiment(object) diff --git a/man/sc3_calc_dists.Rd b/man/sc3_calc_dists.Rd index 53337a0..7e8de77 100644 --- a/man/sc3_calc_dists.Rd +++ b/man/sc3_calc_dists.Rd @@ -6,8 +6,6 @@ \alias{sc3_calc_dists.SingleCellExperiment} \alias{sc3_calc_dists,} \alias{sc3_calc_dists,SingleCellExperiment-method} -\alias{sc3_calc_dists,SingleCellExperiment-method} -\alias{sc3_calc_dists} \title{Calculate distances between the cells.} \usage{ sc3_calc_dists.SingleCellExperiment(object) diff --git a/man/sc3_calc_transfs.Rd b/man/sc3_calc_transfs.Rd index 04b8a37..a2765b4 100644 --- a/man/sc3_calc_transfs.Rd +++ b/man/sc3_calc_transfs.Rd @@ -6,8 +6,6 @@ \alias{sc3_calc_transfs.SingleCellExperiment} \alias{sc3_calc_transfs,} \alias{sc3_calc_transfs,SingleCellExperiment-method} -\alias{sc3_calc_transfs,SingleCellExperiment-method} -\alias{sc3_calc_transfs} \title{Calculate transformations of the distance matrices.} \usage{ sc3_calc_transfs.SingleCellExperiment(object) diff --git a/man/sc3_estimate_k.Rd b/man/sc3_estimate_k.Rd index 46dc3e6..3de1111 100644 --- a/man/sc3_estimate_k.Rd +++ b/man/sc3_estimate_k.Rd @@ -5,8 +5,6 @@ \alias{sc3_estimate_k} \alias{sc3_estimate_k.SingleCellExperiment} \alias{sc3_estimate_k,SingleCellExperiment-method} -\alias{sc3_estimate_k,SingleCellExperiment-method} -\alias{sc3_estimate_k} \title{Estimate the optimal number of cluster \code{k} for a scRNA-Seq expression matrix} \usage{ sc3_estimate_k.SingleCellExperiment(object) diff --git a/man/sc3_export_results_xls.Rd b/man/sc3_export_results_xls.Rd index 6de31ef..a4d6aa1 100644 --- a/man/sc3_export_results_xls.Rd +++ b/man/sc3_export_results_xls.Rd @@ -5,7 +5,6 @@ \alias{sc3_export_results_xls} \alias{sc3_export_results_xls.SingleCellExperiment} \alias{sc3_export_results_xls,SingleCellExperiment-method} -\alias{sc3_export_results_xls} \title{Write \code{SC3} results to Excel file} \usage{ sc3_export_results_xls.SingleCellExperiment(object, filename) diff --git a/man/sc3_interactive.Rd b/man/sc3_interactive.Rd index f2434b1..a633c39 100644 --- a/man/sc3_interactive.Rd +++ b/man/sc3_interactive.Rd @@ -6,8 +6,6 @@ \alias{sc3_interactive.SingleCellExperiment} \alias{sc3_interactive,} \alias{sc3_interactive,SingleCellExperiment-method} -\alias{sc3_interactive,SingleCellExperiment-method} -\alias{sc3_interactive} \title{Opens \code{SC3} results in an interactive session in a web browser.} \usage{ sc3_interactive.SingleCellExperiment(object) diff --git a/man/sc3_kmeans.Rd b/man/sc3_kmeans.Rd index aa48727..f3cfde6 100644 --- a/man/sc3_kmeans.Rd +++ b/man/sc3_kmeans.Rd @@ -6,8 +6,6 @@ \alias{sc3_kmeans.SingleCellExperiment} \alias{sc3_kmeans,} \alias{sc3_kmeans,SingleCellExperiment-method} -\alias{sc3_kmeans,SingleCellExperiment-method} -\alias{sc3_kmeans} \title{\code{kmeans} clustering of cells.} \usage{ sc3_kmeans.SingleCellExperiment(object, ks) diff --git a/man/sc3_plot_cluster_stability.Rd b/man/sc3_plot_cluster_stability.Rd index 3bb2b41..1132ffc 100644 --- a/man/sc3_plot_cluster_stability.Rd +++ b/man/sc3_plot_cluster_stability.Rd @@ -6,8 +6,6 @@ \alias{sc3_plot_cluster_stability.SingleCellExperiment} \alias{sc3_plot_cluster_stability,} \alias{sc3_plot_cluster_stability,SingleCellExperiment-method} -\alias{sc3_plot_cluster_stability,SingleCellExperiment-method} -\alias{sc3_plot_cluster_stability} \title{Plot stability of the clusters} \usage{ sc3_plot_cluster_stability.SingleCellExperiment(object, k) diff --git a/man/sc3_plot_consensus.Rd b/man/sc3_plot_consensus.Rd index b359342..10e8afa 100644 --- a/man/sc3_plot_consensus.Rd +++ b/man/sc3_plot_consensus.Rd @@ -6,8 +6,6 @@ \alias{sc3_plot_consensus.SingleCellExperiment} \alias{sc3_plot_consensus,} \alias{sc3_plot_consensus,SingleCellExperiment-method} -\alias{sc3_plot_consensus,SingleCellExperiment-method} -\alias{sc3_plot_consensus} \title{Plot consensus matrix as a heatmap} \usage{ sc3_plot_consensus.SingleCellExperiment(object, k, show_pdata) diff --git a/man/sc3_plot_de_genes.Rd b/man/sc3_plot_de_genes.Rd index 8e7d97a..0627f4a 100644 --- a/man/sc3_plot_de_genes.Rd +++ b/man/sc3_plot_de_genes.Rd @@ -6,14 +6,12 @@ \alias{sc3_plot_de_genes.SingleCellExperiment} \alias{sc3_plot_de_genes,} \alias{sc3_plot_de_genes,SingleCellExperiment-method} -\alias{sc3_plot_de_genes,SingleCellExperiment-method} -\alias{sc3_plot_de_genes} \title{Plot expression of DE genes of the clusters identified by \code{SC3} as a heatmap} \usage{ sc3_plot_de_genes.SingleCellExperiment(object, k, p.val, show_pdata) -\S4method{sc3_plot_de_genes}{SingleCellExperiment}(object, k, p.val = 0.01, - show_pdata = NULL) +\S4method{sc3_plot_de_genes}{SingleCellExperiment}(object, k, + p.val = 0.01, show_pdata = NULL) } \arguments{ \item{object}{an object of 'SingleCellExperiment' class} diff --git a/man/sc3_plot_expression.Rd b/man/sc3_plot_expression.Rd index b728c28..0745b60 100644 --- a/man/sc3_plot_expression.Rd +++ b/man/sc3_plot_expression.Rd @@ -6,8 +6,6 @@ \alias{sc3_plot_expression.SingleCellExperiment} \alias{sc3_plot_expression,} \alias{sc3_plot_expression,SingleCellExperiment-method} -\alias{sc3_plot_expression,SingleCellExperiment-method} -\alias{sc3_plot_expression} \title{Plot expression matrix used for SC3 clustering as a heatmap} \usage{ sc3_plot_expression.SingleCellExperiment(object, k, show_pdata) diff --git a/man/sc3_plot_markers.Rd b/man/sc3_plot_markers.Rd index 50a0028..98b3698 100644 --- a/man/sc3_plot_markers.Rd +++ b/man/sc3_plot_markers.Rd @@ -6,14 +6,12 @@ \alias{sc3_plot_markers.SingleCellExperiment} \alias{sc3_plot_markers,} \alias{sc3_plot_markers,SingleCellExperiment-method} -\alias{sc3_plot_markers,SingleCellExperiment-method} -\alias{sc3_plot_markers} \title{Plot expression of marker genes identified by \code{SC3} as a heatmap.} \usage{ sc3_plot_markers.SingleCellExperiment(object, k, auroc, p.val, show_pdata) -\S4method{sc3_plot_markers}{SingleCellExperiment}(object, k, auroc = 0.85, - p.val = 0.01, show_pdata = NULL) +\S4method{sc3_plot_markers}{SingleCellExperiment}(object, k, + auroc = 0.85, p.val = 0.01, show_pdata = NULL) } \arguments{ \item{object}{an object of 'SingleCellExperiment' class} diff --git a/man/sc3_plot_silhouette.Rd b/man/sc3_plot_silhouette.Rd index 0f0f0c9..8cffa8c 100644 --- a/man/sc3_plot_silhouette.Rd +++ b/man/sc3_plot_silhouette.Rd @@ -6,8 +6,6 @@ \alias{sc3_plot_silhouette.SingleCellExperiment} \alias{sc3_plot_silhouette,} \alias{sc3_plot_silhouette,SingleCellExperiment-method} -\alias{sc3_plot_silhouette,SingleCellExperiment-method} -\alias{sc3_plot_silhouette} \title{Plot silhouette indexes of the cells} \usage{ sc3_plot_silhouette.SingleCellExperiment(object, k) diff --git a/man/sc3_prepare.Rd b/man/sc3_prepare.Rd index c24bf56..aaa95d8 100644 --- a/man/sc3_prepare.Rd +++ b/man/sc3_prepare.Rd @@ -5,13 +5,12 @@ \alias{sc3_prepare} \alias{sc3_prepare.SingleCellExperiment} \alias{sc3_prepare,SingleCellExperiment-method} -\alias{sc3_prepare,SingleCellExperiment-method} -\alias{sc3_prepare} \title{Prepare the \code{SingleCellExperiment} object for \code{SC3} clustering.} \usage{ sc3_prepare.SingleCellExperiment(object, gene_filter, pct_dropout_min, - pct_dropout_max, d_region_min, d_region_max, svm_num_cells, svm_train_inds, - svm_max, n_cores, kmeans_nstart, kmeans_iter_max, rand_seed) + pct_dropout_max, d_region_min, d_region_max, svm_num_cells, + svm_train_inds, svm_max, n_cores, kmeans_nstart, kmeans_iter_max, + rand_seed) \S4method{sc3_prepare}{SingleCellExperiment}(object, gene_filter = TRUE, pct_dropout_min = 10, pct_dropout_max = 90, d_region_min = 0.04, diff --git a/man/sc3_run_svm.Rd b/man/sc3_run_svm.Rd index 11bd707..7b553b7 100644 --- a/man/sc3_run_svm.Rd +++ b/man/sc3_run_svm.Rd @@ -6,8 +6,6 @@ \alias{sc3_run_svm.SingleCellExperiment} \alias{sc3_run_svm,} \alias{sc3_run_svm,SingleCellExperiment-method} -\alias{sc3_run_svm,SingleCellExperiment-method} -\alias{sc3_run_svm} \title{Run the hybrid \code{SVM} approach.} \usage{ sc3_run_svm.SingleCellExperiment(object, ks)