-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMTS_Analysis.R
More file actions
262 lines (216 loc) · 9.45 KB
/
MTS_Analysis.R
File metadata and controls
262 lines (216 loc) · 9.45 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# Script to run MTS biomarker analyses
# Replace expression_dir and response_dir with appropriate directories to run
# source functions from functions script (also loads libraries)
suppressMessages(source("./src/analysis_functions.R"))
# base directories
expression_dir <- # INSERT path to folder with general expression data
response_dir <- # INSERT path to folder with project specific results
# base directory
script_args <- commandArgs(trailingOnly = TRUE)
if (length(script_args) != 2) {
stop("Please pass directory with data files as argument", call. = FALSE)
}
expression_dir <- script_args[1] # folder with general expression data
response_dir <- script_args[2] # folder with project specific results
#---- Load data ----
print("Loading the data")
GE <- data.table::fread(paste0(expression_dir, "/ge.csv")) %>% # gene exp.
as.matrix(., rownames = "V1")
CNA <- data.table::fread(paste0(expression_dir, "/cna.csv")) %>% # copy num
as.matrix(., rownames = "V1")
MET <- data.table::fread(paste0(expression_dir, "/met.csv")) %>% # metablomics
as.matrix(., rownames = "V1")
miRNA <- data.table::fread(paste0(expression_dir, "/mirna.csv")) %>% # micro RNA
as.matrix(., rownames = "V1")
MUT <- data.table::fread(paste0(expression_dir, "/mut.csv")) %>% # mutations
as.matrix(., rownames = "V1")
PROT <- data.table::fread(paste0(expression_dir, "/prot.csv")) %>% # proteomics
as.matrix(., rownames = "V1")
XPR <- data.table::fread(paste0(expression_dir, "/xpr.csv")) %>% # CRISPR KO
as.matrix(., rownames = "V1")
LIN <- data.table::fread(paste0(expression_dir, "/lin.csv")) %>% # lineage
as.matrix(., rownames = "V1")
shRNA <- data.table::fread(paste0(expression_dir, "/shrna.csv")) %>% # lineage
as.matrix(., rownames = "V1")
repurposing <- data.table::fread(paste0(expression_dir, "/rep.csv")) %>% # repurposing
as.matrix(., rownames = "V1")
repurposing_meta <- data.table::fread(paste0(expression_dir, "/rep_info.csv")) %>%
dplyr::mutate(column_name = paste("REP", column_name, sep = "_"),
name = stringr::str_replace_all(name, "[[:punct:]\\s]+", "-")) %>%
dplyr::select(-dose, -screen_id)
continuous_features <- list(GE, CNA, MET, miRNA, PROT, XPR, shRNA, repurposing)
continuous_names <- c("GE", "CNA", "MET", "miRNA", "PROT", "XPR", "shRNA", "REP")
discrete_features <- list(LIN, MUT)
discrete_names <- c("LIN", "MUT")
# combinations of features for multivariate
X.all <- data.table::fread(paste0(expression_dir, "/x-all.csv")) %>%
subset(select=which(!duplicated(names(.)))) %>%
unique() %>%
as.matrix(., rownames = "V1")
X.ccle <- data.table::fread(paste0(expression_dir, "/x-ccle.csv")) %>%
subset(select=which(!duplicated(names(.)))) %>%
unique() %>%
as.matrix(., rownames = "V1")
# DRC table
DRC <- data.table::fread(paste0(response_dir, "/DRC_TABLE.csv"))
DRC %<>%
dplyr::distinct(ccle_name, culture, pert_name, pert_mfc_id, auc, log2.ic50, max_dose) %>%
dplyr::mutate(log2.ic50 = ifelse((is.finite(auc) & is.na(log2.ic50)),
3 * max_dose, log2.ic50),
log2.auc = log2(auc)) %>%
tidyr::gather(key = "dose", value = "response", log2.auc, log2.ic50) %>%
dplyr::filter(is.finite(response))
# LFC table
LFC <- data.table::fread(paste0(response_dir, "/LFC_TABLE.csv")) %>%
dplyr::distinct(pert_name, ccle_name, culture, pert_idose, pert_mfc_id, LFC.cb) %>%
dplyr::rename(response = LFC.cb, dose = pert_idose) %>%
dplyr::filter(is.finite(response))
# combined
Yall <- dplyr::bind_rows(DRC, LFC)
# get principle components of lineage (for confounders)
LIN_PCs <- gmodels::fast.prcomp(LIN);
LIN_PCs <- LIN %*% LIN_PCs$rotation[, LIN_PCs$sdev > 0.2]
print("Loaded the data")
#---- RUN ANALYSIS ----
# each run is a compound and a dose (dose can be actual dose or AUC/IC50)
runs <- Yall %>%
dplyr::distinct(pert_mfc_id, pert_name, dose)
# empty tibbles to store results
continuous <- tibble()
discrete <- tibble()
rf_results <- tibble()
multi_models <- tibble()
print("Starting analysis")
# loop through runs and do analysis
for (i in 1:nrow(runs)) {
run <- runs[i,] # current run, print for debugging/sanity/progress bar
print(paste(run$pert_name, run$dose))
# select relevant responses and convert to vector
Y <- Yall %>%
dplyr::filter(pert_name == run$pert_name, dose == run$dose)
y <- Y$response; names(y) <- Y$ccle_name
if (all(is.na(y))) {
next
}
#---- MUTLIVARIATE ANALYSIS ----
if (length(intersect(rownames(X.all), names(y))) >= 20) {
multi_results_all <- random_forest(X.all, y, k = 10, n = 500)
# get results (if they exist) and add to aggregation table
if (nrow(multi_results_all$rf.fit) > 0) {
result_rf_all <- multi_results_all$rf.fit %>%
dplyr::arrange(desc(RF.imp.mean)) %>%
dplyr::mutate(pert_mfc_id = run$pert_mfc_id,
pert_name = run$pert_name,
pert_idose = run$dose,
rank = 1:n(),
model = "all")
}
result_models_all <- multi_results_all$mod.tab %>%
dplyr::mutate(pert_mfc_id = run$pert_mfc_id,
pert_name = run$pert_name,
pert_idose = run$dose,
model = "all")
} else {
result_rf_all <- tibble()
result_models_all <- tibble()
}
# repeat with CCLE data set
if (length(intersect(rownames(X.ccle), names(y))) >= 20) {
multi_results_ccle <- random_forest(X.ccle, y, k = 10, n = 500)
if (nrow(multi_results_ccle$rf.fit) > 0) {
result_rf_ccle <- multi_results_ccle$rf.fit %>%
dplyr::arrange(desc(RF.imp.mean)) %>%
dplyr::mutate(pert_mfc_id = run$pert_mfc_id,
pert_name = run$pert_name,
pert_idose = run$dose,
rank = 1:n(),
model = "ccle")
}
result_models_ccle <- multi_results_ccle$mod.tab %>%
dplyr::mutate(pert_mfc_id = run$pert_mfc_id,
pert_name = run$pert_name,
pert_idose = run$dose,
model = "ccle")
} else {
result_rf_ccle <- tibble()
result_models_ccle <- tibble()
}
rf_results %<>%
dplyr::bind_rows(result_rf_all) %>%
dplyr::bind_rows(result_rf_ccle)
multi_models %<>%
dplyr::bind_rows(result_models_all) %>%
dplyr::bind_rows(result_models_ccle)
#---- UNIVARIATE CONTINUOUS ANALYSIS ----
for (j in 1:length(continuous_features)) {
feature_table <- continuous_features[[j]]
feature_type <- continuous_names[j]
overlap <- dplyr::intersect(rownames(feature_table), names(y))
if (min(y[overlap], na.rm = T) == max(y[overlap], na.rm = T)) next;
# run correlation
corr_table <- lin_associations(feature_table[overlap,], y[overlap])$res.table
corr_table %<>%
dplyr::select(ind.var, PosteriorMean, PosteriorSD, qvalue, rho, q.val) %>%
dplyr::rename(feature = ind.var, coef = rho) %>%
dplyr::arrange(desc(abs(coef))) %>%
dplyr::mutate(rank = 1:n()) %>%
dplyr::filter(rank <= 500) %>%
dplyr::mutate(pert_mfc_id = run$pert_mfc_id,
pert_name = run$pert_name,
dose = run$dose,
feature_type = feature_type)
# for repurposing replace metadata
if (feature_type == "REP") {
corr_table %<>%
dplyr::left_join(repurposing_meta, by = c("feature" = "column_name")) %>%
dplyr::select(-feature) %>%
dplyr::rename(feature = name) %>%
dplyr::mutate(feature = paste("REP", feature, sep = "_"))
}
continuous %<>% dplyr::bind_rows(corr_table)
}
# correlations with lineage PCs as confounders
overlap <- dplyr::intersect(rownames(GE), names(y))
overlap <- dplyr::intersect(overlap, rownames(LIN_PCs))
ge_tab <- lin_associations(GE[overlap,], y[overlap], W=LIN_PCs[overlap,])$res.table
ge_tab %<>%
dplyr::select(ind.var, PosteriorMean, PosteriorSD, qvalue, rho, q.val) %>%
dplyr::rename(feature = ind.var, coef = rho) %>%
dplyr::arrange(desc(abs(coef))) %>%
dplyr::mutate(rank = 1:n()) %>%
dplyr::filter(rank <= 500) %>%
dplyr::mutate(pert_mfc_id = run$pert_mfc_id,
pert_name = run$pert_name,
dose = run$dose,
feature_type = "GE_noLIN")
continuous %<>% dplyr::bind_rows(ge_tab)
#---- UNIVARIATE DISCRETE ANALYSIS ----
if(run$dose != "log2.ic50") {
for(z in 1:length(discrete_features)) {
feature_table <- discrete_features[[z]]
feature_type <- discrete_names[z]
# run t-test
t_table <- discrete_test(feature_table, y)
t_table %<>%
dplyr::mutate(pert_mfc_id = run$pert_mfc_id) %>%
dplyr::mutate(pert_name = run$pert_name) %>%
dplyr::mutate(dose = run$dose) %>%
dplyr::mutate(feature_type = feature_type)
# only keep top 100 mutations
if (feature_type == "MUT" & nrow(t_table) > 0) {
t_table %<>%
dplyr::arrange(q.value) %>%
dplyr::mutate(rank = 1:n()) %>%
dplyr::filter(rank <= 100) %>%
dplyr::select(-rank)
}
discrete %<>% dplyr::bind_rows(t_table)
}
}
print(paste(i, "of", nrow(runs), "complete"))
}
# write results to project folder
readr::write_csv(continuous, paste0(response_dir, "/continuous_associations.csv"))
readr::write_csv(discrete, paste0(response_dir, "/discrete_associations.csv"))
readr::write_csv(rf_results, paste0(response_dir, "/RF_table.csv"))
readr::write_csv(multi_models, paste0(response_dir, "/Model_table.csv"))