Skip to contents

Methods computing the nearest neighbour indices and distances for matrix and MSnSet instances.

Methods

signature(object = "matrix", k = "numeric", dist = "character", ...)

Calculates indices and distances to the k (default is 3) nearest neighbours of each feature (row) in the input matrix object. The distance dist can be either of "euclidean" or "mahalanobis". Additional parameters can be passed to the internal function FNN::get.knn. Output is a matrix with 2 * k columns and nrow(object) rows.

signature(object = "MSnSet", k = "numeric", dist = "character", ...)

As above, but for an MSnSet input. The indices and distances to the k nearest neighbours are added to the object's feature metadata.

signature(object = "matrix", query = "matrix", k = "numeric", ...)

If two matrix instances are provided as input, the k (default is 3) indices and distances of the nearest neighbours of query in object are returned as a matrix of dimensions 2 * k by nrow(query). Additional parameters are passed to FNN::get.knnx. Only euclidean distance is available.

Examples

library("pRolocdata")
data(dunkley2006)

## Using a matrix as input
m <- exprs(dunkley2006)
m[1:4, 1:3]
#>              M1F1A    M1F4A    M1F7A
#> AT1G09210 0.323250 0.275500 0.216000
#> AT1G21750 0.332000 0.279667 0.222000
#> AT1G51760 0.397250 0.246500 0.168250
#> AT1G56340 0.336733 0.303267 0.201133
head(nndist(m, k = 5))
#>           index1euc   dist1euc index2euc   dist2euc index3euc   dist3euc
#> AT1G09210         4 0.07704563       151 0.07958198         2 0.08142734
#> AT1G21750         1 0.08142734         4 0.08262245       151 0.08645587
#> AT1G51760       150 0.09921614       156 0.10449514       151 0.10922063
#> AT1G56340         8 0.07268495         1 0.07704563         9 0.07958917
#> AT2G32920         9 0.08767137         4 0.10248944       155 0.10307756
#> AT2G47470       151 0.05067260       153 0.07809223         4 0.08231921
#>           index4euc   dist4euc index5euc   dist5euc
#> AT1G09210         6 0.08738381       150 0.11859295
#> AT1G21750       150 0.09657314         6 0.10902502
#> AT1G51760       155 0.11456182         2 0.11749637
#> AT1G56340         6 0.08231921         2 0.08262245
#> AT2G32920         8 0.10670010         6 0.10806363
#> AT2G47470         1 0.08738381       378 0.09215796
tail(nndist(m[1:100, ], k = 2, dist = "mahalanobis"))
#>           index1mah dist1mah index2mah dist2mah
#> AT3G26830        37 26.26210        33 25.95250
#> AT3G28580        60 25.95250        35 21.88428
#> AT3G28720        70 31.50309        57 28.63844
#> AT3G44330        72 26.74359        64 26.74359
#> AT3G48890        17 33.69198        36 32.85008
#> AT3G51430        56 26.83278        75 22.23005

## Same as above for MSnSet
d <- nndist(dunkley2006, k = 5)
head(fData(d))
#>           assigned  evidence method   new pd.2013 pd.markers markers.orig
#> AT1G09210       ER predicted  PLSDA known      ER   ER lumen           ER
#> AT1G21750       ER predicted  PLSDA known      ER   ER lumen           ER
#> AT1G51760       ER   unknown  PLSDA   new      ER   ER lumen      unknown
#> AT1G56340       ER predicted  PLSDA known      ER   ER lumen           ER
#> AT2G32920       ER predicted  PLSDA known      ER   ER lumen           ER
#> AT2G47470       ER predicted  PLSDA known      ER   ER lumen           ER
#>            markers index1euc   dist1euc index2euc   dist2euc index3euc
#> AT1G09210 ER lumen         4 0.07704563       151 0.07958198         2
#> AT1G21750 ER lumen         1 0.08142734         4 0.08262245       151
#> AT1G51760 ER lumen       150 0.09921614       156 0.10449514       151
#> AT1G56340 ER lumen         8 0.07268495         1 0.07704563         9
#> AT2G32920 ER lumen         9 0.08767137         4 0.10248944       155
#> AT2G47470 ER lumen       151 0.05067260       153 0.07809223         4
#>             dist3euc index4euc   dist4euc index5euc   dist5euc
#> AT1G09210 0.08142734         6 0.08738381       150 0.11859295
#> AT1G21750 0.08645587       150 0.09657314         6 0.10902502
#> AT1G51760 0.10922063       155 0.11456182         2 0.11749637
#> AT1G56340 0.07958917         6 0.08231921         2 0.08262245
#> AT2G32920 0.10307756         8 0.10670010         6 0.10806363
#> AT2G47470 0.08231921         1 0.08738381       378 0.09215796

d <- nndist(dunkley2006[1:100, ], k = 2, dist = "mahalanobis")
tail(fData(d))
#>           assigned evidence method     new pd.2013 pd.markers markers.orig
#> AT3G26830       ER  unknown  PLSDA     new      ER    unknown      unknown
#> AT3G28580       ER  unknown  PLSDA     new      ER    unknown      unknown
#> AT3G28720  unknown  unknown  PLSDA unknown      ER    unknown      unknown
#> AT3G44330       ER  unknown  PLSDA     new      ER    unknown      unknown
#> AT3G48890  unknown  unknown  PLSDA unknown      ER    unknown      unknown
#> AT3G51430       ER  unknown  PLSDA     new      ER    unknown      unknown
#>           markers index1mah dist1mah index2mah dist2mah
#> AT3G26830 unknown        37 26.26210        33 25.95250
#> AT3G28580 unknown        60 25.95250        35 21.88428
#> AT3G28720 unknown        70 31.50309        57 28.63844
#> AT3G44330 unknown        72 26.74359        64 26.74359
#> AT3G48890 unknown        17 33.69198        36 32.85008
#> AT3G51430 unknown        56 26.83278        75 22.23005

## Using a query
nndist(m[1:100, ], m[101:110, ], k = 2)
#>           index1euc   dist1euc index2euc   dist2euc
#> AT3G51440        40 0.08584173        78 0.08761483
#> AT3G51460        59 0.07788915        86 0.07970032
#> AT3G57010        86 0.05580589        61 0.05615363
#> AT3G57030        34 0.07262401        14 0.07373216
#> AT3G57880        34 0.05350903        46 0.05471768
#> AT3G59500        91 0.10986265        24 0.11019115
#> AT3G60600        64 0.07059527        20 0.07176627
#> AT3G62360        48 0.04383448        17 0.05428251
#> AT3G66658        73 0.06079091        34 0.06115031
#> AT4G00175        75 0.07679760        92 0.07719277