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] 911 385 684 406 261 706 922 368 553 49 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  911  787  619  622  543  451  209  776  620   987
##  [2,]  385  376  521    9  553   74  341  601   55   707
##  [3,]  684   56  692  572  504  470  673  685  302   984
##  [4,]  406   41  241  304  705  290  170  731  433    70
##  [5,]  261  378  466   36  958   76  391  978  192   937
##  [6,]  706  458  934  809  504   33  292  314  776   433
##  [7,]  922  279  344  391  607  370  479  539  168   170
##  [8,]  368  996  726  170  279  830  485  270  584   824
##  [9,]  553  385  670  464  376  521  920    2   74   948
## [10,]   49  817  824   41  279  368  383  634  270   324
## [11,]  405  343  895  114  104  408  719  230  886    73
## [12,]  707  385   98  341  376  521  601   74  905     2
## [13,]   54  264  448  383   10  875  485  685  692    56
## [14,]  371  622  542  208  787  630  112  731  741   552
## [15,]  495  684  417  238  442  692  470  891  766   461
## [16,]  688   88  741  123  208   40  152  378  520   120
## [17,]  692  178  226  238  373  862   92  540  860   867
## [18,]  553   98  362    9  670  464  718  521  716   376
## [19,]  751  955  238  737  681  303  373   92  608   692
## [20,]  903   16  987  212  896  837  845   39  524   688
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.08 3.68 2.8 2.99 4.25 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]
##  [1,] 3.075207 3.957256 4.021719 4.173033 4.262167 4.276064 4.453382
##  [2,] 3.679249 3.942074 3.943316 3.986689 4.019910 4.146279 4.236226
##  [3,] 2.804204 2.938322 2.957079 2.983640 3.008166 3.145059 3.163343
##  [4,] 2.990060 3.173675 3.221582 3.224588 3.316178 3.400965 3.585750
##  [5,] 4.248508 4.338485 4.564162 4.633713 4.720591 4.819444 4.865806
##  [6,] 2.508237 2.740607 3.046171 3.055956 3.066226 3.219435 3.273305
##  [7,] 2.471200 2.609882 2.650510 3.105991 3.248058 3.292818 3.300390
##  [8,] 1.977624 2.696946 2.733886 2.767854 2.846347 2.854364 2.889148
##  [9,] 2.866492 3.265561 3.288425 3.475343 3.613709 3.750875 3.914504
## [10,] 2.326419 2.620280 2.731706 2.734650 2.754043 2.776866 2.964534
## [11,] 3.977072 4.313911 4.317570 4.359265 4.389687 4.392127 4.393451
## [12,] 4.319406 4.882209 4.888400 5.044177 5.080887 5.156047 5.171896
## [13,] 3.569078 3.605569 3.615787 3.640033 3.721144 3.743773 3.775224
## [14,] 3.467530 3.759096 3.800485 3.823090 3.888237 4.034886 4.049032
## [15,] 3.429947 3.508064 3.552670 3.585326 3.757137 3.769125 3.785346
## [16,] 3.660582 3.792651 3.944318 4.103791 4.193477 4.292625 4.309653
## [17,] 2.904092 3.256243 3.324121 3.530851 3.548810 3.636062 3.669106
## [18,] 4.510140 4.565450 4.738522 4.741335 4.743826 4.751820 4.779681
## [19,] 2.680930 2.742776 2.825349 2.854281 2.895732 2.930088 2.943991
## [20,] 4.734836 5.090538 5.148678 5.576074 5.586942 6.044643 6.046008
##           [,8]     [,9]    [,10]
##  [1,] 4.465567 4.501044 4.554661
##  [2,] 4.456942 4.473370 4.479839
##  [3,] 3.182114 3.185418 3.226838
##  [4,] 3.605184 3.628740 3.693694
##  [5,] 4.915030 4.931211 4.932820
##  [6,] 3.287488 3.390128 3.393762
##  [7,] 3.338053 3.382955 3.384764
##  [8,] 2.976749 2.986093 3.013004
##  [9,] 3.986689 4.066634 4.215118
## [10,] 2.977156 2.977382 2.990896
## [11,] 4.400750 4.401681 4.409589
## [12,] 5.234075 5.268360 5.268617
## [13,] 3.791006 3.805600 3.850616
## [14,] 4.080342 4.106147 4.147321
## [15,] 3.799843 3.801770 3.803127
## [16,] 4.323675 4.326694 4.385007
## [17,] 3.669776 3.709151 3.718053
## [18,] 4.903699 4.905895 5.049838
## [19,] 2.970063 3.027255 3.042406
## [20,] 6.071963 6.087156 6.239203

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            0.752            1                0.829            0.999
##  2            0.914            0.955            0.859            0.960
##  3            0.179            0.940            0.832            0.978
##  4            0.362            1                1                0.960
##  5            0.705            0.984            0.901            1    
##  6            0.863            1                0.933            0.960
##  7            0.504            1                0.829            1    
##  8            0.612            1                0.970            0.971
##  9            0.797            0.947            0.957            1    
## 10            0.573            0.927            0.941            0.960
## # … 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.460         -0.201         -0.460           -0.741 
##  2         0.787         -0.239          0.0882           0.0555
##  3        -0.146         -0.139         -0.167           -0.937 
##  4        -0.0705        -0.0972        -0.0117          -1.53  
##  5        -0.409         -0.277         -0.137           -1.11  
##  6        -0.149         -0.103         -0.117           -0.490 
##  7        -0.0207         0.275         -0.115           -0.774 
##  8        -0.201         -0.250         -0.0683          -1.41  
##  9        -0.0623        -0.108          0.180           -0.344 
## 10        -0.234         -0.0953        -0.451           -0.721 
## # … 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.216 0.213 0.297 0.265 0.201 ...