Skip to content

Commit fe8cfa2

Browse files
committed
Incorporated CellPopExtract function, which allows sending a particular node out to it's own .fcs file retaining original metadata, with option to also downsample if so desired. Couple changes to Utility_Downsample in the process
1 parent 121faa3 commit fe8cfa2

File tree

5 files changed

+162
-31
lines changed

5 files changed

+162
-31
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
export(AutofluoresceShop)
44
export(AveragedSignature)
5+
export(CellPopExtract)
56
export(CytosetScreen)
67
export(FCS_Jailbreak)
78
export(FluorophoreMatrix)
@@ -67,6 +68,8 @@ importFrom(Biobase,pData)
6768
importFrom(BiocGenerics,colnames)
6869
importFrom(BiocGenerics,nrow)
6970
importFrom(BiocGenerics,subset)
71+
importFrom(CytoML,flowjo_to_gatingset)
72+
importFrom(CytoML,open_flowjo_xml)
7073
importFrom(MASS,ginv)
7174
importFrom(Rtsne,Rtsne)
7275
importFrom(data.table,fread)

R/CellPopExtract.R

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

R/Utility_Downsample.R

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#'
33
#' @param x A gating set object
44
#' @param sample.name Keyword specifying sample name
5-
#' @param removestrings Value to be removed from sample name
65
#' @param subsets The gating hierarchy subset you want to include
76
#' @param subsample Total number of events to sub-sample from each specimen.
87
#' If value between 0 and 1, grabs that equivalent proportion of total cells.
@@ -14,19 +13,14 @@
1413
#' @param outpath When export is true, the file.path to where you want the .fcs file
1514
#' stored.
1615
#' @param metadataCols column names from pData to append as metadata for the .fcs, default NULL
16+
#' @param addon Additional value to add before .fcs to denote modification, default NULL
1717
#'
18-
#' @importFrom flowWorkspace keyword
19-
#' @importFrom flowWorkspace gs_pop_get_data
20-
#' @importFrom flowCore exprs
21-
#' @importFrom dplyr slice_sample
22-
#' @importFrom flowCore parameters
18+
#' @importFrom flowWorkspace keyword gs_pop_get_data
19+
#' @importFrom flowCore exprs parameters write.FCS
20+
#' @importFrom dplyr slice_sample select slice mutate
2321
#' @importFrom methods new
24-
#' @importFrom flowCore write.FCS
25-
#' @importFrom dplyr mutate
2622
#' @importFrom Biobase pData
27-
#' @importFrom dplyr select
2823
#' @importFrom tidyselect all_of
29-
#' @importFrom dplyr slice
3024
#'
3125
#' @return Either a data.frame, a flow.frame or an .fcs file depending on your
3226
#' selected options
@@ -72,23 +66,13 @@
7266
#' subsets = "live", subsample = 2500, internal = FALSE,
7367
#' export = FALSE, inverse.transform=TRUE)
7468
#'
75-
Utility_Downsample <- function(x, sample.name, removestrings,
76-
subsets, subsample=NULL, inverse.transform,
77-
internal = FALSE, export = FALSE, outpath = NULL,
78-
metadataCols=NULL){
79-
80-
if(length(sample.name) == 1){
81-
name <- keyword(x, sample.name)
82-
alternatename <- NameCleanUp(name, removestrings)
83-
} else {first <- sample.name[1]
84-
second <- sample.name[2]
85-
first <- keyword(x, first)
86-
first <- NameCleanUp(first, removestrings)
87-
second <- keyword(x, second)
88-
second <- NameCleanUp(second, removestrings)
89-
alternatename <- paste(first, second, sep="_")
90-
}
91-
69+
Utility_Downsample <- function(x, sample.name, subsets,
70+
subsample=NULL, inverse.transform, internal = FALSE,
71+
export = FALSE, outpath = NULL, metadataCols=NULL, addon=NULL){
72+
73+
keywords <- sample.name
74+
alternatename <- FlowKeywords(x=x, keywords=keywords, addon=addon)
75+
9276
#Retrieving the exprs data for my subset population of interest
9377
ff <- gs_pop_get_data(x, subsets, inverse.transform = inverse.transform)
9478
df <- exprs(ff[[1]])

man/CellPopExtract.Rd

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/Utility_Downsample.Rd

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)