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] 460 345 21 822 547 480 74 145 531 625 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  460  315  914   16  265  982  481  216  963   797
##  [2,]  345  765  524  271  786    5  426  490  859   714
##  [3,]   21  766  394  375  351   85  152  490  805   962
##  [4,]  822  833  360  357  516  577   40  584  265   319
##  [5,]  547  379  384  527  766  786  295  826  811   649
##  [6,]  480  730  179  758  222   86  419   75  981   807
##  [7,]   74  835  963  744  319  491  414  544  798   542
##  [8,]  145  546  690  815  759  418  423  622  288     2
##  [9,]  531  721  985  605   65  547  938   48  826   878
## [10,]  625  953  944  728  436  786  737  829  636   466
## [11,]  139  270  590  406  450  622  595  423  529   490
## [12,]  457  857  743  963  222  630  838  425  899   530
## [13,]  107   61  337  930  571  289  874  870  821   105
## [14,]  149  667  481  279  174  799  980  535  837   781
## [15,]  229  981  211  847  838  991  528  948  183   238
## [16,]  837  269  980  279  481  200  517  615  241    18
## [17,]  264  681  620  227  937  684  574  730  791   381
## [18,]  610  781  241   16  837  782  929  367  175   889
## [19,]  890  693   45  992   28  103  718  241  175   261
## [20,]  300  784  799  989  149  716  566  301  108   535
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.51 2.54 3.13 2.72 2.47 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.505377 3.576304 3.599633 3.605568 3.670720 3.682489 3.777911 3.807332
##  [2,] 2.539880 2.858838 3.014138 3.125896 3.188564 3.215832 3.358280 3.358776
##  [3,] 3.133906 3.145573 3.405517 3.820600 3.875179 3.902213 4.143768 4.191635
##  [4,] 2.719158 3.249377 3.530354 3.583326 3.787112 3.811542 3.812786 3.836429
##  [5,] 2.467871 2.564574 2.696517 2.840044 2.878373 2.878891 3.001232 3.011425
##  [6,] 2.172608 2.706849 2.733156 2.992774 3.077080 3.126652 3.189087 3.191709
##  [7,] 2.740607 3.046171 3.066226 3.252553 3.273305 3.287488 3.357260 3.390128
##  [8,] 2.600569 3.059771 3.193362 3.439839 3.520120 3.851038 3.902911 3.978886
##  [9,] 3.302046 3.591900 3.640134 3.707715 3.722861 3.730543 3.750884 3.906987
## [10,] 3.329113 3.402131 3.673282 3.737479 3.737783 3.770339 3.771929 3.809920
## [11,] 3.056717 3.321665 3.475983 3.579560 3.677234 3.751361 3.765303 3.797230
## [12,] 3.425248 3.431334 3.646368 3.906731 4.118588 4.230430 4.249367 4.259148
## [13,] 3.537685 4.080412 4.148769 4.161267 4.180644 4.186317 4.332501 4.404330
## [14,] 2.866192 2.918729 2.920141 2.964791 3.026797 3.038580 3.053570 3.063518
## [15,] 3.985561 4.087749 4.124803 4.131753 4.152939 4.221588 4.254944 4.264703
## [16,] 2.219441 2.399169 2.736980 2.759300 2.810747 2.896945 2.932420 3.064830
## [17,] 3.457666 4.035891 4.174492 4.284514 4.366909 4.394399 4.407696 4.414300
## [18,] 2.501847 2.937926 3.014618 3.099248 3.124067 3.127559 3.149268 3.172053
## [19,] 2.865907 3.067158 3.125141 3.220861 3.370648 3.384074 3.401002 3.414808
## [20,] 2.544907 3.174652 3.397247 3.417775 3.449982 3.495122 3.565658 3.841865
##           [,9]    [,10]
##  [1,] 3.877310 3.908573
##  [2,] 3.393188 3.472218
##  [3,] 4.197356 4.253011
##  [4,] 3.840055 3.866257
##  [5,] 3.022827 3.027098
##  [6,] 3.227914 3.241524
##  [7,] 3.618946 3.627295
##  [8,] 4.036284 4.131736
##  [9,] 3.924154 3.966585
## [10,] 3.811141 3.823605
## [11,] 3.860219 3.871200
## [12,] 4.270365 4.317082
## [13,] 4.487576 4.574648
## [14,] 3.133546 3.140060
## [15,] 4.313082 4.336712
## [16,] 3.070167 3.099248
## [17,] 4.487955 4.503109
## [18,] 3.182050 3.186249
## [19,] 3.433616 3.460708
## [20,] 3.955273 3.991728

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.980                      1                      0.968
##  2                       0.980                      0.945                  0.979
##  3                       0.991                      0.958                  0.568
##  4                       0.989                      1                      0.979
##  5                       0.980                      1                      0.929
##  6                       0.970                      1                      0.533
##  7                       0.998                      0.964                  0.815
##  8                       0.980                      0.641                  1    
##  9                       0.980                      0.953                  0.826
## 10                       0.980                      0.785                  0.793
## # ℹ 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.299          0.626         -0.278                     0.808
##  2        -1.11          -0.0874        -0.826                    -1.34 
##  3        -0.0189        -0.237          0.439                    -0.237
##  4         0.566          0.160          0.584                    -0.563
##  5        -1.08          -0.239         -0.689                     0.835
##  6        -0.156          0.133         -0.178                    -1.03 
##  7        -0.176         -0.130         -0.221                    -0.428
##  8        -0.311         -0.148         -0.0418                    0.670
##  9         0.361          0.902          1.38                      0.337
## 10        -0.194         -0.262         -0.0632                    0.868
## # ℹ 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.25 0.279 0.231 0.255 0.323 ...