-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFLOW_PROCESSING_1.R
334 lines (287 loc) · 15.4 KB
/
FLOW_PROCESSING_1.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
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
Packages <- c("flowCore","igraph","robustbase","SDMTools","vite","rhandsontable",
"ggcyto","flowStats","gridExtra","reshape2","ggridges","openCyto",
"flowViz","patchwork","limma","gplots","plot3D","Biobase",
"FlowSOM","ConsensusClusterPlus","Rtsne","umap","viridis",
"flowWorkspace", "openCyto","FLOWMAPR") #CytoExploreR
lapply(Packages, library, character.only = TRUE)
Packages <- c("shiny","shinythemes","shinyFiles",
"shinydashboard","shinyalert")
lapply(Packages, library, character.only = TRUE)
library("CATALYST")
dir <- "/proj/lxx_storage/TISSUE_OMICS/GEO/Bone_Marrow/P146_SDY1389_Thymus_Flow/"
# dir <- "~/Desktop/Desktop/CyTOF_ALL/P146_SDY1389_Thymus_Flow/"
# output_dir <- "/Users/luadmpan/Dropbox/KI/Studies/LXXLZ/Website/CyTOF/CyTOF_OUTPUT/P146_SDY1389_Thymus_Flow/"
output_dir <- "/proj/lxx_storage/TISSUE_OMICS/GEO/OUTPUT/FLOW/OUTPUT/P146_SDY1389_Thymus_Flow/"
dir.create(output_dir, recursive = T)
# setwd("~/Desktop/Desktop/CyTOF_ALL/P146_SDY1389_Thymus_Flow/")
project_name <- gsub(".*\\/(.*)\\/","\\1",output_dir)
system(paste("mkdir ",output_dir, sep = ""))
x <- read.FCS()
x@parameters@data
x@parameters@data$desc[grep("Beads",x@parameters@data$desc, ignore.case = T)]
############## Convert FCS to csv back to FCS again
x <- read.FCS(files[1], transformation=FALSE)
current_markers <- x@parameters@data$desc
current_markers[which(is.na(current_markers))] <- x@parameters@data$name[which(is.na(current_markers))]
colnames(x@exprs) <- current_markers
x <- x@exprs
# files <- list.files(dir, pattern = "fcs", ignore.case = T, full.names = T)
# data <- lapply(files, function(x){
# x <- read.FCS(x, transformation=FALSE)
# })
# names(data) <- gsub(".*\\/(.*)","\\1",files)
# data <- as(data,"flowSet")
# sce <- prepData(data)
# table(sce$sample_id)
# names(int_colData(sce))
# # construct SCE
# sce <- prepData(data) # raw_sce <- prepData(raw_data)
# # apply normalization; keep raw data
# res <- normCytof(sce, beads = "dvs", k = 50,
# assays = c("counts", "exprs"), overwrite = FALSE)
# # check number & percentage of bead / removed events
# n <- ncol(sce); ns <- c(ncol(res$beads), ncol(res$removed))
# data.frame(
# check.names = FALSE,
# "#" = c(ns[1], ns[2]),
# "%" = 100*c(ns[1]/n, ns[2]/n),
# row.names = c("beads", "removed"))
#
# save.image("sce_norm_20210621.RData")
#### CSV to FCS ###########
library("flowCore")
library("Biobase")
library("flowWorkspace")
library("ggcyto")
library("openCyto")
library("flowStats")
files <- list.files(pattern=".csv")
tissue <- NULL
subtissue <- NULL
data <- NULL
for(i in 1:length(files)){
x <- read.csv(files[i], header = T)
tissue <- c(tissue,unique(x$TISSUE))
subtissue <- c(subtissue,unique(x$SUBTISSUE))
colnames(x) <- gsub("\\.|-","_",colnames(x))
x <- x[,grep("TISSUE", colnames(x), ignore.case = T, invert = T)] # bc_separation_dist|mahalanobis_dist|
meta <- data.frame(name=colnames(x),desc=colnames(x))
meta$range <- apply(apply(x,2,range),2,diff)
meta$minRange <- apply(x,2,min)
meta$maxRange <- apply(x,2,max)
x <- new("flowFrame",exprs=as.matrix(x),parameters=AnnotatedDataFrame(meta))
x@parameters@data
data[[i]] <- x
}
fs_data = as(data,"flowSet")
sample_names <- gsub("(.*)\\.(csv|fcs)","\\1",files, ignore.case = T)
fs_data@phenoData@data$name <- cols <- NULL
data_summary <- NULL
for(i in 1:length(files)){
x <- read.FCS(files[i])
x@parameters@data
cols[[i]] <- x@parameters@data
# fs_data <- read.flowSet(path = dir, pattern = ".fcs", alter.names = TRUE, transformation = FALSE)
fs_data <- as(x, "flowSet")
fs_data[[1]]@parameters@data
fs_data@phenoData@data$name
sample_names <- gsub("^(.*)\\.fcs","\\1",files[i], ignore.case = T)
sample_names <- gsub("\\s+","_",sample_names)
fs_data@phenoData@data$name <- sample_names
fs_data@phenoData@data$name
gs_data <- NULL
for(i in 1:length(fs_data)){
gs_data[[i]] <- GatingSet(fs_data[i])
}
somePDFPath = paste(output_dir,"SCIENCE_FLOW_STEP1_FSC-A_VS_FSC-H_GATING_",sample_names[i],"_",project_name,".pdf", sep = "")
pdf(file=somePDFPath, width=8, height=6,pointsize=10)
for(i in 1:length(gs_data)){
current_ff <- gh_pop_get_data(gs_data[[i]])
chnl <- colnames(fs_data[[i]])[grep("FSC.*A|FSC.*H", colnames(fs_data[[i]]), ignore.case = T)]
mylimits <- ggcyto_par_set(limits = "instrument")
if(is.null(range_fsc_ssc)){
g <- openCyto:::.flowClust.2d(current_ff, channels = chnl, K=3, target=c(1e5,1e4), quantile=0.95, filterId = "SelectedCells")
}else{
g <- openCyto:::.flowClust.2d(current_ff, channels = chnl, K=3, target=c(as.numeric(as.character(range_fsc_ssc))), quantile=0.95, filterId = "SelectedCells")
}
# print(autoplot(current_ff, x = chnl[1], y = chnl[2], bin = 300) + mylimits + geom_gate(g) + geom_stats(adjust = 1))
gs_pop_add(gs_data[[i]], g)
recompute(gs_data[[i]])
print(ggcyto(gs_data[[1]],aes(x=chnl[1],y=chnl[2]),subset="SelectedCells")+geom_hex(bins = 200)+geom_gate(g)+ggcyto_par_set(limits = "instrument")) # check gate
}
dev.off()
somePDFPath = paste(output_dir,"SCIENCE_FLOW_STEP1_FSC-A_VS_SSC-A_GATING_",sample_names[i],"_",project_name,".pdf", sep = "")
pdf(file=somePDFPath, width=8, height=6,pointsize=10)
for(i in 1:length(gs_data)){
local_min <- density(fs_data[[i]]@exprs[,"FSC-A"])$x[which(diff(sign(diff(density(fs_data[[i]]@exprs[,"FSC-A"])$y)))==+2)+1]
current_ff <- gh_pop_get_data(gs_data[[i]], "defaultEllipsoidGate")
chnl <- colnames(fs_data[[i]])[grep("FSC.*A", colnames(fs_data[[i]]), ignore.case = T)]
# g <- polygonGate(filterId = "Singlets","FSC-A"=c(5e4,24e4,24e4,8e4),"SSC-A"=c(0,2e4,15e4,5e4)) # define gate
# g <- gate_singlet(current_ff, area = chnl[1], height = chnl[2], filterId = "Singlets")
g <- openCyto:::.mindensity(current_ff, channels = chnl, filterId = "Singlets", gate_range=c(local_min[1]-500,local_min[2]-1000))
mylimits <- ggcyto_par_set(limits = "instrument")
gs_pop_add(gs_data[[i]], g, parent = "defaultEllipsoidGate")
recompute(gs_data[[i]])
print(ggcyto(gs_data[[1]],aes(x="FSC-A",y="SSC-A"),subset="Singlets")+geom_hex(bins = 200)+geom_gate(g)+ggcyto_par_set(limits = "instrument")) # check gate
# print(autoplot(current_ff, chnl, strip.text = "gate") + mylimits + geom_density(fill="forestgreen") + geom_gate(g))
# print(autoplot(fs_data[[i]], chnl, colnames(fs_data[[i]])[grep("SSC.*A", colnames(fs_data[[i]]), ignore.case = T)], bin = 200, strip.text = "gate") + geom_gate(g) + geom_stats(adjust = 0.5))
}
dev.off()
dir.create(paste(output_dir, "Filtered_FCS_Input/", sep = ""), recursive = T)
filtered_data <- NULL
for(i in 1:length(gs_data)){
ps <- data.frame(gs_pop_get_count_fast(gs_data[[i]]))
ps$Percent_of_parent <- ps$Count/ps$ParentCount
data_summary <- rbind(data_summary, ps)
filtered_data[[i]] <- gh_pop_get_data(gs_data[[i]], y = "Singlets")
write.FCS(filtered_data[[i]], paste(output_dir, "Filtered_FCS_Input/Filtered_",sample_names[i],".fcs", sep = ""))
}
print(paste("Done: ", files[i]))
}
cols <- NULL
data_summary <- NULL
for(i in 1:length(files)){
x <- read.FCS(files[i])
x@parameters@data
cols[[i]] <- x@parameters@data
# fs_data <- read.flowSet(path = dir, pattern = ".fcs", alter.names = TRUE, transformation = FALSE)
fs_data <- as(x, "flowSet")
fs_data[[1]]@parameters@data
fs_data@phenoData@data$name
sample_names <- gsub("^(.*)\\.fcs","\\1",files[i], ignore.case = T)
sample_names <- gsub("\\s+","_",sample_names)
fs_data@phenoData@data$name <- sample_names
fs_data@phenoData@data$name
gs_data <- NULL
for(i in 1:length(fs_data)){
gs_data[[i]] <- GatingSet(fs_data[i])
}
somePDFPath = paste(output_dir,"SCIENCE_FLOW_STEP1_FSC-A_VS_FSC-H_GATING_",sample_names[i],"_",project_name,".pdf", sep = "")
pdf(file=somePDFPath, width=8, height=6,pointsize=10)
for(i in 1:length(gs_data)){
current_ff <- gh_pop_get_data(gs_data[[i]])
chnl <- colnames(fs_data[[i]])[grep("FSC.*A|FSC.*H", colnames(fs_data[[i]]), ignore.case = T)]
mylimits <- ggcyto_par_set(limits = "instrument")
if(is.null(range_fsc_ssc)){
g <- openCyto:::.flowClust.2d(current_ff, channels = chnl, K=3, target=c(1e5,1e4), quantile=0.95, filterId = "SelectedCells")
}else{
g <- openCyto:::.flowClust.2d(current_ff, channels = chnl, K=3, target=c(as.numeric(as.character(range_fsc_ssc))), quantile=0.95, filterId = "SelectedCells")
}
# print(autoplot(current_ff, x = chnl[1], y = chnl[2], bin = 300) + mylimits + geom_gate(g) + geom_stats(adjust = 1))
gs_pop_add(gs_data[[i]], g)
recompute(gs_data[[i]])
print(ggcyto(gs_data[[1]],aes(x=chnl[1],y=chnl[2]),subset="SelectedCells")+geom_hex(bins = 200)+geom_gate(g)+ggcyto_par_set(limits = "instrument")) # check gate
}
dev.off()
somePDFPath = paste(output_dir,"SCIENCE_FLOW_STEP1_FSC-A_VS_SSC-A_GATING_",sample_names[i],"_",project_name,".pdf", sep = "")
pdf(file=somePDFPath, width=8, height=6,pointsize=10)
for(i in 1:length(gs_data)){
local_min <- density(fs_data[[i]]@exprs[,"FSC-A"])$x[which(diff(sign(diff(density(fs_data[[i]]@exprs[,"FSC-A"])$y)))==+2)+1]
current_ff <- gh_pop_get_data(gs_data[[i]], "defaultEllipsoidGate")
chnl <- colnames(fs_data[[i]])[grep("FSC.*A", colnames(fs_data[[i]]), ignore.case = T)]
# g <- polygonGate(filterId = "Singlets","FSC-A"=c(5e4,24e4,24e4,8e4),"SSC-A"=c(0,2e4,15e4,5e4)) # define gate
# g <- gate_singlet(current_ff, area = chnl[1], height = chnl[2], filterId = "Singlets")
g <- openCyto:::.mindensity(current_ff, channels = chnl, filterId = "Singlets", gate_range=c(local_min[1]-500,local_min[2]-1000))
mylimits <- ggcyto_par_set(limits = "instrument")
gs_pop_add(gs_data[[i]], g, parent = "defaultEllipsoidGate")
recompute(gs_data[[i]])
print(ggcyto(gs_data[[1]],aes(x="FSC-A",y="SSC-A"),subset="Singlets")+geom_hex(bins = 200)+geom_gate(g)+ggcyto_par_set(limits = "instrument")) # check gate
# print(autoplot(current_ff, chnl, strip.text = "gate") + mylimits + geom_density(fill="forestgreen") + geom_gate(g))
# print(autoplot(fs_data[[i]], chnl, colnames(fs_data[[i]])[grep("SSC.*A", colnames(fs_data[[i]]), ignore.case = T)], bin = 200, strip.text = "gate") + geom_gate(g) + geom_stats(adjust = 0.5))
}
dev.off()
dir.create(paste(output_dir, "Filtered_FCS_Input/", sep = ""), recursive = T)
filtered_data <- NULL
for(i in 1:length(gs_data)){
ps <- data.frame(gs_pop_get_count_fast(gs_data[[i]]))
ps$Percent_of_parent <- ps$Count/ps$ParentCount
data_summary <- rbind(data_summary, ps)
filtered_data[[i]] <- gh_pop_get_data(gs_data[[i]], y = "Singlets")
write.FCS(filtered_data[[i]], paste(output_dir, "Filtered_FCS_Input/Filtered_",sample_names[i],".fcs", sep = ""))
}
print(paste("Done: ", files[i]))
}
fs_data@phenoData@data$name
############################
files <- list.files(pattern=".fcs", ignore.case = T)
cols <- NULL
data_summary <- NULL
for(k in 1:length(files)){
x <- read.FCS(files[k])
x@parameters@data
current <- data.frame(x@exprs)
cols[[i]] <- x@parameters@data
# fs_data <- read.flowSet(path = dir, pattern = ".fcs", alter.names = TRUE, transformation = FALSE)
fs_data <- as(x, "flowSet")
fs_data[[1]]@parameters@data
fs_data@phenoData@data$name
sample_names <- gsub("^(.*)\\.fcs","\\1",files[k], ignore.case = T)
sample_names <- gsub("\\s+","_",sample_names)
fs_data@phenoData@data$name <- sample_names
fs_data@phenoData@data$name
gs_data <- NULL
for(i in 1:length(fs_data)){
gs_data[[i]] <- GatingSet(fs_data[i])
}
somePDFPath = paste(output_dir,"SCIENCE_FLOW_STEP1_FSC-A_VS_FSC-H_GATING_",sample_names[k],"_",project_name,".pdf", sep = "")
pdf(file=somePDFPath, width=8, height=6,pointsize=10)
for(i in 1:length(gs_data)){
current_ff <- gh_pop_get_data(gs_data[[i]])
chnl <- colnames(fs_data[[i]])[grep("FSC.*A|FSC.*H", colnames(fs_data[[i]]), ignore.case = T)]
mylimits <- ggcyto_par_set(limits = "instrument")
g <- openCyto:::.flowClust.2d(current_ff, channels = chnl, K=3, target=c(1e5,1e4), quantile=0.95, filterId = "SelectedCells")
# print(autoplot(current_ff, x = chnl[1], y = chnl[2], bin = 300) + mylimits + geom_gate(g) + geom_stats(adjust = 1))
gs_pop_add(gs_data[[i]], g)
recompute(gs_data[[i]])
print(ggcyto(gs_data[[i]],aes(x="FSC-A",y="FSC-H"),subset="root")+geom_hex(bins = 200)+geom_gate(g)+ggcyto_par_set(limits = "instrument")) # check gate
}
dev.off()
somePDFPath = paste(output_dir,"SCIENCE_FLOW_STEP1_FSC-A_VS_SSC-A_GATING_",sample_names[k],"_",project_name,".pdf", sep = "")
pdf(file=somePDFPath, width=8, height=6,pointsize=10)
for(i in 1:length(gs_data)){
local_min <- density(fs_data[[i]]@exprs[,"FSC-A"])$x[which(diff(sign(diff(density(fs_data[[i]]@exprs[,"FSC-A"])$y)))==+2)+1]
current_ff <- gh_pop_get_data(gs_data[[i]], "defaultEllipsoidGate")
chnl <- colnames(fs_data[[i]])[grep("FSC.*A", colnames(fs_data[[i]]), ignore.case = T)]
# g <- polygonGate(filterId = "Singlets","FSC-A"=c(5e4,24e4,24e4,8e4),"SSC-A"=c(0,2e4,15e4,5e4)) # define gate
# g <- gate_singlet(current_ff, area = chnl[1], height = chnl[2], filterId = "Singlets")
g <- openCyto:::.mindensity(current_ff, channels = chnl, filterId = "Singlets", gate_range=c(local_min[1]-500,local_min[2]-1000))
mylimits <- ggcyto_par_set(limits = "instrument")
gs_pop_add(gs_data[[i]], g, parent = "defaultEllipsoidGate")
recompute(gs_data[[i]])
print(ggcyto(gs_data[[i]],aes(x="FSC-A",y="SSC-A"),subset="Singlets")+geom_hex(bins = 200)+geom_gate(g)+ggcyto_par_set(limits = "instrument")) # check gate
# print(autoplot(current_ff, chnl, strip.text = "gate") + mylimits + geom_density(fill="forestgreen") + geom_gate(g))
# print(autoplot(fs_data[[i]], chnl, colnames(fs_data[[i]])[grep("SSC.*A", colnames(fs_data[[i]]), ignore.case = T)], bin = 200, strip.text = "gate") + geom_gate(g) + geom_stats(adjust = 0.5))
}
dev.off()
dir.create(paste(output_dir, "Filtered_FCS_Input/", sep = ""), recursive = T)
filtered_data <- NULL
for(i in 1:length(gs_data)){
ps <- data.frame(gs_pop_get_count_fast(gs_data[[i]]))
ps$Percent_of_parent <- ps$Count/ps$ParentCount
data_summary <- rbind(data_summary, ps)
filtered_data[[i]] <- gh_pop_get_data(gs_data[[i]], y = "Singlets")
write.FCS(filtered_data[[i]], paste(output_dir, "Filtered_FCS_Input/Filtered_",sample_names[k],".fcs", sep = ""))
}
print(paste("Done: ", files[i]))
}
write.csv(data_summary, paste(output_dir, "CyTOF_TABLE_GATING_SUMMARY_",project_name,".csv",sep = ""),row.names = F)
cofactor <- 5
filtered_data <- as(filtered_data,"flowSet")
sampleNames(filtered_data) <- sampleNames(fs_data)
pData(filtered_data)$name <- sampleNames(fs_data)
data <- NULL
for(i in 1:length(filtered_data)){
# data <- rbind(data,data.frame(SAMPLE_ID = pData(filtered_data)$name[i], asinh(exprs(filtered_data[[i]])/cofactor)))
data <- rbind(data,data.frame(SAMPLE_ID = pData(filtered_data)$name[i], exprs(filtered_data[[i]])))
}
fs_data[[1]]@parameters@data
colnames(data) <- c("SAMPLE_ID",fs_data[[1]]@parameters@data$desc)
data <- data[,grep("P74S|P74B|P76S|P76B|^[0-9]+[A-Z]+$|Beads|DNA|Viability|^NA$|^NA\\.[0-9]+$", colnames(data), ignore.case=T, invert=T)]
data <- data[,which(!is.na(colnames(data)))]
data <- data[,grep("P74S|P74B|P76S|P76B|^[0-9]+[A-Z]+$|Beads|DNA|Viability|^NA$|^NA\\.[0-9]+$", colnames(data), ignore.case=T, invert=T)]
colnames(data)
data$SAMPLE_ID <- gsub("^_(.*)\\.[0-9]+\\.fcs","\\1",data$SAMPLE_ID, ignore.case = T)
data$TISSUE <- tissue
data$SUBTISSUE <- subtissue
summary(data[,-1])
write.csv(data, paste(output_dir, "CyTOF_POST_GATING_",project_name,".csv",sep = ""),row.names = F)