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] 179 499 675 95 407 361 884 761 801 304 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  179  507  864  963  127   42  631  473  531   852
##  [2,]  499  668  421  197  184  485  539  775  571   952
##  [3,]  675  175  350  800  449  783  784  188  395   336
##  [4,]   95  133  285  730  985  779  823  816  514   156
##  [5,]  407  497  297   53  605  768  784  535  512   444
##  [6,]  361  884  791  253  674  885   96  343  397   252
##  [7,]  884  650  902  951  200  674  856  738  547   896
##  [8,]  761  520   11  743  729   60  156  994  334   285
##  [9,]  801  444  372   87  279   29  917  209    5   879
## [10,]  304  642   59  858  981  637  646  163  411   906
## [11,]  816  761  514    8  167  743  505  572  740    81
## [12,]  672  602  269  644  954   77  933  810  898   359
## [13,]  275  524  347  970  155  814   56   60  522   776
## [14,]   26  197  896  184    2  166  421  775  571   628
## [15,]   93  122  331  860  418  468  574  192  726   388
## [16,]  921  858   69  551  881   78   59  154  649   304
## [17,]   86  657  891  212  847  147  546  797  760   351
## [18,]  453  916  848  448  280  450  226  993  422   611
## [19,]  516  226  281  841  673   31  418  192  204   174
## [20,]  643  563  440  913  407  313   58  388  495   691
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.87 2.7 3.64 3.23 3.22 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.869815 3.961965 4.085290 4.104447 4.281820 4.310497 4.344866 4.551000
##  [2,] 2.701264 2.736076 2.932863 2.972517 2.990896 3.003486 3.029786 3.044186
##  [3,] 3.642712 3.726866 3.745536 3.764372 3.786412 3.790699 3.790938 3.822202
##  [4,] 3.233651 3.259101 3.261762 3.512197 3.546980 3.561774 3.570982 3.609914
##  [5,] 3.224171 3.251393 3.292420 3.358817 3.379048 3.396851 3.412648 3.453275
##  [6,] 3.104493 3.120017 3.151310 3.259400 3.260959 3.302124 3.329819 3.341544
##  [7,] 2.469708 2.565487 2.737319 2.886617 2.981166 2.981517 3.049882 3.090920
##  [8,] 3.030203 3.340989 3.581615 3.596605 3.634253 3.648404 3.857570 3.873034
##  [9,] 4.105703 4.162334 4.238401 4.272652 4.398573 4.427347 4.464406 4.538040
## [10,] 4.353073 4.734903 4.872277 4.923651 4.966864 4.972235 5.160121 5.237000
## [11,] 3.392868 3.395322 3.492632 3.581615 3.619415 3.708442 3.726359 3.761324
## [12,] 4.181574 4.639681 4.659546 5.078659 5.102001 5.177547 5.242917 5.292716
## [13,] 4.138642 4.227155 4.337072 4.659862 4.781641 4.874225 4.913038 4.914206
## [14,] 3.007084 3.058458 3.125352 3.158027 3.190994 3.192741 3.277275 3.282797
## [15,] 3.071579 3.169094 3.303703 3.317323 3.327155 3.334728 3.339836 3.352330
## [16,] 3.887296 4.332159 4.482201 4.517199 4.820245 4.845608 4.864856 4.936828
## [17,] 3.310888 3.500422 3.510578 3.569947 3.584703 3.650517 3.732448 3.756821
## [18,] 4.224166 4.270625 4.283367 4.288571 4.297067 4.388616 4.408025 4.409965
## [19,] 3.163485 3.320573 3.399586 3.472019 3.478667 3.552670 3.595827 3.617146
## [20,] 4.249507 4.817943 5.131622 5.133870 5.333571 5.379952 5.387872 5.408303
##           [,9]    [,10]
##  [1,] 4.656194 4.690288
##  [2,] 3.061991 3.099771
##  [3,] 3.829362 3.875307
##  [4,] 3.612372 3.705089
##  [5,] 3.534975 3.589407
##  [6,] 3.362425 3.400968
##  [7,] 3.101901 3.102828
##  [8,] 3.877377 3.900658
##  [9,] 4.551256 4.560387
## [10,] 5.377997 5.463677
## [11,] 3.777349 3.875657
## [12,] 5.329571 5.345661
## [13,] 5.052050 5.072518
## [14,] 3.338076 3.373694
## [15,] 3.407887 3.417671
## [16,] 4.938594 4.953250
## [17,] 3.805591 3.819267
## [18,] 4.417019 4.442876
## [19,] 3.624391 3.676392
## [20,] 5.470608 5.475497

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.986                      0.809                  0.914
##  2                       0.881                      0.524                  0.831
##  3                       0.888                      0.992                  0.865
##  4                       0.813                      0.907                  0.831
##  5                       0.813                      0.953                  0.939
##  6                       0.857                      0.958                  0.994
##  7                       0.837                      0.931                  0.988
##  8                       0.934                      0.859                  0.831
##  9                       1                          0.906                  0.831
## 10                       1                          0.859                  0.914
## # ℹ 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.0224        -0.156           1.50                     0.356
##  2         0.361          0.754           1.73                     0.339
##  3         0.399          0.925           0.642                   -1.27 
##  4         0.753          0.445           0.661                   -0.283
##  5         0.349          1.05            2.80                    -0.182
##  6        -0.228          0.0463          1.31                    -0.387
##  7        -0.186          0.564           0.358                    0.764
##  8         0.493         -0.245           1.96                    -0.205
##  9         0.0569         1.36            2.55                     0.882
## 10        -0.223          0.425          -0.135                    0.528
## # ℹ 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.207 0.315 0.255 0.26 0.273 ...