forked from broadinstitute/imaging_metric_comparison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBBBC022_waterfallsPlot.Rmd
216 lines (167 loc) · 5.69 KB
/
BBBC022_waterfallsPlot.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
---
title: "Hash mark plot"
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
library(magrittr)
library(dplyr)
library(stringr)
library(reshape2)
library(ggplot2)
```
## Import data
```{r import data, message=FALSE}
filename <- "Hit_pearson_5925.rds"
pf <- readRDS(file.path("..", "..", "input", "BBBC022_2013", "old", filename))
```
## Metadata
```{r MOAs}
# import MOAs data
moa <-
read.csv("../../input/BBBC022_2013/MOAs.csv", na.strings = c("", "NA")) %>%
mutate_if(is.factor, as.character) # has to do that to duplicate rows where multiple MOA
moa %<>%
filter(!is.na(MOA_group)) %>% # remove rows where moa = NA
rename(Image_Metadata_SOURCE_COMPOUND_NAME = Name) # rename compound name
# remove duplicate compounds
moa$Image_Metadata_SOURCE_COMPOUND_NAME <- toupper(moa$Image_Metadata_SOURCE_COMPOUND_NAME) # put compound names in upper character (to make it comparable)
moa %<>%
group_by(Image_Metadata_SOURCE_COMPOUND_NAME) %>%
slice(1) %>%
ungroup
# duplicate rows where multiple MOA associated to one compounds
for (i in 1:nrow(moa)){
# if there are more than 1 moa associated
if (str_detect(moa$MOA_group[i], ",")){
t1 <- str_trim(str_split(moa$MOA_group[i], ",")[[1]])
moa$MOA_group[i] <- t1[1]
new.row <- moa[i,]
new.row$MOA_group <- t1[2]
moa <- rbind(moa, new.row)
}
}
```
```{r compound-id}
# mapping IDs-Compounds: give access to the compound knowing the IDs
id.cmpds <-
pf$data %>%
dplyr::select(one_of(c("Image_Metadata_BROAD_ID","Image_Metadata_SOURCE_COMPOUND_NAME"))) %>% # select ID and Compound
mutate_if(is.character, funs(toupper)) %>% # same compound can be writen in upper and lower case and looks different# be sure it is a dataframe
group_by(Image_Metadata_SOURCE_COMPOUND_NAME) %>%
slice(1) %>%
ungroup
```
```{r metadata}
# metadata: compound, MOA, target, ID
metadata <-
id.cmpds %>%
left_join(., moa, by = "Image_Metadata_SOURCE_COMPOUND_NAME")
# select only rows that have a MOA
metadata %<>% filter(!is.na(MOA_group))
# select MOA that are appearing more than once (meaning at least two compounds are related to it)
n.MOA <- table(metadata$MOA_group) %>% as.data.frame() %>% filter(Freq > 1) #### filter(Freq != 1)
metadata %<>% filter(MOA_group %in% n.MOA$Var1)
```
## Profiles of the compound
Do the average along replicates.
```{r compound profile}
# average along replicate (but first select ID for unique compound)
pf.cmpds <-
pf$data %>%
filter(Image_Metadata_BROAD_ID %in% metadata$Image_Metadata_BROAD_ID) %>% # select ID that have a unique compound
group_by(Image_Metadata_BROAD_ID) %>%
summarise_each(funs(mean(., na.rm=TRUE)), -c(Image_Metadata_SOURCE_COMPOUND_NAME, Well, Plate)) %>% # mean along replicate
ungroup %>%
as.data.frame()
# keep track of ID of the compounds
row.names(pf.cmpds) <- pf.cmpds$Image_Metadata_BROAD_ID
```
## Combination profile + metadata
```{r profile + metadata}
# Attention: since some compounds have more than one MOAs, few rows have same compounds name.
pf.cmpd.meta <-
metadata %>%
dplyr::left_join(., pf.cmpds, by = "Image_Metadata_BROAD_ID") %>%
as.data.frame
dim(pf.cmpd.meta)
```
## Number of MOA in common for each compounds pair
```{r}
# binary indicator matrix of ID vs MOA
n.moa.ID <-
pf.cmpd.meta %>%
select(MOA_group, Image_Metadata_BROAD_ID) %>%
table %>%
as.data.frame.matrix %>%
as.matrix
# number of moa in common for each ID pairs
n.moa.cmpd.pair <-
t(n.moa.ID) %*% n.moa.ID %>% # calculate matrix compound ID - compound ID relation
melt(.) %>% # transform matrix into column
filter(Var1 != Var2)
```
## Correlation compound-compound
```{r correlation cmpd-cmpd}
# correlation compound-compound
cor.cmpd <-
pf.cmpds[, pf$feat_cols] %>%
as.matrix() %>%
t() %>%
cor()
```
```{r combining correlation with moa info}
cor.cmpd.pair <-
melt(cor.cmpd) %>%
rename(cmpd1 = Var1, cmpd2 = Var2, corr = value) %>% # rename columns
filter(cmpd1 != cmpd2) %>% # remove column were same compound
full_join(.,
metadata,
by = c("cmpd1" = "Image_Metadata_BROAD_ID")) %>%
full_join(.,
metadata,
by = c("cmpd2" = "Image_Metadata_BROAD_ID")) %>%
mutate(value = ifelse(MOA_group.x == MOA_group.y, 1, 0))
```
```{r waterfall plot}
waterfall_plot <- function(moa.name){
tA <-
cor.cmpd.pair %>%
group_by(cmpd1) %>%
filter(MOA_group.x %in% moa.name) %>%
select(-one_of("MOA_group.x", "MOA_group.y")) %>%
arrange(desc(value)) %>%
group_by(cmpd1, cmpd2) %>%
slice(1) %>%
ungroup %>%
select(-cmpd2) %>%
group_by(cmpd1) %>%
#mutate(corr2 = abs(corr)) %>% # TODO: REMOVE
arrange(desc(corr)) %>% #arrange(desc(corr2)) %>% #
mutate(id = row_number()) %>%
ungroup
# sort the plot to do a waterfall (smaller rank to higher rank)
tA2 <-
tA %>%
group_by(Image_Metadata_SOURCE_COMPOUND_NAME.x) %>%
filter(value == 1) %>%
summarise(rank_corr = sum(id)) %>%
arrange(rank_corr)
tA$Image_Metadata_SOURCE_COMPOUND_NAME.x <- factor(tA$Image_Metadata_SOURCE_COMPOUND_NAME.x, levels=tA2$Image_Metadata_SOURCE_COMPOUND_NAME.x)
wf <- ggplot(data = tA, aes(x = Image_Metadata_SOURCE_COMPOUND_NAME.x, y = id)) +
geom_tile(aes(fill = value)) +
scale_y_reverse() +
labs(title = moa.name, x = "compound name", y = "compound ranked by correlation") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
guides(fill=FALSE)
filename = paste("../results/manual/waterfall_", moa.name,".png", sep="")
ggsave(filename, width = 7, height = 5)
return(wf)
}
moa.list <- unique(cor.cmpd.pair$MOA_group.x)
for(m in moa.list){
wf_plot <- waterfall_plot(m)
print(wf_plot)
}
```