BiocNeighbors 1.13.0

The *BiocNeighbors* package implements a few algorithms for exact nearest neighbor searching:

- The k-means for k-nearest neighbors (KMKNN) algorithm (Wang 2012) uses k-means clustering to create an index. Within each cluster, the distance of each of that cluster’s points to the cluster center are computed and used to sort all points. Given a query point, the distance to each cluster center is determined and the triangle inequality is applied to determine which points in each cluster warrant a full distance calculation.
- The vantage point (VP) tree algorithm (Yianilos 1993) involves constructing a tree where each node is located at a data point and is associated with a subset of neighboring points. Each node progressively partitions points into two subsets that are either closer or further to the node than a given threshold. Given a query point, the triangle inequality is applied at each node in the tree to determine if the child nodes warrant searching.
- The exhaustive search is a simple brute-force algorithm that computes distances to between all data and query points. This has the worst computational complexity but can actually be faster than the other exact algorithms in situations where indexing provides little benefit, e.g., data sets with few points and/or a very large number of dimensions.

Both KMKNN and VP-trees involve a component of randomness during index construction, though the k-nearest neighbors result is fully deterministic1 Except in the presence of ties, see `?"BiocNeighbors-ties"`

for details..

The most obvious application is to perform a k-nearest neighbors search. We’ll mock up an example here with a hypercube of points, for which we want to identify the 10 nearest neighbors for each point.

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

The `findKNN()`

method expects a numeric matrix as input with data points as the rows and variables/dimensions as the columns.
We indicate that we want to use the KMKNN algorithm by setting `BNPARAM=KmknnParam()`

(which is also the default, so this is not strictly necessary here).
We could use a VP tree instead by setting `BNPARAM=VptreeParam()`

.

```
fout <- findKNN(data, k=10, BNPARAM=KmknnParam())
head(fout$index)
```

```
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1935 2356 2362 57 8076 6373 5699 2483 9504 9040
## [2,] 9789 1782 9921 1314 4105 9344 9640 9096 5722 7435
## [3,] 2166 3428 1158 2401 9600 1977 2356 5826 3971 3140
## [4,] 7713 915 5590 1773 2314 2003 8328 5043 5424 2663
## [5,] 8997 3311 1852 4290 9367 5674 9882 4370 5598 4785
## [6,] 4614 9224 4163 6303 9040 2594 2314 6288 5133 706
```

`head(fout$distance)`

```
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.9095515 0.9113790 0.9183992 0.9648601 0.9725504 0.9770221 0.9804174
## [2,] 0.8808309 1.0099397 1.0236221 1.0281230 1.0290028 1.0438050 1.0465617
## [3,] 0.8330738 1.0292092 1.0337894 1.0443245 1.0453908 1.0485869 1.0514509
## [4,] 1.0081959 1.0588291 1.0667738 1.0885254 1.0887729 1.1135694 1.1175354
## [5,] 0.9006528 0.9119734 0.9333579 0.9358952 0.9677931 0.9791606 0.9905248
## [6,] 0.8677079 1.0352460 1.0541254 1.0677597 1.0713551 1.0728613 1.0746965
## [,8] [,9] [,10]
## [1,] 0.9971581 1.009139 1.010270
## [2,] 1.0677903 1.073756 1.075368
## [3,] 1.0534295 1.080317 1.080381
## [4,] 1.1239404 1.141637 1.159081
## [5,] 1.0121908 1.013311 1.013700
## [6,] 1.0756643 1.077918 1.086240
```

Each row of the `index`

matrix corresponds to a point in `data`

and contains the row indices in `data`

that are its nearest neighbors.
For example, the 3rd point in `data`

has the following nearest neighbors:

`fout$index[3,]`

`## [1] 2166 3428 1158 2401 9600 1977 2356 5826 3971 3140`

… with the following distances to those neighbors:

`fout$distance[3,]`

```
## [1] 0.8330738 1.0292092 1.0337894 1.0443245 1.0453908 1.0485869 1.0514509
## [8] 1.0534295 1.0803172 1.0803810
```

Note that the reported neighbors are sorted by distance.

Another application is to identify the k-nearest neighbors in one dataset based on query points in another dataset. Again, we mock up a small data set:

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

We then use the `queryKNN()`

function to identify the 5 nearest neighbors in `data`

for each point in `query`

.

```
qout <- queryKNN(data, query, k=5, BNPARAM=KmknnParam())
head(qout$index)
```

```
## [,1] [,2] [,3] [,4] [,5]
## [1,] 9532 5741 6032 3592 1218
## [2,] 578 9667 4461 3273 5279
## [3,] 5562 3333 1980 6898 2117
## [4,] 2093 7215 670 3094 3414
## [5,] 1521 6832 7785 1833 3370
## [6,] 7410 6967 3505 6095 2710
```

`head(qout$distance)`

```
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8899789 0.9336598 1.0216367 1.0930681 1.1071916
## [2,] 0.8409194 0.8982352 0.9818410 0.9975233 1.0060628
## [3,] 0.6049765 0.8877391 0.9093684 0.9267864 0.9368828
## [4,] 0.8781357 0.8907984 0.9068256 0.9382745 0.9781155
## [5,] 0.8517183 0.9315949 0.9406789 0.9647713 0.9707514
## [6,] 0.7912862 0.8507538 0.8785286 0.8936067 0.9549804
```

Each row of the `index`

matrix contains the row indices in `data`

that are the nearest neighbors of a point in `query`

.
For example, the 3rd point in `query`

has the following nearest neighbors in `data`

:

`qout$index[3,]`

`## [1] 5562 3333 1980 6898 2117`

… with the following distances to those neighbors:

`qout$distance[3,]`

`## [1] 0.6049765 0.8877391 0.9093684 0.9267864 0.9368828`

Again, the reported neighbors are sorted by distance.

Users can perform the search for a subset of query points using the `subset=`

argument.
This yields the same result as but is more efficient than performing the search for all points and subsetting the output.

`findKNN(data, k=5, subset=3:5)`

```
## $index
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2166 3428 1158 2401 9600
## [2,] 7713 915 5590 1773 2314
## [3,] 8997 3311 1852 4290 9367
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8330738 1.0292092 1.0337894 1.0443245 1.0453908
## [2,] 1.0081959 1.0588291 1.0667738 1.0885254 1.0887729
## [3,] 0.9006528 0.9119734 0.9333579 0.9358952 0.9677931
```

If only the indices are of interest, users can set `get.distance=FALSE`

to avoid returning the matrix of distances.
This will save some time and memory.

`names(findKNN(data, k=2, get.distance=FALSE))`

`## [1] "index"`

It is also simple to speed up functions by parallelizing the calculations with the *BiocParallel* framework.

```
library(BiocParallel)
out <- findKNN(data, k=10, BPPARAM=MulticoreParam(3))
```

For multiple queries to a constant `data`

, the pre-clustering can be performed in a separate step with `buildIndex()`

.
The result can then be passed to multiple calls, avoiding the overhead of repeated clustering2 The algorithm type is automatically determined when `BNINDEX`

is specified, so there is no need to also specify `BNPARAM`

in the later functions..

```
pre <- buildIndex(data, BNPARAM=KmknnParam())
out1 <- findKNN(BNINDEX=pre, k=5)
out2 <- queryKNN(BNINDEX=pre, query=query, k=2)
```

The default setting is to search on the Euclidean distance.
Alternatively, we can use the Manhattan distance by setting `distance="Manhattan"`

in the `BiocNeighborParam`

object.

`out.m <- findKNN(data, k=5, BNPARAM=KmknnParam(distance="Manhattan"))`

Advanced users may also be interested in the `raw.index=`

argument, which returns indices directly to the precomputed object rather than to `data`

.
This may be useful inside package functions where it may be more convenient to work on a common precomputed object.

`sessionInfo()`

```
## R Under development (unstable) (2021-10-19 r81077)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.3 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.15-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.15-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.29.0 BiocNeighbors_1.13.0 knitr_1.36
## [4] BiocStyle_2.23.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.7 magrittr_2.0.1 BiocGenerics_0.41.0
## [4] lattice_0.20-45 R6_2.5.1 rlang_0.4.12
## [7] fastmap_1.1.0 stringr_1.4.0 tools_4.2.0
## [10] parallel_4.2.0 grid_4.2.0 xfun_0.27
## [13] jquerylib_0.1.4 htmltools_0.5.2 yaml_2.2.1
## [16] digest_0.6.28 bookdown_0.24 Matrix_1.3-4
## [19] BiocManager_1.30.16 S4Vectors_0.33.0 sass_0.4.0
## [22] evaluate_0.14 rmarkdown_2.11 stringi_1.7.5
## [25] compiler_4.2.0 bslib_0.3.1 stats4_4.2.0
## [28] jsonlite_1.7.2
```

Wang, X. 2012. “A Fast Exact k-Nearest Neighbors Algorithm for High Dimensional Search Using k-Means Clustering and Triangle Inequality.” *Proc Int Jt Conf Neural Netw* 43 (6): 2351–8.

Yianilos, P. N. 1993. “Data Structures and Algorithms for Nearest Neighbor Search in General Metric Spaces.” In *SODA*, 93:311–21. 194.