K-nearest neighbors:

We read in input.scone.csv, which is our file modified (and renamed) from the get.marker.names() function. The K-nearest neighbor generation is derived from the Fast Nearest Neighbors (FNN) R package, within our function Fnn(), which takes as input the “input markers” to be used, along with the concatenated data previously generated, and the desired k. We advise the default selection to the total number of cells in the dataset divided by 100, as has been optimized on existing mass cytometry datasets. The output of this function is a matrix of each cell and the identity of its k-nearest neighbors, in terms of its row number in the dataset used here as input.

library(Sconify)
# Markers from the user-generated excel file
marker.file <- system.file('extdata', 'markers.csv', package = "Sconify")
markers <- ParseMarkers(marker.file)

# How to convert your excel sheet into vector of static and functional markers
markers
## $input
##  [1] "CD3(Cd110)Di"           "CD3(Cd111)Di"           "CD3(Cd112)Di"          
##  [4] "CD235-61-7-15(In113)Di" "CD3(Cd114)Di"           "CD45(In115)Di"         
##  [7] "CD19(Nd142)Di"          "CD22(Nd143)Di"          "IgD(Nd145)Di"          
## [10] "CD79b(Nd146)Di"         "CD20(Sm147)Di"          "CD34(Nd148)Di"         
## [13] "CD179a(Sm149)Di"        "CD72(Eu151)Di"          "IgM(Eu153)Di"          
## [16] "Kappa(Sm154)Di"         "CD10(Gd156)Di"          "Lambda(Gd157)Di"       
## [19] "CD24(Dy161)Di"          "TdT(Dy163)Di"           "Rag1(Dy164)Di"         
## [22] "PreBCR(Ho165)Di"        "CD43(Er167)Di"          "CD38(Er168)Di"         
## [25] "CD40(Er170)Di"          "CD33(Yb173)Di"          "HLA-DR(Yb174)Di"       
## 
## $functional
##  [1] "pCrkL(Lu175)Di"  "pCREB(Yb176)Di"  "pBTK(Yb171)Di"   "pS6(Yb172)Di"   
##  [5] "cPARP(La139)Di"  "pPLCg2(Pr141)Di" "pSrc(Nd144)Di"   "Ki67(Sm152)Di"  
##  [9] "pErk12(Gd155)Di" "pSTAT3(Gd158)Di" "pAKT(Tb159)Di"   "pBLNK(Gd160)Di" 
## [13] "pP38(Tm169)Di"   "pSTAT5(Nd150)Di" "pSyk(Dy162)Di"   "tIkBa(Er166)Di"
# Get the particular markers to be used as knn and knn statistics input
input.markers <- markers[[1]]
funct.markers <- markers[[2]]

# Selection of the k. See "Finding Ideal K" vignette
k <- 30

# The built-in scone functions
wand.nn <- Fnn(cell.df = wand.combined, input.markers = input.markers, k = k)
# Cell identity is in rows, k-nearest neighbors are columns
# List of 2 includes the cell identity of each nn, 
#   and the euclidean distance between
#   itself and the cell of interest

# Indices
str(wand.nn[[1]])
##  int [1:1000, 1:30] 848 310 334 234 129 507 279 507 749 25 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  848  558  132  256  344  925  428  163  890   906
##  [2,]  310  467  822  986  885  811  369  609  957   211
##  [3,]  334   64  255  861  728  289  574  554  219   850
##  [4,]  234   55  326  761  891  403  904  533  152   764
##  [5,]  129  696  637  723  285   14  598  546   43   196
##  [6,]  507  880  823  468  589  320  144   94  920    44
##  [7,]  279  255  272  299  897  850   64   13  807   334
##  [8,]  507  920  588    6  682   85  845  357  359   780
##  [9,]  749  815  361    6  491   83  739  113  507   320
## [10,]   25  657   40  152  438  859  162  465  533   201
## [11,]  813  833  742  508  599  363  961  448  652   229
## [12,]  882   20  611   58  992  696  664  814  870   919
## [13,]   76  272  279  575  971  850    7  334  474   417
## [14,]  285  696  814  980   20  461   80  922  740    77
## [15,]  937   61  289  912  188  821  920  816  833   389
## [16,]  301  861   11  833  821  876  207  229  813   389
## [17,]  294  331  975  587  773  514  535  360  588   568
## [18,]  216  178  408  148  346  620  785  896  680   131
## [19,]  733  680  959  912  608  516  420  391   87   896
## [20,]   12  834   58  106  950  740  919  922  882   711
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.42 3.12 2.67 3.62 4.39 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.420489 3.612350 4.051921 4.191703 4.200431 4.206895 4.244952 4.281859
##  [2,] 3.119796 3.238202 3.397645 3.438806 3.476889 3.486682 3.495449 3.561851
##  [3,] 2.670281 3.004736 3.075997 3.115997 3.133923 3.256129 3.265436 3.313952
##  [4,] 3.618035 3.666589 3.672020 3.809696 3.836659 3.908734 3.961121 3.970047
##  [5,] 4.392176 4.574095 4.632136 4.862952 5.006532 5.098635 5.150931 5.255314
##  [6,] 2.916096 3.464806 3.509959 3.515120 3.549773 3.640109 3.684189 3.748847
##  [7,] 3.262575 3.331167 3.470828 3.485061 3.543151 3.553320 3.565500 3.569898
##  [8,] 3.652176 3.991153 4.441109 4.449110 4.518838 4.596414 4.605214 4.665375
##  [9,] 3.348652 3.469219 3.821567 4.023168 4.047665 4.187657 4.254687 4.396174
## [10,] 3.226267 3.923602 4.186758 4.373896 4.379717 4.461656 4.593997 4.647018
## [11,] 2.490997 2.608170 2.728539 2.821396 2.846608 2.887831 2.940268 2.948158
## [12,] 3.096696 3.236308 3.669423 3.842865 3.890517 4.031992 4.056790 4.142554
## [13,] 3.066991 3.252045 3.358895 3.410898 3.506572 3.551685 3.569898 3.621197
## [14,] 3.898640 4.023790 4.188525 4.189104 4.312199 4.326114 4.364901 4.390428
## [15,] 3.271114 3.639932 3.716740 3.717181 3.732477 3.738319 3.742845 3.875122
## [16,] 2.668782 2.889047 3.034984 3.051892 3.227914 3.231673 3.231683 3.252981
## [17,] 4.862263 4.989852 5.362618 5.524886 5.621657 5.629288 5.649932 5.727689
## [18,] 2.326419 2.749611 2.752907 2.767063 2.848022 2.876781 2.920069 2.923723
## [19,] 2.786869 2.876745 2.930500 2.948271 2.954939 3.022705 3.086736 3.107908
## [20,] 3.236308 3.494201 3.537213 3.902774 4.050866 4.087384 4.104316 4.112280
##           [,9]    [,10]
##  [1,] 4.295829 4.301663
##  [2,] 3.566912 3.592812
##  [3,] 3.351092 3.365401
##  [4,] 4.151868 4.195748
##  [5,] 5.290724 5.314025
##  [6,] 3.812522 3.814418
##  [7,] 3.643701 3.687836
##  [8,] 4.721702 4.733855
##  [9,] 4.555771 4.589984
## [10,] 4.730774 4.753673
## [11,] 2.988839 2.994002
## [12,] 4.352212 4.429955
## [13,] 3.625931 3.747158
## [14,] 4.394478 4.401582
## [15,] 3.958643 3.992382
## [16,] 3.357158 3.370297
## [17,] 5.765416 5.787275
## [18,] 2.934396 2.938252
## [19,] 3.109112 3.114164
## [20,] 4.143719 4.188629

Finding scone values:

This function iterates through each KNN, and performs a series of calculations. The first is fold change values for each maker per KNN, where the user chooses whether this will be based on medians or means. The second is a statistical test, where the user chooses t test or Mann-Whitney U test. I prefer the latter, because it does not assume any properties of the distributions. Of note, the p values are adjusted for false discovery rate, and therefore are called q values in the output of this function. The user also inputs a threshold parameter (default 0.05), where the fold change values will only be shown if the corresponding statistical test returns a q value below said threshold. Finally, the “multiple.donor.compare” option, if set to TRUE will perform a t test based on the mean per-marker values of each donor. This is to allow the user to make comparisons across replicates or multiple donors if that is relevant to the user’s biological questions. This function returns a matrix of cells by computed values (change and statistical test results, labeled either marker.change or marker.qvalue). This matrix is intermediate, as it gets concatenated with the original input matrix in the post-processing step (see the relevant vignette). We show the code and the output below. See the post-processing vignette, where we show how this gets combined with the input data, and additional analysis is performed.

wand.scone <- SconeValues(nn.matrix = wand.nn, 
                      cell.data = wand.combined, 
                      scone.markers = funct.markers, 
                      unstim = "basal")

wand.scone
## # A tibble: 1,000 × 34
##    `pCrkL(Lu175)Di.IL7.qvalue` pCREB(Yb176)Di.IL7.qvalu…¹ pBTK(Yb171)Di.IL7.qv…²
##                          <dbl>                      <dbl>                  <dbl>
##  1                       0.958                      0.964                  0.994
##  2                       0.897                      0.964                  0.977
##  3                       0.774                      0.921                  0.878
##  4                       0.950                      1                      0.994
##  5                       0.701                      0.964                  0.999
##  6                       0.986                      0.931                  0.994
##  7                       0.701                      0.853                  0.981
##  8                       0.861                      0.931                  0.999
##  9                       0.925                      0.964                  0.957
## 10                       0.701                      0.972                  0.994
## # ℹ 990 more rows
## # ℹ abbreviated names: ¹​`pCREB(Yb176)Di.IL7.qvalue`,
## #   ²​`pBTK(Yb171)Di.IL7.qvalue`
## # ℹ 31 more variables: `pS6(Yb172)Di.IL7.qvalue` <dbl>,
## #   `cPARP(La139)Di.IL7.qvalue` <dbl>, `pPLCg2(Pr141)Di.IL7.qvalue` <dbl>,
## #   `pSrc(Nd144)Di.IL7.qvalue` <dbl>, `Ki67(Sm152)Di.IL7.qvalue` <dbl>,
## #   `pErk12(Gd155)Di.IL7.qvalue` <dbl>, `pSTAT3(Gd158)Di.IL7.qvalue` <dbl>, …

For programmers: performing additional per-KNN statistics

If one wants to export KNN data to perform other statistics not available in this package, then I provide a function that produces a list of each cell identity in the original input data matrix, and a matrix of all cells x features of its KNN.

I also provide a function to find the KNN density estimation independently of the rest of the “scone.values” analysis, to save time if density is all the user wants. With this density estimation, one can perform interesting analysis, ranging from understanding phenotypic density changes along a developmental progression (see post-processing vignette for an example), to trying out density-based binning methods (eg. X-shift). Of note, this density is specifically one divided by the aveage distance to k-nearest neighbors. This specific measure is related to the Shannon Entropy estimate of that point on the manifold (https://hal.archives-ouvertes.fr/hal-01068081/document).

I use this metric to avoid the unusual properties of the volume of a sphere as it increases in dimensions (https://en.wikipedia.org/wiki/Volume_of_an_n-ball). This being said, one can modify this vector to be such a density estimation (example http://www.cs.haifa.ac.il/~rita/ml_course/lectures_old/KNN.pdf), by treating the distance to knn as the radius of a n-dimensional sphere and incoroprating said volume accordingly.

An individual with basic programming skills can iterate through these elements to perform the statistics of one’s choosing. Examples would include per-KNN regression and classification, or feature imputation. The additional functionality is shown below, with the example knn.list in the package being the first ten instances:

# Constructs KNN list, computes KNN density estimation
wand.knn.list <- MakeKnnList(cell.data = wand.combined, nn.matrix = wand.nn)
wand.knn.list[[8]]
## # A tibble: 30 × 51
##    `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(In113)Di`
##             <dbl>          <dbl>          <dbl>                    <dbl>
##  1        -0.142         -0.171        -0.0665                     0.220
##  2        -0.202         -0.0294       -0.289                     -0.534
##  3         0.103          0.100        -0.236                      0.938
##  4        -0.0866        -0.0432       -0.260                      0.112
##  5        -0.0165        -0.0589       -0.00434                   -1.10 
##  6        -0.208         -0.0944       -0.163                     -0.440
##  7        -0.221         -0.132        -0.427                     -0.636
##  8        -0.158          0.152        -0.673                     -1.65 
##  9        -0.240         -0.571         0.519                     -1.41 
## 10        -0.239         -0.354        -0.245                      0.119
## # ℹ 20 more rows
## # ℹ 47 more variables: `CD3(Cd114)Di` <dbl>, `CD45(In115)Di` <dbl>,
## #   `CD19(Nd142)Di` <dbl>, `CD22(Nd143)Di` <dbl>, `IgD(Nd145)Di` <dbl>,
## #   `CD79b(Nd146)Di` <dbl>, `CD20(Sm147)Di` <dbl>, `CD34(Nd148)Di` <dbl>,
## #   `CD179a(Sm149)Di` <dbl>, `CD72(Eu151)Di` <dbl>, `IgM(Eu153)Di` <dbl>,
## #   `Kappa(Sm154)Di` <dbl>, `CD10(Gd156)Di` <dbl>, `Lambda(Gd157)Di` <dbl>,
## #   `CD24(Dy161)Di` <dbl>, `TdT(Dy163)Di` <dbl>, `Rag1(Dy164)Di` <dbl>, …
# Finds the KNN density estimation for each cell, ordered by column, in the 
# original data matrix
wand.knn.density <- GetKnnDe(nn.matrix = wand.nn)
str(wand.knn.density)
##  num [1:1000] 0.223 0.27 0.293 0.237 0.184 ...