-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5 - Module exploration - Modules and clinical traits matrix calculations.R
223 lines (151 loc) · 6.36 KB
/
5 - Module exploration - Modules and clinical traits matrix calculations.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
#### Script for calculating trait vs module matrices using Pearson correlations
### These can be saved out for use with plotting scripts, after trimming of
### modules that are not so relevent (in order to make plots pretty looking)
### Performed separately on both datasets (TCGA and METABRIC)
setwd("~/Bioinformatics Work/Meth & RNA/Meta-analysis WGCNA")
library(WGCNA)
library(plyr)
library(flashClust)
library(ggplot2)
library(gplots)
library(lattice)
library(extrafont)
loadfonts()
##############
load(file = "MetaAnalysis_trimmed_input.RData")
load(file = "Modules_DS0.RData")
colorsA1 = names(table(modules1))
### add in the rownames for the ME_ data frames
rownames(ME_1A) <- colnames(datExpr1)
rownames(ME_2A) <- colnames(datExpr2)
## need to set the rownames for ME_1A
rownames(ME_1A) <- colnames(datExpr1)
datTraits1 <- read.table("Traits_nonmeth_TCGA.txt", sep = "\t",
header = TRUE, row.names = 1)
list <- intersect(rownames(ME_1trim), rownames(datTraits1))
datTraits1 <- datTraits1[list,]
ME_1trim <- ME_1A[list,]
####### OK, now do as for the
nGenes = ncol(ME_1trim);
nSamples = nrow(ME_1trim);
# Recalculate MEs with color labels
#MEs0 = moduleEigengenes(datExpr, moduleColors)$eigengenes
#MEs = orderMEs(MEs0)
moduleTraitCor = cor(ME_1trim, datTraits1, use = "p");
moduleTraitPvalue = corPvalueStudent(moduleTraitCor, nSamples);
sizeGrWindow(10,6)
# Will display correlations and their p-values
textMatrix = paste(signif(moduleTraitCor, 2), "\n(",
signif(moduleTraitPvalue, 1), ")", sep = "");
##########Will only display correlations
textMatrix = paste(signif(moduleTraitCor, 2),sep = "");
dim(textMatrix) = dim(moduleTraitCor)
par(mar = c(6, 8.5, 3, 3));
# Display the values within a heatmap plot
labeledHeatmap(Matrix = moduleTraitCor,
xLabels = colnames(datTraits1),
yLabels = names(ME_1trim),
ySymbols = names(ME_1trim),
colorLabels = FALSE,
colors = blueWhiteRed(50),
#textMatrix = textMatrix,
setStdMargins = FALSE,
cex.text = 0.85,
zlim = c(-1,1),
main = paste("High module expression vs trait genes"))
#################################
## label and write out the module trait cor and the pvalue
cormat <- as.matrix(moduleTraitCor)
colnames(cormat) <- colnames(datTraits1)
rownames(cormat) <- colnames(ME_1trim)
write.table(cormat, "Correlation_matrix_TCGA_traits.txt", sep = "\t")
corp <- as.matrix(moduleTraitPvalue)
colnames(corp) <- colnames(datTraits1)
rownames(corp) <- colnames(ME_1trim)
write.table(corp, "Cor_Pvalue_matrix_TCGA_traits.txt", sep = "\t")
###### Let's have a look with the meth traits for TCGA
datTraits3 <- read.table("methTraits2_Stir200i.txt", sep = "\t",
header = TRUE, row.names = 1)
list <- intersect(rownames(datTraits3), rownames(ME_1trim))
datTraits3 <- datTraits3[list,]
ME_1trim <- ME_1trim[list,]
####### OK, now do as for the
nGenes = ncol(ME_1trim);
nSamples = nrow(ME_1trim);
# Recalculate MEs with color labels
#MEs0 = moduleEigengenes(datExpr, moduleColors)$eigengenes
#MEs = orderMEs(MEs0)
moduleTraitCor = cor(ME_1trim, datTraits3, use = "p");
moduleTraitPvalue = corPvalueStudent(moduleTraitCor, nSamples);
sizeGrWindow(10,6)
# Will display correlations and their p-values
textMatrix = paste(signif(moduleTraitCor, 2), "\n(",
signif(moduleTraitPvalue, 1), ")", sep = "");
##########Will only display correlations
textMatrix = paste(signif(moduleTraitCor, 2),sep = "");
dim(textMatrix) = dim(moduleTraitCor)
par(mar = c(6, 8.5, 3, 3));
# Display the values within a heatmap plot
labeledHeatmap(Matrix = moduleTraitCor,
xLabels = NULL,
yLabels = names(ME_1trim),
ySymbols = names(ME_1trim),
colorLabels = FALSE,
colors = blueWhiteRed(50),
#textMatrix = textMatrix,
setStdMargins = FALSE,
cex.text = 0.85,
zlim = c(-0.6,0.6),
main = paste("High module expression vs trait genes"))
#### the correlation matrix for the modules and the methylation values
cormat <- as.matrix(moduleTraitCor)
colnames(cormat) <- colnames(datTraits3)
rownames(cormat) <- colnames(ME_1trim)
write.table(cormat, "Correlation_matrix_TCGA_meth.txt", sep = "\t")
corp <- as.matrix(moduleTraitPvalue)
colnames(corp) <- colnames(datTraits3)
rownames(corp) <- colnames(ME_1trim)
write.table(corp, "Cor_Pvalue_matrix_TCGA_meth.txt", sep = "\t")
########################################################################
############ METABRIC
datTraits2 <- read.table("Traits_trimmed_METABRIC.txt", sep = "\t",
header = TRUE, row.names = 1)
list <- intersect(rownames(ME_2A), rownames(datTraits2))
datTraits2 <- datTraits2[list,]
################# for the metabric
nGenes = ncol(ME_2A);
nSamples = nrow(ME_2A);
# Recalculate MEs with color labels
#MEs0 = moduleEigengenes(datExpr, moduleColors)$eigengenes
#MEs = orderMEs(MEs0)
moduleTraitCor = cor(ME_2A, datTraits2, use = "p");
moduleTraitPvalue = corPvalueStudent(moduleTraitCor, nSamples);
sizeGrWindow(10,6)
# Will display correlations and their p-values
textMatrix = paste(signif(moduleTraitCor, 2), "\n(",
signif(moduleTraitPvalue, 1), ")", sep = "");
##########Will only display correlations
textMatrix = paste(signif(moduleTraitCor, 2),sep = "");
dim(textMatrix) = dim(moduleTraitCor)
par(mar = c(6, 8.5, 3, 3));
# Display the values within a heatmap plot
labeledHeatmap(Matrix = moduleTraitCor,
xLabels = colnames(datTraits2),
yLabels = names(ME_2A),
ySymbols = names(ME_2A),
colorLabels = FALSE,
colors = blueWhiteRed(50),
#textMatrix = textMatrix,
setStdMargins = FALSE,
cex.text = 0.85,
zlim = c(-1,1),
main = paste("High module expression vs trait genes"))
############### Correlation tables
cormat <- as.matrix(moduleTraitCor)
colnames(cormat) <- colnames(datTraits2)
rownames(cormat) <- colnames(ME_2A)
write.table(cormat, "Correlation_matrix_METABRIC_traits.txt", sep = "\t")
corp <- as.matrix(moduleTraitPvalue)
colnames(corp) <- colnames(datTraits2)
rownames(corp) <- colnames(ME_2A)
write.table(corp, "Cor_Pvalue_matrix_METABRIC_traits.txt", sep = "\t")