Skip to content

Commit 9db1bb9

Browse files
committed
Missed a transform somewhere in Coereba_Concatenate
1 parent 89faf27 commit 9db1bb9

File tree

6 files changed

+310
-3
lines changed

6 files changed

+310
-3
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(Coereba_App)
4+
export(Coereba_Concatenate)
45
export(Coereba_FCSExport)
56
export(Coereba_FCS_Reversal)
67
export(Coereba_GateCutoffs)

R/Coereba_Concatenate.R

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#' Processes multiple Coereba flowframes, retrieves contents according to our Concatenate criteria, then cumulatively processes
2+
#'
3+
#' @param Set The flow/cyto set we are working with
4+
#' @param metadata_cols The columns from pData to retain.
5+
#' @param outpath Where to store the .fcs file
6+
#' @param filename What to call the .fcs file
7+
#'
8+
#' @export
9+
#'
10+
#' @examples A <- 2+2
11+
Coereba_Concatenate <- function(Set, metadata_cols, outpath=NULL, filename="CombinedCoerebaFile"){
12+
13+
AllData <- map(.x=Set, .f=Coereba_SingleFrame_Reversal, metadata_cols=metadata_cols) |> bind_rows()
14+
15+
if (is.null(outpath)){outpath <- getwd()}
16+
17+
FCSFile <- Coereba_FCSExport(data=AllData, gs=Set[1],
18+
returnType="fcs", outpath=outpath, filename=filename,
19+
nameAppend="", Aggregate=FALSE, coerebaCombine =TRUE)
20+
}
21+
22+
Coereba_SingleFrame_Reversal <- function(x, metadata_cols){
23+
24+
InternalPD <- pData(CoerebaGS)
25+
TheMetadata <- InternalPD |> select(all_of(metadata_cols))
26+
row.names(TheMetadata) <- NULL
27+
CoerebaCS <- gs_pop_get_data(x)
28+
CoerebaCF <- CoerebaCS[[1]]
29+
30+
if (class(CoerebaCF) %in% "cytoframe"){
31+
Data <- exprs(CoerebaCF)
32+
Data <- data.frame(Data, check.names=FALSE)
33+
Original <- Data |> select(!starts_with("Coereba"))
34+
Data <- Data |> select(starts_with("Coereba"))
35+
Data <- Data |> mutate(across(everything(), as.character))
36+
These <- colnames(Data)
37+
Reverted <- map(.f=MetadataRetrieval, .x=These, data=Data, Coereba=CoerebaCF) |> bind_cols()
38+
Assembled <- cbind(Original, Reverted)
39+
40+
Completed <- Assembled |> cross_join(TheMetadata)
41+
return(Completed)
42+
43+
} else {stop("Not a Cytoframe")}
44+
}
45+
46+

R/Coereba_FCS.R

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
#' @param returnType Whether to return "flowframe" or "fcs"
88
#' @param nameAppend For flowframe and fcs returnType, what gets appended before .fcs
99
#' @param Aggregate Combine the items, finish your documentation.
10+
#' @param combineCoereba Internal, used for Coereba flowframe aggregation
1011
#'
1112
#' @importFrom flowWorkspace gs_pop_get_data keyword
1213
#' @importFrom flowCore parameters exprs write.FCS parameters<-
13-
#' @importFrom dplyr select bind_cols
14+
#' @importFrom dplyr select bind_cols filter
15+
#' @importFrom stringr str_detect
1416
#' @importFrom tidyselect all_of
1517
#' @importFrom purrr map flatten
1618
#' @importFrom Biobase pData
@@ -22,13 +24,21 @@
2224
#'
2325
#' @examples A <- 2+2
2426
Coereba_FCSExport <- function(data, gs, outpath, filename, fcsname,
25-
returnType, nameAppend, Aggregate=FALSE){
27+
returnType, nameAppend, Aggregate=FALSE,
28+
coerebaCombine=FALSE){
2629
# Retrieving param information
2730
cs <- gs_pop_get_data(gs, "root")
2831
cf <- cs[[1]]
2932
original_param <- parameters(cf)
3033
original_descr <- keyword(cf)
3134
original_exprs <- exprs(cf)
35+
36+
if (coerebaCombine==TRUE){
37+
paramdata <- original_param@data
38+
paramdata <- paramdata |> filter(!str_detect(name, "Coereba"))
39+
original_param@data <- paramdata
40+
original_exprs <- original_exprs[, !grepl("Coereba", colnames(original_exprs))]
41+
}
3242

3343
# Identifying new columns
3444
OldColNames <- colnames(original_exprs) |> unname()

man/Coereba_Concatenate.Rd

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

man/Coereba_FCSExport.Rd

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

vignettes/Workflow.qmd

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,225 @@ RetrievedData <- Coereba::Coereba_FCS_Reversal(Coereba=FCSPath)
591591
head(RetrievedData, 5)
592592
```
593593

594+
## Normalization Bypass
595+
596+
Alternatively, if you are planning to normalize the files with CytoNorm, you don't want to return a concatenated file, but individual cytoframe objects. Here is an example workflow:
597+
598+
```{r}
599+
#| label: ListOfFlowFrames
600+
CoerebaFlowFrame <- Utility_Coereba(gs=UnmixedGatingSet, subsets="root",
601+
sample.name="GROUPNAME", reference=CheckedGates, starter="Comp-PE-Vio770-A",
602+
inverse.transform = TRUE, returnType="flowframe", Individual=TRUE, outpath=NULL,
603+
columns = MarkersForToday)
604+
```
605+
606+
```{r}
607+
CoerebaFlowFrame1 <- unlist(CoerebaFlowFrame)
608+
609+
FlowSet <- flowCore::flowSet(CoerebaFlowFrame1)
610+
FlowGS <- GatingSet(FlowSet)
611+
612+
MarkersUpdated <- gsub("Comp-", "", MarkersForToday)
613+
```
614+
615+
```{r}
616+
MyBiexponentialTransform <- flowjo_biexp_trans(channelRange = 256, maxValue = 1000000,
617+
pos = 4.5, neg = 0, widthBasis = -1000)
618+
TransformList <- transformerList(MarkersUpdated, MyBiexponentialTransform)
619+
620+
InverseBiexponential <- flowjo_biexp(inverse=TRUE, channelRange=256, maxValue=1000000, pos=5.25, neg=0, width=-1000)
621+
ReverseBiexponential <- transformList(MarkersUpdated, InverseBiexponential)
622+
623+
FlowSet_Transformed <- transform(FlowGS, TransformList)
624+
```
625+
626+
```{r}
627+
ShinyGatesSplitpoints2 <- ShinyGatesSplitpoints
628+
colnames(ShinyGatesSplitpoints2) <- gsub("Comp-", "", colnames(ShinyGatesSplitpoints2))
629+
630+
PostCoereba <- Utility_NbyNPlots(x=FlowGS[4], sample.name=c("GROUPNAME"), experiment="ABs",
631+
condition="DNs", removestrings=".fcs", marginsubset="root", gatesubset="root",
632+
ycolumn="PE-Vio770-A", bins=150, clearance=0.1,
633+
gatelines=TRUE, reference=ShinyGatesSplitpoints2, outpath=StorageLocationForPlotPDF, returntype="patchwork")
634+
```
635+
636+
```{r}
637+
PostCoereba
638+
```
639+
640+
### Readding filtering data
641+
642+
```{r}
643+
Specimen <- function(x){
644+
pattern <- "([A-Z]+[0-9]+_[0-9]+)(?=_SEB)"
645+
result <- stringr::str_extract(x, pattern)
646+
result <- data.frame(Specimen = result)
647+
return(result)
648+
}
649+
650+
Experiment <- function(x){
651+
pattern <- "(?<=_)AB_[0-9]{2}(?=_)"
652+
result <- stringr::str_extract(x, pattern)
653+
result <- data.frame(Experiment = result)
654+
return(result)
655+
}
656+
```
657+
658+
```{r}
659+
OriginalPD <- pData(UnmixedGatingSet)
660+
ValuesOriginal <- OriginalPD |> pull(name)
661+
TheSpecimen <- map(ValuesOriginal, .f=Specimen) %>% bind_rows()
662+
TheExperiment <- map(ValuesOriginal, .f=Experiment) %>% bind_rows()
663+
```
664+
665+
```{r}
666+
OtherPD <- pData(FlowGS)
667+
UpdatedPD <- cbind(OtherPD, TheSpecimen, TheExperiment)
668+
UpdatedPD <- UpdatedPD |> mutate(Donor = str_replace(Specimen, "_[0-9]+$", ""))
669+
pData(FlowGS) <- UpdatedPD
670+
```
671+
672+
### Subsetting Controls
673+
674+
```{r}
675+
TheNormies <- subset(FlowGS, Donor=="NY068")
676+
#pData(TheNormies)
677+
NormCounts <- gs_pop_get_stats(TheNormies, nodes="root", type="count")
678+
TheCount <- sum(NormCounts$count)
679+
NormiesCS <- gs_cyto_data(TheNormies)
680+
#pData(NormiesCS)
681+
TheNewNormies <- cytoset_to_flowSet(NormiesCS)
682+
```
683+
684+
```{r}
685+
Batch <- pData(TheNewNormies)$Experiment
686+
```
687+
688+
```{r}
689+
library(CytoNorm)
690+
691+
NormPath <- file.path("C:", "Users", "12692", "Desktop", "CytoNorm")
692+
693+
model <- CytoNorm.train(files=TheNewNormies,
694+
labels=Batch,
695+
channels=MarkersUpdated,
696+
transformList=NULL,
697+
seed=1989,
698+
plot=TRUE,
699+
verbose=TRUE,
700+
normMethod.train = "QuantileNorm.train",
701+
normParams = list(nQ = 99),
702+
FlowSOM.params = list(nCells = TheCount,
703+
xdim = 15, ydim = 15, nClus = 9,
704+
scale = FALSE, colsToUse = MarkersUpdated),
705+
outputDir = NormPath,
706+
clean = TRUE,
707+
recompute = TRUE)
708+
```
709+
710+
```{r}
711+
LongTermStorage <- file.path(NormPath, "SmallModel.rds")
712+
saveRDS(object = model, file = LongTermStorage)
713+
```
714+
715+
```{r}
716+
LongTermStorage <- file.path(NormPath, "SmallModel.rds")
717+
previousModel <- readRDS(LongTermStorage)
718+
goal_q <- getCytoNormQuantiles(previousModel)
719+
Batch <- pData(TheNormies)$Experiment
720+
#ForClustering1 <- GetChannels(object = NormalizeGS2[[1]], markers = ForClustering)
721+
722+
model_Distribution <- CytoNorm.train(files=TheNewNormies,
723+
labels=Batch,
724+
channels=MarkersUpdated,
725+
transformList=NULL,
726+
seed=1989,
727+
plot=TRUE,
728+
verbose=TRUE,
729+
normParams = list("goal" = goal_q),
730+
FlowSOM.params = list(nCells = TheCount,
731+
xdim = 15, ydim = 15, nClus = 9,
732+
scale = FALSE, colsToUse=MarkersUpdated),
733+
outputDir = NormPath,
734+
clean = TRUE,
735+
recompute = TRUE)
736+
```
737+
738+
739+
And finally, we normalize everyone else
740+
```{r}
741+
AllNormies <- FlowGS
742+
AllCS <- gs_cyto_data(AllNormies)
743+
AllFS <- cytoset_to_flowSet(AllCS)
744+
745+
Batch <- pData(AllFS)$Experiment
746+
747+
# Use model or model_Distribution
748+
749+
NormalizeAttempt <- CytoNorm.normalize(model=model,
750+
files=AllFS,
751+
labels=Batch,
752+
transformList = NULL,
753+
verbose=TRUE,
754+
prefix="Norm_",
755+
transformList.reverse = NULL,
756+
outputDir= NormPath,
757+
clean = TRUE,
758+
write = FALSE)
759+
```
760+
761+
And finally, we update the metadata for the new GatingSet
762+
```{r}
763+
pd3 <- pData(NormalizeAttempt)
764+
colnames(pd3) <- "thename"
765+
766+
pd <- pData(FlowGS)
767+
UpdatedPD <- cbind(pd3, pd)
768+
769+
pData(NormalizeAttempt) <- UpdatedPD
770+
```
771+
772+
```{r}
773+
#| eval: FALSE
774+
Population <- "TestSet"
775+
UpdatedGS <- GatingSet(NormalizeAttempt)
776+
pData(UpdatedGS)
777+
filename <- paste0(Population, "_Norm")
778+
779+
Utility_RidgePlots(gs=UpdatedGS, subset="root", TheY="Specimen", TheFill="Experiment", outpath=NormPath, returntype="pdf", filename = filename, therows = 1)
780+
781+
NormedND <- subset(UpdatedGS, Donor == "NY068")
782+
783+
filename <- paste0(Population, "_Norm_ND006")
784+
785+
Utility_RidgePlots(gs=NormedND, subset="root", TheY="Specimen", TheFill="Experiment", outpath=NormPath, returntype="pdf", filename = filename, therows = 1)
786+
```
787+
788+
### Reverse Biexponential
789+
```{r}
790+
ReversedGS <- transform(NormalizeAttempt, ReverseBiexponential)
791+
ThisWork <- GatingSet(ReversedGS)
792+
```
793+
794+
```{r}
795+
NormedNotND <- subset(ThisWork, Donor != "NY068")
796+
pData(NormedNotND)
797+
TheCalcs <- gs_pop_get_stats(NormedNotND, nodes="root", type="count")
798+
min(TheCalcs$count)
799+
# 1000
800+
```
801+
802+
# Out to FCS File
803+
804+
```{r}
805+
Goodbye <- Coereba_Concatenate(Set=NormedNotND, metadata_cols=c("Experiment", "Donor"), outpath="C:/Users/12692/Desktop", filename="DidItWork")
806+
```
807+
808+
809+
810+
811+
812+
594813
# Generating a Summarized Experiment File
595814

596815
Taking the `Utility_Coereba()` output, we can combine it with metadata and panel information into a SummarizedExperiment object. This will permit us in turn to tie in to a lot of the existing Bioconductor project analysis infrastructure. We do this through the `Coereba_Processing()` function.

0 commit comments

Comments
 (0)