-
Notifications
You must be signed in to change notification settings - Fork 4
/
pipeline.Rmd
457 lines (369 loc) · 15.4 KB
/
pipeline.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
---
title: "Re-mining pipeline"
output: html_document
---
```{r, echo=F, warning=F}
screen <- "twoPlates"
channel <- "nucleus-dapi"
```
> **SCREEN: `r screen`**
> **CHANNEL: `r channel`**
------------------------------------------------------------------------------------------------------------------------------
### **Step 0: Set up the environment management**
------------------------------------------------------------------------------------------------------------------------------
- Screen settings.
```{r, echo=F}
if (screen == "mockScreen")
{
INDEX_START_FEATURES <- 15
}
if (screen == "secretionPilot")
{
INDEX_START_FEATURES <- 16
}
if (screen == "twoPlates")
{
INDEX_START_FEATURES <- 14
}
```
- Load constants, paths and functions.
```{r, echo=F, message=F, warning=F}
source("includes/includeConstants.R")
source("includes/includePaths.R")
source("includes/includeFunctions.R")
library(ggplot2, quietly=T)
library(plotly, quietly=T)
```
------------------------------------------------------------------------------------------------------------------------------
### **Step 1: Feature extraction from images (WND-CHARM)**
------------------------------------------------------------------------------------------------------------------------------
- Data file must have the following column structure:
- First part: metadata, where column order is not relevant, except for *class* that must be at the end.
- Second part: undetermined number of features from WND-CHARM in any order.
```{r, echo=F}
dataPath <- file.path(pathTo_data, paste0(screen,"_db_",channel,".txt"))
data <- read.csv(dataPath, header=T, sep="\t", check.names=F)
```
- Standardize naming: TEST CLASS WITH NO PHENOTYPE.
```{r, echo=F}
# Elements with no phenotype (from test images) should have as label "NO_PH" and IC=0
if (length(data[data$specificCMPO==LABEL_NO_PHENOTYPE,]$IC) > 0)
{
data[data$specificCMPO==LABEL_NO_PHENOTYPE,]$IC <- 0
levels(data$tag) <- c(levels(data$tag), LABEL_NO_PHENOTYPE_TAG)
data[data$specificCMPO==LABEL_NO_PHENOTYPE,]$tag <- LABEL_NO_PHENOTYPE_TAG
}
```
- Standardize labels: CONTROL CLASS.
```{r, echo=F}
# Those with empty gene are no-targeted genes
if (length(data[data$symbol=="",]$symbol) > 0)
{
data[data$symbol=="",]$symbol <- LABEL_CONTROL_CLASS
}
```
- Detect the appropiate controls.
```{r, message=F, echo=F}
# Real controls
controlSiRNA <- c("103860","251283","scramble")
# There are controls in the dataset that are not controls
markedAsControlsInTheDataset <- data[data$class==LABEL_CONTROL_CLASS,]
# Those siRNA that are NOT in the real controls will be removed
wrongControls <- subset(markedAsControlsInTheDataset, !(siRNA %in% controlSiRNA))
# Remove wrong controls from the dataset
data <- utils.differenceOfDataFrames(data, wrongControls)
```
- Standardize data: CONTROL CLASS.
```{r, echo=F}
if (nrow(data[data$symbol=="",]) > 0)
{
data[data$symbol==LABEL_CONTROL_CLASS,]$specificName <- LABEL_NO_PHENOTYPE
data[data$symbol==LABEL_CONTROL_CLASS,]$specificCMPO <- LABEL_CONTROL_CLASS
data[data$symbol==LABEL_CONTROL_CLASS,]$EnsemblID <- LABEL_CONTROL_CLASS
data[data$symbol==LABEL_CONTROL_CLASS,]$IC <- 0
data[data$symbol==LABEL_CONTROL_CLASS,]$tag <- LABEL_CONTROL_TAG
data[data$symbol==LABEL_CONTROL_CLASS,]$class <- LABEL_CONTROL_CLASS
}
```
- Build an unique ID to identify images (*data$id*): **gene_siRNA_plate_replicate_well**.
```{r, echo=F}
data$id <- paste(data$symbol, data$siRNA, data$Plate, data$rep, data$Well, sep="_")
data <- data[c(ncol(data), 1:(ncol(data)-1))]
INDEX_START_FEATURES <- INDEX_START_FEATURES + 1
colsMetadata <- colnames(data)[1:(INDEX_START_FEATURES-1)]
data.metadata <- data[c(colsMetadata[colsMetadata!="class"],colsMetadata[colsMetadata=="class"])]
data.features <- data[c(INDEX_START_FEATURES:ncol(data))]
data <- cbind(data.metadata, data.features)
rm(data.metadata, data.features)
```
- Number of images for each siRNA.
```{r, echo=F}
sirna_freq <- as.data.frame(table(data$siRNA))
colnames(sirna_freq) <- c("siRNA", "#images")
```
- Identify positive controls with id + "**********".
```{r, echo=F}
positiveControls <- subset(sirna_freq, `#images`>5)$siRNA
# Remove negative controls
positiveControls <- positiveControls[positiveControls!="103860"]
# Mark siRNA and gene names
data[data$siRNA %in% positiveControls,]$id <- paste0(data[data$siRNA %in% positiveControls,]$id,"**********")
```
- Save data.
```{r, echo=F}
standardizedDataPath <- file.path(pathTo_data, paste0(screen,"_standardized_db_",channel,".txt"))
write.table(data, file=standardizedDataPath, sep="\t", col.names=T, row.names=T, append=F, quote=F)
```
- Set colors for plots.
```{r, echo=F}
colorCodes <- unique(data[c("id","tag")])
colnames(colorCodes) <- c("label","tag")
```
- Structure pointers to adress data.
```{r, echo=F}
dataColNames <- colnames(data)
indexID <- match("id", dataColNames)
indexClass <- match("class", dataColNames)
indexRangeFeature <- INDEX_START_FEATURES:ncol(data)
```
> THERE ARE **`r nrow(data)`** IMAGES.
```{r, echo=F}
table(data$class)
table(data$tag)
table(data$specificCMPO)
```
------------------------------------------------------------------------------------------------------------------------------
### **Step 2: Feature selection**
------------------------------------------------------------------------------------------------------------------------------
#### **2.1. Initial filtering of the features**
- Select only the WND-CHARM features (without metadata).
```{r, echo=F}
originalFeatures <- data[,indexRangeFeature]
```
- Remove constant columns (standard deviation=0).
```{r, echo=F}
features_noConstants <- stats.remove_constantColumns_fromDF(originalFeatures)
featureNamesToBeSelected <- names(features_noConstants)
```
- Update data and pointers.
```{r, echo=F}
metadata <- data[1:(INDEX_START_FEATURES-1)]
features <- data[featureNamesToBeSelected]
data <- data.frame(metadata, features, check.names=F)
indexRangeFeature <- c((ncol(metadata)+1):ncol(data))
```
#### **2.2. PCA for feature selection**
- Remove positive controls before PCA.
```{r, echo=F}
data <- data[!(data$siRNA %in% positiveControls),]
```
- Principal Component Analysis.
```{r, echo=F}
pca <- prcomp(data[indexRangeFeature], scale.=T, center=T)
```
- Eigenvalues of the PCs.
```{r, echo=F}
pcaEigenvalues <- pca$sdev^2
```
- Select the number of eigenvalues given by the elbow.
```{r, echo=F}
numberOfSelectedFeatures <- stats.get_elbow_ofDistribution(1:length(pcaEigenvalues), pcaEigenvalues)
```
- Plot eigenvalues.
```{r, fig.height=4, fig.width=8, message=F, echo=F}
plotEigenvalues <- as.data.frame(pcaEigenvalues)
plotEigenvalues$eigenvalue <- 1:length(pcaEigenvalues)
colnames(plotEigenvalues)[1] <- "value"
library(viridis)
g <- ggplot(plotEigenvalues, aes(x=eigenvalue, y=value))
g <- g + geom_point(shape=20, size=1)
g <- g + geom_vline(xintercept=numberOfSelectedFeatures, colour="red")
g <- g + ggtitle(paste0("VARIANCES [1, ",nrow(plotEigenvalues),"]"))
g <- g + scale_color_viridis(name="", discrete=T, end=0, begin=1)
g <- g + theme_bw()
ggplotly(g)
pcaEigenvalues <- pcaEigenvalues[1:numberOfSelectedFeatures]
```
| Stage | # features |
| ----------------------------------------------- |:---------------------------------:|
| After WND-CHARM feature extraction | `r ncol(originalFeatures)` |
| After cleaning constant columns | `r ncol(features_noConstants)` |
| After PCA | `r numberOfSelectedFeatures` |
| | |
- Select `r numberOfSelectedFeatures` PCA features.
```{r, echo=F}
filteredFeatures <- as.data.frame(pca$x[,1:numberOfSelectedFeatures])
metadata <- data[1:(INDEX_START_FEATURES-1)]
```
- Update data and pointer with the new PCA features.
```{r, echo=F}
data <- data.frame(metadata, filteredFeatures, check.names=F)
indexRangeFeature <- INDEX_START_FEATURES:ncol(data)
```
- Save `r numberOfSelectedFeatures` PCA features.
```{r, echo=F}
PCAfeaturesFile <- file.path(pathTo_PCAs, paste0(screen,"_db_PCAs_",channel,".txt"))
write.table(data, file=PCAfeaturesFile, sep="\t", col.names=T, row.names=T, append=F, quote=F)
```
- Save PCA loadings.
```{r, echo=F}
loadingFile <- file.path(pathTo_PCAs, paste0(screen,"_PCAloadings_",channel,".txt"))
write.table(pca$rotation[,1:numberOfSelectedFeatures], file=loadingFile, sep="\t", col.names=T, row.names=T, append=F, quote=F)
```
------------------------------------------------------------------------------------------------------------------------------
### **Step 3: Quality control**
------------------------------------------------------------------------------------------------------------------------------
- Split data into control and test.
```{r, echo=F}
controlClass <- subset(data, data$class==LABEL_CONTROL_CLASS)
testClass <- subset(data, data$class!=LABEL_CONTROL_CLASS)
rownames(controlClass) <- controlClass$id
rownames(testClass) <- testClass$id
controlClassFeatures <- controlClass[indexRangeFeature]
testClassFeatures <- testClass[indexRangeFeature]
```
> THERE ARE **`r nrow(testClass)`** TEST IMAGES AND **`r nrow(controlClass)`** CONTROL IMAGES.
#### **3.1. Control Class definition (CC)**
- Distance between the control class images.
```{r}
covMatrix <- diag(pcaEigenvalues)
distControlImagesToCC <- mahalanobis(controlClassFeatures, colMeans(controlClassFeatures), covMatrix, tol=1e-20, inverted=F) # inverted=F when the cov matrix is not inverted outside
```
- Find a threshold distance with **25%** chance of missing a control.
```{r}
# Mahalanobis distance (d^2) is Chi-squared distributed.
percControlsRejected <- 25
alpha <- percControlsRejected/100
degreesOfFreedom <- numberOfSelectedFeatures
t <- qchisq(alpha, df=degreesOfFreedom, lower.tail=F)
```
> THRESHOLD DISTANCE = **`r t`**
- Select the control images outside of the control boundaries.
```{r, echo=F}
controlImages_farFromControls <- distControlImagesToCC[distControlImagesToCC>t]
```
| CONTROL IMAGES | # images |
| --------------------------------------------|:-----------------------------------------:|
| Total number of control images | `r nrow(controlClass)` |
| Number of control images far from the CC | `r length(controlImages_farFromControls)` |
| | |
| **Number of remaining control images** | **`r nrow(controlClass) -length(controlImages_farFromControls)`** |
| | |
- Update the CC to have only those controls images close to the center of the CC.
```{r, echo=F}
controlClass <- controlClass[!(controlClass$id %in% names(controlImages_farFromControls)),]
controlClassFeatures <- controlClass[indexRangeFeature]
```
#### **3.2. Distance of Test Images to the Control Class**
- Mahalanobis distance of the test images to CC (note that this control class has been updated).
```{r, echo=F, message=F}
distTestImagesToCC <- mahalanobis(testClassFeatures, colMeans(controlClassFeatures), covMatrix, tol=1e-20, inverted=F)
```
- Reject test images with the Mahalanobis distance within the boundaries of the CC.
```{r, echo=F}
testImages_similarToControls <- distTestImagesToCC[distTestImagesToCC<=t]
```
| TEST IMAGES | # images |
| --------------------------------------------|:-----------------------------------------:|
| Total number of test images | `r nrow(testClassFeatures)` |
| Number of test images similar to controls | `r length(testImages_similarToControls)` |
| | |
| **Number of remaining test images** | **`r nrow(testClassFeatures)-length(testImages_similarToControls)`** |
| | |
#### **3.3. Histograms of control and test images**
```{r, fig.height=3, fig.width=9, message=F, echo=F}
library(viridis)
g <- qplot(distControlImagesToCC, binwidth=1, xlab="Distance of the control images to the center of the Control Class")
g <- g + geom_vline(xintercept=t, colour="green")
g <- g + scale_color_viridis(name="", discrete=T, end=0, begin=1)
g <- g + theme_bw()
ggplotly(g)
g <- qplot(distTestImagesToCC, binwidth=10, xlab="Distance of the test images to the center of the Control Class")
g <- g + geom_vline(xintercept=t, colour="green")
g <- g + scale_color_viridis(name="", discrete=T, end=0, begin=1)
g <- g + theme_bw()
ggplotly(g)
```
#### **3.4. Data reduction**
- Reject ALL CONTROLS (**-`r nrow(controlClassFeatures)`**):
Starting with **`r nrow(data)`** images (TI + CC)...
```{r}
data <- data[data$class!=LABEL_CONTROL_CLASS,]
```
... we get **`r nrow(data)`** (TI + CC).
```{r, echo=F}
table(data$tag)
```
- Reject TEST IMAGES close to controls (**-`r length(testImages_similarToControls)`**):
Starting with **`r nrow(data)`** images (TI + CC)...
```{r}
testImagesToReject <- as.factor(names(testImages_similarToControls))
data <- data[!data$id %in% testImagesToReject,]
```
... we get **`r nrow(data)`**.
```{r, echo=F}
table(data$tag)
```
- Reproducibility:
Starting with **`r nrow(data)`** images (TI + CC)...
```{r}
X <- 2
data <- images.remove_siRNAWithLessThanXImagesLeft(data, X)
```
... we get **`r nrow(data)`**.
```{r, echo=F}
table(data$tag)
utils.sort_vector(table(data$specificName))
```
- Save data.
```{r, echo=F}
fileData <- file.path(pathTo_QC, paste0(screen,"_db_qualityControlled.txt"))
write.table(data, file=fileData, sep="\t", col.names=F, row.names=T, append=F, quote=F)
```
#### **3.5. Plot the remaining images on the first two PCs.**
```{r, fig.height=10, fig.width=10, echo=F}
library(viridis)
g <- ggplot(data, aes(x=PC1, y=PC2, color=tag, text=id))
g <- g + geom_point(shape=20, size=1.5)
g <- g + scale_color_viridis(name="", discrete=T, end=0, begin=1)
g <- g + theme_bw()
ggplotly(g)
```
### **Step 4: Similarity between images**
- Cosine similarity between PCA features of the test images.
```{r, echo=F}
features <- data[c(INDEX_START_FEATURES:ncol(data))]
```
- Similarity matrix.
```{r, echo=F, warning=F, message=F}
simMatrix <- measures.get_cosineSimilarityMatrix(features)
rownames(simMatrix) <- colnames(simMatrix) <- data$id
```
- Reorder matrix by gene to have them together in the heat map.
```{r, echo=F}
newOrder <- rownames(simMatrix)[order(rownames(simMatrix))]
simMatrix <- simMatrix[newOrder, newOrder]
```
- Save heat map of the similarity matrix between images.
```{r, echo=F, warning=F, message=F}
simList <- utils.convert_MatrixToListOfPairs(simMatrix)
library(viridis)
gg <- ggplot(simList, aes(x=X1, y=X2, fill=value))
gg <- gg + geom_tile(color="white", size=0.15)
gg <- gg + scale_fill_viridis(name="similarity")
gg <- gg + coord_equal()
gg <- gg + labs(x=NULL, y=NULL)
gg <- gg + theme(axis.text.x = element_text(angle=90, hjust=1))
gg <- gg + theme(axis.ticks=element_blank())
gg <- gg + theme(axis.text=element_text(size=7))
gg <- gg + theme(legend.title=element_text(size=12))
gg <- gg + theme(legend.text=element_text(size=10))
ggplotly(gg, width=1000, height=1000)
ggsave(gg, width=20, height=20, filename=file.path(pathTo_plots,"cosineSimilarityBetweenImages.pdf"))
```
- Performance:
```{r, cache=FALSE, echo=F}
proc.time()
library(pryr)
mem_used()
```