generated from Bioconductor/BuildABiocWorkshop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.Rhistory
512 lines (512 loc) · 15 KB
/
.Rhistory
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
cs <- load_cytoset_from_fcs(path = "../data/fcs_data/",pattern = "TNK-CR1")
# add mock metadata
set.seed(123)
meta <- data.frame(name = sampleNames(cs),
status = "Healthy",
panel = "T Cell",
mock_treatment = sample(x = c("Treated","Control"),size = 4,prob = c(0.5,0.5),replace = TRUE),
row.names = sampleNames(cs))
pData(cs) <- meta
# creating a GatingSet
gs <- flowWorkspace::GatingSet(cs)
spill <- keyword(gs[[1]],"$SPILLOVER") # extract spillover matrix stored within the file
compensate(gs,spill) # GatingSet will be compensated and stores the spill matrix as well
recompute(gs) # update the gs
my_trans <- readRDS(file = "../data/fj_wsp/fj_transform")
# make into transformerList object
my_trans_list <- flowWorkspace::transformerList(from = names(my_trans),
trans = my_trans)
gs <- flowWorkspace::transform(gs, my_trans_list) # transforms underlying data
# calculate and add a singlet gate
singlet_gate <- fsApply(gs_pop_get_data(gs),function(x){ # sample wise calculation using fsApply
flowStats::gate_singlet(x,
filterId = "singlet", # name for the gate
wider_gate = FALSE, # indicate if the returned gate should be linient
prediction_level = 0.95,
subsample_pct = 0.5 # percent of events to sample to calculate the gate
)
})
# add gate to the GatingSet
gs_pop_add(gs,singlet_gate, parent = "root") # add singlet_gate to the root node
recompute(gs) # recompute updates the gs
# calculate a live gate
## Example of a quantile gate
live_gate <- fsApply(gs_pop_get_data(gs,"singlet"), # indicating that estimation should be done on filtered data: singlet
function(x){
gate <- openCyto::gate_quantile(fr = x, channel = "U450-A",
probs = 0.95,
filterId = "live"
)
# keep negative events
gate@max <- gate@min
gate@min <- -Inf
return(gate)
})
# add live gate
gs_pop_add(gs, parent = "singlet",gate = live_gate)
recompute(gs)
# calculate lymphocyte gate
## Example of using flowClust
lymphocyte_gate <- fsApply(gs_pop_get_data(gs,"live"),
function(x){
openCyto::gate_flowclust_2d(fr = x,
xChannel = "FSC-A",
yChannel = "SSC-A",
filterId = "lymphocytes",
K = 1,target = c(1E5,0.5E3))
})
# call it lymphocytes
lymphocyte_gate <- lapply(lymphocyte_gate,
function(x){
x@filterId <- "lymphocytes"
x
})
# add lymphocyte gate
gs_pop_add(gs, parent = "live", gate = lymphocyte_gate)
recompute(gs)
# add T cells gate
## Example of rectangleGate
# using rectangle gate to add T cell gate
cd3_rectanlge <- matrix(c(140, 205, 0, 200),
nrow = 2,ncol = 2,
byrow = F,
dimnames = list(NULL,c("V510-A","U570-A"))) # channel names
cd3_rectangle_gate <- rectangleGate(.gate = cd3_rectanlge,filterId = "CD3+ T cells")
# add gate
gs_pop_add(gs, gate = cd3_rectangle_gate, parent = "lymphocytes")
recompute(gs)
# add NKT cell gate
## Example of polygonGate
# define coordinates
nkt_poly <- matrix(c(
115,140,
150,150,
150,180,
200,180,
200,140), # coordinates
ncol = 2,
byrow = T,
dimnames = list(NULL, c("R670-A","V510-A"))
)
# convert to gate
nkt_poly_gate <- polygonGate(nkt_poly,
filterId = "NKT cells")
# move gate and make smaller
nkt_poly_gate <- flowCore::transform_gate(nkt_poly_gate,
dx = 1,
scale = c(1.05,1.05)
)
# add to gs
gs_pop_add(gs,nkt_poly_gate, parent = "CD3+ T cells")
recompute(gs)
# Add non-NKT cells
## Example rangeGate
# make a list of sample specific gates
non_nkt <- lapply(gs,
function(x){
ff <- gh_pop_get_data(x, "CD3+ T cells") # extract data at specific node for cleaner calculation
flowStats::rangeGate(ff,
stain = "R670-A",
filterId = "non-NKT Cells",
positive = FALSE,
alpha = 0.1
)
})
non_nkt$`1615fa39c8b_4002_TNK-CR1.fcs` <- transform_gate(non_nkt$`1615fa39c8b_4002_TNK-CR1.fcs`,
dx = 25)
plot(gs)
non_nkt
transform_gate(non_nkt$`1615fa39c8b_4002_TNK-CR1.fcs`,
dx = 25)
non_nkt$`1615fa39c8b_4002_TNK-CR1.fcs` <- transform_gate(non_nkt$`4002_TNK-CR1.fcs`,
dx = 25)
# add non_nkt
gs_pop_add(gs, non_nkt,parent = "CD3+ T cells")
sampleNames(gs)
# make a list of sample specific gates
non_nkt <- lapply(gs,
function(x){
ff <- gh_pop_get_data(x, "CD3+ T cells") # extract data at specific node for cleaner calculation
flowStats::rangeGate(ff,
stain = "R670-A",
filterId = "non-NKT Cells",
positive = FALSE,
alpha = 0.1
)
})
non_nkt[[2]] <- transform_gate(non_nkt[[2]],
dx = 25)
# add non_nkt
gs_pop_add(gs, non_nkt,parent = "CD3+ T cells")
recompute(gs)
# identify conventional T cells
# Example use multiple gating tools and piping
## Estimate gate by sampling the gatinset
conv_t_cell_gate <- gs |>
gs_pop_get_data(y = "non-NKT Cells") |>
cytoset_to_flowSet() |>
(function(x){
set.seed(123)
sample_n <- 1E3
all_exprs <- fsApply(x,function(y)exprs(y)[sample(nrow(y),sample_n),])
all_exprs <- flowFrame(all_exprs)
attrs = c("min","max")
# identify cutpoints
g1 <- openCyto::gate_quantile(all_exprs,
channel = "B515-A",
prob = 0.98
)
g1_attrs <- sapply(attrs, function(y){
attr(g1,y)
})
g2 <- openCyto::gate_quantile(all_exprs,
channel = "G575-A",
prob = 0.90
)
g2_attrs <- sapply(attrs, function(y){
attr(g2,y)
})
# get cutpoints
g1_cutpoint <- g1_attrs[!is.infinite(g1_attrs)]
g2_cutpoint <- g2_attrs[!is.infinite(g2_attrs)]
# make rectangleGate
qg <- rectangleGate(list("B515-A" = c(g1_cutpoint,-Inf),
"G575-A" = c(g2_cutpoint,-Inf)),
filterId = "conv_Tcells")
})()
# adjust gates
conv_t_cell_gate <- flowCore::transform_gate(conv_t_cell_gate,
dx = 11,
dy = 11
)
# add quad gate
gs_pop_add(gs, conv_t_cell_gate,parent = "non-NKT Cells")
recompute(gs)
# identify MAIT cells
# add MAIT cells
## Example of estimation using collapsed data
mait_gate_all <- openCyto::gate_flowclust_2d(fr = flowFrame(fsApply(gs_pop_get_data(gs,"conv_Tcells"),
function(y){
sample_n = min(nrow(y), 1E4)
exprs(y)[sample(nrow(y),sample_n),]
}
)
),
xChannel = "V710-A",
yChannel = "G660-A",target = c(175,175),
K = 14,filterId = "MAIT Cells" # K indicates number of clusters to estimate
)
# fix filterId
mait_gate_all@filterId <- "MAIT Cells"
# scale gate
mait_gate_all <- scale_gate(mait_gate_all, scale = 2)
# add gate
gs_pop_add(gs,gate = mait_gate_all, parent = "conv_Tcells")
recompute(gs)
# add not MAIT gate
## Example of booleanFilter
not_mait <- booleanFilter(`!MAIT Cells`, filterId = "not_MAIT")
# add boolean gate
gs_pop_add(gs, not_mait, parent = "conv_Tcells")
Example of how to use booleanFilter to create a polygon gate
# Example of how to use booleanFilter to create a polygon gate
not_mait_gate <- lapply(seq(1,length(
gs_pop_get_gate(gs,"MAIT Cells")),1),
function(x){
gate = gh_pop_get_gate(gs[[x]],"MAIT Cells")
# enlarge for stringency
gate <- scale_gate(gate,3)
ff <- cytoframe_to_flowFrame(gh_pop_get_data(gs[[x]],"conv_Tcells"))
fl <- filter(ff,gate)
idx <- which(!fl@subSet)
set.seed(123)
sample_n <- min(length(idx),1E5)
m.names <- colnames(gate@cov)
events <- exprs(ff)[idx[sample(length(idx),sample_n)],m.names]
c.hull <- chull(events)
polygon_mait <- polygonGate(.gate = events[
c(c.hull,c.hull[1]),],
filterId = "not_MAIT_Polygon")
}
);names(not_mait_gate) <- sampleNames(gs)
# add gate
gs_pop_add(gs,not_mait_gate,parent = "conv_Tcells")
recompute(gs)
# add a quad gate
cd4_cd8_quad_gate <- lapply(gs_pop_get_data(gs,"not_MAIT"),
function(x){
qg <- openCyto::gate_quad_tmix(x,
channels = c("U785-A",
"V570-A"),
K = 2)
# give readable names
r.names <- c("U785-A" = "CD4", # these are human readable names
"V570-A" = "CD8a")
for(i in 1:length(qg)){ # iterate over each quadrant
sapply(1:length(r.names),function(x){ # iterate over human readable names
current.name <- attr(qg[[i]],"filterId") # keep a record of the current name
attr(qg[[i]],"filterId") <<- gsub( # substitute and apply to qg
pattern = names(r.names)[x],
replacement = r.names[x],
x = current.name)
})
}
return(qg) # return modified gate
})
# add gate
gs_pop_add(gs, cd4_cd8_quad_gate,parent = "not_MAIT_Polygon")
recompute(gs)
# cleanup
rm(list = ls()[!(ls() %in% "gs")])
autoplot(gs[[1]])
library(ggcyto)
autoplot(gs[[1]])
autoplot(gs[[1]], inverse.transform = T)
rm(gs)
gs <- "a"
rm(gs)
cs <- load_cytoset_from_fcs("../data/fcs_data/",pattern = "TNK-CR1")
cs <- load_cytoset_from_fcs(path = "../data/fcs_data/",pattern = "TNK-CR1")
# add mock metadata
set.seed(123)
meta <- data.frame(name = sampleNames(cs),
status = "Healthy",
panel = "T Cell",
mock_treatment = sample(x = c("Treated","Control"),size = 4,prob = c(0.5,0.5),replace = TRUE),
row.names = sampleNames(cs))
pData(cs) <- meta
# creating a GatingSet
gs <- flowWorkspace::GatingSet(cs)
spill <- keyword(gs[[1]],"$SPILLOVER") # extract spillover matrix stored within the file
compensate(gs,spill) # GatingSet will be compensated and stores the spill matrix as well
recompute(gs) # update the gs
my_trans <- readRDS(file = "../data/fj_wsp/fj_transform")
# make into transformerList object
my_trans_list <- flowWorkspace::transformerList(from = names(my_trans),
trans = my_trans)
gs <- flowWorkspace::transform(gs, my_trans_list) # transforms underlying data
gh_get_transformations(gs[[1]])
singlet_gate <- fsApply(gs_pop_get_data(gs),function(x){ # sample wise calculation using fsApply
flowStats::gate_singlet(x,
filterId = "singlet", # name for the gate
wider_gate = FALSE, # indicate if the returned gate should be linient
prediction_level = 0.95,
subsample_pct = 0.5 # percent of events to sample to calculate the gate
)
})
# add gate to the GatingSet
gs_pop_add(gs,singlet_gate, parent = "root") # add singlet_gate to the root node
recompute(gs) # recompute updates the gs
# calculate a live gate
## Example of a quantile gate
live_gate <- fsApply(gs_pop_get_data(gs,"singlet"), # indicating that estimation should be done on filtered data: singlet
function(x){
gate <- openCyto::gate_quantile(fr = x, channel = "U450-A",
probs = 0.95,
filterId = "live"
)
# keep negative events
gate@max <- gate@min
gate@min <- -Inf
return(gate)
})
# add live gate
gs_pop_add(gs, parent = "singlet",gate = live_gate)
recompute(gs)
# calculate lymphocyte gate
## Example of using flowClust
lymphocyte_gate <- fsApply(gs_pop_get_data(gs,"live"),
function(x){
openCyto::gate_flowclust_2d(fr = x,
xChannel = "FSC-A",
yChannel = "SSC-A",
filterId = "lymphocytes",
K = 1,target = c(1E5,0.5E3))
})
# call it lymphocytes
lymphocyte_gate <- lapply(lymphocyte_gate,
function(x){
x@filterId <- "lymphocytes"
x
})
# add lymphocyte gate
gs_pop_add(gs, parent = "live", gate = lymphocyte_gate)
recompute(gs)
# add T cells gate
## Example of rectangleGate
# using rectangle gate to add T cell gate
cd3_rectanlge <- matrix(c(140, 205, 0, 200),
nrow = 2,ncol = 2,
byrow = F,
dimnames = list(NULL,c("V510-A","U570-A"))) # channel names
cd3_rectangle_gate <- rectangleGate(.gate = cd3_rectanlge,filterId = "CD3+ T cells")
# add gate
gs_pop_add(gs, gate = cd3_rectangle_gate, parent = "lymphocytes")
recompute(gs)
# add NKT cell gate
## Example of polygonGate
# define coordinates
nkt_poly <- matrix(c(
115,140,
150,150,
150,180,
200,180,
200,140), # coordinates
ncol = 2,
byrow = T,
dimnames = list(NULL, c("R670-A","V510-A"))
)
# convert to gate
nkt_poly_gate <- polygonGate(nkt_poly,
filterId = "NKT cells")
# move gate and make smaller
nkt_poly_gate <- flowCore::transform_gate(nkt_poly_gate,
dx = 1,
scale = c(1.05,1.05)
)
# add to gs
gs_pop_add(gs,nkt_poly_gate, parent = "CD3+ T cells")
recompute(gs)
# Add non-NKT cells
## Example rangeGate
# make a list of sample specific gates
non_nkt <- lapply(gs,
function(x){
ff <- gh_pop_get_data(x, "CD3+ T cells") # extract data at specific node for cleaner calculation
flowStats::rangeGate(ff,
stain = "R670-A",
filterId = "non-NKT Cells",
positive = FALSE,
alpha = 0.1
)
})
non_nkt[[2]] <- transform_gate(non_nkt[[2]],
dx = 25)
# add non_nkt
gs_pop_add(gs, non_nkt,parent = "CD3+ T cells")
recompute(gs)
# identify conventional T cells
# Example use multiple gating tools and piping
## Estimate gate by sampling the gatinset
conv_t_cell_gate <- gs |>
gs_pop_get_data(y = "non-NKT Cells") |>
cytoset_to_flowSet() |>
(function(x){
set.seed(123)
sample_n <- 1E3
all_exprs <- fsApply(x,function(y)exprs(y)[sample(nrow(y),sample_n),])
all_exprs <- flowFrame(all_exprs)
attrs = c("min","max")
# identify cutpoints
g1 <- openCyto::gate_quantile(all_exprs,
channel = "B515-A",
prob = 0.98
)
g1_attrs <- sapply(attrs, function(y){
attr(g1,y)
})
g2 <- openCyto::gate_quantile(all_exprs,
channel = "G575-A",
prob = 0.90
)
g2_attrs <- sapply(attrs, function(y){
attr(g2,y)
})
# get cutpoints
g1_cutpoint <- g1_attrs[!is.infinite(g1_attrs)]
g2_cutpoint <- g2_attrs[!is.infinite(g2_attrs)]
# make rectangleGate
qg <- rectangleGate(list("B515-A" = c(g1_cutpoint,-Inf),
"G575-A" = c(g2_cutpoint,-Inf)),
filterId = "conv_Tcells")
})()
# adjust gates
conv_t_cell_gate <- flowCore::transform_gate(conv_t_cell_gate,
dx = 11,
dy = 11
)
# add quad gate
gs_pop_add(gs, conv_t_cell_gate,parent = "non-NKT Cells")
recompute(gs)
# identify MAIT cells
# add MAIT cells
## Example of estimation using collapsed data
mait_gate_all <- openCyto::gate_flowclust_2d(fr = flowFrame(fsApply(gs_pop_get_data(gs,"conv_Tcells"),
function(y){
sample_n = min(nrow(y), 1E4)
exprs(y)[sample(nrow(y),sample_n),]
}
)
),
xChannel = "V710-A",
yChannel = "G660-A",target = c(175,175),
K = 14,filterId = "MAIT Cells" # K indicates number of clusters to estimate
)
# fix filterId
mait_gate_all@filterId <- "MAIT Cells"
# scale gate
mait_gate_all <- scale_gate(mait_gate_all, scale = 2)
# add gate
gs_pop_add(gs,gate = mait_gate_all, parent = "conv_Tcells")
recompute(gs)
# add not MAIT gate
## Example of booleanFilter
not_mait <- booleanFilter(`!MAIT Cells`, filterId = "not_MAIT")
# add boolean gate
gs_pop_add(gs, not_mait, parent = "conv_Tcells")
recompute(gs)
# Example of how to use booleanFilter to create a polygon gate
not_mait_gate <- lapply(seq(1,length(
gs_pop_get_gate(gs,"MAIT Cells")),1),
function(x){
gate = gh_pop_get_gate(gs[[x]],"MAIT Cells")
# enlarge for stringency
gate <- scale_gate(gate,3)
ff <- cytoframe_to_flowFrame(gh_pop_get_data(gs[[x]],"conv_Tcells"))
fl <- filter(ff,gate)
idx <- which(!fl@subSet)
set.seed(123)
sample_n <- min(length(idx),1E5)
m.names <- colnames(gate@cov)
events <- exprs(ff)[idx[sample(length(idx),sample_n)],m.names]
c.hull <- chull(events)
polygon_mait <- polygonGate(.gate = events[
c(c.hull,c.hull[1]),],
filterId = "not_MAIT_Polygon")
}
);names(not_mait_gate) <- sampleNames(gs)
# add gate
gs_pop_add(gs,not_mait_gate,parent = "conv_Tcells")
recompute(gs)
# add a quad gate
cd4_cd8_quad_gate <- lapply(gs_pop_get_data(gs,"not_MAIT"),
function(x){
qg <- openCyto::gate_quad_tmix(x,
channels = c("U785-A",
"V570-A"),
K = 2)
# give readable names
r.names <- c("U785-A" = "CD4", # these are human readable names
"V570-A" = "CD8a")
for(i in 1:length(qg)){ # iterate over each quadrant
sapply(1:length(r.names),function(x){ # iterate over human readable names
current.name <- attr(qg[[i]],"filterId") # keep a record of the current name
attr(qg[[i]],"filterId") <<- gsub( # substitute and apply to qg
pattern = names(r.names)[x],
replacement = r.names[x],
x = current.name)
})
}
return(qg) # return modified gate
})
# add gate
gs_pop_add(gs, cd4_cd8_quad_gate,parent = "not_MAIT_Polygon")
recompute(gs)
gh_get_transformations(gs[[1]])
autoplot(gs[[1]], inverse.transform = T)
?autoplot
autoplot(gs[[1]], axis_inverse_trans = T)
gh_get_transformations(gs[[1]])
autoplot(gs[[1]], gate = "lymphocytes", axis_inverse_trans = TRUE)
autoplot(gs[[1]], gate = "lymphocytes", axis_inverse_trans = FALSE)
autoplot(gs[[1]], gate = "lymphocytes")+axis_y_inverse_trans()