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] 30 561 269 330 81 57 898 935 962 841 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]   30  264  499  849  861  120  164  677  933   606
##  [2,]  561  889  224  646  525   84  584  110  812   943
##  [3,]  269  737  504  642  717  206  794  554  769   373
##  [4,]  330  354  252  906  428  952  142  987  394   487
##  [5,]   81   84  943  398  274  443   54  740   27   472
##  [6,]   57  496  947  349  416  589  495  200  844   444
##  [7,]  898  191  734  826  338  387   42  785  279   391
##  [8,]  935  580  371   22  850  573  367  636  381   920
##  [9,]  962   96  961  335  780  845  753  752  469   667
## [10,]  841  793  861  784  561  247  216  505  824     1
## [11,]  313  643  587  667  335  943  327  372  974   902
## [12,]  895  492  666  279  830   42  898  671  623   191
## [13,]  196  828  911   20  119  904  681  338  121   611
## [14,]  117  836  922  954  326  514  527  236  264   929
## [15,]  744  394  942  969  550   83  169  718   35    29
## [16,]  873  647  850  935   22  494  573  436  661   512
## [17,]  609  482  135  261  933  598  123  146   14   395
## [18,]  925  606  874  594  672  933  425  437   30   325
## [19,]  321  236  138  931  371  742  292  495  211   661
## [20,]  681  830  215  828  196  904  789  506   13   121
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.29 3.4 4.36 2.7 3.44 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.288550 3.000608 3.003486 3.044186 3.061991 3.109655 3.120947 3.143332
##  [2,] 3.399359 3.590860 3.611403 3.614056 3.635727 3.651106 3.738744 3.751228
##  [3,] 4.364293 4.453823 4.484552 4.529442 4.531635 4.603279 4.644896 4.653877
##  [4,] 2.696517 3.045004 3.228404 3.275969 3.371336 3.466070 3.481313 3.509693
##  [5,] 3.438753 3.547274 3.550283 3.574922 3.601996 3.622886 3.663286 3.665880
##  [6,] 2.899267 3.228795 3.236289 3.294599 3.349499 3.407082 3.426532 3.497627
##  [7,] 4.257666 4.947874 5.131117 5.163658 5.237404 5.323673 5.384779 5.470879
##  [8,] 2.917114 3.200399 3.226009 3.557851 3.612108 3.722776 3.786677 4.152940
##  [9,] 3.517126 3.827344 4.093881 4.102763 4.109884 4.138264 4.231963 4.243987
## [10,] 3.965962 4.003654 4.026565 4.088930 4.168260 4.186605 4.225033 4.235365
## [11,] 3.715483 3.774560 3.866266 4.166911 4.269376 4.396100 4.525753 4.592158
## [12,] 3.708601 3.884775 4.026843 4.052049 4.073326 4.124767 4.221158 4.266084
## [13,] 3.491925 3.688802 3.958095 4.057822 4.120862 4.163044 4.172630 4.205990
## [14,] 2.242857 2.549984 2.661766 2.669284 2.724573 2.768719 2.903233 3.017756
## [15,] 2.792933 3.177894 3.288704 3.351850 3.411552 3.464850 3.548353 3.592468
## [16,] 2.789617 2.958366 3.075730 3.432619 3.561533 3.631523 3.664861 3.682744
## [17,] 2.962943 3.015275 3.197231 3.294505 3.294976 3.369024 3.433291 3.526737
## [18,] 3.510986 3.640091 3.652262 3.811541 3.929153 3.966727 3.997528 4.029139
## [19,] 2.954442 3.082324 3.125124 3.190342 3.219256 3.324642 3.420620 3.434119
## [20,] 3.176084 3.580175 3.626039 3.633989 3.797314 3.899205 3.950938 3.973965
##           [,9]    [,10]
##  [1,] 3.149825 3.229093
##  [2,] 3.808707 3.829575
##  [3,] 4.678131 4.780792
##  [4,] 3.540702 3.577555
##  [5,] 3.692212 3.692374
##  [6,] 3.498950 3.589647
##  [7,] 5.485570 5.602948
##  [8,] 4.155591 4.251814
##  [9,] 4.313544 4.327914
## [10,] 4.259835 4.284406
## [11,] 4.668892 4.735616
## [12,] 4.293006 4.490877
## [13,] 4.281796 4.339715
## [14,] 3.140742 3.142932
## [15,] 3.653179 3.676636
## [16,] 3.683244 3.695702
## [17,] 3.555110 3.586548
## [18,] 4.038410 4.058102
## [19,] 3.453518 3.465046
## [20,] 4.057822 4.128345

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.752            0.959            0.581            1    
##  2                      0.957            0.959            0.758            0.938
##  3                      0.715            0.959            0.918            0.951
##  4                      1                0.971            0.616            1    
##  5                      0.926            0.971            0.557            0.938
##  6                      0.752            0.935            0.790            0.692
##  7                      0.956            0.991            0.980            0.999
##  8                      0.960            0.979            0.578            0.781
##  9                      0.960            0.877            0.578            0.938
## 10                      0.902            0.972            0.803            0.951
## # … 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.147          -0.264         -0.845            -1.25            0.131
##  2       -0.768          -0.580         -0.264            -1.63           -0.386
##  3       -0.00552        -0.187         -0.142            -1.89            0.102
##  4       -0.161          -0.0198        -0.339            -1.10           -0.419
##  5       -0.357          -0.0968        -0.253            -0.233          -0.457
##  6       -0.277          -0.287         -0.636            -0.0210         -0.484
##  7       -0.174          -0.0635        -0.219            -0.776          -0.391
##  8       -0.126          -0.0303        -0.481            -2.32           -0.915
##  9       -0.0964         -0.0378         0.0962            0.134          -0.411
## 10        0.360          -0.113          0.894            -0.315          -0.145
## # … 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.303 0.255 0.203 0.274 0.262 ...