1 Introduction

The BiocNeighbors package provides several algorithms for approximate neighbor searches:

  • The Annoy (Approximate Nearest Neighbors Oh Yeah) method uses C++ code in the RcppAnnoy package. It works by building a tree where a random hyperplane partitions points into two child groups at each internal node. This is repeated to construct a forest of trees where the number of trees determines the accuracy of the search. Given a query data point, we identify all points in the same leaf node for each tree. We then take the union of leaf node sets across trees and search them exactly for the nearest neighbors.
  • The HNSW (Hierarchical Navigable Small Worlds) method uses C++ code in the RcppHNSW package. It works by building a series of nagivable small world graphs containing links between points across the entire data set. The algorithm walks through the graphs where each step is chosen to get closer to a given query point. Different graphs contain links of different lengths, yielding a hierarchy where earlier steps are large and later steps are small. The accuracy of the search is determined by the connectivity of the graphs and the size of the intermediate list of potential neighbors.

These methods complement the exact algorithms described previously. Again, it is straightforward to switch from one algorithm to another by simply changing the BNPARAM argument in findKNN and queryKNN.

2 Identifying nearest neighbors

We perform the k-nearest neighbors search with the Annoy algorithm by specifying BNPARAM=AnnoyParam().

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

fout <- findKNN(data, k=10, BNPARAM=AnnoyParam())
head(fout$index)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,]   53 5353 2023 9931 2760  994 2604 7819  193  5814
## [2,] 8586 8981 2517   30 5372 9505 2323 2509 4531  2725
## [3,] 8854 9155 1583 3675 1517 8073 2266 6583 9844  8169
## [4,] 1471 6059 5283 5085 5558 5117 2252 8044 1832  7740
## [5,] 4140 8087 5650 8297 1478 2883 7849 1196 1603  7392
## [6,] 5630 8636 1413 1497 8811 4017 4451 5918  308  7238
head(fout$distance)
##           [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
## [1,] 0.8884642 0.9403174 0.9653104 1.0295044 1.0579624 1.0800753 1.0907043
## [2,] 0.8462005 0.8791326 0.9770808 1.0681456 1.0965602 1.1017785 1.1087655
## [3,] 0.9260832 0.9414257 0.9436049 0.9547907 0.9627618 0.9983353 1.0060830
## [4,] 0.7942638 0.9342518 0.9665577 0.9737488 0.9789218 0.9791681 0.9900901
## [5,] 0.8463366 0.8486429 0.8935444 0.9395248 0.9626724 0.9695854 0.9758337
## [6,] 0.9866148 1.0038661 1.0106814 1.0253493 1.0294634 1.0418093 1.0592244
##           [,8]      [,9]     [,10]
## [1,] 1.1039586 1.1043444 1.1120301
## [2,] 1.1115894 1.1366431 1.1477174
## [3,] 1.0140244 1.0210154 1.0217787
## [4,] 0.9931630 0.9944342 1.0029531
## [5,] 0.9780093 0.9782090 0.9799659
## [6,] 1.0645270 1.0812619 1.0826969

We can also identify the k-nearest neighbors in one dataset based on query points in another dataset.

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

qout <- queryKNN(data, query, k=5, BNPARAM=AnnoyParam())
head(qout$index)
##      [,1] [,2] [,3] [,4] [,5]
## [1,] 3716 4025 1881 7406 1396
## [2,] 8143 5213 5877 3956 2456
## [3,] 1309 3117 6457 4684 2083
## [4,] 1267 6694  621 2222 8358
## [5,] 9348 8161  448 3750   76
## [6,] 2257 2699 5238  769 4032
head(qout$distance)
##           [,1]      [,2]      [,3]      [,4]      [,5]
## [1,] 0.8983552 0.9042156 0.9361026 0.9491780 0.9586194
## [2,] 0.9948943 1.0481845 1.0545416 1.0589106 1.0658462
## [3,] 0.9102096 0.9705096 0.9977661 1.0842166 1.1232716
## [4,] 0.8271732 1.0789361 1.1740106 1.2131128 1.2179416
## [5,] 0.8764364 0.9215204 0.9771883 1.0012633 1.0081911
## [6,] 0.7942141 0.8562478 0.8593587 0.9096819 0.9113994

It is similarly easy to use the HNSW algorithm by setting BNPARAM=HnswParam().

3 Further options

Most of the options described for the KMKNN algorithm 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.
  • BNINDEX to build the forest once for a given data set and re-use it across calls.

The use of a pre-built BNINDEX is illustrated below:

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

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

4 Saving the index files

The forest of trees form an indexing structure that is saved to file. By default, this file is located in tempdir()1 On HPC file systems, you can change TEMPDIR to a location that is more amenable to parallelized access. and will be removed when the session finishes.

AnnoyIndex_path(pre)
## [1] "/tmp/RtmpP3KLXO/file12efe4c5809cb.idx"

If the index is to persist across sessions, the path of the index file can be directly specified in buildIndex. However, this means that it becomes the responsibility of the user to clean up any temporary indexing files after calculations are complete.

5 Session information

sessionInfo()
## R version 3.6.0 (2019-04-26)
## 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/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/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] BiocNeighbors_1.2.0 knitr_1.22          BiocStyle_2.12.0   
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.1          bookdown_0.9        digest_0.6.18      
##  [4] stats4_3.6.0        magrittr_1.5        evaluate_0.13      
##  [7] stringi_1.4.3       S4Vectors_0.22.0    rmarkdown_1.12     
## [10] BiocParallel_1.18.0 tools_3.6.0         stringr_1.4.0      
## [13] parallel_3.6.0      xfun_0.6            yaml_2.2.0         
## [16] compiler_3.6.0      BiocGenerics_0.30.0 BiocManager_1.30.4 
## [19] htmltools_0.3.6