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] 910 262 168 317 266 474 497 905 321 86 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  910  766  175  112  203   53  635  101  898   656
##  [2,]  262  700  246  723  553  693   40  917  538   597
##  [3,]  168  321  937  564  693  864  824  976  109   946
##  [4,]  317  596  836  284  383  839  972  568  927    25
##  [5,]  266  840  577  651  858   44  451  915  850   197
##  [6,]  474  729  546  661  279  184  728  481  971    16
##  [7,]  497  524  627  522  528  102  650  818  300   125
##  [8,]  905  613  941  479  913  795  562  985   58   163
##  [9,]  321    3  937  258   96  168  553  247  640   282
## [10,]   86  120  298  467  822  309  881  395  210   986
## [11,]   98   65  766  787  902  238  311  863  119   345
## [12,]  403  979  628  468  549   40  939  725  292   196
## [13,]  718  337  184  587  865  941  464  944  304   985
## [14,]  602  182  565   66  448  845  910  784  176   438
## [15,]  636  878  885  972  807   55  701  839  652   473
## [16,]  458  609  546  729  163  712  279  943  679   796
## [17,]  811  178  879  244  794  926  119  484  675   155
## [18,]  413  937  282  553  640  289  377  647  156   660
## [19,]  155   53  544  845  177  110  811   65  766   495
## [20,]  264  623  453   94  474  552  909    6  184   318
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.16 4.06 3.26 3.47 3.62 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.163055 3.642992 3.674974 3.867695 3.913557 3.994928 4.031744 4.037393
##  [2,] 4.060528 4.122820 4.150343 4.335013 4.370475 4.460616 4.600119 4.722819
##  [3,] 3.261762 3.577959 3.603593 3.648593 3.828365 3.839198 3.850349 3.865013
##  [4,] 3.471128 4.285868 4.326560 4.444999 4.664446 4.700536 4.706743 4.804098
##  [5,] 3.622950 3.631121 3.736735 3.749652 3.753022 3.867628 3.908090 3.925925
##  [6,] 3.280572 3.393617 3.462860 3.582059 3.585558 3.630077 3.635046 3.638980
##  [7,] 2.994119 3.285947 3.530676 3.556735 3.632301 3.712904 3.871217 3.882875
##  [8,] 3.628330 3.721614 3.764528 3.812533 3.898113 3.902123 3.910839 3.950359
##  [9,] 3.982846 4.241187 4.366695 4.421682 4.489985 4.508412 4.622240 4.627651
## [10,] 3.441652 3.709277 3.911358 3.913552 4.088325 4.136013 4.182617 4.187651
## [11,] 3.692020 3.766231 4.076608 4.109448 4.163996 4.241979 4.269572 4.392679
## [12,] 3.823747 3.992522 4.266755 4.304861 4.362201 4.535784 4.613245 4.656977
## [13,] 2.266397 2.469692 2.661384 2.682995 2.847252 2.964757 2.989359 2.994446
## [14,] 3.021091 3.218097 3.239051 3.386201 3.388964 3.425448 3.507701 3.512956
## [15,] 3.600701 3.812674 4.059967 4.131129 4.139887 4.336047 4.395158 4.444189
## [16,] 2.289549 2.674750 2.710660 2.817048 2.824527 2.830405 2.871365 2.916928
## [17,] 2.659198 3.246829 3.348197 3.371351 3.484666 3.544291 3.555447 3.580441
## [18,] 3.417272 3.767443 3.835858 3.858011 4.011873 4.080676 4.107612 4.207351
## [19,] 2.985225 3.063431 3.096491 3.246503 3.315205 3.442619 3.605121 3.650564
## [20,] 4.691686 4.787794 4.810937 4.869627 4.917376 4.990637 5.005949 5.016011
##           [,9]    [,10]
##  [1,] 4.161751 4.179527
##  [2,] 4.759005 4.787616
##  [3,] 3.866061 3.883219
##  [4,] 4.844865 4.930607
##  [5,] 3.965490 4.027999
##  [6,] 3.642457 3.657719
##  [7,] 3.890315 3.914613
##  [8,] 3.965693 3.968549
##  [9,] 4.629036 4.634261
## [10,] 4.202808 4.249487
## [11,] 4.426677 4.465376
## [12,] 4.669576 4.819817
## [13,] 3.048919 3.054558
## [14,] 3.518717 3.536555
## [15,] 4.513846 4.547011
## [16,] 2.935566 2.946039
## [17,] 3.662005 3.678749
## [18,] 4.220961 4.327347
## [19,] 3.670544 3.768121
## [20,] 5.095549 5.108312

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.747                          1                  0.757
##  2                       0.960                          1                  0.848
##  3                       1                              1                  0.785
##  4                       0.995                          1                  0.908
##  5                       0.829                          1                  0.783
##  6                       0.546                          1                  0.910
##  7                       0.939                          1                  0.785
##  8                       0.992                          1                  0.688
##  9                       0.831                          1                  0.928
## 10                       1                              1                  0.964
## # ℹ 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.157          -0.168        -0.0231                  -0.0425
##  2        -0.0882          1.04          0.385                   -0.361 
##  3        -0.192           0.452        -0.404                   -0.350 
##  4        -0.0982         -0.256         0.216                   -0.554 
##  5        -0.275          -0.295        -0.633                    0.187 
##  6        -0.474          -0.231        -0.312                   -0.419 
##  7        -0.269          -0.250         0.691                    0.817 
##  8        -0.171          -0.126        -0.0613                   0.187 
##  9        -0.203          -0.105        -0.347                   -0.137 
## 10        -0.0657         -0.243        -0.0264                  -0.319 
## # ℹ 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.237 0.207 0.245 0.199 0.237 ...