1+ # ' Extracts out a subset from a GatingSet, and returns as their respective .fcs files
2+ # ' to a target folder. Additional option to downsample to a desired number.
3+ # '
4+ # ' @param x Either a file path to a FlowJo workspace, or a GatingSet object.
5+ # ' @param path When using a FlowJo workspace for argument x, path to the folder containing
6+ # ' the respective .fcs files contained within that workspace
7+ # ' @param keywords Default "GROUPNAME", can handle up to 3, ex. c("$PROJ", "GROUPNAME", "TUBENAME")
8+ # ' @param subset The population node in the GatingSet that you want to extract as their own .fcs files
9+ # ' @param outpath The file.path to the storage location
10+ # ' @param addon Default NULL, appends to the end of the filename to distinguish from original fcs file.
11+ # ' @param downsample Default NULL, provide a number to downsample to that number, alternatively use
12+ # ' 0.1 for a proportion of total cells
13+ # '
14+ # ' @importFrom CytoML open_flowjo_xml flowjo_to_gatingset
15+ # ' @importFrom flowWorkspace gs_pop_get_data
16+ # ' @importFrom purrr walk
17+ # '
18+ # ' @return Returns .fcs files for the designated cell population to the designated folder.
19+ # '
20+ # ' @export
21+ # '
22+ # ' @examples A <- 2+2
23+ CellPopExtract <- function (x , path , keywords = " GROUPNAME" , subset , outpath , addon = NULL ,
24+ downsample = NULL ){
25+
26+ if (inherits(x , " GatingSet" )){gs <- x
27+ } else {
28+ ws <- CytoML :: open_flowjo_xml(x )
29+ gs <- CytoML :: flowjo_to_gatingset(ws , name = 1 , path = path , keywords = keywords )
30+ }
31+
32+ if (! is.null(downsample )){
33+ purrr :: walk(.x = gs , .f = Utility_Downsample , sample.name = keywords ,
34+ subsets = subset , subsample = downsample , internal = FALSE , export = TRUE ,
35+ inverse.transform = TRUE , outpath = outpath , addon = addon )
36+ } else {
37+ SubsetSolo <- flowWorkspace :: gs_pop_get_data(gs , subset , inverse.transform = TRUE )
38+ purrr :: walk(.x = SubsetSolo , .f = FCS_Subset_Copy , keywords = keywords ,
39+ outpath = outpath , addon = addon )
40+ }
41+ }
42+
43+
44+ # ' Internal for CellPopExtract
45+ # '
46+ # ' @param x Either a file path to a FlowJo workspace, or a GatingSet object.
47+ # ' @param keywords Default "GROUPNAME", can handle up to 3, ex. c("$PROJ", "GROUPNAME", "TUBENAME")
48+ # ' @param outpath The file.path to the storage location
49+ # ' @param addon Default NULL, appends to the end of the filename to distinguish from original fcs file.
50+ # '
51+ # ' @importFrom flowCore keyword write.FCS
52+ # ' @importFrom flowWorkspace cytoframe_to_flowFrame
53+ # '
54+ # ' @return Writes the .fcs file with altered naming to the designated outpath
55+ # '
56+ # ' @noRd
57+ FCS_Subset_Copy <- function (x , keywords , outpath , addon ){
58+
59+ FileName <- FlowKeywords(x = x , keywords = keywords , addon = addon )
60+
61+ FileNameOut <- paste0(FileName , " .fcs" )
62+ if (is.null(outpath )) {outpath <- getwd()}
63+ FinalRestingPlace <- file.path(outpath , FileNameOut )
64+
65+ new_fcs <- cytoframe_to_flowFrame(x )
66+ new_fcs @ description $ GUID <- FileName
67+ write.FCS(new_fcs , filename = FinalRestingPlace , delimiter = " #" )
68+ }
69+
70+ # ' An util function, handles the sample.name/keyword piping, handling generating
71+ # ' an appended name out of flowCore keywords
72+ # '
73+ # ' @param x The iterated GatingSet object
74+ # ' @param keywords The .fcs file keyword, or a c("A", "B", "C") vector of keywords to extract
75+ # ' @param addon A character string value to append after the x argument, but before the .fcs
76+ # '
77+ # ' @importFrom flowCore keyword
78+ # '
79+ # ' @return A character string resulting from the provided keywords for the respective specimen.
80+ # '
81+ # ' @noRd
82+ FlowKeywords <- function (x , keywords , addon ){
83+ if (length(keywords ) > 0 ){
84+ First <- keyword(x , keywords [1 ])
85+ if (length(keywords ) > 1 ) {
86+ Second <- keyword(x , keywords [2 ])
87+ } else {Nomenclature <- First }
88+ if (length(keywords ) > 2 ) {
89+ Third <- keyword(x , keywords [3 ])
90+ } else {Nomenclature <- paste(First , Second , sep = " _" )}
91+ if (length(keywords ) > 3 ) {
92+ stop(" Please choose only three keywords, thank you!" )
93+ } else {Nomenclature <- paste(First , Second , Third , sep = " _" )}
94+ }
95+
96+ if (! is.null(addon )){FileName <- paste(Nomenclature , addon , sep = " _" )
97+ } else {FileName <- Nomenclature }
98+
99+ return (FileName )
100+ }
0 commit comments