-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_buildModels.R
338 lines (306 loc) · 12.3 KB
/
4_buildModels.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
library(here)
library(data.table)
library(fst)
library(tidyverse)
library(glmnet)
library(nestedcv)
library(caret)
library(pROC)
set.seed(1997, "L'Ecuyer-CMRG")
# model function ----------------------------------------------------------
formatFeatureMatrix <- function(trainCounts, testCounts, trainMeta, testMeta) {
# genes not present in both cohorts
absent_lin <- setdiff(trainCounts$feature, testCounts$feature)
# remove these genes from counts
trainCounts <- trainCounts %>% filter(!feature %in% absent_lin)
testCounts <- testCounts %>% filter(!feature %in% absent_lin)
# make sure same features in both
trainCounts <- trainCounts %>% filter(feature %in% testCounts$feature)
testCounts <- testCounts %>% filter(feature %in% trainCounts$feature)
# format count matrices
trainCounts <- trainCounts %>% column_to_rownames("feature")
testCounts <- testCounts %>% column_to_rownames("feature")
# transpose
trainCounts <- trainCounts %>%
t() %>%
as.data.frame() %>%
rownames_to_column("id") %>%
as_tibble()
testCounts <- testCounts %>%
t() %>%
as.data.frame() %>%
rownames_to_column("id") %>%
as_tibble()
# add condition onto counts
trainCounts <- trainCounts %>%
left_join(trainMeta[, c("id", "condition")], by = "id") %>%
relocate(condition, .after = id) %>%
column_to_rownames("id")
testCounts <- testCounts %>%
left_join(testMeta[, c("id", "condition")], by = "id") %>%
relocate(condition, .after = id) %>%
column_to_rownames("id")
# export as list object
output <- list(train = trainCounts, test = testCounts)
return(output)
}
trainLR <- function(counts, filter) {
set.seed(1997, "L'Ecuyer-CMRG")
counts$condition <- as.factor(ifelse(counts$condition == "PD", 1, 0))
x <- as.matrix(counts[, -1])
y <- counts$condition
if (filter == TRUE) {
nested_results <- nestcv.glmnet(
x = x, y = y,
filterFUN = wilcoxon_filter,
filter_options = list(p_cutoff = 0.05, rsq_cutoff = 0.9),
n_outer_folds = 10, n_inner_folds = 10, outer_train_predict = TRUE,
family = "binomial", alphaSet = seq(0, 1, 0.05),
finalCV = TRUE, cv.cores = 2
)
} else {
nested_results <- nestcv.glmnet(
x = x, y = y,
n_outer_folds = 10, n_inner_folds = 10, outer_train_predict = TRUE,
family = "binomial", alphaSet = seq(0, 1, 0.05),
finalCV = TRUE, cv.cores = 2
)
}
return(nested_results)
}
modelFunction <- function(listModelCounts) {
# train model on PPMI
train_model <- trainLR(counts = listModelCounts[["train"]], filter = FALSE)
print(train_model[["roc"]])
# predict on ICICLE-PD
test_predict <- predict(train_model,
newdata = as.matrix(listModelCounts[["test"]][, -1]),
type = "response"
)
test_predict <- test_predict %>%
as.data.frame() %>%
rownames_to_column("id") %>%
left_join(meta.icicle[, c("id", "condition")], by = "id")
print(roc(test_predict$condition ~ test_predict$s1, levels = c("Control", "PD"), direction = "<"))
# export
return(list(
train = train_model,
test = test_predict
))
}
# IMPORT GENERAL DATA -----------------------------------------------------
# metadata
meta.ppmi <- read_rds("circRNA/data/ppmi_metadata.rds") %>%
mutate(id = as.character(id))
meta.icicle <- read_rds("circRNA/data/icicle_metadata.rds")
# junction counts
jc <- read_fst("circRNA/data/bound_juncInfo.fst") %>%
rename(feature = coord_id)
# CLASSIFICATION WITH TOTAL RNA -------------------------------------------
# import significant features from total rna results
sig_linear_features <- read_csv("linear/output/ppmi_deseqResults.csv") %>%
filter(pvalue < 0.05) %>%
pull(ensembl)
# import counts
counts_lin.ppmi <- fread("linear/data/ppmi_deseqFilteredNormalisedCounts.csv", header = TRUE) %>%
rename(feature = ensembl) %>%
filter(feature %in% sig_linear_features)
counts_lin.icicle <- fread("linear/data/icicle_deseqFilteredNormalisedCounts.csv", header = TRUE) %>%
rename(feature = ensembl) %>%
filter(feature %in% sig_linear_features)
# getTPM <- function(){
# formatTPM <- function(txi, study){
# txi[["abundance"]] %>%
# as.data.frame() %>%
# rownames_to_column('gene_id') %>%
# pivot_longer(cols = !gene_id, names_to = 'path', values_to = 'TPM') %>%
# mutate(study = {{ study }})
# }
# cat("Importing PPMI TPMs...\n")
# linear_meta.ppmi <- read_csv(here('ppmi/output/BL_iPD_HC_metadata.csv')) %>%
# mutate(id = as.character(id))
# ppmi <- readRDS(here("linear/data/ppmi_txiForDeseq.rds")) %>%
# formatTPM("PPMI") %>%
# # add on sample ID that corresponds to fastq full name
# left_join(linear_meta.ppmi[, c('id', 'path')], by = 'path') %>%
# # remove fastq name path by selecting only the specific columns we need
# select(gene_id, id, TPM, study)
# cat("Importing ICICLE-PD TPMs...\n")
# icicle <- readRDS(here("linear/data/icicle_txiForDeseq.rds")) %>%
# formatTPM("ICICLE-PD") %>%
# rename('id' = 'path')
# tpm <- bind_rows(ppmi, icicle)
# return(tpm)
# }
# # import TPM counts
# tpm_counts <- getTPM()
# # filter for PPMI and log2 + 1
# counts_lin.ppmi <- tpm_counts %>%
# mutate(TPM = log2(TPM + 1)) %>%
# filter(study == 'PPMI') %>%
# pivot_wider(id_cols = 'gene_id', names_from = 'id', values_from = 'TPM', values_fill = 0) %>%
# rename(feature = gene_id)
# # filter for ICICLE and log2 +1
# counts_lin.icicle <- tpm_counts %>%
# mutate(TPM = log2(TPM + 1)) %>%
# filter(study == 'ICICLE-PD') %>%
# pivot_wider(id_cols = 'gene_id', names_from = 'id', values_from = 'TPM', values_fill = 0) %>%
# rename(feature = gene_id)
# # clean up
# rm(tpm_counts)
# only use DEGs from PPMI
counts_lin.ppmi <- counts_lin.ppmi %>% filter(feature %in% sig_linear_features)
counts_lin.icicle <- counts_lin.icicle %>% filter(feature %in% sig_linear_features)
lin_model_counts <- formatFeatureMatrix(
trainCounts = counts_lin.ppmi,
testCounts = counts_lin.icicle,
trainMeta = meta.ppmi,
testMeta = meta.icicle
)
# model + prediction
lin_model <- modelFunction(lin_model_counts)
# CLASSIFICATION WITH BSJ COUNTS ------------------------------------------
# import sig BSJs
bsj_results <- read_csv("circRNA/output/ppmi_BSJresults.csv")
sig_bsj_features <- bsj_results %>%
filter(pvalue < 0.05) %>%
pull(coord_id)
# import counts
bsj_counts.ppmi <- read_csv("circRNA/data/ppmi_vstBSJCounts.csv") %>%
rename(feature = coord_id) %>%
filter(feature %in% sig_bsj_features)
bsj_counts.icicle <- read_csv("circRNA/data/icicle_vstBSJCounts.csv") %>%
rename(feature = coord_id) %>%
filter(feature %in% sig_bsj_features)
# bsj_counts.ppmi <- jc %>%
# filter(study == "PPMI") %>%
# select(feature, id, bsj_perMapped) %>%
# pivot_wider(id_cols = feature, names_from = id, values_from = bsj_perMapped, values_fill = 0) %>%
# mutate(across(where(is.numeric), ~log2(.x + 1)))
#
# bsj_counts.icicle <- jc %>%
# filter(study == "ICICLE-PD") %>%
# select(feature, id, bsj_perMapped) %>%
# pivot_wider(id_cols = feature, names_from = id, values_from = bsj_perMapped, values_fill = 0) %>%
# mutate(across(where(is.numeric), ~log2(.x + 1)))
# format for model building
bsj_model_counts <- formatFeatureMatrix(
trainCounts = bsj_counts.ppmi, testCounts = bsj_counts.icicle,
trainMeta = meta.ppmi, testMeta = meta.icicle
)
# model + prediction
bsj_model <- modelFunction(bsj_model_counts)
# CLASSIFICATION WITH FSJ COUNTS ------------------------------------------
# import counts
fsj_counts.ppmi <- read_csv("circRNA/data/ppmi_vstFSJCounts.csv") %>%
rename(feature = coord_id) %>%
filter(feature %in% sig_bsj_features)
fsj_counts.icicle <- read_csv("circRNA/data/icicle_vstFSJCounts.csv") %>%
rename(feature = coord_id) %>%
filter(feature %in% sig_bsj_features)
# format for model building
fsj_model_counts <- formatFeatureMatrix(
trainCounts = fsj_counts.ppmi, testCounts = fsj_counts.icicle,
trainMeta = meta.ppmi, testMeta = meta.icicle
)
# model + prediction
fsj_model <- modelFunction(fsj_model_counts)
# BSJ:FSJ RATIO MODEL -----------------------------------------------------
ratio_counts.ppmi <- jc %>%
filter(
study == "PPMI",
feature %in% sig_bsj_features
) %>%
select(id, feature, junc_ratio) %>%
pivot_wider(id_cols = feature, names_from = id, values_from = junc_ratio, values_fill = 0)
ratio_counts.icicle <- jc %>%
filter(
study == "ICICLE-PD",
feature %in% sig_bsj_features
) %>%
select(id, feature, junc_ratio) %>%
pivot_wider(id_cols = feature, names_from = id, values_from = junc_ratio, values_fill = 0)
# format
ratio_model_counts <- formatFeatureMatrix(
trainCounts = ratio_counts.ppmi, testCounts = ratio_counts.icicle,
trainMeta = meta.ppmi, testMeta = meta.icicle
)
# model + prediction
ratio_model <- modelFunction(ratio_model_counts)
# COMBINED LINEAR + BSJ ---------------------------------------------------
# create a matrix from linear + BSJ counts (just bind the cols together)
combined_lin_bsj_model_counts <- list(
train = bind_cols(lin_model_counts$train, bsj_model_counts$train[, -1]),
test = bind_cols(lin_model_counts$test, bsj_model_counts$test[, -1])
)
# model + prediction
combined_lin_bsj_model <- modelFunction(combined_lin_bsj_model_counts)
# CIRCRNA IMBALANCE MODEL ---------------------------------------------------------------------
control_vst <- bsj_counts.ppmi %>%
pivot_longer(cols = !feature, names_to = "id", values_to = "vst") %>%
left_join(meta.ppmi[, c("id", "condition")], by = "id") %>%
filter(condition == "Control") %>%
group_by(feature) %>%
summarise(control_vst = mean(vst))
fold_changes <- bsj_results %>%
# encode whether bsj is up or down in PD
mutate(direction = case_when(
log2FoldChange < 0 ~ "down",
log2FoldChange > 0 ~ "up",
TRUE ~ "noDiff"
)) %>%
# rename coord id to feature for joining
rename(feature = coord_id)
# convert counts to tidy format
imbalance_model <- bsj_counts.ppmi %>%
pivot_longer(cols = !feature, names_to = "id", values_to = "vst")
# add on direction from DEA
imbalance_model <- imbalance_model %>% left_join(fold_changes[, c("feature", "direction")], by = "feature")
# add on control mean vst counts
imbalance_model <- imbalance_model %>% left_join(control_vst, by = "feature")
# add on study group
imbalance_model <- imbalance_model %>% left_join(meta.ppmi[, c("id", "condition")], by = "id")
# create model score - add 1 if expression agrees
imbalance_model <- imbalance_model %>% mutate(score = case_when(
direction == "down" & vst < control_vst ~ 1,
direction == "up" & vst > control_vst ~ 1,
TRUE ~ 0
))
# calculate total score
imbalance_model <- imbalance_model %>%
group_by(id) %>%
mutate(total_score = sum(score)) %>%
select(id, total_score, condition) %>%
distinct()
# repeat in ICICLE-PD
imbalance_model_replicate <- bsj_counts.icicle %>% pivot_longer(cols = !feature, names_to = "id", values_to = "vst")
# add direction from PPMI
imbalance_model_replicate <- imbalance_model_replicate %>% left_join(fold_changes[, c("feature", "direction")], by = "feature")
# add on control vst from PPMI
imbalance_model_replicate <- imbalance_model_replicate %>% left_join(control_vst, by = "feature")
# calculate score
imbalance_model_replicate <- imbalance_model_replicate %>% mutate(score = case_when(
direction == "down" & vst < control_vst ~ 1,
direction == "up" & vst > control_vst ~ 1,
TRUE ~ 0
))
# add on ICICLE study groups
imbalance_model_replicate <- imbalance_model_replicate %>% left_join(meta.icicle[, c("id", "condition")], by = "id")
# total score
imbalance_model_replicate <- imbalance_model_replicate %>%
group_by(id) %>%
mutate(total_score = sum(score)) %>%
select(id, total_score, condition) %>%
distinct()
# export model predictions
imbalance_model <- list(train = imbalance_model, test = imbalance_model_replicate)
# EXPORT MODELS -----------------------------------------------------------
models <- list(
lin_model = lin_model,
bsj_model = bsj_model,
fsj_model = fsj_model,
ratio_model = ratio_model,
combined_lin_bsj_model = combined_lin_bsj_model,
imbalance_model = imbalance_model
)
write_rds(models, "combinedLinCirc/output/models.rds")