Commit 95bc7ce3 authored by smorabit's avatar smorabit
Browse files

working on module preservation tutorial

parent 27b17bde
Loading
Loading
Loading
Loading
+9 −7
Original line number Diff line number Diff line
@@ -1648,6 +1648,7 @@ MotifTargetScore <- function(
#' @examples
RunModuleUMAP <- function(
  seurat_obj,
  features = "TOM", # "TOM" or "kME"
  n_hubs = 10,
  exclude_grey = TRUE,
  wgcna_name = NULL,
@@ -1661,15 +1662,13 @@ RunModuleUMAP <- function(

  if(is.null(wgcna_name)){wgcna_name <- seurat_obj@misc$active_wgcna}

  # get the TOM
  TOM <- GetTOM(seurat_obj, wgcna_name)

  # get modules,
  modules <- GetModules(seurat_obj, wgcna_name)
  mods <- levels(modules$module)

  if(exclude_grey){
    mods <- mods[mods != 'grey']
    modules <- subset(modules, module != 'grey')
  }

  # get hub genes:
@@ -1683,15 +1682,18 @@ RunModuleUMAP <- function(
  # get all genes that aren't in gray mod
  selected_genes <- modules[modules$module %in% mods,'gene_name']

  # get the TOM
  TOM <- GetTOM(seurat_obj, wgcna_name)

  # subset the TOM for umap
  # keep all genes as rows, and keep only hubs as cols (features)
  umap_TOM <- TOM[selected_genes,unlist(hub_list)]
  feature_mat <- TOM[selected_genes,unlist(hub_list)]

  # run UMAP
  if(supervised){
    print('running supervised UMAP:')
    hub_umap <-  uwot::umap(
      X = umap_TOM,
      X = feature_mat,
      min_dist = min_dist,
      n_neighbors= n_neighbors,
      metric = metric,
@@ -1701,7 +1703,7 @@ RunModuleUMAP <- function(
    )
  } else {
    hub_umap <-  uwot::umap(
      X = umap_TOM,
      X = feature_mat,
      min_dist = min_dist,
      n_neighbors= n_neighbors,
      metric = metric,
@@ -1714,7 +1716,7 @@ RunModuleUMAP <- function(
  # set up plotting df
  plot_df <- as.data.frame(hub_umap)
  colnames(plot_df) <- c("UMAP1", "UMAP2")
  plot_df$gene <- rownames(umap_TOM)
  plot_df$gene <- rownames(feature_mat)

  # add module color, and hub gene status to the plotting df:
  ix <- match(plot_df$gene, modules$gene_name)
+2 −1
Original line number Diff line number Diff line
@@ -1055,7 +1055,8 @@ ModuleUMAPPlot <- function(
    vertex.label.font = 3,
    vertex.label.color = V(g)$fontcolor,
    vertex.label.cex=0,
    vertex.frame.color=V(g)$framecolor
    vertex.frame.color=V(g)$framecolor,
    margin=0
  )

}
+10 −10
Original line number Diff line number Diff line
@@ -184,7 +184,7 @@
   <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>
<p><img src="figures/basic_tutorial/umap_celltype.png" width="600" height="600"></p>
</div>
</div>
<div class="section level2">
@@ -248,7 +248,7 @@
<span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/DimPlotMetacells.html">DimPlotMetacells</a></span><span class="op">(</span><span class="va">seurat_obj</span>, group.by<span class="op">=</span><span class="st">'Sample'</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">"Sample"</span><span class="op">)</span>

<span class="va">p1</span> <span class="op">|</span> <span class="va">p2</span></code></pre></div>
<p><img src="figures/basic_tutorial/umap_metacells.png"></p>
<p><img src="figures/basic_tutorial/umap_metacells.png" width="600" height="600"></p>
</div>
</div>
<div class="section level2">
@@ -302,7 +302,7 @@
<p>scWGCNA also includes a function <code>PlotDendrogram</code> to visualize the WGCNA dendrogram, a common visualization to show the different co-expression modules resulting from the network analysis. Each leaf on the dendrogram represents a single gene, and the color at the bottom indicates the co-expression module assignment.</p>
<div class="sourceCode" id="cb11"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="../reference/PlotDendrogram.html">PlotDendrogram</a></span><span class="op">(</span><span class="va">seurat_obj</span>, main<span class="op">=</span><span class="st">'INH scWGCNA Dendrogram'</span><span class="op">)</span></code></pre></div>
<p><img src="figures/basic_tutorial/inh_dendro.png"></p>
<p><img src="figures/basic_tutorial/inh_dendro.png" width="600" height="600"></p>
</div>
</div>
<div class="section level2">
@@ -344,7 +344,7 @@
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># compute intramodular connectivity:</span>
<span class="va">seurat_obj</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_obj</span><span class="op">)</span></code></pre></div>
<p>For convenience, we re-name the scWGCNA modules to indicate that they are from the inhibitory neuron group. More information about renaming modules can be found in the <a href="articles/customization.html">module customization tutorial</a>.</p>
<p>For convenience, we re-name the scWGCNA modules to indicate that they are from the inhibitory neuron group. More information about renaming modules can be found in the <a href="customization.html">module customization tutorial</a>.</p>
<div class="sourceCode" id="cb15"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># rename the modules</span>
<span class="va">seurat_obj</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/ResetModuleNames.html">ResetModuleNames</a></span><span class="op">(</span>
@@ -404,7 +404,7 @@

<span class="co"># stitch together with patchwork</span>
<span class="fu"><a href="https://patchwork.data-imaginist.com/reference/wrap_plots.html" class="external-link">wrap_plots</a></span><span class="op">(</span><span class="va">plot_list</span>, ncol<span class="op">=</span><span class="fl">6</span><span class="op">)</span></code></pre></div>
<p><img src="figures/basic_tutorial/ME_featureplot.png"></p>
<p><img src="figures/basic_tutorial/ME_featureplot.png" width="600" height="600"></p>
<p>We can also plot the hub gene signature score using the same function:</p>
<div class="sourceCode" id="cb20"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># make a featureplot of hub scores for each module</span>
@@ -416,7 +416,7 @@

<span class="co"># stitch together with patchwork</span>
<span class="fu"><a href="https://patchwork.data-imaginist.com/reference/wrap_plots.html" class="external-link">wrap_plots</a></span><span class="op">(</span><span class="va">plot_list</span>, ncol<span class="op">=</span><span class="fl">6</span><span class="op">)</span></code></pre></div>
<p><img src="figures/basic_tutorial/ME_featureplot_scores.png"></p>
<p><img src="figures/basic_tutorial/ME_featureplot_scores.png" width="600" height="600"></p>
</div>
<div class="section level3">
<h3 id="module-correlations">Module Correlations<a class="anchor" aria-label="anchor" href="#module-correlations"></a>
@@ -425,12 +425,12 @@
<div class="sourceCode" id="cb21"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># plot module correlagram</span>
<span class="fu"><a href="../reference/ModuleCorrelogram.html">ModuleCorrelogram</a></span><span class="op">(</span><span class="va">seurat_obj</span><span class="op">)</span></code></pre></div>
<p><img src="figures/basic_tutorial/ME_correlogram.png"></p>
<p><img src="figures/basic_tutorial/ME_correlogram.png" width="500" height="500"></p>
<p>While scWGCNA includes the <code>ModuleCorrelogram</code> function as shown above, we note that R has a rich ecosystem of statistical analysis tools that can be leveraged to understand the relationship between different modules, and we encourage users to explore their data beyond the functions that we provide here. Here is an example of inspecting the correlation structure of the hMEs using an alternative approach, <a href="rdocumentation.org/packages/GGally/versions/1.5.0">GGally</a>:</p>
<div class="sourceCode" id="cb22"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># use GGally to investigate 6 selected modules:</span>
<span class="fu">GGally</span><span class="fu">::</span><span class="fu">ggpairs</span><span class="op">(</span><span class="fu"><a href="../reference/GetMEs.html">GetMEs</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="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">1</span><span class="op">:</span><span class="fl">3</span>,<span class="fl">12</span><span class="op">:</span><span class="fl">15</span><span class="op">)</span><span class="op">]</span><span class="op">)</span></code></pre></div>
<p><img src="figures/basic_tutorial/ME_correlation_GGally.png"></p>
<p><img src="figures/basic_tutorial/ME_correlation_GGally.png" width="500" height="500"></p>
</div>
<div class="section level3">
<h3 id="seurat-plotting-functions">Seurat plotting functions<a class="anchor" aria-label="anchor" href="#seurat-plotting-functions"></a>
@@ -462,7 +462,7 @@

<span class="co"># plot output</span>
<span class="va">p</span></code></pre></div>
<p><img src="figures/basic_tutorial/ME_dotplot.png"></p>
<p><img src="figures/basic_tutorial/ME_dotplot.png" width="600" height="600"></p>
<p>Here is another example where we use Seurat’s <code>VlnPlot</code> function:</p>
<div class="sourceCode" id="cb25"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># Plot INH-M4 hME using Seurat VlnPlot function</span>
@@ -481,7 +481,7 @@

<span class="co"># plot output</span>
<span class="va">p</span></code></pre></div>
<p><img src="figures/basic_tutorial/ME_vlnplot.png"></p>
<p><img src="figures/basic_tutorial/ME_vlnplot.png" width="600" height="600"></p>
</div>
</div>
<div class="section level2">
+1.99 MiB
Loading image diff...
+352 KiB
Loading image diff...
Loading