-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhit_selection_distance.Rmd
258 lines (193 loc) · 7.42 KB
/
hit_selection_distance.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
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
---
title: "Replicate Distance"
output:
html_document: default
pdf_document: default
word_document: default
---
The goal is to do some Hit Selection. Selecting the compound that have effects, that are showing a phenotype.
Find the median replicate distance. The smaller the distance, the more correlated the replicates are.
Use different type of distance metric:
- Euclidean distance: usual distance between two vectors (L2 norm)
- Maximum distance: maximum distance between two components
- Manhattan distance: absolute distance between two vector (L1 norm)
## Data
The input data is a 7680 by 803 matrix.
There are 7680 different observations and 799 features (extracted with CellProfiler).
Each compound (1600 different) has 4 replicates. The negative control has 1280 replicates.
20 plates with 384 wells in each plate.
```{r setup, include=FALSE}
# all usefull libraries
library(magrittr)
library(dplyr)
library(ggplot2)
library(foreach)
library(doMC)
library(stringr)
library(tidyverse)
```
```{r import data with feature selection, message=FALSE, eval=FALSE}
profiles <-
readr::read_csv(file.path("..", "..", "input", "BBBC022_2013", "BBBC022_2013_sel_feat.csv")) # 7680x(nfeat+metadata)
variables <-
names(profiles) %>% str_subset("^Cells_|^Cytoplasm_|^Nuclei_") # nfeat
metadata <-
names(profiles) %>% str_subset("^Metadata_") # metadata
# Remove the negative control (DMSO) from the data
profiles %<>%
filter(!Metadata_broad_sample %in% "DMSO") # 6400xnfeat
```
```{r import data no feature selection, message=FALSE}
profiles <-
list.files("../../backend/BBBC022_2013/",
pattern = "*_normalized.csv",
recursive = T,
full.names = T) %>%
map_df(read_csv)
dim(profiles)
variables <-
names(profiles) %>% str_subset("^Cells_|^Cytoplasm_|^Nuclei_")
metadata <-
names(profiles) %>% str_subset("^Metadata_")
profiles %<>%
cytominer::select(
sample =
profiles %>%
filter(Metadata_pert_type == "control"),
variables = variables,
operation = "variance_threshold"
)
variables <-
names(profiles) %>% str_subset("^Cells_|^Cytoplasm_|^Nuclei_")
# Remove the negative control (DMSO) from the data
profiles %<>%
filter(!Metadata_broad_sample %in% "DMSO") # 6400xnfeat
```
## Parameter
```{r parameter}
# method of distance matrix computation: "euclidean" (default), "maximum", "manhattan"
dist.method <- "euclidean"
# number of data to make the non replicate correlation
N <- 5000
# seed for the reproducibility
set.seed(42)
# number of CPU cores for parallelization
registerDoMC(7)
```
## Separation of data
Separation is made according to the compound that was added.
Image_Metadata_BROAD_ID = gives the ID of the compound that was added.
```{r separation of data}
# find the different compounds
IDs <- distinct(profiles, Metadata_broad_sample)
dim(IDs)
```
## Distance of the data
Calculate the distance of the replicate for each compound.
```{r distance}
# loop over all IDs
comp.dist.median <- foreach(i = 1:length(IDs$Image_Metadata_BROAD_ID), .combine=cbind) %dopar% {
#filtering to choose only for one compound
#comp <-
# filter(profiles, Metadata_broad_sample %in% IDs$Metadata_broad_sample[i])
#comp <-
# comp[, variables] %>%
# as.matrix()
#filtering to choose only for one compound
comp <-
filter(pf$data, Image_Metadata_BROAD_ID %in% IDs$Image_Metadata_BROAD_ID[i])
comp <-
comp[, pf$feat_cols] %>%
as.matrix()
# distance of the features
comp.dist <- dist(comp, method = dist.method) %>% as.matrix()
# median of the distances
comp.dist.median <- median(comp.dist[lower.tri(comp.dist)],na.rm=TRUE)
}
hist(comp.dist.median,
main="Histogram for Median Replicate Distance",
xlab="Median Replicate Distance")
```
## Thresholding of poor replicate correlation
H0: median non replicate correlation.
The Null distribution is estimated by finding the median correlation of non replicates.
Select randomly 4 replicates each coming for a different compound and calculate the median correlation.
Repeat this N times to get a distribution.
Finally estimate a threshold (5th percentile) to filter out compounds with poor replicate correlation.
```{r non replicate distance parallel}
start.time <- Sys.time()
# set seed for reproducibility
set.seed(42)
# random sequence for reproducibility
a <- sample(1:10000, N, replace=F)
# loop over N times to get a distribution
random.replicate.dist.median <- foreach(i = 1:N, .combine=cbind) %dopar% {
# set seed according to random sequence
set.seed(a[i])
# group by IDs
# sample fixed number per group -> choose 4 replicates randomly from different group
#random.replicate <-
# profiles %>%
# group_by(Metadata_broad_sample) %>%
# sample_n(1, replace = FALSE) %>%
# ungroup(random.replicate)
#random.replicate <- sample_n(random.replicate, 4, replace = FALSE)
#comp <- random.replicate[,variables] %>%
# as.matrix()
random.replicate <-
pf$data %>%
group_by(Image_Metadata_BROAD_ID) %>%
sample_n(1, replace = FALSE) %>%
ungroup(random.replicate)
random.replicate <- sample_n(random.replicate, 4, replace = FALSE)
comp <- random.replicate[,pf$feat_cols] %>%
as.matrix()
# distance of the features
random.replicate.dist <- dist(comp, method = dist.method)
# median of the non replicate distance
random.replicate.dist.median <- median(random.replicate.dist[lower.tri(random.replicate.dist)],na.rm=TRUE)
}
# histogram plot
hist(random.replicate.dist.median,
main="Histogram for Non Replicate Median Correlation",
xlab="Non Replicate Median Correlation")
# threshold to determine if can reject H0
thres <- quantile(random.replicate.dist.median, .05)
print(thres)
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
```
## Hit Selection
Select strong replicate correlation comparing with the 95th percentile of the Null Distribution.
```{r Hit Selection}
# find indices of replicate median correlation > threshold
inds <- which(comp.dist.median < thres)
# find values of the median that are hit selected
hit.select <- comp.dist.median[inds]
# find component that are hit selected
hit.select.IDs <- IDs$Metadata_broad_sample[inds]
# ratio of strong median replicate correlation
high.median.dist <- length(hit.select)/length(comp.dist.median)
print(high.median.dist)
```
## Results
| Method | Pearson | Spearman | Kendall | Euclidean | Maximum | Manhattan |
| --------- | ------- | -------- | ------- | --------- | ------- | --------- |
| N = 1000 | 0.5819 | -------- | ------- | --------- | ------- | --------- |
| N = 5000 | 0.5806 | 0.5519 | 0.5419 | 0.4606 | 0.3612 | 0.4875 |
| N = 10000 | 0.5850 | -------- | ------- | --------- | ------- | --------- |
- Difference between Pearson and Spearman correlation seem not to be very significant (no statistical test was performed).
- Distance method compared to correlation metric gives a lower ratio of hit selection (more or less 10% lower).
```{r plot}
type1 <- rep("Non-replicate Distance", length(random.replicate.dist.median))
type2 <- rep("Replicate Distance", length(comp.dist.median))
type <- c(type1, type2)
distance <- c(random.replicate.dist.median, comp.dist.median)
dat <- data.frame(distance = distance, type = type)
ggplot(dat, aes(x=distance, fill=type, y=..density../sum(..density..))) +
geom_histogram(binwidth=5, alpha=.4, position="identity") +
xlim(0, 400) +
geom_vline(xintercept=thres, colour = "red", alpha = 0.4) +
ylab("density")
```