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 420 601 208 586 11 622 62 578 107 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  635  158  853  713  551  433  544  722  975   826
##  [2,]  420  218  774   77  237  589  153  418  597   546
##  [3,]  601  488  941  177  496  893  884  466  647    97
##  [4,]  208  813  572  481   12  161  367  323   49   411
##  [5,]  586  124  451  447   21  235  544  788  206   473
##  [6,]   11  846  547   90  118  387  889  931  778   804
##  [7,]  622  829   42  492  771  413  311  984  858   831
##  [8,]   62  544  315  551  158  826  975  853  579   910
##  [9,]  578  397  798  751  824  331  985  838  588   925
## [10,]  107  136  219  695  429  813   87  786  834   235
## [11,]  647  420  119  519    6  520  724  740  402   846
## [12,]  208  932  910    4  788  563  411  617  481   234
## [13,]  165  822  474  901  860  625  271  114  678   691
## [14,]  454  426   87  244  323  219  176  445  706   205
## [15,]  788  158  551  826  544   69   49  641  956   737
## [16,]  359  950  611  993  718  501  478  866   39   984
## [17,]  930  744  625  925   64  838  482  239  290    86
## [18,]  792  726  993  120  353  207  871  717  718   442
## [19,]  496  941  177  601  880  861  464  893  194   741
## [20,]  400  678  814  901  838  985  754  698  798   354
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.34 3.06 5.13 2.24 3.09 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.337286 3.395826 3.578798 3.622334 3.717202 3.788450 3.847290 3.854464
##  [2,] 3.063779 3.229940 3.522686 3.620843 3.706292 3.745903 3.781679 3.842906
##  [3,] 5.131341 5.145582 5.162997 5.280490 5.314025 5.346165 5.423181 5.520873
##  [4,] 2.242857 2.638514 2.683325 2.706844 2.724573 2.806825 2.895198 3.017756
##  [5,] 3.090384 3.110235 3.611978 3.811505 3.849901 3.873507 3.937630 4.014342
##  [6,] 3.576664 3.968248 3.985976 4.096753 4.156860 4.215418 4.235535 4.278129
##  [7,] 3.297777 3.950556 4.390887 4.402406 4.517866 4.529301 4.624106 4.698552
##  [8,] 2.544907 2.904092 3.436294 3.439476 3.530851 3.566553 3.634390 3.634820
##  [9,] 2.478696 2.527205 2.560700 2.631096 2.718144 2.784622 2.813570 2.854047
## [10,] 3.324877 3.327581 3.470366 3.513273 3.533073 3.561370 3.699885 3.785623
## [11,] 3.483282 3.516798 3.562495 3.566005 3.576664 3.670245 3.686823 3.754792
## [12,] 2.396287 2.623729 2.702809 2.724573 2.735809 2.855423 2.889691 2.932616
## [13,] 3.666321 3.685552 3.732034 3.875570 3.905678 3.922941 3.943375 3.982846
## [14,] 2.969913 3.167213 3.207533 3.222807 3.283637 3.338601 3.370018 3.412866
## [15,] 2.508443 2.802788 2.858035 2.893614 3.078036 3.126057 3.129184 3.155397
## [16,] 3.965085 4.067644 4.080168 4.170941 4.178009 4.220931 4.285856 4.425686
## [17,] 3.341492 3.400104 3.450653 3.467755 3.581125 3.639763 3.674263 3.713939
## [18,] 4.079191 4.104734 4.468533 4.497335 4.541565 4.730356 4.826238 4.847785
## [19,] 4.032526 4.734903 4.872277 4.923651 5.158796 5.234448 5.237000 5.412234
## [20,] 3.324507 3.392146 3.503116 3.587884 3.614842 3.640331 3.671128 3.682761
##           [,9]    [,10]
##  [1,] 3.868582 3.874966
##  [2,] 3.844232 3.896887
##  [3,] 5.629140 5.666921
##  [4,] 3.133040 3.133930
##  [5,] 4.187289 4.214135
##  [6,] 4.286119 4.387900
##  [7,] 4.725140 4.741643
##  [8,] 3.636062 3.656537
##  [9,] 2.874898 2.923918
## [10,] 3.802806 3.822110
## [11,] 3.778793 3.809381
## [12,] 2.932717 2.961418
## [13,] 4.023391 4.057174
## [14,] 3.493995 3.509718
## [15,] 3.233042 3.278267
## [16,] 4.492742 4.511457
## [17,] 3.748484 3.792614
## [18,] 4.857236 4.861939
## [19,] 5.444645 5.519559
## [20,] 3.691471 3.740260

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.qval…` `pCREB(Yb176)D…` `pBTK(Yb171)Di…` `pS6(Yb172)Di.…`
##                         <dbl>            <dbl>            <dbl>            <dbl>
##  1                      0.978            0.976            0.972            0.953
##  2                      0.990            0.578            0.972            0.781
##  3                      0.856            0.918            0.858            0.863
##  4                      0.971            0.980            0.972            0.974
##  5                      0.695            1                0.986            0.974
##  6                      0.695            0.994            0.972            0.934
##  7                      0.777            0.976            0.972            0.855
##  8                      0.957            0.964            0.858            0.810
##  9                      0.955            0.944            0.972            0.835
## 10                      0.951            0.867            1                0.784
## # … 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>, …

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(…` `CD3(Cd114)Di`
##             <dbl>          <dbl>          <dbl>             <dbl>          <dbl>
##  1        -0.647         -0.668          0.888             -1.43         -1.11  
##  2        -0.234         -0.0633        -0.104             -0.579        -0.142 
##  3        -0.413         -0.0340         0.139             -0.193        -0.464 
##  4        -0.159         -0.235         -0.0423            -1.17         -0.193 
##  5        -0.218         -0.0910        -0.198              0.145        -0.417 
##  6        -0.143         -0.484         -0.289             -0.892        -0.0153
##  7        -0.328         -0.389          0.283             -0.268         0.214 
##  8        -0.0699        -0.221          0.959              0.396        -0.225 
##  9        -1.26          -0.980         -0.244             -1.77         -1.01  
## 10        -0.134         -0.122         -0.217             -0.641        -0.228 
## # … 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.252 0.254 0.173 0.321 0.241 ...