Browse code

Add some missing parameters in seuratFunctions.R

Irzam Sarfraz authored on 04/04/2021 11:45:01
Showing 7 changed files

... ...
@@ -61,7 +61,7 @@
61 61
 #' @export
62 62
 seuratNormalizeData <- function(inSCE, useAssay, normAssayName = "seuratNormData", normalizationMethod = "LogNormalize", scaleFactor = 10000, verbose = TRUE) {
63 63
   if(missing(useAssay)){
64
-    useAssay <- assayNames(inSCE)[1]
64
+    useAssay <- SummarizedExperiment::assayNames(inSCE)[1]
65 65
     message("'useAssay' parameter missing. Using the first available assay instead: '", useAssay, "'")
66 66
   }
67 67
   seuratObject <- Seurat::NormalizeData(convertSCEToSeurat(inSCE, useAssay), normalization.method = normalizationMethod, scale.factor = scaleFactor, verbose = verbose)
... ...
@@ -209,6 +209,10 @@ seuratICA <- function(inSCE, useAssay, reducedDimName = "seuratICA", nics = 20)
209 209
 #' @param inSCE (sce) object on which to compute and store jackstraw plot
210 210
 #' @param useAssay Assay containing scaled counts to use in JackStraw calculation.
211 211
 #' @param dims Number of components to test in Jackstraw. If \code{NULL}, then all components are used. Default \code{NULL}.
212
+#' @param numReplicate Numeric value indicating the number of replicate samplings to perform.
213
+#'  Default value is \code{100}.
214
+#' @param propFreq Numeric value indicating the proportion of data to randomly permute for each replicate.
215
+#'  Default value is \code{0.025}.
212 216
 #' @param externalReduction Pass DimReduc object if PCA/ICA computed through other libraries. Default \code{NULL}.
213 217
 #' @examples
214 218
 #' data(scExample, package = "singleCellTK")
... ...
@@ -221,7 +225,7 @@ seuratICA <- function(inSCE, useAssay, reducedDimName = "seuratICA", nics = 20)
221 225
 #' }
222 226
 #' @return Updated \code{SingleCellExperiment} object with jackstraw computations stored in it
223 227
 #' @export
224
-seuratComputeJackStraw <- function(inSCE, useAssay, dims = NULL, externalReduction = NULL) {
228
+seuratComputeJackStraw <- function(inSCE, useAssay, dims = NULL, numReplicate = 100, propFreq = 0.025, externalReduction = NULL) {
225 229
   seuratObject <- convertSCEToSeurat(inSCE, scaledAssay = useAssay)
226 230
   if(!is.null(externalReduction)){
227 231
     #convert (_) to (-) as required by Seurat
... ...
@@ -250,7 +254,7 @@ seuratComputeJackStraw <- function(inSCE, useAssay, dims = NULL, externalReducti
250 254
   if(is.null(dims)) {
251 255
     dims <- ncol(seuratObject@reductions[["pca"]])
252 256
   }
253
-  seuratObject <- Seurat::JackStraw(seuratObject, dims = as.double(dims))
257
+  seuratObject <- Seurat::JackStraw(seuratObject, dims = as.double(dims), num.replicate = numReplicate, prop.freq = propFreq)
254 258
   seuratObject <- Seurat::ScoreJackStraw(seuratObject, dims = 1:dims)
255 259
   inSCE <- .addSeuratToMetaDataSCE(inSCE, seuratObject)
256 260
   return(inSCE)
... ...
@@ -260,6 +264,8 @@ seuratComputeJackStraw <- function(inSCE, useAssay, dims = NULL, externalReducti
260 264
 #' Computes the plot object for jackstraw plot from the pca slot in the input sce object
261 265
 #' @param inSCE (sce) object from which to compute the jackstraw plot (pca should be computed)
262 266
 #' @param dims Number of components to plot in Jackstraw. If \code{NULL}, then all components are plotted Default \code{NULL}.
267
+#' @param xmax X-axis maximum on each QQ plot. Default \code{0.1}.
268
+#' @param ymax Y-axis maximum on each QQ plot. Default \code{0.3}.
263 269
 #' @param externalReduction Pass DimReduc object if PCA/ICA computed through other libraries. Default \code{NULL}.
264 270
 #' @examples
265 271
 #' data(scExample, package = "singleCellTK")
... ...
@@ -273,7 +279,7 @@ seuratComputeJackStraw <- function(inSCE, useAssay, dims = NULL, externalReducti
273 279
 #' }
274 280
 #' @return plot object
275 281
 #' @export
276
-seuratJackStrawPlot <- function(inSCE, dims = NULL, externalReduction = NULL) {
282
+seuratJackStrawPlot <- function(inSCE, dims = NULL, xmax = 0.1, ymax = 0.3, externalReduction = NULL) {
277 283
   seuratObject <- convertSCEToSeurat(inSCE)
278 284
   if(!is.null(externalReduction)){
279 285
     seuratObject@reductions <- list(pca = externalReduction)
... ...
@@ -284,7 +290,7 @@ seuratJackStrawPlot <- function(inSCE, dims = NULL, externalReduction = NULL) {
284 290
   if(is.null(dims)) {
285 291
     dims <- ncol(seuratObject@reductions[["pca"]])
286 292
   }
287
-  return(Seurat::JackStrawPlot(seuratObject, dims = 1:dims))
293
+  return(Seurat::JackStrawPlot(seuratObject, dims = 1:dims, xmax = xmax, ymax = ymax))
288 294
 }
289 295
 
290 296
 #' seuratPlotHVG
... ...
@@ -375,6 +381,8 @@ seuratReductionPlot <- function(inSCE, useReduction = c("pca", "ica", "tsne", "u
375 381
 #' @param groupSingletons boolean if singletons should be grouped together or not. Default \code{TRUE}.
376 382
 #' @param resolution Set the resolution parameter to find larger (value above 1) or smaller (value below 1) number of communities. Default \code{0.8}.
377 383
 #' @param externalReduction Pass DimReduc object if PCA/ICA computed through other libraries. Default \code{NULL}.
384
+#' @param verbose Logical value indicating if informative messages should 
385
+#'  be displayed. Default is \code{TRUE}.
378 386
 #' @examples
379 387
 #' data(scExample, package = "singleCellTK")
380 388
 #' \dontrun{
... ...
@@ -386,8 +394,16 @@ seuratReductionPlot <- function(inSCE, useReduction = c("pca", "ica", "tsne", "u
386 394
 #' }
387 395
 #' @return Updated sce object which now contains the computed clusters
388 396
 #' @export
389
-seuratFindClusters <- function(inSCE, useAssay, useReduction = c("pca", "ica"), dims = 10, algorithm = c("louvain", "multilevel", "SLM"), groupSingletons = TRUE, resolution = 0.8, externalReduction = NULL) {
390
-  
397
+seuratFindClusters <- function(
398
+  inSCE,
399
+  useAssay = "seuratScaledData", 
400
+  useReduction = c("pca", "ica"), 
401
+  dims = 10, 
402
+  algorithm = c("louvain", "multilevel", "SLM"), 
403
+  groupSingletons = TRUE, 
404
+  resolution = 0.8, 
405
+  externalReduction = NULL,
406
+  verbose = TRUE) {
391 407
   
392 408
   algorithm <- match.arg(algorithm)
393 409
   useReduction <- match.arg(useReduction)
... ...
@@ -398,7 +414,7 @@ seuratFindClusters <- function(inSCE, useAssay, useReduction = c("pca", "ica"),
398 414
     seuratObject@reductions <- list(pca = externalReduction)
399 415
   }
400 416
   
401
-  seuratObject <- Seurat::FindNeighbors(seuratObject, reduction = useReduction, dims = seq(dims))
417
+  seuratObject <- Seurat::FindNeighbors(seuratObject, reduction = useReduction, dims = seq(dims), verbose = verbose)
402 418
   no_algorithm <- 1
403 419
   if (algorithm == "louvain") {
404 420
     no_algorithm = 1
... ...
@@ -407,7 +423,7 @@ seuratFindClusters <- function(inSCE, useAssay, useReduction = c("pca", "ica"),
407 423
   } else if (algorithm == "SLM") {
408 424
     no_algorithm = 3
409 425
   }
410
-  seuratObject <- Seurat::FindClusters(seuratObject, algorithm = no_algorithm, group.singletons = groupSingletons, resolution = resolution)
426
+  seuratObject <- Seurat::FindClusters(seuratObject, algorithm = no_algorithm, group.singletons = groupSingletons, resolution = resolution, verbose = verbose)
411 427
   inSCE <- .addSeuratToMetaDataSCE(inSCE, seuratObject)
412 428
   colData(inSCE)[[paste0("Seurat","_",algorithm,"_","Resolution",resolution)]] <- [email protected]$seurat_clusters
413 429
   S4Vectors::metadata(inSCE)$seurat$clusterName <- paste0("Seurat","_",algorithm,"_","Resolution",resolution)
... ...
@@ -436,7 +452,6 @@ seuratRunTSNE <- function(inSCE, useReduction = c("pca", "ica"), reducedDimName
436 452
   return(inSCE)
437 453
 }
438 454
 
439
-#RunUMAP(seurat, reduction = "pca", dims = 1:10, min.dist = 0.4, n.neighbors = 40, spread = 20)
440 455
 #' seuratRunUMAP
441 456
 #' Computes UMAP from the given sce object and stores the UMAP computations back into the sce object
442 457
 #' @param inSCE (sce) object on which to compute the UMAP
... ...
@@ -446,6 +461,8 @@ seuratRunTSNE <- function(inSCE, useReduction = c("pca", "ica"), reducedDimName
446 461
 #' @param minDist Sets the \code{"min.dist"} parameter to the underlying UMAP call. See \link[Seurat]{RunUMAP} for more information. Default \code{0.3}.
447 462
 #' @param nNeighbors Sets the \code{"n.neighbors"} parameter to the underlying UMAP call. See \link[Seurat]{RunUMAP} for more information. Default \code{30L}.
448 463
 #' @param spread Sets the \code{"spread"} parameter to the underlying UMAP call. See \link[Seurat]{RunUMAP} for more information. Default \code{1}.
464
+#' @param verbose Logical value indicating if informative messages should 
465
+#'  be displayed. Default is \code{TRUE}.
449 466
 #' @examples
450 467
 #' data(scExample, package = "singleCellTK")
451 468
 #' \dontrun{
... ...
@@ -459,7 +476,7 @@ seuratRunTSNE <- function(inSCE, useReduction = c("pca", "ica"), reducedDimName
459 476
 #' @return Updated sce object with UMAP computations stored
460 477
 #' @export
461 478
 #' @importFrom SingleCellExperiment reducedDim<-
462
-seuratRunUMAP <- function(inSCE, useReduction = c("pca", "ica"), reducedDimName = "seuratUMAP", dims = 10, minDist = 0.3, nNeighbors = 30L, spread = 1) {
479
+seuratRunUMAP <- function(inSCE, useReduction = c("pca", "ica"), reducedDimName = "seuratUMAP", dims = 10, minDist = 0.3, nNeighbors = 30L, spread = 1, verbose = TRUE) {
463 480
   useReduction <- match.arg(useReduction)
464 481
   seuratObject <- convertSCEToSeurat(inSCE)
465 482
   seuratObject <- Seurat::RunUMAP(seuratObject,
... ...
@@ -467,7 +484,8 @@ seuratRunUMAP <- function(inSCE, useReduction = c("pca", "ica"), reducedDimName
467 484
                                   dims = 1:dims,
468 485
                                   min.dist = minDist,
469 486
                                   n.neighbors = nNeighbors,
470
-                                  spread = spread)
487
+                                  spread = spread,
488
+                                  verbose = verbose)
471 489
   inSCE <- .addSeuratToMetaDataSCE(inSCE, seuratObject)
472 490
   
473 491
   temp <- seuratObject@[email protected]
... ...
@@ -494,6 +512,7 @@ seuratRunUMAP <- function(inSCE, useReduction = c("pca", "ica"), reducedDimName
494 512
 #' @param inSCE (sce) object from which to compute the elbow plot (pca should be computed)
495 513
 #' @param significantPC Number of significant principal components to plot. This is used to alter the color of the points for the corresponding PCs. If \code{NULL}, all points will be the same color. Default \code{NULL}.
496 514
 #' @param reduction Reduction to use for elbow plot generation. Either \code{"pca"} or \code{"ica"}. Default \code{"pca"}.
515
+#' @param ndims Number of components to use. Default \code{20}.
497 516
 #' @param externalReduction Pass DimReduc object if PCA/ICA computed through other libraries. Default \code{NULL}.
498 517
 #' @param interactive Logical value indicating if the returned object should
499 518
 #'  be an interactive plotly object if \code{TRUE} or a ggplot object if
... ...
@@ -512,13 +531,14 @@ seuratRunUMAP <- function(inSCE, useReduction = c("pca", "ica"), reducedDimName
512 531
 seuratElbowPlot <- function(inSCE, 
513 532
                             significantPC = NULL, 
514 533
                             reduction = "pca", 
534
+                            ndims = 20,
515 535
                             externalReduction = NULL,
516 536
                             interactive = TRUE) {
517 537
   seuratObject <- convertSCEToSeurat(inSCE)
518 538
   if(!is.null(externalReduction)){
519 539
     seuratObject@reductions <- list(pca = externalReduction)
520 540
   }
521
-  plot <- Seurat::ElbowPlot(seuratObject, reduction = reduction)
541
+  plot <- Seurat::ElbowPlot(seuratObject, reduction = reduction, ndims = ndims)
522 542
   if(!is.null(significantPC)){
523 543
     plot$data$Significant <- c(rep("Yes", significantPC), rep("No", length(rownames(plot$data)) - significantPC))
524 544
     plot <- ggplot2::ggplot(data = plot$data, ggplot2::aes(x = plot$data$dims, y = plot$data$stdev, color = plot$data$Significant)) + ggplot2::geom_point()
... ...
@@ -890,12 +910,14 @@ seuratIntegration <- function(inSCE, useAssay = "counts", batch, newAssayName =
890 910
 #' @param threshUse Numeric value indicating the logFC threshold value on 
891 911
 #'  which on average, at least X-fold difference (log-scale) between the 
892 912
 #'  two groups of cells exists. Default is \code{0.25}.
913
+#' @param verbose Logical value indicating if informative messages should 
914
+#'  be displayed. Default is \code{TRUE}.
893 915
 #' @return A \code{SingleCellExperiment} object that contains marker genes populated in a data.frame stored inside metadata slot.
894 916
 #' @export
895 917
 seuratFindMarkers <- function(
896 918
   inSCE, cells1 = NULL, cells2 = NULL, group1 = NULL, group2 = NULL, 
897 919
   allGroup = NULL, conserved = FALSE, test = "wilcox", onlyPos = FALSE,
898
-  minPCT = 0.1, threshUse = 0.25){
920
+  minPCT = 0.1, threshUse = 0.25, verbose = TRUE){
899 921
   seuratObject <- convertSCEToSeurat(inSCE)
900 922
   markerGenes <- NULL
901 923
   if(is.null(allGroup)
... ...
@@ -953,7 +975,8 @@ seuratFindMarkers <- function(
953 975
       test.use = test, 
954 976
       only.pos = onlyPos,
955 977
       logfc.threshold = threshUse,
956
-      min.pct = minPCT)
978
+      min.pct = minPCT,
979
+      verbose = verbose)
957 980
     gene.id <- markerGenes$gene
958 981
     markerGenes <- cbind(gene.id, markerGenes)
959 982
     markerGenes$gene <- NULL
... ...
@@ -973,7 +996,8 @@ seuratFindMarkers <- function(
973 996
       test.use = test, 
974 997
       only.pos = onlyPos,
975 998
       logfc.threshold = threshUse,
976
-      min.pct = minPCT)
999
+      min.pct = minPCT,
1000
+      verbose = verbose)
977 1001
     gene.id <- markerGenes$gene
978 1002
     markerGenes <- cbind(gene.id, markerGenes)
979 1003
     markerGenes$gene <- NULL
... ...
@@ -1173,8 +1197,8 @@ seuratGenePlot <- function(inSCE,
1173 1197
 #' @return A list of variable feature names.
1174 1198
 #' @export
1175 1199
 seuratVariableFeatures <- function(inSCE){
1176
-  if(!is.null(metadata(inSCE)$seurat$obj)){
1177
-    return(Seurat::VariableFeatures(metadata(inSCE)$seurat$obj))
1200
+  if(!is.null(S4Vectors::metadata(inSCE)$seurat$obj)){
1201
+    return(Seurat::VariableFeatures(S4Vectors::metadata(inSCE)$seurat$obj))
1178 1202
   }
1179 1203
   else{
1180 1204
     return(NULL)
... ...
@@ -5,7 +5,14 @@
5 5
 \title{seuratComputeJackStraw
6 6
 Compute jackstraw plot and store the computations in the input sce object}
7 7
 \usage{
8
-seuratComputeJackStraw(inSCE, useAssay, dims = NULL, externalReduction = NULL)
8
+seuratComputeJackStraw(
9
+  inSCE,
10
+  useAssay,
11
+  dims = NULL,
12
+  numReplicate = 100,
13
+  propFreq = 0.025,
14
+  externalReduction = NULL
15
+)
9 16
 }
10 17
 \arguments{
11 18
 \item{inSCE}{(sce) object on which to compute and store jackstraw plot}
... ...
@@ -14,6 +21,12 @@ seuratComputeJackStraw(inSCE, useAssay, dims = NULL, externalReduction = NULL)
14 21
 
15 22
 \item{dims}{Number of components to test in Jackstraw. If \code{NULL}, then all components are used. Default \code{NULL}.}
16 23
 
24
+\item{numReplicate}{Numeric value indicating the number of replicate samplings to perform.
25
+Default value is \code{100}.}
26
+
27
+\item{propFreq}{Numeric value indicating the proportion of data to randomly permute for each replicate.
28
+Default value is \code{0.025}.}
29
+
17 30
 \item{externalReduction}{Pass DimReduc object if PCA/ICA computed through other libraries. Default \code{NULL}.}
18 31
 }
19 32
 \value{
... ...
@@ -9,6 +9,7 @@ seuratElbowPlot(
9 9
   inSCE,
10 10
   significantPC = NULL,
11 11
   reduction = "pca",
12
+  ndims = 20,
12 13
   externalReduction = NULL,
13 14
   interactive = TRUE
14 15
 )
... ...
@@ -20,6 +21,8 @@ seuratElbowPlot(
20 21
 
21 22
 \item{reduction}{Reduction to use for elbow plot generation. Either \code{"pca"} or \code{"ica"}. Default \code{"pca"}.}
22 23
 
24
+\item{ndims}{Number of components to use. Default \code{20}.}
25
+
23 26
 \item{externalReduction}{Pass DimReduc object if PCA/ICA computed through other libraries. Default \code{NULL}.}
24 27
 
25 28
 \item{interactive}{Logical value indicating if the returned object should
... ...
@@ -7,13 +7,14 @@ Computes the clusters from the input sce object and stores them back in sce obje
7 7
 \usage{
8 8
 seuratFindClusters(
9 9
   inSCE,
10
-  useAssay,
10
+  useAssay = "seuratScaledData",
11 11
   useReduction = c("pca", "ica"),
12 12
   dims = 10,
13 13
   algorithm = c("louvain", "multilevel", "SLM"),
14 14
   groupSingletons = TRUE,
15 15
   resolution = 0.8,
16
-  externalReduction = NULL
16
+  externalReduction = NULL,
17
+  verbose = TRUE
17 18
 )
18 19
 }
19 20
 \arguments{
... ...
@@ -32,6 +33,9 @@ seuratFindClusters(
32 33
 \item{resolution}{Set the resolution parameter to find larger (value above 1) or smaller (value below 1) number of communities. Default \code{0.8}.}
33 34
 
34 35
 \item{externalReduction}{Pass DimReduc object if PCA/ICA computed through other libraries. Default \code{NULL}.}
36
+
37
+\item{verbose}{Logical value indicating if informative messages should 
38
+be displayed. Default is \code{TRUE}.}
35 39
 }
36 40
 \value{
37 41
 Updated sce object which now contains the computed clusters
... ...
@@ -15,7 +15,8 @@ seuratFindMarkers(
15 15
   test = "wilcox",
16 16
   onlyPos = FALSE,
17 17
   minPCT = 0.1,
18
-  threshUse = 0.25
18
+  threshUse = 0.25,
19
+  verbose = TRUE
19 20
 )
20 21
 }
21 22
 \arguments{
... ...
@@ -45,6 +46,9 @@ cells in which genes are detected. Default is \code{0.1}.}
45 46
 \item{threshUse}{Numeric value indicating the logFC threshold value on 
46 47
 which on average, at least X-fold difference (log-scale) between the 
47 48
 two groups of cells exists. Default is \code{0.25}.}
49
+
50
+\item{verbose}{Logical value indicating if informative messages should 
51
+be displayed. Default is \code{TRUE}.}
48 52
 }
49 53
 \value{
50 54
 A \code{SingleCellExperiment} object that contains marker genes populated in a data.frame stored inside metadata slot.
... ...
@@ -5,13 +5,23 @@
5 5
 \title{seuratJackStrawPlot
6 6
 Computes the plot object for jackstraw plot from the pca slot in the input sce object}
7 7
 \usage{
8
-seuratJackStrawPlot(inSCE, dims = NULL, externalReduction = NULL)
8
+seuratJackStrawPlot(
9
+  inSCE,
10
+  dims = NULL,
11
+  xmax = 0.1,
12
+  ymax = 0.3,
13
+  externalReduction = NULL
14
+)
9 15
 }
10 16
 \arguments{
11 17
 \item{inSCE}{(sce) object from which to compute the jackstraw plot (pca should be computed)}
12 18
 
13 19
 \item{dims}{Number of components to plot in Jackstraw. If \code{NULL}, then all components are plotted Default \code{NULL}.}
14 20
 
21
+\item{xmax}{X-axis maximum on each QQ plot. Default \code{0.1}.}
22
+
23
+\item{ymax}{Y-axis maximum on each QQ plot. Default \code{0.3}.}
24
+
15 25
 \item{externalReduction}{Pass DimReduc object if PCA/ICA computed through other libraries. Default \code{NULL}.}
16 26
 }
17 27
 \value{
... ...
@@ -12,7 +12,8 @@ seuratRunUMAP(
12 12
   dims = 10,
13 13
   minDist = 0.3,
14 14
   nNeighbors = 30L,
15
-  spread = 1
15
+  spread = 1,
16
+  verbose = TRUE
16 17
 )
17 18
 }
18 19
 \arguments{
... ...
@@ -29,6 +30,9 @@ seuratRunUMAP(
29 30
 \item{nNeighbors}{Sets the \code{"n.neighbors"} parameter to the underlying UMAP call. See \link[Seurat]{RunUMAP} for more information. Default \code{30L}.}
30 31
 
31 32
 \item{spread}{Sets the \code{"spread"} parameter to the underlying UMAP call. See \link[Seurat]{RunUMAP} for more information. Default \code{1}.}
33
+
34
+\item{verbose}{Logical value indicating if informative messages should 
35
+be displayed. Default is \code{TRUE}.}
32 36
 }
33 37
 \value{
34 38
 Updated sce object with UMAP computations stored