## Warning: replacing previous import 'ncdfFlow::filter' by 'dplyr::filter' when
## loading 'flowWorkspace'

parse the workspace into gs

ws <- open_flowjo_xml("~/rglab/workspace/CytoML/wsTestSuite/allstats.wsp")
gs <- flowjo_to_gatingset(ws, name = 1, path = "~/rglab/workspace/CytoML/wsTestSuite/Cytotrol/NHLBI/Tcell/")
gs_get_pop_paths(gs, path = "auto")
## [1] "root"        "Lymphocytes" "CD3"         "cd4"         "HLA"        
## [6] "cd8"

list all the available stats for the entire gating tree

gh <- gs[[1]]
gh_pop_stats_ls(gh)
##  [1] "Count"            "SD"               "Robust CV"        "Mode"            
##  [5] "Median Abs Dev"   "40% Percentile"   "Median"           "Robust SD"       
##  [9] "Mean"             "Geometric Mean"   "Freqoftotal"      "Freqofgranparent"
## [13] "CV"               "Freqof"           "Freqofparent"

list all the available stats for the individual pops

gh_pop_stats_ls(gh, "CD3")
## [1] "Count"

to invoke the stats computing

gs_pop_stats_compute(gs)
gs_pop_stats_print(gs, "HLA", type = "Median")
##                            sample     channel pop    value
## 1: CytoTrol_CytoTrol_1.fcs_119531 Comp-B710-A HLA 17809.85

compare stats

gh_pop_stats_compare(gs, "HLA", type = "Median")
##      type     channel  cytolib      xml node
## 1: Median Comp-B710-A 17809.85 17778.66  HLA
gh_pop_stats_compare(gh)
##     type cytolib    xml        node
## 1: Count  119531 119531        root
## 2: Count   67633  67633 Lymphocytes
## 3: Count   51450  51450         CD3
## 4: Count   31640  31640         cd4
## 5: Count   30036  30036         HLA
## 6: Count   14934  14934         cd8

render it to legacy format for backward compatibility

gh_pop_stats_compare(gh, legacy = T)
##    openCyto.count xml.count        node
## 1:         119531    119531        root
## 2:          67633     67633 Lymphocytes
## 3:          51450     51450         CD3
## 4:          31640     31640         cd4
## 5:          30036     30036         HLA
## 6:          14934     14934         cd8

% is currently concatenated to the type (otherwise we need to append extra column for all the rest stats)

gh_pop_stats_compare(gh, type = "40% Percentile")
##              type     channel  cytolib      xml node
## 1: 40% Percentile Comp-B710-A 16643.83 16612.87  HLA

How to format columns for multiple stats that has different attr (channel vs ancestor)

gh_pop_stats_compare(gh, type = c("Median", "CV"))
## Warning in if (type == "Freqof") col.attr <- "ancestor" else col.attr <-
## "channel": the condition has length > 1 and only the first element will be used
##      type     channel     cytolib        xml node
## 1:     CV Comp-B710-A    26.27054    26.2696  HLA
## 2: Median Comp-B710-A 17809.85156 17778.6641  HLA

Add stats

prevent invalid stats to be added

stat <- list("new_stats")
gs_pop_stats_add(gs, "cd4", stat)
## Error in gs_pop_stats_add(gs, "cd4", stat): stat must be a valid 'cyto_stats' object generated by 'cyto_stats()' constuctor

cyto_stats()

validity checks of constructor

stats <- cyto_stats(type = "Freqof",  channel = c("Comp-V450-A"))
## Error in .cyto_stats(tp, ch, an, pc): channel argument is not applicable for Freqof
stats <- cyto_stats(type = "Median")
## Error in .cyto_stats(tp, ch, an, pc): channels argument is required for Median

construct multi-stats within one call

stats <- cyto_stats(type = c("CV", "Median"),  channel = c("Comp-V450-A", "Comp-G560-A"))
stats
## [[1]]
## cyto_stats 
## type:  CV 
## attr:  Comp-V450-A 
## 
## [[2]]
## cyto_stats 
## type:  CV 
## attr:  Comp-G560-A 
## 
## [[3]]
## cyto_stats 
## type:  Median 
## attr:  Comp-V450-A 
## 
## [[4]]
## cyto_stats 
## type:  Median 
## attr:  Comp-G560-A

add stats

gh_pop_stats_ls(gh, "cd4")
## [1] "Count"
gs_pop_stats_add(gs, "cd4", stats)
gh_pop_stats_ls(gh, "cd4")
## [1] "Median" "CV"     "Count"
gs_pop_stats_compute(gs, "cd4")
gh_pop_stats_compare(gh, "cd4", type = c("CV", "Median"))
## Warning in if (type == "Freqof") col.attr <- "ancestor" else col.attr <-
## "channel": the condition has length > 1 and only the first element will be used
##      type     channel    cytolib xml node
## 1:     CV Comp-V450-A   38.62045 NaN  cd4
## 2: Median Comp-V450-A 6426.73584 NaN  cd4
## 3:     CV Comp-G560-A   91.61989 NaN  cd4
## 4: Median Comp-G560-A 1870.52136 NaN  cd4

remove stats

# gs_pop_stats_remove(gs, "cd4", type = "CV")
#gh_pop_stats_ls(gh, "cd4")