-
Notifications
You must be signed in to change notification settings - Fork 130
/
WGCNA.R
295 lines (176 loc) · 7.39 KB
/
WGCNA.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
# script to perform WGCNA
# setwd("~/Desktop/demo/WGCNA")
library(WGCNA)
library(DESeq2)
library(GEOquery)
library(tidyverse)
library(CorLevelPlot)
library(gridExtra)
allowWGCNAThreads() # allow multi-threading (optional)
# 1. Fetch Data ------------------------------------------------
data <- read.delim('~/Desktop/demo/WGCNA/GSE152418_p20047_Study1_RawCounts.txt', header = T)
# get metadata
geo_id <- "GSE152418"
gse <- getGEO(geo_id, GSEMatrix = TRUE)
phenoData <- pData(phenoData(gse[[1]]))
head(phenoData)
phenoData <- phenoData[,c(1,2,46:50)]
# prepare data
data[1:10,1:10]
data <- data %>%
gather(key = 'samples', value = 'counts', -ENSEMBLID) %>%
mutate(samples = gsub('\\.', '-', samples)) %>%
inner_join(., phenoData, by = c('samples' = 'title')) %>%
select(1,3,4) %>%
spread(key = 'geo_accession', value = 'counts') %>%
column_to_rownames(var = 'ENSEMBLID')
# 2. QC - outlier detection ------------------------------------------------
# detect outlier genes
gsg <- goodSamplesGenes(t(data))
summary(gsg)
gsg$allOK
table(gsg$goodGenes)
table(gsg$goodSamples)
# remove genes that are detectd as outliers
data <- data[gsg$goodGenes == TRUE,]
# detect outlier samples - hierarchical clustering - method 1
htree <- hclust(dist(t(data)), method = "average")
plot(htree)
# pca - method 2
pca <- prcomp(t(data))
pca.dat <- pca$x
pca.var <- pca$sdev^2
pca.var.percent <- round(pca.var/sum(pca.var)*100, digits = 2)
pca.dat <- as.data.frame(pca.dat)
ggplot(pca.dat, aes(PC1, PC2)) +
geom_point() +
geom_text(label = rownames(pca.dat)) +
labs(x = paste0('PC1: ', pca.var.percent[1], ' %'),
y = paste0('PC2: ', pca.var.percent[2], ' %'))
### NOTE: If there are batch effects observed, correct for them before moving ahead
# exclude outlier samples
samples.to.be.excluded <- c('GSM4615000', 'GSM4614993', 'GSM4614995')
data.subset <- data[,!(colnames(data) %in% samples.to.be.excluded)]
# 3. Normalization ----------------------------------------------------------------------
# create a deseq2 dataset
# exclude outlier samples
colData <- phenoData %>%
filter(!row.names(.) %in% samples.to.be.excluded)
# fixing column names in colData
names(colData)
names(colData) <- gsub(':ch1', '', names(colData))
names(colData) <- gsub('\\s', '_', names(colData))
# making the rownames and column names identical
all(rownames(colData) %in% colnames(data.subset))
all(rownames(colData) == colnames(data.subset))
# create dds
dds <- DESeqDataSetFromMatrix(countData = data.subset,
colData = colData,
design = ~ 1) # not spcifying model
## remove all genes with counts < 15 in more than 75% of samples (31*0.75=23.25)
## suggested by WGCNA on RNAseq FAQ
dds75 <- dds[rowSums(counts(dds) >= 15) >= 24,]
nrow(dds75) # 13284 genes
# perform variance stabilization
dds_norm <- vst(dds75)
# get normalized counts
norm.counts <- assay(dds_norm) %>%
t()
# 4. Network Construction ---------------------------------------------------
# Choose a set of soft-thresholding powers
power <- c(c(1:10), seq(from = 12, to = 50, by = 2))
# Call the network topology analysis function
sft <- pickSoftThreshold(norm.counts,
powerVector = power,
networkType = "signed",
verbose = 5)
sft.data <- sft$fitIndices
# visualization to pick power
a1 <- ggplot(sft.data, aes(Power, SFT.R.sq, label = Power)) +
geom_point() +
geom_text(nudge_y = 0.1) +
geom_hline(yintercept = 0.8, color = 'red') +
labs(x = 'Power', y = 'Scale free topology model fit, signed R^2') +
theme_classic()
a2 <- ggplot(sft.data, aes(Power, mean.k., label = Power)) +
geom_point() +
geom_text(nudge_y = 0.1) +
labs(x = 'Power', y = 'Mean Connectivity') +
theme_classic()
grid.arrange(a1, a2, nrow = 2)
# convert matrix to numeric
norm.counts[] <- sapply(norm.counts, as.numeric)
soft_power <- 18
temp_cor <- cor
cor <- WGCNA::cor
# memory estimate w.r.t blocksize
bwnet <- blockwiseModules(norm.counts,
maxBlockSize = 14000,
TOMType = "signed",
power = soft_power,
mergeCutHeight = 0.25,
numericLabels = FALSE,
randomSeed = 1234,
verbose = 3)
cor <- temp_cor
# 5. Module Eigengenes ---------------------------------------------------------
module_eigengenes <- bwnet$MEs
# Print out a preview
head(module_eigengenes)
# get number of genes for each module
table(bwnet$colors)
# Plot the dendrogram and the module colors before and after merging underneath
plotDendroAndColors(bwnet$dendrograms[[1]], cbind(bwnet$unmergedColors, bwnet$colors),
c("unmerged", "merged"),
dendroLabels = FALSE,
addGuide = TRUE,
hang= 0.03,
guideHang = 0.05)
# grey module = all genes that doesn't fall into other modules were assigned to the grey module
# 6A. Relate modules to traits --------------------------------------------------
# module trait associations
# create traits file - binarize categorical variables
traits <- colData %>%
mutate(disease_state_bin = ifelse(grepl('COVID', disease_state), 1, 0)) %>%
select(8)
# binarize categorical variables
colData$severity <- factor(colData$severity, levels = c("Healthy", "Convalescent", "ICU", "Moderate", "Severe"))
severity.out <- binarizeCategoricalColumns(colData$severity,
includePairwise = FALSE,
includeLevelVsAll = TRUE,
minCount = 1)
traits <- cbind(traits, severity.out)
# Define numbers of genes and samples
nSamples <- nrow(norm.counts)
nGenes <- ncol(norm.counts)
module.trait.corr <- cor(module_eigengenes, traits, use = 'p')
module.trait.corr.pvals <- corPvalueStudent(module.trait.corr, nSamples)
# visualize module-trait association as a heatmap
heatmap.data <- merge(module_eigengenes, traits, by = 'row.names')
head(heatmap.data)
heatmap.data <- heatmap.data %>%
column_to_rownames(var = 'Row.names')
CorLevelPlot(heatmap.data,
x = names(heatmap.data)[18:22],
y = names(heatmap.data)[1:17],
col = c("blue1", "skyblue", "white", "pink", "red"))
module.gene.mapping <- as.data.frame(bwnet$colors)
module.gene.mapping %>%
filter(`bwnet$colors` == 'turquoise') %>%
rownames()
# 6B. Intramodular analysis: Identifying driver genes ---------------
# Calculate the module membership and the associated p-values
# The module membership/intramodular connectivity is calculated as the correlation of the eigengene and the gene expression profile.
# This quantifies the similarity of all genes on the array to every module.
module.membership.measure <- cor(module_eigengenes, norm.counts, use = 'p')
module.membership.measure.pvals <- corPvalueStudent(module.membership.measure, nSamples)
module.membership.measure.pvals[1:10,1:10]
# Calculate the gene significance and associated p-values
gene.signf.corr <- cor(norm.counts, traits$data.Severe.vs.all, use = 'p')
gene.signf.corr.pvals <- corPvalueStudent(gene.signf.corr, nSamples)
gene.signf.corr.pvals %>%
as.data.frame() %>%
arrange(V1) %>%
head(25)
# Using the gene significance you can identify genes that have a high significance for trait of interest
# Using the module membership measures you can identify genes with high module membership in interesting modules.