From 8267148d040cf52a599dc0c379a0a0cf874811bf Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 16 Nov 2020 14:58:23 -0500 Subject: [PATCH 01/16] Put in basic changes: navbar, dict, snakefile, Rmd --- .../network-analysis_rnaseq_01_wgcna.Rmd | 552 ++ .../network-analysis_rnaseq_01_wgcna.html | 4429 +++++++++++++++++ Snakefile | 8 +- components/_navbar.html | 6 +- components/dictionary.txt | 6 +- 5 files changed, 4992 insertions(+), 9 deletions(-) create mode 100644 04-advanced-topics/network-analysis_rnaseq_01_wgcna.Rmd create mode 100644 04-advanced-topics/network-analysis_rnaseq_01_wgcna.html diff --git a/04-advanced-topics/network-analysis_rnaseq_01_wgcna.Rmd b/04-advanced-topics/network-analysis_rnaseq_01_wgcna.Rmd new file mode 100644 index 00000000..a8b8c63a --- /dev/null +++ b/04-advanced-topics/network-analysis_rnaseq_01_wgcna.Rmd @@ -0,0 +1,552 @@ +--- +title: "WGCNA - RNA-seq" +author: "CCDL for ALSF" +date: "October 2020" +output: + html_notebook: + toc: true + toc_float: true + number_sections: true +--- + +**Draft** + +# Purpose of this analysis + +In this example, we use [weighted gene co-expression network analysis (WGCNA)]() to identify co-expressed gene modules. +WGCNA using a series of correlations to determine what genes are expressed together in your data set. + +⬇️ [**Jump to the analysis code**](#analysis) ⬇️ + +# How to run this example + +For general information about our tutorials and the basic software packages you will need, please see our ['Getting Started' section](https://alexslemonade.github.io/refinebio-examples/01-getting-started/getting-started.html#how-this-tutorial-is-structured). +We recommend taking a look at our [Resources for Learning R](https://alexslemonade.github.io/refinebio-examples/01-getting-started/getting-started.html#resources-for-learning-r) if you have not written code in R before. + +## Obtain the `.Rmd` file + +To run this example yourself, [download the `.Rmd` for this analysis by clicking this link](https://alexslemonade.github.io/refinebio-examples/03-rnaseq/differential_expression_rnaseq_01_rnaseq.Rmd). + +Clicking this link will most likely send this to your downloads folder on your computer. +Move this `.Rmd` file to where you would like this example and its files to be stored. + +You can open this `.Rmd` file in RStudio and follow the rest of these steps from there. (See our [section about getting started with R notebooks](https://alexslemonade.github.io/refinebio-examples/01-getting-started/getting-started.html#how-to-get-and-use-rmds) if you are unfamiliar with `.Rmd` files.) + +## Set up your analysis folders + +Good file organization is helpful for keeping your data analysis project on track! +We have set up some code that will automatically set up a folder structure for you. +Run this next chunk to set up your folders! + +If you have trouble running this chunk, see our [introduction to using `.Rmd`s](https://alexslemonade.github.io/refinebio-examples/01-getting-started/getting-started.html#how-to-get-and-use-rmds) for more resources and explanations. + +```{r} +# Create the data folder if it doesn't exist +if (!dir.exists("data")) { + dir.create("data") +} + +# Define the file path to the plots directory +plots_dir <- "plots" # Can replace with path to desired output plots directory + +# Create the plots folder if it doesn't exist +if (!dir.exists(plots_dir)) { + dir.create(plots_dir) +} + +# Define the file path to the results directory +results_dir <- "results" # Can replace with path to desired output results directory + +# Create the results folder if it doesn't exist +if (!dir.exists(results_dir)) { + dir.create(results_dir) +} +``` + +In the same place you put this `.Rmd` file, you should now have three new empty folders called `data`, `plots`, and `results`! + +## Obtain the dataset from refine.bio + +For general information about downloading data for these examples, see our ['Getting Started' section](https://alexslemonade.github.io/refinebio-examples/01-getting-started/getting-started.html#how-to-get-the-data). + +Go to this [dataset's page on refine.bio](https://www.refine.bio/experiments/SRP133573/identification-of-transcription-factor-relationships-associated-with-androgen-deprivation-therapy-response-and-metastatic-progression-in-prostate-cancer). + +Click the "Download Now" button on the right side of this screen. + + + +Fill out the pop up window with your email and our Terms and Conditions: + + + +We are going to use non-quantile normalized data for this analysis. +To get this data, you will need to check the box that says "Skip quantile normalization for RNA-seq samples". +Note that this option will only be available for RNA-seq datasets. + + + +It may take a few minutes for the dataset to process. +You will get an email when it is ready. + +## About the dataset we are using for this example + +For this example analysis, we will use this [prostate cancer dataset](https://www.refine.bio/experiments/SRP133573). +The data that we downloaded from refine.bio for this analysis has 175 RNA-seq samples obtained from 20 patients with prostate cancer. +Patients underwent androgen deprivation therapy (ADT) and RNA-seq samples include pre-ADT biopsies and post-ADT prostatectomy specimens. + +## Place the dataset in your new `data/` folder + +refine.bio will send you a download button in the email when it is ready. +Follow the prompt to download a zip file that has a name with a series of letters and numbers and ends in `.zip`. +Double clicking should unzip this for you and create a folder of the same name. + + + +For more details on the contents of this folder see [these docs on refine.bio](http://docs.refine.bio/en/latest/main_text.html#downloadable-files). + +The `` folder has the data and metadata TSV files you will need for this example analysis. +Experiment accession ids usually look something like `GSE1235` or `SRP12345`. + +Copy and paste the `SRP133573` folder into your newly created `data/` folder. + +## Check out our file structure! + +Your new analysis folder should contain: + +- The example analysis `.Rmd` you downloaded +- A folder called "data" which contains: + - The `SRP133573` folder which contains: + - The gene expression + - The metadata TSV +- A folder for `plots` (currently empty) +- A folder for `results` (currently empty) + +Your example analysis folder should now look something like this (except with respective experiment accession ID and analysis notebook name you are using): + + + +In order for our example here to run without a hitch, we need these files to be in these locations so we've constructed a test to check before we get started with the analysis. +These chunks will declare your file paths and double check that your files are in the right place. + +First we will declare our file paths to our data and metadata files, which should be in our data directory. +This is handy to do because if we want to switch the dataset (see next section for more on this) we are using for this analysis, we will only have to change the file path here to get started. + +```{r} +# Define the file path to the data directory +data_dir <- file.path("data", "SRP133573") # Replace with accession number which will be the name of the folder the files will be in + +# Declare the file path to the gene expression matrix file using the data directory saved as `data_dir` +data_file <- file.path(data_dir, "SRP133573.tsv") # Replace with file path to your dataset + +# Declare the file path to the metadata file using the data directory saved as `data_dir` +metadata_file <- file.path(data_dir, "metadata_SRP133573.tsv") # Replace with file path to your metadata +``` + +Now that our file paths are declared, we can use the `file.exists()` function to check that the files are where we specified above. + +```{r} +# Check if the gene expression matrix file is at the file path stored in `data_file` +file.exists(data_file) + +# Check if the metadata file is at the file path stored in `metadata_file` +file.exists(metadata_file) +``` + +If the chunk above printed out `FALSE` to either of those tests, you won't be able to run this analysis _as is_ until those files are in the appropriate place. + +If the concept of a "file path" is unfamiliar to you; we recommend taking a look at our [section about file paths](https://alexslemonade.github.io/refinebio-examples/01-getting-started/getting-started.html#an-important-note-about-file-paths-and-Rmds). + +# Using a different refine.bio dataset with this analysis? + +If you'd like to adapt an example analysis to use a different dataset from [refine.bio](https://www.refine.bio/), we recommend placing the files in the `data/` directory you created and changing the filenames and paths in the notebook to match these files (we've put comments to signify where you would need to change the code). +We suggest saving plots and results to `plots/` and `results/` directories, respectively, as these are automatically created by the notebook. +From here you can customize this analysis example to fit your own scientific questions and preferences. + +Keep in mind that WGCNA requires at least 15 samples to produce a meaningful result [according to its authors](https://horvath.genetics.ucla.edu/html/CoexpressionNetwork/Rpackages/WGCNA/faq.html). So you will need to make sure the dataset you switch this to is sufficiently large. + +*** + +   + +# Identifying co-expression gene modules with WGCNA - RNA-seq + +## Install libraries + +See our Getting Started page with [instructions for package installation](https://alexslemonade.github.io/refinebio-examples/01-getting-started/getting-started.html#what-you-need-to-install) for a list of the other software you will need, as well as more tips and resources. + +In this analysis, we will be using [`WGCNA`](https://cran.r-project.org/web/packages/WGCNA/index.html) package. +WGCNA also requires a package called `impute` that is sometimes has trouble installing so we recommend installing that first. + +```{r} +if (!("DESeq2" %in% installed.packages())) { + # Install this package if it isn't installed yet + BiocManager::install("DESeq2", update = FALSE) +} + +if (!("impute" %in% installed.packages())) { + # Install this package if it isn't installed yet + BiocManager::install("impute") +} + +if (!("WGCNA" %in% installed.packages())) { + # Install this package if it isn't installed yet + install.packages("WGCNA") +} + +if (!("ggforce" %in% installed.packages())) { + # Install this package if it isn't installed yet + install.packages("ggforce") +} +``` + +Attach the packages we need for this analysis. + +```{r} +# Attach the DESeq2 library +library(DESeq2) + +# Homo sapiens annotation package we'll use for gene identifier conversion +library(org.Hs.eg.db) + +# We will need this so we can use the pipe: %>% +library(magrittr) + +# We'll need this for finding gene modules +library(WGCNA) +``` + +## Import and set up data + +Data downloaded from refine.bio include a metadata tab separated values (TSV) file and a data TSV file. +This chunk of code will read the both TSV files and add them as data frames to your environment. + +We stored our file paths as objects named `metadata_file` and `data_file` in [this previous step](#check-out-our-file-structure). + +```{r} +# Read in metadata TSV file +metadata <- readr::read_tsv(metadata_file) + +# Read in data TSV file +df <- readr::read_tsv(data_file) %>% + # Here we are going to store the gene IDs as rownames so that we can have a numeric matrix to perform calculations on later + tibble::column_to_rownames("Gene") +``` + +Let's ensure that the metadata and data are in the same sample order. + +```{r} +# Make the data in the order of the metadata +df <- df %>% + dplyr::select(metadata$refinebio_accession_code) + +# Check if this is in the same order +all.equal(colnames(df), metadata$refinebio_accession_code) +``` + +### Prepare data for `DESeq2` + +We need to make sure all of the values in our data are converted to integers as required by a `DESeq2` function we will use later. + +```{r} +# The `DESeqDataSetFromMatrix()` function needs the values to be converted to integers +df <- df %>% + # Bring out rownames and store as their own column + tibble::rownames_to_column("Gene") %>% + # Mutate numeric variables to be integers + dplyr::mutate_if(is.numeric, round) +``` + +## Create a DESeqDataset + +We will be using the `DESeq2` package for [normalizing and transforming our data](https://alexslemonade.github.io/refinebio-examples/03-rnaseq/00-intro-to-rnaseq.html#deseq2-transformation-methods), which requires us to format our data into a `DESeqDataSet` object. +We turn the data frame (or matrix) into a [`DESeqDataSet` object](https://alexslemonade.github.io/refinebio-examples/03-rnaseq/00-intro-to-rnaseq.html#02_About_DESeq2). ) and specify which variable labels our experimental groups using the [`design` argument](http://bioconductor.org/packages/devel/bioc/vignettes/DESeq2/inst/doc/DESeq2.html#multi-factor-designs) [@Love2014]. +In this chunk of code, we will not provide a specific model to the `design` argument because we are not performing a differential expression analysis. + +```{r} +# Create a `DESeqDataSet` object +dds <- DESeqDataSetFromMatrix( + countData = df, # This is the data frame with the counts values for all replicates in our dataset + colData = metadata, # This is the data frame with the annotation data for the replicates in the counts data frame + design = ~1, # Here we are not specifying a model -- Replace with an appropriate design variable for your analysis + tidy = TRUE +) +``` + +## Define a minimum counts cutoff + +We want to filter out the genes that have not been expressed or that have low expression counts. +This is recommended by [WGCNA docs for RNA-seq data](https://horvath.genetics.ucla.edu/html/CoexpressionNetwork/Rpackages/WGCNA/faq.html#:~:text=Can%20WGCNA%20be%20used%20to,Yes.&text=Whether%20one%20uses%20RPKM%2C%20FPKM,were%20processed%20the%20same%20way.). +Removing low count genes can help improve your WGCNA results. +We are going to do some pre-filtering to keep only genes with 10 or more reads in total across the samples. + +```{r} +# Define a minimum counts cutoff and filter `DESeqDataSet` object to include +# only rows that have counts above the cutoff +genes_to_keep <- rowSums(counts(dds)) >= 50 +dds <- dds[genes_to_keep, ] +``` + +## Perform DESeq2 normalization and transformation + +We often suggest normalizing and transforming your data for various applications and in this instance WGCNA's authors [suggest using variance stabilizing transformation before running WGCNA](https://horvath.genetics.ucla.edu/html/CoexpressionNetwork/Rpackages/WGCNA/faq.html#:~:text=Can%20WGCNA%20be%20used%20to,Yes.&text=Whether%20one%20uses%20RPKM%2C%20FPKM,were%20processed%20the%20same%20way.). +We are going to use the `vst()` function from the `DESeq2` package to normalize and transform the data. +For more information about these transformation methods, [see here](https://alexslemonade.github.io/refinebio-examples/03-rnaseq/00-intro-to-rnaseq.html#deseq2-transformation-methods). + +```{r} +# Normalize and transform the data in the `DESeqDataSet` object using the `vst()` function from the `DESEq2` R package +dds_norm <- vst(dds) +``` + +## Format normalized data for WGCNA + +Here we will use WGCNA to identify co-expressed gene modules. + +Extract the normalized counts to a matrix and transpose it so we can pass it to WGCNA. + +```{r} +# First we are going to retrieve the normalized data from the `DESeqDataSet` object using the `assay()` function +normalized_counts <- assay(dds_norm) %>% + t() # We need to transpose this data in preparation for the `WGCNA::blockwiseModules()` function +``` + +## Determine parameters for WGCNA + +**Draft** + +WGCNA has to decide what genes are included in which modules, but it might encounter some genes that are close to call between modules. +By providing a soft-threshold power parameter, we can guide it on to what exponent it should compare R values for deciding between modules for a gene. +The "soft" part of a soft threshold just means that this will not be an absolute cutoff and WGCNA will use this parameter with some flexibility. + +```{r} +sft <- pickSoftThreshold(normalized_counts, + dataIsExpr = TRUE, + corFnc = cor, + networkType = "signed" +) +``` + +This `sft` object has a lot of information, we will want to plot some of it to figure out what our `power` soft-threshold should be. +We have to first calculate the model fit, the R squared and make that a new variable. + +```{r} +sft_df <- data.frame(sft$fitIndices) %>% + dplyr::mutate(model_fit = -sign(slope) * SFT.R.sq) +``` + +Now, let's plot the model fitting by the `power` soft threshold so we can decide on a soft-threshold for power. + +```{r} +sft_df %>% + ggplot2::ggplot() + + ggplot2::geom_label(ggplot2::aes(x = Power, y = model_fit), label = sft_df$Power) + + # We will plot what WGCNA recommends as an R^2 cutoff + ggplot2::geom_hline(yintercept = 0.80, col = "red") + + # Just in case our values are low, we want to make sure we can still see the 0.80 level + ggplot2::ylim(c(min(sft_df$model_fit), 1)) + + # We can add more sensible labels for our axis + ggplot2::xlab("Soft Threshold (power)") + + ggplot2::ylab("Scale Free Topology Model Fit, signed R^2") + + ggplot2::ggtitle("Scale independence") + + # This adds some nicer aesthetics to our plot + ggplot2::theme_classic() +``` + +Using this plot we can decide on a power parameter. +WGCNA's authors recommend using a `power` that has an signed R^2 above `0.80`, otherwise they warn your results may be too noisy to be meaningful. + +If you have multiple power values with signed R^2 above `0.80`, then picking the one at an inflection point, in other words where the R^2 values seem to have reached their saturation [{{SOURCE}}](https://dibernardo.tigem.it/files/papers/2008/zhangbin-statappsgeneticsmolbio.pdf). +You want to a `power` that gives you a big enough R^2 but is not excessively large. + +So using the plot above, going with a power soft-threshold of `16`! + +If you find you have all very low R^2 values this may be because there are too many genes with low values that are cluttering up the calculations. +You can try returning to [gene filtering step](#define-a-minimum-counts-cutoff) and choosing a more stringent cutoff (you'll then need to re-run the transformation and subsequent steps to remake this plot to see if that helped). + +```{r} +sft_df %>% + ggplot2::ggplot() + + ggplot2::geom_label(ggplot2::aes(x = Power, y = max.k.), label = sft_df$Power) + + # We can add more sensible labels for our axis + ggplot2::xlab("Soft Threshold (power)") + + ggplot2::ylab("Scale Free Topology Model Fit, signed R^2") + + ggplot2::ggtitle("Scale independence") + + # This adds some nicer aesthetics to our plot + ggplot2::theme_classic() +``` + +## Run WGCNA! + +**Review** + +We will use the `blockwiseModules()` function to find gene co-expression modules. +We will use `16` for the `power` argument like we determined above. + +This next step take some time to run. +The `blockwise` part of the `blockwiseModules()` function name refers to that these calculations will be done on chunks of your data at a time to help with computing resources. + +Here we are using the default `maxBlockSize`, 5000 but, you may want to adjust the `maxBlockSize` argument depending on your computer's memory. +The authors of WGCNA recommend running [the largest block your computer can handle](https://peterlangfelder.com/2018/11/25/blockwise-network-analysis-of-large-data/) and provide some approximations as to GB of memory of a laptop and what `maxBlockSize` it should be able to handle. + +> • If the reader has access to a large workstation with more than 4 GB of memory, the parameter maxBlockSize +can be increased. A 16GB workstation should handle up to 20000 probes; a 32GB workstation should handle +perhaps 30000. A 4GB standard desktop or a laptop may handle up to 8000-10000 probes, depending on +operating system and other running programs. + + +```{r} +bwnet <- blockwiseModules(normalized_counts, + maxBlockSize = 5000, # What size chunks (how many genes) the calculations should be run in. + TOMType = "signed", # topological overlap matrix + power = 16, # soft threshold for network construction + numericLabels = TRUE, # Let's use numbers instead of colors for module labels + randomSeed = 1234, # there's some randomness associated with this calculation + # so we should set a seed +) +``` + +There are a lot of other settings you can tweak -- look at `?blockwiseModules` help page to see more. + +The `TOMtype` argument specifies what kind of topological overlap matrix (TOM) should be used to make gene modules. +You can safely assume for most situations a `signed` network represents what you want -- we want WGCNA to pay attention to directionality. +However if you suspect you may benefit from an `unsigned` network, where positive/negative is ignored see [this article](https://peterlangfelder.com/2018/11/25/signed-or-unsigned-which-network-type-is-preferable/) to help you figure that out. + +## Write main WGCNA results object to file + +```{r} +readr::write_rds(bwnet, + file = file.path("results", "SRP133573_wgcna_results.RDS") +) +``` + +## Explore our WGCNA results + +This object, `bwnet` has a lot of information. +We can pull out the parts we are most interested in and may want to use use for plotting. + +In `bwnet` we have a data frame of eigengene module data for each sample in the `MEs` slot. +These represent the collapsed and combined expression of the genes a part of each module. + +```{r} +module_eigengenes <- bwnet$MEs + +# Print out a preview +head(module_eigengenes) +``` + +## Which modules have biggest differences across treatment groups? + +We can also see if our eigengenes relate to our metadata labels. +First we double check that our samples are still in order. + +```{r} +all.equal(metadata$refinebio_accession_code, rownames(module_eigengenes)) +``` + +```{r} +# Create the design matrix from the refinebio_treatment variable +des_mat <- model.matrix(~ metadata$refinebio_treatment) +``` + +Limma wants our tests to be per row, so we need to transpose so the eigengenes are rows + +```{r} +# Transpose so the eigengenes are rows and samples are columns +module_eigengenes_t <- t(module_eigengenes) +``` + +Run linear model on each module. + +```{r} +# Apply linear model to data +fit <- limma::lmFit(module_eigengenes_t, design = des_mat) + +# Apply empirical Bayes to smooth standard errors +fit <- limma::eBayes(fit) +``` + +Apply multiple testing correction and obtain stats in a data frame. + +```{r} +# Apply multiple testing correction and obtain stats +stats_df <- limma::topTable(fit, number = nrow(module_eigengenes_t)) %>% + tibble::rownames_to_column("module") +``` + +Let's arrange the results by the smallest adjusted p values. + +```{r} +stats_df %>% + dplyr::arrange(adj.P.Val) +``` + +Module 52 seems to be the most differentially expressed across `refinebio_treatment` groups. +Now we can do some investigation into this module. + +## Let's make plot of module 52? + +As a sanity check, let's use `ggplot` to see what module 52's eigengene looks like between treatment groups. + +```{r} +module_eigengenes %>% + tibble::rownames_to_column("accession_code") %>% + dplyr::inner_join(metadata %>% + dplyr::select(refinebio_accession_code, refinebio_treatment), + by = c("accession_code" = "refinebio_accession_code") + ) %>% + ggplot2::ggplot(ggplot2::aes( + x = refinebio_treatment, + y = ME52, + color = refinebio_treatment + )) + + ggforce::geom_sina() + + ggplot2::theme_classic() +``` + +This makes sense! + +## What genes are a part of module 52? + +If you want to know which of your genes make up a modules, you can look at the `$colors` slot. +This is a named list which associates the genes with the module they are a part of. +We can turn this into a data frame for handy use. + +```{r} +gene_modules_df <- tibble::enframe(bwnet$colors, name = "gene", value = "module") %>% + # Let's add the `ME` part so its more clear what these numbers are and it matches elsewhere + dplyr::mutate(module = paste0("ME", module)) +``` + +Now we can find what genes are a part of module 52. + +```{r} +module_52_genes <- gene_modules_df %>% + dplyr::filter(module == "ME52") +``` + +Let's save this gene to module key to a TSV file for future use. + +```{r} +readr::write_tsv(gene_modules_df, + file = file.path("results", "SRP133573_wgcna_gene_to_module.tsv") +) +``` + +**Draft** + +# Resources for further learning + +- [WGCNA FAQ page](https://horvath.genetics.ucla.edu/html/CoexpressionNetwork/Rpackages/WGCNA/faq.html) +- [WGCNA tutorial](https://horvath.genetics.ucla.edu/html/CoexpressionNetwork/Rpackages/WGCNA/Tutorials/) +- [WGCNA paper](https://bmcbioinformatics.biomedcentral.com/articles/10.1186/1471-2105-9-559) + +# Session info + +At the end of every analysis, before saving your notebook, we recommend printing out your session info. +This helps make your code more reproducible by recording what versions of software and packages you used to run this. + +```{r} +# Print session info +sessionInfo() +``` + +# References diff --git a/04-advanced-topics/network-analysis_rnaseq_01_wgcna.html b/04-advanced-topics/network-analysis_rnaseq_01_wgcna.html new file mode 100644 index 00000000..97c3fd82 --- /dev/null +++ b/04-advanced-topics/network-analysis_rnaseq_01_wgcna.html @@ -0,0 +1,4429 @@ + + + + + + + + + + + + + + +WGCNA - RNA-seq + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+
+
+
+
+ +
+ + + + + + + + + +

Draft

+
+

1 Purpose of this analysis

+

In this example, we use weighted gene co-expression network analysis (WGCNA) to identify co-expressed gene modules. WGCNA using a series of correlations to determine what genes are expressed together in your data set.

+

⬇️ Jump to the analysis code ⬇️

+
+
+

2 How to run this example

+

For general information about our tutorials and the basic software packages you will need, please see our ‘Getting Started’ section. We recommend taking a look at our Resources for Learning R if you have not written code in R before.

+
+

2.1 Obtain the .Rmd file

+

To run this example yourself, download the .Rmd for this analysis by clicking this link.

+

Clicking this link will most likely send this to your downloads folder on your computer. Move this .Rmd file to where you would like this example and its files to be stored.

+

You can open this .Rmd file in RStudio and follow the rest of these steps from there. (See our section about getting started with R notebooks if you are unfamiliar with .Rmd files.)

+
+
+

2.2 Set up your analysis folders

+

Good file organization is helpful for keeping your data analysis project on track! We have set up some code that will automatically set up a folder structure for you. Run this next chunk to set up your folders!

+

If you have trouble running this chunk, see our introduction to using .Rmds for more resources and explanations.

+
# Create the data folder if it doesn't exist
+if (!dir.exists("data")) {
+  dir.create("data")
+}
+
+# Define the file path to the plots directory
+plots_dir <- "plots" # Can replace with path to desired output plots directory
+
+# Create the plots folder if it doesn't exist
+if (!dir.exists(plots_dir)) {
+  dir.create(plots_dir)
+}
+
+# Define the file path to the results directory
+results_dir <- "results" # Can replace with path to desired output results directory
+
+# Create the results folder if it doesn't exist
+if (!dir.exists(results_dir)) {
+  dir.create(results_dir)
+}
+

In the same place you put this .Rmd file, you should now have three new empty folders called data, plots, and results!

+
+
+

2.3 Obtain the dataset from refine.bio

+

For general information about downloading data for these examples, see our ‘Getting Started’ section.

+

Go to this dataset’s page on refine.bio.

+

Click the “Download Now” button on the right side of this screen.

+

+

Fill out the pop up window with your email and our Terms and Conditions:

+

+

We are going to use non-quantile normalized data for this analysis. To get this data, you will need to check the box that says “Skip quantile normalization for RNA-seq samples”. Note that this option will only be available for RNA-seq datasets.

+

+

It may take a few minutes for the dataset to process. You will get an email when it is ready.

+
+
+

2.4 About the dataset we are using for this example

+

For this example analysis, we will use this prostate cancer dataset. The data that we downloaded from refine.bio for this analysis has 175 RNA-seq samples obtained from 20 patients with prostate cancer. Patients underwent androgen deprivation therapy (ADT) and RNA-seq samples include pre-ADT biopsies and post-ADT prostatectomy specimens.

+
+
+

2.5 Place the dataset in your new data/ folder

+

refine.bio will send you a download button in the email when it is ready. Follow the prompt to download a zip file that has a name with a series of letters and numbers and ends in .zip. Double clicking should unzip this for you and create a folder of the same name.

+

+

For more details on the contents of this folder see these docs on refine.bio.

+

The <experiment_accession_id> folder has the data and metadata TSV files you will need for this example analysis. Experiment accession ids usually look something like GSE1235 or SRP12345.

+

Copy and paste the SRP133573 folder into your newly created data/ folder.

+
+
+

2.6 Check out our file structure!

+

Your new analysis folder should contain:

+
    +
  • The example analysis .Rmd you downloaded
    +
  • +
  • A folder called “data” which contains: +
      +
    • The SRP133573 folder which contains: +
        +
      • The gene expression
        +
      • +
      • The metadata TSV
        +
      • +
    • +
  • +
  • A folder for plots (currently empty)
  • +
  • A folder for results (currently empty)
  • +
+

Your example analysis folder should now look something like this (except with respective experiment accession ID and analysis notebook name you are using):

+

+

In order for our example here to run without a hitch, we need these files to be in these locations so we’ve constructed a test to check before we get started with the analysis. These chunks will declare your file paths and double check that your files are in the right place.

+

First we will declare our file paths to our data and metadata files, which should be in our data directory. This is handy to do because if we want to switch the dataset (see next section for more on this) we are using for this analysis, we will only have to change the file path here to get started.

+
# Define the file path to the data directory
+data_dir <- file.path("data", "SRP133573") # Replace with accession number which will be the name of the folder the files will be in
+
+# Declare the file path to the gene expression matrix file using the data directory saved as `data_dir`
+data_file <- file.path(data_dir, "SRP133573.tsv") # Replace with file path to your dataset
+
+# Declare the file path to the metadata file using the data directory saved as `data_dir`
+metadata_file <- file.path(data_dir, "metadata_SRP133573.tsv") # Replace with file path to your metadata
+

Now that our file paths are declared, we can use the file.exists() function to check that the files are where we specified above.

+
# Check if the gene expression matrix file is at the file path stored in `data_file`
+file.exists(data_file)
+
## [1] TRUE
+
# Check if the metadata file is at the file path stored in `metadata_file`
+file.exists(metadata_file)
+
## [1] TRUE
+

If the chunk above printed out FALSE to either of those tests, you won’t be able to run this analysis as is until those files are in the appropriate place.

+

If the concept of a “file path” is unfamiliar to you; we recommend taking a look at our section about file paths.

+
+
+
+

3 Using a different refine.bio dataset with this analysis?

+

If you’d like to adapt an example analysis to use a different dataset from refine.bio, we recommend placing the files in the data/ directory you created and changing the filenames and paths in the notebook to match these files (we’ve put comments to signify where you would need to change the code). We suggest saving plots and results to plots/ and results/ directories, respectively, as these are automatically created by the notebook. From here you can customize this analysis example to fit your own scientific questions and preferences.

+

Keep in mind that WGCNA requires at least 15 samples to produce a meaningful result according to its authors. So you will need to make sure the dataset you switch this to is sufficiently large.

+
+ +

 

+
+
+

4 Identifying co-expression gene modules with WGCNA - RNA-seq

+
+

4.1 Install libraries

+

See our Getting Started page with instructions for package installation for a list of the other software you will need, as well as more tips and resources.

+

In this analysis, we will be using WGCNA package. WGCNA also requires a package called impute that is sometimes has trouble installing so we recommend installing that first.

+
if (!("DESeq2" %in% installed.packages())) {
+  # Install this package if it isn't installed yet
+  BiocManager::install("DESeq2", update = FALSE)
+}
+
+if (!("impute" %in% installed.packages())) {
+  # Install this package if it isn't installed yet
+  BiocManager::install("impute")
+}
+
+if (!("WGCNA" %in% installed.packages())) {
+  # Install this package if it isn't installed yet
+  install.packages("WGCNA")
+}
+
+if (!("ggforce" %in% installed.packages())) {
+  # Install this package if it isn't installed yet
+  install.packages("ggforce")
+}
+

Attach the packages we need for this analysis.

+
# Attach the DESeq2 library
+library(DESeq2)
+
## Loading required package: S4Vectors
+
## Loading required package: stats4
+
## Loading required package: BiocGenerics
+
## Loading required package: parallel
+
## 
+## Attaching package: 'BiocGenerics'
+
## The following objects are masked from 'package:parallel':
+## 
+##     clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
+##     clusterExport, clusterMap, parApply, parCapply, parLapply,
+##     parLapplyLB, parRapply, parSapply, parSapplyLB
+
## The following objects are masked from 'package:stats':
+## 
+##     IQR, mad, sd, var, xtabs
+
## The following objects are masked from 'package:base':
+## 
+##     anyDuplicated, append, as.data.frame, basename, cbind, colnames,
+##     dirname, do.call, duplicated, eval, evalq, Filter, Find, get, grep,
+##     grepl, intersect, is.unsorted, lapply, Map, mapply, match, mget,
+##     order, paste, pmax, pmax.int, pmin, pmin.int, Position, rank,
+##     rbind, Reduce, rownames, sapply, setdiff, sort, table, tapply,
+##     union, unique, unsplit, which.max, which.min
+
## 
+## Attaching package: 'S4Vectors'
+
## The following object is masked from 'package:base':
+## 
+##     expand.grid
+
## Loading required package: IRanges
+
## Loading required package: GenomicRanges
+
## Loading required package: GenomeInfoDb
+
## Loading required package: SummarizedExperiment
+
## Loading required package: MatrixGenerics
+
## Loading required package: matrixStats
+
## 
+## Attaching package: 'MatrixGenerics'
+
## The following objects are masked from 'package:matrixStats':
+## 
+##     colAlls, colAnyNAs, colAnys, colAvgsPerRowSet, colCollapse,
+##     colCounts, colCummaxs, colCummins, colCumprods, colCumsums,
+##     colDiffs, colIQRDiffs, colIQRs, colLogSumExps, colMadDiffs,
+##     colMads, colMaxs, colMeans2, colMedians, colMins, colOrderStats,
+##     colProds, colQuantiles, colRanges, colRanks, colSdDiffs, colSds,
+##     colSums2, colTabulates, colVarDiffs, colVars, colWeightedMads,
+##     colWeightedMeans, colWeightedMedians, colWeightedSds,
+##     colWeightedVars, rowAlls, rowAnyNAs, rowAnys, rowAvgsPerColSet,
+##     rowCollapse, rowCounts, rowCummaxs, rowCummins, rowCumprods,
+##     rowCumsums, rowDiffs, rowIQRDiffs, rowIQRs, rowLogSumExps,
+##     rowMadDiffs, rowMads, rowMaxs, rowMeans2, rowMedians, rowMins,
+##     rowOrderStats, rowProds, rowQuantiles, rowRanges, rowRanks,
+##     rowSdDiffs, rowSds, rowSums2, rowTabulates, rowVarDiffs, rowVars,
+##     rowWeightedMads, rowWeightedMeans, rowWeightedMedians,
+##     rowWeightedSds, rowWeightedVars
+
## Loading required package: Biobase
+
## Welcome to Bioconductor
+## 
+##     Vignettes contain introductory material; view with
+##     'browseVignettes()'. To cite Bioconductor, see
+##     'citation("Biobase")', and for packages 'citation("pkgname")'.
+
## 
+## Attaching package: 'Biobase'
+
## The following object is masked from 'package:MatrixGenerics':
+## 
+##     rowMedians
+
## The following objects are masked from 'package:matrixStats':
+## 
+##     anyMissing, rowMedians
+
# Homo sapiens annotation package we'll use for gene identifier conversion
+library(org.Hs.eg.db)
+
## Loading required package: AnnotationDbi
+
## 
+
# We will need this so we can use the pipe: %>%
+library(magrittr)
+
+# We'll need this for finding gene modules
+library(WGCNA)
+
## Loading required package: dynamicTreeCut
+
## Loading required package: fastcluster
+
## 
+## Attaching package: 'fastcluster'
+
## The following object is masked from 'package:stats':
+## 
+##     hclust
+
## 
+
## 
+## Attaching package: 'WGCNA'
+
## The following object is masked from 'package:IRanges':
+## 
+##     cor
+
## The following object is masked from 'package:S4Vectors':
+## 
+##     cor
+
## The following object is masked from 'package:stats':
+## 
+##     cor
+
+
+

4.2 Import and set up data

+

Data downloaded from refine.bio include a metadata tab separated values (TSV) file and a data TSV file. This chunk of code will read the both TSV files and add them as data frames to your environment.

+

We stored our file paths as objects named metadata_file and data_file in this previous step.

+
# Read in metadata TSV file
+metadata <- readr::read_tsv(metadata_file)
+
## 
+## ── Column specification ───────────────────────────────────────────────────────────────────────────────────────────────────
+## cols(
+##   .default = col_character(),
+##   refinebio_age = col_logical(),
+##   refinebio_cell_line = col_logical(),
+##   refinebio_compound = col_logical(),
+##   refinebio_disease_stage = col_logical(),
+##   refinebio_genetic_information = col_logical(),
+##   refinebio_processed = col_logical(),
+##   refinebio_sex = col_logical(),
+##   refinebio_source_archive_url = col_logical(),
+##   refinebio_specimen_part = col_logical(),
+##   refinebio_time = col_logical()
+## )
+## ℹ Use `spec()` for the full column specifications.
+
# Read in data TSV file
+df <- readr::read_tsv(data_file) %>%
+  # Here we are going to store the gene IDs as rownames so that we can have a numeric matrix to perform calculations on later
+  tibble::column_to_rownames("Gene")
+
## 
+## ── Column specification ───────────────────────────────────────────────────────────────────────────────────────────────────
+## cols(
+##   .default = col_double(),
+##   Gene = col_character()
+## )
+## ℹ Use `spec()` for the full column specifications.
+

Let’s ensure that the metadata and data are in the same sample order.

+
# Make the data in the order of the metadata
+df <- df %>%
+  dplyr::select(metadata$refinebio_accession_code)
+
+# Check if this is in the same order
+all.equal(colnames(df), metadata$refinebio_accession_code)
+
## [1] TRUE
+
+

4.2.1 Prepare data for DESeq2

+

We need to make sure all of the values in our data are converted to integers as required by a DESeq2 function we will use later.

+
# The `DESeqDataSetFromMatrix()` function needs the values to be converted to integers
+df <- df %>%
+  # Bring out rownames and store as their own column
+  tibble::rownames_to_column("Gene") %>%
+  # Mutate numeric variables to be integers
+  dplyr::mutate_if(is.numeric, round)
+
+
+
+

4.3 Create a DESeqDataset

+

We will be using the DESeq2 package for normalizing and transforming our data, which requires us to format our data into a DESeqDataSet object. We turn the data frame (or matrix) into a DESeqDataSet object. ) and specify which variable labels our experimental groups using the design argument (Love et al. 2014). In this chunk of code, we will not provide a specific model to the design argument because we are not performing a differential expression analysis.

+
# Create a `DESeqDataSet` object
+dds <- DESeqDataSetFromMatrix(
+  countData = df, # This is the data frame with the counts values for all replicates in our dataset
+  colData = metadata, # This is the data frame with the annotation data for the replicates in the counts data frame
+  design = ~1, # Here we are not specifying a model -- Replace with an appropriate design variable for your analysis
+  tidy = TRUE
+)
+
## converting counts to integer mode
+
+
+

4.4 Define a minimum counts cutoff

+

We want to filter out the genes that have not been expressed or that have low expression counts. This is recommended by WGCNA docs for RNA-seq data. Removing low count genes can help improve your WGCNA results. We are going to do some pre-filtering to keep only genes with 10 or more reads in total across the samples.

+
# Define a minimum counts cutoff and filter `DESeqDataSet` object to include
+# only rows that have counts above the cutoff
+genes_to_keep <- rowSums(counts(dds)) >= 50
+dds <- dds[genes_to_keep, ]
+
+
+

4.5 Perform DESeq2 normalization and transformation

+

We often suggest normalizing and transforming your data for various applications and in this instance WGCNA’s authors suggest using variance stabilizing transformation before running WGCNA.
+We are going to use the vst() function from the DESeq2 package to normalize and transform the data. For more information about these transformation methods, see here.

+
# Normalize and transform the data in the `DESeqDataSet` object using the `vst()` function from the `DESEq2` R package
+dds_norm <- vst(dds)
+
+
+

4.6 Format normalized data for WGCNA

+

Here we will use WGCNA to identify co-expressed gene modules.

+

Extract the normalized counts to a matrix and transpose it so we can pass it to WGCNA.

+
# First we are going to retrieve the normalized data from the `DESeqDataSet` object using the `assay()` function
+normalized_counts <- assay(dds_norm) %>%
+  t() # We need to transpose this data in preparation for the `WGCNA::blockwiseModules()` function
+
+
+

4.7 Determine parameters for WGCNA

+

Draft

+

WGCNA has to decide what genes are included in which modules, but it might encounter some genes that are close to call between modules. By providing a soft-threshold power parameter, we can guide it on to what exponent it should compare R values for deciding between modules for a gene. The “soft” part of a soft threshold just means that this will not be an absolute cutoff and WGCNA will use this parameter with some flexibility.

+
sft <- pickSoftThreshold(normalized_counts,
+  dataIsExpr = TRUE,
+  corFnc = cor,
+  networkType = "signed"
+)
+
## Warning: executing %dopar% sequentially: no parallel backend registered
+
##    Power SFT.R.sq  slope truncated.R.sq mean.k. median.k. max.k.
+## 1      1  0.58200 12.200          0.957 13500.0   13600.0  15500
+## 2      2  0.44500  5.130          0.972  7630.0    7650.0   9910
+## 3      3  0.26300  2.570          0.985  4480.0    4450.0   6680
+## 4      4  0.06480  0.914          0.985  2730.0    2680.0   4720
+## 5      5  0.00662 -0.236          0.964  1720.0    1660.0   3450
+## 6      6  0.15900 -1.010          0.965  1120.0    1060.0   2580
+## 7      7  0.36500 -1.470          0.971   746.0     689.0   1980
+## 8      8  0.50000 -1.730          0.972   509.0     459.0   1550
+## 9      9  0.59700 -1.910          0.972   356.0     313.0   1220
+## 10    10  0.67000 -2.060          0.973   253.0     217.0    982
+## 11    12  0.74000 -2.260          0.970   135.0     110.0    651
+## 12    14  0.79400 -2.320          0.978    76.9      58.6    447
+## 13    16  0.82000 -2.350          0.981    45.9      32.7    315
+## 14    18  0.83800 -2.360          0.985    28.6      18.9    227
+## 15    20  0.84500 -2.350          0.987    18.5      11.2    167
+

This sft object has a lot of information, we will want to plot some of it to figure out what our power soft-threshold should be. We have to first calculate the model fit, the R squared and make that a new variable.

+
sft_df <- data.frame(sft$fitIndices) %>%
+  dplyr::mutate(model_fit = -sign(slope) * SFT.R.sq)
+

Now, let’s plot the model fitting by the power soft threshold so we can decide on a soft-threshold for power.

+
sft_df %>%
+  ggplot2::ggplot() +
+  ggplot2::geom_label(ggplot2::aes(x = Power, y = model_fit), label = sft_df$Power) +
+  # We will plot what WGCNA recommends as an R^2 cutoff
+  ggplot2::geom_hline(yintercept = 0.80, col = "red") +
+  # Just in case our values are low, we want to make sure we can still see the 0.80 level
+  ggplot2::ylim(c(min(sft_df$model_fit), 1)) +
+  # We can add more sensible labels for our axis
+  ggplot2::xlab("Soft Threshold (power)") +
+  ggplot2::ylab("Scale Free Topology Model Fit, signed R^2") +
+  ggplot2::ggtitle("Scale independence") +
+  # This adds some nicer aesthetics to our plot
+  ggplot2::theme_classic()
+

+

Using this plot we can decide on a power parameter. WGCNA’s authors recommend using a power that has an signed R^2 above 0.80, otherwise they warn your results may be too noisy to be meaningful.

+

If you have multiple power values with signed R^2 above 0.80, then picking the one at an inflection point, in other words where the R^2 values seem to have reached their saturation {{SOURCE}}. You want to a power that gives you a big enough R^2 but is not excessively large.

+

So using the plot above, going with a power soft-threshold of 16!

+

If you find you have all very low R^2 values this may be because there are too many genes with low values that are cluttering up the calculations. You can try returning to gene filtering step and choosing a more stringent cutoff (you’ll then need to re-run the transformation and subsequent steps to remake this plot to see if that helped).

+
sft_df %>%
+  ggplot2::ggplot() +
+  ggplot2::geom_label(ggplot2::aes(x = Power, y = max.k.), label = sft_df$Power) +
+  # We can add more sensible labels for our axis
+  ggplot2::xlab("Soft Threshold (power)") +
+  ggplot2::ylab("Scale Free Topology Model Fit, signed R^2") +
+  ggplot2::ggtitle("Scale independence") +
+  # This adds some nicer aesthetics to our plot
+  ggplot2::theme_classic()
+

+
+
+

4.8 Run WGCNA!

+

Review

+

We will use the blockwiseModules() function to find gene co-expression modules. We will use 16 for the power argument like we determined above.

+

This next step take some time to run. The blockwise part of the blockwiseModules() function name refers to that these calculations will be done on chunks of your data at a time to help with computing resources.

+

Here we are using the default maxBlockSize, 5000 but, you may want to adjust the maxBlockSize argument depending on your computer’s memory. The authors of WGCNA recommend running the largest block your computer can handle and provide some approximations as to GB of memory of a laptop and what maxBlockSize it should be able to handle.

+
+

• If the reader has access to a large workstation with more than 4 GB of memory, the parameter maxBlockSize can be increased. A 16GB workstation should handle up to 20000 probes; a 32GB workstation should handle perhaps 30000. A 4GB standard desktop or a laptop may handle up to 8000-10000 probes, depending on operating system and other running programs.

+
+
bwnet <- blockwiseModules(normalized_counts,
+  maxBlockSize = 5000, # What size chunks (how many genes) the calculations should be run in.
+  TOMType = "signed", # topological overlap matrix
+  power = 16, # soft threshold for network construction
+  numericLabels = TRUE, # Let's use numbers instead of colors for module labels
+  randomSeed = 1234, # there's some randomness associated with this calculation
+  # so we should set a seed
+)
+

There are a lot of other settings you can tweak – look at ?blockwiseModules help page to see more.

+

The TOMtype argument specifies what kind of topological overlap matrix (TOM) should be used to make gene modules. You can safely assume for most situations a signed network represents what you want – we want WGCNA to pay attention to directionality. However if you suspect you may benefit from an unsigned network, where positive/negative is ignored see this article to help you figure that out.

+
+
+

4.9 Write main WGCNA results object to file

+
readr::write_rds(bwnet,
+  file = file.path("results", "SRP133573_wgcna_results.RDS")
+)
+
+
+

4.10 Explore our WGCNA results

+

This object, bwnet has a lot of information. We can pull out the parts we are most interested in and may want to use use for plotting.

+

In bwnet we have a data frame of eigengene module data for each sample in the MEs slot. These represent the collapsed and combined expression of the genes a part of each module.

+
module_eigengenes <- bwnet$MEs
+
+# Print out a preview
+head(module_eigengenes)
+
+ +
+
+
+

4.11 Which modules have biggest differences across treatment groups?

+

We can also see if our eigengenes relate to our metadata labels. First we double check that our samples are still in order.

+
all.equal(metadata$refinebio_accession_code, rownames(module_eigengenes))
+
## [1] TRUE
+
# Create the design matrix from the refinebio_treatment variable
+des_mat <- model.matrix(~ metadata$refinebio_treatment)
+

Limma wants our tests to be per row, so we need to transpose so the eigengenes are rows

+
# Transpose so the eigengenes are rows and samples are columns
+module_eigengenes_t <- t(module_eigengenes)
+

Run linear model on each module.

+
# Apply linear model to data
+fit <- limma::lmFit(module_eigengenes_t, design = des_mat)
+
+# Apply empirical Bayes to smooth standard errors
+fit <- limma::eBayes(fit)
+

Apply multiple testing correction and obtain stats in a data frame.

+
# Apply multiple testing correction and obtain stats
+stats_df <- limma::topTable(fit, number = nrow(module_eigengenes_t)) %>%
+  tibble::rownames_to_column("module")
+
## Removing intercept from test coefficients
+

Let’s arrange the results by the smallest adjusted p values.

+
stats_df %>%
+  dplyr::arrange(adj.P.Val)
+
+ +
+

Module 52 seems to be the most differentially expressed across refinebio_treatment groups. Now we can do some investigation into this module.

+
+
+

4.12 Let’s make plot of module 52?

+

As a sanity check, let’s use ggplot to see what module 52’s eigengene looks like between treatment groups.

+
module_eigengenes %>%
+  tibble::rownames_to_column("accession_code") %>%
+  dplyr::inner_join(metadata %>%
+    dplyr::select(refinebio_accession_code, refinebio_treatment),
+  by = c("accession_code" = "refinebio_accession_code")
+  ) %>%
+  ggplot2::ggplot(ggplot2::aes(
+    x = refinebio_treatment,
+    y = ME52,
+    color = refinebio_treatment
+  )) +
+  ggforce::geom_sina() +
+  ggplot2::theme_classic()
+

+

This makes sense!

+
+
+

4.13 What genes are a part of module 52?

+

If you want to know which of your genes make up a modules, you can look at the $colors slot. This is a named list which associates the genes with the module they are a part of. We can turn this into a data frame for handy use.

+
gene_modules_df <- tibble::enframe(bwnet$colors, name = "gene", value = "module") %>%
+  # Let's add the `ME` part so its more clear what these numbers are and it matches elsewhere
+  dplyr::mutate(module = paste0("ME", module))
+

Now we can find what genes are a part of module 52.

+
module_52_genes <- gene_modules_df %>%
+  dplyr::filter(module == "ME52")
+

Let’s save this gene to module key to a TSV file for future use.

+
readr::write_tsv(gene_modules_df,
+  file = file.path("results", "SRP133573_wgcna_gene_to_module.tsv")
+)
+

Draft

+
+
+
+

5 Resources for further learning

+ +
+
+

6 Session info

+

At the end of every analysis, before saving your notebook, we recommend printing out your session info. This helps make your code more reproducible by recording what versions of software and packages you used to run this.

+
# Print session info
+sessionInfo()
+
## R version 4.0.2 (2020-06-22)
+## Platform: x86_64-pc-linux-gnu (64-bit)
+## Running under: Ubuntu 20.04 LTS
+## 
+## Matrix products: default
+## BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so
+## 
+## locale:
+##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
+##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
+##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
+##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
+##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
+## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
+## 
+## attached base packages:
+## [1] parallel  stats4    stats     graphics  grDevices utils     datasets 
+## [8] methods   base     
+## 
+## other attached packages:
+##  [1] WGCNA_1.69                  fastcluster_1.1.25         
+##  [3] dynamicTreeCut_1.63-1       magrittr_1.5               
+##  [5] org.Hs.eg.db_3.12.0         AnnotationDbi_1.52.0       
+##  [7] DESeq2_1.30.0               SummarizedExperiment_1.20.0
+##  [9] Biobase_2.50.0              MatrixGenerics_1.2.0       
+## [11] matrixStats_0.57.0          GenomicRanges_1.42.0       
+## [13] GenomeInfoDb_1.26.0         IRanges_2.24.0             
+## [15] S4Vectors_0.28.0            BiocGenerics_0.36.0        
+## [17] optparse_1.6.6             
+## 
+## loaded via a namespace (and not attached):
+##   [1] colorspace_1.4-1       ellipsis_0.3.1         htmlTable_2.1.0       
+##   [4] XVector_0.30.0         base64enc_0.1-3        rstudioapi_0.11       
+##   [7] farver_2.0.3           getopt_1.20.3          bit64_4.0.5           
+##  [10] fansi_0.4.1            codetools_0.2-16       splines_4.0.2         
+##  [13] R.methodsS3_1.8.1      doParallel_1.0.15      impute_1.64.0         
+##  [16] geneplotter_1.68.0     knitr_1.30             polyclip_1.10-0       
+##  [19] jsonlite_1.7.1         Formula_1.2-3          annotate_1.68.0       
+##  [22] cluster_2.1.0          GO.db_3.12.1           png_0.1-7             
+##  [25] R.oo_1.24.0            ggforce_0.3.2          readr_1.4.0           
+##  [28] compiler_4.0.2         httr_1.4.2             backports_1.1.10      
+##  [31] assertthat_0.2.1       Matrix_1.2-18          limma_3.46.0          
+##  [34] cli_2.1.0              tweenr_1.0.1           htmltools_0.5.0       
+##  [37] tools_4.0.2            gtable_0.3.0           glue_1.4.2            
+##  [40] GenomeInfoDbData_1.2.4 dplyr_1.0.2            Rcpp_1.0.5            
+##  [43] styler_1.3.2           vctrs_0.3.4            preprocessCore_1.52.0 
+##  [46] iterators_1.0.12       xfun_0.18              stringr_1.4.0         
+##  [49] ps_1.4.0               lifecycle_0.2.0        XML_3.99-0.5          
+##  [52] MASS_7.3-51.6          zlibbioc_1.36.0        scales_1.1.1          
+##  [55] hms_0.5.3              rematch2_2.1.2         RColorBrewer_1.1-2    
+##  [58] yaml_2.2.1             memoise_1.1.0          gridExtra_2.3         
+##  [61] ggplot2_3.3.2          rpart_4.1-15           latticeExtra_0.6-29   
+##  [64] stringi_1.5.3          RSQLite_2.2.1          genefilter_1.72.0     
+##  [67] foreach_1.5.0          checkmate_2.0.0        BiocParallel_1.24.1   
+##  [70] rlang_0.4.8            pkgconfig_2.0.3        bitops_1.0-6          
+##  [73] evaluate_0.14          lattice_0.20-41        purrr_0.3.4           
+##  [76] htmlwidgets_1.5.2      labeling_0.3           bit_4.0.4             
+##  [79] tidyselect_1.1.0       R6_2.4.1               generics_0.0.2        
+##  [82] Hmisc_4.4-1            DelayedArray_0.16.0    DBI_1.1.0             
+##  [85] pillar_1.4.6           foreign_0.8-80         survival_3.1-12       
+##  [88] RCurl_1.98-1.2         nnet_7.3-14            tibble_3.0.4          
+##  [91] crayon_1.3.4           rmarkdown_2.4          jpeg_0.1-8.1          
+##  [94] locfit_1.5-9.4         grid_4.0.2             data.table_1.13.0     
+##  [97] blob_1.2.1             digest_0.6.25          xtable_1.8-4          
+## [100] R.cache_0.14.0         R.utils_2.10.1         munsell_0.5.0
+
+
+

References

+
+
+

Love M. I., W. Huber, and S. Anders, 2014 Moderated estimation of fold change and dispersion for RNA-Seq data with DESeq2. Genome Biology 15. https://doi.org/10.1186/s13059-014-0550-8

+
+
+
+ + + + +
+
+ +
+ + + + + + + + + + + + + + + + diff --git a/Snakefile b/Snakefile index 0e38d621..803918b5 100644 --- a/Snakefile +++ b/Snakefile @@ -8,8 +8,8 @@ rule target: "02-microarray/dimension-reduction_microarray_01_pca.html", "02-microarray/dimension-reduction_microarray_02_umap.html", "02-microarray/gene-id-annotation_microarray_01_ensembl.html", - "02-microarray/pathway-analysis_microarray_01_ora.html", - "02-microarray/pathway-analysis_microarray_02_gsea.html", + "02-microarray/pathway-analysis_microarray_02_ora.html", + "02-microarray/pathway-analysis_microarray_03_gsea.html", "02-microarray/ortholog-mapping_microarray_01_ensembl.html", "03-rnaseq/00-intro-to-rnaseq.html", "03-rnaseq/clustering_rnaseq_01_heatmap.html", @@ -18,7 +18,9 @@ rule target: "03-rnaseq/dimension-reduction_rnaseq_02_umap.html", "03-rnaseq/gene-id-annotation_rnaseq_01_ensembl.html", "03-rnaseq/ortholog-mapping_rnaseq_01_ensembl.html", - "04-advanced-topics/00-intro-to-advanced-topics.html" + "04-advanced-topics/00-intro-to-advanced-topics.html", + "04-advanced-topics/network-analysis_rnaseq_01_wgcna.html" + rule render_citations: input: diff --git a/components/_navbar.html b/components/_navbar.html index 3201a3f2..811467dd 100644 --- a/components/_navbar.html +++ b/components/_navbar.html @@ -7,7 +7,7 @@ - + -
-

4.4 Define a minimum counts cutoff

-

We want to filter out the genes that have not been expressed or that have low expression counts. This is recommended by WGCNA docs for RNA-seq data. Removing low count genes can help improve your WGCNA results. We are going to do some pre-filtering to keep only genes with 50 or more reads in total across the samples.

-
# Define a minimum counts cutoff and filter `DESeqDataSet` object to include
-# only rows that have counts above the cutoff
-genes_to_keep <- rowSums(counts(dds)) >= 50
-dds <- dds[genes_to_keep, ]
-
-

4.5 Perform DESeq2 normalization and transformation

+

4.4 Perform DESeq2 normalization and transformation

We often suggest normalizing and transforming your data for various applications and in this instance WGCNA’s authors suggest using variance stabilizing transformation before running WGCNA.
We are going to use the vst() function from the DESeq2 package to normalize and transform the data. For more information about these transformation methods, see here.

-
# Normalize and transform the data in the `DESeqDataSet` object using the `vst()`
-# function from the `DESEq2` R package
-dds_norm <- vst(dds)
+
# Normalize and transform the data in the `DESeqDataSet` object using the `vst()`
+# function from the `DESEq2` R package
+dds_norm <- vst(dds)
-

4.6 Format normalized data for WGCNA

+

4.5 Format normalized data for WGCNA

Extract the normalized counts to a matrix and transpose it so we can pass it to WGCNA.

-
# First we are going to retrieve the normalized data from the `DESeqDataSet` object using the `assay()` function
-normalized_counts <- assay(dds_norm) %>%
-  t() # We need to transpose this data in preparation for the `WGCNA::blockwiseModules()` function
-
-
-

4.7 Determine parameters for WGCNA

-

WGCNA has to decide what genes are included in which modules, but it might encounter some genes that are close to call between modules. By providing a soft-threshold power parameter, we can guide it on to what exponent it should compare R values for deciding between modules for a gene. The “soft” part of a soft threshold just means that this will not be an absolute cutoff and WGCNA will use this parameter with some flexibility.

-
sft <- pickSoftThreshold(normalized_counts,
-  dataIsExpr = TRUE,
-  corFnc = cor,
-  networkType = "signed"
-)
-
## Warning: executing %dopar% sequentially: no parallel backend registered
-
##    Power SFT.R.sq  slope truncated.R.sq mean.k. median.k. max.k.
-## 1      1  0.58200 12.200          0.957 13500.0   13600.0  15500
-## 2      2  0.44500  5.130          0.972  7630.0    7650.0   9910
-## 3      3  0.26300  2.570          0.985  4480.0    4450.0   6680
-## 4      4  0.06480  0.914          0.985  2730.0    2680.0   4720
-## 5      5  0.00662 -0.236          0.964  1720.0    1660.0   3450
-## 6      6  0.15900 -1.010          0.965  1120.0    1060.0   2580
-## 7      7  0.36500 -1.470          0.971   746.0     689.0   1980
-## 8      8  0.50000 -1.730          0.972   509.0     459.0   1550
-## 9      9  0.59700 -1.910          0.972   356.0     313.0   1220
-## 10    10  0.67000 -2.060          0.973   253.0     217.0    982
-## 11    12  0.74000 -2.260          0.970   135.0     110.0    651
-## 12    14  0.79400 -2.320          0.978    76.9      58.6    447
-## 13    16  0.82000 -2.350          0.981    45.9      32.7    315
-## 14    18  0.83800 -2.360          0.985    28.6      18.9    227
-## 15    20  0.84500 -2.350          0.987    18.5      11.2    167
-

This sft object has a lot of information, we will want to plot some of it to figure out what our power soft-threshold should be. We have to first calculate the model fit, the R squared and make that a new variable.

-
sft_df <- data.frame(sft$fitIndices) %>%
-  dplyr::mutate(model_fit = -sign(slope) * SFT.R.sq)
-

Now, let’s plot the model fitting by the power soft threshold so we can decide on a soft-threshold for power.

-
sft_df %>%
-  ggplot2::ggplot() +
-  ggplot2::geom_label(ggplot2::aes(x = Power, y = model_fit), label = sft_df$Power) +
-  # We will plot what WGCNA recommends as an R^2 cutoff
-  ggplot2::geom_hline(yintercept = 0.80, col = "red") +
-  # Just in case our values are low, we want to make sure we can still see the 0.80 level
-  ggplot2::ylim(c(min(sft_df$model_fit), 1)) +
-  # We can add more sensible labels for our axis
-  ggplot2::xlab("Soft Threshold (power)") +
-  ggplot2::ylab("Scale Free Topology Model Fit, signed R^2") +
-  ggplot2::ggtitle("Scale independence") +
-  # This adds some nicer aesthetics to our plot
-  ggplot2::theme_classic()
-

-

Using this plot we can decide on a power parameter. WGCNA’s authors recommend using a power that has an signed R^2 above 0.80, otherwise they warn your results may be too noisy to be meaningful.

-

If you have multiple power values with signed R^2 above 0.80, then picking the one at an inflection point, in other words where the R^2 values seem to have reached their saturation (Zhang and Horvath 2005). You want to a power that gives you a big enough R^2 but is not excessively large.

-

So using the plot above, going with a power soft-threshold of 16!

-

If you find you have all very low R^2 values this may be because there are too many genes with low values that are cluttering up the calculations. You can try returning to gene filtering step and choosing a more stringent cutoff (you’ll then need to re-run the transformation and subsequent steps to remake this plot to see if that helped).

-
sft_df %>%
-  ggplot2::ggplot() +
-  ggplot2::geom_label(ggplot2::aes(x = Power, y = max.k.), label = sft_df$Power) +
-  # We can add more sensible labels for our axis
-  ggplot2::xlab("Soft Threshold (power)") +
-  ggplot2::ylab("Scale Free Topology Model Fit, signed R^2") +
-  ggplot2::ggtitle("Scale independence") +
-  # This adds some nicer aesthetics to our plot
-  ggplot2::theme_classic()
-

-
-
-

4.8 Run WGCNA!

-

We will use the blockwiseModules() function to find gene co-expression modules in WGCNA, using 16 for the power argument like we determined above.

-

This next step takes some time to run. The blockwise part of the blockwiseModules() function name refers to that these calculations will be done on chunks of your data at a time to help with conserving computing resources.

-

Here we are using the default maxBlockSize, 5000 but, you may want to adjust the maxBlockSize argument depending on your computer’s memory. The authors of WGCNA recommend running the largest block your computer can handle and they provide some approximations as to GB of memory of a laptop and what maxBlockSize it should be able to handle:

-
-

• If the reader has access to a large workstation with more than 4 GB of memory, the parameter maxBlockSize can be increased. A 16GB workstation should handle up to 20000 probes; a 32GB workstation should handle perhaps 30000. A 4GB standard desktop or a laptop may handle up to 8000-10000 probes, depending on operating system and other running programs.

-
-

(Langfelder and Horvath 2016)

-
bwnet <- blockwiseModules(normalized_counts,
-  maxBlockSize = 5000, # What size chunks (how many genes) the calculations should be run in
-  TOMType = "signed", # topological overlap matrix
-  power = 16, # soft threshold for network construction
-  numericLabels = TRUE, # Let's use numbers instead of colors for module labels
-  randomSeed = 1234, # there's some randomness associated with this calculation
-  # so we should set a seed
-)
-

The TOMtype argument specifies what kind of topological overlap matrix (TOM) should be used to make gene modules. You can safely assume for most situations a signed network represents what you want – we want WGCNA to pay attention to directionality. However if you suspect you may benefit from an unsigned network, where positive/negative is ignored see this article to help you figure that out (Langfelder 2018).

-

There are a lot of other settings you can tweak – look at ?blockwiseModules help page as well as the WGCNA tutorial (Langfelder and Horvath 2016).

-
-
-

4.9 Write main WGCNA results object to file

-

We will save our whole results object to an RDS file in case we want to return to our original WGCNA results.

-
readr::write_rds(bwnet,
-  file = file.path("results", "SRP133573_wgcna_results.RDS")
-)
+
# Retrieve the normalized data from the `DESeqDataSet`
+normalized_counts <- assay(dds_norm) %>%
+  t() # Transpose this data

Next sections addressed in upcoming PR

@@ -4199,8 +4107,8 @@

5 Resources for further learning<

6 Session info

At the end of every analysis, before saving your notebook, we recommend printing out your session info. This helps make your code more reproducible by recording what versions of software and packages you used to run this.

-
# Print session info
-sessionInfo()
+
# Print session info
+sessionInfo()
## R version 4.0.2 (2020-06-22)
 ## Platform: x86_64-pc-linux-gnu (64-bit)
 ## Running under: Ubuntu 20.04 LTS
@@ -4238,39 +4146,35 @@ 

6 Session info

## [13] colorspace_1.4-1 nnet_7.3-14 gridExtra_2.3 ## [16] tidyselect_1.1.0 preprocessCore_1.52.0 bit_4.0.4 ## [19] compiler_4.0.2 cli_2.1.0 htmlTable_2.1.0 -## [22] DelayedArray_0.16.0 labeling_0.3 checkmate_2.0.0 -## [25] scales_1.1.1 readr_1.4.0 genefilter_1.72.0 -## [28] stringr_1.4.0 digest_0.6.25 foreign_0.8-80 -## [31] rmarkdown_2.4 R.utils_2.10.1 XVector_0.30.0 -## [34] jpeg_0.1-8.1 base64enc_0.1-3 pkgconfig_2.0.3 -## [37] htmltools_0.5.0 styler_1.3.2 htmlwidgets_1.5.2 -## [40] rlang_0.4.8 impute_1.64.0 rstudioapi_0.11 -## [43] RSQLite_2.2.1 farver_2.0.3 generics_0.0.2 -## [46] BiocParallel_1.24.1 dplyr_1.0.2 R.oo_1.24.0 -## [49] RCurl_1.98-1.2 Formula_1.2-3 GO.db_3.12.1 -## [52] GenomeInfoDbData_1.2.4 Matrix_1.2-18 Rcpp_1.0.5 -## [55] munsell_0.5.0 fansi_0.4.1 lifecycle_0.2.0 -## [58] R.methodsS3_1.8.1 stringi_1.5.3 yaml_2.2.1 -## [61] zlibbioc_1.36.0 grid_4.0.2 blob_1.2.1 -## [64] crayon_1.3.4 lattice_0.20-41 splines_4.0.2 -## [67] annotate_1.68.0 hms_0.5.3 locfit_1.5-9.4 -## [70] ps_1.4.0 knitr_1.30 pillar_1.4.6 -## [73] geneplotter_1.68.0 codetools_0.2-16 XML_3.99-0.5 -## [76] glue_1.4.2 evaluate_0.14 latticeExtra_0.6-29 -## [79] data.table_1.13.0 png_0.1-7 vctrs_0.3.4 -## [82] foreach_1.5.0 gtable_0.3.0 getopt_1.20.3 -## [85] purrr_0.3.4 rematch2_2.1.2 assertthat_0.2.1 -## [88] ggplot2_3.3.2 xfun_0.18 xtable_1.8-4 -## [91] survival_3.1-12 tibble_3.0.4 iterators_1.0.12 -## [94] AnnotationDbi_1.52.0 memoise_1.1.0 cluster_2.1.0 -## [97] ellipsis_0.3.1
+## [22] DelayedArray_0.16.0 checkmate_2.0.0 scales_1.1.1 +## [25] readr_1.4.0 genefilter_1.72.0 stringr_1.4.0 +## [28] digest_0.6.25 foreign_0.8-80 rmarkdown_2.4 +## [31] R.utils_2.10.1 XVector_0.30.0 jpeg_0.1-8.1 +## [34] base64enc_0.1-3 pkgconfig_2.0.3 htmltools_0.5.0 +## [37] styler_1.3.2 htmlwidgets_1.5.2 rlang_0.4.8 +## [40] impute_1.64.0 rstudioapi_0.11 RSQLite_2.2.1 +## [43] generics_0.0.2 BiocParallel_1.24.1 dplyr_1.0.2 +## [46] R.oo_1.24.0 RCurl_1.98-1.2 Formula_1.2-3 +## [49] GO.db_3.12.1 GenomeInfoDbData_1.2.4 Matrix_1.2-18 +## [52] Rcpp_1.0.5 munsell_0.5.0 fansi_0.4.1 +## [55] lifecycle_0.2.0 R.methodsS3_1.8.1 stringi_1.5.3 +## [58] yaml_2.2.1 zlibbioc_1.36.0 grid_4.0.2 +## [61] blob_1.2.1 crayon_1.3.4 lattice_0.20-41 +## [64] splines_4.0.2 annotate_1.68.0 hms_0.5.3 +## [67] locfit_1.5-9.4 ps_1.4.0 knitr_1.30 +## [70] pillar_1.4.6 geneplotter_1.68.0 codetools_0.2-16 +## [73] XML_3.99-0.5 glue_1.4.2 evaluate_0.14 +## [76] latticeExtra_0.6-29 data.table_1.13.0 png_0.1-7 +## [79] vctrs_0.3.4 foreach_1.5.0 gtable_0.3.0 +## [82] getopt_1.20.3 purrr_0.3.4 rematch2_2.1.2 +## [85] assertthat_0.2.1 ggplot2_3.3.2 xfun_0.18 +## [88] xtable_1.8-4 survival_3.1-12 tibble_3.0.4 +## [91] iterators_1.0.12 AnnotationDbi_1.52.0 memoise_1.1.0 +## [94] cluster_2.1.0 ellipsis_0.3.1

References

-
-

Alter O., P. O. Brown, and D. Botstein, 2000 Singular value decomposition for genome-wide expression data processing and modeling. Proceedings of the National Academy of Sciences 97: 10101–10106. https://doi.org/10.1073/pnas.97.18.10101

-

Hastie T., R. Tibshirani, B. Narasimhan, and G. Chu, 2020 Impute: Imputation for microarray data. https://www.bioconductor.org/packages/devel/bioc/manuals/impute/man/impute.pdf

@@ -4283,15 +4187,9 @@

References

Langfelder P., and S. Horvath, 2016 Tutorials for the WGCNA package. https://horvath.genetics.ucla.edu/html/CoexpressionNetwork/Rpackages/WGCNA/Tutorials/

-
-

Langfelder P., 2018 Signed or unsigned: Which network type is preferable? https://peterlangfelder.com/2018/11/25/signed-or-unsigned-which-network-type-is-preferable/

-

Love M. I., W. Huber, and S. Anders, 2014 Moderated estimation of fold change and dispersion for RNA-Seq data with DESeq2. Genome Biology 15. https://doi.org/10.1186/s13059-014-0550-8

-
-

Zhang B., and S. Horvath, 2005 A general framework for weighted gene co-expression network analysis. Statistical Applications in Genetics and Molecular Biology 4. https://doi.org/10.2202/1544-6115.1128

-