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] 957 435 988 659 563 714 541 655 411 374 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  957  335  758  314  848  700  769  605  511   665
##  [2,]  435  152  766  709  798  951   49  936  935   710
##  [3,]  988  906  385  219  465  582  569  552  498   353
##  [4,]  659  277  183  498  552  514  743  894  757   602
##  [5,]  563  106  856  470   21  742  960  545  287   455
##  [6,]  714  722  195  756  186  699  117   99  397   737
##  [7,]  541  235  189  643  616  441  220  394  973   359
##  [8,]  655   67  359  909  485  300  791  369  555   643
##  [9,]  411  899  605  386   84  336  261  947  813   344
## [10,]  374  366  576  104  433  882  440  580  763   393
## [11,]  552  832  864   98  894  183  353  514   31     4
## [12,]  357  962  615  381  942  266  482  850  791   515
## [13,]  772  592  486  745  562  664  758  646  961   583
## [14,]  423  656  952  324  859   91  829  445   20   517
## [15,]  592  800  583  490   13  934  772  714  840   558
## [16,]  402  907  564  223  226  983  497  176  628   977
## [17,]  725  770  631  314  689  393  580  213  215    83
## [18,]  120  746  811  190  639  434  897  766  248   999
## [19,]  491  715  316  869  309  226   44   71  834   862
## [20,]  445  233   77  517   91  612  473  462  769   423
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.07 3.38 3.3 3.31 3.12 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.065316 3.397660 3.437216 3.484488 3.553685 3.605198 3.611376 3.613591
##  [2,] 3.377001 3.505477 3.516507 3.542438 3.574119 3.607632 3.622953 3.668460
##  [3,] 3.297777 3.950556 4.178218 4.348766 4.390887 4.517866 4.518523 4.522039
##  [4,] 3.311331 3.339767 3.352972 3.390046 3.541037 3.625683 3.642903 3.687154
##  [5,] 3.122905 3.367718 3.475313 3.748741 3.752576 3.771008 3.863596 3.912126
##  [6,] 2.877301 3.061208 3.094085 3.148103 3.155099 3.199561 3.211280 3.218700
##  [7,] 3.900709 4.031615 4.370300 4.377665 4.533079 4.557106 4.571585 4.573960
##  [8,] 3.372035 3.378054 3.417346 3.489331 3.500729 3.507048 3.508005 3.512798
##  [9,] 2.740607 3.046171 3.101700 3.273305 3.285297 3.357260 3.393762 3.447957
## [10,] 4.430561 4.630947 4.673146 4.808819 4.952011 5.082232 5.083216 5.207046
## [11,] 3.395928 3.815551 3.986953 4.111590 4.117455 4.124254 4.140898 4.259279
## [12,] 2.102466 2.969952 3.090734 3.121466 3.129891 3.145059 3.193380 3.291733
## [13,] 2.382848 2.787093 2.816422 2.880935 2.887437 2.911389 2.963617 2.971006
## [14,] 7.031263 7.277038 7.884798 7.958540 8.151665 8.187366 8.216847 8.282400
## [15,] 3.524509 3.619611 3.697266 3.750548 3.794082 3.915970 3.976458 4.012887
## [16,] 4.138642 4.227155 4.456780 4.519103 4.781641 4.874225 4.914206 4.943459
## [17,] 2.306681 2.788849 3.124734 3.341306 3.377360 3.435826 3.458431 3.465291
## [18,] 2.994119 3.411322 3.782354 3.811089 3.880291 3.971995 4.028750 4.085494
## [19,] 3.707049 4.129631 4.178302 4.200740 4.366362 4.408984 4.602414 4.616120
## [20,] 4.012866 4.047417 4.093232 4.216919 4.362413 4.413734 4.465344 4.518486
##           [,9]    [,10]
##  [1,] 3.659387 3.665259
##  [2,] 3.726756 3.741228
##  [3,] 4.529301 4.624106
##  [4,] 3.696173 3.712973
##  [5,] 4.016532 4.021432
##  [6,] 3.235612 3.248603
##  [7,] 4.582614 4.605655
##  [8,] 3.553182 3.591305
##  [9,] 3.576495 3.606970
## [10,] 5.226651 5.229419
## [11,] 4.282645 4.302765
## [12,] 3.292718 3.314765
## [13,] 3.057822 3.060117
## [14,] 8.537099 8.647748
## [15,] 4.046038 4.125926
## [16,] 4.972149 4.985732
## [17,] 3.471657 3.473259
## [18,] 4.098819 4.122375
## [19,] 4.673308 4.686859
## [20,] 4.529640 4.552861

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.00                           1                  0.968
##  2                       0.982                          1                  0.748
##  3                       0.991                          1                  0.708
##  4                       0.982                          1                  0.887
##  5                       1                              1                  1    
##  6                       0.982                          1                  0.812
##  7                       0.982                          1                  0.926
##  8                       0.950                          1                  0.932
##  9                       0.991                          1                  0.690
## 10                       0.982                          1                  0.708
## # ℹ 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.188         -0.479          1.54                    -0.974 
##  2        -0.166         -0.476         -0.304                   -1.55  
##  3        -0.908         -0.813         -0.287                   -1.83  
##  4        -0.204         -0.0232        -0.140                    0.0694
##  5        -0.326         -0.0343         1.04                     0.157 
##  6        -0.262         -0.454         -0.110                   -0.447 
##  7        -0.349         -0.171         -0.0710                   0.432 
##  8        -0.0477        -0.0514        -0.0339                  -0.474 
##  9        -0.473         -0.140         -0.487                   -0.122 
## 10        -0.185          0.101         -0.121                   -0.442 
## # ℹ 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.267 0.259 0.211 0.253 0.243 ...