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.

  k = 10,
  d = 50,
  transposed = FALSE,
  get.distance = FALSE,
  reduced.dim = "PCA",
  BNPARAM = KmknnParam(),
  BSPARAM = bsparam(),
  BPPARAM = SerialParam()



A matrix, SingleCellExperiment or Milo object containing feature X cell gene expression data.


An integer scalar that specifies the number of nearest-neighbours to consider for the graph building.


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.


Logical if the input x is transposed with rows as cells.


A logical scalar whether to compute distances during graph construction.


A character scalar that refers to a specific entry in the reduceDim slot of the Milo object.


refer to buildKNNGraph for details.


refer to buildKNNGraph for details.


refer to buildKNNGraph for details.


A Milo object with the graph and distance slots populated.


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 directed=TRUE.

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 Milo object.


library(SingleCellExperiment) ux <- matrix(rpois(12000, 5), ncol=200) vx <- log2(ux + 1) pca <- prcomp(t(vx)) sce <- SingleCellExperiment(assays=list(counts=ux, logcounts=vx), reducedDims=SimpleList(PCA=pca$x)) milo <- Milo(sce) milo <- buildGraph(milo, d=30, transposed=TRUE)
#> Constructing kNN graph with k:10
#> class: Milo #> dim: 60 200 #> metadata(0): #> assays(2): counts logcounts #> rownames: NULL #> rowData names(0): #> colnames: NULL #> colData names(0): #> reducedDimNames(1): PCA #> spikeNames(0): #> altExpNames(0): #> nhoods dimensions(1): 0 #> nhoodCounts dimensions(2): 1 1 #> nhoodDistances dimension(1): 0 #> graph names(1): graph #> nhoodIndex names(1): 0 #> nhoodExpression dimension(2): 1 1 #> nhoodReducedDim names(0): #> nhoodGraph names(0): #> nhoodAdjacency dimension(0):