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"          
##  [3] "CD3(Cd112)Di"           "CD235-61-7-15(In113)Di"
##  [5] "CD3(Cd114)Di"           "CD45(In115)Di"         
##  [7] "CD19(Nd142)Di"          "CD22(Nd143)Di"         
##  [9] "IgD(Nd145)Di"           "CD79b(Nd146)Di"        
## [11] "CD20(Sm147)Di"          "CD34(Nd148)Di"         
## [13] "CD179a(Sm149)Di"        "CD72(Eu151)Di"         
## [15] "IgM(Eu153)Di"           "Kappa(Sm154)Di"        
## [17] "CD10(Gd156)Di"          "Lambda(Gd157)Di"       
## [19] "CD24(Dy161)Di"          "TdT(Dy163)Di"          
## [21] "Rag1(Dy164)Di"          "PreBCR(Ho165)Di"       
## [23] "CD43(Er167)Di"          "CD38(Er168)Di"         
## [25] "CD40(Er170)Di"          "CD33(Yb173)Di"         
## [27] "HLA-DR(Yb174)Di"       
## 
## $functional
##  [1] "pCrkL(Lu175)Di"  "pCREB(Yb176)Di"  "pBTK(Yb171)Di"  
##  [4] "pS6(Yb172)Di"    "cPARP(La139)Di"  "pPLCg2(Pr141)Di"
##  [7] "pSrc(Nd144)Di"   "Ki67(Sm152)Di"   "pErk12(Gd155)Di"
## [10] "pSTAT3(Gd158)Di" "pAKT(Tb159)Di"   "pBLNK(Gd160)Di" 
## [13] "pP38(Tm169)Di"   "pSTAT5(Nd150)Di" "pSyk(Dy162)Di"  
## [16] "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] 703 615 663 737 881 934 499 760 546 561 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  703  369  386  245  734  752  469  602  160   845
##  [2,]  615  751  255  164  725  362  994  237  583   815
##  [3,]  663  956  935  821  667  680   89  674  859   246
##  [4,]  737  912  640  144  101   74  344  204  748   577
##  [5,]  881  703   33  333  757   27  752  368  791   283
##  [6,]  934  762  766   12  933  342    9  868  906   650
##  [7,]  499  753  399   42  757  783  243  482  108    65
##  [8,]  760  604  737  898  748  302  891  344  908   316
##  [9,]  546  906  529  549  211  853   12  762  947   537
## [10,]  561  254  611  444  735   58  819  760  803    72
## [11,]  154  600  729  588  476  836  575  275  623   930
## [12,]  537    9  455  853   75  440  549  601   19    95
## [13,]  496  617  814  569  343  164  579  994  423   182
## [14,]  530  917  855  552  749  559  191  887  522   949
## [15,]  129  476   53  530  729  787  943  365  528   623
## [16,]  352  787  750   26  616  338  530   45  168   365
## [17,]  638  779  716  974  636  682  306  224  217   742
## [18,]  241  201  239  596  227  784   52  209  630    41
## [19,]  314  529  906  989   12    9  918  217  131   651
## [20,]  413  930  530  938  686  441  600  352  928   750
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.88 3.27 4.35 3.21 2.81 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]
##  [1,] 2.883953 2.920141 2.964791 3.026797 3.053570 3.135941 3.180808
##  [2,] 3.268422 3.517126 4.102763 4.228391 4.231963 4.313544 4.327914
##  [3,] 4.346599 4.442967 4.848014 4.920839 4.969283 4.970772 5.004522
##  [4,] 3.205816 3.242520 3.475945 3.640327 3.876835 3.881543 3.912304
##  [5,] 2.814795 2.852736 2.911103 2.932717 2.973127 3.050343 3.099217
##  [6,] 4.165912 4.208693 4.226428 4.310194 4.391947 4.395456 4.474153
##  [7,] 3.090906 3.807217 4.047665 4.233044 4.238328 4.270631 4.530476
##  [8,] 3.238448 3.468780 3.518805 3.531683 3.582698 3.617614 3.654889
##  [9,] 2.612928 2.724873 2.775316 2.928508 2.941141 3.009110 3.016592
## [10,] 3.653285 3.700107 3.837157 3.911224 3.917315 3.941473 4.033796
## [11,] 4.482201 4.543668 4.698699 4.868759 4.967538 5.178057 5.335642
## [12,] 3.001880 3.016592 3.118466 3.181838 3.219256 3.226009 3.356572
## [13,] 4.164679 4.372719 4.526836 4.548545 4.567160 4.636970 4.660180
## [14,] 3.600701 3.861832 3.884845 4.061345 4.131129 4.139887 4.155880
## [15,] 3.645440 4.392323 4.398688 4.610302 4.723101 4.802364 4.891938
## [16,] 3.849542 4.028213 4.078992 4.159745 4.359839 4.386872 4.387797
## [17,] 3.085945 3.112783 3.156877 3.169237 3.244668 3.254884 3.265306
## [18,] 3.045342 3.272169 3.592151 3.780027 4.020970 4.132393 4.143439
## [19,] 3.267832 3.276624 3.277552 3.369748 3.433823 3.537103 3.597653
## [20,] 3.339597 3.643471 3.666349 3.708491 3.728559 3.969359 4.005788
##           [,8]     [,9]    [,10]
##  [1,] 3.225316 3.290506 3.330900
##  [2,] 4.332077 4.356062 4.446324
##  [3,] 5.039408 5.043362 5.061510
##  [4,] 3.947851 3.961548 3.975931
##  [5,] 3.198756 3.246064 3.297719
##  [6,] 4.499441 4.540173 4.549172
##  [7,] 4.592534 4.602821 4.623483
##  [8,] 3.776265 3.807573 3.856978
##  [9,] 3.046709 3.180061 3.280049
## [10,] 4.205665 4.257668 4.281344
## [11,] 5.465106 5.488455 5.499684
## [12,] 3.376996 3.433823 3.470655
## [13,] 4.708065 4.742229 4.751128
## [14,] 4.319296 4.369172 4.388089
## [15,] 4.892778 4.940585 4.978660
## [16,] 4.493017 4.494916 4.531708
## [17,] 3.270347 3.271496 3.312614
## [18,] 4.226182 4.280701 4.289129
## [19,] 3.615454 3.643005 3.673685
## [20,] 4.128160 4.183223 4.190485

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 x 34
##    `pCrkL(Lu175)Di… `pCREB(Yb176)Di… `pBTK(Yb171)Di.… `pS6(Yb172)Di.I…
##               <dbl>            <dbl>            <dbl>            <dbl>
##  1            1                    1            1                0.699
##  2            1                    1            1                0.672
##  3            1                    1            1                0.545
##  4            1                    1            1                0.545
##  5            1                    1            1                1    
##  6            0.948                1            1                0.845
##  7            1                    1            0.999            0.976
##  8            0.948                1            1                0.300
##  9            1                    1            1                0.924
## 10            0.978                1            1                0.602
## # … 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>,
## #   `pCREB(Yb176)Di.IL7.change` <dbl>, `pBTK(Yb171)Di.IL7.change` <dbl>,
## #   `pS6(Yb172)Di.IL7.change` <dbl>, `cPARP(La139)Di.IL7.change` <dbl>,
## #   `pPLCg2(Pr141)Di.IL7.change` <dbl>, `pSrc(Nd144)Di.IL7.change` <dbl>,
## #   `Ki67(Sm152)Di.IL7.change` <dbl>, `pErk12(Gd155)Di.IL7.change` <dbl>,
## #   `pSTAT3(Gd158)Di.IL7.change` <dbl>, `pAKT(Tb159)Di.IL7.change` <dbl>,
## #   `pBLNK(Gd160)Di.IL7.change` <dbl>, `pP38(Tm169)Di.IL7.change` <dbl>,
## #   `pSTAT5(Nd150)Di.IL7.change` <dbl>, `pSyk(Dy162)Di.IL7.change` <dbl>,
## #   `tIkBa(Er166)Di.IL7.change` <dbl>, IL7.fraction.cond.2 <dbl>,
## #   density <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 x 51
##    `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(…
##             <dbl>          <dbl>          <dbl>            <dbl>
##  1         0.154         -0.0483          0.259           0.846 
##  2         0.523          0.752           0.664           0.820 
##  3        -0.0224        -0.156           1.50            0.356 
##  4         0.256         -0.104           1.02           -0.537 
##  5        -0.168         -0.0518         -0.321           0.161 
##  6         0.202         -0.0108          0.381           0.551 
##  7        -0.216         -0.157          -0.148           0.322 
##  8         0.296          1.00            1.28           -0.0860
##  9        -0.439         -0.422          -0.257          -0.478 
## 10        -0.165          1.48           -0.470          -0.545 
## # … with 20 more rows, and 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>, `PreBCR(Ho165)Di` <dbl>, `CD43(Er167)Di` <dbl>,
## #   `CD38(Er168)Di` <dbl>, `CD40(Er170)Di` <dbl>, `CD33(Yb173)Di` <dbl>,
## #   `HLA-DR(Yb174)Di` <dbl>, Time <dbl>, Cell_length <dbl>,
## #   `cPARP(La139)Di` <dbl>, `pPLCg2(Pr141)Di` <dbl>,
## #   `pSrc(Nd144)Di` <dbl>, `pSTAT5(Nd150)Di` <dbl>, `Ki67(Sm152)Di` <dbl>,
## #   `pErk12(Gd155)Di` <dbl>, `pSTAT3(Gd158)Di` <dbl>,
## #   `pAKT(Tb159)Di` <dbl>, `pBLNK(Gd160)Di` <dbl>, `pSyk(Dy162)Di` <dbl>,
## #   `tIkBa(Er166)Di` <dbl>, `pP38(Tm169)Di` <dbl>, `pBTK(Yb171)Di` <dbl>,
## #   `pS6(Yb172)Di` <dbl>, `pCrkL(Lu175)Di` <dbl>, `pCREB(Yb176)Di` <dbl>,
## #   `DNA1(Ir191)Di` <dbl>, `DNA2(Ir193)Di` <dbl>,
## #   `Viability1(Pt195)Di` <dbl>, `Viability2(Pt196)Di` <dbl>,
## #   wanderlust <dbl>, condition <chr>
# 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.298 0.223 0.194 0.249 0.302 ...