-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHigherLevelAnalysis.R
More file actions
147 lines (96 loc) · 4.75 KB
/
HigherLevelAnalysis.R
File metadata and controls
147 lines (96 loc) · 4.75 KB
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
PlotPathwayHeatmaps <- function(tissueOfInterest,pathways,pathwayNames){
readPath <- "data/FGSEAResults/"
if(!dir.exists("data/HigherLevelAnalysis")){
dir.create("data/HigherLevelAnalysis")
}
if(!dir.exists("data/HigherLevelAnalysis/PathwayHeatmaps")){
dir.create("data/HigherLevelAnalysis/PathwayHeatmaps")
}
writePath <- "data/HigherLevelAnalysis/PathwayHeatmaps/"
for (i in 1:length(pathwayNames)) {
if(!dir.exists(paste0("data/HigherLevelAnalysis/PathwayHeatmaps/",pathwayNames[i]))){
dir.create(paste0("data/HigherLevelAnalysis/PathwayHeatmaps/",pathwayNames[i]))
}
writePath <- paste0("data/HigherLevelAnalysis/PathwayHeatmaps/",pathwayNames[i],"/")
contrasts <- c("HF_Surgery","LF_Surgery","Diet","Surgery","Surgery_Diet")
for (contrast in contrasts){
pathNames <- names(pathways[[i]])
heatmapData <- as.data.frame(matrix(ncol = length(tissueOfInterest),nrow = length(pathNames)))
rownames(heatmapData) <- pathNames
colnames(heatmapData) <- tissueOfInterest
for (tissue in tissueOfInterest) {
fgseares <- read.xlsx(paste0(readPath,tissue,"/AllPathways.xlsx"),sheet = paste0(pathwayNames[i],"_",contrast))
sigUpPathwayNames <- fgseares[fgseares$padj < 0.05 & fgseares$NES > 0,]$pathway
sigDownPathwayNames <- fgseares[fgseares$padj < 0.05 & fgseares$NES < 0,]$pathway
for (pathway in pathNames) {
if(pathway %in% sigUpPathwayNames){
heatmapData[pathway,tissue] <- 1
}else if(pathway %in% sigDownPathwayNames){
heatmapData[pathway,tissue] <- -1
}else{
heatmapData[pathway,tissue] <- 0
}
}
}
heatmapData <- heatmapData %>%
dplyr::mutate(UpCount = rowSums(across(all_of(tissueOfInterest)) == 1))
heatmapData <- heatmapData %>%
dplyr::mutate(DownCount = rowSums(across(all_of(tissueOfInterest)) == -1))
heatmapData <- heatmapData %>%
dplyr::mutate(NoSigCount = rowSums(across(all_of(tissueOfInterest)) == 0))
heatmapDataDf <- heatmapData[!heatmapData$NoSigCount==8,-(9:11)]
colFun <- colorRamp2(c(-1, 0, 1), c("blue", "white", "red"))
heatMap <- Heatmap(as.matrix(heatmapDataDf),
column_names_side = "top",
col = colFun,
border = TRUE,
row_names_gp = gpar(fontsize = 15),
column_names_gp = gpar(fontsize = 12),
heatmap_width = unit(28,"cm"),
heatmap_height = unit(40,"cm"),
show_heatmap_legend = FALSE,
rect_gp = gpar(col = "black", lwd = 1),
column_title = paste0("Heatmap of significant pathways for ",contrast," contrast"),
column_title_gp = gpar(fontsize = 20, fontface = "bold")
)
lgd = Legend(
labels = c("Up", "NotSig", "Down"),
title = "Significance",
legend_gp = gpar(fill = c("red", "white", "blue")),
border = TRUE,
direction = "horizontal",
title_gp = gpar(fontsize = 15),
labels_gp = gpar(fontsize = 15)
)
jpeg(paste0(writePath,pathwayNames[i],"_",contrast,"_PathwayHeatMap.jpeg"),height = 1400,width = 1600,quality = 100)
draw(heatMap,annotation_legend_list = list(lgd),
annotation_legend_side = "bottom"
)
dev.off()
file <- paste0(writePath,pathwayNames[i],"_PathwaysSignificance.xlsx")
if (file.exists(file)) {
# Load the existing workbook
wb <- loadWorkbook(file)
sheetName <- contrast
# Add a new sheet (check if the sheet already exists first)
if (!(sheetName %in% names(wb))) {
addWorksheet(wb, sheetName)
}
# Optionally, write some data to the new sheet
writeData(wb, sheetName,heatmapData, startCol = 1, startRow = 1,rowNames = TRUE, colNames = TRUE)
# Save the workbook
saveWorkbook(wb, file, overwrite = TRUE)
} else {
# Create a new workbook if the file doesn't exist
wb <- createWorkbook()
# Add a new sheet
sheetName <- contrast
addWorksheet(wb, sheetName)
# Optionally, write some data to the new sheet
writeData(wb, sheetName,heatmapData, startCol = 1, startRow = 1,rowNames = TRUE, colNames = TRUE)
# Save the new workbook
saveWorkbook(wb, file, overwrite = TRUE)
}
}
}
}