-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-BLN_BUE-experiments.Rmd
More file actions
294 lines (253 loc) · 8.84 KB
/
03-BLN_BUE-experiments.Rmd
File metadata and controls
294 lines (253 loc) · 8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
---
title: "BLN_BUE-experiments"
output: html_document
date: "2025-09-01"
---
# Setup
## Setup libraries
```{r, message = FALSE, warning = FALSE}
library(CytoScan)
library(dplyr)
library(flowCore)
library(FlowSOM)
```
## Setup file locations
```{r}
outputFolder <- "output/"
# Location of data from Liechti et al.
ALLData <- "PATH"
bueFiles <- list.files(path = ALLData, pattern = "^bue.*\\.fcs$", full.names = TRUE)
blnFiles <- list.files(path = ALLData, pattern = "^bln.*\\.fcs$", full.names = TRUE)
channels <- c("CD58", "CD10", "CD34", "CD19", "CD38", "CD20", "CD45")
```
# CytoScan example
```{r}
CS <- CytoScan()
skipPreprocess <- function(ff){
return(ff)
}
CS$preprocessFunction <- skipPreprocess
labels <- c()
for (file in c(bueFiles, blnFiles)){
if (file %in% bueFiles){
labels <- c(labels, "Buenos Aires")
} else {
labels <- c(labels, "Berlin")
}
}
CS <- addTestlabels(CS, labels)
CS <- addTestdata(CS, input=c(bueFiles, blnFiles))
CS <- generateFeatures(CS, channels, featMethod="quantiles")
CS <- generateFeatures(CS, channels, featMethod="EMD")
features_quantiles <- CS$features$test$quantiles
features_quantiles$Center <- labels
write.csv(features_quantiles, paste0(outputFolder, "ALL_PCA_quantiles.csv"))
features_EMD <- CS$features$test$EMD
features_EMD$Center <- labels
write.csv(features_EMD, paste0(outputFolder, "ALL_PCA_EMD.csv"))
```
# FlowSOM example
```{r}
set.seed(42)
blnFiles.sub <- sample(blnFiles, 20)
bueFiles.sub <- sample(bueFiles, 1)
labels <- c()
for (file in c(blnFiles.sub, bueFiles.sub)){
if (file %in% bueFiles.sub){
labels <- c(labels, "Buenos Aires")
} else {
labels <- c(labels, "Berlin")
}
}
pat <- "CD58$|CD10$|CD34$|CD19$|CD38$|CD20$|CD45"
f.set <- read.flowSet(files = c(blnFiles.sub, bueFiles.sub),
column.pattern = pat,
transformation = F,
min.limit = NULL,
truncate_max_range = FALSE)
agg <- AggregateFlowFrames(f.set, cTotal = 1e6)
fSOM <- FlowSOM(agg, colsToUse = channels, seed = 42)
```
```{r, echo=FALSE}
library(RColorBrewer)
tab10_colors <- brewer.pal(10, "Paired") # Get the tab10 color palette
# Ensure Arial font is available
library(extrafont)
font_import(pattern = "Arial")
loadfonts(device = "pdf")
file_colors <- c("#4DBBD5FF", "#E64B35FF")
p1 <- PlotPies(fsom = fSOM,
cellTypes = factor(labels[fSOM$data[,"File"]]),
colorPalette = file_colors,
maxNodeSize = 2,
nodeSizes = rep(2, 10*10))
p1 <- p1 + theme(legend.position = "none")
ggsave("figures/Figure 3B (files).pdf", p1, width = 40, height = 50, dpi = 600, units = "mm")
p2 <- PlotPies(fsom = fSOM,
cellTypes = GetMetaclusters(fSOM),
maxNodeSize = 2,
colorPalette = tab10_colors, # Apply the Okabe-Ito color palette
nodeSizes = rep(2, 10*10))
p2 <- p2 + theme(legend.position = "none")
ggsave("figures/Figure 3B (metaclusters).pdf", p2, width = 40, height = 50, dpi = 600, units = "mm")
```
# Simulation (one-by-one)
```{r}
# This is a helper is used to assess performance
getPerformance <- function(CS, files, modifiedSample, featMethod, slot){
# Computing TP, TN, FP, FN
gt <- data.frame(file = files, gt = files == modifiedSample)
pred <- data.frame(pred = CS[[slot]][[featMethod]])
pred$file <- rownames(pred)
df <- merge(gt, pred)
df$gt <- as.logical(df$gt)
df$pred <- as.logical(df$pred)
TP <- sum(df$gt & df$pred)
TN <- sum(!df$gt & !df$pred)
FP <- sum(!df$gt & df$pred)
FN <- sum(df$gt & !df$pred)
output <- data.frame(modifiedSample = modifiedSample,
featMethod = featMethod,
TP = TP,
TN = TN,
FP = FP,
FN = FN)
return(output)
}
```
```{r}
simulateDatasets <- function(dataset1, dataset2){
datasets <- list("dataset1" = dataset1,
"dataset2" = dataset2)
results <- data.frame()
for (dataset in names(datasets)){
normalSamples <- datasets[[dataset]]
anomalySamples <- datasets[[names(datasets)[names(datasets) != dataset]]]
for (anomalyIndex in seq_along(anomalySamples)){
anomalySample <- anomalySamples[anomalyIndex]
cat(dataset, anomalyIndex)
CS <- CytoScan()
skipPreprocess <- function(ff){
return(ff)
}
CS$preprocessFunction <- skipPreprocess
CS <- addTestdata(CS, input = c(normalSamples, anomalySample))
CS <- addReferencedata(CS, input = normalSamples)
for (featMethod in featMethods){
CS <- generateFeatures(CS, channels, featMethod = featMethod)
CS <- Flag(CS, flagMethod = "outlier", featMethod = featMethod)
output <- getPerformance(CS, c(normalSamples, anomalySample),
anomalySample, featMethod, "outliers")
output$majorityDataset <- dataset
output$flagMethod <- "outlier"
results <- rbind(results, output)
CS <- Flag(CS, flagMethod = "novelty", featMethod = featMethod)
output <- getPerformance(CS, c(normalSamples, anomalySample),
anomalySample, featMethod, "novelties")
output$majorityDataset <- dataset
output$flagMethod <- "novelty"
results <- rbind(results, output)
}
}
}
return(results)
}
```
Note: this step can take hours to run!
```{r}
filename <- paste0("output/datasetSimulation.csv")
if (!file.exists(filename)){
results <- simulateDatasets(bueFiles, blnFiles)
write.csv(results, filename)
}
```
# Increasing amounts
```{r}
# This is a helper is used to assess performance
getPerformance <- function(CS, files, modifiedFiles, featMethod, slot){
# Computing TP, TN, FP, FN
gt <- data.frame(file = files)
gt$gt <- gt$file %in% modifiedFiles
pred <- data.frame(pred = CS[[slot]][[featMethod]])
pred$file <- rownames(pred)
df <- merge(gt, pred)
df$gt <- as.logical(df$gt)
df$pred <- as.logical(df$pred)
TP <- sum(df$gt & df$pred)
TN <- sum(!df$gt & !df$pred)
FP <- sum(!df$gt & df$pred)
FN <- sum(df$gt & !df$pred)
output <- data.frame(featMethod = featMethod,
TP = TP,
TN = TN,
FP = FP,
FN = FN)
return(output)
}
```
```{r}
simulateBatchdatasets <- function(dataset1, dataset2, max_fraction = 0.25, random_starts = 20){
datasets <- list("dataset1" = dataset1,
"dataset2" = dataset2)
results <- data.frame()
for (dataset in names(datasets)){
normalSamples <- datasets[[dataset]]
anomalySamples <- datasets[[names(datasets)[names(datasets) != dataset]]]
# Add a maximum of max_fraction anomalies
total_size <- round(max_fraction * length(anomalySamples))
for (seed in seq(1, random_starts)){
# Select a random subset of anomalies
set.seed(seed)
seedAnomalies <- sample(anomalySamples, total_size)
for (size in seq(1, total_size)){
selectedAnomalies <- seedAnomalies[1:size]
cat(dataset, seed, size)
CS <- CytoScan()
skipPreprocess <- function(ff){
return(ff)
}
CS$preprocessFunction <- skipPreprocess
for (featMethod in featMethods){
# Evaluate outlier detection
CS <- addTestdata(CS, input = c(normalSamples, selectedAnomalies))
CS <- addReferencedata(CS, input = normalSamples)
CS <- generateFeatures(CS, channels, featMethod = featMethod)
CS <- Flag(CS, flagMethod = "outlier", featMethod = featMethod)
output <- getPerformance(CS, c(normalSamples, selectedAnomalies),
selectedAnomalies, featMethod, "outliers")
output$majorityDataset <- dataset
output$seed <- seed
output$index <- size
output$flagMethod <- "outlier"
results <- rbind(results, output)
# If novelty detection + EMD, recalculate features on reference only!
if (featMethod == "EMD"){
CS <- generateFeatures(CS, channels, featMethod = featMethod,
aggSlot = "reference", recalculate = TRUE)
}
CS <- Flag(CS, flagMethod = "novelty", featMethod = featMethod)
output <- getPerformance(CS, c(normalSamples, selectedAnomalies),
selectedAnomalies, featMethod, "novelties")
output$majorityDataset <- dataset
output$seed <- seed
output$index <- size
output$flagMethod <- "novelty"
results <- rbind(results, output)
}
}
}
}
return(results)
}
```
```{r}
featMethods <- c("quantiles", "EMD")
filename <- paste0("output/datasetBatchsimulation.csv")
if (!file.exists(filename)){
results <- simulateBatchdatasets(bueFiles, blnFiles)
write.csv(results, filename)
}
```
```{r sessionInfo, echo=FALSE}
sessionInfo()
```