-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobal.R
310 lines (262 loc) · 10.5 KB
/
global.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
#
# screenQC: a Shiny app for exploring screen quality in HCS
# Author: Maciej Dobrzynski
#
# This is the auxilary logic for a Shiny web application.
#
# Check if all required packages are installed, if not, attempt to install the missing ones
required_packages = c(
"shiny",
"shinydashboard",
"shinyBS",
"data.table",
"ggplot2",
"plotly",
"ggthemes",
"RColorBrewer"
)
missing_packages =
required_packages[!(required_packages %in% installed.packages()[, "Package"])]
if (length(missing_packages)) {
cat(paste(
"Missing packages:",
paste(missing_packages, collapse = ";"),
"\nAttempting to install them."
))
install.packages(missing_packages)
}
library(shinydashboard)
library(shinyBS)
library(data.table)
library(ggplot2)
library(plotly)
library(ggthemes)
library(RColorBrewer)
## Help text ----
helpTextServer = list(
chBrobust ="Calculate robust statistics: median instead of the mean, MAD instead of SD.",
rBscreenType = "Choose antagonist when testing inhibitors, agonist for activators.",
alPlateFormat = "Choose a plate format for synthetic data, e.g. 96-, 384-, 1536-well.",
alSSMDcrit = "<p>Choose quality control level for the strictly
standardized mean difference (SSMD).</p>
<p>Learn more <a href=\"https://en.wikipedia.org/wiki/Strictly_standardized_mean_difference#Quality_control\" target=\"_blank\">here</a>.</p>",
alSSMDinfo = "<p>Strictly standardized mean difference (SSMD)
is a measure of effect size. It is the mean divided by the
standard deviation of a difference of two random values respectively
from two groups.</p>
<p>Learn more <a href=\"https://en.wikipedia.org/wiki/Strictly_standardized_mean_difference#Statistical_parameter\" target=\"_blank\">here</a>.</p>",
alZprimeInfo = "<p>The Z'-factor is the characteristic parameter for
the quality of the assay itself, without intervention of test compounds.</p>
<p>
<table style=\"width:100%\">
<tr>
<th>Z'-factor</th>
<th>Interpretation</th>
</tr>
<tr>
<td>1.0</td>
<td>Theoretical maximum.</td>
</tr>
<tr>
<td>0.5 - 1.0</td>
<td>An excellent assay.</td>
</tr>
<tr>
<td>0 - 0.5</td>
<td>A marginal assay.</td>
</tr>
<tr>
<td>Less than 0</td>
<td>Too much overlap between the positive and negative controls for the assay to be useful.</td>
</tr>
</table>
</p>
<p>Learn more <a href=\"https://en.wikipedia.org/wiki/Z-factor#Definition\" target=\"_blank\">here</a>.</p>"
)
## Plate quality ----
calcZfactor = function(inDT,
inColMeas = "meas",
inColType = "type",
inWellType = list(ctrlNeg = "Ctrl Neg",
ctrlPos = "Ctrl Pos",
sample = "Sample"),
inScreenType = c("inhibition",
"activation"),
inRobust = F) {
inScreenType = match.arg(inScreenType)
# Aggregate input data
if(inRobust) {
locDTaggr = inDT[,
.(wellMn = median(get(inColMeas)),
wellSd = mad(get(inColMeas))),
by = c(inColType)]
} else {
locDTaggr = inDT[,
.(wellMn = mean(get(inColMeas)),
wellSd = sd(get(inColMeas))),
by = c(inColType)]
}
# Calculate stats for samples
locSampleMn = locDTaggr[get(inColType) == inWellType[["sample"]]][["wellMn"]]
locSampleSd = locDTaggr[get(inColType) == inWellType[["sample"]]][["wellSd"]]
# Calculate stats for the control
if (inScreenType == "inhibition") {
locCtrlMn = locDTaggr[get(inColType) == inWellType[["ctrlNeg"]]][["wellMn"]]
locCtrlSd = locDTaggr[get(inColType) == inWellType[["ctrlNeg"]]][["wellSd"]]
} else {
locCtrlMn = locDTaggr[get(inColType) == inWellType[["ctrlPos"]]][["wellMn"]]
locCtrlSd = locDTaggr[get(inColType) == inWellType[["ctrlPos"]]][["wellSd"]]
}
return(1 - (3*locSampleSd + 3*locCtrlSd)/abs(locSampleMn - locCtrlMn))
}
calcZprime = function(inDT,
inColMeas = "meas",
inColType = "type",
inWellType = list(ctrlNeg = "Ctrl Neg",
ctrlPos = "Ctrl Pos",
sample = "Sample"),
inRobust = F) {
# Aggregate input data
if(inRobust) {
locDTaggr = inDT[get(inColType) %in% c(inWellType[["ctrlNeg"]],
inWellType[["ctrlPos"]]),
.(wellMn = median(get(inColMeas)),
wellSd = mad(get(inColMeas))),
by = c(inColType)]
} else {
locDTaggr = inDT[get(inColType) %in% c(inWellType[["ctrlNeg"]],
inWellType[["ctrlPos"]]),
.(wellMn = mean(get(inColMeas)),
wellSd = sd(get(inColMeas))),
by = c(inColType)]
}
# Calculate stats for controls
locCtrlNegMn = locDTaggr[get(inColType) == inWellType[["ctrlNeg"]]][["wellMn"]]
locCtrlNegSd = locDTaggr[get(inColType) == inWellType[["ctrlNeg"]]][["wellSd"]]
locCtrlPosMn = locDTaggr[get(inColType) == inWellType[["ctrlPos"]]][["wellMn"]]
locCtrlPosSd = locDTaggr[get(inColType) == inWellType[["ctrlPos"]]][["wellSd"]]
return(1 - (3*locCtrlPosSd + 3*locCtrlNegSd)/abs(locCtrlPosMn - locCtrlNegMn))
}
calcSSMD = function(inDT,
inColMeas = "meas",
inColType = "type",
inWellType = list(ctrlNeg = "Ctrl Neg",
ctrlPos = "Ctrl Pos",
sample = "Sample"),
inScreenType = c("inhibition",
"activation"),
inRobust = F) {
inScreenType = match.arg(inScreenType)
# Aggregate input data
if(inRobust) {
locDTaggr = inDT[get(inColType) %in% c(inWellType[["ctrlNeg"]],
inWellType[["ctrlPos"]]),
.(wellMn = median(get(inColMeas)),
wellSd = mad(get(inColMeas))),
by = c(inColType)]
} else {
locDTaggr = inDT[get(inColType) %in% c(inWellType[["ctrlNeg"]],
inWellType[["ctrlPos"]]),
.(wellMn = mean(get(inColMeas)),
wellSd = sd(get(inColMeas))),
by = c(inColType)]
}
# Calculate stats for controls
locCtrlNegMn = locDTaggr[get(inColType) == inWellType[["ctrlNeg"]]][["wellMn"]]
locCtrlNegSd = locDTaggr[get(inColType) == inWellType[["ctrlNeg"]]][["wellSd"]]
locCtrlPosMn = locDTaggr[get(inColType) == inWellType[["ctrlPos"]]][["wellMn"]]
locCtrlPosSd = locDTaggr[get(inColType) == inWellType[["ctrlPos"]]][["wellSd"]]
if (inScreenType == "inhibition") {
locRes = (locCtrlPosMn - locCtrlNegMn) / sqrt(locCtrlPosSd^2 + locCtrlNegSd^2)
} else {
locRes = (locCtrlNegMn - locCtrlPosMn) / sqrt(locCtrlPosSd^2 + locCtrlNegSd^2)
}
return(locRes)
}
## Plate normalisations ----
calcZscore = function(inDT,
inColMeas = "meas",
inColType = "type",
inWellType = list(ctrlNeg = "Ctrl Neg",
ctrlPos = "Ctrl Pos",
sample = "Sample"),
inRobust = F) {
if(inRobust) {
locDTaggr = inDT[get(inColType) == inWellType[["sample"]],
.(wellMn = median(get(inColMeas)),
wellSd = mad(get(inColMeas)))]
} else {
locDTaggr = inDT[get(inColType) == inWellType[["sample"]],
.(wellMn = mean(get(inColMeas)),
wellSd = sd(get(inColMeas)))]
}
inDT[,
zScore := (get(inColMeas) - locDTaggr[["wellMn"]]) / locDTaggr[["wellSd"]]]
return(inDT)
}
calcNPI = function(inDT,
inColMeas = "meas",
inColType = "type",
inWellType = list(ctrlNeg = "Ctrl Neg",
ctrlPos = "Ctrl Pos",
sample = "Sample"),
inScreenType = c("inhibition",
"activation"),
inRobust = F) {
inScreenType = match.arg(inScreenType)
# Aggregate input data
if(inRobust) {
locDTaggr = inDT[get(inColType) %in% c(inWellType[["ctrlNeg"]],
inWellType[["ctrlPos"]]),
.(wellMn = median(get(inColMeas))),
by = c(inColType)]
} else {
locDTaggr = inDT[get(inColType) %in% c(inWellType[["ctrlNeg"]],
inWellType[["ctrlPos"]]),
.(wellMn = mean(get(inColMeas))),
by = c(inColType)]
}
# Calculate stats for controls
locCtrlNegMn = locDTaggr[get(inColType) == inWellType[["ctrlNeg"]]][["wellMn"]]
locCtrlPosMn = locDTaggr[get(inColType) == inWellType[["ctrlPos"]]][["wellMn"]]
if (inScreenType == "activation") {
inDT[,
NPI := 100 *(locCtrlPosMn - get(inColMeas)) / (locCtrlPosMn - locCtrlNegMn)]
} else {
inDT[,
NPI := 100 *(locCtrlNegMn - get(inColMeas)) / (locCtrlNegMn - locCtrlPosMn)]
}
return(inDT)
}
## Hit selection ----
calcHits = function(inDT,
inColMeas = "meas",
inColType = "type",
inWellType = list(ctrlNeg = "Ctrl Neg",
ctrlPos = "Ctrl Pos",
sample = "Sample"),
inScreenType = c("inhibition",
"activation"),
inThr = -3,
inRobust = F) {
inScreenType = match.arg(inScreenType)
if(inRobust) {
locDTaggr = inDT[get(inColType) == inWellType[["sample"]],
.(wellMn = median(get(inColMeas)),
wellSd = mad(get(inColMeas)))]
} else {
locDTaggr = inDT[get(inColType) == inWellType[["sample"]],
.(wellMn = mean(get(inColMeas)),
wellSd = sd(get(inColMeas)))]
}
inDT[,
zScore := (get(inColMeas) - locDTaggr[["wellMn"]]) / locDTaggr[["wellSd"]]]
if (inScreenType == "inhibition") {
inDT[,
hits := zScore < inThr]
} else {
inDT[,
hits := zScore > inThr]
}
return(inDT)
}