Browse code

Update background check function to also update bgBatch when background needs to be updated

Yuan authored on 13/01/2022 19:20:44
Showing 1 changed files

... ...
@@ -168,10 +168,14 @@ setMethod("decontX", "SingleCellExperiment", function(x,
168 168
   countsBackground <- NULL
169 169
   if (!is.null(background)) {
170 170
     # Remove cells with the same ID between x and the background matrix
171
-    background <- .checkBackground(x = x, background = background,
172
-                                   logfile = logfile, verbose = verbose)
171
+    temp <- .checkBackground(x = x,
172
+                             background = background,
173
+                             bgBatch = bgBatch,
174
+                             logfile = logfile,
175
+                             verbose = verbose)
173 176
     
174
-    # TODO: Does bgBatch needs to be checked?
177
+    background <- temp$background
178
+    bgBatch <- temp$bgBatch
175 179
     
176 180
     if (is.null(bgAssayName)) {
177 181
       bgAssayName <- assayName
... ...
@@ -254,10 +258,14 @@ setMethod("decontX", "ANY", function(x,
254 258
   countsBackground <- NULL
255 259
   if (!is.null(background)) {
256 260
     # Remove cells with the same ID between x and the background matrix
257
-    background <- .checkBackground(x = x, background = background,
258
-                                   logfile = logfile, verbose = verbose)
261
+    temp <- .checkBackground(x = x,
262
+                             background = background,
263
+                             bgBatch = bgBatch,
264
+                             logfile = logfile,
265
+                             verbose = verbose)
259 266
     
260
-    # TODO: Does bgBatch needs to be checked?
267
+    background <- temp$background
268
+    bgBatch <- temp$bgBatch
261 269
     
262 270
   }
263 271
 
... ...
@@ -1388,8 +1396,10 @@ simulateContamination <- function(C = 300,
1388 1396
 }
1389 1397
 
1390 1398
 
1391
-.checkBackground <- function(x, background, logfile = NULL, verbose = FALSE) {
1399
+.checkBackground <- function(x, background, bgBatch,
1400
+                             logfile = NULL, verbose = FALSE) {
1392 1401
   # Remove background barcodes that have already appeared in x
1402
+  # If bgBatch param is supplied, also remove duplicate bgBatch
1393 1403
   if(!is.null(colnames(background))) {
1394 1404
     dupBarcode <- colnames(background) %in% colnames(x)
1395 1405
   } else {
... ...
@@ -1413,6 +1423,20 @@ simulateContamination <- function(C = 300,
1413 1423
       verbose = verbose
1414 1424
     )
1415 1425
     background <- background[, !(dupBarcode), drop = FALSE]
1426
+    
1427
+    if(!is.null(bgBatch)){
1428
+      if (length(bgBatch) != length(dupBarcode)) {
1429
+        stop(
1430
+          "Length of bgBatch must be equal to the number of columns",
1431
+          "of background matrix."
1432
+          )
1433
+      }
1434
+      bgBatch <- bgBatch[!(dupBarcode)]
1435
+    }
1416 1436
   }
1417
-  return(background)
1437
+  
1438
+  re = list(background = background,
1439
+            bgBatch = bgBatch)
1440
+  
1441
+  return(re)
1418 1442
 }