Skip to content

Commit

Permalink
Merge pull request #42 from malisas/fix-spillover-bugs
Browse files Browse the repository at this point in the history
Fix spillover bugs
  • Loading branch information
Mike Jiang authored Dec 16, 2021
2 parents 6a75b8f + d54ac0a commit 1f02de9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
11 changes: 6 additions & 5 deletions R/spillover.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ setMethod("spillover",
if (is.na(match(ssc, allcols))) {
stop("Could not find side scatter parameter. ",
"Please set the ssc parameter", call. = FALSE)
n2f <- norm2Filter(fsc, ssc, scale.factor = 1.5)
x <- Subset(x, n2f)
}
n2f <- norm2Filter(fsc, ssc, scale.factor = 1.5)
x <- Subset(x, n2f)
}

# Here, we match the stain channels with the compensation controls
Expand Down Expand Up @@ -211,9 +211,10 @@ setMethod("spillover",
channel_order <- channel_order - (channel_order > unstained)
}

# Then reverse any shuffling so the rows are in the same
# order as the channels for symmetry
inten <- inten[order(channel_order),]
# Place the rows (which originally corresponded to samples) in
# channel_order, which is the order that rows should be placed
# in in order to have the same order as the channels
inten <- inten[channel_order,]

rownames(inten) <- colnames(inten)
inten
Expand Down
50 changes: 42 additions & 8 deletions tests/testthat/test-spillover.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ controls <- controls[,c(3,1,4,5,2,6,7)]
test_that("spillover: Match columns using regexpr", {
ref_file <- system.file("extdata", "compdata", "compref1", package="flowCore")
comp_ref <- as.matrix(read.table(ref_file, check.names = FALSE))
comp <- spillover(controls, unstained = "UNSTAINED", fsc="FSC-H", ssc="SSC-H", patt = "-H", stain_match="regexpr")
comp <- spillover(controls, unstained = "UNSTAINED", fsc="FSC-H",
ssc="SSC-H", patt = "-H", stain_match="regexpr",
useNormFilt = FALSE)
expect_equal(colnames(comp), colnames(comp_ref))
expect_equal(rownames(comp), rownames(comp_ref))
expect_equivalent(comp, comp_ref, tolerance=3e-08)
Expand All @@ -28,7 +30,9 @@ test_that("spillover: Match columns using ordered", {
comp_ref <- as.matrix(read.table(ref_file, check.names = FALSE))
# The spillover matrix here should (appropriately) be incorrect bc the rows are
# not in the order of the columns.
comp <- spillover(controls, unstained = "UNSTAINED", fsc="FSC-H", ssc="SSC-H", patt = "-H", stain_match="ordered")
comp <- spillover(controls, unstained = "UNSTAINED", fsc="FSC-H",
ssc="SSC-H", patt = "-H", stain_match="ordered",
useNormFilt = FALSE)
expect_equal(colnames(comp), colnames(comp_ref))
expect_equal(rownames(comp), rownames(comp_ref))
expect_equivalent(comp, comp_ref, tolerance=3e-08)
Expand All @@ -37,19 +41,20 @@ test_that("spillover: Match columns using ordered", {
test_that("spillover: Match columns using intensity", {
ref_file <- system.file("extdata", "compdata", "compref1", package="flowCore")
comp_ref <- as.matrix(read.table(ref_file, check.names = FALSE))
comp <- spillover(controls, unstained = "UNSTAINED", fsc="FSC-H", ssc="SSC-H", patt = "-H", stain_match="intensity")
comp <- spillover(controls, unstained = "UNSTAINED", fsc="FSC-H",
ssc="SSC-H", patt = "-H", stain_match="intensity",
useNormFilt = FALSE)
expect_equal(colnames(comp), colnames(comp_ref))
expect_equal(rownames(comp), rownames(comp_ref))
expect_equivalent(comp, comp_ref, tolerance=3e-08)
})


test_that("spillover_match: Using path to dir with files", {
ref_file <- system.file("extdata", "compdata", "compref3", package="flowCore")
comp_ref <- as.matrix(read.table(ref_file, check.names = FALSE))
matched <- spillover_match(path=control_path, fsc="FSC-H", ssc="SSC-H", matchfile = matchfile_path)
comp <- spillover(matched, prematched = TRUE, fsc="FSC-H", ssc="SSC-H", patt = "-H",
useNormFilt = TRUE, method = "mode")
useNormFilt = FALSE, method = "mode")
expect_equal(colnames(comp), colnames(comp_ref))
expect_equal(rownames(comp), rownames(comp_ref))
expect_equivalent(comp, comp_ref, tolerance=3e-08)
Expand All @@ -61,7 +66,7 @@ test_that("spillover_match: Using preconstructed flowSet with filenames as sampl
comp_ref <- as.matrix(read.table(ref_file, check.names = FALSE))
matched <- spillover_match(controls, fsc="FSC-H", ssc="SSC-H", matchfile = matchfile_path)
comp <- spillover(matched, prematched = TRUE, fsc="FSC-H", ssc="SSC-H", patt = "-H",
useNormFilt = TRUE, method = "mode")
useNormFilt = FALSE, method = "mode")
expect_equal(colnames(comp), colnames(comp_ref))
expect_equal(rownames(comp), rownames(comp_ref))
expect_equivalent(comp, comp_ref, tolerance=3e-08)
Expand All @@ -71,7 +76,8 @@ test_that("spillover_ng: Using path to dir with files", {
ref_file <- system.file("extdata", "compdata", "compref4", package="flowCore")
comp_ref <- as.matrix(read.table(ref_file, check.names = FALSE))
comp <- spillover_ng(path=control_path, fsc="FSC-H", ssc="SSC-H",
patt="-H", matchfile = matchfile_path, pregate = FALSE)
patt="-H", matchfile = matchfile_path, pregate = FALSE,
useNormFilt = FALSE)
expect_equal(colnames(comp), colnames(comp_ref))
expect_equal(rownames(comp), rownames(comp_ref))
expect_equivalent(comp, comp_ref, tolerance=3e-08)
Expand All @@ -83,11 +89,39 @@ test_that("spillover_ng: Using preconstructed flowSet with filenames as sample n
comp_ref <- as.matrix(read.table(ref_file, check.names = FALSE))
matched <- spillover_match(controls, fsc="FSC-H", ssc="SSC-H", matchfile = matchfile_path)
comp <- spillover_ng(path=control_path, fsc="FSC-H", ssc="SSC-H",
patt="-H", matchfile = matchfile_path, pregate = FALSE)
patt="-H", matchfile = matchfile_path, pregate = FALSE,
useNormFilt = FALSE)
expect_equal(colnames(comp), colnames(comp_ref))
expect_equal(rownames(comp), rownames(comp_ref))
expect_equivalent(comp, comp_ref, tolerance=3e-08)
})

test_that("spillover_ng: Using preconstructed flowSet with channels in new order", {
match <- read.csv(matchfile_path, colClasses = "character")
fs <- read.flowSet(files=match$filename, path=control_path)

# Scramble the channel order
fs <- fs[,c("FSC-H", "FL4-H", "FL3-H", "FL1-A", "FL1-H", "FL2-H", "SSC-H")]
# Note that the order of the channels used for compensation (FL4-H, FL3-H, FL1-H, FL2-H)
# is c(3, 4, 2, 1), which has the property that
# order(c(3, 4, 2, 1)) != c(3, 4, 2, 1).
# This is in contrast to the channel order at the top of the file,
# where order(c(2, 1, 3, 4)) == c(2, 1, 3, 4).
# The channel order used here is important for testing the correctness of the
# approach used to re-order rows of the matrix in the spillover function.

comp <- spillover_ng(x=fs, fsc="FSC-H", ssc="SSC-H",
patt="-H", matchfile = matchfile_path, pregate = FALSE,
useNormFilt = FALSE)
# The diagonals should be equal to 1
for (i in 1:4) {
expect_equal(comp[i,i], 1)
}
comp_order <- c(3, 4, 2, 1)
# If you unscramble the rows and columns, you should get the original comp_ref
comp_ordered <- comp[comp_order, comp_order]
expect_equal(colnames(comp_ordered), colnames(comp_ref))
expect_equal(rownames(comp_ordered), rownames(comp_ref))
expect_equivalent(comp_ordered, comp_ref, tolerance=3e-08)
})

0 comments on commit 1f02de9

Please sign in to comment.