tests/testthat/test-tailgate.R

openCyto:::register_plugins(fun=.gate_tail,methodName="gate_tail")
test_that("gate_tail", {
  
  # Right tail
  gs1 <- load_gs(system.file("extdata", "gs_manual", package = "flowWorkspaceData"))
  gs2 <- gs_clone(gs1)
  
  # flowWorkspace::gs_pop_add approach
  right_tail_gate <- gate_tail(gh_pop_get_data(gs1[[1]], "CD4"), channel = "<G560-A>", side = "right")
  right_tail_gate@filterId <- "CCR7_right_tail"
  right_tail_gate
  gs_pop_add(gs1, right_tail_gate, parent = "CD4", name = "CCR7_right_tail_pos")
  gs_pop_add(gs1, right_tail_gate, parent = "CD4", name = "CCR7_right_tail_neg", negated = TRUE)
  recompute(gs1)
  
  # openCyto::gs_add_gating_method approach
  openCyto::gs_add_gating_method(gs2, alias = "CCR7_right_tail_pos", pop = "+", parent = "CD4",
                                 dims = "<G560-A>", gating_method = "gate_tail", 
                                 gating_args = "side='right'")
  openCyto::gs_add_gating_method(gs2, alias = "CCR7_right_tail_neg", pop = "-", parent = "CD4",
                                 dims = "<G560-A>", gating_method = "gate_tail", 
                                 gating_args = "side='right'")
  
  g1_pos <- gh_pop_get_gate(gs1[[1]], "CCR7_right_tail_pos")
  g2_pos <- gh_pop_get_gate(gs2[[1]], "CCR7_right_tail_pos")
  g1_neg <- gh_pop_get_gate(gs1[[1]], "CCR7_right_tail_pos")
  g2_neg <- gh_pop_get_gate(gs2[[1]], "CCR7_right_tail_pos")
  
  # For each, check that the cut point is on the right side of the peak
  test_bound <- c("<G560-A>" = 2661)
  expect_equal(g1_pos@min, test_bound, tolerance = 1)
  expect_equal(g2_pos@min, test_bound, tolerance = 1)
  expect_equal(g1_neg@min, test_bound, tolerance = 1)
  expect_equal(g2_neg@min, test_bound, tolerance = 1)
  
  # And check that their infinite bounds are in the appropriate direction
  # Negation is done at the logical level, so the bounds should be untouched
  test_bound <- c("<G560-A>" = Inf)
  expect_equal(g1_pos@max, test_bound)
  expect_equal(g2_pos@max, test_bound)
  expect_equal(g1_pos@max, test_bound)
  expect_equal(g2_pos@max, test_bound)
  
  # Check that the resulting stats of both approaches agree
  expect_equal(gh_pop_get_stats(gs1[[1]], "CCR7_right_tail_pos"), gh_pop_get_stats(gs2[[1]], "CCR7_right_tail_pos"))
  expect_equal(gh_pop_get_stats(gs1[[1]], "CCR7_right_tail_neg"), gh_pop_get_stats(gs2[[1]], "CCR7_right_tail_neg"))
  
  
  # Left tail
  
  gs1 <- load_gs(system.file("extdata", "gs_manual", package = "flowWorkspaceData"))
  gs2 <- gs_clone(gs1)
  
  # flowWorkspace::gs_pop_add approach
  left_tail_gate <- gate_tail(gh_pop_get_data(gs1[[1]], "CD4"), channel = "<G560-A>", side = "left")
  left_tail_gate@filterId <- "CCR7_left_tail"
  left_tail_gate
  gs_pop_add(gs1, left_tail_gate, parent = "CD4", name = "CCR7_left_tail_pos")
  gs_pop_add(gs1, left_tail_gate, parent = "CD4", name = "CCR7_left_tail_neg", negated = TRUE)
  recompute(gs1)
  
  # openCyto::gs_add_gating_method approach
  openCyto::gs_add_gating_method(gs2, alias = "CCR7_left_tail_pos", pop = "+", parent = "CD4",
                                 dims = "<G560-A>", gating_method = "gate_tail", 
                                 gating_args = "side='left'")
  openCyto::gs_add_gating_method(gs2, alias = "CCR7_left_tail_neg", pop = "-", parent = "CD4",
                                 dims = "<G560-A>", gating_method = "gate_tail", 
                                 gating_args = "side='left'")
  
  g1_pos <- gh_pop_get_gate(gs1[[1]], "CCR7_left_tail_pos")
  g2_pos <- gh_pop_get_gate(gs2[[1]], "CCR7_left_tail_pos")
  g1_neg <- gh_pop_get_gate(gs1[[1]], "CCR7_left_tail_pos")
  g2_neg <- gh_pop_get_gate(gs2[[1]], "CCR7_left_tail_pos")
  
  # For each, check that the cut point is on the left side of the peak
  test_bound <- c("<G560-A>" = 1991)
  expect_equal(g1_pos@min, test_bound, tolerance = 1)
  expect_equal(g2_pos@min, test_bound, tolerance = 1)
  expect_equal(g1_neg@min, test_bound, tolerance = 1)
  expect_equal(g2_neg@min, test_bound, tolerance = 1)
  
  # And check that their infinite bounds are in the appropriate direction
  # Negation is done at the logical level, so the bounds should be untouched
  test_bound <- c("<G560-A>" = Inf)
  expect_equal(g1_pos@max, test_bound)
  expect_equal(g2_pos@max, test_bound)
  expect_equal(g1_pos@max, test_bound)
  expect_equal(g2_pos@max, test_bound)
  
  # Check that the resulting stats of both approaches agree
  expect_equal(gh_pop_get_stats(gs1[[1]], "CCR7_left_tail_pos"), gh_pop_get_stats(gs2[[1]], "CCR7_left_tail_pos"))
  expect_equal(gh_pop_get_stats(gs1[[1]], "CCR7_left_tail_neg"), gh_pop_get_stats(gs2[[1]], "CCR7_left_tail_neg"))
})
RGLab/cytoUtils documentation built on Jan. 31, 2024, 11:26 p.m.