comparison aurora_wgcna.Rmd @ 0:66ef158fa85c draft

Uploaded
author spficklin
date Fri, 22 Nov 2019 19:44:46 -0500
parents
children b14e4bf568b0
comparison
equal deleted inserted replaced
-1:000000000000 0:66ef158fa85c
1 ---
2 title: 'Aurora Galaxy WGCNA Tool: Gene Co-Expression Network Construction & Analysis'
3 output:
4 pdf_document:
5 number_sections: false
6 ---
7
8 ```{r setup, include=FALSE, warning=FALSE, message=FALSE}
9 knitr::opts_chunk$set(error = FALSE, echo = FALSE)
10 ```
11
12 ```{r}
13 # Make a directory for saving the figures.
14 dir.create('figures', showWarnings = FALSE)
15 ```
16
17 # Introduction
18 This report contains step-by-step results from use of the [Aurora Galaxy](https://github.com/statonlab/aurora-galaxy-tools) Weighted Gene Co-expression Network Analysis [WGCNA](https://bmcbioinformatics.biomedcentral.com/articles/10.1186/1471-2105-9-559) tool. This tool wraps the WGCNA R package into a ready-to-use Rmarkdown file. It performs module discovery and network construction using a dataset and optional trait data matrix provided.
19
20 If you provided trait data, a second report will be available with results comparing the trait values to the identified modules.
21
22 This report was generated on:
23 ```{r}
24 format(Sys.time(), "%a %b %d %X %Y")
25 ```
26
27
28 ## About the Input Data
29 ### Gene Expression Matrix (GEM)
30 The gene expression data is an *n* x *m* matrix where *n* rows are the genes, *m* columns are the samples and the elements represent gene expression levels (derived either from Microarray or RNA-Seq). The matrix was provided in a file meething these rules:
31 - Housed in a comma-separated (CSV) file.
32 - The rows represent the gene expression levels
33 - The first column of each row is the gene, transcript or probe name.
34 - The header contains only the sample names and therefore is one value less than the remaining rows of the file.
35
36 ### Trait/Phenotype Matrix
37 The trait/phenotype data is an *n* x *m* matrix where *n* is the samples and *m* are the features such as experimental condition, biosample properties, traits or phenotype values. The matrix is stored in a comma-separated (CSV) file and has a header.
38
39 ## Parameters provided by the user.
40 The following describes the input arguments provided to this tool:
41 ```{r}
42
43 if (!is.null(opt$height_cut)) {
44 print('The cut height for outlier removal of the sample dendrogram:')
45 print(opt$cut_height)
46 }
47
48 if (!is.null(opt$power)) {
49 print('The power to which the gene expression data is raised:')
50 print(opt$power)
51 }
52 print('The minimal size for a module:')
53 print(opt$min_cluster_size)
54
55 print('The block size for dividing the GEM to reduce memory requirements:')
56 print(opt$block_size)
57
58 print('The hard threshold when generating the graph file:')
59 print(opt$hard_threshold)
60
61 print('The character string used to identify missing values in the GEM:')
62 print(opt$missing_value1)
63
64 if (!is.null(opt$trait_data)) {
65 print('The column in the trait data that contains the sample name:')
66 print(opt$sname_col)
67
68 print('The character string used to identify missing values in the trait data:')
69 print(opt$missing_value2)
70
71 print('Columns in the trait data that should be treated as categorical:')
72 print(opt$one_hot_cols)
73
74 print('Columns in the trait data that should be ignored:')
75 print(opt$ignore_cols)
76 }
77 ```
78
79 ## If Errors Occur
80 Please note, that if any of the R code encountered problems, error messages will appear in the report below. If an error occurs anywhere in the report, results should be thrown out. Errors are usually caused by improperly formatted input data or improper input arguments. Use the following checklist to find and correct potential errors:
81
82 - Do the formats for the input datasets match the requirements listed above.
83 - Do the values set for missing values match the values in the input files, and is the missing value used consistently within the input files (i.e you don't have more than one such as 0.0 and 0, or NA and 0.0)
84 - If trait data was provided, check that the column specified for the sample name is correct.
85 - The block size should not exceed 10,000 and should not be lower than 1,000.
86 - Ensure that the sample names and all headers in the trait/phenotype data only contain alpha-numeric and underscore characters.
87
88
89 # Expression Data
90
91 The content below shows the first 10 rows and 6 columns of the Gene Expression Matrix (GEM) file that was provided.
92
93 ```{r}
94 gem = read.csv(opt$expression_data, header = TRUE, row.names = 1, na.strings = opt$missing_value1)
95 #table_data = head(gem, 100)
96 #datatable(table_data)
97 gem[1:10,1:6]
98 ```
99
100 ```{r}
101 gemt = as.data.frame(t(gem))
102 ```
103
104 The next step is to check the data for low quality samples or genes. These have too many missing values or consist of genes with zero-variance. Samples and genes are removed if they are low quality. The `goodSamplesGenes` function of WGCNA is used to identify such cases. The following cell indicates if WGCNA identified any low quality genes or samples, and these were removed.
105
106
107 ```{r}
108 gsg = goodSamplesGenes(gemt, verbose = 3)
109
110 if (!gsg$allOK) {
111 gemt = gemt[gsg$goodSamples, gsg$goodGenes]
112 } else {
113 print('all genes are OK!')
114 }
115 ```
116
117
118 Hierarchical clustering can be used to explore the similarity of expression across the samples of the GEM. The following dendrogram shows the results of that clustering. Outliers typically appear on their own in the dendrogram. If a height was not specified to trim outlier samples, then the `cutreeDynamic` function is used to automatically find outliers, and then they are removed. If you do not approve of the automatically detected height, you can re-run this tool with a desired cut height. The two plots below show the dendrogram before and after outlier removal.
119
120 ```{r fig.align='center'}
121 sampleTree = hclust(dist(gemt), method = "average");
122
123 plotSampleDendro <- function() {
124 plot(sampleTree, main = "Sample Clustering Prior to Outlier Removal", sub="", xlab="",
125 cex.axis = 1, cex.main = 1, cex = 0.5)
126 }
127 png('figures/01-sample_dendrogram.png', width=6 ,height=5, units="in", res=300)
128 plotSampleDendro()
129 invisible(dev.off())
130 plotSampleDendro()
131 ```
132
133 ```{r}
134 if (is.null(opt$height_cut)) {
135 print("You did not specify a height for cutting the dendrogram. The cutreeDynamic function was used.")
136 clust = cutreeDynamic(sampleTree, method="tree", minClusterSize = opt$min_cluster_size)
137 keepSamples = (clust!=0)
138 gemt = gemt[keepSamples, ]
139 } else {
140 print("You specified a height for cutting of", opt$height_cut, ". The cutreeStatic function was used.")
141 clust = cutreeStatic(sampleTree, cutHeight = opt$height_cut, minSize = opt$min_cluster_size)
142 keepSamples = (clust==1)
143 gemt = gemt[keepSamples, ]
144 }
145 n_genes = ncol(gemt)
146 n_samples = nrow(gemt)
147 removed = length(which(keepSamples == FALSE))
148 if (removed == 1) {
149 print(paste("A total of", removed, "sample was removed"))
150 } else {
151 print(paste("A total of", removed, "samples were removed"))
152 }
153
154 # Write out the filtered GEM
155 write.csv(t(gemt), opt$filtered_GEM, quote=FALSE)
156 ```
157 A file named `filtered_GEM.csv` has been created. This file is a comma-separated file containing the original gene expression data but with outlier samples removed. If no outliers were detected this file will be identical to the original.
158
159 ```{r fig.align='center'}
160 sampleTree = hclust(dist(gemt), method = "average");
161
162 plotFilteredSampleDendro <- function() {
163 plot(sampleTree, main = "Sample Clustering After Outlier Removal", sub="", xlab="",
164 cex.axis = 1, cex.main = 1, cex = 0.5)
165 }
166 png('figures/02-filtered-sample_dendrogram.png', width=6 ,height=5, units="in", res=300)
167 plotFilteredSampleDendro()
168 invisible(dev.off())
169 plotFilteredSampleDendro()
170 ```
171
172 # Network Module Discovery
173
174 The first step in network module discovery is calculating similarity of gene expression. This is performed by comparing the expression of every gene with every other gene using a correlation test. However, the WGCNA authors suggest that raising the GEM to a power that best approximates scale-free behavior improves the quality of the final modules. However, the power to which the data should be raised is initially unknown. This is determined using the `pickSoftThreshold` function of WGCNA which iterates through a series of power values (usually between 1 to 20) and tests how well the data approximates scale-free behavior. The following table shows the results of those tests. The meaning of the table headers are:
175
176 - Power: The power tested
177 - SFT.R.sq: This is the scale free index, or the R.squared value of the undelrying regression model. It indicates how well the power-raised data appears scale free. The higher the value the more scale-free.
178 - slope: The slope of the regression line used to calculate SFT.R.sq
179 - trunacted.R.sq: The adjusted R.squared measure from the truncated exponential model used to calculate SFT.R.sq
180 - mean.k: The mean degree (degree is a measure of how connected a gene is to every other gene. The higher the number the more connected.)
181 - median.k: The median degree
182 - max.k: The largest degree.
183
184 ```{r}
185 powers = c(1:10, seq(12, 20, 2))
186 sft = pickSoftThreshold(gemt, powerVector = powers, verbose = 5)
187 ```
188
189 The following plots show how the scale-free index and mean connectivity change as the power is adjusted. The ideal power value for the network should be the value where there is a diminishing change in both the scale-free index and mean connectivity.
190
191 ```{r fig.align='center'}
192 par(mfrow=c(1,2))
193 th = sft$fitIndices$SFT.R.sq[which(sft$fitIndices$Power == sft$powerEstimate)]
194
195 plotPower <- function() {
196 # Scale-free topology fit index as a function of the soft-thresholding power.
197 plot(sft$fitIndices[,1], -sign(sft$fitIndices[,3])*sft$fitIndices[,2],
198 xlab="Soft Threshold (power)",
199 ylab="Scale Free Topology Model Fit,signed R^2", type="n",
200 main = paste("Scale Independence"), cex.lab = 0.5);
201 text(sft$fitIndices[,1], -sign(sft$fitIndices[,3])*sft$fitIndices[,2],
202 labels=powers,cex=0.5,col="red");
203 #abline(h=th, col="blue")
204
205 # Mean connectivity as a function of the soft-thresholding power.
206 plot(sft$fitIndices[,1], sft$fitIndices[,5],
207 xlab="Soft Threshold (power)",ylab="Mean Connectivity", type="n",
208 main = paste("Mean Connectivity"), cex.lab = 0.5)
209 text(sft$fitIndices[,1], sft$fitIndices[,5], labels=powers, cex=0.5,col="red")
210 #abline(h=th, col="blue")
211 par(mfrow=c(1,1))
212 }
213
214 png('figures/03-power_thresholding.png', width=6 ,height=5, units="in", res=300)
215 plotPower()
216 invisible(dev.off())
217 plotPower()
218 ```
219 Using the values in the table above, WGCNA is able to predict the ideal power. This selection is indicated in the following cell and is shown as a blue line on the plots above. If you believe that the power was incorrectly chosen, you can re-run this tool with the same input files and provide the desired power.
220
221 ```{r}
222 print("WGCNA predicted the following power:")
223 print(sft$powerEstimate)
224 power = sft$powerEstimate
225 if (!is.null(opt$power)) {
226 print("However, you selected to override this by providing a power of:", opt$soft_threshold_power)
227 print(opt$soft_threshold_power)
228 power = opt$power
229 }
230 ```
231
232 Now that a power has been identified, modules can be discovered. Here, the `blockwiseModule` function of WGCNA is called. The dataset is divided into blocks of genes in order to keep memory usage low. The output of that function call is shown below. The number of blocks is dependent on the block size you provided.
233
234 ```{r}
235 net = blockwiseModules(gemt, power = power, maxBlockSize = opt$block_size,
236 TOMType = "unsigned", minModuleSize = opt$min_cluster_size,
237 reassignThreshold = 0, mergeCutHeight = 0.25,
238 numericLabels = TRUE, pamRespectsDendro = FALSE,
239 verbose = 1, saveTOMs = TRUE,
240 saveTOMFileBase = "TOM")
241 blocks = sort(unique(net$blocks))
242 ```
243 The following table shows the list of modules that were discovered and their size (i.e. number of genes).
244
245 ```{r}
246 module_labels = labels2colors(net$colors)
247 module_labels = paste("ME", module_labels, sep="")
248 module_labels2num = unique(data.frame(label = module_labels, num = net$color, row.names=NULL))
249 rownames(module_labels2num) = paste0('ME', module_labels2num$num)
250 modules = unique(as.data.frame(table(module_labels)))
251 n_modules = length(modules) - 1
252 module_size_upper = modules[2]
253 module_size_lower = modules[length(modules)]
254 colnames(modules) = c('Module', 'Module Size')
255 #datatable(modules)
256 modules
257 ```
258
259 Modules consist of a set of genes that have highly similar expression patterns. Therefore, the similarity of genes within a module can be summarized using an "eigengene" vector. This vector is analgous to the first principal component in a PCA analysis. Once each module's eigengene is calculated, they can be compared and displayed in dendrogram to identify which modules are most similar to each other. This is visible in the following plot.
260
261 ```{r fig.align='center'}
262 MEs = net$MEs
263 colnames(MEs) = module_labels2num[colnames(MEs),]$label
264
265 png('figures/04-module_dendrogram.png', width=6 ,height=5, units="in", res=300)
266 plotEigengeneNetworks(MEs, "Module Eigengene Dendrogram", plotHeatmaps = FALSE)
267 dev.off()
268 plotEigengeneNetworks(MEs, "Module Eigengene Dendrogram", plotHeatmaps = FALSE)
269 ```
270
271 Alternatively, we can use a heatmap to explore similarity of each module.
272 ```{r fig.align='center'}
273 plotModuleHeatmap <- function() {
274 plotEigengeneNetworks(MEs, "Module Eigengene Heatmap",
275 marHeatmap = c(2, 3, 2, 2),
276 plotDendrograms = FALSE)
277 }
278 png('figures/05-module_eigengene_heatmap.png', width=4 ,height=4, units="in", res=300)
279 plotModuleHeatmap()
280 invisible(dev.off())
281 plotModuleHeatmap()
282 ```
283
284 We can examine gene similarity within the context of our modules. The following dendrogram clusters genes by their similarity of expression and the modules to which each gene belongs is shown under the graph. When similar genes appear in the same module, the same colors will be visible in "blocks" under the dendrogram. The presence of blocks of color indicate that genes in modules tend to have similar expression.
285
286 ```{r}
287 # Plot the dendrogram and the module colors underneath
288 for (i in blocks) {
289 options(repr.plot.width=15, repr.plot.height=10)
290 colors = module_labels[net$blockGenes[[i]]]
291 colors = sub('ME','', colors)
292 plotClusterDendro <- function() {
293 plotDendroAndColors(net$dendrograms[[i]], colors,
294 "Module colors", dendroLabels = FALSE, hang = 0.03,
295 addGuide = TRUE, guideHang = 0.05,
296 main=paste('Cluster Dendgrogram, Block', i))
297 }
298 png(paste0('figures/06-cluster_dendrogram_block_', i, '.png'), width=6 ,height=4, units="in", res=300)
299 plotClusterDendro();
300 invisible(dev.off())
301 plotClusterDendro();
302 }
303
304 ```
305
306 The network is housed in a *n* x *n* similarity matrix known as the the Topological Overlap Matrix (TOM), where *n* is the number of genes and the value in each cell indicates the measure of similarity in terms of correlation of expression and interconnectedness. The following heat maps shows the TOM. Note, that the dendrograms in the TOM heat map may differ from what is shown above. This is because a subset of genes were selected to draw the heat maps in order to save on computational time.
307
308 ```{r}
309 for (i in blocks) {
310 # Load the TOM from a file.
311 load(net$TOMFiles[i])
312 TOM_size = length(which(net$blocks == i))
313 TOM = as.matrix(TOM, nrow=TOM_size, ncol=TOM_size)
314 dissTOM = 1-TOM
315
316 # For reproducibility, we set the random seed
317 set.seed(10);
318 select = sample(dim(TOM)[1], size = 1000);
319 selectColors = module_labels[net$blockGenes[[i]][select]]
320 selectTOM = dissTOM[select, select];
321
322 # There’s no simple way of restricting a clustering tree to a subset of genes, so we must re-cluster.
323 selectTree = hclust(as.dist(selectTOM), method = "average")
324
325 # Taking the dissimilarity to a power, say 10, makes the plot more informative by effectively changing
326 # the color palette; setting the diagonal to NA also improves the clarity of the plot
327 plotDiss = selectTOM^7;
328 diag(plotDiss) = NA;
329 colors = sub('ME','', selectColors)
330
331 png(paste0('figures/06-TOM_heatmap_block_', i, '.png'), width=6 ,height=6, units="in", res=300)
332 TOMplot(plotDiss, selectTree, colors, main = paste('TOM Heatmap, Block', i))
333 dev.off()
334 TOMplot(plotDiss, selectTree, colors, main = paste('TOM Heatmap, Block', i))
335 }
336 ```
337
338 ```{r}
339 output = cbind(colnames(gemt), module_labels)
340 colnames(output) = c('Gene', 'Module')
341 write.csv(output, file = opt$gene_module_file, quote=FALSE, row.names=FALSE)
342 ```
343
344 A file has been generated named `gene_module_file.csv` which contains the list of genes and the modules they belong to.
345
346 The TOM serves as both a simialrity matrix and an adjacency matrix. The adjacency matrix is typically identical to a similarity matrix but with values above a set threshold set to 1 and values below set to 0. This is known as hard thresholding. However, WGCNA does not set values above a threshold to zero but leaves the values as they are, hence the word "weighted" in the WGCNA name. Additionally, it does not use a threshold at all, so no elements are set to 0. This approach is called "soft thresholding", because the pairwise weights of all genes contributed to discover of modules. The name "soft thresholding" may be a misnomer, however, because no thresholding in the traditional sense actually occurs.
347
348 Unfortunately, this "soft thresholding" approach can make creation of a graph representation of the network difficult. If we exported the TOM as a connected graph it would result in a fully complete graph and would be difficult to interpret. Therefore, we must still set a hard-threshold if we want to visualize connectivity in graph form. Setting a hard threshold, if too high can result in genes being excluded from the graph and a threshold that is too low can result in too many false edges in the graph.
349
350 ```{r}
351 edges = data.frame(fromNode= c(), toNode=c(), weight=c(), direction=c(), fromAltName=c(), toAltName=c())
352 for (i in blocks) {
353 # Load the TOM from a file.
354 load(net$TOMFiles[i])
355 TOM_size = length(which(net$blocks == i))
356 TOM = as.matrix(TOM, nrow=TOM_size, ncol=TOM_size)
357 colnames(TOM) = colnames(gemt)[net$blockGenes[[i]]]
358 row.names(TOM) = colnames(gemt)[net$blockGenes[[i]]]
359
360 cydata = exportNetworkToCytoscape(TOM, threshold = opt$hard_threshold)
361 edges = rbind(edges, cydata$edgeData)
362 }
363
364 edges$Interaction = 'co'
365 output = edges[,c('fromNode','toNode','Interaction', 'weight')]
366 colnames(output) = c('Source', 'Target', 'Interaction', 'Weight')
367 write.table(output, file = opt$network_edges_file, quote=FALSE, row.names=FALSE, sep="\t")
368 ```
369
370 Using the hard threshold parameter provided, a file has been generated named `network_edges.txt` which contains the list of edges. You can import this file into [Cytoscape](https://cytoscape.org/) for visualization. If you would like a larger graph, you must re-run the tool with a smaller threshold.
371
372 ```{r}
373 # Save this image for the next step which is optional if theuser
374 # provides a trait file.
375 save.image(file=opt$r_data)
376 ```