-
Notifications
You must be signed in to change notification settings - Fork 0
/
TAS.Rmd
203 lines (189 loc) · 5.75 KB
/
TAS.Rmd
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
---
title: "Transformation activity scores (TASs) for FGFR1-4 variants"
author: "Masachika Ikegami"
date: "5/15/2019"
output:
pdf_document:
fig_caption: yes
latex_engine: xelatex
number_sections: yes
toc: true
html_document:
df_print: paged
toc: yes
header-includes:
- \usepackage{amsmath,amssymb}
- \usepackage{mathspec}
- \setlength{\mathindent}{0pt}
classoption: xelatex,ja=standard,fleqn
---
\pagebreak
# Setting up
```{r setup, results='hide'}
Working = "home/TAS"
knitr::opts_chunk$set(message=FALSE)
knitr::opts_chunk$set(tidy=TRUE)
knitr::opts_chunk$set(warning=FALSE)
knitr::opts_chunk$set(error=FALSE)
knitr::opts_chunk$set(fig.width=12, fig.height=6, fig.align='center', fig.pos='H', out.extra='', tidy.opts = list(width.cutoff = 45))
knitr::opts_knit$set(root.dir=Working)
setwd(Working)
options(scipen=100)
library(stringr)
library(rstan)
library(tidyverse)
library(reshape2)
library(shinystan)
library(codetools)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
```
# Loading the experimental results of focus formation assay and low-serum cell proliferation assay
```{r}
filename = "Data.csv"
v_list = NULL
N = NULL
V = NULL
I = 4
FFA_Data = read.csv(file = filename, stringsAsFactors=F)
FFA_Data$Name = str_replace_all(FFA_Data$Name, pattern="\\*", replacement="STOP")
FFA_Data$AA = str_sub(FFA_Data$Name, 1,-3)
v_l = unique(FFA_Data$AA)
anchor = "FGFR1_WT"
v_l2 = v_l[-which(v_l %in% anchor)]
v_list = c(anchor,v_l2)
N = length(FFA_Data$score)
V = length(v_list)
var_num = rep(0,N)
for(k in 1:N){
for(j in 1:V){
if(FFA_Data$AA[k] == v_list[j]){
var_num[k] = j
}
}
}
FFA_Data = cbind(FFA_Data, var_num)
```
# Ordered logit model simulation code in Stan language
```{r}
stan_code_F <- "
data{
int I; // Number of classes
int N; // Number of measurements
int B[N]; // Batches of each measurement
int BN; // Number of the batches
int V; // Number of the variants
int Y[N]; // Results of each measurement
int variant[N]; // Variant ID of each
}
parameters{
ordered[I-1] d; // Thresholds for the ordered logit model
simplex[V] eta; // Functional scores of each variant
vector[BN] be_t; // Batch-specific random effects, intercept
vector<lower=0>[BN] tau; // Batch-specific random effects, slope
}
transformed parameters{
vector[BN] bet;
bet[1] = -tau[1]/V;
for(b in 2:BN){
bet[b] = be_t[b];
}
}
model{
for(n in 1:N){
Y[n] ~ ordered_logistic(bet[B[n]]+tau[B[n]]*eta[variant[n]], d);
}
}
generated quantities{
vector[V] Delta_Eta;
for(v in 1:V){
Delta_Eta[v] = eta[v]-eta[1];
}
}
"
write(stan_code_F, file="stan_code_FGFR.stan")
stanmodel_F <- stan_model(file="stan_code_FGFR.stan")
```
## Execution of Hamiltonian Monte Carlo simulation
Change "chains", "iter", "warmup" referring to R-hat values and posterior expectations.
```{r,eval=FALSE}
fit_FFA = NULL
HMC_data <- list(N = N, V = V, I = I,
Y = FFA_Data$score, variant = FFA_Data$var_num,
B = FFA_Data$batch, BN = max(FFA_Data$batch))
fit <- stan(file="stan_code_FGFR.stan", data=HMC_data,
control=list(adapt_delta=0.9999, max_treedepth = 20),
seed=1234, chains=4, iter=3500, warmup=1500, thin=2)
fit_FFA = fit
```
# Quality check
Judge the convergence of simulation if R-hat values of all parameters are < 1.1 or not.
```{r eval=FALSE}
launch_shinystan(fit_FFA)
```
## Simulation summary
```{r}
DF = NULL
DF_eta = NULL
DF_d = NULL
df_eta <- subset(summary(fit_FFA,probs = c(0.025, 0.05, 0.50, 0.95, 0.975))$summary, grepl("eta",
rownames(summary(fit_FFA,probs = c(0.025, 0.05, 0.50, 0.95, 0.975))$summary)))
df_Eta <- subset(summary(fit_FFA,probs = c(0.025, 0.05, 0.50, 0.95, 0.975))$summary, grepl("Eta",
rownames(summary(fit_FFA,probs = c(0.025, 0.05, 0.50, 0.95, 0.975))$summary)))
df_d <- subset(summary(fit_FFA,probs = c(0.025, 0.05, 0.50, 0.95, 0.975))$summary, grepl("d",
rownames(summary(fit_FFA,probs = c(0.025, 0.05, 0.50, 0.95, 0.975))$summary)))
df_beta <- subset(summary(fit_FFA,probs = c(0.025, 0.05, 0.50, 0.95, 0.975))$summary, grepl("bet",
rownames(summary(fit_FFA,probs = c(0.025, 0.05, 0.50, 0.95, 0.975))$summary)))
df_tau <- subset(summary(fit_FFA,probs = c(0.025, 0.05, 0.50, 0.95, 0.975))$summary, grepl("tau",
rownames(summary(fit_FFA,probs = c(0.025, 0.05, 0.50, 0.95, 0.975))$summary)))
df_ <- rbind(df_eta,df_Eta,df_d,df_tau,df_beta)
DF = df_
DF_eta = df_eta
DF_d = df_d
```
## Sampling from posterior distribution and estimating TAS
Draw 500 parameter samples from the trace randomly, then generate values, taking batch-to-batch ratio into account.
```{r}
FFA_result = NULL
len <- length(v_list)
eta <- NULL
eta$ave <- DF_eta[1:len]
eta$name <- v_list
repN <- 500
BN <- max(FFA_Data$batch)
BN_ <- NULL
for(j in 1:BN){
BN_ <- c(BN_,length(subset(FFA_Data, batch == j)$batch))
}
NY <- length(v_list)
p_Y <- array(0,c(repN, BN, NY))
d.ext<-rstan::extract(fit_FFA)
for(k in 1:repN){
for(j in 1: NY){
tmp <- 0
for(l in 1: BN){
p_Y[k,l,j] = p_Y[k,l,j] + (d.ext$bet[k,l] + d.ext$tau[k,l]*d.ext$eta[k,j]) * BN_[l] * BN / sum(BN_)
}
}
}
eta$le95 <- DF_eta[(len*3+1):(len*4)]
eta$ue95 <- DF_eta[(len*7+1):(len*8)]
y_ave <- rep(0,len)
y_class <- rep(4,len)
for(j in 1:len){
y_ave[j] <- mean(p_Y[,,j])
eta$ue95[j] <- t.test(p_Y[,,j])[4]$conf.int[2]
eta$le95[j] <- t.test(p_Y[,,j])[4]$conf.int[1]
if(y_ave[j] < DF_d[3])
y_class[j] <- 3
if(y_ave[j] < DF_d[2])
y_class[j] <- 2
if(y_ave[j] < DF_d[1])
y_class[j] <- 1
}
eta$y_ave <- y_ave
eta$y_class <- y_class
filename <- "FGFR-FFA_score.tsv"
write.table(eta, file=filename, sep='\t',
quote=FALSE, col.names=NA)
```