# Detecting all neighbors within range

Aaron Lun1

1Cancer Research UK Cambridge Institute, Cambridge, United Kingdom

#### Revised: 28 September 2018

#### Package

BiocNeighbors 1.5.1

# 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 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]  711 7423    1 8216 2707 5484 5933 9142 4155 7829 1354 6996 9021  933
    ##
    ## [[2]]
    ## [1] 2232 3576    2 4834
    ##
    ## [[3]]
    ## [1] 6292 5320 7778 1289    3 7107 4406 7837
    ##
    ## [[4]]
    ## [1] 8327    4
    ##
    ## [[5]]
    ## [1] 4289 7552 6093 5288    5
    ##
    ## [[6]]
    ##  [1] 7404    6  121 1438 2080 1467 9904 5979 8815 6218  404 9366

    head(fout$distance)

    ## [[1]]
    ##  [1] 0.9838265 0.9869738 0.0000000 0.9405493 0.8941410 0.9368004 0.9798719
    ##  [8] 0.9662484 0.9101414 0.9026346 0.8078189 0.9695158 0.9125880 0.9380882
    ##
    ## [[2]]
    ## [1] 0.9970904 0.8694451 0.0000000 0.9789182
    ##
    ## [[3]]
    ## [1] 0.9894113 0.9700598 0.9296143 0.9689030 0.0000000 0.9789739 0.8699015
    ## [8] 0.8887366
    ##
    ## [[4]]
    ## [1] 0.8254642 0.0000000
    ##
    ## [[5]]
    ## [1] 0.9464944 0.9510440 0.9111727 0.8701098 0.0000000
    ##
    ## [[6]]
    ##  [1] 0.9076229 0.0000000 0.9145881 0.9867155 0.8704935 0.8975491 0.9446186
    ##  [8] 0.9844137 0.9836144 0.9560209 0.9006277 0.9829508

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] 6292 5320 7778 1289    3 7107 4406 7837

… with the following distances to those neighbors:

    fout$distance[[3]]

    ## [1] 0.9894113 0.9700598 0.9296143 0.9689030 0.0000000 0.9789739 0.8699015
    ## [8] 0.8887366

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 Under development (unstable) (2019-11-03 r77362)
    ## Platform: x86_64-apple-darwin15.6.0 (64-bit)
    ## Running under: OS X El Capitan 10.11.6
    ##
    ## Matrix products: default
    ## BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.0.dylib
    ## LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
    ##
    ## locale:
    ## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    ##
    ## attached base packages:
    ## [1] stats     graphics  grDevices utils     datasets  methods   base
    ##
    ## other attached packages:
    ## [1] BiocParallel_1.21.0 BiocNeighbors_1.5.1 knitr_1.25
    ## [4] BiocStyle_2.15.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] Rcpp_1.0.2          bookdown_0.14       lattice_0.20-38
    ##  [4] digest_0.6.22       grid_4.0.0          stats4_4.0.0
    ##  [7] magrittr_1.5        evaluate_0.14       rlang_0.4.1
    ## [10] stringi_1.4.3       S4Vectors_0.25.0    Matrix_1.2-17
    ## [13] rmarkdown_1.16      tools_4.0.0         stringr_1.4.0
    ## [16] parallel_4.0.0      xfun_0.10           yaml_2.2.0
    ## [19] compiler_4.0.0      BiocGenerics_0.33.0 BiocManager_1.30.9
    ## [22] htmltools_0.4.0