forked from broadinstitute/cytominer_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollapse.R
executable file
·79 lines (56 loc) · 1.9 KB
/
collapse.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
#!/usr/bin/env Rscript
'collapse
Usage:
collapse.R -b <id> -m <id> -o <file> [-f <str>]
Options:
-h --help Show this screen.
-b <id> --batch_id=<id> Batch ID.
-m <id> --plate_map_name=<id> Plate map name.
-o <file> --output=<file> Output CSV file.
-f <str> --suffix=<str> Suffix to append to barcode to select a profile file [default: _normalized_variable_selected.csv]' -> doc
suppressWarnings(suppressMessages(library(docopt)))
suppressWarnings(suppressMessages(library(dplyr)))
suppressWarnings(suppressMessages(library(magrittr)))
opts <- docopt(doc)
batch_id <- opts[["batch_id"]]
output <- opts[["output"]]
plate_map_name <- opts[["plate_map_name"]]
suffix <- opts[["suffix"]]
backend_dir <- paste("../..", "backend", batch_id, sep = "/")
metadata_dir <- paste("../..", "metadata", batch_id, sep = "/")
barcode_platemap <- suppressMessages(readr::read_csv(paste0(metadata_dir, "/barcode_platemap.csv")))
filelist <- barcode_platemap %>%
filter(Plate_Map_Name == plate_map_name) %>%
mutate(filename = normalizePath(
paste0(
backend_dir,
"/",
Assay_Plate_Barcode,
"/",
Assay_Plate_Barcode,
suffix
)
))
profiles <- lapply(filelist$filename,
function(filename) {
if (file.exists(filename)) {
suppressMessages(readr::read_csv(filename))
} else {
tibble::data_frame()
}
}) %>% bind_rows()
variables <-
colnames(profiles) %>%
stringr::str_subset("^Nuclei_|^Cells_|^Cytoplasm_")
metadata <-
stringr::str_subset(names(profiles), "^Metadata_")
degroup <- c("Metadata_Plate", "Metadata_Assay_Plate_Barcode",
"Metadata_Batch_Date", "Metadata_Batch_Number")
aggregated <-
cytominer::aggregate(
population = profiles,
variables = variables,
strata = setdiff(metadata, degroup),
operation = "mean"
)
aggregated %>% readr::write_csv(output)