forked from CSB-IG/pa3bc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.3_Synthetic_heatmap.R
366 lines (316 loc) · 13.9 KB
/
6.3_Synthetic_heatmap.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
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
###############################################################################
# Heatmap Sintético Grupos tumorales y grupos de procesos
### Author: Angel García-Campos https://github.com/AngelCampos
###############################################################################
###############################################################################
## Load Pathifier results and turn into a matrix
###############################################################################
load ("PDS.RData")
PDSmatrix <- mapply(FUN = c, PDS$scores)
PDSmatrix <- t(PDSmatrix)
###############################################################################
## Generating and assigning labels for pathways used in the analysis
###############################################################################
# Read input pathways from .txt
pathways <- read.delim("pathways.txt", header = F)
rownames(pathways) <- pathways[,2] # Naming rownames
# Extract NUMBER.ID of pathways that Pathifier succesfully useds
pathwaysInPDS <- rownames(PDSmatrix)
# Selecting the NAMES of pathways
paths <- as.vector(pathways[,1]) # Extracting pathway names in vector
names(paths) <- pathways[,2] # Nombrar vector con pathways
labels <- paths[pathwaysInPDS] # Selects names of pathways as labels
rownames(PDSmatrix) <- labels # assign labels as row names in PDSmatrix
###############################################################################
### Installing and/or loading required packages "gplots" "RColorBrewer"
###############################################################################
if (!require("gplots")) {
install.packages("gplots", dependencies = TRUE)
library(gplots)
}
if (!require("RColorBrewer")) {
install.packages("RColorBrewer", dependencies = TRUE)
library(RColorBrewer)
}
###############################################################################
## Creating Palette
###############################################################################
# creates a own color palette passing from blue, green yellow to red
my_palette <- colorRampPalette(c("blue", "cyan", "chartreuse1", "yellow",
"red", "firebrick4"))(n = 1000)
###############################################################################
## Clustering Methods
###############################################################################
row.distance = dist(PDSmatrix, method = "euclidean")
row.cluster = hclust(row.distance, method = "ward.D2")
col.distance = dist(t(PDSmatrix), method = "euclidean")
col.cluster = hclust(col.distance, method = "ward.D2")
###############################################################################
# Subgroup labeling
###############################################################################
mycl <- cutree(col.cluster, k= 6)
mycl <-sub(2,1L,mycl); mycl <- sub(3,2L,mycl); mycl <- sub(4,3L,mycl)
mycl <- sub(5,4L,mycl); mycl <- sub(6,5L,mycl); mycl <- as.integer(mycl)
mycolhc <- rainbow(length(unique(mycl)), start=0.1, end=0.9)
mycolhc <- mycolhc[as.vector(mycl)]
myrw <- cutree(row.cluster, k = 9)
myrowhc <- rainbow(length(unique(myrw)), start=0.1, end=0.9)
myrowhc <- myrowhc[as.vector(myrw)]
###############################################################################
## Dividing the processes in groups
###############################################################################
png(paste("Synthetic_Heatmap_9.png", sep = ""), # Name of png file
width = 7.5 * 500, # Easier re-scaling X*500 = Y pixels
height = 7.5 * 400, # 6 x 400 = 2400 px
units = "px", # px (Pixels = default), in (inches), cm or mm
res = 300, # 300 pixels per inch
pointsize = 6) # font size
heatmap.2(PDSmatrix,
main = "Subgrupos tumorales / Grupos de procesos", # heat map title
density.info= "none", # turns off density plot inside color legend
trace= "none", # turns off trace lines inside the heat map
margins= c(10,21), # widens margins around plot
col=my_palette, # use on color palette defined earlier
Rowv = as.dendrogram(row.cluster), # apply selected clustering method
Colv = as.dendrogram(col.cluster), # apply selected clustering method
keysize= 0.8, # size of color key
## Color labeling columns (Opt. RowSideColors for rows)
ColSideColors= mycolhc,
RowSideColors= myrowhc # Samples 62-880
)
par(lend = 1) # square line ends for the color legend
legend("topright", # location of the legend on the heatmap plot
legend = c(as.character(1:5)), # category labels
col = unique(mycolhc), # color key
lty= 1, # line style
lwd = 5, unit # line width
)
dev.off() # close the PNG device
################################################################################
# Separación y promedio de datos de subgrupos
################################################################################
# Subgrupo 1
x1 <- NULL
for(i in 1:9){
m <- mean(PDSmatrix[myrw == i, mycl == 1])
x1 <- rbind(x1,m)
print(x1)
}
# Subgrupo 2
x2 <- NULL
for(i in 1:9){
m <- mean(PDSmatrix[myrw == i, mycl == 2])
x2 <- rbind(x2,m)
print(x2)
}
# Subgrupo 3
x3 <- NULL
for(i in 1:9){
m <- mean(PDSmatrix[myrw == i, mycl == 3])
x3 <- rbind(x3,m)
print(x3)
}
# Subgrupo 4
x4 <- NULL
for(i in 1:9){
m <- mean(PDSmatrix[myrw == i, mycl == 4])
x4 <- rbind(x4,m)
print(x4)
}
# Subgrupo 5
x5 <- NULL
for(i in 1:9){
m <- mean(PDSmatrix[myrw == i, mycl == 5])
x5 <- rbind(x5,m)
print(x5)
}
#Unir por columnas
sumPDS <- cbind(x1,x2,x3,x4,x5)
sumPDSPRIME <- sumPDS[c(7,9,1,3,5,2,8,4,6), c(1,4,5,3,2)]
rownames(sumPDSPRIME) <- c("BPG1","BPG2","BPG3","BPG4","BPG5","BPG6","BPG7",
"BPG8","BPG9")
colnames(sumPDSPRIME) <- c("SG0","SG1","SG2","SG3","SG4")
sumPDSsignif <- signif(sumPDSPRIME, 2)
###############################################################################
## Plotting the SYNTHETIC Heatmap!! (where all colorful things happen... again)
###############################################################################
# creates a own color palette passing from blue, green yellow to red
new_palette <- colorRampPalette(c("blue", "cyan", "chartreuse1", "yellow",
"red", "firebrick4"))(n = 100)
png(paste("Synthetic_Heatmap.png", sep = ""), # Name of png file
width = 7.5 * 400, # Easier re-scaling X*500 = Y pixels
height = 7.5 * 400, # 6 x 400 = 2400 px
units = "px", # px (Pixels = default), in (inches), cm or mm
res = 300, # 300 pixels per inch
pointsize = 20) # font size
heatmap.2(sumPDSPRIME,
density.info= "none", # turns off density plot inside color legend
trace= "none", # turns off trace lines inside the heat map
key = FALSE,
# margins= c(5,20), # widens margins around plot
col=new_palette, # use on color palette defined earlier
Rowv = "none", # apply selected clustering method
Colv = "none", # apply selected clustering method
# lmat = c(10,10),
lhei = c(10,100),
lwid = c(10,100)
)
dev.off() # close the PNG device
### SQUARED cells Synthetic Heatmap
png(paste("SQ_Synthetic_Heatmap.png", sep = ""), # Name of png file
width = 5.5 * 400, # Easier re-scaling X*500 = Y pixels
height = 9 * 400, # 6 x 400 = 2400 px
units = "px", # px (Pixels = default), in (inches), cm or mm
res = 300, # 300 pixels per inch
pointsize = 20) # font size
heatmap.2(sumPDSPRIME,
density.info= "none", # turns off density plot inside color legend
trace= "none", # turns off trace lines inside the heat map
key = FALSE,
# margins= c(5,20), # widens margins around plot
col=new_palette, # use on color palette defined earlier
Rowv = "none", # apply selected clustering method
Colv = "none", # apply selected clustering method
# lmat = c(10,10),
lhei = c(10,100),
lwid = c(10,100)
)
dev.off() # close the PNG device
### Version with quantitative data notes
png(paste("SQ_Synthetic_Heatmap_cuantitative.png", sep = ""), # Name of png file
width = 5.5 * 400, # Easier re-scaling X*500 = Y pixels
height = 9 * 400, # 6 x 400 = 2400 px
units = "px", # px (Pixels = default), in (inches), cm or mm
res = 300, # 300 pixels per inch
pointsize = 20) # font size
heatmap.2(sumPDSPRIME,
density.info= "none", # turns off density plot inside color legend
trace= "none", # turns off trace lines inside the heat map
key = FALSE,
# margins= c(5,20), # widens margins around plot
col=new_palette, # use on color palette defined earlier
Rowv = "none", # apply selected clustering method
Colv = "none", # apply selected clustering method
# lmat = c(10,10),
lhei = c(10,100),
lwid = c(10,100),
cellnote = sumPDSsignif,
notecol = "black"
)
dev.off() # close the PNG device
################################################################################
# Informacion de entrecruzamiento por cada subproceso
################################################################################
# Grado de interseccion para cada subproceso
nBP <- c(1:96)
xsyn <- NULL
for (i in 1:9) {
BPGg <- NULL
BPGg <- unlist(PDS$genesinpathway[nBP[myrw == i]])
names(BPGg) <- NULL
BPGg <- unique(gsub(" ", "", BPGg, fixed = TRUE))
p <- intersect(BPGg, genes1)
q <- intersect(BPGg, genes2)
if (length(p)>length(q)) {
xt <- length(q)/length(p)
xsyn <- c(xsyn, xt)
}
if (length(q)>length(p)) {
xsyn <- length(p)/length(q)
xt <- length(q)/length(p)
xsyn <- c(xsyn, xt)
}
}
xsyn <- xsyn[c(7,9,1,3,5,2,8,4,6)] #Re order
xsyn <- matrix(data = xsyn, 9, 1)
xsyn <- cbind(xsyn, seq(0, 1, length.out = 9))
rownames(xsyn) <- rownames(sumPDSPRIME)
colnames(xsyn) <- c("X-t", "g-s")
round_xsyn <- round(xsyn, 2)
# creates a own color palette passing from blue, green yellow to red
gray_scale <- colorRampPalette(c("white", "black"))(n = 50)
png(paste("SQ_Synthetic_Xtalk.png", sep = ""), # Name of png file
width = 3 * 400, # Easier re-scaling X*500 = Y pixels
height = 9 * 400, # 6 x 400 = 2400 px
units = "px", # px (Pixels = default), in (inches), cm or mm
res = 300, # 300 pixels per inch
pointsize = 20) # font size
heatmap.2(xsyn,
density.info= "none", # turns off density plot inside color legend
trace= "none", # turns off trace lines inside the heat map
key = FALSE,
# margins= c(5,20), # widens margins around plot
col= gray_scale, # use on color palette defined earlier
Rowv = "none", # apply selected clustering method
Colv = "none", # apply selected clustering method
# lmat = c(10,10),
lhei = c(10,100),
lwid = c(10,100),
cellnote = round_xsyn,
notecol = "black"
)
dev.off()
################################################################################
# Procesos biologicos y genes de cada grupo
################################################################################
# Reordering subgroups bioprocess number
# Escribir un archivo con los bio procesos de cada grupo
#1
bpn <- rownames(PDSdataframe[myrw == 7,])
write(x = bpn, file = paste("Bio_Process_G", 1, ".txt", sep = ""),sep = "\t")
#2
bpn <- rownames(PDSdataframe[myrw == 9,])
write(x = bpn, file = paste("Bio_Process_G", 2, ".txt", sep = ""),sep = "\t")
#3
bpn <- rownames(PDSdataframe[myrw == 1,])
write(x = bpn, file = paste("Bio_Process_G", 3, ".txt", sep = ""),sep = "\t")
#4
bpn <- rownames(PDSdataframe[myrw == 3,])
write(x = bpn, file = paste("Bio_Process_G", 4, ".txt", sep = ""),sep = "\t")
#5
bpn <- rownames(PDSdataframe[myrw == 5,])
write(x = bpn, file = paste("Bio_Process_G", 5, ".txt", sep = ""),sep = "\t")
#6
bpn <- rownames(PDSdataframe[myrw == 2,])
write(x = bpn, file = paste("Bio_Process_G", 6, ".txt", sep = ""),sep = "\t")
#7
bpn <- rownames(PDSdataframe[myrw == 8,])
write(x = bpn, file = paste("Bio_Process_G", 7, ".txt", sep = ""),sep = "\t")
#8
bpn <- rownames(PDSdataframe[myrw == 4,])
write(x = bpn, file = paste("Bio_Process_G", 8, ".txt", sep = ""),sep = "\t")
#9
bpn <- rownames(PDSdataframe[myrw == 6,])
write(x = bpn, file = paste("Bio_Process_G", 9, ".txt", sep = ""),sep = "\t")
c(7,9,1,3,5,2,8,4,6)
# Obtener la lista de genes de cada grupo
#1
BPGg <- unlist(PDS$genesinpathway[nBP[myrw == 7]])
write(x = BPGg, file = paste("Bio_Process_G", 1 , "_genes.txt", sep = ""))
#2
BPGg <- unlist(PDS$genesinpathway[nBP[myrw == 9]])
write(x = BPGg, file = paste("Bio_Process_G", 2 , "_genes.txt", sep = ""))
#3
BPGg <- unlist(PDS$genesinpathway[nBP[myrw == 1]])
write(x = BPGg, file = paste("Bio_Process_G", 3 , "_genes.txt", sep = ""))
#4
BPGg <- unlist(PDS$genesinpathway[nBP[myrw == 3]])
write(x = BPGg, file = paste("Bio_Process_G", 4 , "_genes.txt", sep = ""))
#5
BPGg <- unlist(PDS$genesinpathway[nBP[myrw == 5]])
write(x = BPGg, file = paste("Bio_Process_G", 5 , "_genes.txt", sep = ""))
#6
BPGg <- unlist(PDS$genesinpathway[nBP[myrw == 2]])
write(x = BPGg, file = paste("Bio_Process_G", 6 , "_genes.txt", sep = ""))
#7
BPGg <- unlist(PDS$genesinpathway[nBP[myrw == 8]])
write(x = BPGg, file = paste("Bio_Process_G", 7 , "_genes.txt", sep = ""))
#8
BPGg <- unlist(PDS$genesinpathway[nBP[myrw == 4]])
write(x = BPGg, file = paste("Bio_Process_G", 8 , "_genes.txt", sep = ""))
#9
BPGg <- unlist(PDS$genesinpathway[nBP[myrw == 6]])
write(x = BPGg, file = paste("Bio_Process_G", 9 , "_genes.txt", sep = ""))
# List with all genes used in PATHIFIER analysis
genes0 <- union(genes5, genes6)
write(x = genes0, file = paste("Bio_Process_G", 0 , "_genes.txt", sep = ""))