# Detecting all neighbors within range

Aaron Lun1

1Cancer Research UK Cambridge Institute, Cambridge, United Kingdom

#### Revised: 28 September 2018

#### Package

BiocNeighbors 1.8.0

# 1 Identifying all neighbors within range

Another application of the KMKNN or VP tree algorithms is to identify all neighboring points within a certain distance11 1 The default here is Euclidean, but again, we can set `distance="Manhattan"` in the `BNPARAM` object if so desired. of the current point. We first mock up some data:

    nobs <- 10000
    ndim <- 20
    data <- matrix(runif(nobs*ndim), ncol=ndim)

We apply the `findNeighbors()` function to `data`:

    fout <- findNeighbors(data, threshold=1)
    head(fout$index)

    ## [[1]]
    ## [1] 5291 8613    1 1660
    ##
    ## [[2]]
    ##  [1] 9460    2 1643 8863 1214 3331 9838 8900 9113 7972
    ##
    ## [[3]]
    ##  [1] 9565 3978  447  337    3 7375  362 2067  399 2478
    ##
    ## [[4]]
    ## [1] 7825    4
    ##
    ## [[5]]
    ##  [1] 9702 2146  697 7415    5 5755 7852 1680 9945 5586 9335 1064 3707
    ##
    ## [[6]]
    ## [1]  341 8082 9458 1772 4863  196 8912    6

    head(fout$distance)

    ## [[1]]
    ## [1] 0.9308363 0.9412722 0.0000000 0.9632592
    ##
    ## [[2]]
    ##  [1] 0.9871524 0.0000000 0.9795025 0.9714413 0.8111108 0.9010951 0.9261032
    ##  [8] 0.9772216 0.9130513 0.9953502
    ##
    ## [[3]]
    ##  [1] 0.8852746 0.9432349 0.9792330 0.8797598 0.0000000 0.9371186 0.8575601
    ##  [8] 0.9610529 0.9819835 0.9525417
    ##
    ## [[4]]
    ## [1] 0.9990103 0.0000000
    ##
    ## [[5]]
    ##  [1] 0.9883951 0.9710681 0.9731129 0.9381541 0.0000000 0.9833067 0.8854986
    ##  [8] 0.8638151 0.9886198 0.9846417 0.9387662 0.7971140 0.9369964
    ##
    ## [[6]]
    ## [1] 0.9762256 0.9414490 0.9840734 0.9340114 0.9578628 0.9384782 0.8607713
    ## [8] 0.0000000

Each entry of the `index` list corresponds to a point in `data` and contains the row indices in `data` that are within `threshold`. For example, the 3rd point in `data` has the following neighbors:

    fout$index[[3]]

    ##  [1] 9565 3978  447  337    3 7375  362 2067  399 2478

… with the following distances to those neighbors:

    fout$distance[[3]]

    ##  [1] 0.8852746 0.9432349 0.9792330 0.8797598 0.0000000 0.9371186 0.8575601
    ##  [8] 0.9610529 0.9819835 0.9525417

Note that, for this function, the reported neighbors are not sorted by distance. The order of the output is completely arbitrary and will vary depending on the random seed. However, the identity of the neighbors is fully deterministic.

# 2 Querying another data set for neighbors

The `queryNeighbors()` function is also provided for identifying all points within a certain distance of a query point. Given a query data set:

    nquery <- 1000
    ndim <- 20
    query <- matrix(runif(nquery*ndim), ncol=ndim)

… we apply the `queryNeighbors()` function:

    qout <- queryNeighbors(data, query, threshold=1)
    length(qout$index)

    ## [1] 1000

… where each entry of `qout$index` corresponds to a row of `query` and contains its neighbors in `data`. Again, the order of the output is arbitrary but the identity of the neighbors is deterministic.

# 3 Further options

Most of the options described for `findKNN()` are also applicable here. For example:

- `subset` to identify neighbors for a subset of points.
- `get.distance` to avoid retrieving distances when unnecessary.
- `BPPARAM` to parallelize the calculations across multiple workers.
- `raw.index` to return the raw indices from a precomputed index.

Note that the argument for a precomputed index is `precomputed`:

    pre <- buildIndex(data, BNPARAM=KmknnParam())
    fout.pre <- findNeighbors(BNINDEX=pre, threshold=1)
    qout.pre <- queryNeighbors(BNINDEX=pre, query=query, threshold=1)

Users are referred to the documentation of each function for specific details.

# 4 Session information

    sessionInfo()

    ## R version 4.0.3 (2020-10-10)
    ## Platform: x86_64-pc-linux-gnu (64-bit)
    ## Running under: Ubuntu 18.04.5 LTS
    ##
    ## Matrix products: default
    ## BLAS:   /home/biocbuild/bbs-3.12-bioc/R/lib/libRblas.so
    ## LAPACK: /home/biocbuild/bbs-3.12-bioc/R/lib/libRlapack.so
    ##
    ## locale:
    ##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
    ##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C
    ##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
    ##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
    ##  [9] LC_ADDRESS=C               LC_TELEPHONE=C
    ## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
    ##
    ## attached base packages:
    ## [1] stats     graphics  grDevices utils     datasets  methods   base
    ##
    ## other attached packages:
    ## [1] BiocParallel_1.24.0 BiocNeighbors_1.8.0 knitr_1.30
    ## [4] BiocStyle_2.18.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] Rcpp_1.0.5          bookdown_0.21       lattice_0.20-41
    ##  [4] digest_0.6.27       grid_4.0.3          stats4_4.0.3
    ##  [7] magrittr_1.5        evaluate_0.14       rlang_0.4.8
    ## [10] stringi_1.5.3       S4Vectors_0.28.0    Matrix_1.2-18
    ## [13] rmarkdown_2.5       tools_4.0.3         stringr_1.4.0
    ## [16] parallel_4.0.3      xfun_0.18           yaml_2.2.1
    ## [19] compiler_4.0.3      BiocGenerics_0.36.0 BiocManager_1.30.10
    ## [22] htmltools_0.5.0