-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclustering_iND750.R
178 lines (125 loc) · 6.05 KB
/
clustering_iND750.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
require('tidyverse')
library("RColorBrewer")
library(extrafont)
font_import(pattern='calibri')
loadfonts(device = "win")
log_shift = 1.1
shift_pos = 23
# Read the ECMs as row vectors (R convention), and add column names for each metabolite
ecms <- read_csv('data/iND750_indirect.csv', col_names=TRUE)
# Drop uninteresting ECMs
interesting_ecms <- ecms %>%
filter(objective > 0 & M_ala__L_e >= 0 & M_asp__L_e >= 0 & M_gly_e >= 0 & M_ser__L_e >= 0 & M_thr__L_e >= 0)
# Reduce complexity by only looking at uptake (-1), production (+1) or nothing (0)
interesting_ecms[interesting_ecms<0] = -1
interesting_ecms[interesting_ecms>0] = 1
# Rows could have become duplicated: keep only unique rows
ecms_unique <- interesting_ecms %>% distinct()
# Drop unused metabolites, and empty ECMs (they are normally not present to begin with)
col_sums <- colSums(abs(ecms_unique))
row_sums <- rowSums(abs(ecms_unique))
col_indices <- col_sums != 0
row_indices <- row_sums != 0
filled_ecms <- ecms_unique[row_indices,col_indices]
filled_ecms <- as.data.frame(filled_ecms)
rownames(filled_ecms) <- 1:nrow(filled_ecms)
# Read in matching of metabolite ids and names
# metab_info <- read_csv(file.path('data','metab_info_iIT.csv'),col_names=TRUE)
# metab_names <- c()
# for (col in colnames(filled_ecms)) {
# metab_names <- c(metab_names,metab_info[metab_info$id==col,]$name)
# }
# colnames(filled_ecms) <- metab_names
clust_weights <- setNames(as.list(rep(1,length(colnames(filled_ecms)))), colnames(filled_ecms))
clust_weights[c('M_glc__D_e','M_xylt_e','M_ac_e','M_gly_e','M_ser__L_e', 'M_asp__L_e', 'M_ala__L_e', 'M_thr__L_e')] <- c(2000,90,80,70,60,50,40,30)
weighted_ecms <- filled_ecms * clust_weights
row.order <- hclust(dist(weighted_ecms,method='manhattan'), method = "complete")$order # clustering
# Cluster metabolites
col.order <- order(colSums(filled_ecms))
# col.order <- hclust(dist(t(filled_ecms),method='manhattan'), method = "complete")$order
ordered_metabs <- attributes(filled_ecms)$names[col.order]
# Order ECMs according to clustering
clustered_ecms <- filled_ecms[row.order,] %>%
as.data.frame() %>%
mutate(ecm=1:n()) %>%
gather('metabolite', 'stoich', -ecm)
# Order metabolites according to clustering
clustered_ecms$metabolite <- factor(clustered_ecms$metabolite,
levels=ordered_metabs)
# Render clusters
clustered_ecms %>%
ggplot(aes(x=ecm, y=metabolite, fill=stoich)) +
geom_tile() +
#scale_fill_brewer( type="div", palette=c("#F9BA00FF", "#88FA4EFF", "#56C1FFFF"), guide="legend") +
scale_fill_gradientn(colours = brewer.pal(3, 'RdYlBu'), n.breaks=3, labels=c('uptake','none','export'),
guide=guide_legend( label.theme = element_text(family='Calibri',size=18)),
na.value='white', name=NULL) +
geom_raster() +
theme(axis.title.x = element_text(family='Calibri', size=18),
axis.title.y = element_text(family='Calibri', size=18),
axis.text.y = element_text(angle = 0, hjust = 1, family='Calibri'),
axis.text.x = element_blank())
# Log-scale all coefficients except for objective coefficients. Then shift numbers such that they are all negative again
max_neg = max(filled_ecms[filled_ecms<0])
filled_ecms[filled_ecms<0] <- -log(-filled_ecms[filled_ecms<0]) + log_shift * log(-max_neg)
# Shift positive numbers too, to better use colourscale
filled_ecms[filled_ecms>0] <- filled_ecms[filled_ecms>0]*shift_pos
row_clust <- hclust(dist(filled_ecms,method='manhattan'), method = "complete") # clustering
row.order <- row_clust$order
plot(row_clust) # display dendogram
groups <- cutree(row_clust, k=20) # cut tree into 5 clusters
# draw dendogram with red borders around the 5 clusters
rect.hclust(row_clust, k=20, border="red")
# Convert hclust into a dendrogram and plot
hcd <- as.dendrogram(row_clust)
# Default plot
plot(hcd, type = "rectangle", ylab = "Height")
# Cluster metabolites
col.order <- hclust(dist(t(filled_ecms),method='manhattan'), method = "complete")$order
ordered_metabs <- attributes(filled_ecms)$names[col.order]
man_ordered_metabs <- c("M_ala__L_e","M_ala__D_e","M_arg__L_e","M_o2_e","M_pi_e","M_h_e","M_nh4_e","M_so4_e","M_pheme_e",
"M_fe2_e","M_his__L_e", "M_val__L_e","M_leu__L_e", "M_ile__L_e", "M_met__L_e","M_pime_e","M_thm_e","objective")
# Read in matching of metabolite ids and names
man_ordered_names <- c()
for (col in man_ordered_metabs) {
man_ordered_names <- c(man_ordered_names,metab_info[metab_info$id==col,]$name)
}
man_ordered_metabs <- man_ordered_names
# Order ECMs according to clustering
clustered_ecms <- filled_ecms[row.order,] %>%
as.data.frame() %>%
mutate(ecm=1:n()) %>%
gather('metabolite', 'stoich', -ecm)
# Order metabolites according to clustering
clustered_ecms$metabolite <- factor(clustered_ecms$metabolite,
levels=man_ordered_metabs)
inv_get_labels <- function(orig){
result = rep(NA,length(orig))
for(i in 1:length(orig)){
if (orig[i]<0){
result[i] = -log(-orig[i]) + log_shift*log(-max_neg)
}else if(orig[i]>0){
result[i] = orig[i]*shift_pos
}else{
result[i] = 0
}
}
result
}
paper_colors = brewer.pal(3, 'RdYlBu')
clustered_ecms[clustered_ecms$stoich==0,]$stoich<-NA
# Render clusters
clustered_ecms %>%
ggplot(aes(x=ecm, y=metabolite, fill=stoich)) +
geom_tile() +
scale_fill_gradient2(midpoint = 0, low = paper_colors[1], mid = "grey90",
high = paper_colors[3], space = "Lab", breaks=inv_get_labels(c(-10,-.001,0,1)),
labels=as.character(c(-10,-1e-3,0,1),format='e'),
na.value='white',
guide=guide_colorbar(label.theme = element_text(family='Calibri',size=16),
title.theme = element_text(family='Calibri',size=22))) +
geom_raster() +
theme(axis.title.x = element_text(family='Calibri', size=22),
axis.title.y = element_text(family='Calibri', size=22),
axis.text.y = element_text(angle = 0, hjust = 1, family='Calibri', size=16),
axis.text.x = element_blank())