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] 635 389 82 702 805 129 604 389 431 948 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  635  705  158  551  968  480  643  118  694   268
##  [2,]  389  784  537   85  815    8  178  315  603   253
##  [3,]   82  185  633  263  763  224  557  528  814   239
##  [4,]  702   77  182  156  566   75  381  951  541   683
##  [5,]  805   83  775  745  634   45  921  373  287   992
##  [6,]  129  345   48  662  850  408  446  770  739   670
##  [7,]  604  300  917  489  873  211  357  775  757   734
##  [8,]  389  784    2  136  537  905  253  343  437   972
##  [9,]  431  467  948  657  796  959  797  729  320   969
## [10,]  948  447  320  540  196  367  796  558  310   431
## [11,]  843  364  435  934  476  601  694  801  912   880
## [12,]  504  942  260  636  937   80   18  382  743   419
## [13,]   58  315  311   24  931  286  258  530  711    66
## [14,]  976  588   37  714  390  268  364  842  541   685
## [15,]  425  870  411   42  197  397  849  272  262   415
## [16,]  353   64  269  381  683  771  289  980   35   679
## [17,]  110  952  719  347   73  670  847  201  930   532
## [18,]    2  537  905  260  603   85  460  437  784   343
## [19,]  268  390  217  980  635  777  465  197  394   195
## [20,]  783  823  115  468  258  806  872  336  833    21
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.31 2.82 2.37 3.4 2.98 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.306132 3.495325 3.528192 3.582077 3.584387 3.591139 3.678692 3.697357
##  [2,] 2.821755 2.912047 3.001880 3.016592 3.181838 3.226009 3.362216 3.433823
##  [3,] 2.373526 2.839894 2.960747 2.998301 3.028863 3.056242 3.086590 3.121995
##  [4,] 3.395493 3.514509 3.544002 3.642016 3.652081 3.765027 3.912850 3.968964
##  [5,] 2.978653 3.287994 3.328334 3.341226 3.378307 3.405819 3.431261 3.455521
##  [6,] 3.739465 3.749448 3.826477 3.840959 3.863303 3.864758 3.891089 3.947432
##  [7,] 2.608074 2.767854 2.793839 2.806661 3.130170 3.151570 3.167068 3.170648
##  [8,] 2.902729 2.917114 3.226009 3.545225 3.601325 3.807435 3.856667 4.155591
##  [9,] 3.390046 3.404682 3.642903 3.659687 3.696173 3.712973 3.731367 3.757619
## [10,] 4.028213 4.159745 4.211292 4.398015 4.488994 4.494916 4.568916 4.653132
## [11,] 2.811223 2.856932 2.864140 2.884994 2.946328 3.048251 3.080909 3.105949
## [12,] 4.785187 4.789237 5.176979 5.330974 5.650293 5.661905 5.723587 5.863151
## [13,] 4.172872 4.215889 4.272803 4.321498 4.365737 4.468188 4.600322 4.619353
## [14,] 3.290888 3.911146 3.926375 3.955070 3.983958 4.119979 4.138253 4.182650
## [15,] 3.218228 3.372553 3.472081 3.569087 3.599660 3.617268 3.635197 3.726114
## [16,] 3.233651 3.256717 3.259101 3.261762 3.609914 3.622210 3.666321 3.687049
## [17,] 4.516977 4.659617 4.740539 4.872632 4.898085 4.923243 4.981736 4.982850
## [18,] 3.719667 3.898985 4.176736 4.204732 4.291213 4.313618 4.378579 4.436698
## [19,] 3.422646 3.647824 3.978432 3.993102 4.012787 4.014178 4.170238 4.258487
## [20,] 3.779581 3.975874 4.018055 4.020621 4.044881 4.071587 4.186337 4.213175
##           [,9]    [,10]
##  [1,] 3.697649 3.734023
##  [2,] 3.483265 3.490977
##  [3,] 3.143491 3.205352
##  [4,] 3.984562 4.017990
##  [5,] 3.463302 3.475804
##  [6,] 3.984764 4.056766
##  [7,] 3.189350 3.190156
##  [8,] 4.187781 4.322580
##  [9,] 3.970345 3.970788
## [10,] 4.721544 4.747459
## [11,] 3.114853 3.143604
## [12,] 5.919405 5.944053
## [13,] 4.634538 4.638489
## [14,] 4.215407 4.233163
## [15,] 3.862350 3.882210
## [16,] 3.705089 3.791091
## [17,] 4.989053 5.049009
## [18,] 4.441487 4.460467
## [19,] 4.317486 4.414417
## [20,] 4.213938 4.262022

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.996                          1                  0.724
##  2                       0.864                          1                  0.810
##  3                       0.559                          1                  0.641
##  4                       0.953                          1                  0.807
##  5                       0.947                          1                  0.887
##  6                       0.529                          1                  0.613
##  7                       0.357                          1                  0.701
##  8                       0.970                          1                  1    
##  9                       0.996                          1                  0.952
## 10                       0.851                          1                  0.936
## # ℹ 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.218          -0.233         -0.0306                   -1.52 
##  2       -0.147          -0.264         -0.845                    -1.25 
##  3       -0.00552        -0.187         -0.142                    -1.89 
##  4       -0.363          -0.455         -0.644                    -0.609
##  5       -0.185          -0.420         -1.34                     -1.04 
##  6       -0.484          -0.309         -0.582                    -0.901
##  7       -0.0612         -0.199         -0.499                    -0.615
##  8       -0.0964         -0.0378         0.0962                    0.134
##  9       -0.632          -0.155         -0.152                    -1.24 
## 10        0.345          -0.117         -0.121                     0.121
## # ℹ 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.263 0.266 0.305 0.24 0.282 ...