BiocNeighbors 1.10.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,] 7389 1450 2164 9211 7194 4443 9111 803 3126 4575
## [2,] 160 546 9618 1800 3617 1006 834 7920 3807 1834
## [3,] 5454 529 233 1240 6736 9751 4592 4785 6988 2221
## [4,] 489 8267 3749 2533 3593 5144 7229 9924 8080 3132
## [5,] 9370 8464 5536 6041 4117 8928 6982 3942 7181 9119
## [6,] 6173 2112 6177 5850 3322 8184 64 1704 1265 6995
```

`head(fout$distance)`

```
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 1.0191862 1.0287630 1.0781962 1.0882660 1.1263916 1.1276476 1.1486515
## [2,] 0.8373512 0.9119985 0.9125318 0.9232977 0.9310514 0.9336446 0.9569254
## [3,] 0.8630420 0.9878666 1.0299327 1.0332656 1.0421341 1.0990158 1.1030762
## [4,] 0.9630268 1.0121882 1.0215013 1.0874147 1.1002334 1.1085028 1.1131320
## [5,] 0.9092166 0.9607478 0.9669230 0.9706720 1.0064631 1.0389461 1.0681980
## [6,] 1.0057431 1.0535690 1.0613942 1.0650245 1.0650918 1.0792601 1.0903098
## [,8] [,9] [,10]
## [1,] 1.149139 1.1560334 1.1646049
## [2,] 0.958047 0.9643782 0.9722215
## [3,] 1.113153 1.1140538 1.1651180
## [4,] 1.119254 1.1240496 1.1353650
## [5,] 1.085290 1.0873903 1.0944883
## [6,] 1.090889 1.1048695 1.1184216
```

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] 5454 529 233 1240 6736 9751 4592 4785 6988 2221`

… with the following distances to those neighbors:

`fout$distance[3,]`

```
## [1] 0.8630420 0.9878666 1.0299327 1.0332656 1.0421341 1.0990158 1.1030762
## [8] 1.1131533 1.1140538 1.1651180
```

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,] 4511 6778 127 9241 2451
## [2,] 8371 8130 6141 6190 4871
## [3,] 7072 9878 1706 2136 2877
## [4,] 6004 8387 463 6508 8215
## [5,] 1396 7610 1218 4706 5291
## [6,] 3543 7057 5913 3415 4186
```

`head(qout$distance)`

```
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.9408713 0.9821246 1.0343322 1.0367641 1.0424123
## [2,] 0.9023687 0.9440995 0.9831849 1.0174269 1.0213958
## [3,] 0.9290601 0.9838771 0.9892178 1.0400482 1.0841410
## [4,] 0.7644032 0.8516743 0.8601078 0.8895502 0.9249100
## [5,] 0.9652028 0.9978860 1.0026678 1.0127375 1.0430779
## [6,] 0.8654624 0.8911202 0.9048687 0.9236370 0.9436597
```

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] 7072 9878 1706 2136 2877`

… with the following distances to those neighbors:

`qout$distance[3,]`

`## [1] 0.9290601 0.9838771 0.9892178 1.0400482 1.0841410`

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,] 5454 529 233 1240 6736
## [2,] 489 8267 3749 2533 3593
## [3,] 9370 8464 5536 6041 4117
##
## $distance
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8630420 0.9878666 1.029933 1.033266 1.042134
## [2,] 0.9630268 1.0121882 1.021501 1.087415 1.100233
## [3,] 0.9092166 0.9607478 0.966923 0.970672 1.006463
```

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 version 4.1.0 (2021-05-18)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.2 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.13-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.13-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.26.0 BiocNeighbors_1.10.0 knitr_1.33
## [4] BiocStyle_2.20.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.6 magrittr_2.0.1 BiocGenerics_0.38.0
## [4] lattice_0.20-44 R6_2.5.0 rlang_0.4.11
## [7] stringr_1.4.0 tools_4.1.0 parallel_4.1.0
## [10] grid_4.1.0 xfun_0.23 jquerylib_0.1.4
## [13] htmltools_0.5.1.1 yaml_2.2.1 digest_0.6.27
## [16] bookdown_0.22 Matrix_1.3-3 BiocManager_1.30.15
## [19] S4Vectors_0.30.0 sass_0.4.0 evaluate_0.14
## [22] rmarkdown_2.8 stringi_1.6.2 compiler_4.1.0
## [25] bslib_0.2.5.1 stats4_4.1.0 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.