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] 647 892 652 230 231 825 284 679 239 65 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  647  969  582  941 1000  257  843  589  625   695
##  [2,]  892  771  450  577  835  601  830  484  681   265
##  [3,]  652  719  360   56  704  162  481  309  670   744
##  [4,]  230  603  801  338  402  907  243  715  496   700
##  [5,]  231  754  411  963  273  145  230  412  530   676
##  [6,]  825  835  267  503  894  830  536  379  242   165
##  [7,]  284  278  360  782  122  839  704  905  719   907
##  [8,]  679  775  238   84  132  783  308  697  191   234
##  [9,]  239  356  786  413  201  923  236  946  867   146
## [10,]   65  820  698  776    9  212  311  839  763   968
## [11,]  260  574  854  663  691  951  203  502  724    28
## [12,]  617  763  237  862  753  908  545  877  917   375
## [13,] 1000  235  647   35  332  582  589  172  356   860
## [14,]  949  561  988  518  896  683  117  938  639   711
## [15,]  198  846  545  588  617  303  586  307  725   474
## [16,]  113  591  945  577  560  937  892  993  450   656
## [17,]  841  959  169  738  664  335  391   47  202   546
## [18,]  605  399  722  410  249  374  466  586  501   327
## [19,]  141  723  414  978  885  936  952  739   51   513
## [20,]   77  267  675  427  349  524  394  787  253   656
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.95 2.81 3.52 4.02 2.69 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.953928 3.318031 3.472346 3.605232 3.619428 3.632531 3.720944 3.751212
##  [2,] 2.808518 2.889098 3.166375 3.285978 3.290163 3.348788 3.396739 3.419711
##  [3,] 3.522686 3.535084 3.620843 3.706292 3.745903 3.781679 3.814151 3.904025
##  [4,] 4.020063 4.071853 4.076856 4.272803 4.321498 4.365737 4.415725 4.430108
##  [5,] 2.690266 3.146843 3.165143 3.254884 3.265306 3.270347 3.271496 3.273412
##  [6,] 2.994517 3.033252 3.037809 3.087345 3.143790 3.172041 3.321594 3.338371
##  [7,] 3.215614 3.715483 4.269376 4.396100 4.592158 4.668892 4.735616 4.783465
##  [8,] 4.110670 4.276462 4.326114 4.336576 4.381202 4.509305 4.545038 4.549587
##  [9,] 3.331167 3.550177 3.553320 3.565500 3.607025 3.639282 3.687836 3.698943
## [10,] 4.518016 4.651768 5.611126 5.624392 5.637483 5.647521 5.703896 5.744498
## [11,] 3.417272 3.775577 3.830728 4.315870 4.484796 4.617598 4.639104 4.670535
## [12,] 4.181574 4.639681 4.659546 5.078659 5.102001 5.177237 5.255882 5.292716
## [13,] 3.735717 3.868038 3.902536 4.004290 4.026934 4.030179 4.096880 4.204724
## [14,] 2.644720 2.818289 3.022139 3.071176 3.107513 3.124147 3.231125 3.308493
## [15,] 4.192058 4.199042 4.338093 4.365472 4.380111 4.395456 4.500361 4.699085
## [16,] 2.718018 3.048336 3.268406 3.330299 3.434021 3.442078 3.445528 3.455785
## [17,] 5.277550 5.322125 5.372345 5.380729 5.407068 5.442963 5.544765 5.550085
## [18,] 3.652176 4.199195 4.441109 4.449110 4.477519 4.547056 4.596414 4.733855
## [19,] 4.048453 4.331377 4.763687 4.834264 4.968252 4.996791 5.014418 5.065214
## [20,] 3.514509 3.544002 3.652081 3.765027 3.845087 3.865452 3.912850 3.968964
##           [,9]    [,10]
##  [1,] 3.784599 3.909524
##  [2,] 3.428941 3.435176
##  [3,] 3.920367 3.966544
##  [4,] 4.521247 4.573941
##  [5,] 3.320257 3.329156
##  [6,] 3.380381 3.381674
##  [7,] 4.785975 4.798028
##  [8,] 4.587822 4.607493
##  [9,] 3.743360 3.757137
## [10,] 5.752139 5.759549
## [11,] 4.775378 4.780274
## [12,] 5.372905 5.383269
## [13,] 4.245660 4.250724
## [14,] 3.318214 3.323475
## [15,] 4.703711 4.707577
## [16,] 3.483882 3.485682
## [17,] 5.610981 5.626681
## [18,] 4.788771 4.796307
## [19,] 5.089879 5.107419
## [20,] 3.987274 4.017990

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.908                      0.727                  1    
##  2                       0.908                      0.762                  0.826
##  3                       0.975                      0.727                  0.806
##  4                       0.995                      0.982                  0.929
##  5                       0.908                      0.870                  0.477
##  6                       0.999                      0.982                  0.922
##  7                       0.975                      0.964                  1    
##  8                       0.975                      0.727                  0.983
##  9                       0.975                      0.990                  0.781
## 10                       0.975                      0.936                  0.938
## # ℹ 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.689           1.67           1.65                     0.479
##  2        -0.0619          0.102          1.21                     0.496
##  3        -0.337           0.202         -0.859                    0.888
##  4        -0.871          -0.433         -0.124                    0.780
##  5        -0.110           0.373          0.316                    0.891
##  6        -0.326           1.53          -0.579                    0.724
##  7        -0.441          -0.274         -0.333                   -0.704
##  8        -0.0949          0.966          0.577                   -1.15 
##  9         0.802           1.13           0.913                   -1.45 
## 10        -0.0144         -0.262         -0.116                   -0.640
## # ℹ 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.254 0.28 0.247 0.215 0.294 ...