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] 898 107 790 756 790 796 476 907 846 282 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  898  938   87  892  544  894  742  994  192   155
##  [2,]  107  194  723  593  906  322  876  523   23    39
##  [3,]  790    8  907   57  210  308  802    5  454   360
##  [4,]  756  657  878  323  344  781  661  520  810   640
##  [5,]  790    3  958  454   91    8  907  308  357   210
##  [6,]  796  215  900  539  581  289   25  884  147   708
##  [7,]  476  908  520  765  678  120  953  456  962   957
##  [8,]  907  790    3  451  690   34  169  824  308   526
##  [9,]  846   86  722  928  191  436  557  823  569   626
## [10,]  282  107   71  360  876  369  593  194   23   726
## [11,]   86  559  104  503  392  157  626  580  170   185
## [12,]  340  741   60  385  430  510  279   93  622   186
## [13,]  977  647  728  181   52  401  115  361  512   879
## [14,]  616  452  716  695   24  652  858  302  170   928
## [15,]  718  229  641  231  434  235  477  864  333   701
## [16,]  908  499  875  957  274  120   38   96  520   392
## [17,]  267  898  747  275  662  825  892  154  634   707
## [18,]  893  711  566  971   85  888  986  877  395   221
## [19,]  971  138  494  954  912  840   34  986  314   711
## [20,]  870  722  375  312  823  883   56  357  733   476
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.21 3.31 2.89 3.4 3.65 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.207374 3.480259 3.599337 3.607481 3.731712 3.736488 3.753828 3.769329
##  [2,] 3.313186 3.854078 4.013864 4.044330 4.101227 4.117041 4.142000 4.163028
##  [3,] 2.885774 2.974989 3.139709 3.382013 3.487800 3.513848 3.579100 3.678958
##  [4,] 3.400857 3.406403 3.505638 3.555033 3.636335 3.639760 3.655267 3.796365
##  [5,] 3.654094 3.678958 3.875789 3.893263 4.023613 4.099897 4.116374 4.149045
##  [6,] 2.852235 3.265534 3.289348 3.516531 3.597213 3.623875 3.689459 3.725871
##  [7,] 2.690266 3.042903 3.085945 3.169237 3.320257 3.320706 3.329156 3.339701
##  [8,] 2.453201 2.967051 2.974989 3.104128 3.167093 3.189332 3.191709 3.206891
##  [9,] 3.721614 3.842150 3.860959 3.876880 3.900910 3.921677 3.937205 3.963566
## [10,] 3.732413 4.982312 5.064902 5.093407 5.219101 5.220080 5.291917 5.315915
## [11,] 3.327581 3.513273 3.533073 3.561370 3.676124 3.699885 3.785623 3.792353
## [12,] 4.209103 4.245753 4.393115 4.451718 4.656001 4.711475 4.724508 4.775084
## [13,] 3.220076 3.402167 3.425279 3.534260 3.593361 3.627505 3.660338 3.660755
## [14,] 3.510082 3.639932 3.656104 3.679785 3.716740 3.720733 3.912308 3.913865
## [15,] 3.562037 3.579958 3.713589 3.732426 3.750626 3.786793 3.796046 3.819036
## [16,] 2.751027 3.074504 3.120326 3.189666 3.206119 3.285611 3.315812 3.326392
## [17,] 2.183445 2.467871 2.548933 2.564574 2.624695 2.698236 2.840044 2.873438
## [18,] 3.831822 3.974534 4.049181 4.149865 4.341237 4.653371 4.664139 4.741421
## [19,] 4.181244 4.308902 4.516977 4.659617 4.740539 4.981736 4.982850 4.989053
## [20,] 3.629077 3.643743 3.653273 3.743585 3.747092 3.820737 3.850621 3.857852
##           [,9]    [,10]
##  [1,] 3.811161 3.855312
##  [2,] 4.191601 4.197095
##  [3,] 3.715603 3.740663
##  [4,] 3.800983 3.810580
##  [5,] 4.195495 4.208327
##  [6,] 3.764945 3.794277
##  [7,] 3.392670 3.421600
##  [8,] 3.218411 3.228432
##  [9,] 3.970520 4.036965
## [10,] 5.333945 5.387931
## [11,] 3.793545 3.822110
## [12,] 4.804957 4.851907
## [13,] 3.724096 3.762578
## [14,] 3.916840 3.924472
## [15,] 3.834848 3.871313
## [16,] 3.330879 3.334825
## [17,] 2.878373 2.878891
## [18,] 4.839777 4.985220
## [19,] 5.039231 5.049009
## [20,] 3.971378 3.981675

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.972               0.997              0.996              0.873
##  2               0.972               0.997              0.991              0.855
##  3               0.806               0.997              0.952              0.876
##  4               0.753               0.997              0.991              0.851
##  5               0.783               0.997              1                  0.851
##  6               0.932               0.997              0.991              0.924
##  7               0.772               0.997              0.931              0.851
##  8               0.923               0.997              0.996              0.851
##  9               0.997               0.997              0.952              0.977
## 10               0.997               0.997              0.996              0.943
## # … 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.104         -0.0145         0.276            -0.288         -0.628 
##  2        -0.0498         0.201          0.573            -0.355         -0.0410
##  3        -0.0603         0.798          0.460            -1.21          -0.694 
##  4        -0.254         -0.193         -0.121            -0.267         -0.0189
##  5         0.540         -0.181          0.0845            0.266         -0.235 
##  6        -0.166         -0.476         -0.304            -1.55          -0.101 
##  7        -0.0300        -0.0665         0.406             0.365         -0.135 
##  8        -0.204         -0.0232        -0.140             0.0694        -0.123 
##  9        -0.0559        -0.157          0.375            -0.539         -0.307 
## 10        -0.0905        -0.346         -0.635            -0.202         -0.920 
## # … 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.256 0.234 0.267 0.259 0.231 ...