-
Notifications
You must be signed in to change notification settings - Fork 1
/
preselect.R
executable file
·136 lines (95 loc) · 4.25 KB
/
preselect.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
#!/usr/bin/env Rscript
'preselect
Usage:
preselect.R -b <id> -i <file> -r <list> [-n <n>] [-s <query>]
options:
-h --help Show this screen.
-b <id> --batch_id=<id> Batch ID.
-i <file> --input=<file> Test data on which to perform variable selection operations.
-r <list> --operations=<list> Comma-separated list of operations.
-n <n> --replicates=<n> Number of replicates selected per plate map in <file>.
-s <query> --subset=<query> Query to create the training data by subsetting.' -> doc
suppressWarnings(suppressMessages(library(docopt)))
suppressWarnings(suppressMessages(library(dplyr)))
suppressWarnings(suppressMessages(library(magrittr)))
suppressWarnings(suppressMessages(library(foreach)))
suppressWarnings(suppressMessages(library(doParallel)))
doParallel::registerDoParallel(cores=detectCores())
opts <- docopt(doc)
batch_id <- opts[["batch_id"]]
input <- opts[["input"]]
operations <- opts[["operations"]]
replicates <- opts[["replicates"]]
subset <- opts[["subset"]] #"Metadata_broad_sample_type == '''control'''"
operations <- stringr::str_split(operations, ",")[[1]]
dir.create(paste0("../../parameters/", batch_id, "/variable_selection/"), recursive = TRUE, showWarnings = FALSE)
for (operation in operations) {
variable_selections_file <- paste0("../../parameters/", batch_id, "/variable_selection/", operation, ".txt" )
if (tools::file_ext(input) == "rds") {
df <- readRDS(input)
} else if (tools::file_ext(input) == "csv"){
df <- suppressMessages(readr::read_csv(input))
} else if (tools::file_ext(input) == "feather"){
df <- suppressMessages(feather::read_feather(input))
} else {
stop(paste0("Unsupported file extension: ", tools::file_ext(input)))
}
variables <-
colnames(df) %>%
stringr::str_subset("^Nuclei_|^Cells_|^Cytoplasm_")
futile.logger::flog.info("Dropping variables that have NA in any row...")
df %<>%
cytominer::variable_select(variables = variables,
operation = "drop_na_columns",
cutoff = 0.0)
variables <-
colnames(df) %>%
stringr::str_subset("^Nuclei_|^Cells_|^Cytoplasm_")
if(!is.null(subset)) {
futile.logger::flog.info(sprintf("Subsetting using %s", subset))
sample <- df %>% filter_(subset)
} else {
sample <- df
}
futile.logger::flog.info(sprintf("Performing %s...", operation))
if (operation %in% c("variance_threshold", "correlation_threshold")) {
df <-
cytominer::variable_select(
population = df,
variables = variables,
sample = sample,
operation = operation
) %>%
collect()
} else if (operation == "replicate_correlation") {
# This is handled differently because there is no direct way yet to do filtering in cytominer
# TODO: rewrite this after cytominer has an appropriate filtering function for this
testthat::expect_false(is.null(replicates), info="replicates should be specified when performing replicate_correlation")
feature_replicate_correlations <-
df %>%
cytominer::replicate_correlation(
sample = .,
variables = variables,
strata = c("Metadata_Plate_Map_Name", "Metadata_Well"),
split_by = "Metadata_Plate_Map_Name",
replicates = replicates
)
feature_replicate_correlations %>%
readr::write_csv(paste0("../../parameters/", batch_id, "/variable_selection/", operation, ".csv" ))
variables <-
feature_replicate_correlations %>%
na.omit() %>%
filter(median > 0.6) %>% # intentionally hard-coded to avoid confusion
magrittr::extract2("variable")
metadata <-
colnames(df) %>%
stringr::str_subset("^Metadata_")
df %<>%
select_(.dots = c(metadata, variables))
}
variables <-
colnames(df) %>%
stringr::str_subset("^Nuclei_|^Cells_|^Cytoplasm_")
futile.logger::flog.info(sprintf("Writing variable selections to %s", variable_selections_file))
data_frame(variable = variables) %>% readr::write_csv(variable_selections_file)
}