<!-- Generated by pkgdown: do not edit by hand -->
<!DOCTYPE html>
<html lang="en">
  <head>
  <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>Build a k-nearest neighbour graph — buildGraph • miloR</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 rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" 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="Build a k-nearest neighbour graph — buildGraph" />
<meta property="og:description" content="This function is borrowed from the old buildKNNGraph function in scran.
Instead of returning an igraph object it populates the graph and distance
slots in a Milo object. If the input is a SingleCellExperiment object or
a matrix then it will return a de novo Milo object with the same slots
filled." />




<!-- 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-reference-topic">
      <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">miloR</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Released version">0.1.0</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li>
  <a href="../index.html">
    <span class="fas fa fas fa-home fa-lg"></span>
     
  </a>
</li>
<li>
  <a href="../reference/index.html">Reference</a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
    Articles
     
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu">
    <li>
      <a href="../articles/milo_demo.html">Differential abundance testing with Milo</a>
    </li>
    <li>
      <a href="../articles/milo_gastrulation.html">Differential abundance testing with Milo - Mouse gastrulation example</a>
    </li>
  </ul>
</li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        
      </ul>
      
    </div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->

      

      </header>

<div class="row">
  <div class="col-md-9 contents">
    <div class="page-header">
    <h1>Build a k-nearest neighbour graph</h1>
    
    <div class="hidden name"><code>buildGraph.Rd</code></div>
    </div>

    <div class="ref-description">
    <p>This function is borrowed from the old buildKNNGraph function in scran.
Instead of returning an igraph object it populates the graph and distance
slots in a Milo object. If the input is a SingleCellExperiment object or
a matrix then it will return a de novo Milo object with the same slots
filled.</p>
    </div>

    <pre class="usage"><span class='fu'>buildGraph</span>(
  <span class='no'>x</span>,
  <span class='kw'>k</span> <span class='kw'>=</span> <span class='fl'>10</span>,
  <span class='kw'>d</span> <span class='kw'>=</span> <span class='fl'>50</span>,
  <span class='kw'>transposed</span> <span class='kw'>=</span> <span class='fl'>FALSE</span>,
  <span class='kw'>get.distance</span> <span class='kw'>=</span> <span class='fl'>FALSE</span>,
  <span class='kw'>reduced.dim</span> <span class='kw'>=</span> <span class='st'>"PCA"</span>,
  <span class='kw'>BNPARAM</span> <span class='kw'>=</span> <span class='fu'>KmknnParam</span>(),
  <span class='kw'>BSPARAM</span> <span class='kw'>=</span> <span class='fu'>bsparam</span>(),
  <span class='kw'>BPPARAM</span> <span class='kw'>=</span> <span class='fu'>SerialParam</span>()
)</pre>

    <h2 class="hasAnchor" id="arguments"><a class="anchor" href="#arguments"></a>Arguments</h2>
    <table class="ref-arguments">
    <colgroup><col class="name" /><col class="desc" /></colgroup>
    <tr>
      <th>x</th>
      <td><p>A matrix, <code>SingleCellExperiment</code> or Milo object
containing feature X cell gene expression data.</p></td>
    </tr>
    <tr>
      <th>k</th>
      <td><p>An integer scalar that specifies the number of nearest-neighbours
to consider for the graph building.</p></td>
    </tr>
    <tr>
      <th>d</th>
      <td><p>The number of dimensions to use if the input is a matrix of cells
X reduced dimensions. If this is provided, transposed should also be
set=TRUE.</p></td>
    </tr>
    <tr>
      <th>transposed</th>
      <td><p>Logical if the input x is transposed with rows as cells.</p></td>
    </tr>
    <tr>
      <th>get.distance</th>
      <td><p>A logical scalar whether to compute distances during graph
construction.</p></td>
    </tr>
    <tr>
      <th>reduced.dim</th>
      <td><p>A character scalar that refers to a specific entry in
the <code>reduceDim</code> slot of the <code><a href='Milo.html'>Milo</a></code> object.</p></td>
    </tr>
    <tr>
      <th>BNPARAM</th>
      <td><p>refer to <code><a href='https://rdrr.io/pkg/scran/man/buildSNNGraph.html'>buildKNNGraph</a></code> for details.</p></td>
    </tr>
    <tr>
      <th>BSPARAM</th>
      <td><p>refer to <code><a href='https://rdrr.io/pkg/scran/man/buildSNNGraph.html'>buildKNNGraph</a></code> for details.</p></td>
    </tr>
    <tr>
      <th>BPPARAM</th>
      <td><p>refer to <code><a href='https://rdrr.io/pkg/scran/man/buildSNNGraph.html'>buildKNNGraph</a></code> for details.</p></td>
    </tr>
    </table>

    <h2 class="hasAnchor" id="value"><a class="anchor" href="#value"></a>Value</h2>

    <p>A <code><a href='Milo.html'>Milo</a></code> object with the graph and distance slots populated.</p>
    <h2 class="hasAnchor" id="details"><a class="anchor" href="#details"></a>Details</h2>

    <p>This function computes a k-nearest neighbour graph. Each graph vertex is a
single-cell connected by the edges between its neighbours. Whilst a
kNN-graph is strictly directed, we remove directionality by forcing all
edge weights to 1; this behaviour can be overriden by providing
<code>directed=TRUE</code>.</p>
<p>If you wish to use an
alternative graph structure, such as a shared-NN graph I recommend you
construct this separately and add to the relevant slot in the
<code><a href='Milo.html'>Milo</a></code> object.</p>

    <h2 class="hasAnchor" id="examples"><a class="anchor" href="#examples"></a>Examples</h2>
    <pre class="examples"><div class='input'><span class='fu'><a href='https://rdrr.io/r/base/library.html'>library</a></span>(<span class='no'>SingleCellExperiment</span>)
<span class='no'>ux</span> <span class='kw'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/base/matrix.html'>matrix</a></span>(<span class='fu'><a href='https://rdrr.io/r/stats/Poisson.html'>rpois</a></span>(<span class='fl'>12000</span>, <span class='fl'>5</span>), <span class='kw'>ncol</span><span class='kw'>=</span><span class='fl'>200</span>)
<span class='no'>vx</span> <span class='kw'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/base/Log.html'>log2</a></span>(<span class='no'>ux</span> + <span class='fl'>1</span>)
<span class='no'>pca</span> <span class='kw'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/stats/prcomp.html'>prcomp</a></span>(<span class='fu'><a href='https://rdrr.io/r/base/t.html'>t</a></span>(<span class='no'>vx</span>))

<span class='no'>sce</span> <span class='kw'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/pkg/SingleCellExperiment/man/SingleCellExperiment.html'>SingleCellExperiment</a></span>(<span class='kw'>assays</span><span class='kw'>=</span><span class='fu'><a href='https://rdrr.io/r/base/list.html'>list</a></span>(<span class='kw'>counts</span><span class='kw'>=</span><span class='no'>ux</span>, <span class='kw'>logcounts</span><span class='kw'>=</span><span class='no'>vx</span>),
                            <span class='kw'>reducedDims</span><span class='kw'>=</span><span class='fu'>SimpleList</span>(<span class='kw'>PCA</span><span class='kw'>=</span><span class='no'>pca</span>$<span class='no'>x</span>))

<span class='no'>milo</span> <span class='kw'>&lt;-</span> <span class='fu'><a href='Milo.html'>Milo</a></span>(<span class='no'>sce</span>)
<span class='no'>milo</span> <span class='kw'>&lt;-</span> <span class='fu'>buildGraph</span>(<span class='no'>milo</span>, <span class='kw'>d</span><span class='kw'>=</span><span class='fl'>30</span>, <span class='kw'>transposed</span><span class='kw'>=</span><span class='fl'>TRUE</span>)</div><div class='output co'>#&gt; <span class='message'>Constructing kNN graph with k:10</span></div><div class='input'>
<span class='no'>milo</span></div><div class='output co'>#&gt; class: Milo 
#&gt; dim: 60 200 
#&gt; metadata(0):
#&gt; assays(2): counts logcounts
#&gt; rownames: NULL
#&gt; rowData names(0):
#&gt; colnames: NULL
#&gt; colData names(0):
#&gt; reducedDimNames(1): PCA
#&gt; spikeNames(0):
#&gt; altExpNames(0):
#&gt; nhoods dimensions(1): 0
#&gt; nhoodCounts dimensions(2): 1 1
#&gt; nhoodDistances dimension(1): 0
#&gt; graph names(1): graph
#&gt; nhoodIndex names(1): 0
#&gt; nhoodExpression dimension(2): 1 1
#&gt; nhoodReducedDim names(0):
#&gt; nhoodGraph names(0):
#&gt; nhoodAdjacency dimension(0):</div></pre>
  </div>
  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
    <nav id="toc" data-toggle="toc" class="sticky-top">
      <h2 data-toc-skip>Contents</h2>
    </nav>
  </div>
</div>


      <footer>
      <div class="copyright">
  <p>Developed by Mike Morgan, Emma Dann.</p>
</div>

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

      </footer>
   </div>

  


  </body>
</html>


