Browse code

Updates to decontX vignette and minor change to a decontX plotting function

Joshua D. Campbell authored on 29/03/2022 15:05:16
Showing 1 changed files
... ...
@@ -371,7 +371,7 @@ plotDecontXMarkerExpression <- function(x, markers, groupClusters = NULL,
371 371
       df.list[[i]] <- cbind(df, assay = assayName[i])
372 372
     }
373 373
     df <- do.call(rbind, df.list)
374
-    assay <- as.factor(df$assay)
374
+    assay <- factor(df$assay, levels = assayName)
375 375
     if (length(assayName) > 1) {
376 376
       legend <- "right"
377 377
     }
... ...
@@ -397,7 +397,7 @@ plotDecontXMarkerExpression <- function(x, markers, groupClusters = NULL,
397 397
   }
398 398
   Expression <- df$Expression
399 399
   Marker <- df$Marker
400
-  Assay <- df$assay
400
+  Assay <- factor(df$assay, levels = assayName)
401 401
   Cluster <- df$Cluster
402 402
   if (!is.null(groupClusters)) {
403 403
     df <- cbind(df, Cell_Type = names(groupClusters)[Cluster])
Browse code

Fix lints. Fix issue #323

Joshua D. Campbell authored on 30/03/2021 19:16:30
Showing 1 changed files
... ...
@@ -492,13 +492,13 @@ plotDecontXMarkerExpression <- function(x, markers, groupClusters = NULL,
492 492
 
493 493
     # Check for duplicate groupClusters
494 494
     ta <- table(unlist(groupClusters))
495
-    if(any(ta > 1)) {
495
+    if (any(ta > 1)) {
496 496
       dup <- names(ta)[ta > 1]
497 497
       stop("'groupClusters' had duplicate values for the following clusters: ",
498 498
            paste(dup, collapse = ","), ". Clusters need be assigned to a",
499 499
            "single group.")
500 500
     }
501
-    
501
+
502 502
     labels <- rep(NA, ncol(x))
503 503
     for (i in seq_along(groupClusters)) {
504 504
       labels[z %in% groupClusters[[i]]] <- names(groupClusters)[i]
Browse code

fixed bugs in decontx plotting related to groupClusters parameter

Joshua D. Campbell authored on 30/03/2021 18:55:52
Showing 1 changed files
... ...
@@ -490,9 +490,18 @@ plotDecontXMarkerExpression <- function(x, markers, groupClusters = NULL,
490 490
       )
491 491
     }
492 492
 
493
+    # Check for duplicate groupClusters
494
+    ta <- table(unlist(groupClusters))
495
+    if(any(ta > 1)) {
496
+      dup <- names(ta)[ta > 1]
497
+      stop("'groupClusters' had duplicate values for the following clusters: ",
498
+           paste(dup, collapse = ","), ". Clusters need be assigned to a",
499
+           "single group.")
500
+    }
501
+    
493 502
     labels <- rep(NA, ncol(x))
494 503
     for (i in seq_along(groupClusters)) {
495
-      labels[z %in% groupClusters[i]] <- names(groupClusters)[i]
504
+      labels[z %in% groupClusters[[i]]] <- names(groupClusters)[i]
496 505
     }
497 506
     na.ix <- is.na(labels)
498 507
     labels <- labels[!na.ix]
Browse code

Fixed typos in the documentation

Joshua D. Campbell authored on 18/03/2021 00:54:49
Showing 1 changed files
... ...
@@ -286,7 +286,7 @@ plotDecontXMarkerPercentage <- function(x, markers, groupClusters = NULL,
286 286
 #' or list of character vectors
287 287
 #' with the names of the marker genes of interest.
288 288
 #' @param groupClusters List. A named list that allows
289
-#' cell clusterslabels coded in
289
+#' cell clusters labels coded in
290 290
 #' \code{z} to be regrouped and renamed on the fly. For example,
291 291
 #' \code{list(Tcells=c(1, 2), Bcells=7)} would recode clusters
292 292
 #' 1 and 2 to "Tcells"
... ...
@@ -301,7 +301,7 @@ plotDecontXMarkerPercentage <- function(x, markers, groupClusters = NULL,
301 301
 #' @param z Character, Integer, or Vector.
302 302
 #' Indicates the cluster labels for each cell.
303 303
 #' If \code{x} is a \linkS4class{SingleCellExperiment} and \code{z = NULL},
304
-#' then the cluster labels from \code{\link{decontX}} will be retived from the
304
+#' then the cluster labels from \code{\link{decontX}} will be retreived from the
305 305
 #' \code{colData} of \code{x} (i.e. \code{colData(x)$decontX_clusters}).
306 306
 #' If \code{z} is a single character or integer, then that column will be
307 307
 #' retrived from \code{colData} of \code{x}. (i.e. \code{colData(x)[,z]}).
Browse code

fix note

zhewa authored on 21/10/2020 20:20:32
Showing 1 changed files
... ...
@@ -160,6 +160,8 @@ plotDecontXMarkerPercentage <- function(x, markers, groupClusters = NULL,
160 160
                                         exactMatch = TRUE, by = "rownames",
161 161
                                         ncol = round(sqrt(length(markers))),
162 162
                                         labelBars = TRUE, labelSize = 3) {
163
+  cellTypeLabels <- percent <- NULL # fix check note
164
+
163 165
   legend <- "none"
164 166
   # Check that list arguments are named
165 167
   if (!is(markers, "list") || is.null(names(markers))) {
Browse code

fix no visible binding for global variable note

zhewa authored on 21/10/2020 18:06:56
Showing 1 changed files
... ...
@@ -224,9 +224,9 @@ plotDecontXMarkerPercentage <- function(x, markers, groupClusters = NULL,
224 224
   df <- cbind(df, markerLabels = names(markers)[df$markers])
225 225
   df$markerLabels <- factor(df$markerLabels, levels = names(markers))
226 226
 
227
-  plt <- ggplot2::ggplot(df, ggplot2::aes(
228
-    x = cellTypeLabels,
229
-    y = percent, fill = assay
227
+  plt <- ggplot2::ggplot(df, ggplot2::aes_string(
228
+    x = "cellTypeLabels",
229
+    y = "percent", fill = "assay"
230 230
   )) +
231 231
     ggplot2::geom_bar(
232 232
       stat = "identity",
Browse code

fix warnings in plot decontX ggplot

zhewa authored on 01/05/2020 12:10:38
Showing 1 changed files
... ...
@@ -225,8 +225,8 @@ plotDecontXMarkerPercentage <- function(x, markers, groupClusters = NULL,
225 225
   df$markerLabels <- factor(df$markerLabels, levels = names(markers))
226 226
 
227 227
   plt <- ggplot2::ggplot(df, ggplot2::aes(
228
-    x = df$cellTypeLabels,
229
-    y = df$percent, fill = assay
228
+    x = cellTypeLabels,
229
+    y = percent, fill = assay
230 230
   )) +
231 231
     ggplot2::geom_bar(
232 232
       stat = "identity",
... ...
@@ -261,9 +261,9 @@ plotDecontXMarkerPercentage <- function(x, markers, groupClusters = NULL,
261 261
 
262 262
   if (isTRUE(labelBars)) {
263 263
     plt <- plt + ggplot2::geom_text(ggplot2::aes(
264
-      x = df$cellTypeLabels,
265
-      y = df$percent + 2.5,
266
-      label = df$percent
264
+      x = cellTypeLabels,
265
+      y = percent + 2.5,
266
+      label = percent
267 267
     ),
268 268
     position = ggplot2::position_dodge2(width = 0.9, preserve = "single"),
269 269
     size = labelSize
Browse code

fix doc warning

zhewa authored on 29/04/2020 06:44:40
Showing 1 changed files
... ...
@@ -7,7 +7,7 @@
7 7
 #' @param batch Character. Batch of cells to plot. If \code{NULL}, then
8 8
 #' the first batch in the list will be selected. Default \code{NULL}.
9 9
 #' @param colorScale Character vector. Contains the color spectrum to be passed
10
-#' to \link[ggplot2]{scale_colour_gradientn} from package 'ggplot2'. Default
10
+#' to \code{scale_colour_gradientn} from package 'ggplot2'. Default
11 11
 #' c("blue","green","yellow","orange","red").
12 12
 #' @param size Numeric. Size of points in the scatterplot. Default 1.
13 13
 #' @return Returns a \code{ggplot} object.
Browse code

Ran styler and fixed lints

Joshua D. Campbell authored on 06/04/2020 23:58:56
Showing 1 changed files
... ...
@@ -1,7 +1,7 @@
1 1
 #' @title Plots contamination on UMAP coordinates
2 2
 #' @description A scatter plot of the UMAP dimensions generated by DecontX with
3 3
 #' cells colored by the estimated percentation of contamation.
4
-#' @param x Either a \linkS4class{SingleCellExperiment} with \code{decontX} 
4
+#' @param x Either a \linkS4class{SingleCellExperiment} with \code{decontX}
5 5
 #' results stored in \code{metadata(x)$decontX} or the result from running
6 6
 #' decontX on a count matrix.
7 7
 #' @param batch Character. Batch of cells to plot. If \code{NULL}, then
... ...
@@ -13,62 +13,76 @@
13 13
 #' @return Returns a \code{ggplot} object.
14 14
 #' @author Shiyi Yang, Joshua Campbell
15 15
 #' @seealso See \code{\link{decontX}} for a full example of how to estimate
16
-#' and plot contamination. 
16
+#' and plot contamination.
17 17
 #' @export
18 18
 plotDecontXContamination <- function(x,
19 19
                                      batch = NULL,
20
-                                     colorScale = c("blue",
21
-                                          "green",
22
-                                          "yellow",
23
-                                          "orange",
24
-                                          "red"),
20
+                                     colorScale = c(
21
+                                       "blue",
22
+                                       "green",
23
+                                       "yellow",
24
+                                       "orange",
25
+                                       "red"
26
+                                     ),
25 27
                                      size = 1) {
26
-  
27
-  if(inherits(x, "SingleCellExperiment")) {
28
-    estimates <- S4Vectors::metadata(x)$decontX$estimates 
28
+  if (inherits(x, "SingleCellExperiment")) {
29
+    estimates <- S4Vectors::metadata(x)$decontX$estimates
29 30
   } else {
30 31
     estimates <- x$estimates
31 32
   }
32
-  if(is.null(estimates)) {
33
+  if (is.null(estimates)) {
33 34
     stop("decontX estimates not found. Estimates will be found in
34 35
           'metadata(x)$decontX$estimates' if 'x' is a
35 36
           SingleCellExperiment or 'x$estimates' if decontX was run
36 37
           on a count matrix. Are you sure 'x' is output from decontX?")
37 38
   }
38 39
   batches <- names(estimates)
39
-  
40
-  if(is.null(batch)) {
40
+
41
+  if (is.null(batch)) {
41 42
     i <- batches[1]
42 43
   } else {
43
-    if(!(batch %in% batches)) {
44
-      stop("'", batch, "' is not one of the batches in 'x'. Batches available for plotting: '", paste(batches, collapse=","), "'")
44
+    if (!(batch %in% batches)) {
45
+      stop(
46
+        "'", batch, "' is not one of the batches in 'x'. Batches available",
47
+        " for plotting: '", paste(batches, collapse = ","), "'"
48
+      )
45 49
     }
46 50
     i <- batch
47 51
   }
48 52
 
49
-  contamin <- estimates[[i]]$contamination 
53
+  contamin <- estimates[[i]]$contamination
50 54
   umap <- estimates[[i]]$UMAP
51 55
 
52 56
   ## Create data.frame
53 57
   df <- data.frame(umap, "Contamination" = contamin)
54
-  naIx <- is.na(umap[,1]) | is.na(umap[,2])
58
+  naIx <- is.na(umap[, 1]) | is.na(umap[, 2])
55 59
   df <- df[!naIx, ]
56 60
 
57 61
   ## Generate ggplot scatterplot
58
-  gg <- ggplot2::ggplot(df,
59
-                        ggplot2::aes_string(x = colnames(umap)[1],
60
-                                            y = colnames(umap)[2])) +
61
-        ggplot2::geom_point(stat = "identity",
62
-                            size = size,
63
-                            ggplot2::aes_string(color = "Contamination")) +
64
-        ggplot2::theme_bw() +
65
-        ggplot2::scale_colour_gradientn(colors = colorScale,
66
-                                    name = "Contamination",
67
-                                    limits = c(0,1)) +
68
-        ggplot2::theme(panel.grid.major = ggplot2::element_blank(),
69
-                       panel.grid.minor = ggplot2::element_blank(),
70
-                       axis.text = ggplot2::element_text(size=15),
71
-                       axis.title = ggplot2::element_text(size=15))
62
+  gg <- ggplot2::ggplot(
63
+    df,
64
+    ggplot2::aes_string(
65
+      x = colnames(umap)[1],
66
+      y = colnames(umap)[2]
67
+    )
68
+  ) +
69
+    ggplot2::geom_point(
70
+      stat = "identity",
71
+      size = size,
72
+      ggplot2::aes_string(color = "Contamination")
73
+    ) +
74
+    ggplot2::theme_bw() +
75
+    ggplot2::scale_colour_gradientn(
76
+      colors = colorScale,
77
+      name = "Contamination",
78
+      limits = c(0, 1)
79
+    ) +
80
+    ggplot2::theme(
81
+      panel.grid.major = ggplot2::element_blank(),
82
+      panel.grid.minor = ggplot2::element_blank(),
83
+      axis.text = ggplot2::element_text(size = 15),
84
+      axis.title = ggplot2::element_text(size = 15)
85
+    )
72 86
   return(gg)
73 87
 }
74 88
 
... ...
@@ -76,32 +90,46 @@ plotDecontXContamination <- function(x,
76 90
 #' @title Plots percentage of cells cell types expressing markers
77 91
 #' @description Generates a barplot that shows the percentage of
78 92
 #' cells within clusters or cell types that have detectable levels
79
-#' of given marker genes. Can be used to view the expression of 
93
+#' of given marker genes. Can be used to view the expression of
80 94
 #' marker genes in different cell types before and after
81
-#' decontamination with \code{\link{decontX}}.  
82
-#' @param x Either a \linkS4class{SingleCellExperiment} or a matrix-like object of counts.
83
-#' @param markers List. A named list indicating the marker genes for each cell type of
95
+#' decontamination with \code{\link{decontX}}.
96
+#' @param x Either a \linkS4class{SingleCellExperiment} or
97
+#' a matrix-like object of counts.
98
+#' @param markers List. A named list indicating the marker genes
99
+#' for each cell type of
84 100
 #' interest. Multiple markers can be supplied for each cell type. For example,
85
-#' \code{list(Tcell_Markers=c("CD3E", "CD3D"), Bcell_Markers=c("CD79A", "CD79B", "MS4A1")}
86
-#' would specify markers for human T-cells and B-cells. A cell will be considered 
87
-#' "positive" for a cell type if it has a count greater than \code{threshold} for at least
88
-#' one of the marker genes in the list. 
89
-#' @param groupClusters List. A named list that allows cell clusters labels coded in
101
+#' \code{list(Tcell_Markers=c("CD3E", "CD3D"),
102
+#' Bcell_Markers=c("CD79A", "CD79B", "MS4A1")}
103
+#' would specify markers for human T-cells and B-cells.
104
+#' A cell will be considered
105
+#' "positive" for a cell type if it has a count greater than \code{threshold}
106
+#' for at least one of the marker genes in the list.
107
+#' @param groupClusters List. A named list that allows
108
+#' cell clusters labels coded in
90 109
 #' \code{z} to be regrouped and renamed on the fly. For example,
91
-#' \code{list(Tcells=c(1, 2), Bcells=7)} would recode clusters 1 and 2 to "Tcells"
92
-#' and cluster 7 to "Bcells". Note that if this is used, clusters in \code{z} not found
93
-#' in \code{groupClusters} will be excluded from the barplot. Default \code{NULL}.
94
-#' @param assayName Character vector. Name(s) of the assay(s) to plot if \code{x} is a
95
-#' \linkS4class{SingleCellExperiment}. If more than one assay is listed, then 
96
-#' side-by-side barplots will be generated. Default \code{c("counts", "decontXcounts")}.
97
-#' @param z Character, Integer, or Vector. Indicates the cluster labels for each cell. 
98
-#' If \code{x} is a \linkS4class{SingleCellExperiment} and \code{z = NULL}, then 
99
-#' the cluster labels from \code{\link{decontX}} will be retived from the
110
+#' \code{list(Tcells=c(1, 2), Bcells=7)} would recode
111
+#' clusters 1 and 2 to "Tcells"
112
+#' and cluster 7 to "Bcells". Note that if this is
113
+#' used, clusters in \code{z} not found
114
+#' in \code{groupClusters} will be excluded from the barplot.
115
+#' Default \code{NULL}.
116
+#' @param assayName Character vector. Name(s) of the assay(s) to
117
+#' plot if \code{x} is a
118
+#' \linkS4class{SingleCellExperiment}. If more than one assay
119
+#' is listed, then side-by-side barplots will be generated.
120
+#' Default \code{c("counts", "decontXcounts")}.
121
+#' @param z Character, Integer, or Vector. Indicates the cluster labels
122
+#' for each cell.
123
+#' If \code{x} is a \linkS4class{SingleCellExperiment} and \code{z = NULL},
124
+#' then the cluster labels from \code{\link{decontX}} will be retived from the
100 125
 #' \code{colData} of \code{x} (i.e. \code{colData(x)$decontX_clusters}).
101
-#' If \code{z} is a single character or integer, then that column will be retrived
126
+#' If \code{z} is a single character or integer,
127
+#' then that column will be retrived
102 128
 #' from \code{colData} of \code{x}. (i.e. \code{colData(x)[,z]}). If \code{x}
103
-#' is a counts matrix, then \code{z} will need to be a vector the same length as
104
-#' the number of columns in \code{x} that indicate the cluster to which each cell
129
+#' is a counts matrix, then \code{z} will need
130
+#' to be a vector the same length as
131
+#' the number of columns in \code{x} that indicate
132
+#' the cluster to which each cell
105 133
 #' belongs. Default \code{NULL}.
106 134
 #' @param threshold Numeric. Markers greater than or equal to this value will
107 135
 #' be considered detected in a cell. Default 1.
... ...
@@ -109,112 +137,139 @@ plotDecontXContamination <- function(x,
109 137
 #' for the markers or to identify partial matches using \code{\link{grep}}. See
110 138
 #' \code{\link{retrieveFeatureIndex}} for more details. Default \code{TRUE}.
111 139
 #' @param by Character. Where to search for the markers if \code{x} is a
112
-#' \linkS4class{SingleCellExperiment}. See \code{\link{retrieveFeatureIndex}} for
113
-#' more details. If \code{x} is a matrix, then this must be set to \code{"rownames"}.
114
-#' Default \code{"rownames"}.
140
+#' \linkS4class{SingleCellExperiment}. See \code{\link{retrieveFeatureIndex}}
141
+#' for more details. If \code{x} is a matrix,
142
+#' then this must be set to \code{"rownames"}.Default \code{"rownames"}.
115 143
 #' @param ncol Integer. Number of columns to make in the plot.
116
-#' Default \code{round(sqrt(length(markers))}. 
117
-#' @param labelBars Boolean. Whether to display percentages above each bar Default
118
-#' \code{TRUE}.
119
-#' @param labelSize Numeric. Size of the percentage labels in the barplot. Default 3.
144
+#' Default \code{round(sqrt(length(markers))}.
145
+#' @param labelBars Boolean. Whether to display percentages above each bar
146
+#' Default \code{TRUE}.
147
+#' @param labelSize Numeric. Size of the percentage labels in the barplot.
148
+#' Default 3.
120 149
 #' @return Returns a \code{ggplot} object.
121 150
 #' @author Shiyi Yang, Joshua Campbell
122 151
 #' @seealso See \code{\link{decontX}} for a full example of how to estimate
123
-#' and plot contamination. 
152
+#' and plot contamination.
124 153
 #' @export
125 154
 plotDecontXMarkerPercentage <- function(x, markers, groupClusters = NULL,
126
-                               assayName = c("counts", "decontXcounts"),
127
-                               z = NULL, threshold = 1,
128
-                               exactMatch = TRUE, by = "rownames", 
129
-                               ncol = round(sqrt(length(markers))),
130
-                               labelBars = TRUE, labelSize = 3) {
155
+                                        assayName = c(
156
+                                          "counts",
157
+                                          "decontXcounts"
158
+                                        ),
159
+                                        z = NULL, threshold = 1,
160
+                                        exactMatch = TRUE, by = "rownames",
161
+                                        ncol = round(sqrt(length(markers))),
162
+                                        labelBars = TRUE, labelSize = 3) {
163
+  legend <- "none"
164
+  # Check that list arguments are named
165
+  if (!is(markers, "list") || is.null(names(markers))) {
166
+    stop("'markers' needs to be a named list.")
167
+  }
131 168
 
132
-    legend <- "none"
133
-    # Check that list arguments are named
134
-    if(!is(markers, "list") || is.null(names(markers))) {
135
-      stop("'markers' needs to be a named list.")
169
+  temp <- .processPlotDecontXMarkerInupt(
170
+    x = x,
171
+    z = z,
172
+    markers = markers,
173
+    groupClusters = groupClusters,
174
+    by = by,
175
+    exactMatch = exactMatch
176
+  )
177
+  x <- temp$x
178
+  z <- temp$z
179
+  geneMarkerIndex <- temp$geneMarkerIndex
180
+  geneMarkerCellTypeIndex <- temp$geneMarkerCellTypeIndex
181
+  groupClusters <- temp$groupClusters
182
+  xlab <- temp$xlab
183
+
184
+  if (inherits(x, "SingleCellExperiment")) {
185
+    # If 'x' is SingleCellExperiment, then get percentage
186
+    # for each matrix in 'assayName'
187
+    df.list <- list()
188
+    for (i in seq_along(assayName)) {
189
+      counts <- SummarizedExperiment::assay(
190
+        x[geneMarkerIndex, ],
191
+        assayName[i]
192
+      )
193
+      df <- .calculateDecontXBarplotPercent(
194
+        counts,
195
+        z,
196
+        geneMarkerCellTypeIndex,
197
+        threshold
198
+      )
199
+      df.list[[i]] <- cbind(df, assay = assayName[i])
136 200
     }
137
-    
138
-    temp <- .processPlotDecontXMarkerInupt(x = x,
139
-                                           z = z,
140
-                                           markers = markers,
141
-                                           groupClusters = groupClusters,
142
-                                           by = by,
143
-                                           exactMatch = exactMatch)
144
-    x <- temp$x
145
-    z <- temp$z
146
-    geneMarkerIndex <- temp$geneMarkerIndex
147
-    geneMarkerCellTypeIndex <- temp$geneMarkerCellTypeIndex
148
-    groupClusters <- temp$groupClusters
149
-    xlab <- temp$xlab
150
-    
151
-    if(inherits(x, "SingleCellExperiment")) {
152
-      # If 'x' is SingleCellExperiment, then get percentage
153
-      # for each matrix in 'assayName'
154
-      df.list <- list()
155
-      for(i in seq_along(assayName)) {
156
-        counts <- SummarizedExperiment::assay(x[geneMarkerIndex,], assayName[i])  
157
-        df <- .calculateDecontXBarplotPercent(counts,
158
-                                        z,
159
-                                        geneMarkerCellTypeIndex,
160
-                                        threshold)
161
-        df.list[[i]] <- cbind(df, assay = assayName[i])
162
-      }
163
-      df <- do.call(rbind, df.list)
164
-      assay <- as.factor(df$assay)
165
-      if(length(assayName) > 1) {
166
-        legend <- "right"
167
-      }
168
-    } else {
169
-      ## If 'x' is matrix, then calculate percentages directly
170
-      counts <- x[geneMarkerIndex,]
171
-      df <- .calculateDecontXBarplotPercent(counts,
172
-                                            z,
173
-                                            geneMarkerCellTypeIndex,
174
-                                            threshold)
175
-      assay <- "red3"
176
-      legend <- "none"
201
+    df <- do.call(rbind, df.list)
202
+    assay <- as.factor(df$assay)
203
+    if (length(assayName) > 1) {
204
+      legend <- "right"
177 205
     }
206
+  } else {
207
+    ## If 'x' is matrix, then calculate percentages directly
208
+    counts <- x[geneMarkerIndex, ]
209
+    df <- .calculateDecontXBarplotPercent(
210
+      counts,
211
+      z,
212
+      geneMarkerCellTypeIndex,
213
+      threshold
214
+    )
215
+    assay <- "red3"
216
+    legend <- "none"
217
+  }
178 218
 
179
-    # Build data.frame for ggplots
180
-    df <- cbind(df, cellTypeLabels = names(groupClusters)[df$cellType])
181
-    df$cellTypeLabels <- factor(df$cellTypeLabels, levels = names(groupClusters))
182
-    df <- cbind(df, markerLabels = names(markers)[df$markers])
183
-    df$markerLabels <- factor(df$markerLabels, levels = names(markers))
184
-    
185
-    plt <- ggplot2::ggplot(df, ggplot2::aes(x = df$cellTypeLabels,
186
-        y = df$percent, fill = assay)) +
187
-        ggplot2::geom_bar(stat = "identity",
188
-                position = ggplot2::position_dodge2(width = 0.9, preserve = "single")) +
189
-        ggplot2::xlab(xlab) +
190
-        ggplot2::ylab(paste0("Percentage of cells expressing markers")) +
191
-        ggplot2::facet_wrap(. ~ df$markerLabels, ncol = ncol) +
192
-        ggplot2::theme(
193
-            panel.background = ggplot2::element_rect(fill = "white",
194
-                color = "grey"),
195
-            panel.grid = ggplot2::element_line("grey"),
196
-            legend.position = legend,
197
-            legend.key = ggplot2::element_rect(fill = "white",
198
-                color = "white"),
199
-            panel.grid.minor = ggplot2::element_blank(),
200
-            panel.grid.major = ggplot2::element_blank(),
201
-            text = ggplot2::element_text(size = 10),
202
-            axis.text.x = ggplot2::element_text(size = 8, angle = 45,
203
-                hjust = 1),
204
-            axis.text.y = ggplot2::element_text(size = 9),
205
-            legend.key.size = grid::unit(8, "mm"),
206
-            legend.text = ggplot2::element_text(size = 10),
207
-            strip.text.x = ggplot2::element_text(size = 10)
208
-        )
209
-
210
-    if(isTRUE(labelBars)){
211
-      plt <- plt + ggplot2::geom_text(ggplot2::aes(x = df$cellTypeLabels,
212
-                  y = df$percent + 2.5,
213
-                  label = df$percent),
214
-                  position = ggplot2::position_dodge2(width = 0.9, preserve = "single"),
215
-                  size = labelSize)
216
-    }
217
-    return(plt)
219
+  # Build data.frame for ggplots
220
+  df <- cbind(df, cellTypeLabels = names(groupClusters)[df$cellType])
221
+  df$cellTypeLabels <- factor(df$cellTypeLabels,
222
+    levels = names(groupClusters)
223
+  )
224
+  df <- cbind(df, markerLabels = names(markers)[df$markers])
225
+  df$markerLabels <- factor(df$markerLabels, levels = names(markers))
226
+
227
+  plt <- ggplot2::ggplot(df, ggplot2::aes(
228
+    x = df$cellTypeLabels,
229
+    y = df$percent, fill = assay
230
+  )) +
231
+    ggplot2::geom_bar(
232
+      stat = "identity",
233
+      position = ggplot2::position_dodge2(width = 0.9, preserve = "single")
234
+    ) +
235
+    ggplot2::xlab(xlab) +
236
+    ggplot2::ylab(paste0("Percentage of cells expressing markers")) +
237
+    ggplot2::facet_wrap(. ~ df$markerLabels, ncol = ncol) +
238
+    ggplot2::theme(
239
+      panel.background = ggplot2::element_rect(
240
+        fill = "white",
241
+        color = "grey"
242
+      ),
243
+      panel.grid = ggplot2::element_line("grey"),
244
+      legend.position = legend,
245
+      legend.key = ggplot2::element_rect(
246
+        fill = "white",
247
+        color = "white"
248
+      ),
249
+      panel.grid.minor = ggplot2::element_blank(),
250
+      panel.grid.major = ggplot2::element_blank(),
251
+      text = ggplot2::element_text(size = 10),
252
+      axis.text.x = ggplot2::element_text(
253
+        size = 8, angle = 45,
254
+        hjust = 1
255
+      ),
256
+      axis.text.y = ggplot2::element_text(size = 9),
257
+      legend.key.size = grid::unit(8, "mm"),
258
+      legend.text = ggplot2::element_text(size = 10),
259
+      strip.text.x = ggplot2::element_text(size = 10)
260
+    )
261
+
262
+  if (isTRUE(labelBars)) {
263
+    plt <- plt + ggplot2::geom_text(ggplot2::aes(
264
+      x = df$cellTypeLabels,
265
+      y = df$percent + 2.5,
266
+      label = df$percent
267
+    ),
268
+    position = ggplot2::position_dodge2(width = 0.9, preserve = "single"),
269
+    size = labelSize
270
+    )
271
+  }
272
+  return(plt)
218 273
 }
219 274
 
220 275
 
... ...
@@ -222,39 +277,48 @@ plotDecontXMarkerPercentage <- function(x, markers, groupClusters = NULL,
222 277
 #' @description Generates a violin plot that shows the counts of marker
223 278
 #' genes in cells across specific clusters or cell types. Can be used to view
224 279
 #' the expression of marker genes in different cell types before and after
225
-#' decontamination with \code{\link{decontX}}.  
226
-#' @param x Either a \linkS4class{SingleCellExperiment} or a matrix-like object of counts.
227
-#' @param markers Character Vector or List. A character vector or list of character vectors
228
-#' with the names of the marker genes of interest.  
229
-#' @param groupClusters List. A named list that allows cell clusters labels coded in
280
+#' decontamination with \code{\link{decontX}}.
281
+#' @param x Either a \linkS4class{SingleCellExperiment}
282
+#' or a matrix-like object of counts.
283
+#' @param markers Character Vector or List. A character vector
284
+#' or list of character vectors
285
+#' with the names of the marker genes of interest.
286
+#' @param groupClusters List. A named list that allows
287
+#' cell clusterslabels coded in
230 288
 #' \code{z} to be regrouped and renamed on the fly. For example,
231
-#' \code{list(Tcells=c(1, 2), Bcells=7)} would recode clusters 1 and 2 to "Tcells"
232
-#' and cluster 7 to "Bcells". Note that if this is used, clusters in \code{z} not found
289
+#' \code{list(Tcells=c(1, 2), Bcells=7)} would recode clusters
290
+#' 1 and 2 to "Tcells"
291
+#' and cluster 7 to "Bcells". Note that if this is used, clusters
292
+#' in \code{z} not found
233 293
 #' in \code{groupClusters} will be excluded. Default \code{NULL}.
234
-#' @param assayName Character vector. Name(s) of the assay(s) to plot if \code{x} is a
235
-#' \linkS4class{SingleCellExperiment}. If more than one assay is listed, then 
236
-#' side-by-side violin plots will be generated. Default \code{c("counts", "decontXcounts")}.
237
-#' @param z Character, Integer, or Vector. Indicates the cluster labels for each cell. 
238
-#' If \code{x} is a \linkS4class{SingleCellExperiment} and \code{z = NULL}, then 
239
-#' the cluster labels from \code{\link{decontX}} will be retived from the
294
+#' @param assayName Character vector. Name(s) of the assay(s) to
295
+#' plot if \code{x} is a
296
+#' \linkS4class{SingleCellExperiment}. If more than one assay is listed, then
297
+#' side-by-side violin plots will be generated.
298
+#' Default \code{c("counts", "decontXcounts")}.
299
+#' @param z Character, Integer, or Vector.
300
+#' Indicates the cluster labels for each cell.
301
+#' If \code{x} is a \linkS4class{SingleCellExperiment} and \code{z = NULL},
302
+#' then the cluster labels from \code{\link{decontX}} will be retived from the
240 303
 #' \code{colData} of \code{x} (i.e. \code{colData(x)$decontX_clusters}).
241
-#' If \code{z} is a single character or integer, then that column will be retrived
242
-#' from \code{colData} of \code{x}. (i.e. \code{colData(x)[,z]}). If \code{x}
243
-#' is a counts matrix, then \code{z} will need to be a vector the same length as
244
-#' the number of columns in \code{x} that indicate the cluster to which each cell
245
-#' belongs. Default \code{NULL}.
304
+#' If \code{z} is a single character or integer, then that column will be
305
+#' retrived from \code{colData} of \code{x}. (i.e. \code{colData(x)[,z]}).
306
+#' If \code{x} is a counts matrix, then \code{z} will need to be a vector
307
+#' the same length as the number of columns in \code{x} that indicate
308
+#' the cluster to which each cell belongs. Default \code{NULL}.
246 309
 #' @param exactMatch Boolean. Whether to only identify exact matches
247
-#' for the markers or to identify partial matches using \code{\link{grep}}. See
248
-#' \code{\link{retrieveFeatureIndex}} for more details Default \code{TRUE}.
310
+#' for the markers or to identify partial matches using \code{\link{grep}}.
311
+#' See \code{\link{retrieveFeatureIndex}} for more details.
312
+#' Default \code{TRUE}.
249 313
 #' @param by Character. Where to search for the markers if \code{x} is a
250
-#' \linkS4class{SingleCellExperiment}. See \code{\link{retrieveFeatureIndex}} for
251
-#' more details. If \code{x} is a matrix, then this must be set to \code{"rownames"}.
252
-#' Default \code{"rownames"}.
314
+#' \linkS4class{SingleCellExperiment}. See \code{\link{retrieveFeatureIndex}}
315
+#' for more details. If \code{x} is a matrix, then this must be set to
316
+#' \code{"rownames"}. Default \code{"rownames"}.
253 317
 #' @param log1p Boolean. Whether to apply the function \code{log1p} to the data
254
-#' before plotting. This function will add a pseudocount of 1 and then log 
255
-#' transform the expression values. Default \code{FALSE}. 
318
+#' before plotting. This function will add a pseudocount of 1 and then log
319
+#' transform the expression values. Default \code{FALSE}.
256 320
 #' @param ncol Integer. Number of columns to make in the plot.
257
-#' Default \code{NULL}. 
321
+#' Default \code{NULL}.
258 322
 #' @param plotDots Boolean. If \code{TRUE}, the
259 323
 #'  expression of features will be plotted as points in addition to the violin
260 324
 #'  curve. Default \code{FALSE}.
... ...
@@ -263,59 +327,69 @@ plotDecontXMarkerPercentage <- function(x, markers, groupClusters = NULL,
263 327
 #' @return Returns a \code{ggplot} object.
264 328
 #' @author Shiyi Yang, Joshua Campbell
265 329
 #' @seealso See \code{\link{decontX}} for a full example of how to estimate
266
-#' and plot contamination. 
330
+#' and plot contamination.
267 331
 #' @export
268 332
 plotDecontXMarkerExpression <- function(x, markers, groupClusters = NULL,
269
-                                        assayName = c("counts", "decontXcounts"),
333
+                                        assayName = c(
334
+                                          "counts",
335
+                                          "decontXcounts"
336
+                                        ),
270 337
                                         z = NULL, exactMatch = TRUE,
271 338
                                         by = "rownames", log1p = FALSE,
272 339
                                         ncol = NULL,
273
-                                        plotDots = FALSE, dotSize = 0.1){
340
+                                        plotDots = FALSE, dotSize = 0.1) {
274 341
   legend <- "none"
275
-  temp <- .processPlotDecontXMarkerInupt(x = x,
276
-                                 z = z,
277
-                                 markers = markers,
278
-                                 groupClusters = groupClusters,
279
-                                 by = by,
280
-                                 exactMatch = exactMatch)
342
+  temp <- .processPlotDecontXMarkerInupt(
343
+    x = x,
344
+    z = z,
345
+    markers = markers,
346
+    groupClusters = groupClusters,
347
+    by = by,
348
+    exactMatch = exactMatch
349
+  )
281 350
   x <- temp$x
282 351
   z <- temp$z
283 352
   geneMarkerIndex <- temp$geneMarkerIndex
284 353
   groupClusters <- temp$groupClusters
285 354
   xlab <- temp$xlab
286
-    
287
-  if(inherits(x, "SingleCellExperiment")) {
355
+
356
+  if (inherits(x, "SingleCellExperiment")) {
288 357
     # If 'x' is SingleCellExperiment, then get percentage
289 358
     # for each matrix in 'assayName'
290 359
     df.list <- list()
291
-    for(i in seq_along(assayName)) {
292
-      counts <- SummarizedExperiment::assay(x[geneMarkerIndex,], assayName[i])  
360
+    for (i in seq_along(assayName)) {
361
+      counts <- SummarizedExperiment::assay(
362
+        x[geneMarkerIndex, ],
363
+        assayName[i]
364
+      )
293 365
       df <- reshape2::melt(as.matrix(counts),
294
-                           varnames = c("Marker", "Cell"),
295
-                           value.name = "Expression")
366
+        varnames = c("Marker", "Cell"),
367
+        value.name = "Expression"
368
+      )
296 369
       df.list[[i]] <- cbind(df, assay = assayName[i])
297 370
     }
298 371
     df <- do.call(rbind, df.list)
299 372
     assay <- as.factor(df$assay)
300
-    if(length(assayName) > 1) {
373
+    if (length(assayName) > 1) {
301 374
       legend <- "right"
302 375
     }
303 376
   } else {
304 377
     ## If 'x' is matrix, then calculate percentages directly
305
-    counts <- x[geneMarkerIndex,]
378
+    counts <- x[geneMarkerIndex, ]
306 379
     df <- reshape2::melt(counts,
307
-                         varnames = c("Marker", "Cell"),
308
-                         value.name = "Expression")
380
+      varnames = c("Marker", "Cell"),
381
+      value.name = "Expression"
382
+    )
309 383
     assay <- "red3"
310 384
     legend <- "none"
311 385
   }
312 386
 
313
-  # Create data.frame and add cell type groups back in  
387
+  # Create data.frame and add cell type groups back in
314 388
   names(z) <- colnames(x)
315
-  df <- cbind(df, Cluster=z[df$Cell])
316
-  
389
+  df <- cbind(df, Cluster = z[df$Cell])
390
+
317 391
   ylab <- "Expression"
318
-  if(isTRUE(log1p)) {
392
+  if (isTRUE(log1p)) {
319 393
     df$Expression <- log1p(df$Expression)
320 394
     ylab <- "Expression (log1p)"
321 395
   }
... ...
@@ -323,92 +397,105 @@ plotDecontXMarkerExpression <- function(x, markers, groupClusters = NULL,
323 397
   Marker <- df$Marker
324 398
   Assay <- df$assay
325 399
   Cluster <- df$Cluster
326
-  if(!is.null(groupClusters)) {
400
+  if (!is.null(groupClusters)) {
327 401
     df <- cbind(df, Cell_Type = names(groupClusters)[Cluster])
328 402
     Cell_Type <- factor(df$Cell_Type, levels = names(groupClusters))
329
-    plt <- ggplot2::ggplot(df, ggplot2::aes(x = Cell_Type,
330
-                                            y = Expression,
331
-                                            fill = Assay)) +
332
-      ggplot2::facet_wrap( ~ Cell_Type + Marker,
333
-                           scales = "free",
334
-                           labeller = ggplot2::label_context,
335
-                           ncol = ncol) 
403
+    plt <- ggplot2::ggplot(df, ggplot2::aes(
404
+      x = Cell_Type,
405
+      y = Expression,
406
+      fill = Assay
407
+    )) +
408
+      ggplot2::facet_wrap(~ Cell_Type + Marker,
409
+        scales = "free",
410
+        labeller = ggplot2::label_context,
411
+        ncol = ncol
412
+      )
336 413
   } else {
337
-    plt <- ggplot2::ggplot(df, ggplot2::aes(x = Cluster,
338
-                                            y = Expression,
339
-                                            fill = Assay)) +
340
-      ggplot2::facet_wrap( ~ Cluster + Marker,
341
-                           scales = "free",
342
-                           labeller = ggplot2::label_context,
343
-                           ncol = ncol)
344
-  } 
345
-    plt <- plt + ggplot2::geom_violin(trim = TRUE,
346
-                         scale = "width") + 
347
-    ggplot2::theme_bw() + ggplot2::theme(axis.text.x = ggplot2::element_blank(),
348
-                       axis.ticks.x = ggplot2::element_blank(),
349
-                       axis.title.x = ggplot2::element_blank(),
350
-                       strip.text = ggplot2::element_text(size=8),
351
-                       panel.grid = ggplot2::element_blank(),
352
-                       legend.position = legend) + ggplot2::ylab(ylab)
353
-  
354
-  
355
-  if(isTRUE(plotDots)) {
414
+    plt <- ggplot2::ggplot(df, ggplot2::aes(
415
+      x = Cluster,
416
+      y = Expression,
417
+      fill = Assay
418
+    )) +
419
+      ggplot2::facet_wrap(~ Cluster + Marker,
420
+        scales = "free",
421
+        labeller = ggplot2::label_context,
422
+        ncol = ncol
423
+      )
424
+  }
425
+  plt <- plt + ggplot2::geom_violin(
426
+    trim = TRUE,
427
+    scale = "width"
428
+  ) +
429
+    ggplot2::theme_bw() + ggplot2::theme(
430
+      axis.text.x = ggplot2::element_blank(),
431
+      axis.ticks.x = ggplot2::element_blank(),
432
+      axis.title.x = ggplot2::element_blank(),
433
+      strip.text = ggplot2::element_text(size = 8),
434
+      panel.grid = ggplot2::element_blank(),
435
+      legend.position = legend
436
+    ) + ggplot2::ylab(ylab)
437
+
438
+
439
+  if (isTRUE(plotDots)) {
356 440
     plt <- plt + ggplot2::geom_jitter(height = 0, size = dotSize)
357 441
   }
358
-  
442
+
359 443
   return(plt)
360
-}  
444
+}
361 445
 
362 446
 
363 447
 
364 448
 .processPlotDecontXMarkerInupt <- function(x, z, markers, groupClusters,
365 449
                                            by, exactMatch) {
366
-  
450
+
367 451
   # Process z and convert to a factor
368
-  if(is.null(z) & inherits(x, "SingleCellExperiment")) {
369
-    if(!("decontX_clusters" %in% colnames(SummarizedExperiment::colData(x)))) {
452
+  if (is.null(z) & inherits(x, "SingleCellExperiment")) {
453
+    cn <- colnames(SummarizedExperiment::colData(x))
454
+    if (!("decontX_clusters" %in% cn)) {
370 455
       stop("'decontX_clusters' not found in 'colData(x)'. Make sure you have
371 456
            run 'decontX' or supply 'z' directly.")
372 457
     }
373 458
     z <- SummarizedExperiment::colData(x)$decontX_clusters
374
-    } else if (length(z) == 1 & inherits(x, "SingleCellExperiment")) {
375
-      if(!(z %in% colnames(SummarizedExperiment::colData(x)))) {
376
-        stop("'", z, "' not found in 'colData(x)'.")
377
-      }
378
-      z <- SummarizedExperiment::colData(x)[,z]
379
-    } else if(length(z) != ncol(x)) {
380
-      stop(" if 'x' is a SingleCellExperiment, then 'z' needs to be 
381
-           a single character or integer specifying the column in
382
-           'colData(x)'. Alternatively to specify the cell cluster
383
-           labels directly as a vector, the length of 'z' needs to
384
-           be the same as the number of columns in 'x'. This is 
385
-           required if 'x' is a matrix.")
459
+  } else if (length(z) == 1 & inherits(x, "SingleCellExperiment")) {
460
+    if (!(z %in% colnames(SummarizedExperiment::colData(x)))) {
461
+      stop("'", z, "' not found in 'colData(x)'.")
386 462
     }
387
-  if(!is.factor(z)) {
463
+    z <- SummarizedExperiment::colData(x)[, z]
464
+  } else if (length(z) != ncol(x)) {
465
+    stop("If 'x' is a SingleCellExperiment, then 'z' needs to be",
466
+          " a single character or integer specifying the column in",
467
+          " 'colData(x)'. Alternatively to specify the cell cluster",
468
+          " labels directly as a vector, the length of 'z' needs to",
469
+          " be the same as the number of columns in 'x'. This is",
470
+          " required if 'x' is a matrix.")
471
+  }
472
+  if (!is.factor(z)) {
388 473
     z <- as.factor(z)
389 474
   }
390
-  
391
-  if(!is.null(groupClusters)) {
392
-    if(!is(groupClusters, "list") || is.null(names(groupClusters))) {
475
+
476
+  if (!is.null(groupClusters)) {
477
+    if (!is(groupClusters, "list") || is.null(names(groupClusters))) {
393 478
       stop("'groupClusters' needs to be a named list.")
394 479
     }
395
-    
396
-    # Check that groupClusters are found in 'z'  
480
+
481
+    # Check that groupClusters are found in 'z'
397 482
     cellMappings <- unlist(groupClusters)
398
-    if(any(!(cellMappings %in% z))) {
483
+    if (any(!(cellMappings %in% z))) {
399 484
       missing <- cellMappings[!(cellMappings %in% z)]
400
-      stop("'groupClusters' not found in 'z': ",
401
-           paste(missing, collapse=","))
485
+      stop(
486
+        "'groupClusters' not found in 'z': ",
487
+        paste(missing, collapse = ",")
488
+      )
402 489
     }
403
-    
490
+
404 491
     labels <- rep(NA, ncol(x))
405
-    for(i in seq_along(groupClusters)) {
492
+    for (i in seq_along(groupClusters)) {
406 493
       labels[z %in% groupClusters[i]] <- names(groupClusters)[i]
407 494
     }
408 495
     na.ix <- is.na(labels)
409 496
     labels <- labels[!na.ix]
410
-    x <- x[,!na.ix]
411
-    z <- as.integer(factor(labels, levels=names(groupClusters)))
497
+    x <- x[, !na.ix]
498
+    z <- as.integer(factor(labels, levels = names(groupClusters)))
412 499
     xlab <- "Cell types"
413 500
   } else {
414 501
     labels <- as.factor(z)
... ...
@@ -416,47 +503,56 @@ plotDecontXMarkerExpression <- function(x, markers, groupClusters = NULL,
416 503
     names(groupClusters) <- levels(labels)
417 504
     xlab <- "Clusters"
418 505
   }
419
-  
506
+
420 507
   # Find index of each feature in 'x'
421
-  geneMarkerCellTypeIndex <- rep(seq(length(markers)),
422