-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwgcna.R
206 lines (160 loc) · 5.61 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
# Title: Weighted correlation network analysis (WGCNA) network
# Author: Albert García López
pacman::p_load(tidyverse, devtools, WGCNA, Seurat, doParallel)
options(readr.show_col_types = FALSE, stringsAsFactors = FALSE)
outDir <- "path_to_dir"
dataDir <- "path_to_dir"
stopifnot(dir.exists(outDir) & dir.exists(dataDir))
# Load counts ##################################################################
# NOTE: Each row corresponds to a gene and each column to a sample or
# auxiliary information.
SeuratObj <- ReadMtx(
mtx = file.path(dataDir, "matrix.mtx.gz"),
cells = file.path(dataDir, "barcodes.tsv.gz"),
features = file.path(dataDir, "features.tsv.gz")
) %>%
CreateSeuratObject()
counts_mat <- GetAssayData(SeuratObj, slot = "counts") %>%
as.matrix()
# Counts matrix contains NAs?
stopifnot(anyNA(counts_mat) == FALSE)
# Define metadata ##############################################################
metadata <- counts_mat %>%
as.data.frame() %>%
rownames_to_column("gene_id") %>%
filter(gene_id == "IL1A") %>%
pivot_longer(-gene_id, names_to = "cells", values_to = "values") %>%
mutate(type = ifelse(values != 0, "IL1A_positive", "IL1A_negative")) %>%
select(cells, values, type)
il1a_pos_cells <- metadata %>%
filter(type == "IL1A_positive") %>%
pull(cells)
# Filter counts for IL1A+ samples only and transpose the expression data for
# further analysis.
counts_filtered <- counts_mat %>%
as.data.frame() %>%
select(all_of(il1a_pos_cells)) %>%
t() %>%
as.data.frame()
# Data input, cleaning and pre-processing ######################################
# Checking data for excessive missing values and identification of outliers
gsg <- goodSamplesGenes(datExpr = counts_filtered)
genes_to_keep <- which(gsg$goodGenes == TRUE)
samples_to_keep <- which(gsg$goodSamples == TRUE)
counts_good <- counts_filtered[samples_to_keep, genes_to_keep]
# Automatic construction of the gene network and identification of modules #####
registerDoParallel(cores = 4)
# Pick soft-thresholding power: analysis of network topology #
sft <- pickSoftThreshold(
data = counts_good,
dataIsExpr = TRUE
)
# One-step network construction and module detection ###########################
net <- blockwiseModules(
datExpr = counts_good,
power = sft$powerEstimate,
randomSeed = 123,
maxBlockSize = ncol(counts_good),
minModuleSize = 2,
networkType = "unsigned",
TOMType = "signed",
saveTOMs = TRUE,
reassignThreshold = 0,
numericLabels = TRUE,
verbose = 0,
nThreads = 20
)
table(net$colors)
il1a_module <- net$colors %>%
enframe() %>%
filter(name == "IL1A") %>%
pull(value)
il1a_related_genes <- net$colors %>%
enframe() %>%
filter(value == il1a_module) %>%
pull(name)
# Annotation and enrichment analysis ###########################################
library(biomaRt)
il1a_related_genes_info <- getBM(
attributes = c("ensembl_gene_id", "external_gene_name", "entrezgene_id", "gene_biotype"),
filters = "external_gene_name",
values = il1a_related_genes,
mart = useMart(
biomart = "ENSEMBL_MART_ENSEMBL",
dataset = "hsapiens_gene_ensembl"
)
) %>%
as_tibble()
all_genes_info <- getBM(
attributes = c("ensembl_gene_id", "external_gene_name", "entrezgene_id", "gene_biotype"),
filters = "external_gene_name",
values = names(net$colors),
mart = useMart(
biomart = "ENSEMBL_MART_ENSEMBL",
dataset = "hsapiens_gene_ensembl"
)
) %>%
as_tibble()
unload("biomaRt")
all_genes_info_complete <- all_genes_info %>%
drop_na(entrezgene_id)
set.seed(123)
go_enrichment <- GOenrichmentAnalysis(
labels = net$colors[all_genes_info_complete$external_gene_name],
entrezCodes = all_genes_info_complete$entrezgene_id,
organism = "human",
ontologies = c("CC", "BP", "MF"),
evidence = "all",
pCut = 1
)
go_enrichment_df <- go_enrichment$bestPTerms$`CC, BP, MF`$enrichment %>%
as_tibble()
il1a_enrichment <- go_enrichment_df %>%
filter(module == il1a_module)
write_tsv(
x = il1a_enrichment,
file = "file_name.tsv")
)
# Load TOM and filter for IL1A module ##########################################
load("block.RData")
TOM_mat <- as.matrix(TOM)
dimnames(TOM_mat) <- list(names(counts_good), names(counts_good))
# Get TOM for IL1A related genes
TOM_IL1A <- TOM_mat[il1a_related_genes_info$external_gene_name, il1a_related_genes_info$external_gene_name]
# Filter counts belonging to module IL1A
counts_il1a <- counts_good %>%
as.data.frame() %>%
dplyr::select(all_of(il1a_related_genes_info$external_gene_name)) %>%
as.matrix()
# Make correlation matrix
il1a_cor <- cor(x = counts_il1a, method = "spearman") %>%
as.data.frame() %>%
rownames_to_column("Target") %>%
pivot_longer(cols = -Target, names_to = "Source", values_to = "Corr") %>%
filter(Target != Source) %>%
dplyr::rename(fromAltName = Target, toAltName = Source) %>%
mutate(`shared name` = paste(fromAltName, toAltName, sep = " (interacts with) ")) %>%
dplyr::select(`shared name`, everything()) %>%
as.data.frame()
write_tsv(
x = il1a_cor,
file = "file_name.tsv"
)
# Export to Cytoscape
cyt <- exportNetworkToCytoscape(
adjMat = TOM_IL1A,
edgeFile = paste0(outDir, "Cytoscape_edges_", paste(il1a_module, collapse = "_signed"), ".txt"),
nodeFile = paste0(outDir, "Cytoscape_nodes_", paste(il1a_module, collapse = "_signed"), ".txt"),
weighted = TRUE,
threshold = 0,
nodeNames = colnames(TOM_IL1A),
altNodeNames = colnames(TOM_IL1A),
nodeAttr = net$colors[il1a_related_genes_info$external_gene_name]
)
il1a_network_nodes_info <- il1a_cor %>%
filter(fromAltName == "IL1A") %>%
dplyr::select(`shared name` = toAltName, name = toAltName, Corr)
write_tsv(
x = il1a_network_nodes_info,
file = "file_name.tsv"
)