Browse code

Fixed bug in checking background matrix

Joshua D. Campbell authored on 17/09/2021 01:57:09
Showing 1 changed files

... ...
@@ -162,22 +162,9 @@ setMethod("decontX", "SingleCellExperiment", function(x,
162 162
                                                       verbose = TRUE) {
163 163
   countsBackground <- NULL
164 164
   if (!is.null(background)) {
165
-    # Remove background barcodes that have already appeared in x
166
-    dupBarcode <- background$Barcode %in% x$Barcode
167
-
168
-    if (any(dupBarcode)) {
169
-      .logMessages(
170
-        sum(dupBarcode),
171
-        " columns in background removed as they are found in filtered matrix",
172
-        logfile = logfile,
173
-        append = TRUE,
174
-        verbose = verbose
175
-      )
176
-    }
177
-
178
-    background <- background[, !(dupBarcode)]
179
-
180
-
165
+    # Remove cells with the same ID between x and the background matrix
166
+    background <- .checkBackground(x = x, background = background,
167
+                                   logfile = logfile, verbose = verbose)
181 168
     if (is.null(bgAssayName)) {
182 169
       bgAssayName <- assayName
183 170
     }
... ...
@@ -256,20 +243,9 @@ setMethod("decontX", "ANY", function(x,
256 243
 
257 244
   countsBackground <- NULL
258 245
   if (!is.null(background)) {
259
-    # Remove background barcodes that have already appeared in x
260
-    dupBarcode <- colnames(background) %in% colnames(x)
261
-
262
-    if (any(dupBarcode)) {
263
-      .logMessages(
264
-        sum(dupBarcode),
265
-        " columns in background removed as they are found in filtered matrix",
266
-        logfile = logfile,
267
-        append = TRUE,
268
-        verbose = verbose
269
-      )
270
-    }
271
-
272
-    countsBackground <- background[, !(dupBarcode)]
246
+    # Remove cells with the same ID between x and the background matrix
247
+    background <- .checkBackground(x = x, background = background,
248
+                                   logfile = logfile, verbose = verbose)
273 249
   }
274 250
 
275 251
   .decontX(
... ...
@@ -1368,3 +1344,31 @@ simulateContamination <- function(C = 300,
1368 1344
     )
1369 1345
   )
1370 1346
 }
1347
+
1348
+
1349
+.checkBackground <- function(x, background, logfile = NULL, verbose = FALSE) {
1350
+  # Remove background barcodes that have already appeared in x
1351
+  if(!is.null(colnames(background))) {
1352
+    dupBarcode <- colnames(background) %in% colnames(x)
1353
+  } else {
1354
+    dupBarcode <- FALSE
1355
+    warning("No column names were found for the 'background' matrix. ",
1356
+            "No checking was performed between the ids in the 'backgroud' ",
1357
+            "matrix and 'x'.",
1358
+            " Please ensure that no true cells are included in the background ",
1359
+            "matrix. Otherwise, results will be incorrect.")
1360
+  }
1361
+  
1362
+  if (any(dupBarcode)) {
1363
+    .logMessages(
1364
+      sum(dupBarcode),
1365
+      " columns in the background matrix were removed as they were found in",
1366
+      " the filtered matrix.",
1367
+      logfile = logfile,
1368
+      append = TRUE,
1369
+      verbose = verbose
1370
+    )
1371
+    background <- background[, !(dupBarcode)]
1372
+  }
1373
+  return(background)
1374
+}