Skip to content

Commit 7fb21b3

Browse files
committed
Column and Row Subsetting Arguments for both inversed and transformed values have been checked.
1 parent 91c844b commit 7fb21b3

File tree

1 file changed

+26
-46
lines changed

1 file changed

+26
-46
lines changed

R/Utility_Coereba.R

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ Utility_Coereba <- function(x, subsets, sample.name, subsample = NULL, columns=N
6262
notcolumns=NULL, reference, starter,
6363
inverse.transform = TRUE){
6464

65-
# Bringing in splitpoint and clean column names
6665
if (!is.data.frame(reference)){
6766
ReferenceLines <- read.csv(reference, check.names = FALSE)
6867
} else {ReferenceLines <- reference}
@@ -71,17 +70,14 @@ Utility_Coereba <- function(x, subsets, sample.name, subsample = NULL, columns=N
7170
colnames(ReferenceLines) <- NameCleanUp(colnames(ReferenceLines),
7271
removestrings = internalstrings)
7372

74-
# Checking for specimen identifier consistency
7573
if (sample.name != colnames(ReferenceLines)[[1]]){
7674
message("sample.name does not match the specimen identifier name found in reference,
7775
converting over")
7876
colnames(ReferenceLines)[1] <- sample.name
7977
}
8078

81-
# Internal switch in naming convention
8279
New <- ReferenceLines
8380

84-
# Retrieving a name, needs to match that in reference file naming convention.
8581
name <- keyword(x, sample.name)
8682

8783
Specimens <- ReferenceLines %>% pull(.data[[sample.name]])
@@ -93,70 +89,63 @@ Utility_Coereba <- function(x, subsets, sample.name, subsample = NULL, columns=N
9389
return(Reintegrated1)
9490
}
9591

96-
# Preparing if sending back to .fcs
9792
if (inverse.transform == TRUE){
9893
inversed_ff <- gs_pop_get_data(x, subsets, inverse.transform = TRUE)
9994
flippedDF <- as.data.frame(exprs(inversed_ff[[1]]), check.names = FALSE)
10095
flippedDF <- flippedDF |> mutate(Backups = 1:nrow(flippedDF)) |>
10196
relocate(Backups, .before=1)
10297
}
10398

104-
# Retrieving Data for Coereba assignment
10599
ff <- gs_pop_get_data(x, subsets, inverse.transform = FALSE)
106100

107-
# Extracting Data
108101
startingcells <- RowWorkAround(ff)
109-
DF <- as.data.frame(exprs(ff[[1]]), check.names = FALSE)
102+
OriginalDF <- as.data.frame(exprs(ff[[1]]), check.names = FALSE)
110103

111-
# Saving Columns for future column reordering
112-
OriginalColumnsVector <- colnames(DF)
104+
OriginalColumnsVector <- colnames(OriginalDF)
113105

114-
# Adding Backups for future row reordering
115-
DF <- DF |> mutate(Backups = 1:nrow(DF))
106+
OriginalDF <- OriginalDF |> mutate(Backups = 1:nrow(OriginalDF))
116107

117108
if (inverse.transform == TRUE){
118109
if (nrow(ff)[[1]] != nrow(inversed_ff)[[1]]){
119110
stop("Mismatched number of rows, contact Maintainer")
120111
}
121112
}
122113

123-
# Optional Downsampling
124-
if(!is.null(subsample)){DF <- slice_sample(DF, n = subsample,
114+
if(!is.null(subsample)){DF <- slice_sample(OriginalDF, n = subsample,
125115
replace = FALSE)
126116
startingcells <- nrow(DF)
127-
} else{DF <- DF}
117+
} else{DF <- OriginalDF}
128118

129-
# Identifying retained cells
130119
TheBackups <- DF |> dplyr::select(Backups)
131120

132-
#Stashing Away FSC SSC For Later
133121
StashedDF <- DF[,grep("Time|FS|SC|SS|Original|W$|H$", names(DF))]
134122
StashedDF <- cbind(TheBackups, StashedDF)
135123

136-
#Consolidating Columns Going Forward
137124
CleanedDF <- DF[,-grep("Time|FS|SC|SS|Original|W$|H$", names(DF))]
138125
BackupNames <- colnames(CleanedDF)
139126
CleanedDF <- CleanedDF |> dplyr::select(-Backups)
140127

141-
# If external columns interest specified
142128
if (!is.null(columns) && !is.null(notcolumns)){
143129
stop("Please select either columns (to keep) or notcolumns (to exclude).
144130
Leave the other agument as NULL")
145131
}
146132

147-
if (!is.null(columns)){dsf <- CleanedDF %>% select(all_of(columns))
148-
} else {dsf <- CleanedDF}
133+
if (!is.null(columns)){
134+
dsf <- CleanedDF %>% select(all_of(c(columns, starter)))
135+
} else {dsf <- CleanedDF
136+
}
149137

150-
if (!is.null(notcolumns)){dsf <- CleanedDF %>% select(-all_of(columns))
151-
} else {dsf <- dsf}
138+
if (!is.null(notcolumns)){
139+
if (starter %in% notcolumns){stop("The fluorophore ", starter, "shouldn't be included in the notcolumns list")}
140+
dsf <- CleanedDF %>% select(-all_of(notcolumns))
141+
} else {dsf <- dsf
142+
}
152143

153144
NamingColBackup <- colnames(dsf)
154145

155-
# Final Column Name Clean Up On Data Side
156146
colnames(dsf) <- NameCleanUp(colnames(dsf), removestrings = internalstrings)
157147
starter <- NameCleanUp(starter, removestrings = internalstrings)
158148

159-
# Name Swap and getting column vector ready.
160149
MyData <- dsf
161150
Columns <- colnames(MyData)
162151
Columns <- Columns[!Columns == starter]
@@ -177,27 +166,20 @@ Utility_Coereba <- function(x, subsets, sample.name, subsample = NULL, columns=N
177166
Combined <- apply(MyDataPieces, 1, paste, collapse = "")
178167
Cluster <- data.frame(Cluster=Combined)
179168
MyNewestData <- cbind(MyData, Cluster)
180-
#str(MyNewestData)
181-
182-
if (inverse.transform == FALSE){
183-
# Return Original Names
184-
Reordering <- MyNewestData
185-
UpdatedBackupNames <- c(NamingColBackup, "Cluster")
186-
colnames(Reordering) <- UpdatedBackupNames
187-
# Left Join By Backups
188-
Reordering <- cbind(TheBackups, Reordering)
189-
Reintegrated <- left_join(Reordering, StashedDF, by = "Backups")
190-
# Rearrange by original column order
191-
DesiredOrder <- c("Backups", OriginalColumnsVector, "Cluster")
192-
Reintegrated1 <- Reintegrated |> relocate(all_of(DesiredOrder)) |> select(-Backups)
193-
Reintegrated1 <- Reintegrated1 |> mutate(specimen = name[[1]])
194-
}
195169

196-
if (inverse.transform == TRUE && is.null(subsample)) {
170+
# Left-joining to original data and adding ClusterID Specimen
171+
if (inverse.transform == TRUE) {
172+
Subsetted <- left_join(TheBackups, flippedDF, by = "Backups")
197173
Cluster <- MyNewestData |> select(Cluster)
198-
DF <- as.data.frame(exprs(inversed_ff[[1]]), check.names = FALSE)
199-
Reintegrated1 <- cbind(DF, Cluster)
200-
Reintegrated1 <- Reintegrated1 |> mutate(specimen = name[[1]])
174+
Reintegrated1 <- cbind(Subsetted, Cluster)
175+
Reintegrated1 <- Reintegrated1 |> select(-Backups) |>
176+
mutate(specimen = name[[1]])
177+
} else {
178+
Subsetted <- left_join(TheBackups, OriginalDF, by = "Backups")
179+
Cluster <- MyNewestData |> select(Cluster)
180+
Reintegrated1 <- cbind(Subsetted, Cluster)
181+
Reintegrated1 <- Reintegrated1 |> select(-Backups) |>
182+
mutate(specimen = name[[1]])
201183
}
202184

203185
return(Reintegrated1)
@@ -230,8 +212,6 @@ TheCoerebaIterator <- function(x, data, reference, sample.name, name){
230212
return(Internal)
231213
}
232214

233-
234-
235215
#' Internal Utility_Coereba, work around for nrow function
236216
#'
237217
#' @param x The flowframe object

0 commit comments

Comments
 (0)