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] 161 529 693 584 554 680 165 722 252 836 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  161  726  262   20  554  692  541   98  524   775
##  [2,]  529  754  763  770  313  416  669  643  817   828
##  [3,]  693  176  964   25  546  448  467  200   38   651
##  [4,]  584  567  554  775  116  401  692  889  821   199
##  [5,]  554   86  820  148  207  889  244  933  694   332
##  [6,]  680   14  125  136  691  724  548  256  659    78
##  [7,]  165   64  868  368  694  107  795  262  965   193
##  [8,]  722  515  739  163   19  848  616  942   58   418
##  [9,]  252  585  634  603  901  335  574  615  228   857
## [10,]  836  230  367  531  427  264  314  200  391   212
## [11,]  434   66  292   18  630  334  516  125   40   556
## [12,]  111  518  531  394  793  836  157  264  361   610
## [13,]  400  456  559  193  663  652  672   84  105   948
## [14,]  691  449  125  714   79  334  689   78  924   198
## [15,]  601  538  872  498  391  626  604  618  732   764
## [16,]  663  775  888  145  107  174  133  443  889    97
## [17,]  222  719   43  861  371  706  829  183  310   774
## [18,]  630  361  909  556  266  110  904  283  292    35
## [19,]  418  163  848  722   60  670  739  515    8   507
## [20,]   98  726  358  338  982  524  287  161  554   967
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.03 3.64 3.63 2.37 2.91 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.025148 3.323617 3.388649 3.421620 3.473889 3.486982 3.613140 3.670150
##  [2,] 3.642857 4.050647 4.135238 4.296608 4.339992 4.637186 4.975909 5.001013
##  [3,] 3.626690 3.781224 3.921660 4.270507 4.324263 4.357121 4.374040 4.441968
##  [4,] 2.374976 2.456418 2.506369 2.943577 3.006399 3.105921 3.153009 3.168570
##  [5,] 2.913686 3.017727 3.070864 3.172605 3.205827 3.272177 3.280142 3.291697
##  [6,] 3.217416 3.420631 3.694704 3.768833 3.773349 3.800538 3.847745 3.859671
##  [7,] 2.696517 2.705975 2.815296 3.045004 3.275969 3.421595 3.466070 3.481313
##  [8,] 3.211137 3.266432 3.639487 3.668870 3.928391 4.199958 4.530719 4.585742
##  [9,] 3.471128 3.631353 3.834755 3.836526 3.913398 3.935926 3.950912 3.967010
## [10,] 3.374277 3.612151 3.625169 3.636588 3.757469 3.818075 3.822374 3.823425
## [11,] 4.022475 4.064084 4.185329 4.284669 4.365769 4.484508 4.586446 4.612879
## [12,] 3.244902 3.582281 3.696477 3.747924 3.855553 3.914378 3.944160 3.958140
## [13,] 4.241187 4.311942 4.365442 4.627651 4.629036 4.680737 4.760091 4.780920
## [14,] 2.676100 2.741691 2.970100 3.173130 3.184604 3.193548 3.231701 3.258761
## [15,] 2.480504 2.646200 2.787093 2.816422 2.861422 2.887437 2.955679 2.963617
## [16,] 4.011873 4.295945 4.306582 4.380067 4.491528 4.616279 4.631275 4.635294
## [17,] 2.902257 3.145769 3.178082 3.249180 3.362196 3.381648 3.386810 3.404567
## [18,] 3.325274 3.442563 3.453003 3.541359 3.553116 3.580820 3.628534 3.692994
## [19,] 2.554459 2.623359 2.955946 3.174172 3.475357 3.785996 3.889002 3.890587
## [20,] 2.718018 2.739598 2.864140 2.965590 3.106157 3.149070 3.185572 3.256762
##           [,9]    [,10]
##  [1,] 3.706352 3.738727
##  [2,] 5.425431 5.436872
##  [3,] 4.448236 4.487402
##  [4,] 3.199420 3.249657
##  [5,] 3.358462 3.361166
##  [6,] 3.870613 3.912247
##  [7,] 3.509693 3.577555
##  [8,] 4.593757 4.611769
##  [9,] 4.033075 4.153947
## [10,] 3.840243 3.872869
## [11,] 4.613099 4.682071
## [12,] 4.029057 4.031206
## [13,] 4.895823 4.921965
## [14,] 3.263031 3.274972
## [15,] 2.971006 2.972881
## [16,] 4.675183 4.709642
## [17,] 3.447821 3.523685
## [18,] 3.700835 3.725202
## [19,] 3.928391 3.997474
## [20,] 3.268406 3.416803

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.982                      0.956                  0.964
##  2                       0.960                      0.948                  0.937
##  3                       1                          0.948                  0.811
##  4                       0.930                      0.973                  1    
##  5                       0.930                      0.948                  0.982
##  6                       0.979                      0.948                  1    
##  7                       0.930                      0.948                  0.799
##  8                       0.988                      0.948                  0.964
##  9                       0.930                      0.948                  0.747
## 10                       0.930                      0.961                  0.742
## # ℹ 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.0894        -0.309       -0.000283                    0.253
##  2        -0.147         -0.355        0.160                      -1.11 
##  3         0.200          0.333        0.227                       0.779
##  4        -0.0230        -0.197       -0.110                      -0.901
##  5        -0.0638        -0.235       -0.265                      -0.240
##  6        -0.114         -0.0571      -0.215                      -0.887
##  7         0.592          0.446        0.877                      -0.784
##  8        -0.414         -0.0421      -0.158                      -0.888
##  9        -0.139         -0.0870      -0.0530                      0.537
## 10        -0.257         -0.311       -0.447                      -0.920
## # ℹ 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.261 0.171 0.219 0.303 0.286 ...