-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataHandling.R
184 lines (155 loc) · 7.1 KB
/
DataHandling.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
##### Data ComputationalCytometry ##############################################
dir.create("Background_material/Subsetted/")
dir.create("Background_material/Preprocessed//")
dir.create("Background_material/Batched/")
dir.create("Background_material/Final/")
#### List raw files ####
filesKO <- list.files(path = "Background_material/Raw",
pattern = ".fcs",
full.names = TRUE)[1:3]
filesWT <- list.files(path = "Background_material/Raw",
pattern = ".fcs",
full.names = TRUE)[4:7]
#### Subset data and give more informative names ####
subset_i <- matrix(NA,
nrow = 300000, ncol = 10,
dimnames = list(NULL,
c(basename(filesWT),
basename(filesKO),
basename(filesKO[1]),
rep(basename(filesWT[1]), 2))))
day <- rep(rep(c(1,2), each = 2),2)
tube <- 1
set.seed(1)
for (file in filesWT){
ff <- flowCore::read.FCS(file)
i <- sort(sample(1:nrow(ff), 300000))
flowCore::write.FCS(ff[i,], paste0("Background_material/Subsetted/Tube0", tube, "_WT_", "Day", day[tube], ".fcs"))
subset_i[,tube] <- i
tube <- tube + 1
}
for (file in filesKO){
ff <- flowCore::read.FCS(file)
i <- sort(sample(1:nrow(ff), 300000))
flowCore::write.FCS(ff[i,], paste0("Background_material/Subsetted/Tube0", tube, "_KO_", "Day", day[tube], ".fcs"))
subset_i[,tube] <- i
tube <- tube + 1
}
# Sample other file out of first file (to have 2 KO samples in batch 2)
ff <- flowCore::read.FCS(filesKO[1])
i <- sort(sample(1:nrow(ff), 300000))
flowCore::write.FCS(ff[i,], paste0("Background_material/Subsetted/Tube0", tube, "_KO_", "Day", day[tube], ".fcs"))
subset_i[,tube] <- i
tube <- tube + 1
# Sample 2 other file out of first WT file (to have control in each batch)
ff <- flowCore::read.FCS(filesWT[1])
for (ctrl in 1:2){
i <- sort(sample(1:nrow(ff), 300000))
flowCore::write.FCS(ff[i,], paste0("Background_material/Subsetted/Control_Day", ctrl, ".fcs"))
subset_i[,tube] <- i
tube <- tube + 1
}
saveRDS(subset_i, "Background_material/Subsetted/subsetIndices.rds")
#### Get manual labels ####
gating <- FlowSOM::GetFlowJoLabels(files = basename(c(filesWT, filesKO)),
wspFile = "Background_material/Raw/General_panel.wsp")
files <- list.files(path = "Background_material/Subsetted",
pattern = ".fcs")[c(3:10, 1:2)]
manual_labeling <- list()
for (col in 1:ncol(subset_i)){
manual_labeling[[files[col]]] <- gating[[colnames(subset_i)[col]]][["manual"]][subset_i[,col]]
}
saveRDS(manual_labeling, "Background_material/Subsetted/ManualLabeling.rds")
#### Preprocess files ####
files <- list.files(path = "Background_material/Subsetted",
pattern = ".fcs",
full.names = TRUE)
p5 <- NULL
for (file in files){
ff <- flowCore::read.FCS(file)
comp <- ff@description$SPILL
ff_c <- flowCore::compensate(ff, comp)
cols_to_transform <- colnames(comp)
t <- flowCore::arcsinhTransform(transformationId="defaultArcsinhTransform", a=0, b=1/150)
t_list <- flowCore::transformList(cols_to_transform, t)
ff_t <- flowCore::transform(ff_c, t_list)
SSCA <- ff_t@exprs[,"SSC-A"]
if (is.null(p5)){ # Calculate percentiles on first file and apply to all
p5 <- quantile(SSCA, 0.05)
p95 <- quantile(SSCA, 0.95)
}
SSCA <- 10 * (SSCA - p5) / (p95 - p5)
ff_t@exprs[,"SSC-A"] <- SSCA
flowCore::write.FCS(ff_t, sub("Subsetted", "Preprocessed", file))
}
#### Introduce batch effects ####
files <- list.files(path = "Background_material/Preprocessed",
pattern = ".fcs",
full.names = TRUE)
files_batch <- list.files(path = "Background_material/Preprocessed",
pattern = "2.fcs",
full.names = TRUE)
channels_of_interest <- c("PE-Cy7-A", "PE-Cy5-A", "PE-A", "Alexa Fluor 700-A",
"APC-A", "SSC-A", "PerCP-Cy5-5-A", "Pacific Blue-A",
"BV605-A","BV711-A", "BV786-A")
n <- length(channels_of_interest)
transformation <- matrix(c(0.5, -0.5, 0.3, 1, 0.9, 1.1),
nrow = 3,
ncol = 2,
dimnames = list(c("PE-A", "PerCP-Cy5-5-A", "APC-A"), # Other transformation values for each channel
c("shift", "scale"))) # 2 transformations
for (file in files_batch){
ff <- flowCore::read.FCS(file)
for (channel in channels_of_interest){
if (channel == "PE-A"){ # Only affect positive population for CD3
gate <- flowDensity::deGate(ff, channel)
pos <- ff@exprs[,channel] > gate
ff@exprs[pos,channel] <- ff@exprs[pos,channel] + transformation[channel, "shift"]
} else if (channel == "PerCP-Cy5-5-A"){ #Only affect MHCII expression of DCs
DC <- manual_labeling[[basename(file)]] == "DCs"
ff@exprs[DC,channel] <- (ff@exprs[DC,channel] + transformation[channel, "shift"])
ff@exprs[DC,channel] <- scales::rescale(ff@exprs[DC,channel],
to=c(min(ff@exprs[DC,channel]),
max(ff@exprs[DC,channel]*transformation[channel, "scale"])))
} else if (channel == "APC-A"){# Affect whole population for CD161
ff@exprs[,channel] <- ff@exprs[,channel] + transformation[channel, "shift"]
ff@exprs[,channel] <- scales::rescale(ff@exprs[,channel],
to=c(min(ff@exprs[,channel]),
max(ff@exprs[,channel])*transformation[channel, "scale"]))
}
}
flowCore::write.FCS(ff, sub("Preprocessed", "Batched", file))
}
#### Backtransform and save files ####
files_batched <- list.files(path = "Background_material/Batched",
pattern = "2.fcs",
full.names = TRUE)
files_or <- list.files(path = "Background_material/Subsetted",
pattern = "1.fcs",
full.names = TRUE)
for (file in files_batched){
ff <- flowCore::read.FCS(file)
SSCA <- ff@exprs[,"SSC-A"]
SSCA <- SSCA * (p95 - p5) / 10 + p5
ff@exprs[,"SSC-A"] <- SSCA
ff@exprs[,cols_to_transform] <- apply(X = ff@exprs[,cols_to_transform],
MARGIN = 2,
FUN = function(x) sinh(x)*150)
ff_or <- flowCore::read.FCS(sub("Batched", "Subsetted", file))
ff_or@exprs <- ff@exprs
ff_or <- flowCore::decompensate(ff_or, ff_or@description$SPILL)
flowCore::write.FCS(ff_or, sub("Batched", "Final", file))
}
for (file in files_or){
ff <- flowCore::read.FCS(file)
flowCore::write.FCS(ff, sub("Subsetted", "Final", file))
}
##### Data ComputationalCytometry Beginner #####################################
##### Data AdditionalExercise ##################################################
files <- list.files(path = "Data/AdditionalExercise",
pattern = ".fcs",
full.names = TRUE)
for (file in files){
ff <- read.FCS(file)
write.FCS(ff, file)
}