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] 737 296 450 454 299 508 577 360 933 928 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  737  404  570  425  429  565  612  562  579   810
##  [2,]  296  131   27  622   82  927  226  610  421   967
##  [3,]  450  271  664   23  408  947  307  428  516    84
##  [4,]  454  219  904  409  264  655  453  124  831   595
##  [5,]  299  931  226  306  131  605  354  781  932   927
##  [6,]  508  775  520  468  222  122   38  751  175   771
##  [7,]  577  372  687  838  672  773  190  869  370   388
##  [8,]  360  495   72  703  392  816   46  419   48   134
##  [9,]  933  415  898  821  456  162  170  811  697   289
## [10,]  928  166  789  170  685  681  626   61  412   596
## [11,]  397  444  994  474  828  146  447  292  496   903
## [12,]  449  443  505  364  906  168   92  343  598   184
## [13,]  520  376  508  775  736  341  316  686  532   653
## [14,]  505  534  449  149  181  532  168  988  630   443
## [15,]  423  786  879  814  290  358  636  375  472   266
## [16,]  894  451   69  659  280  367  496  615  251   595
## [17,]  847  964  352  760  827  177  973  775  988   656
## [18,]  784  505  676  443  343   53  448  542  181   281
## [19,]  936  938  509  796  514  455  515   25  950   457
## [20,]   97  807  951  795  601  252  194  311  309   592
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.54 2.78 3.15 4.06 2.7 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.541029 4.151482 4.220672 4.230856 4.283320 4.383323 4.438286 4.473253
##  [2,] 2.775803 2.900045 3.195962 3.235202 3.297567 3.314718 3.354773 3.411198
##  [3,] 3.149332 3.176083 3.332833 3.392172 3.419672 3.596919 3.617902 3.675168
##  [4,] 4.059664 4.067759 4.291209 4.323342 4.395517 4.452250 4.524020 4.556114
##  [5,] 2.697392 3.599633 3.605568 3.670720 3.807332 3.890420 3.897629 3.908573
##  [6,] 3.947932 4.332159 4.820245 4.845608 4.864856 4.884269 4.936828 4.938594
##  [7,] 3.378475 3.622770 3.777268 3.886309 3.945826 3.968145 3.974977 3.984534
##  [8,] 3.418311 3.466023 3.758921 3.853933 3.875657 4.013946 4.033475 4.083185
##  [9,] 2.747345 2.822890 2.950760 2.974361 3.149429 3.163289 3.255711 3.294081
## [10,] 2.822432 3.114888 3.424771 3.453525 3.544157 3.553433 3.587427 3.600897
## [11,] 2.396558 2.816422 2.935804 3.021559 3.129580 3.174945 3.200948 3.237568
## [12,] 3.318162 3.660755 3.692126 3.729749 3.731091 3.748499 3.932257 3.965040
## [13,] 4.262442 5.094219 5.145122 5.161890 5.164985 5.209098 5.243460 5.322587
## [14,] 3.581189 3.898671 4.034297 4.087599 4.110172 4.131634 4.181593 4.296895
## [15,] 3.504714 3.681373 3.734036 3.789941 3.843100 3.935591 3.942463 3.982561
## [16,] 4.092943 4.184986 4.192058 4.199042 4.252569 4.395456 4.453105 4.484500
## [17,] 4.074709 4.677194 5.034077 5.203828 5.353293 5.433573 5.434077 5.599368
## [18,] 3.800335 3.832430 4.081626 4.109364 4.114174 4.116124 4.145820 4.285681
## [19,] 2.456418 2.506369 2.623666 2.943577 3.249856 3.275014 3.343591 3.394642
## [20,] 3.118686 3.385710 3.487595 3.526954 3.533406 3.549104 3.578584 3.605730
##           [,9]    [,10]
##  [1,] 4.481873 4.565029
##  [2,] 3.453370 3.460021
##  [3,] 3.683567 3.813584
##  [4,] 4.623213 4.626337
##  [5,] 3.912374 3.914122
##  [6,] 4.953250 5.028734
##  [7,] 4.015064 4.020086
##  [8,] 4.190173 4.248892
##  [9,] 3.299178 3.324928
## [10,] 3.643436 3.646429
## [11,] 3.246859 3.294610
## [12,] 4.028702 4.174727
## [13,] 5.352871 5.434153
## [14,] 4.301980 4.329732
## [15,] 4.035097 4.038634
## [16,] 4.699085 4.703711
## [17,] 5.655597 5.670642
## [18,] 4.312616 4.336576
## [19,] 3.402360 3.483540
## [20,] 3.616157 3.708658

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.IL… `pCREB(Yb176)Di.IL… `pBTK(Yb171)Di.IL… `pS6(Yb172)Di.IL7…
##                  <dbl>               <dbl>              <dbl>              <dbl>
##  1               0.920               1                  0.890              0.887
##  2               0.987               0.976              0.999              0.895
##  3               0.920               0.327              0.720              0.839
##  4               0.773               0.992              0.460              0.882
##  5               0.713               0.918              0.802              0.895
##  6               0.978               0.960              0.890              0.915
##  7               0.867               0.918              0.802              0.651
##  8               0.931               0.960              0.880              0.895
##  9               0.797               0.960              0.789              0.882
## 10               0.946               0.918              0.802              0.926
## # … with 990 more rows, and 30 more variables: 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>, pAKT(Tb159)Di.IL7.qvalue <dbl>,
## #   pBLNK(Gd160)Di.IL7.qvalue <dbl>, pP38(Tm169)Di.IL7.qvalue <dbl>,
## #   pSTAT5(Nd150)Di.IL7.qvalue <dbl>, pSyk(Dy162)Di.IL7.qvalue <dbl>,
## #   tIkBa(Er166)Di.IL7.qvalue <dbl>, pCrkL(Lu175)Di.IL7.change <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(I… `CD3(Cd114)Di`
##             <dbl>          <dbl>          <dbl>             <dbl>          <dbl>
##  1         0.907           1.46           1.81            -2.46            2.02 
##  2        -0.264           1.77           2.83            -1.57            0.912
##  3         0.296           1.00           1.28            -0.0860          0.506
##  4         0.264           1.63          -0.225            0.644           0.723
##  5         0.399           0.925          0.642           -1.27            0.664
##  6         0.264           1.95           2.52            -0.241           0.381
##  7         0.0136          1.41           0.705            0.683           1.57 
##  8        -0.323          -0.151          1.22            -0.215           1.58 
##  9        -0.142           0.598         -0.200           -0.616           1.11 
## 10        -0.729           1.76           1.10             0.560           1.17 
## # … with 20 more rows, and 46 more variables: 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>,
## #   PreBCR(Ho165)Di <dbl>, CD43(Er167)Di <dbl>, CD38(Er168)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.217 0.276 0.258 0.212 0.252 ...