-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreparing_semi_synth_data_11p.Rmd
372 lines (303 loc) · 10.8 KB
/
preparing_semi_synth_data_11p.Rmd
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
---
title: "Preparing semi-synthetic data: 11 parameters"
output: html_document
params:
experience: "20190716_billes"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = T)
```
```{r}
library(flowWorkspace)
library(caret)
library(flowCore)
```
# Introduction
An `R Markdown` script has been developed in order to prepare flow cytometry data for algorithm benchmarking. The data has been previously pre-treated and gated in `FlowJo`. This script enables importing and exploring the data, applying the gating strategy performed on `FlowJo`, labelling the examples according to this gating, performing down-sampling and exporting the data for further analysis.
# Data
Stained beads (11 markers), 18 detectors.
# Parameters
```{r}
params
```
# Open `fcs` files
## Compensated
```{r}
comp <- list.files(".", pattern = "compens_18") # Compensated
```
```{r}
colnames(read.FCS("compens_18_01_NK.fcs")@exprs)
```
\
We are interested in columns 1:22.
Files:
```{r}
comp
```
\
Label list:
```{r}
label_list <- c("P01", "P02", "P03", "P04", "P05", "P06", "P07", "P08", "P09", "P10",
"P11", "P12", "P13", "P14", "P15", "P16", "P17", "P18", "P19", "P20")
```
Open the compensated files and put the selected columns in a labelled dataframe,:
```{r}
j <- 1
for(i in 1:length(comp)){
c <- as.data.frame(read.FCS(comp[i])@exprs[,1:22])
c <- cbind(c, label = label_list[i])
if(j == 1){
data_comp <- c
}else{
data_comp <- rbind(data_comp, c)
}
j <- j + 1
rm(c)
}
summary(data_comp[,"label"])
```
## Uncompensated data
```{r}
( uncomp <- list.files(".", pattern = "uncomp") )
```
Open the uncompensated files and put the selected columns in a labelled dataframe,:
```{r}
j <- 1
for(i in 1:length(uncomp)){
c <- as.data.frame(read.FCS(uncomp[i])@exprs[,1:22])
c <- cbind(c, label = label_list[i])
if(j == 1){
data_uncomp <- c
}else{
data_uncomp <- rbind(data_uncomp, c)
}
j <- j + 1
rm(c)
}
table(summary(data_comp[,"label"]) == summary(data_uncomp[,"label"]))
```
# Naming the variables
```{r}
colnames(data_comp)
```
Replacing the fluorophores by the markers:
```{r}
colnames(data_comp)[5:22] <- c("355-A-A", "355-B-A", "CD25_BV786",
"405-B-A", "CD45RA_BV650", "405-D-A",
"405-E-A", "CD27_BV786", "CD8a_PerCP-Cy55",
"CD4_BB515", "CCR7_PE-Cy7", "561-B-A",
"561-C-A", "CD19_PECF554", "CD3_PE",
"CD56_APCH7", "CD127_APC-R700", "CD38-APC")
colnames(data_uncomp) <- colnames(data_comp)
colnames(data_comp)
```
# Frequencies
Bead frequencies are completely arbitrary.
```{r}
(comp_freq <- (prop.table(table(data_comp$label))))
```
## Equivalent frequencies:
```{r}
# looking for the minimum
min_beads <- which.min(table(data_comp$label))
table(data_comp$label)[min_beads]
```
We could do 15621*20 = 312420 beads, but it is weird.
We wil take 15000 beads/pop in order to have a final sample with 300000 beads.
```{r}
n <- 15000
down_sample <- c()
set.seed(23)
for(i in 1:length(label_list)){
sel <- sample(rownames(data_comp[data_comp$label == label_list[i],]), n, replace = F)
down_sample <- c(down_sample, sel)
rm(sel)
}
data_comp_eq <- data_comp[down_sample,]
table(data_comp_eq$label)
```
Great
```{r}
data_uncomp_eq <- data_uncomp[down_sample,]
table(data_uncomp_eq$label)
```
## Different frequencies
Let's use the frequancies used to generate synthetic samples for this experimental design:
```{r}
(theo_props <- readRDS("props_13_a_2019-08-01")/100 )
```
Which is the limitant?
If we take the max freq:
```{r}
max_freq <- which.max(theo_props)
theo_props[max_freq]
table(data_comp$label)[max_freq]
```
So, the total number of beads will be:
```{r}
(tot <- floor(table(data_comp$label)[max_freq]/theo_props[max_freq]))
```
Thus, the number of cells for each population will be:
```{r}
(ns <- round(tot * theo_props))
```
```{r}
sum(ns)
```
If we substract these numbers to the toal number of beads:
```{r}
table(data_comp$label) - ns
```
Great. Let's do the sample:
```{r}
down_sample <- c()
set.seed(23)
for(i in 1:length(theo_props)){
sel <- sample(rownames(data_comp[data_comp$label == label_list[i],]), ns[i], replace = F)
down_sample <- c(down_sample, sel)
rm(sel)
}
data_comp_diff <- data_comp[down_sample,]
table(data_comp_diff$label)
```
```{r}
data_uncomp_diff <- data_comp[down_sample,]
table(table(data_uncomp_diff$label) == table(data_comp_diff$label))
```
# Rearranging
We have 3 kind of samples, in compensate and uncompensate version.
Let's randomly rearrange all of them:
```{r}
## ALL THE BEADS
(n <- nrow(data_comp))
```
```{r}
# Randomly sampling
set.seed(42)
x <- sample(1:n, n, replace = F)
# Rearranging the dataframes
data_comp <-data_comp[x,]
data_uncomp <-data_uncomp[x,]
# Renaming the rows (in order)
rownames(data_comp) <- 1:n
rownames(data_uncomp) <- 1:n
## EQUAL FREQUENCIES
n <- nrow(data_comp_eq)
# Randomly sampling
set.seed(42)
x <- sample(1:n, n, replace = F)
# Rearranging the dataframes
data_comp_eq <-data_comp_eq[x,]
data_uncomp_eq <-data_uncomp_eq[x,]
# Renaming the rows (in order)
rownames(data_comp_eq) <- 1:n
rownames(data_uncomp_eq) <- 1:n
## DIFFERENT FREQUENCIES
n <- nrow(data_comp_diff)
# Randomly sampling
set.seed(42)
x <- sample(1:n, n, replace = F)
# Rearranging the dataframes
data_comp_diff <-data_comp_diff[x,]
data_uncomp_diff <-data_uncomp_diff[x,]
# Renaming the rows (in order)
rownames(data_comp_diff) <- 1:n
rownames(data_uncomp_diff) <- 1:n
```
# Conversion to `FlowFrame` class objects and export to `.fcs`
## All parameters
```{r}
library(flowCore)
ff_comp <- new("flowFrame", exprs = as.matrix(data_comp[, 1:22])) # 1:22 channels, w/o the labels
ff_uncomp <- new("flowFrame", exprs = as.matrix(data_uncomp[, 1:22]))
ff_comp_eq <- new("flowFrame", exprs = as.matrix(data_comp_eq[, 1:22]))
ff_uncomp_eq <- new("flowFrame", exprs = as.matrix(data_uncomp_eq[, 1:22]))
ff_comp_diff <- new("flowFrame", exprs = as.matrix(data_comp_diff[, 1:22]))
ff_uncomp_diff <- new("flowFrame", exprs = as.matrix(data_uncomp_diff[, 1:22]))
summary(ff_comp)
```
```{r}
write.FCS(ff_comp, paste("ff_comp_18_all_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_uncomp, paste("ff_non_comp_18_all_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_comp_eq, paste("ff_comp_eq_18_all_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_uncomp_eq, paste("ff_non_comp_18_eq_all_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_comp_diff, paste("ff_comp_diff_18_all_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_uncomp_diff, paste("ff_non_comp_diff_18_all_", params$experience, "_", params$beads, ".fcs", sep = ""))
```
## All detectors, without FSC, SSC
```{r}
ff_comp <- new("flowFrame", exprs = as.matrix(data_comp[, 5:22])) # 1:22 channels, w/o the labels
ff_uncomp <- new("flowFrame", exprs = as.matrix(data_uncomp[, 5:22]))
ff_comp_eq <- new("flowFrame", exprs = as.matrix(data_comp_eq[, 5:22]))
ff_uncomp_eq <- new("flowFrame", exprs = as.matrix(data_uncomp_eq[, 5:22]))
ff_comp_diff <- new("flowFrame", exprs = as.matrix(data_comp_diff[, 5:22]))
ff_uncomp_diff <- new("flowFrame", exprs = as.matrix(data_uncomp_diff[, 5:22]))
summary(ff_comp)
```
```{r}
write.FCS(ff_comp, paste("ff_comp_18_fluo_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_uncomp, paste("ff_non_comp_18_fluo_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_comp_eq, paste("ff_comp_eq_18_fluo_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_uncomp_eq, paste("ff_non_comp_eq_18_fluo_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_comp_diff, paste("ff_comp_diff_18_fluo_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_uncomp_diff, paste("ff_non_comp_diff_18_fluo_", params$experience, "_", params$beads, ".fcs", sep = ""))
```
## 11 detectors (with FSC, SSC)
```{r}
colnames(data_comp)
```
```{r}
(c <- colnames(data_comp)[c(1,2,3,4,7,9,12,13,14,15,18,19,20,21,22)])
```
```{r}
ff_comp <- new("flowFrame", exprs = as.matrix(data_comp[, c])) # 1:22 channels, w/o the labels
ff_uncomp <- new("flowFrame", exprs = as.matrix(data_uncomp[, c]))
ff_comp_eq <- new("flowFrame", exprs = as.matrix(data_comp_eq[, c]))
ff_uncomp_eq <- new("flowFrame", exprs = as.matrix(data_uncomp_eq[, c]))
ff_comp_diff <- new("flowFrame", exprs = as.matrix(data_comp_diff[, c]))
ff_uncomp_diff <- new("flowFrame", exprs = as.matrix(data_uncomp_diff[, c]))
summary(ff_comp)
```
```{r}
write.FCS(ff_comp, paste("ff_comp_11_all_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_uncomp, paste("ff_non_comp_11_all_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_comp_eq, paste("ff_comp_eq_11_all_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_uncomp_eq, paste("ff_non_comp_11_eq_all_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_comp_diff, paste("ff_comp_diff_11_all_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_uncomp_diff, paste("ff_non_comp_diff_11_all_", params$experience, "_", params$beads, ".fcs", sep = ""))
```
## 11 detectors (without FSC, SSC)
```{r}
(c <- colnames(data_comp)[c(7,9,12,13,14,15,18,19,20,21,22)])
```
```{r}
ff_comp <- new("flowFrame", exprs = as.matrix(data_comp[, c])) # 1:22 channels, w/o the labels
ff_uncomp <- new("flowFrame", exprs = as.matrix(data_uncomp[, c]))
ff_comp_eq <- new("flowFrame", exprs = as.matrix(data_comp_eq[, c]))
ff_uncomp_eq <- new("flowFrame", exprs = as.matrix(data_uncomp_eq[, c]))
ff_comp_diff <- new("flowFrame", exprs = as.matrix(data_comp_diff[, c]))
ff_uncomp_diff <- new("flowFrame", exprs = as.matrix(data_uncomp_diff[, c]))
summary(ff_comp)
```
```{r}
write.FCS(ff_comp, paste("ff_comp_11_fluo_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_uncomp, paste("ff_non_comp_11_fluo_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_comp_eq, paste("ff_comp_eq_11_fluo_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_uncomp_eq, paste("ff_non_comp_11_eq_fluo_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_comp_diff, paste("ff_comp_diff_11_fluo_", params$experience, "_", params$beads, ".fcs", sep = ""))
write.FCS(ff_uncomp_diff, paste("ff_non_comp_diff_11_fluo_", params$experience, "_", params$beads, ".fcs", sep = ""))
```
Visulization
```{r}
library(ggcyto)
autoplot(ff_comp, "CD56_APCH7", "CD3_PE")
```
```{r}
autoplot(ff_uncomp, "CD56_APCH7", "CD3_PE")
```
## Saving the labels
```{r}
saveRDS(data_comp[, "label"], paste("labels", params$experience, sep = "_"))
saveRDS(data_comp_eq[, "label"], paste("labels_eq", params$experience, sep = "_"))
saveRDS(data_comp_diff[, "label"], paste("labels_diff", params$experience, sep = "_"))
```