## Loading required package: flowWorkspaceData
library(flowCore)
library(ncdfFlow)
library(flowWorkspace)
library(openCyto)
library(ggcyto)
flowDataPath <- system.file("extdata", package = "flowWorkspaceData")
gtFile <- system.file("extdata/gating_template/tcell.csv", package = "openCyto")
gt_tcell <- gatingTemplate(gtFile)
gt_tcell
## --- Gating Template: default
## with 29 populations defined
plot(gt_tcell)

fcsFiles <- list.files(pattern = "CytoTrol", flowDataPath, full = TRUE)
fs <- read.ncdfFlowSet(fcsFiles)
gs <- GatingSet(fs)
## attach meta data
pData(gs)[["PTID"]] <- c("p1", "p2")
pData(gs)[["VISIT"]] <- c("v1", "v2")
pData(gs)
## name PTID VISIT
## CytoTrol_CytoTrol_1.fcs CytoTrol_CytoTrol_1.fcs p1 v1
## CytoTrol_CytoTrol_2.fcs CytoTrol_CytoTrol_2.fcs p2 v2
compMat <- spillover(fs[[1]])[[1]]
gs <- compensate(gs, compMat)
chnls <- colnames(compMat)
trans <- estimateLogicle(gs[[1]], channels = chnls)
gs <- transform(gs, trans)
gating(gt_tcell, gs, mc.cores=2, parallel_type = "multicore")
plot(gs[[1]])

##hide the intermediate pops/gates
toggle.helperGates(gt_tcell, gs)
plot(gs[[1]])

autoplot(gs[[1]])

## extract stats
library(tibble)
res <- gs_pop_get_count_with_meta(gs, path = "auto")
res <- as.tibble(res)
rbind(head(res), tail(res))
## # A tibble: 12 x 8
## name Population Parent Count ParentCount sampleName PTID VISIT
## <chr> <chr> <chr> <int> <int> <chr> <chr> <chr>
## 1 CytoTrol… nonDebris root 92362 119531 CytoTrol_Cy… p1 v1
## 2 CytoTrol… singlets nonDe… 86351 92362 CytoTrol_Cy… p1 v1
## 3 CytoTrol… lymph singl… 65211 86351 CytoTrol_Cy… p1 v1
## 4 CytoTrol… cd3 lymph 50156 65211 CytoTrol_Cy… p1 v1
## 5 CytoTrol… cd4-cd8- cd3 1895 50156 CytoTrol_Cy… p1 v1
## 6 CytoTrol… cd4+cd8- cd3 32701 50156 CytoTrol_Cy… p1 v1
## 7 CytoTrol… cd4-cd8+/CC… cd4-c… 2556 15183 CytoTrol_Cy… p2 v2
## 8 CytoTrol… cd4-cd8+/CC… cd4-c… 1303 15183 CytoTrol_Cy… p2 v2
## 9 CytoTrol… cd4-cd8+/CC… cd4-c… 4531 15183 CytoTrol_Cy… p2 v2
## 10 CytoTrol… cd4-cd8+/CC… cd4-c… 6793 15183 CytoTrol_Cy… p2 v2
## 11 CytoTrol… activated c… cd4-c… 116 15183 CytoTrol_Cy… p2 v2
## 12 CytoTrol… cd4+cd8+ cd3 575 49938 CytoTrol_Cy… p2 v2
## output to csv
write.csv(res, tempfile())
## more generic API
as.tibble(gs_pop_get_stats(gs, type = "count"))
## # A tibble: 38 x 3
## sample pop count
## <chr> <chr> <dbl>
## 1 CytoTrol_CytoTrol_1.… root 119531
## 2 CytoTrol_CytoTrol_1.… /nonDebris 92362
## 3 CytoTrol_CytoTrol_1.… /nonDebris/singlets 86351
## 4 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph 65211
## 5 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph/cd3 50156
## 6 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph/cd3/cd4-cd8- 1895
## 7 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph/cd3/cd4+cd8- 32701
## 8 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph/cd3/cd4+cd8-/CCR… 6717
## 9 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph/cd3/cd4+cd8-/CCR… 13543
## 10 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph/cd3/cd4+cd8-/CCR… 983
## # … with 28 more rows
as.tibble(gs_pop_get_stats(gs, type = "percent"))
## # A tibble: 38 x 3
## sample pop percent
## <chr> <chr> <dbl>
## 1 CytoTrol_CytoTrol_1.… root 1
## 2 CytoTrol_CytoTrol_1.… /nonDebris 0.773
## 3 CytoTrol_CytoTrol_1.… /nonDebris/singlets 0.935
## 4 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph 0.755
## 5 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph/cd3 0.769
## 6 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph/cd3/cd4-cd8- 0.0378
## 7 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph/cd3/cd4+cd8- 0.652
## 8 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph/cd3/cd4+cd8-/CC… 0.205
## 9 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph/cd3/cd4+cd8-/CC… 0.414
## 10 CytoTrol_CytoTrol_1.… /nonDebris/singlets/lymph/cd3/cd4+cd8-/CC… 0.0301
## # … with 28 more rows
as.tibble(gs_pop_get_stats(gs, type = pop.MFI))
## # A tibble: 38 x 9
## sample pop `CD4 PcpCy55` `CD38 APC` `CD8 APCH7` `CD3 V450`
## <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 CytoT… root 1.70 1.93 1.39 2.57
## 2 CytoT… /non… 2.57 2.28 1.55 2.76
## 3 CytoT… /non… 2.54 2.20 1.49 2.76
## 4 CytoT… /non… 2.97 1.98 1.35 2.79
## 5 CytoT… /non… 3.23 1.79 1.33 2.86
## 6 CytoT… /non… 1.14 1.54 1.27 2.94
## 7 CytoT… /non… 3.33 1.84 1.03 2.89
## 8 CytoT… /non… 3.33 1.41 1.10 2.81
## 9 CytoT… /non… 3.35 1.50 1.06 2.85
## 10 CytoT… /non… 3.23 1.98 1.09 2.85
## # … with 28 more rows, and 3 more variables: `HLA-DR V500` <dbl>, `CCR7
## # PE` <dbl>, `CD45RA PECy7` <dbl>