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] 447 933 291 434 47 693 943 382 100 68 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  447  385  524  152  374   11  465  688  362   294
##  [2,]  933  724   19  918  380  859  655  773  149   755
##  [3,]  291  100  834  655  149  318  464  928   56   757
##  [4,]  434  371  358  555  509  230  730  932  139   801
##  [5,]   47  311  729  897  575  969  979  564  242   976
##  [6,]  693  220  259  518  986  499  455  906  963   425
##  [7,]  943  792  587  723  433  857  358  640  944   198
##  [8,]  382  349  849  585  789  918  503  616  865   277
##  [9,]  100  291  850   51  834  724   24  655  987   464
## [10,]   68  589  923  461  808  974  611  754  727    78
## [11,]  922  231  872  374  489  492  524  294   82   507
## [12,]  294  141  688  177  238  374  603  869  362    82
## [13,]  926  801  741   27  685  730  545  169  198   139
## [14,]  572  709  563  503  916  117  953  619  113   956
## [15,]  258  266  224  319  953  384  616  935  802   288
## [16,]  503  585  135  288  918  709  293  690  619   616
## [17,]  273  662  726  499  788  984  324  907  195   277
## [18,]  789  274  656  989  321  435  356  298  478   865
## [19,]  452  430  598  658  634  925  966  298  384   802
## [20,]  766  337  102  275  257  308   25  199  458   344
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.76 4.33 2.64 3.41 3.07 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.756475 3.789129 3.981918 3.998771 4.028293 4.057891 4.208631 4.298468
##  [2,] 4.332648 4.819611 4.841182 4.875379 4.955078 4.993592 5.006686 5.011562
##  [3,] 2.636345 2.654513 2.748174 2.772766 2.860018 2.874252 2.972508 3.037446
##  [4,] 3.413191 3.810307 3.983984 4.012622 4.103935 4.231706 4.332587 4.341292
##  [5,] 3.074132 3.293389 3.435677 3.445301 3.496037 3.528708 3.577640 3.587734
##  [6,] 3.181706 3.356851 3.359119 3.477465 3.662189 3.786363 3.906281 3.954731
##  [7,] 4.047615 4.070849 4.157514 4.318927 4.394431 4.453658 4.458590 4.520748
##  [8,] 2.742485 2.835728 2.875798 2.910265 3.053854 3.116936 3.123300 3.200762
##  [9,] 3.289579 3.465452 3.609777 3.621416 3.720741 3.787543 3.793872 3.891145
## [10,] 2.289549 2.674750 2.767685 2.799328 2.817048 2.830405 2.916928 2.946039
## [11,] 2.548350 2.825372 2.887469 3.016062 3.174962 3.228291 3.232466 3.305843
## [12,] 2.853031 3.550295 3.665247 3.715607 3.732783 3.818286 3.821909 3.834746
## [13,] 3.815551 4.117455 4.121812 4.140898 4.259279 4.362753 4.409985 4.416034
## [14,] 3.616236 3.887278 3.927038 4.003654 4.026565 4.160402 4.168260 4.259835
## [15,] 2.905517 3.179290 3.203555 3.237597 3.251187 3.263027 3.274653 3.338310
## [16,] 2.316222 2.645666 2.917214 2.973821 2.978794 3.044055 3.080280 3.113044
## [17,] 3.799120 4.016923 4.100286 4.161794 4.196074 4.341566 4.406037 4.560377
## [18,] 3.740966 3.847246 3.875031 3.930930 4.052366 4.055403 4.063006 4.066817
## [19,] 2.102466 3.246829 3.284013 3.344346 3.348197 3.484666 3.662005 3.678749
## [20,] 2.762653 3.199871 3.268338 3.362720 3.380915 3.545225 3.561651 3.563866
##           [,9]    [,10]
##  [1,] 4.314644 4.370974
##  [2,] 5.032813 5.080259
##  [3,] 3.059503 3.067768
##  [4,] 4.375022 4.376458
##  [5,] 3.603934 3.712189
##  [6,] 3.974488 4.117223
##  [7,] 4.581503 4.635136
##  [8,] 3.208651 3.223906
##  [9,] 3.892530 3.989745
## [10,] 3.027499 3.107892
## [11,] 3.314668 3.334937
## [12,] 3.846091 3.854649
## [13,] 4.426915 4.440302
## [14,] 4.284406 4.294126
## [15,] 3.363383 3.368632
## [16,] 3.185147 3.232046
## [17,] 4.567713 4.598744
## [18,] 4.067455 4.077304
## [19,] 3.729281 3.771632
## [20,] 3.625486 3.635257

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.997                          1                  0.911
##  2                       1                              1                  0.666
##  3                       0.993                          1                  0.666
##  4                       0.748                          1                  0.953
##  5                       0.873                          1                  0.905
##  6                       0.827                          1                  0.996
##  7                       1                              1                  0.778
##  8                       0.666                          1                  0.666
##  9                       0.927                          1                  0.891
## 10                       0.871                          1                  0.666
## # ℹ 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.402          -0.356         -0.397                  -0.883  
##  2       -0.0897         -0.411         -0.395                  -0.649  
##  3       -0.140          -0.291         -0.305                  -0.494  
##  4       -0.374          -0.343         -0.443                  -0.326  
##  5       -0.234          -0.0633        -0.104                  -0.579  
##  6       -0.231          -0.449         -0.894                   0.285  
##  7       -0.445          -0.289         -0.451                  -0.496  
##  8       -0.00438        -0.304         -0.248                   0.347  
##  9       -0.251          -0.150          0.989                  -0.00211
## 10        0.504          -0.0952        -0.0422                  0.453  
## # ℹ 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.221 0.194 0.324 0.219 0.265 ...