<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<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>Cell Bender Functionality &amp; Plotting • scCustomize</title>
<!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.4.0/flatly/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="../bootstrap-toc.css">
<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="Cell Bender Functionality &amp; Plotting">
<meta property="og:description" content="scCustomize">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body data-spy="scroll" data-target="#toc">
    

    <div class="container template-article">
      <header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="../index.html">scCustomize</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="">2.1.2</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
<li>
  <a href="../index.html">Getting Started</a>
</li>
<li>
  <a href="../articles/Installation.html">Install</a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
    Vignettes
     
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu">
<li class="dropdown-header">Plotting Vignettes</li>
    <li>
      <a href="../articles/Gene_Expression_Plotting.html">Plotting #1: General Analysis Plots</a>
    </li>
    <li>
      <a href="../articles/QC_Plots.html">Plotting #2: QC Plots</a>
    </li>
    <li>
      <a href="../articles/Sequencing_QC_Plots.html">Plotting #3: Sequencing QC Plots/Analysis</a>
    </li>
    <li>
      <a href="../articles/Iterative_Plotting.html">Plotting #4: Iterative Plotting Functions</a>
    </li>
    <li>
      <a href="../articles/Color_Palettes.html">Customized Color Palettes &amp; Themes</a>
    </li>
    <li class="divider">
    </li>
<li class="dropdown-header">Helpers &amp; Utilities</li>
    <li>
      <a href="../articles/Helpers_and_Utilities.html">General Helpers &amp; Utilities</a>
    </li>
    <li>
      <a href="../articles/Read_and_Write_Functions.html">Read &amp; Write Data Functions</a>
    </li>
    <li>
      <a href="../articles/Object_Conversion.html">Object/Assay Format Conversion</a>
    </li>
    <li>
      <a href="../articles/Markers_and_Cluster_Annotation.html">Marker Identification &amp; Cluster Annotation Helpers</a>
    </li>
    <li>
      <a href="../articles/Statistics.html">Statistics Functions</a>
    </li>
    <li>
      <a href="../articles/Update_Gene_Symbols.html">Updating Gene Symbols</a>
    </li>
    <li>
      <a href="../articles/Misc_Functions.html">Misc Functions</a>
    </li>
    <li class="divider">
    </li>
<li class="dropdown-header">Other Package Interactivity/Functions</li>
    <li>
      <a href="../articles/LIGER_Functions.html">LIGER Plotting &amp; Functionality</a>
    </li>
    <li>
      <a href="../articles/Cell_Bender_Functions.html">Cell Bender Functionality &amp; Plotting</a>
    </li>
  </ul>
</li>
<li>
  <a href="../news/index.html">News/Changelog</a>
</li>
<li>
  <a href="../reference/index.html">Reference</a>
</li>
<li>
  <a href="../articles/FAQ.html">FAQ</a>
</li>
      </ul>
<ul class="nav navbar-nav navbar-right">
<li>
  <a href="https://github.com/samuel-marsh/scCustomize/" class="external-link">
    <span class="fab fa-github fa-lg"></span>
     
  </a>
</li>
      </ul>
</div>
<!--/.nav-collapse -->
  </div>
<!--/.container -->
</div>
<!--/.navbar -->

      

      </header><script src="Cell_Bender_Functions_files/kePrint-0.0.1/kePrint.js"></script><link href="Cell_Bender_Functions_files/lightable-0.0.1/lightable.css" rel="stylesheet">
<div class="row">
  <div class="col-md-9 contents">
    <div class="page-header toc-ignore">
      <h1 data-toc-skip>Cell Bender Functionality &amp; Plotting</h1>
            
            <h4 data-toc-skip class="date">Compiled: February 28,
2024</h4>
      
      <small class="dont-index">Source: <a href="https://github.com/samuel-marsh/scCustomize/blob/HEAD/vignettes/articles/Cell_Bender_Functions.Rmd" class="external-link"><code>vignettes/articles/Cell_Bender_Functions.Rmd</code></a></small>
      <div class="hidden name"><code>Cell_Bender_Functions.Rmd</code></div>

    </div>

    
    
<hr>
<style>
p.caption {
  font-size: 0.9em;
}
</style>
<div class="section level2">
<h2 id="cellbender-functionality">CellBender Functionality<a class="anchor" aria-label="anchor" href="#cellbender-functionality"></a>
</h2>
<p>CellBender is software for elimination of technical artifacts in
scRNA-seq/snRNA-seq that uses deep generative model for unsupervised
removal of ambient RNA and chimeric PCR artifacts. You can find out more
info about CellBender from the <a href="https://doi.org/10.1101/791699" class="external-link">bioRxiv Preprint</a>, <a href="https://github.com/broadinstitute/CellBender" class="external-link">GitHub Repo</a>, and
<a href="https://cellbender.readthedocs.io/en/latest/" class="external-link">Documentation</a>.</p>
<p>Following completion of CellBender scCustomize contains a couple of
functions that may be helpful when creating and visualizing data in
Seurat.</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><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://ggplot2.tidyverse.org" class="external-link">ggplot2</a></span><span class="op">)</span></span>
<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://dplyr.tidyverse.org" class="external-link">dplyr</a></span><span class="op">)</span></span>
<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://magrittr.tidyverse.org" class="external-link">magrittr</a></span><span class="op">)</span></span>
<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>
<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://sjmgarnier.github.io/viridis/" class="external-link">viridis</a></span><span class="op">)</span></span>
<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>
<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://github.com/samuel-marsh/scCustomize" class="external-link">scCustomize</a></span><span class="op">)</span></span>
<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://github.com/traversc/qs" class="external-link">qs</a></span><span class="op">)</span></span></code></pre></div>
<div class="section level3">
<h3 id="importing-cell-bender-h5-outputs">Importing Cell Bender H5 Outputs<a class="anchor" aria-label="anchor" href="#importing-cell-bender-h5-outputs"></a>
</h3>
<p>The output from CellBender is an H5 file that is styled and can be
read like 10X Genomics H5 file. In CellBender pre-v3 this file could be
read by <code><a href="https://satijalab.org/seurat/reference/Read10X_h5.html" class="external-link">Seurat::Read10X_h5()</a></code> (although that function
assumes that the file contains no name prefix). However, v3+ contains
extra information that causes <code>Read10X_h5</code> to fail.</p>
<p>scCustomize contains function <code><a href="../reference/Read_CellBender_h5_Mat.html">Read_CellBender_h5_Mat()</a></code>
can be used when reading in CellBender h5 files regardless of the
version of CellBender used.</p>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">cell_bender_mat</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/Read_CellBender_h5_Mat.html">Read_CellBender_h5_Mat</a></span><span class="op">(</span>file_name <span class="op">=</span> <span class="st">"PATH/SampleA_out_filtered.h5"</span><span class="op">)</span></span></code></pre></div>
<div class="section level4">
<h4 id="importing-cellbender-data-based-on-starsolo-or-pre-v3-cell-ranger-inputs">Importing CellBender data based on STARsolo or pre-V3 Cell Ranger
inputs<a class="anchor" aria-label="anchor" href="#importing-cellbender-data-based-on-starsolo-or-pre-v3-cell-ranger-inputs"></a>
</h4>
<p>If the input that CellBender uses is based on STARsolo or pre-V3 Cell
Ranger data then some of the slot name which stores feature/gene ids in
the H5 file is different. These files can be read by specifying the
optional <code>feature_slot_name</code> parameter.</p>
<div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">cell_bender_starsolo_mat</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/Read_CellBender_h5_Mat.html">Read_CellBender_h5_Mat</a></span><span class="op">(</span>file_name <span class="op">=</span> <span class="st">"PATH/SampleA_out_filtered.h5"</span>, feature_slot_name <span class="op">=</span> <span class="st">"genes"</span><span class="op">)</span></span></code></pre></div>
</div>
<div class="section level4">
<h4 id="importing-cellbender-data-with-non-standard-group-names">Importing CellBender data with non-standard group names<a class="anchor" aria-label="anchor" href="#importing-cellbender-data-with-non-standard-group-names"></a>
</h4>
<p>If CellBender H5 file contains non-standard H5 group names then
<code>Read_CellBender_h5_Mat</code> will error. To circumvent this
simply supply the name of the H5 group that contains the count data.</p>
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">cell_bender_name_mat</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/Read_CellBender_h5_Mat.html">Read_CellBender_h5_Mat</a></span><span class="op">(</span>file_name <span class="op">=</span> <span class="st">"PATH/SampleA_out_filtered.h5"</span>, h5_group_name <span class="op">=</span> <span class="st">"background_removed"</span><span class="op">)</span></span></code></pre></div>
</div>
</div>
<div class="section level3">
<h3 id="reading-multiple-cellbender-files-with-single-function">Reading multiple CellBender files with single function<a class="anchor" aria-label="anchor" href="#reading-multiple-cellbender-files-with-single-function"></a>
</h3>
<p>scCustomize also contains two wrapper functions to easily read
multiple CellBender files stored either in single directory or in
multiple sub-directories <code>Read_CellBender_h5_Multi_File</code> and
<code>Read_CellBender_h5_Multi_Directory</code>.</p>
<div class="section level4">
<h4 id="read-in-h5-outputs-from-multiple-sub-directories-with-read_cellbender_h5_multi_directory-">Read in H5 outputs from multiple sub-directories with
<code>Read_CellBender_h5_Multi_Directory()</code>.<a class="anchor" aria-label="anchor" href="#read-in-h5-outputs-from-multiple-sub-directories-with-read_cellbender_h5_multi_directory-"></a>
</h4>
<div class="section level5">
<h5 id="cell-bender-output-structure">Cell Bender Output Structure<a class="anchor" aria-label="anchor" href="#cell-bender-output-structure"></a>
</h5>
<p>The following is typical output directory structure for Cell Bender
files with sub-directory labeled with sample name and each file also
prefixed with the sample name.</p>
<pre><code>Parent_Directory
├── sample_01
│   └── sample_01_out_cell_barcodes.csv
│   └── sample_01_out_filtered.h5
│   └── sample_01_out.h5
│   └── sample_01_out.log
│   └── sample_01_out.pdf
└── sample_02
│   └── sample_02_out_cell_barcodes.csv
│   └── sample_02_out_filtered.h5
│   └── sample_02_out.h5
│   └── sample_02_out.log
│   └── sample_02_out.pdf</code></pre>
</div>
<div class="section level5">
<h5 id="read-files">Read Files<a class="anchor" aria-label="anchor" href="#read-files"></a>
</h5>
<p>All we have to do is adjust the parameters to account for cell bender
file names and directory structure.</p>
<ul>
<li>
<code>secondary_path</code> In this case we can leave as
<code>NULL</code> because samples are located in immediate subdirectory
(secondary path must be the same for all samples).</li>
<li>
<code>custom_name = "_out.h5"</code> This specifies what the common
file suffix of all files are.</li>
</ul>
<p><strong>Optional Parameters</strong><br>
* <code>parallel</code> and <code>num_cores</code> to use multiple core
processing. * <code>sample_list</code> By default
<code>Read_CellBender_h5_Multi_Directory</code> will read in all
sub-directories present in parent directory. However a subset can be
specified by passing a vector of sample directory names. *
<code>sample_names</code> As with other functions by default
<code>Read_CellBender_h5_Multi_Directory</code> will use the
sub-directory names within parent directory to name the output list
entries. Alternate names for the list entries can be provided here if
desired. These names will also be used to add cell prefixes if
<code>merge = TRUE</code> (see below).<br>
* <code>merge</code> logical (default FALSE). Whether to combine all
samples into single sparse matrix and using <code>sample_names</code> to
provide sample prefixes.</p>
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">cell_bender_merged</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/Read_CellBender_h5_Multi_Directory.html">Read_CellBender_h5_Multi_Directory</a></span><span class="op">(</span>base_path <span class="op">=</span> <span class="st">"assets/Cell_Bender_Example/"</span>,</span>
<span>    custom_name <span class="op">=</span> <span class="st">"_out.h5"</span>, sample_names <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="st">"WT1"</span>, <span class="st">"WT2"</span><span class="op">)</span>, merge <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></code></pre></div>
</div>
</div>
<div class="section level4">
<h4 id="read-in-h5-outputs-from-single-directory-with-read_cellbender_h5_multi_file-">Read in H5 outputs from single directory with
<code>Read_CellBender_h5_Multi_File()</code>.<a class="anchor" aria-label="anchor" href="#read-in-h5-outputs-from-single-directory-with-read_cellbender_h5_multi_file-"></a>
</h4>
<p>If all output files are in single directory you can use
<code>Read_CellBender_h5_Multi_File</code> to read in all of the files
with single function.</p>
<pre><code>Parent_Directory
├── CellBender_Outputs
│   └── sample_01_out.h5
│   └── sample_02_out.h5</code></pre>
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">cell_bender_merged</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/Read_CellBender_h5_Multi_File.html">Read_CellBender_h5_Multi_File</a></span><span class="op">(</span>data_dir <span class="op">=</span> <span class="st">"assets/Cell_Bender_Example/"</span>, custom_name <span class="op">=</span> <span class="st">"_out.h5"</span>,</span>
<span>    sample_names <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="st">"WT1"</span>, <span class="st">"WT2"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
</div>
</div>
<div class="section level3">
<h3 id="create-seurat-object-with-corrected-counts">Create Seurat Object with Corrected Counts<a class="anchor" aria-label="anchor" href="#create-seurat-object-with-corrected-counts"></a>
</h3>
<p>Creating a Seurat object from merged CellBender matrices then is
identical to creating any other Seurat object.</p>
<div class="sourceCode" id="cb9"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">cell_bender_seurat</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/SeuratObject/man/CreateSeuratObject.html" class="external-link">CreateSeuratObject</a></span><span class="op">(</span>counts <span class="op">=</span> <span class="va">cell_bender_merged</span>, names.field <span class="op">=</span> <span class="fl">1</span>, names.delim <span class="op">=</span> <span class="st">"_"</span><span class="op">)</span></span></code></pre></div>
</div>
<div class="section level3">
<h3 id="creating-dual-assay-objects">Creating Dual Assay Objects<a class="anchor" aria-label="anchor" href="#creating-dual-assay-objects"></a>
</h3>
<p>Sometimes it can be helpful to create object that contains both the
cell ranger values and cell bender values (we’ll come to why below).
scCustomize contains a helper function
<code><a href="../reference/Create_CellBender_Merged_Seurat.html">Create_CellBender_Merged_Seurat()</a></code> to handle object creation
in one quick step.</p>
<p>For this function we assume that we will use the cell calling
algorithm of Cell Ranger with the modified counts for Cell Bender.</p>
<div class="section level4">
<h4 id="read-in-both-sets-of-data">Read in both sets of data<a class="anchor" aria-label="anchor" href="#read-in-both-sets-of-data"></a>
</h4>
<div class="sourceCode" id="cb10"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">cell_bender_merged</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/Read_CellBender_h5_Multi_Directory.html">Read_CellBender_h5_Multi_Directory</a></span><span class="op">(</span>base_path <span class="op">=</span> <span class="st">"assets/Cell_Bender_Cell_Ranger_Data/Cell_Bender_Example/"</span>,</span>
<span>    custom_name <span class="op">=</span> <span class="st">"_out.h5"</span>, sample_names <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="st">"WT1"</span>, <span class="st">"WT2"</span><span class="op">)</span>, merge <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span>
<span></span>
<span><span class="va">cell_ranger_merged</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/Read10X_h5_Multi_Directory.html">Read10X_h5_Multi_Directory</a></span><span class="op">(</span>base_path <span class="op">=</span> <span class="st">"assets/Cell_Bender_Cell_Ranger_Data/Cell_Ranger_Example/"</span>,</span>
<span>    default_10X_path <span class="op">=</span> <span class="cn">FALSE</span>, h5_filename <span class="op">=</span> <span class="st">"filtered_feature_bc_matrix.h5"</span>, merge <span class="op">=</span> <span class="cn">TRUE</span>, sample_names <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="st">"WT1"</span>,</span>
<span>        <span class="st">"WT2"</span><span class="op">)</span>, parallel <span class="op">=</span> <span class="cn">TRUE</span>, num_cores <span class="op">=</span> <span class="fl">2</span><span class="op">)</span></span></code></pre></div>
</div>
<div class="section level4">
<h4 id="create-dual-assay-seurat-object">Create Dual Assay Seurat Object<a class="anchor" aria-label="anchor" href="#create-dual-assay-seurat-object"></a>
</h4>
<p>To run the function the user simply needs to provide the names of the
two matrices and a name for assay containing the Cell Ranger counts (by
default this is named “RAW”).</p>
<div class="sourceCode" id="cb11"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">dual_seurat</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/Create_CellBender_Merged_Seurat.html">Create_CellBender_Merged_Seurat</a></span><span class="op">(</span>raw_cell_bender_matrix <span class="op">=</span> <span class="va">cell_bender_merged</span>, raw_counts_matrix <span class="op">=</span> <span class="va">cell_ranger_merged</span>,</span>
<span>    raw_assay_name <span class="op">=</span> <span class="st">"RAW"</span><span class="op">)</span></span></code></pre></div>
</div>
<div class="section level4">
<h4 id="optional-parameters">Optional Parameters<a class="anchor" aria-label="anchor" href="#optional-parameters"></a>
</h4>
<p>Users can specify any additional parameters normally passed to
<code><a href="https://rdrr.io/pkg/SeuratObject/man/CreateSeuratObject.html" class="external-link">Seurat::CreateSeuratObject()</a></code> when using this function.</p>
<div class="sourceCode" id="cb12"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">dual_seurat</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/Create_CellBender_Merged_Seurat.html">Create_CellBender_Merged_Seurat</a></span><span class="op">(</span>raw_cell_bender_matrix <span class="op">=</span> <span class="va">cell_bender_merged</span>, raw_counts_matrix <span class="op">=</span> <span class="va">cell_ranger_merged</span>,</span>
<span>    raw_assay_name <span class="op">=</span> <span class="st">"RAW"</span>, min_cells <span class="op">=</span> <span class="fl">5</span>, min_features <span class="op">=</span> <span class="fl">200</span><span class="op">)</span></span></code></pre></div>
</div>
</div>
<div class="section level3">
<h3 id="prepost-cell-bender-analysis">Pre/Post Cell Bender Analysis<a class="anchor" aria-label="anchor" href="#prepost-cell-bender-analysis"></a>
</h3>
<p>It can be very important with tools like Cell Bender to analyze how
much the process has effected data on a per cell basis.</p>
<div class="section level4">
<h4 id="add-prepost-to-meta-data">Add Pre/Post to Meta Data<a class="anchor" aria-label="anchor" href="#add-prepost-to-meta-data"></a>
</h4>
<p>scCustomize includes function <code><a href="../reference/Add_CellBender_Diff.html">Add_CellBender_Diff()</a></code> to
help with this process. This function will take the nCount and nFeature
statistics from both assays in the object and calculate the difference
and return 2 new columns (“nCount_Diff” and “nFeature_Diff”) to the
object meta.data.</p>
<div class="sourceCode" id="cb13"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">dual_seurat</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/Add_CellBender_Diff.html">Add_CellBender_Diff</a></span><span class="op">(</span>seurat_object <span class="op">=</span> <span class="va">dual_seurat</span>, raw_assay_name <span class="op">=</span> <span class="st">"RAW"</span>, cell_bender_assay_name <span class="op">=</span> <span class="st">"RNA"</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="va">dual_seurat</span><span class="op">@</span><span class="va">meta.data</span>, <span class="fl">5</span><span class="op">)</span></span></code></pre></div>
<table class="table table table-bordered table-condensed table-responsive table-striped" style="margin-left: auto; margin-right: auto;">
<thead><tr>
<th style="text-align:left;">
</th>
<th style="text-align:left;">
orig.ident
</th>
<th style="text-align:right;">
nCount_RNA
</th>
<th style="text-align:right;">
nFeature_RNA
</th>
<th style="text-align:right;">
nCount_RAW
</th>
<th style="text-align:right;">
nFeature_RAW
</th>
<th style="text-align:right;">
nFeature_Diff
</th>
<th style="text-align:right;">
nCount_Diff
</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:left;">
WT1_AAACCCAAGCGACCCT-1
</td>
<td style="text-align:left;">
WT1
</td>
<td style="text-align:right;">
9844
</td>
<td style="text-align:right;">
3423
</td>
<td style="text-align:right;">
10032
</td>
<td style="text-align:right;">
3481
</td>
<td style="text-align:right;">
58
</td>
<td style="text-align:right;">
188
</td>
</tr>
<tr>
<td style="text-align:left;">
WT1_AAACCCAAGGAGGCAG-1
</td>
<td style="text-align:left;">
WT1
</td>
<td style="text-align:right;">
9248
</td>
<td style="text-align:right;">
3477
</td>
<td style="text-align:right;">
9431
</td>
<td style="text-align:right;">
3531
</td>
<td style="text-align:right;">
54
</td>
<td style="text-align:right;">
183
</td>
</tr>
<tr>
<td style="text-align:left;">
WT1_AAACCCACACACCAGC-1
</td>
<td style="text-align:left;">
WT1
</td>
<td style="text-align:right;">
3207
</td>
<td style="text-align:right;">
1699
</td>
<td style="text-align:right;">
3472
</td>
<td style="text-align:right;">
1852
</td>
<td style="text-align:right;">
153
</td>
<td style="text-align:right;">
265
</td>
</tr>
<tr>
<td style="text-align:left;">
WT1_AAACCCACATCTGTTT-1
</td>
<td style="text-align:left;">
WT1
</td>
<td style="text-align:right;">
14328
</td>
<td style="text-align:right;">
3930
</td>
<td style="text-align:right;">
14572
</td>
<td style="text-align:right;">
3993
</td>
<td style="text-align:right;">
63
</td>
<td style="text-align:right;">
244
</td>
</tr>
<tr>
<td style="text-align:left;">
WT1_AAACCCAGTATTCTCT-1
</td>
<td style="text-align:left;">
WT1
</td>
<td style="text-align:right;">
12034
</td>
<td style="text-align:right;">
3911
</td>
<td style="text-align:right;">
12223
</td>
<td style="text-align:right;">
3958
</td>
<td style="text-align:right;">
47
</td>
<td style="text-align:right;">
189
</td>
</tr>
</tbody>
</table>
</div>
<div class="section level4">
<h4 id="calculate-per-sample-averages">Calculate per sample averages<a class="anchor" aria-label="anchor" href="#calculate-per-sample-averages"></a>
</h4>
<p>We can then use <code><a href="../reference/Median_Stats.html">Median_Stats()</a></code> to calculate per sample
averages across all cells by supplying the new variables to the
<code>median_var</code> parameter.</p>
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">median_stats</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/Median_Stats.html">Median_Stats</a></span><span class="op">(</span>seurat_object <span class="op">=</span> <span class="va">dual_seurat</span>, group_by_var <span class="op">=</span> <span class="st">"orig.ident"</span>, median_var <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="st">"nCount_Diff"</span>,</span>
<span>    <span class="st">"nFeature_Diff"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<table class="table table table-bordered table-condensed table-responsive table-striped" style="margin-left: auto; margin-right: auto;">
<thead><tr>
<th style="text-align:left;">
orig.ident
</th>
<th style="text-align:right;">
Median_nCount_RNA
</th>
<th style="text-align:right;">
Median_nFeature_RNA
</th>
<th style="text-align:right;">
Median_nCount_Diff
</th>
<th style="text-align:right;">
Median_nFeature_Diff
</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:left;">
WT1
</td>
<td style="text-align:right;">
7724.5
</td>
<td style="text-align:right;">
2936
</td>
<td style="text-align:right;">
216
</td>
<td style="text-align:right;">
73
</td>
</tr>
<tr>
<td style="text-align:left;">
WT2
</td>
<td style="text-align:right;">
7404.5
</td>
<td style="text-align:right;">
2803
</td>
<td style="text-align:right;">
212
</td>
<td style="text-align:right;">
61
</td>
</tr>
<tr>
<td style="text-align:left;">
Totals (All Cells)
</td>
<td style="text-align:right;">
7565.0
</td>
<td style="text-align:right;">
2869
</td>
<td style="text-align:right;">
214
</td>
<td style="text-align:right;">
67
</td>
</tr>
</tbody>
</table>
</div>
<div class="section level4">
<h4 id="examine-most-changed-features">Examine most changed features<a class="anchor" aria-label="anchor" href="#examine-most-changed-features"></a>
</h4>
<p>It can also be helpful to understand what features may have changed
the most. scCustomize provides the function
<code>CellBender_Feature_Diff</code> to determine changes in features.
This will return a data.frame with rowSums for each feature, the
difference in each feature, and the percent change of each feature.</p>
<div class="sourceCode" id="cb15"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">feature_diff</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/CellBender_Feature_Diff.html">CellBender_Feature_Diff</a></span><span class="op">(</span>seurat_object <span class="op">=</span> <span class="va">dual_seurat</span>, raw_assay <span class="op">=</span> <span class="st">"RAW"</span>, cell_bender_assay <span class="op">=</span> <span class="st">"RNA"</span><span class="op">)</span></span></code></pre></div>
<table class="table table table-bordered table-condensed table-responsive table-striped" style="margin-left: auto; margin-right: auto;">
<thead><tr>
<th style="text-align:left;">
</th>
<th style="text-align:right;">
Raw_Counts
</th>
<th style="text-align:right;">
CellBender_Counts
</th>
<th style="text-align:right;">
Count_Diff
</th>
<th style="text-align:right;">
Pct_Diff
</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:left;">
Gm42418
</td>
<td style="text-align:right;">
256834
</td>
<td style="text-align:right;">
3198
</td>
<td style="text-align:right;">
253636
</td>
<td style="text-align:right;">
98.75484
</td>
</tr>
<tr>
<td style="text-align:left;">
Uqcr11
</td>
<td style="text-align:right;">
1315
</td>
<td style="text-align:right;">
20
</td>
<td style="text-align:right;">
1295
</td>
<td style="text-align:right;">
98.47909
</td>
</tr>
<tr>
<td style="text-align:left;">
Gm48099
</td>
<td style="text-align:right;">
277
</td>
<td style="text-align:right;">
5
</td>
<td style="text-align:right;">
272
</td>
<td style="text-align:right;">
98.19495
</td>
</tr>
<tr>
<td style="text-align:left;">
Tmsb4x
</td>
<td style="text-align:right;">
6010
</td>
<td style="text-align:right;">
116
</td>
<td style="text-align:right;">
5894
</td>
<td style="text-align:right;">
98.06988
</td>
</tr>
<tr>
<td style="text-align:left;">
Rps21
</td>
<td style="text-align:right;">
2597
</td>
<td style="text-align:right;">
61
</td>
<td style="text-align:right;">
2536
</td>
<td style="text-align:right;">
97.65114
</td>
</tr>
</tbody>
</table>
</div>
<div class="section level4">
<h4 id="plot-feature-differences">Plot feature differences<a class="anchor" aria-label="anchor" href="#plot-feature-differences"></a>
</h4>
<p>In addition to returning the data.frame it can be useful to visually
examine the changes/trends after running CellBender. The function
<code>CellBender_Diff_Plot</code> takes the data.frame from
<code>CellBender_Feature_Diff</code> as input and plots the results.</p>
<p><img src="Cell_Bender_Functions_files/figure-html/unnamed-chunk-17-1.png" width="768" style="display: block; margin: auto;"></p>
<p><strong>Optional Parameters</strong></p>
<ul>
<li>
<code>pct_diff_threshold</code> plot genes that exhibit a change
equal to or greater than this threshold.<br>
</li>
<li>
<code>num_features</code> instead of plotting genes above a
threshold simply plot the top X changed genes.<br>
</li>
<li>
<code>num_labels</code> change how many genes are labeled.<br>
</li>
<li>
<code>label</code> logical, whether or not to label features.<br>
</li>
<li>
<code>custom_labels</code> specify vector of specific features to
label.</li>
</ul>
<div class="sourceCode" id="cb16"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">p1</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/CellBender_Diff_Plot.html">CellBender_Diff_Plot</a></span><span class="op">(</span>feature_diff_df <span class="op">=</span> <span class="va">feature_diff</span><span class="op">)</span></span>
<span><span class="va">p2</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/CellBender_Diff_Plot.html">CellBender_Diff_Plot</a></span><span class="op">(</span>feature_diff_df <span class="op">=</span> <span class="va">feature_diff</span>, pct_diff_threshold <span class="op">=</span> <span class="fl">50</span><span class="op">)</span></span>
<span><span class="va">p3</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/CellBender_Diff_Plot.html">CellBender_Diff_Plot</a></span><span class="op">(</span>feature_diff_df <span class="op">=</span> <span class="va">feature_diff</span>, num_features <span class="op">=</span> <span class="fl">500</span>, pct_diff_threshold <span class="op">=</span> <span class="cn">NULL</span><span class="op">)</span></span>
<span><span class="va">p4</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/CellBender_Diff_Plot.html">CellBender_Diff_Plot</a></span><span class="op">(</span>feature_diff_df <span class="op">=</span> <span class="va">feature_diff</span>, num_labels <span class="op">=</span> <span class="fl">10</span><span class="op">)</span></span>
<span><span class="va">p5</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/CellBender_Diff_Plot.html">CellBender_Diff_Plot</a></span><span class="op">(</span>feature_diff_df <span class="op">=</span> <span class="va">feature_diff</span>, label <span class="op">=</span> <span class="cn">F</span><span class="op">)</span></span>
<span><span class="va">p6</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/CellBender_Diff_Plot.html">CellBender_Diff_Plot</a></span><span class="op">(</span>feature_diff_df <span class="op">=</span> <span class="va">feature_diff</span>, custom_labels <span class="op">=</span> <span class="st">"Gm48099"</span><span class="op">)</span></span>
<span></span>
<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">p1</span>, <span class="va">p2</span>, <span class="va">p3</span>, <span class="va">p4</span>, <span class="va">p5</span>, <span class="va">p6</span>, ncol <span class="op">=</span> <span class="fl">2</span><span class="op">)</span></span></code></pre></div>
<p><img src="Cell_Bender_Functions_files/figure-html/unnamed-chunk-19-1.png" width="1440" style="display: block; margin: auto;"></p>
</div>
</div>
<div class="section level3">
<h3 id="dual-assay-plotting">Dual Assay Plotting<a class="anchor" aria-label="anchor" href="#dual-assay-plotting"></a>
</h3>
<p>For Cell Bender especially, but also potentially for other assays as
well, it can be helpful during analysis to plot the corrected and
uncorrected counts for given feature. scCustomize contains function
<code><a href="../reference/FeaturePlot_DualAssay.html">FeaturePlot_DualAssay()</a></code> to make easy.</p>
<p>Users just need to supply the names of the two assays to plot and the
features. <em>NOTE: Make sure both assays have been normalized before
plotting. The function will attempt to check and make sure both assays
have been normalized but has not been tested in all scenarios.</em></p>
<div class="section level4">
<h4 id="example-plotting">Example Plotting<a class="anchor" aria-label="anchor" href="#example-plotting"></a>
</h4>
<p>For this example I’m using unpublished single nucleus RNA dataset
from mouse cortex and have subsetted the astrocytes.</p>
<div class="section level5">
<h5 id="ambient-rna-gene">Ambient RNA gene<a class="anchor" aria-label="anchor" href="#ambient-rna-gene"></a>
</h5>
<p>First let’s plot gene that represents ambient RNA as it’s restricted
in expression to neurons (synaptic gene). If Cell Bender has worked well
we expect that expression of this gene will be very different between
the two assays.
<img src="Cell_Bender_Functions_files/figure-html/unnamed-chunk-20-1.png" width="960" style="display: block; margin: auto;"></p>
</div>
<div class="section level5">
<h5 id="non-ambient-rna-gene">Non-Ambient RNA gene<a class="anchor" aria-label="anchor" href="#non-ambient-rna-gene"></a>
</h5>
<p>Now let’s plot normally astrocyte restricted gene. If Cell Bender has
worked well we expect that expression of this gene shouldn’t be very
different between the two assays.
<img src="Cell_Bender_Functions_files/figure-html/unnamed-chunk-21-1.png" width="960" style="display: block; margin: auto;"></p>
</div>
</div>
</div>
</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>



      <footer><div class="copyright">
  <p></p>
<p>Developed by Samuel Marsh.</p>
</div>

<div class="pkgdown">
  <p></p>
<p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.0.7.</p>
</div>

      </footer>
</div>

  


  

  </body>
</html>
