Commit b332e406 authored by smorabit's avatar smorabit
Browse files

working on module projection tutorial

parent 06613849
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -878,13 +878,19 @@ AvgModuleExpr <- function(seurat_obj, n_genes = 25, wgcna_name=NULL, ...){
#' @export
#' @examples
#' ModuleConnectivity(pbmc)
ModuleConnectivity <- function(seurat_obj, harmonized=TRUE, wgcna_name=NULL, ...){
ModuleConnectivity <- function(
  seurat_obj,
  harmonized=TRUE,
  wgcna_name=NULL,
  ...
){

  # set as active assay if wgcna_name is not given
  if(is.null(wgcna_name)){wgcna_name <- seurat_obj@misc$active_wgcna}

  # get module df, wgcna genes, and wgcna params:
  modules <- GetModules(seurat_obj, wgcna_name)
  MEs <- GetMEs(seurat_obj, harmonized, wgcna_name)
  genes_use <- GetWGCNAGenes(seurat_obj, wgcna_name)
  params <- GetWGCNAParams(seurat_obj, wgcna_name)

@@ -895,8 +901,10 @@ ModuleConnectivity <- function(seurat_obj, harmonized=TRUE, wgcna_name=NULL, ...
    slot=params$metacell_slot
  ))[,genes_use]

  # get MEs:
  MEs <- GetMEs(seurat_obj, harmonized, wgcna_name)
  print('datExpr')
  print(dim(datExpr))

  print('running signedKME:')

  kMEs <- WGCNA::signedKME(
    datExpr,
+4 −3
Original line number Diff line number Diff line
@@ -181,7 +181,7 @@
<p>Here we will plot the non-linear dimensionality reduction (UMAP) colored by cell type just to check that we have loaded the data correctly, and to make sure that we have grouped cells into clusters and cell types.</p>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">p</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://satijalab.org/seurat/reference/DimPlot.html" class="external-link">DimPlot</a></span><span class="op">(</span><span class="va">seurat_obj</span>, group.by<span class="op">=</span><span class="st">'cell_type'</span>, label<span class="op">=</span><span class="cn">FALSE</span><span class="op">)</span> <span class="op">+</span>
   <span class="va">umap_theme</span> <span class="op">+</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">'Cell Type'</span><span class="op">)</span>
   <span class="fu">umap_theme</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">'Cell Type'</span><span class="op">)</span>

<span class="va">p</span></code></pre></div>
<p><img src="figures/basic_tutorial/umap_celltype.png"></p>
@@ -238,8 +238,9 @@
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">seurat_obj</span> <span class="op">&lt;-</span> <span class="va">seurat_obj</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span>
  <span class="fu"><a href="../reference/NormalizeMetacells.html">NormalizeMetacells</a></span><span class="op">(</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span>
  <span class="fu"><a href="../reference/RunPCAMetacells.html">RunPCAMetacells</a></span><span class="op">(</span>features<span class="op">=</span><span class="fu"><a href="https://mojaveazure.github.io/seurat-object/reference/VariableFeatures.html" class="external-link">VariableFeatures</a></span><span class="op">(</span><span class="va">seurat_obj</span><span class="op">)</span><span class="op">)</span>
  <span class="fu"><a href="../reference/RunHarmonyMetacells.html">RunHarmonyMetacells</a></span><span class="op">(</span>group.by.vars<span class="op">=</span><span class="st">'Sample'</span><span class="op">)</span>
  <span class="fu"><a href="../reference/ScaleMetacells.html">ScaleMetacells</a></span><span class="op">(</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span>
  <span class="fu"><a href="../reference/RunPCAMetacells.html">RunPCAMetacells</a></span><span class="op">(</span>features<span class="op">=</span><span class="fu"><a href="https://mojaveazure.github.io/seurat-object/reference/VariableFeatures.html" class="external-link">VariableFeatures</a></span><span class="op">(</span><span class="va">seurat_obj</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span>
  <span class="fu"><a href="../reference/RunHarmonyMetacells.html">RunHarmonyMetacells</a></span><span class="op">(</span>group.by.vars<span class="op">=</span><span class="st">'Sample'</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span>
  <span class="fu"><a href="../reference/RunUMAPMetacells.html">RunUMAPMetacells</a></span><span class="op">(</span>reduction<span class="op">=</span><span class="st">'harmony'</span>, dims<span class="op">=</span><span class="fl">1</span><span class="op">:</span><span class="fl">15</span><span class="op">)</span>


+397 KiB
Loading image diff...
+1 −1
Original line number Diff line number Diff line
@@ -113,7 +113,7 @@
        <dd><p>Tutorial for identifying transcription factor binding motifs that are related to scWGCNA modules.</p>
        </dd><dt><a href="network_visualizations.html">Network Visualization</a></dt>
        <dd><p>Tutorial for visualizing the scWGCNA networks using igraph.</p>
        </dd><dt><a href="projecting_modules.html">Projecting Modules</a></dt>
        </dd><dt><a href="projecting_modules.html">Projecting modules</a></dt>
        <dd><p>Tutorial for applying the core functions of scWGCNA.</p>
        </dd><dt><a href="projecting_modules_cross.html">Cross-species and cross-modality analysis</a></dt>
        <dd><p>Projecting scWGCNA co-expression modules across species and -omics modalities.</p>
+90 −5
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Projecting Modules • scWGCNA</title>
<title>Projecting modules • scWGCNA</title>
<!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="../favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="../favicon-32x32.png">
<link rel="apple-touch-icon" type="image/png" sizes="180x180" href="../apple-touch-icon.png">
@@ -17,7 +17,7 @@
<script src="../bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
<!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script><meta property="og:title" content="Projecting Modules">
<script src="../pkgdown.js"></script><meta property="og:title" content="Projecting modules">
<meta property="og:description" content="Tutorial for applying the core functions of scWGCNA.
">
<meta property="og:image" content="https://smorabit.github.io/scWGCNA/logo.png">
@@ -127,7 +127,7 @@
      </header><div class="row">
  <div class="col-md-9 contents">
    <div class="page-header toc-ignore">
      <h1 data-toc-skip>Projecting Modules</h1>
      <h1 data-toc-skip>Projecting modules</h1>
            
      
      
@@ -137,12 +137,97 @@

    
    
<p>In this tutorial, we begin to analyze the co-expresion modules detected in one dataset in external datasets. Rather than building a new co-expression network from scratch in a new dataset, we can take the modules from one dataset and project them into the new dataset. Before starting this tutorial, make sure that you have constructed the co-expression network as in the <a href="articles/basics_tutorial.html">scWGCNA basics</a>. The main scWGCNA tutorials have been using the control brain samples from <a href="https://www.nature.com/articles/s41591-019-0695-9" class="external-link">this publication</a>, and now we will project the inhibitory neruon modules from Zhou et al into the control brain samples from <a href="https://doi.org/10.1038/s41588-021-00894-z" class="external-link">Morabito and Miyoshi 2021</a>.</p>
<p>First we must load the data and the required libraries:</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/r/base/print.html" class="external-link">print</a></span><span class="op">(</span><span class="st">'hello world'</span><span class="op">)</span></code></pre></div>
<code class="sourceCode R"><span class="co"># single-cell analysis package</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://satijalab.org/seurat" class="external-link">Seurat</a></span><span class="op">)</span>

<span class="co"># plotting and data science packages</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://tidyverse.tidyverse.org" class="external-link">tidyverse</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://wilkelab.org/cowplot/" class="external-link">cowplot</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://patchwork.data-imaginist.com" class="external-link">patchwork</a></span><span class="op">)</span>

<span class="co"># co-expression network analysis packages:</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="http://horvath.genetics.ucla.edu/html/CoexpressionNetwork/Rpackages/WGCNA/" class="external-link">WGCNA</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://smorabit.github.io/scWGCNA/">scWGCNA</a></span><span class="op">)</span>

<span class="co"># network analysis &amp; visualization package:</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://igraph.org" class="external-link">igraph</a></span><span class="op">)</span>

<span class="co"># using the cowplot theme for ggplot</span>
<span class="fu"><a href="https://ggplot2.tidyverse.org/reference/theme_get.html" class="external-link">theme_set</a></span><span class="op">(</span><span class="fu"><a href="https://wilkelab.org/cowplot/reference/theme_cowplot.html" class="external-link">theme_cowplot</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span>

<span class="co"># set random seed for reproducibility</span>
<span class="fu"><a href="https://rdrr.io/r/base/Random.html" class="external-link">set.seed</a></span><span class="op">(</span><span class="fl">12345</span><span class="op">)</span>

<span class="co"># load the Zhou et al snRNA-seq dataset</span>
<span class="va">seurat_ref</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/readRDS.html" class="external-link">readRDS</a></span><span class="op">(</span><span class="st">'data/Zhou_control.rds'</span><span class="op">)</span>

<span class="co"># load the Morabito &amp; Miyoshi 2021 snRNA-seq dataset</span>
<span class="va">seurat_query</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/readRDS.html" class="external-link">readRDS</a></span><span class="op">(</span>file<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste0</a></span><span class="op">(</span><span class="va">data_dir</span>, <span class="st">'Morabito_Miyoshi_2021_control.rds'</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="section level2">
<h2 id="projecting-modules">Projecting modules<a class="anchor" aria-label="anchor" href="#projecting-modules"></a>
</h2>
<p>In this section we project the modules from the Zhou et al inhibitory neuron scWGCNA experiment into the Morabito &amp; Miyoshi et al control brain dataset. We refer to the Zhou et al dataset as the “reference” dataset, and the Morabito &amp; Miyoshi et al dataset as the “query” dataset. Just <a href="basic_tutorial.html">as we had done before</a> when building the co-expression network from scratch, the basic single-cell pipeline has to be 0done on the query dataset (normalization, scaling, variable features, PCA, batch correction, UMAP, clustering). First we make a UMAP plot to visualize the two datasets to ensure they have both been processed.</p>
<details><summary>
Code
</summary><div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">p1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://satijalab.org/seurat/reference/DimPlot.html" class="external-link">DimPlot</a></span><span class="op">(</span><span class="va">seurat_ref</span>, group.by<span class="op">=</span><span class="st">'cell_type'</span>, label<span class="op">=</span><span class="cn">TRUE</span><span class="op">)</span> <span class="op">+</span>
   <span class="fu">umap_theme</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
   <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">'Zhou'</span><span class="op">)</span> <span class="op">+</span>
   <span class="fu"><a href="https://satijalab.org/seurat/reference/SeuratTheme.html" class="external-link">NoLegend</a></span><span class="op">(</span><span class="op">)</span>

<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://satijalab.org/seurat/reference/DimPlot.html" class="external-link">DimPlot</a></span><span class="op">(</span><span class="va">seurat_query</span>, group.by<span class="op">=</span><span class="st">'cell_type'</span>, label<span class="op">=</span><span class="cn">TRUE</span><span class="op">)</span> <span class="op">+</span>
   <span class="fu">umap_theme</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
   <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/labs.html" class="external-link">ggtitle</a></span><span class="op">(</span><span class="st">'Morabito &amp; Miyoshi'</span><span class="op">)</span> <span class="op">+</span>
   <span class="fu"><a href="https://satijalab.org/seurat/reference/SeuratTheme.html" class="external-link">NoLegend</a></span><span class="op">(</span><span class="op">)</span>

<span class="va">p1</span> <span class="op">|</span> <span class="va">p2</span></code></pre></div>
</details><p><img src="figures/projection/compare_umaps.png" width="600" height="600"></p>
<p>Next we will run the function <code>ProjectModules</code> to project the modules from the reference dataset into the query dataset. If the genes used for co-expression network analysis in the reference dataset have not been scaled in the query dataset, Seurat’s <code>ScaleData</code> function will be run from within <code>ProjectModules</code>. We perform the following analysis steps to project the modules into the query dataset:</p>
<ul>
<li>Run <code>ProjectModules</code> to compute the module eigengenes in the query dataset based on the gene modules in the reference dataset.</li>
<li>Optionally run <code>ModuleExprScore</code> to compute hub gene expression scores for the projected modules.</li>
<li>Run <code>ModuleConnectivity</code> to compute intramodular connectivity (<em>kME</em>) in the query dataset.</li>
</ul>
<div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># Project modules from query to reference dataset</span>
<span class="va">seurat_query</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/ProjectModules.html">ProjectModules</a></span><span class="op">(</span>
  seurat_obj <span class="op">=</span> <span class="va">seurat_query</span>,
  seurat_ref <span class="op">=</span> <span class="va">seurat_ref</span>,
  scale_genes <span class="op">=</span> <span class="cn">TRUE</span>,
  <span class="co"># vars.to.regress = c(), # optionally regress covariates when running ScaleData</span>
  wgcna_name_proj<span class="op">=</span><span class="st">"Zhou_projected"</span>, <span class="co"># name of the new scWGCNA experiment in the query dataset</span>
  wgcna_name <span class="op">=</span> <span class="st">"tutorial"</span> <span class="co"># name of the scWGCNA experiment in the ref dataset</span>
<span class="op">)</span></code></pre></div>
<p>As you can see it only takes running one function to project co-expression modules from one dataset into another using scWGCNA. Optionally, we can also compute the hub gene expression scores and the intramodular connectivity for the projected modules.</p>
<details><summary>
Code
</summary><div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># compute module hub gene expression scores for projected modules:</span>
<span class="va">seurat_query</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/ModuleExprScore.html">ModuleExprScore</a></span><span class="op">(</span>
  <span class="va">seurat_query</span>,
  n_genes <span class="op">=</span> <span class="fl">25</span>,
  method<span class="op">=</span><span class="st">'Seurat'</span>
<span class="op">)</span>

<span class="co"># compute intramodular connectivity</span>
<span class="va">seurat_query</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/ModuleConnectivity.html">ModuleConnectivity</a></span><span class="op">(</span><span class="va">seurat_query</span><span class="op">)</span></code></pre></div>
</details><p>We can extract the projected module eigengenes using the <code>GetMEs</code> function.</p>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">projected_hMEs</span> <span class="op">&lt;-</span> <span class="fu">GetModules</span><span class="op">(</span><span class="va">seurat_query</span><span class="op">)</span></code></pre></div>
</div>
<div class="section level2">
<h2 id="visualization">Visualization<a class="anchor" aria-label="anchor" href="#visualization"></a>
</h2>
</div>
  </div>

  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">

        <nav id="toc" data-toggle="toc"><h2 data-toc-skip>Contents</h2>
    </nav>
</div>

</div>
Loading