-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule v traits script.R
390 lines (289 loc) · 12.4 KB
/
Module v traits script.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#### Next step - look at expression of module vs trait
################################Define numbers of genes and samples to do traits comparison - use dynamic first?
load(file = "lncRNA_top10K_input.RData")
load(file = "Dynamic_merged_lncRNA.RData")
#### May load either the dynamic above or the static below, but not both at same time (same name for diff values)
load(file = "Static_lncRNA.RData")
nGenes = ncol(datExpr);
nSamples = nrow(datExpr);
# Recalculate MEs with color labels
MEs0 = moduleEigengenes(datExpr, moduleColors)$eigengenes
MEs = orderMEs(MEs0)
moduleTraitCor = cor(MEs, datTraits, 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(datTraits),
yLabels = names(MEs),
ySymbols = names(MEs),
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"))
######################################
################################Define numbers of genes and samples to do traits comparison
nGenes = ncol(datExpr);
nSamples = nrow(datExpr);
# Recalculate MEs with color labels
MEs0 = moduleEigengenes(datExpr, moduleColours)$eigengenes
MEs = orderMEs(MEs0)
moduleTraitCor = cor(MEs, 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(MEs),
ySymbols = names(MEs),
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"))
######################################
################################Define numbers of genes and samples to do traits comparison
nGenes = ncol(datExpr);
nSamples = nrow(datExpr);
# Recalculate MEs with color labels
MEs0 = moduleEigengenes(datExpr, moduleColours)$eigengenes
MEs = orderMEs(MEs0)
moduleTraitCor = cor(MEs, 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 = NA,
yLabels = names(MEs),
ySymbols = names(MEs),
colorLabels = FALSE,
colors = blueWhiteRed(50),
#textMatrix = textMatrix,
setStdMargins = FALSE,
cex.text = 0.85,
zlim = c(-1,1),
main = paste("High module expression vs \n methylation of trait genes"))
######################################
################################Define numbers of genes and samples to do traits comparison
nGenes = ncol(datExpr);
nSamples = nrow(datExpr);
# Recalculate MEs with color labels
MEs0 = moduleEigengenes(datExpr, moduleColours)$eigengenes
MEs = orderMEs(MEs0)
moduleTraitCor = cor(MEs, 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 = colnames(datTraits3),
yLabels = names(MEs),
ySymbols = names(MEs),
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"))
######################################
###### Set the trait of most interest in determining the modules
y = datTraits$MethScore
#### Calculate the modules' correlation to this trait
datME=moduleEigengenes(datExpr,moduleColors)$eigengenes
signif(cor(datME, use="p"), 2)
dissimME=(1-t(cor(datME, method="p")))/2
hclustdatME=hclust(as.dist(dissimME), method="average" )
# Plot the eigengene dendrogram
par(mfrow=c(1,1))
plot(hclustdatME, main="Clustering tree based of the module eigengenes")
sizeGrWindow(8,9)
plotMEpairs(datME,y=y)
#######Heatmaps of module expression - two most interconnected modules
sizeGrWindow(8,9)
par(mfrow=c(3,1), mar=c(1, 2, 4, 1))
which.module="brown";
plotMat(t(scale(datExpr[,moduleColors==which.module ]) ),nrgcols=30,rlabels=T,
clabels=T,rcols=which.module,
title=which.module )
# for the other modules... we use
which.module="black";
plotMat(t(scale(datExpr[,moduleColours==which.module ]) ),nrgcols=30,rlabels=T,
clabels=T,rcols=which.module,
title=which.module )
which.module="turquoise";
plotMat(t(scale(datExpr[,moduleColors==which.module ]) ),nrgcols=30,rlabels=T,
clabels=T,rcols=which.module,
title=which.module )
#######
GS1=as.numeric(cor(y,datExpr, use="p"))
GeneSig <- data.frame(GS1)
rownames(GeneSig) <- colnames(datExpr)
GeneSig$Module <- moduleColors
#save out genesig - can link to modulecolours in excel and search
write.table(GeneSig, "GeneSignifigance_methScore.txt", sep = "\t")
list <- GeneSig[BlueMod$colnames.datExpr.,]
BlueSig <- as.data.frame(GeneSig[BlueMod$colnames.datExpr.,])
rownames(BlueSig) <- BlueMod$colnames.datExpr.
BlackSig <- as.data.frame(GeneSig[BlackMod$colnames.datExpr.,])
rownames(BlackSig) <- BlackMod$colnames.datExpr.
BlackSig$DNMT1
GeneSignificance=abs(GS1)
# Next module significance is defined as average gene significance.
ModuleSignificance=tapply(GeneSignificance, moduleColors, mean, na.rm=T)
sizeGrWindow(8,7)
par(mfrow = c(1,1))
plotModuleSignificance(GeneSignificance,moduleColors)
p.values = corPvalueStudent(cor(y,datME, use="p"), nSamples = length(y))
write.table(p.values, "Module correlation Trait.txt", sep ="\t")
########### Calculate connectivity within modules
ADJ1=abs(cor(datExpr,use="p"))^12
Alldegrees1=intramodularConnectivity(ADJ1, moduleColors)
head(Alldegrees1)
colorlevels=unique(moduleColors)
sizeGrWindow(9,6)
par(mfrow=c(2,as.integer(0.5+length(colorlevels)/2)))
par(mar = c(4,5,3,1))
for (i in c(1:length(colorlevels)))
{
whichmodule=colorlevels[[i]];
restrict1 = (moduleColors==whichmodule);
verboseScatterplot(Alldegrees1$kWithin[restrict1],
GeneSignificance[restrict1], col=moduleColors[restrict1],
main=whichmodule,
xlab = "Connectivity", ylab = "Gene Significance", abline = TRUE)
}
######Just one module
sizeGrWindow(8,7)
par(mfrow = c(1,1))
whichmodule="red";
restrict1 = (moduleColors==whichmodule);
verboseScatterplot(Alldegrees1$kWithin[restrict1],
GeneSignificance[restrict1], col=moduleColors[restrict1],
main=whichmodule,
xlab = "Connectivity", ylab = "Gene Significance", abline = TRUE)
whichmodule="brown";
restrict1 = (moduleColors==whichmodule);
verboseScatterplot(Alldegrees1$kWithin[restrict1],
GeneSignificance[restrict1], col=moduleColors[restrict1],
main=whichmodule,
xlab = "Connectivity", ylab = "Gene Significance", abline = TRUE)
##### Output the measures of gene signifigance
# Output of eigengene information:
datMEy = data.frame(y, datME)
eigengeneSignificance = cor(datMEy, y);
eigengeneSignificance[1,1] = (1+max(eigengeneSignificance[-1, 1]))/2
eigengeneSignificance.pvalue = corPvalueStudent(eigengeneSignificance, nSamples = length(y))
namesME=names(datMEy)
# Form a summary data frame
out1 = data.frame(t(data.frame(eigengeneSignificance,eigengeneSignificance.pvalue, namesME, t(datMEy))))
# Set appropriate row names
dimnames(out1)[[1]][1]="EigengeneSignificance"
dimnames(out1)[[1]][2]="EigengeneSignificancePvalue"
dimnames(out1)[[1]][3]="ModuleEigengeneName"
dimnames(out1)[[1]][-c(1:3)]=dimnames(datExpr)[[1]]
# Write the data frame into a file
write.table(out1, file="MEResultsNetworkScreening.csv", row.names=TRUE, col.names = TRUE, sep=",")
# Display the first few rows:
head(out1)
#####
Alldegrees1$Modules <- moduleColors
write.table(Alldegrees1, "All_modules_connectivity.txt", sep = "\t")
ConnectRed <- Alldegrees1[moduleColors == "red",]
ConnectBrown <- Alldegrees1[moduleColors == "brown",]
ConnectTurq <- Alldegrees1[moduleColors == "turquoise",]
write.table(ConnectRed, "Red_intramodular_connect.txt", sep ="\t")
write.table(ConnectBrown, "Brown_intramodular_connect.txt", sep ="\t")
write.table(ConnectTurq, "Turquoise_intramodular_connect.txt", sep ="\t")
Her2 <- as.factor(datClin$HER2_Final_Status_nature2012)
Group <- as.factor(datClin$PAM50Call_RNAseq)
aov <- aov(MEs$MEbrown ~ Her2)
summary(aov)
TukeyHSD(aov)
aov <- aov(MEs$MEred ~ Her2)
summary(aov)
aov <- aov(MEs$MEred ~ ER)
summary(aov)
#############################################################
source("http://bioconductor.org/biocLite.R")
biocLite("GO.db")
biocLite("org.Hs.eg.db")
biocLite("AnnotationDbi")
library(Go.db)
library(AnnotationDbi)
library(org.Hs.eg.db)
source("http://bioconductor.org/biocLite.R")
biocLite("org.Hs.eg.db")
#load library
library(org.Hs.eg.db)
GOenr = GOenrichmentAnalysis(dynamicColors, allLLIDs, organism = "human", nBestP = 10);
tab = GOenr$bestPTerms[[4]]$enrichment
names(tab)
keepCols = c(1, 2, 5, 6, 7, 12, 13);
screenTab = tab[, keepCols];
# Round the numeric columns to 2 decimal places:
numCols = c(3, 4);
screenTab[, numCols] = signif(apply(screenTab[, numCols], 2, as.numeric), 2)
# Truncate the the term name to at most 40 characters
screenTab[, 7] = substring(screenTab[, 7], 1, 40)
# Shorten the column names:
colnames(screenTab) = c("module", "size", "p-val", "Bonf", "nInTerm", "ont", "term name");
rownames(screenTab) = NULL;
# Set the width of R’s output. The reader should play with this number to obtain satisfactory output.
options(width=95)
# Finally, display the enrichment table:
screenTab
###################### MDS scaling
cmd1=cmdscale(as.dist(dissTOM),2)
sizeGrWindow(7, 6)
par(mfrow=c(1,1))
plot(cmd1, col=as.character(moduleColors), main="MDS plot",
xlab="Scaling Dimension 1", ylab="Scaling Dimension 2")
###########################################
#### Cytoscape
### First reduce the network to the top genes only
list <- row.names(Top30Black)
TOMblack <- datExpr[,list]
TOMBlue <- subset(datExpr, moduleColours == "blue")
# Recalculate topological overlap on only the new small expression set
TOM = TOMsimilarityFromExpr(TOMblue, power = 12);
# Export the network into edge and node list files Cytoscape can read
cyt = exportNetworkToCytoscape(TOM,
edgeFile = paste("CytoscapeInput-edges-", paste(modules, collapse="-"), ".txt", sep=""),
nodeFile = paste("CytoscapeInput-nodes-", paste(modules, collapse="-"), ".txt", sep=""),
weighted = TRUE,
threshold = 0.02,
nodeNames = names(TOMblack));
#### End of doc