-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_flowsom.Rmd
397 lines (312 loc) · 7.22 KB
/
test_flowsom.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
---
title: "test flowSOM - synthetic sample"
output:
html_document:
number_sections: yes
theme: journal
toc: yes
toc_float: yes
author: Anna Guadall
params:
file: "ff_02_2019-04-01.fcs"
pheno: "pheno_02_2019-04-01"
labels: "labels_02_2019-04-01"
seed: 23
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Reading the data
```{r}
# import the fcs file
#ff <- flowCore::read.FCS(params$file)
# Import the labels
labels <- readRDS(params$labels)
# number of populations
c <- length(levels(labels))
```
# The easy way (reading and clustering)
Creating a large list from an FCS file with the wrapper `FlowSOM`
No scaling:
```{r}
#library(flowCore)
#fframe <- read.FCS(params$file, transformation = F)
library(FlowSOM)
fs <- FlowSOM(params$file,
# Input options:
compensate = F,transform = F,
scale = F,
# SOM options:
colsToUse = colnames(params$file),
#xdim = 7, ydim = 7, # determines the number of nodes and dimensions of the grid
# Metaclustering options:
nClus = 11,
# Seed for reproducible results:
seed = params$seed)
PlotStars(fs$FlowSOM, backgroundValues = as.factor(fs$metaclustering))
```
```{r}
PlotStars(fs$FlowSOM, backgroundValues = as.factor(fs$metaclustering), view = "grid")
```
# Step by step
## Reading the data
```{r}
ff <- suppressWarnings(flowCore::read.FCS(params$file))
ff
```
```{r}
fs <- ReadInput(ff, compensate = F, transform = F, scale = F)
# compensate = F, transform = F : there is no compensation matrix
str(fs, max.level = 2)
```
## Building the self-organizing map
```{r, eval = FALSE}
# Which columns?
names(fs$prettyColnames)
```
In this case, these are all the columns, no need to specify.
```{r}
set.seed(params$seed) # set the seed! The same used in "the easy way" if we want to have the same results
fs <- BuildSOM(fs)
str(fs$map,max.level = 2)
```
## Building the minimal spanning tree
**BuildMST()** will return a FLowSOM object with extra information contained in the $MST parameter.
```{r}
fs <- BuildMST(fs,tSNE=TRUE)
str(fs$MST)
```
## Plotting
### Minimal Spanning Tree
```{r}
PlotStars(fs)
```
### SOM grid
```{r}
PlotStars(fs, view="grid")
```
### tSNE
Only possible when tSNE was TRUE in BuildMST.
```{r}
PlotStars(fs,view="tSNE")
```
## Looking just at one specific marker with `PlotMarker`
```{r}
print(colnames(fs$map$medianValues))
```
```{r}
for(i in 1:length(colnames(fs$map$medianValues))){
PlotMarker(fs, colnames(fs$map$medianValues)[[i]])
}
```
```{r}
for(i in 1:length(colnames(fs$map$medianValues))){
PlotMarker(fs, colnames(fs$map$medianValues)[[i]], view = "tSNE")
}
```
## Numbering the nodes
```{r}
PlotNumbers(UpdateNodeSize(fs,reset=TRUE), nodeSize = 15)
```
```{r}
PlotNumbers(UpdateNodeSize(fs,reset=TRUE), nodeSize = 20, view = "grid")
```
```{r}
PlotNumbers(UpdateNodeSize(fs,reset=TRUE), nodeSize = 15, view = "tSNE")
```
```{r}
PlotClusters2D(fs, "CD3","CD19",c(8, 9, 10, 18, 19, 20))
```
## Meta-clustering the data
This can be the first step in further analysis of the data, and often gives a good approximation of manual gating results.
```{r}
head(fs$map$codes)
dim(fs$map$codes)
```
100 nodes, 5 markers.
K: Number of clusters
```{r}
mc <- metaClustering_consensus(fs$map$codes, k = c) # c: number of cell types on the synthetic data
mc
```
```{r}
PlotStars(fs, backgroundValues = as.factor(mc))
```
```{r}
PlotStars(fs,view="grid", backgroundValues = as.factor(mc))
```
```{r}
PlotStars(fs,view="tSNE", backgroundValues = as.factor(mc))
```
### Meta-clustering each cell individually
```{r}
head(fs$map$mapping)
```
```{r}
dim(fs$map$mapping)
```
There are `r nrow(fs$map$mapping)` files, one per cell.
```{r}
summary(fs$map$mapping[,1])
# table(fs$map$mapping[,1])
```
Column 1 assigns one node to each cell (`r max(fs$map$mapping[,1])` nodes).
```{r}
summary(fs$map$mapping[,2])
```
I don't know what is on column 2.
```{r}
mc_cell <- mc[fs$map$mapping[,1]]
mc_cell[1:10]
```
```{r}
table(mc_cell)
```
It looks nice. As I now how my "cells" are ordered, I can verify for every type of them:
B cells:
```{r}
n <- 909
table(mc_cell[1:n])
```
NK cells:
```{r}
table(mc_cell[(n+1):(n+n)])
```
T4 cells:
```{r}
t <- n + n
table(mc_cell[(t+1):(t+n)])
```
T8 cells:
```{r}
t <- t + n
table(mc_cell[(t+1):(t+n)])
```
NKT_NN cells:
```{r}
t <- t + n
table(mc_cell[(t+1):(t+n)])
```
NKT_4 cells:
```{r}
t <- t + n
table(mc_cell[(t+1):(t+n)])
```
NKT_8 cells:
```{r}
t <- t + n
table(mc_cell[(t+1):(t+n)])
```
U1 cells:
```{r}
t <- t + n
table(mc_cell[(t+1):(t+n)])
```
U2 cells:
```{r}
t <- t + n
table(mc_cell[(t+1):(t+n)])
```
U3 cells:
```{r}
t <- t + n
table(mc_cell[(t+1):(t+n)])
```
U4 cells:
```{r}
t <- t + n
table(mc_cell[(t+1):(t+n)])
```
#### Evaluating the performance
Matching the labels with the predictions:
```{r}
#cells <- as.data.frame(cbind(cells = seq(1, length(labels), 1), mc_cell, labels))
cells <- cbind(as.data.frame(labels), mc_cell)
head(cells)
```
```{r, message=FALSE}
(t <- table(cells$mc_cell, cells$labels))
```
Finding the maximum number of each cell type (columns) on each cluster (rows):
```{r}
(m <- apply(t, 2, which.max))
```
Replacing the numbers of the clusters by the names of the cell types:
```{r}
# on the cell metaclustering
for(i in 1:length(mc_cell)){
for(j in 1:c){
if(cells$mc_cell[i] == m[[j]]){
cells$mc_cell[i] <- levels(labels)[j]
}
}
}
table(cells$mc_cell, cells$labels)
```
This is the confusion matrix.
Replacing the numbers of the clusters by the names of the cell types:
```{r}
table(mc)
```
```{r}
# on the node metaclustering
for(i in 1:length(mc)){
for(j in 1:c){
if(mc[i] == m[[j]]){
mc[i] <- levels(labels)[j]
}
}
}
table(mc)
```
Computing the confusion matrix and other performance measurements:
```{r}
library(caret)
cells$mc_cell <- factor(cells$mc_cell, levels = levels(labels))
(cm <- confusionMatrix(data = cells$mc_cell, reference = labels))
```
```{r}
cm$byClass
```
Great!
We can visualize the original cell types on the MST:
Original cell types are represented as plot pies.
We can add the result of the metaclustering on the background.
```{r}
PlotPies(fs, cellTypes = labels, backgroundValues = as.factor(mc))
```
Or on the star plots:
```{r}
PlotStars(fs, backgroundValues = as.factor(mc))
```
Or on the heat maps:
```{r}
for(i in 1:length(colnames(fs$map$medianValues))){
PlotMarker(fs, colnames(fs$map$medianValues)[[i]], backgroundValues = as.factor(mc))
}
```
```{r}
for(i in 1:length(colnames(fs$map$medianValues))){
PlotMarker(fs, colnames(fs$map$medianValues)[[i]], view = "tSNE", backgroundValues = as.factor(mc))
}
```
So many colors...
## Detecting nodes with a specific pattern
I think this functionality will not be very useful.
```{r}
# Import the patterns
patterns <- readRDS(params$pheno)
patterns
```
```{r}
# All nodes as "unknown"
cellTypes_01 <- factor(rep("Unknown",100),levels=c("Unknown", rownames(patterns)))
for(i in rownames(patterns)){
query <- QueryStarPlot(UpdateNodeSize(fs, reset=T), patterns[i,], plot = F)
cellTypes_01[query$selected] <- i
}
PlotStars(fs, backgroundValues=cellTypes_01)
```
```{r}
PlotStars(fs,view="tSNE", backgroundValues = cellTypes_01)
```