-
Notifications
You must be signed in to change notification settings - Fork 13
/
run_immunoClust.R
executable file
·233 lines (172 loc) · 6.9 KB
/
run_immunoClust.R
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
#########################################################################################
# R script to run immunoClust
#
# Lukas Weber, August 2016
#########################################################################################
# note installation from Bioconductor requires GNU Scientific Library
library(flowCore)
library(immunoClust)
#################
### LOAD DATA ###
#################
# use non-transformed data files, since immunoClust will transform automatically
# filenames: non-transformed (note: not available for FlowCAP data sets)
DATA_DIR <- "../../../benchmark_data_sets"
files <- list(
Levine_32dim = file.path(DATA_DIR, "Levine_32dim/data/Levine_32dim_notransform.fcs"),
Levine_13dim = file.path(DATA_DIR, "Levine_13dim/data/Levine_13dim_notransform.fcs"),
Samusik_01 = file.path(DATA_DIR, "Samusik/data/Samusik_01_notransform.fcs"),
Samusik_all = file.path(DATA_DIR, "Samusik/data/Samusik_all_notransform.fcs"),
Nilsson_rare = file.path(DATA_DIR, "Nilsson_rare/data/Nilsson_rare_notransform.fcs"),
Mosmann_rare = file.path(DATA_DIR, "Mosmann_rare/data/Mosmann_rare_notransform.fcs"),
FlowCAP_ND = file.path(DATA_DIR, "FlowCAP_ND/data/FlowCAP_ND.fcs"),
FlowCAP_WNV = file.path(DATA_DIR, "FlowCAP_WNV/data/FlowCAP_WNV.fcs")
)
# FlowCAP data sets are treated separately since they require clustering algorithms to be
# run individually for each sample
is_FlowCAP <- c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE)
# load data files: immunoClust requires flowFrame objects
data <- vector("list", length(files))
names(data) <- names(files)
for (i in 1:length(data)) {
f <- files[[i]]
if (!is_FlowCAP[i]) {
data[[i]] <- flowCore::read.FCS(f, transformation = FALSE, truncate_max_range = FALSE)
} else {
smp <- flowCore::exprs(flowCore::read.FCS(f, transformation = FALSE, truncate_max_range = FALSE))
smp <- smp[, "sample"]
d <- flowCore::read.FCS(f, transformation = FALSE, truncate_max_range = FALSE)
data[[i]] <- flowCore::split(d, smp)
}
}
head(data[[1]])
head(data[[8]][[1]])
sapply(data, length)
sapply(data[!is_FlowCAP], dim)
sapply(data[is_FlowCAP], function(d) {
sapply(d, function(d2) {
dim(d2)
})
})
# subsampling for data sets with excessive runtime (> 12 hrs on server)
ix_subsample <- c(1, 4)
n_sub <- 100000
for (i in ix_subsample) {
if (!is_FlowCAP[i]) {
set.seed(123)
data[[i]] <- data[[i]][sample(1:nrow(data[[i]]), n_sub), ]
# save subsampled population IDs
true_labels_i <- flowCore::exprs(data[[i]])[, "label", drop = FALSE]
files_true_labels_i <- paste0("../../results/auto/immunoClust/true_labels_immunoClust_",
names(data)[i], ".txt")
write.table(true_labels_i, file = f, row.names = FALSE, quote = FALSE, sep = "\t")
}
}
# indices of protein marker columns
marker_cols <- list(
Levine_32dim = 5:36,
Levine_13dim = 1:13,
Samusik_01 = 9:47,
Samusik_all = 9:47,
Nilsson_rare = c(5:7, 9:18),
Mosmann_rare = c(7:9, 11:21),
FlowCAP_ND = 3:12,
FlowCAP_WNV = 3:8
)
sapply(marker_cols, length)
# column names (parameters)
pars <- vector("list", length(data))
for (i in 1:length(data)) {
if (!is_FlowCAP[i]) {
pars[[i]] <- colnames(data[[i]])[marker_cols[[i]]]
} else {
pars[[i]] <- colnames(data[[i]][[1]])[marker_cols[[i]]]
}
}
pars
#####################################################
### Run immunoClust: automatic number of clusters ###
#####################################################
# run immunoClust with automatic selection of number of clusters
# (note: "classify.all = TRUE" classifies all cells including outliers; and decreasing
# the bias argument increases the number of clusters)
seed <- 123
out <- runtimes <- vector("list", length(data))
names(out) <- names(runtimes) <- names(data)
for (i in 1:length(data)) {
if (!is_FlowCAP[i]) {
set.seed(seed)
runtimes[[i]] <- system.time({
out[[i]] <- immunoClust::cell.process(data[[i]],
parameters = pars[[i]],
classify.all = TRUE)
})
cat("data set", names(data[i]), ": run complete\n")
} else {
# FlowCAP data sets: run clustering algorithm separately for each sample
out[[i]] <- runtimes[[i]] <- vector("list", length(data[[i]]))
names(out[[i]]) <- names(runtimes[[i]]) <- names(data[[i]])
for (j in 1:length(data[[i]])) {
set.seed(seed)
runtimes[[i]][[j]] <- system.time({
out[[i]][[j]] <- immunoClust::cell.process(data[[i]][[j]],
parameters = pars[[i]],
classify.all = TRUE)
})
}
cat("data set", names(data[i]), ": run complete\n")
# FlowCAP data sets: sum runtimes over samples
runtimes_i <- do.call(rbind, runtimes[[i]])[, 1:3]
runtimes_i <- colSums(runtimes_i)
names(runtimes_i) <- c("user", "system", "elapsed")
runtimes[[i]] <- runtimes_i
}
}
# number of clusters
summary(out[[1]])
# extract cluster labels
clus <- vector("list", length(data))
names(clus) <- names(data)
for (i in 1:length(clus)) {
if (!is_FlowCAP[i]) {
clus[[i]] <- out[[i]]@label
} else {
# FlowCAP data sets
clus_list_i <- vector("list", length(data[[i]]))
names(clus_list_i) <- names(data[[i]])
for (j in 1:length(data[[i]])) {
clus_list_i[[j]] <- out[[i]][[j]]@label
}
# convert FlowCAP cluster labels into format "sample_number"_"cluster_number"
# e.g. sample 1, cluster 3 -> cluster label 1_3
names_i <- rep(names(clus_list_i), times = sapply(clus_list_i, length))
clus_collapse_i <- unlist(clus_list_i, use.names = FALSE)
clus[[i]] <- paste(names_i, clus_collapse_i, sep = "_")
}
}
sapply(clus, length)
# cluster sizes and number of clusters
# (for FlowCAP data sets, total no. of clusters = no. samples * no. clusters per sample)
table(clus[[1]])
sapply(clus, function(cl) length(table(cl)))
# plots
#png("../../results/auto/immunoClust/plot_immunoClust_Levine_32dim.png", width = 1000, height = 1000)
#immunoClust::splom(out[[1]], immunoClust::trans.ApplyToData(out[[1]], data[[1]]), N = 1000)
#dev.off()
# save cluster labels
files_labels <- paste0("../../results/auto/immunoClust/immunoClust_labels_",
names(clus), ".txt")
for (i in 1:length(files_labels)) {
res_i <- data.frame(label = clus[[i]])
write.table(res_i, file = files_labels[i], row.names = FALSE, quote = FALSE, sep = "\t")
}
# save runtimes
runtimes <- lapply(runtimes, function(r) r["elapsed"])
runtimes <- t(as.data.frame(runtimes, row.names = "runtime"))
write.table(runtimes, file = "../../results/auto/runtimes/runtime_immunoClust.txt",
quote = FALSE, sep = "\t")
# save session information
sink(file = "../../results/auto/session_info/session_info_immunoClust.txt")
print(sessionInfo())
sink()
cat("immunoClust automatic : all runs complete\n")