Browse code

Updated plotRPC functions to use splines instead of rolling mean

Joshua D. Campbell authored on 13/04/2022 18:39:31
Showing 3 changed files

... ...
@@ -519,7 +519,7 @@ setMethod("resamplePerplexity",
519 519
 #'  parameters.
520 520
 #' @export
521 521
 setGeneric("plotGridSearchPerplexity",
522
-    function(x, altExpName = "featureSubset", sep = 1, alpha = 0.5) {
522
+    function(x, altExpName = "featureSubset", sep = 5, alpha = 0.5) {
523 523
     standardGeneric("plotGridSearchPerplexity")})
524 524
 
525 525
 
... ...
@@ -531,7 +531,7 @@ setGeneric("plotGridSearchPerplexity",
531 531
 #' @export
532 532
 setMethod("plotGridSearchPerplexity",
533 533
     signature(x = "SingleCellExperiment"),
534
-    function(x, altExpName = "featureSubset", sep = 1, alpha = 0.5) {
534
+    function(x, altExpName = "featureSubset", sep = 5, alpha = 0.5) {
535 535
         altExp <- SingleCellExperiment::altExp(x, altExpName)
536 536
         celdaList <- S4Vectors::metadata(altExp)$celda_grid_search
537 537
         g <- do.call(paste0(".plotGridSearchPerplexity",
... ...
@@ -553,7 +553,7 @@ setMethod("plotGridSearchPerplexity",
553 553
 #' @export
554 554
 setMethod("plotGridSearchPerplexity",
555 555
     signature(x = "celdaList"),
556
-    function(x, sep = 1, alpha = 0.5) {
556
+    function(x, sep = 5, alpha = 0.5) {
557 557
         g <- do.call(paste0(".plotGridSearchPerplexity",
558 558
             as.character(class(resList(x)[[1]]))),
559 559
             args = list(x, sep, alpha))
... ...
@@ -777,8 +777,7 @@ setMethod("plotGridSearchPerplexity",
777 777
 
778 778
 #' @title Visualize perplexity differences of a list of celda models
779 779
 #' @description Visualize perplexity differences of every model in a celdaList,
780
-#'  by unique K/L combinations. Line represents centered moving average with
781
-#'  windows of length \code{n}.
780
+#'  by unique K/L combinations. 
782 781
 #' @param x Can be one of
783 782
 #' \itemize{
784 783
 #'  \item A \linkS4class{SingleCellExperiment} object returned from
... ...
@@ -789,15 +788,14 @@ setMethod("plotGridSearchPerplexity",
789 788
 #' @param altExpName The name for the \link{altExp} slot
790 789
 #'  to use. Default "featureSubset".
791 790
 #' @param sep Numeric. Breaks in the x axis of the resulting plot.
792
-#' @param n Integer. Width of the rolling window. Default 10.
793 791
 #' @param alpha Numeric. Passed to \link{geom_jitter}. Opacity of the points.
794 792
 #'  Values of alpha range from 0 to 1, with lower values corresponding
795 793
 #'  to more transparent colors.
796
-#' @return A ggplot plot object showing perplexity diferences as a function of
794
+#' @return A ggplot plot object showing perplexity differences as a function of
797 795
 #'  clustering parameters.
798 796
 #' @export
799 797
 setGeneric("plotRPC",
800
-    function(x, altExpName = "featureSubset", sep = 1, n = 10, alpha = 0.5) {
798
+    function(x, altExpName = "featureSubset", sep = 5, alpha = 0.5) {
801 799
     standardGeneric("plotRPC")})
802 800
 
803 801
 
... ...
@@ -805,24 +803,24 @@ setGeneric("plotRPC",
805 803
 #' @examples
806 804
 #' data(sceCeldaCGGridSearch)
807 805
 #' sce <- resamplePerplexity(sceCeldaCGGridSearch)
808
-#' plotRPC(sce, n = 1)
806
+#' plotRPC(sce)
809 807
 #' @export
810 808
 setMethod("plotRPC",
811 809
     signature(x = "SingleCellExperiment"),
812
-    function(x, altExpName = "featureSubset", sep = 1, n = 10, alpha = 0.5) {
810
+    function(x, altExpName = "featureSubset", sep = 5, alpha = 0.5) {
813 811
         altExp <- SingleCellExperiment::altExp(x, altExpName)
814 812
         model <- altExp@metadata$celda_grid_search@celdaGridSearchParameters$
815 813
             model
816 814
         celdaList <- S4Vectors::metadata(altExp)$celda_grid_search
817 815
 
818 816
         if (model == "celda_C") {
819
-            g <- .plotRPCC(celdaList, sep, n = n,
817
+            g <- .plotRPCC(celdaList, sep, 
820 818
                 alpha = alpha)
821 819
         } else if (model == "celda_G") {
822
-            g <- .plotRPCG(celdaList, sep, n = n,
820
+            g <- .plotRPCG(celdaList, sep, 
823 821
                 alpha = alpha)
824 822
         } else if (model == "celda_CG") {
825
-            g <- .plotRPCCG(celdaList, sep, n = n,
823
+            g <- .plotRPCCG(celdaList, sep, 
826 824
                 alpha = alpha)
827 825
         } else {
828 826
             stop("S4Vectors::metadata(altExp(x, altExpName))$",
... ...
@@ -842,20 +840,20 @@ setMethod("plotRPC",
842 840
 #' celdaCGGridSearchRes <- resamplePerplexity(
843 841
 #'   celdaCGSim$counts,
844 842
 #'   celdaCGGridSearchRes)
845
-#' plotRPC(celdaCGGridSearchRes, n = 1)
843
+#' plotRPC(celdaCGGridSearchRes)
846 844
 #' @export
847 845
 setMethod("plotRPC",
848 846
     signature(x = "celdaList"),
849
-    function(x, sep = 1, n = 10, alpha = 0.5) {
847
+    function(x, sep = 5, alpha = 0.5) {
850 848
         g <- do.call(paste0(".plotRPC",
851 849
             unlist(strsplit(as.character(class(resList(x)[[1]])), "_"))[[2]]),
852
-            args = list(x, sep, n, alpha))
850
+            args = list(x, sep, alpha))
853 851
         return(g)
854 852
     }
855 853
 )
856 854
 
857 855
 
858
-.plotRPCCG <- function(celdaList, sep, n, alpha) {
856
+.plotRPCCG <- function(celdaList, sep, alpha) {
859 857
     # fix check note
860 858
     K <- L <- perpdiffK <- meanperpdiffK <- perpdiffL <- meanperpdiffL <- NULL
861 859
 
... ...
@@ -895,10 +893,9 @@ setMethod("plotRPC",
895 893
         colnames(diffMeansByK) <- c("K", "L", "meanperpdiffK")
896 894
         diffMeansByK$K <- as.factor(diffMeansByK$K)
897 895
         diffMeansByK$L <- as.factor(diffMeansByK$L)
898
-        diffMeansByK$rollmean <- data.table::frollmean(
899
-            diffMeansByK$meanperpdiffK, n = n, align = "center")
900 896
         diffMeansByK <- diffMeansByK[stats::complete.cases(diffMeansByK), ]
901
-
897
+        diffMeansByK$spline <- stats::smooth.spline(diffMeansByK$meanperpdiffK)$y
898
+        
902 899
         if (nlevels(dt$L) > 1) {
903 900
             plot <- ggplot2::ggplot(dt[!is.na(perpdiffK), ],
904 901
                 ggplot2::aes_string(x = "K",
... ...
@@ -907,7 +904,7 @@ setMethod("plotRPC",
907 904
                     ggplot2::aes_string(color = "L")) +
908 905
                 ggplot2::scale_color_discrete(name = "L") +
909 906
                 ggplot2::geom_path(data = diffMeansByK,
910
-                    ggplot2::aes_string(x = "K", y = "rollmean", group = "L",
907
+                    ggplot2::aes_string(x = "K", y = "spline", group = "L",
911 908
                         color = "L"), size = 1) +
912 909
                 ggplot2::ylab("Rate of perplexity change") +
913 910
                 ggplot2::xlab("K") +
... ...
@@ -925,7 +922,7 @@ setMethod("plotRPC",
925 922
                     color = "grey", alpha = alpha) +
926 923
                 ggplot2::scale_color_manual(name = "L", values = "black") +
927 924
                 ggplot2::geom_path(data = diffMeansByK,
928
-                    ggplot2::aes_string(x = "K", y = "rollmean", group = "L",
925
+                    ggplot2::aes_string(x = "K", y = "spline", group = "L",
929 926
                         color = "L"), size = 1) +
930 927
                 ggplot2::ylab("Rate of perplexity change") +
931 928
                 ggplot2::xlab("K") +
... ...
@@ -952,10 +949,9 @@ setMethod("plotRPC",
952 949
         colnames(diffMeansByL) <- c("K", "L", "meanperpdiffL")
953 950
         diffMeansByL$K <- as.factor(diffMeansByL$K)
954 951
         diffMeansByL$L <- as.factor(diffMeansByL$L)
955
-        diffMeansByL$rollmean <- data.table::frollmean(
956
-            diffMeansByL$meanperpdiffL, n = n, align = "center")
957 952
         diffMeansByL <- diffMeansByL[stats::complete.cases(diffMeansByL), ]
958
-
953
+        diffMeansByL$spline <- stats::smooth.spline(diffMeansByL$meanperpdiffL)$y
954
+        
959 955
         plot <- ggplot2::ggplot(dt[!is.na(perpdiffL), ],
960 956
             ggplot2::aes_string(x = "L", y = "perpdiffL")) +
961 957
             ggplot2::geom_jitter(height = 0, width = 0.1,
... ...
@@ -964,7 +960,7 @@ setMethod("plotRPC",
964 960
             ggplot2::geom_path(
965 961
                 data = diffMeansByL,
966 962
                 ggplot2::aes_string(
967
-                    x = "L", y = "rollmean", group = "K", color = "K"),
963
+                    x = "L", y = "spline", group = "K", color = "K"),
968 964
                 size = 1) +
969 965
             ggplot2::ylab("Rate of perplexity change") +
970 966
             ggplot2::xlab("L") +
... ...
@@ -983,7 +979,7 @@ setMethod("plotRPC",
983 979
 }
984 980
 
985 981
 
986
-.plotRPCC <- function(celdaList, sep, n, alpha) {
982
+.plotRPCC <- function(celdaList, sep, alpha) {
987 983
     K <- perpdiffK <- meanperpdiffK <- NULL # fix check note
988 984
     if (!all(c("K") %in% colnames(runParams(celdaList)))) {
989 985
         stop("runParams(celdaList) needs the column K.")
... ...
@@ -1015,17 +1011,16 @@ setMethod("plotRPC",
1015 1011
             FUN = mean))
1016 1012
         colnames(diffMeansByK) <- c("K", "meanperpdiffK")
1017 1013
         diffMeansByK$K <- as.factor(diffMeansByK$K)
1018
-        diffMeansByK$rollmean <- data.table::frollmean(
1019
-            diffMeansByK$meanperpdiffK, n = n, align = "center")
1020 1014
         diffMeansByK <- diffMeansByK[stats::complete.cases(diffMeansByK), ]
1021
-
1015
+        diffMeansByK$spline <- stats::smooth.spline(diffMeansByK$meanperpdiffK)$y
1016
+        
1022 1017
         plot <- ggplot2::ggplot(dt[!is.na(perpdiffK), ],
1023 1018
             ggplot2::aes_string(x = "K",
1024 1019
                 y = "perpdiffK")) +
1025 1020
             ggplot2::geom_jitter(height = 0, width = 0.1,
1026 1021
                 color = "grey", alpha = alpha) +
1027 1022
             ggplot2::geom_path(data = diffMeansByK,
1028
-                ggplot2::aes_string(x = "K", y = "rollmean", group = 1),
1023
+                ggplot2::aes_string(x = "K", y = "spline", group = 1),
1029 1024
                 size = 1) +
1030 1025
             ggplot2::ylab("Perplexity difference compared to previous K") +
1031 1026
             ggplot2::xlab("K") +
... ...
@@ -1043,7 +1038,7 @@ setMethod("plotRPC",
1043 1038
 }
1044 1039
 
1045 1040
 
1046
-.plotRPCG <- function(celdaList, sep, n, alpha) {
1041
+.plotRPCG <- function(celdaList, sep, alpha) {
1047 1042
     L <- perpdiffL <- meanperpdiffL <- NULL # fix check note
1048 1043
     if (!all(c("L") %in% colnames(runParams(celdaList)))) {
1049 1044
         stop("runParams(celdaList) needs the column L.")
... ...
@@ -1075,17 +1070,16 @@ setMethod("plotRPC",
1075 1070
             FUN = mean))
1076 1071
         colnames(diffMeansByL) <- c("L", "meanperpdiffL")
1077 1072
         diffMeansByL$L <- as.factor(diffMeansByL$L)
1078
-        diffMeansByL$rollmean <- data.table::frollmean(
1079
-            diffMeansByL$meanperpdiffL, n = n, align = "center")
1080 1073
         diffMeansByL <- diffMeansByL[stats::complete.cases(diffMeansByL), ]
1081
-
1074
+        diffMeansByL$spline <- stats::smooth.spline(diffMeansByL$meanperpdiffL)$y
1075
+        
1082 1076
         plot <- ggplot2::ggplot(dt[!is.na(perpdiffL), ],
1083 1077
             ggplot2::aes_string(x = "L",
1084 1078
                 y = "perpdiffL")) +
1085 1079
             ggplot2::geom_jitter(height = 0, width = 0.1,
1086 1080
                 color = "grey", alpha = alpha) +
1087 1081
             ggplot2::geom_path(data = diffMeansByL,
1088
-                ggplot2::aes_string(x = "L", y = "rollmean", group = 1),
1082
+                ggplot2::aes_string(x = "L", y = "spline", group = 1),
1089 1083
                 size = 1) +
1090 1084
             ggplot2::ylab("Perplexity difference compared to previous L") +
1091 1085
             ggplot2::xlab("L") +
... ...
@@ -6,11 +6,11 @@
6 6
 \alias{plotGridSearchPerplexity,celdaList-method}
7 7
 \title{Visualize perplexity of a list of celda models}
8 8
 \usage{
9
-plotGridSearchPerplexity(x, altExpName = "featureSubset", sep = 1, alpha = 0.5)
9
+plotGridSearchPerplexity(x, altExpName = "featureSubset", sep = 5, alpha = 0.5)
10 10
 
11
-\S4method{plotGridSearchPerplexity}{SingleCellExperiment}(x, altExpName = "featureSubset", sep = 1, alpha = 0.5)
11
+\S4method{plotGridSearchPerplexity}{SingleCellExperiment}(x, altExpName = "featureSubset", sep = 5, alpha = 0.5)
12 12
 
13
-\S4method{plotGridSearchPerplexity}{celdaList}(x, sep = 1, alpha = 0.5)
13
+\S4method{plotGridSearchPerplexity}{celdaList}(x, sep = 5, alpha = 0.5)
14 14
 }
15 15
 \arguments{
16 16
 \item{x}{Can be one of
... ...
@@ -6,11 +6,11 @@
6 6
 \alias{plotRPC,celdaList-method}
7 7
 \title{Visualize perplexity differences of a list of celda models}
8 8
 \usage{
9
-plotRPC(x, altExpName = "featureSubset", sep = 1, n = 10, alpha = 0.5)
9
+plotRPC(x, altExpName = "featureSubset", sep = 5, alpha = 0.5)
10 10
 
11
-\S4method{plotRPC}{SingleCellExperiment}(x, altExpName = "featureSubset", sep = 1, n = 10, alpha = 0.5)
11
+\S4method{plotRPC}{SingleCellExperiment}(x, altExpName = "featureSubset", sep = 5, alpha = 0.5)
12 12
 
13
-\S4method{plotRPC}{celdaList}(x, sep = 1, n = 10, alpha = 0.5)
13
+\S4method{plotRPC}{celdaList}(x, sep = 5, alpha = 0.5)
14 14
 }
15 15
 \arguments{
16 16
 \item{x}{Can be one of
... ...
@@ -26,29 +26,26 @@ to use. Default "featureSubset".}
26 26
 
27 27
 \item{sep}{Numeric. Breaks in the x axis of the resulting plot.}
28 28
 
29
-\item{n}{Integer. Width of the rolling window. Default 10.}
30
-
31 29
 \item{alpha}{Numeric. Passed to \link{geom_jitter}. Opacity of the points.
32 30
 Values of alpha range from 0 to 1, with lower values corresponding
33 31
 to more transparent colors.}
34 32
 }
35 33
 \value{
36
-A ggplot plot object showing perplexity diferences as a function of
34
+A ggplot plot object showing perplexity differences as a function of
37 35
  clustering parameters.
38 36
 }
39 37
 \description{
40 38
 Visualize perplexity differences of every model in a celdaList,
41
- by unique K/L combinations. Line represents centered moving average with
42
- windows of length \code{n}.
39
+ by unique K/L combinations.
43 40
 }
44 41
 \examples{
45 42
 data(sceCeldaCGGridSearch)
46 43
 sce <- resamplePerplexity(sceCeldaCGGridSearch)
47
-plotRPC(sce, n = 1)
44
+plotRPC(sce)
48 45
 data(celdaCGSim, celdaCGGridSearchRes)
49 46
 ## Run various combinations of parameters with 'celdaGridSearch'
50 47
 celdaCGGridSearchRes <- resamplePerplexity(
51 48
   celdaCGSim$counts,
52 49
   celdaCGGridSearchRes)
53
-plotRPC(celdaCGGridSearchRes, n = 1)
50
+plotRPC(celdaCGGridSearchRes)
54 51
 }