-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcytof_utils.R
116 lines (74 loc) · 2.96 KB
/
cytof_utils.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
load_libraries <- function(force_install=FALSE) {
if (force_install || !require("BiocManager", quietly = TRUE))
install.packages("BiocManager", force=force_install)
suppressMessages(suppressWarnings(library(BiocManager, quietly = TRUE)))
if (force_install || !require("devtools", quietly = TRUE))
install.packages("devtools", force=force_install)
library(devtools, quietly = TRUE)
if (force_install || !require("CATALYST", quietly = TRUE))
install_github("neuro-sysmed/CATALYST", force=T)
suppressPackageStartupMessages(library(CATALYST, quietly = TRUE))
if (force_install || !require("flowCore", quietly = TRUE))
BiocManager::install("flowCore", force=force_install)
suppressMessages(suppressWarnings(library(flowCore, quietly = TRUE)))
if (force_install || !require("stringi", quietly = TRUE))
install.packages("stringi", force=force_install)
library(stringi, quietly = TRUE)
if (force_install || !require("readr", quietly = TRUE))
install.packages("readr", force=force_install)
library(readr, quietly = TRUE)
}
mk_outdirs <- function(output_dir ) {
cat(fcs_dir, log_dir, "\n")
return(list(fcs_dir, log_dir))
}
read_fcs_and_est_cutoffs <- function( directory ) {
sce <- prepData( directory )
sce <- assignPrelim(sce, sample_key)
# cat(table(sce$bc_id))
cat('ExtCutoffs...\n')
sce <- estCutoffs(sce)
cutoffs <- metadata(sce)$sep_cutoffs
# table(sce$bc_id)
# should be stored for later as well!
cutoffs <- metadata(sce)$sep_cutoffs
write.table(table(sce$bc_id), paste(log_dir, "barcode_counts.csv", sep="/"), sep="\t", col.names = NA, row.names = TRUE)
write.table(cutoffs, paste(log_dir, "barcode_cutoffs.csv", sep="/"), sep="\t", col.names = NA, row.names = TRUE)
plotYields(sce, which = c(0),
out_path = directory,
out_name = "barcode_separation")
return(sce)
}
save_debarcoded_fcs_files <- function( sce, file_fstring ) {
(fs <- sce2fcs(sce,split_by = "bc_id"))
all(c(fsApply(fs, nrow)) == table(sce$bc_id))
ids <- fsApply(fs, identifier)
for (id in ids) {
cat(sprintf("Writing %s barcode to %s file\n", id, sprintf(file_fstring, id)))
ff <- fs[[id]]
fn <- sprintf(file_fstring, id)
write.FCS(ff, fn)
}
}
plot_dna_scatter <- function( sce, outfile) {
chs <- c("193Ir", "191Ir")
jpeg(outfile)
plotScatter(sce, chs)
dev.off()
}
stop_quietly <- function() {
opt <- options(show.error.messages = FALSE)
on.exit(options(opt))
stop()
}
subsample_fcs <- function(fsc_file, max_events=5500, random_seed=10) {
sce <- flowCore::read.FCS(fsc_file)
set.seed(random_seed)
# If there is less than max_events, select all
picked = sample(seq_len(nrow(sce)),size = min(nrow(sce), max_events))
sce_picked <- sce[ picked,]
return(sce_picked)
}
write_fcs_file <- function(sce, filename) {
write.FCS(sce, filename)
}