-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcancer_similarity.R
168 lines (137 loc) · 6.17 KB
/
cancer_similarity.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
# ------------- cancer_similarity.R -----------------
# use similarity indexes to find the differences
# by comparing between the chaperons in each cancer
# --------------------------------------------------- #
#-------- includes --------
library(readxl)
library(vegan)
library(ggplot2)
library(reshape2)
library(tidyr)
library(dplyr)
library(igraph)
source("functions.r")
#-------- functions --------
long_format_from_dist_triangle <- function(similr, cancer_name) {
# this function gets a distances triangle and turns it into a long format
x <- as.matrix(similr)
r_ind <- cbind(as.data.frame(rownames(x)), 1:15)
names(r_ind) <- c("chap_row", "row")
c_ind <- cbind(as.data.frame(rownames(x)), 1:15)
names(c_ind) <- c("chap_col", "col")
ind <- which(upper.tri(x, diag = FALSE), arr.ind = TRUE)
sims_cancer <- as.data.frame(cbind(ind, value=x[ind])) %>%
left_join(y=r_ind, by=c("row")) %>%
left_join(y=c_ind, by=c("col")) %>%
select(row=chap_row, col=chap_col, similarity=value) %>%
add_column(cancer=cancer_name)
return(sims_cancer)
}
#only one cancer at a time
print_cancer_igraph <- function(cancer_name, c_att, inters) {
# prepare vertices and edges
intr_vals <- inters %>% filter(cancer == cancer_name) %>% select(from, to, prots)
cancertail <- in_tail %>% filter(cancer == cancer_name) %>%
left_join(intr_vals, by=c("row"="from","col"="to")) %>%
left_join(intr_vals, by=c("row"="to","col"="from"))
cancertail[is.na(cancertail)] <- 0
cancertail <- cancertail %>% mutate(prots=prots.x+prots.y) %>%
select(chap1 = row, chap2 = col, cancer, prots)
cancertail
# plot the graph
cancer_igraph <- igraph::graph_from_data_frame(d = cancertail, vertices = c_att, directed = FALSE)
plot.igraph(cancer_igraph, axes = FALSE, vertex.frame.color = NA,
#vertex.label = V(g)$name, vertex.label.color = "gray20",
vertex.size = 40, vertex.size2 = 30,
vertex.color = chap_attrib$colour, #"gray90", vertex.frame.color = "gray20",
vertex.shape = chap_attrib$shape,
#edge.arrow.size=0.5, edge.color=col,
edge.width = cancertail$prots / 30,
#edge.curved = T,
main = cancer_name,
margin = c(-0.05,-0.05,-0.07,-0.1),
layout = as.matrix(chap_attrib[2:3]))
}
#-------- load the networks from an excel file, and calculate similarity --------
excel_path <- "HPC/binari_validated_corrs.xlsx"
# do the convert for every cancer
sheet_names <- excel_sheets(excel_path)
networks <- list()
sim_triangles <- list()
all_simlrs <- matrix(0, nrow = 105, ncol = 0)
all_simlrs_long <- NULL
for (name in sheet_names) {
x <- as.data.frame(read_excel(excel_path, sheet = name, col_names = TRUE))
x2 <- x[,-1]
rownames(x2) <- x[,1]
networks[[name]] <- x2
# generate similarity between the chaperons in this cancer
res <- vegdist(x2, method="jaccard")
simlr <- 1-res
all_simlrs <- cbind(all_simlrs, simlr)
sim_triangles[[name]] <- simlr
long <- long_format_from_dist_triangle(simlr, name)
all_simlrs_long <- rbind(all_simlrs_long, long)
}
colnames(all_simlrs) <- sheet_names
write.csv(all_simlrs, file = "output/jaccard_values_per_cancer.csv", row.names = FALSE)
write.csv(all_simlrs_long, file = "output/jaccard_values_per_cancer_long_format.csv", row.names = FALSE)
#-------- visualize the results --------
all_simlrs <- read.csv("output/jaccard_values_per_cancer.csv")
# melt the data to a long format
mlt_sim <- as.data.frame(melt(all_simlrs))
g1 <- ggplot(mlt_sim, aes(x=variable,y=value))+
geom_boxplot(outlier.colour="black", outlier.shape=16,
outlier.size=2, notch=FALSE)+
xlab("") + ylab("Similarity index")
g1
# distribute similarities
p<-ggplot(mlt_sim, aes(x=value)) +
geom_histogram(bins=40, color="black") +
labs(x="Jaccard similarity index")
p
# distribute similarities by cancer
p2<-ggplot(mlt_sim, aes(x=value, fill= variable)) +
geom_histogram(bins=20, color="black", position="identity", alpha=0.5) +
labs(x="Jaccard similarity index", fill = "Cancer")
p2
#-------- investigate who is in the upper tail --------
all_simlrs_long <- read.csv("output/jaccard_values_per_cancer_long_format.csv")
in_tail <- all_simlrs_long %>% filter(similarity > 0.4)
chap_attrib <- read.csv("output/data/chap_attributes.csv") %>%
arrange(v_order) %>%
mutate(shape=case_when(function.=='protease' ~ 'rectangle',
function.=='folding' ~ 'circle')) %>%
mutate(colour=case_when(module==1 ~ 'lightblue',
module==2 ~ 'pink',
module==3 ~ 'lightgreen'))
intersections_df <- read.csv(file = "output/data/chapchap_intersections.csv") %>%
select(from=Var1, to=Var2, value, cancer) %>%
mutate(prots=value*1143)
excel_path <- "HPC/binari_validated_corrs.xlsx"
sheet_names <- excel_sheets(excel_path)
pdf("output/cancer_similarity_tail_chapchap_network.pdf")
for (cancer_nm in sheet_names) {
print_cancer_igraph(cancer_nm, chap_attrib, intersections_df)
}
dev.off()
# all cancers togather ----
# distribution in tail by cancer
p2<-ggplot(in_tail, aes(x=similarity, fill= cancer)) +
geom_histogram(bins=20, color="black", position="stack", alpha=0.5) +
labs(x="Jaccard similarity index", fill = "Cancer")
p2
n_distinct(c(in_tail$col, in_tail$row)) # all 15 chaps are in the tail.
table(c(in_tail$col, in_tail$row)) # how do they distribute in the chaps?
# igraph for all cancers in the same plot
rrr <- igraph::graph_from_data_frame(d = in_tail, vertices = chap_attrib, directed = FALSE)
plot.igraph(rrr, axes = FALSE, vertex.frame.color = NA,
#vertex.label = V(g)$name, vertex.label.color = "gray20",
vertex.size = 25, #ideg*25 + 40, vertex.size2 = 30,
vertex.color = chap_attrib$colour, #"gray90", vertex.frame.color = "gray20",
vertex.shape = chap_attrib$shape,
#edge.arrow.size=0.5, edge.color=col, edge.width = E(g)$weight / 10,
#edge.curved = T,
main = "all",
margin = c(-0.4,-0.4,-0.15,-0.2),
layout = as.matrix(chap_attrib[2:3]))