-
Notifications
You must be signed in to change notification settings - Fork 0
/
06a-timing-R.Rmd
140 lines (118 loc) · 3.94 KB
/
06a-timing-R.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
---
title: "Pre-processing of cMRD data"
author:
- name: T.R. Mocking
output:
BiocStyle::html_document
vignette: |
%\VignetteIndexEntry{Vignette Title}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r}
library(flowCore)
library(PeacoQC)
# CytoTools can be obtained from Github with devtools
# devtools::install_github("AUMC-HEMA/CytoTools")
library(CytoTools)
```
```{r}
# Input
BLAST110_path <- "<ENTER PATH>"
LAIP29_path <- "<ENTER PATH>"
RBM18_path <- "<ENTER PATH>"
# Obtain a list of all the files
BLAST110_info <- read.csv(paste0(BLAST110_path, "sample_info.csv"))
LAIP29_info <- read.csv(paste0(LAIP29_path, "sample_info.csv"))
RBM18_info <- read.csv(paste0(RBM18_path, "sample_info.csv"))
# Output
transform_path <- "data/transform"
BLAST110_export_path <- "data/BLAST110"
LAIP29_export_path <- "data/LAIP29"
RBM18_export_path <- "data/RBM18"
```
# Determine optimal logicle transform
Optimal linearization widths are based on NBM samples
```{r}
if (!dir.exists(transform_path)){
dir.create(transform_path, recursive = TRUE)
}
for (tube in unique(BLAST110_info$tube)){
transform_file <- paste0(transform_path, "/", tube, "_logicleTransform.rds")
if (file.exists(transform_file)) {
next
}
paths <- BLAST110_info[(BLAST110_info$sample_type == "NBM") & (BLAST110_info$tube == tube),]$BLAST110_ID
paths <- paste0(BLAST110_path, "FCS/", paths, ".fcs")
tfList <- CytoTools::getLogicle(paths)
saveRDS(tfList, transform_file)
}
```
# Pre-process
```{r}
preprocessFCS <- function(path, tube, FCS_export_path){
results <- list()
results["file"] <- FCS_export_path
start_time <- Sys.time()
ff <- read.FCS(path, truncate_max_range = FALSE)
end_time <- Sys.time()
results["read.FCS"] <- end_time - start_time
# Remove margin events
cols <- colnames(ff)
start_time <- Sys.time()
ff <- PeacoQC::RemoveMargins(ff, c("FSC-A", "FSC-H", "SSC-A", "SSC-H"))
end_time <- Sys.time()
results["RemoveMargins"] <- end_time - start_time
# Remove the "Original ID" column added by PeacoQC
ff <- ff[, cols]
# Remove doublets
start_time <- Sys.time()
doublets <- CytoTools::gateDoublets(ff)
singlet_vector <- doublets[, "PeacoQC_doublet"] == 0 & doublets[, "flowStats_doublet"] == 0
ff@exprs <- ff@exprs[singlet_vector, ]
end_time <- Sys.time()
results["gateDoublets"] <- end_time - start_time
# Compensate
start_time <- Sys.time()
ff <- compensate(ff, ff@description$SPILL)
# Transform based on pre-calculated transformations
tfList <- readRDS(paste0(transform_path, "/", tube, "_logicleTransform.rds"))
ff <- transform(ff, tfList)
# Add MinMax scaled variants of scatters + markers
cols <- c(c('FSC-A', 'FSC-H', 'SSC-A', 'SSC-H'), colnames(ff@description$SPILL))
scaled_exprs <-apply(flowCore::exprs(ff)[,cols], 2, function(x){
return((x - quantile(x, 0.01)) / (quantile(x, 0.99) - quantile(x, 0.01)))
})
end_time <- Sys.time()
results["Comp-Trans-Scale"] <- end_time - start_time
# Add "_scaled" suffix to each column
colnames(scaled_exprs) <- paste0(colnames(scaled_exprs), "_scaled")
# Add scaled parameters to the FCS file
for (scaled_channel in colnames(scaled_exprs)){
events <- as.matrix(scaled_exprs[,scaled_channel])
colnames(events) <- scaled_channel
ff <- fr_append_cols(ff, events)
}
return(results)
}
```
## LAIP29
```{r}
if (!dir.exists(paste0(LAIP29_export_path, "/FCS/"))){
dir.create(paste0(LAIP29_export_path, "/FCS/"), recursive = TRUE)
}
all_results <- list()
for (i in 1:nrow(LAIP29_info)) {
print(i)
sample_name <- LAIP29_info[i, 1]
path <- paste0(LAIP29_path, "FCS/", sample_name, ".fcs")
tube <- LAIP29_info[i, "tube"]
FCS_export_path <- paste0(LAIP29_export_path, "/FCS/", sample_name, "_preprocessed.fcs")
all_results[[FCS_export_path]] <- preprocessFCS(path, tube, FCS_export_path)
}
all_results <- dplyr::bind_rows(all_results)
write.csv(all_results, paste0("output/BLAST110/R_timing.csv"))
```
```{r sessionInfo, echo=FALSE}
sessionInfo()
```