# Detecting all neighbors within range

Aaron Lun1

1Cancer Research UK Cambridge Institute, Cambridge, United Kingdom

#### Revised: 28 September 2018

#### Package

BiocNeighbors 1.16.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 distance1 1 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]    3 7303 8210 5693    1 2813 9885 6949 7981 7427
    ##
    ## [[2]]
    ## [1] 7823 8187    2 4646 5844 9500
    ##
    ## [[3]]
    ##  [1] 7958    3 2571 7246 8579 7303 9512 8357 4455 5614 6510 7854 4670 2346 4750
    ## [16] 5985    1 4408 6949 7471 7427 7888
    ##
    ## [[4]]
    ##  [1] 9205    4 7730 3564 4954 3807  788 3906 6434 6467 1316
    ##
    ## [[5]]
    ## [1] 9865    5 9920
    ##
    ## [[6]]
    ## [1] 3224 6703 6479    6 1500 2402 4847 5078

    head(fout$distance)

    ## [[1]]
    ##  [1] 0.9108644 0.9617366 0.9968087 0.9118757 0.0000000 0.9694526 0.9655727
    ##  [8] 0.9738352 0.9356868 0.8028429
    ##
    ## [[2]]
    ## [1] 0.9841614 0.9075798 0.0000000 0.9833485 0.9875270 0.9853291
    ##
    ## [[3]]
    ##  [1] 0.9804724 0.0000000 0.9545653 0.9800560 0.9833224 0.9873952 0.9568120
    ##  [8] 0.9448316 0.8739030 0.9574421 0.9633749 0.9632738 0.9349043 0.9932143
    ## [15] 0.9161523 0.9253871 0.9108644 0.9564172 0.9725381 0.9900969 0.9598312
    ## [22] 0.9082073
    ##
    ## [[4]]
    ##  [1] 0.9535167 0.0000000 0.7280785 0.9664667 0.9438754 0.9970728 0.9949827
    ##  [8] 0.9495220 0.9977621 0.9753064 0.9684954
    ##
    ## [[5]]
    ## [1] 0.9095580 0.0000000 0.9916518
    ##
    ## [[6]]
    ## [1] 0.9694973 0.9623232 0.9855303 0.0000000 0.8709202 0.9256503 0.9942111
    ## [8] 0.8875772

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] 7958    3 2571 7246 8579 7303 9512 8357 4455 5614 6510 7854 4670 2346 4750
    ## [16] 5985    1 4408 6949 7471 7427 7888

… with the following distances to those neighbors:

    fout$distance[[3]]

    ##  [1] 0.9804724 0.0000000 0.9545653 0.9800560 0.9833224 0.9873952 0.9568120
    ##  [8] 0.9448316 0.8739030 0.9574421 0.9633749 0.9632738 0.9349043 0.9932143
    ## [15] 0.9161523 0.9253871 0.9108644 0.9564172 0.9725381 0.9900969 0.9598312
    ## [22] 0.9082073

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.2.1 (2022-06-23)
    ## Platform: x86_64-pc-linux-gnu (64-bit)
    ## Running under: Ubuntu 20.04.5 LTS
    ##
    ## Matrix products: default
    ## BLAS:   /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
    ## LAPACK: /home/biocbuild/bbs-3.16-bioc/R/lib/libRlapack.so
    ##
    ## locale:
    ##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
    ##  [3] LC_TIME=en_GB              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.32.0  BiocNeighbors_1.16.0 knitr_1.40
    ## [4] BiocStyle_2.26.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] Rcpp_1.0.9          magrittr_2.0.3      BiocGenerics_0.44.0
    ##  [4] lattice_0.20-45     R6_2.5.1            rlang_1.0.6
    ##  [7] fastmap_1.1.0       stringr_1.4.1       tools_4.2.1
    ## [10] parallel_4.2.1      grid_4.2.1          xfun_0.34
    ## [13] cli_3.4.1           jquerylib_0.1.4     htmltools_0.5.3
    ## [16] yaml_2.3.6          digest_0.6.30       bookdown_0.29
    ## [19] Matrix_1.5-1        BiocManager_1.30.19 S4Vectors_0.36.0
    ## [22] sass_0.4.2          codetools_0.2-18    cachem_1.0.6
    ## [25] evaluate_0.17       rmarkdown_2.17      stringi_1.7.8
    ## [28] compiler_4.2.1      bslib_0.4.0         stats4_4.2.1
    ## [31] jsonlite_1.8.3