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] 182 815 520 659 67 953 643 845 665 204 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  182  416  869   46  432  576  615  318  359   231
##  [2,]  815  143  764  468  965  866  459  953  717    98
##  [3,]  520  961  317   83  893   48  841  650  659   900
##  [4,]  659  931  153  622  694  287    3  757  977   265
##  [5,]   67  784  207  884  345  513  319  490  906   266
##  [6,]  953  110  323  346  528  240  960  369  965   345
##  [7,]  643  136  781   49  181  616  164  853  208   663
##  [8,]  845  594  584  346  749  601  414   39  902   906
##  [9,]  665  146  667  982  212  325  812  258  703   424
## [10,]  204  598  636  893  896  153  900  159  855   145
## [11,]  643  663  863  505  112  943  577  270   91   412
## [12,]  717  425  167   67  256  898   24  755  804   343
## [13,]  776   48   58  277  775   74  896  169  900    14
## [14,]  287   13  177  520  538  227  585  776  775   900
## [15,]  958  564  966  198  531  624  674  417  448   483
## [16,]  770  910  681  174  117  668  583  357  464   686
## [17,]  305  175  624  608  364   86  664   15  290   356
## [18,]  363  220   39  150  347  866  459    5  226   311
## [19,]  563  275  624  179  686  117  290  579  175   890
## [20,]  727  761  232   95   76  429  849  288  798   205
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.6 3.13 3.83 5.51 2.21 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.600569 3.316723 3.439839 3.651505 3.677320 3.774882 3.854498 4.036284
##  [2,] 3.133982 3.196974 3.445301 3.458017 3.478402 3.587734 3.603934 3.766172
##  [3,] 3.832430 4.081626 4.116124 4.230996 4.241192 4.241636 4.273084 4.276462
##  [4,] 5.505333 5.680063 5.765442 5.908005 6.197790 6.456469 6.585670 6.667373
##  [5,] 2.213633 2.548933 2.564574 2.698236 2.873438 2.878373 2.903163 2.956495
##  [6,] 2.491643 3.037649 3.381674 3.399999 3.434338 3.511985 3.525676 3.530482
##  [7,] 2.688826 2.790624 2.792039 2.814087 2.857515 3.054258 3.109715 3.124076
##  [8,] 2.977112 3.115229 3.248057 3.274541 3.291762 3.303733 3.351861 3.361758
##  [9,] 5.218843 5.312682 5.317264 5.323891 5.486029 5.544956 5.585368 5.636201
## [10,] 3.171068 3.690300 4.051073 4.102270 4.114573 4.153243 4.286236 4.331974
## [11,] 2.407407 2.637939 2.815737 2.844380 2.919770 2.949460 3.008812 3.071325
## [12,] 3.341104 3.793809 3.805116 3.963862 4.059348 4.116736 4.129074 4.131508
## [13,] 2.551057 2.877558 3.149285 3.246497 3.301250 3.339767 3.431880 3.467129
## [14,] 3.491925 3.537213 3.797314 3.827327 3.904092 3.994113 4.087384 4.093090
## [15,] 2.663812 2.665027 2.920562 3.020810 3.106465 3.141899 3.203740 3.211929
## [16,] 2.539375 2.706849 2.992774 3.045836 3.077080 3.112940 3.184171 3.191709
## [17,] 3.160703 3.264204 3.399980 3.437360 3.569888 3.616157 3.619084 3.645062
## [18,] 3.802450 3.897873 3.912981 3.954685 3.998455 4.008490 4.072789 4.135402
## [19,] 4.097330 4.195101 4.292326 4.388171 4.477444 4.484913 4.514869 4.525190
## [20,] 3.375224 3.529484 3.799120 3.950426 3.982444 4.050666 4.149509 4.196860
##           [,9]    [,10]
##  [1,] 4.061484 4.084623
##  [2,] 3.839323 3.873827
##  [3,] 4.285681 4.306341
##  [4,] 6.741004 6.760492
##  [5,] 3.027098 3.038234
##  [6,] 3.537609 3.579136
##  [7,] 3.134536 3.140842
##  [8,] 3.387902 3.415559
##  [9,] 5.703959 5.705084
## [10,] 4.414010 4.416711
## [11,] 3.072693 3.082324
## [12,] 4.151666 4.185224
## [13,] 3.528934 3.537213
## [14,] 4.143719 4.188629
## [15,] 3.225933 3.234054
## [16,] 3.203300 3.227914
## [17,] 3.705418 3.743153
## [18,] 4.205364 4.218315
## [19,] 4.596748 4.602671
## [20,] 4.319607 4.352936

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                       1                          0.965                  0.786
##  2                       1                          1                      1    
##  3                       1                          0.895                  0.786
##  4                       0.997                      0.965                  0.742
##  5                       1                          0.926                  0.946
##  6                       1                          1                      0.786
##  7                       0.637                      0.961                  0.786
##  8                       1                          1                      0.946
##  9                       1                          0.945                  1    
## 10                       1                          0.972                  0.861
## # ℹ 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.264           1.63          -0.225                    0.644
##  2         0.361           0.754          1.73                     0.339
##  3         0.979           0.413          0.370                    0.748
##  4        -0.0733          0.734          1.09                     0.514
##  5         0.548           0.632          1.62                     0.647
##  6         0.815           0.922          0.293                   -0.580
##  7        -0.465           0.662          0.643                    0.176
##  8         0.367           0.421          1.35                    -0.847
##  9        -0.373           1.01           2.49                     0.955
## 10         0.523           0.752          0.664                    0.820
## # ℹ 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.242 0.259 0.226 0.149 0.322 ...