suppressPackageStartupMessages(library(flowWorkspace))
suppressPackageStartupMessages(library(ggcyto))
dataDir <- system.file("extdata",package="flowWorkspaceData")
gs_dir <- list.files(dataDir, pattern = "gs_manual",full = TRUE)
suppressWarnings(suppressMessages(gs <- load_gs(gs_dir)))

the tree structures and cells count of original gs

nrow(gs)
## $CytoTrol_CytoTrol_1.fcs
## [1] 119531
plot(gs)

get subtree from CD3 descendants

pop <- "CD3+"
gs1 <- gs_pop_get_gs(gs, pop)
## done!
plot(gs1)

only has cells from cd3

nrow(gs1)
## $CytoTrol_CytoTrol_1.fcs
## [1] 54483
gh_pop_get_count(gs[[1]], pop)
## [1] 54483

stats unchanged

nodes <- gh_pop_get_descendants(gs[[1]], pop)
parent <- gs_pop_get_parent(gs, pop)
nodes.new <- gsub(paste0("^\\Q", file.path(parent, pop), "\\E"), "", nodes)
gs_pop_get_stats(gs, nodes)[1:10, 2:3]
##                                           pop count
##  1:             /not debris/singlets/CD3+/CD4 34032
##  2:     /not debris/singlets/CD3+/CD4/38- DR+  1123
##  3:     /not debris/singlets/CD3+/CD4/38+ DR+  1044
##  4:     /not debris/singlets/CD3+/CD4/38+ DR- 14682
##  5:     /not debris/singlets/CD3+/CD4/38- DR- 17183
##  6: /not debris/singlets/CD3+/CD4/CCR7- 45RA+   740
##  7: /not debris/singlets/CD3+/CD4/CCR7+ 45RA+ 12116
##  8: /not debris/singlets/CD3+/CD4/CCR7+ 45RA- 14743
##  9: /not debris/singlets/CD3+/CD4/CCR7- 45RA-  6433
## 10:             /not debris/singlets/CD3+/CD8 14564
gs_pop_get_stats(gs1, nodes.new)[1:10, 2:3]
##                  pop count
##  1:             /CD4 34032
##  2:     /CD4/38- DR+  1123
##  3:     /CD4/38+ DR+  1044
##  4:     /CD4/38+ DR- 14682
##  5:     /CD4/38- DR- 17183
##  6: /CD4/CCR7- 45RA+   740
##  7: /CD4/CCR7+ 45RA+ 12116
##  8: /CD4/CCR7+ 45RA- 14743
##  9: /CD4/CCR7- 45RA-  6433
## 10:             /CD8 14564
autoplot(gs1[[1]])
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

cyto data is a view of original gs

cs <- gs_cyto_data(gs1)
cs
## A cytoset with 1 samples.
## 
##   column names:
##     FSC-A, FSC-H, FSC-W, SSC-A, <B710-A>, <R660-A>, <R780-A>, <V450-A>, <V545-A>, <G560-A>, <G780-A>, Time
## 
## cytoset has been subsetted and can be realized through 'realize_view()'.

thus still points to the same file

cs_get_uri(cs)
## [1] "/media/wjiang2/real_home/wjiang2/mylib/R-devel-build/library/flowWorkspaceData/extdata/gs_manual"

but it is a different view object from the original cs

cs@pointer
## <pointer: 0x5575416c37e0>
gs_cyto_data(gs)@pointer
## <pointer: 0x5575371dfa30>

realize it by saving new gs

tmp <- tempfile()
save_gs(gs1, tmp)
## Done
## To reload it, use 'load_gs' function
gs1 <- load_gs(tmp)