Browse code

Merge pull request #167 from zhewa/RELEASE_3_9

subset rows and columns before row scaling fix #164

Zhe Wang authored on 14/05/2019 16:02:37 • GitHub committed on 14/05/2019 16:02:37
Showing 2 changed files

... ...
@@ -9,7 +9,7 @@
9 9
 #'  NULL, no subsetting will be performed. Default NULL.
10 10
 #' @param cellIx Integer vector. Select cells for display in heatmap. If NULL,
11 11
 #'  no subsetting will be performed. Default NULL.
12
-#' @param scaleRow Function; A function to scale each individual row. Set to
12
+#' @param scaleRow Function. A function to scale each individual row. Set to
13 13
 #'  NULL to disable. Occurs after normalization and log transformation. Defualt
14 14
 #'  is 'scale' and thus will Z-score transform each row.
15 15
 #' @param trim Numeric vector. Vector of length two that specifies the lower
... ...
@@ -116,26 +116,6 @@ plotHeatmap <- function(counts,
116 116
 
117 117
     colorScheme <- match.arg(colorScheme)
118 118
 
119
-    if (!is.null(scaleRow)) {
120
-        if (is.function(scaleRow)) {
121
-            cn <- colnames(counts)
122
-            counts <- t(base::apply(counts, 1, scaleRow))
123
-            colnames(counts) <- cn
124
-        } else {
125
-            stop("'scaleRow' needs to be of class 'function'")
126
-        }
127
-    }
128
-
129
-    if (!is.null(trim)) {
130
-        if (length(trim) != 2) {
131
-            stop("'trim' should be a 2 element vector specifying the lower",
132
-                " and upper boundaries")
133
-        }
134
-        trim <- sort(trim)
135
-        counts[counts < trim[1]] <- trim[1]
136
-        counts[counts > trim[2]] <- trim[2]
137
-    }
138
-
139 119
     ## Create cell annotation
140 120
     if (!is.null(annotationCell) & !is.null(z)) {
141 121
         if (is.null(rownames(annotationCell))) {
... ...
@@ -173,6 +153,29 @@ plotHeatmap <- function(counts,
173 153
         annotationFeature <- NA
174 154
     }
175 155
 
156
+    ## Select subsets of features/cells
157
+    if (!is.null(featureIx)) {
158
+        counts <- counts[featureIx, , drop = FALSE]
159
+        if (length(annotationFeature) > 1 ||
160
+                (length(annotationFeature) == 1 & !is.na(annotationFeature))) {
161
+            annotationFeature <- annotationFeature[featureIx, , drop = FALSE]
162
+        }
163
+        if (!is.null(y)) {
164
+            y <- y[featureIx]
165
+        }
166
+    }
167
+
168
+    if (!is.null(cellIx)) {
169
+        counts <- counts[, cellIx, drop = FALSE]
170
+        if (length(annotationCell) > 1 ||
171
+                (length(annotationCell) == 1 & !is.na(annotationCell))) {
172
+            annotationCell <- annotationCell[cellIx, , drop = FALSE]
173
+        }
174
+        if (!is.null(z)) {
175
+            z <- z[cellIx]
176
+        }
177
+    }
178
+
176 179
     ## Set annotation colors
177 180
     if (!is.null(z)) {
178 181
         K <- sort(unique(z))
... ...
@@ -201,27 +204,25 @@ plotHeatmap <- function(counts,
201 204
         }
202 205
     }
203 206
 
204
-    ## Select subsets of features/cells
205
-    if (!is.null(featureIx)) {
206
-        counts <- counts[featureIx, , drop = FALSE]
207
-        if (length(annotationFeature) > 1 ||
208
-                (length(annotationFeature) == 1 & !is.na(annotationFeature))) {
209
-            annotationFeature <- annotationFeature[featureIx, , drop = FALSE]
210
-        }
211
-        if (!is.null(y)) {
212
-            y <- y[featureIx]
207
+    # scale indivisual rows by scaleRow
208
+    if (!is.null(scaleRow)) {
209
+        if (is.function(scaleRow)) {
210
+            cn <- colnames(counts)
211
+            counts <- t(base::apply(counts, 1, scaleRow))
212
+            colnames(counts) <- cn
213
+        } else {
214
+            stop("'scaleRow' needs to be of class 'function'")
213 215
         }
214 216
     }
215 217
 
216
-    if (!is.null(cellIx)) {
217
-        counts <- counts[, cellIx, drop = FALSE]
218
-        if (length(annotationCell) > 1 ||
219
-                (length(annotationCell) == 1 & !is.na(annotationCell))) {
220
-            annotationCell <- annotationCell[cellIx, , drop = FALSE]
221
-        }
222
-        if (!is.null(z)) {
223
-            z <- z[cellIx]
218
+    if (!is.null(trim)) {
219
+        if (length(trim) != 2) {
220
+            stop("'trim' should be a 2 element vector specifying the lower",
221
+                " and upper boundaries")
224 222
         }
223
+        trim <- sort(trim)
224
+        counts[counts < trim[1]] <- trim[1]
225
+        counts[counts > trim[2]] <- trim[2]
225 226
     }
226 227
 
227 228
     ## Set color scheme and breaks
... ...
@@ -244,7 +245,6 @@ plotHeatmap <- function(counts,
244 245
                 seq(colorSchemeCenter + 1e-6, uBoundRange,
245 246
                     length.out = colLen - round(colLen / 2)))
246 247
         }
247
-
248 248
     } else {
249 249
         # Sequential color scheme
250 250
         if (is.null(col)) {
... ...
@@ -25,7 +25,7 @@ features and columns represent cells. .}
25 25
 
26 26
 \item{y}{Numeric vector. Denotes feature module labels.}
27 27
 
28
-\item{scaleRow}{Function; A function to scale each individual row. Set to
28
+\item{scaleRow}{Function. A function to scale each individual row. Set to
29 29
 NULL to disable. Occurs after normalization and log transformation. Defualt
30 30
 is 'scale' and thus will Z-score transform each row.}
31 31