-
Notifications
You must be signed in to change notification settings - Fork 1
/
2d_param_new.R
227 lines (190 loc) · 9.77 KB
/
2d_param_new.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
#################################################
# libs
#################################################
library(dgRaph)
library(dplyr)
library(VGAM)
library(bbmle)
library(pROC) # calculates AUC
loglik <- function(alpha, beta){
-sum(dbetabinom.ab(xi, ni, alpha, beta, log = T))
}
IDs <- c("RAG1AP1","CPA1","NEK2","RNASEH2A","LOC148145","TMEM63B","TIMM17A","PLK1","RABIF","PTF1A")
auc <- NULL
kl <- NULL
facPotNormals <- NULL
facPotTumours <- NULL
for (j in 1:length(IDs)) {
i <- which(names(data_BRCA) %in% IDs[j]) # find the index to work with
# calculate discretization upper and lower limits
expr <- as.data.frame(data_BRCA[[i]]) %>%
mutate(EXPR = read_count / lib_size) %>%
select(EXPR)
pr <- as.data.frame(data_BRCA[[i]]) %>%
select(starts_with("pr", ignore.case = F))
n_pr_cpg <- ncol(pr)
gb <- as.data.frame(data_BRCA[[i]]) %>%
select(starts_with("gb", ignore.case = F))
n_gb_cpg <- ncol(gb)
# upper and lower limits
min_p <- min(pr)-0.1
max_p <- max(pr)+0.1
min_gb <- min(gb)-0.1
max_gb <- max(gb)+0.1
min_e <- max(0,min(expr)-0.001*min(expr))
max_e <- max(expr)+0.001*max(expr)
#################################################
# Normals model
#################################################
data_normals_train <- as.data.frame(data_BRCA[[i]][an_training,])
#################################################
# Learn betabinomial
#################################################
ni <- as.integer(data_normals_train$lib_size)
xi <- as.integer(data_normals_train$read_count)
m0 <- mle2(minuslogl = loglik, start = list(alpha = 1, beta = 1), method = "L-BFGS-B", lower=c(alpha = 0.0001, beta = 0.0001))
alpha <- coef(m0)['alpha']
beta <- coef(m0)['beta']
# Posteriors
alphaPost <- alpha + data_normals_train$read_count
betaPost <- beta + data_normals_train$lib_size - data_normals_train$read_count
#################################################
# Data-massage
#################################################
df_normals <- data_normals_train %>%
mutate(EXPR = NA) %>%
mutate(PR_overall = NA) %>%
mutate(GB_overall = NA) %>%
mutate_each(funs(bin = as.integer(cut(., breaks = seq(min_p,max_p,length.out = 101), labels = c(1:100)))), PR = starts_with("pr", ignore.case = F)) %>%
mutate_each(funs(bin = as.integer(cut(., breaks = seq(min_gb,max_gb,length.out = 101), labels = c(1:100)))), GB = starts_with("gb", ignore.case = F)) %>%
select(-starts_with("pr", ignore.case = F), -starts_with("gb", ignore.case = F), -lib_size, -read_count)
#################################################
# Building models and training
#################################################
# Build Model
varDim <- rep(100, 3+n_pr_cpg+n_gb_cpg)
facPot <- list(linregPotential(dim = c(100, 100)), # PR_overall | EXPR
linregPotential(dim = c(100, 100)), # GB_overall | EXPR
fixedNormalPotential(), # PR_i | PR
fixedNormalPotential()) # GB_i | GB
facNbs <- c(list(c(1,2)), # PR_overall | EXPR
list(c(1,3)), # GB_overall | EXPR
lapply(4:(3+n_pr_cpg), FUN=function(i){c(2,i)}), # PR_i | PR_overall
lapply((1+3+n_pr_cpg):(3+n_pr_cpg+n_gb_cpg), FUN=function(i){c(3,i)}) # GB_i | GB_overall
)
potMap <- c(1, 2, rep(3, n_pr_cpg), rep(4, n_gb_cpg))
dfg_normals <- dfg(varDim, facPot, facNbs, potMap, varNames = names(df_normals))
optimFun <- list(linreg1 = linregOptimize(range1 = c(min_e,max_e), range2 = c(min_p,max_p)),
linreg2 = linregOptimize(range1 = c(min_e,max_e), range2 = c(min_gb,max_gb)))
# Data
dataList <- list()
dataList[[1]] <- lapply(1:nrow(df_normals), FUN=function(i){
breaks <- seq(min_e, max_e, length.out = 101)
diff( pbeta(breaks, alphaPost[i], betaPost[i]))
})
# Train
dfg_normals <- train(data = df_normals,
dataList = dataList,
dfg = dfg_normals,
optim = c("linreg1", "linreg2", "noopt", "noopt"),
optimFun = optimFun, iter.max = 500, threshold = 1e-5)
# Hack Expression prior
cur_length <- length(dfg_normals$facNbs)+1
facNbs[[cur_length]] <- 1
potMap[cur_length] <- 5
facPot <- potentials(dfg_normals)
facPot[[5]] <- betaPotential(range=c(min_e,max_e),alphas=alpha, betas=beta)
dfg_normals <- dfg(varDim, facPot, facNbs, potMap, varNames = names(df_normals))
facPotNormals[[j]] <- potentials(dfg_normals)
#################################################
# Tumours model
#################################################
data_tumours_train <- as.data.frame(data_BRCA[[i]][t_training,])
#################################################
# Learn betabinomial
#################################################
ni <- as.integer(data_tumours_train$lib_size)
xi <- as.integer(data_tumours_train$read_count)
m0 <- mle2(minuslogl = loglik, start = list(alpha = 1, beta = 1), method = "L-BFGS-B", lower=c(alpha = 0.0001, beta = 0.0001))
alpha <- coef(m0)['alpha']
beta <- coef(m0)['beta']
# Posteriors
alphaPost <- alpha + data_tumours_train$read_count
betaPost <- beta + data_tumours_train$lib_size - data_tumours_train$read_count
#################################################
# Data-massage
#################################################
df_tumours <- data_tumours_train %>%
mutate(EXPR = NA) %>%
mutate(PR_overall = NA) %>%
mutate(GB_overall = NA) %>%
mutate_each(funs(bin = as.integer(cut(., breaks = seq(min_p,max_p,length.out = 101), labels = c(1:100)))), PR = starts_with("pr", ignore.case = F)) %>%
mutate_each(funs(bin = as.integer(cut(., breaks = seq(min_gb,max_gb,length.out = 101), labels = c(1:100)))), GB = starts_with("gb", ignore.case = F)) %>%
select(-starts_with("pr", ignore.case = F), -starts_with("gb", ignore.case = F), -lib_size, -read_count)
#################################################
# Building models and training
#################################################
# Build Model
varDim <- rep(100, 3+n_pr_cpg+n_gb_cpg)
facPot <- list(linregPotential(dim = c(100, 100)), # PR_overall | EXPR
linregPotential(dim = c(100, 100)), # GB_overall | EXPR
fixedNormalPotential(), # PR_i | PR_overall
fixedNormalPotential()) # GB_i | GB_overall
facNbs <- c(list(c(1,2)), # PR_overall | EXPR
list(c(1,3)), # GB_overall | EXPR
lapply(4:(3+n_pr_cpg), FUN=function(i){c(2,i)}), # PR_i | PR_overall
lapply((1+3+n_pr_cpg):(3+n_pr_cpg+n_gb_cpg), FUN=function(i){c(3,i)}) # GB_i | GB_overall
)
potMap <- c(1, 2, rep(3, n_pr_cpg), rep(4, n_gb_cpg))
dfg_tumours <- dfg(varDim, facPot, facNbs, potMap, varNames = names(df_tumours))
optimFun <- list(linreg1 = linregOptimize(range1 = c(min_e,max_e), range2 = c(min_p,max_p)),
linreg2 = linregOptimize(range1 = c(min_e,max_e), range2 = c(min_gb,max_gb)))
# Data
dataList <- list()
dataList[[1]] <- lapply(1:nrow(df_tumours), FUN=function(i){
breaks <- seq(min_e, max_e, length.out = 101)
diff( pbeta(breaks, alphaPost[i], betaPost[i]))
})
# Train
dfg_tumours <- train(data = df_tumours,
dataList = dataList,
dfg = dfg_tumours,
optim = c("linreg1", "linreg2", "noopt", "noopt"),
optimFun = optimFun, iter.max = 500, threshold = 1e-5)
# Hack Expression prior
cur_length <- length(dfg_tumours$facNbs)+1
facNbs[[cur_length]] <- 1
potMap[cur_length] <- 5
facPot <- potentials(dfg_tumours)
facPot[[5]] <- betaPotential(range=c(min_e,max_e),alphas=alpha, betas=beta)
dfg_tumours <- dfg(varDim, facPot, facNbs, potMap, varNames = names(df_tumours))
facPotTumours[[j]] <- potentials(dfg_tumours)
#################################################
# Evaluation
#################################################
data_train <- as.data.frame(data_BRCA[[i]][c(an_training,t_training),])
df_train <- data_train %>%
mutate(EXPR = (read_count+1) / lib_size) %>%
mutate(EXPR = as.integer(cut(EXPR, breaks = seq(min_e,max_e,length.out = 101), labels = c(1:100)))) %>%
mutate(PR_overall = NA) %>%
mutate(GB_overall = NA) %>%
mutate_each(funs(bin = as.integer(cut(., breaks = seq(min_p,max_p,length.out = 101), labels = c(1:100)))), PR = starts_with("pr", ignore.case = F)) %>%
mutate_each(funs(bin = as.integer(cut(., breaks = seq(min_gb,max_gb,length.out = 101), labels = c(1:100)))), GB = starts_with("gb", ignore.case = F)) %>%
select(-starts_with("pr", ignore.case = F), -starts_with("gb", ignore.case = F), -lib_size, -read_count)
scores_train <- likelihood(dfg = dfg_tumours, data = df_train, log = T) - likelihood(dfg = dfg_normals, data = df_train, log = T)
scores_train[which(is.na(scores_train))] <- vapply(scores_train[which(is.na(scores_train))], FUN= function(x) rnorm(n=1,mean=50), FUN.VALUE = rnorm(n=1,mean=50))
data_eval <- as.data.frame(data_BRCA[[i]][-c(an_training,t_training),])
df_eval <- data_eval %>%
mutate(EXPR = (read_count+1) / lib_size) %>%
mutate(EXPR = as.integer(cut(EXPR, breaks = seq(min_e,max_e,length.out = 101), labels = c(1:100)))) %>%
mutate(PR_overall = NA) %>%
mutate(GB_overall = NA) %>%
mutate_each(funs(bin = as.integer(cut(., breaks = seq(min_p,max_p,length.out = 101), labels = c(1:100)))), PR = starts_with("pr", ignore.case = F)) %>%
mutate_each(funs(bin = as.integer(cut(., breaks = seq(min_gb,max_gb,length.out = 101), labels = c(1:100)))), GB = starts_with("gb", ignore.case = F)) %>%
select(-starts_with("pr", ignore.case = F), -starts_with("gb", ignore.case = F), -lib_size, -read_count)
# Metrics
scores_eval <- likelihood(dfg = dfg_tumours, data = df_eval, log = T) - likelihood(dfg = dfg_normals, data = df_eval, log = T)
scores_eval[which(is.na(scores_eval))] <- vapply(scores_eval[which(is.na(scores_eval))], FUN= function(x) rnorm(n=1,mean=50), FUN.VALUE = rnorm(n=1,mean=50))
auc[[j]] <- auc(predictor=scores_eval,response=c(rep("N",27),rep("T",243)))
kl[[j]] <- kl(dfg_normals,dfg_tumours)
}