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] 660 570 342 898 835 943 61 673 771 362 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  660  455  330  279  122  982  599  102  670   373
##  [2,]  570  732  639  609  634  328  946  272  539   978
##  [3,]  342  995  296  205  831   26  439  593   51   785
##  [4,]  898  825  614  879  569   32  157  537  973   317
##  [5,]  835  121  348  718  156  216  687  271  648   604
##  [6,]  943  500  691  512  719  879  101  902  743    16
##  [7,]   61  177  903  938  904  443  680  976   67   932
##  [8,]  673   40  574  271  455  114  156  955   25   102
##  [9,]  771  236  889  112  291  593  524  855  795   941
## [10,]  362  649  625  858  100  295  152  717  710   937
## [11,]  251  622  544  872  789  701  276  857  884   184
## [12,]  945  393  535  815  258  146  523  663   18   621
## [13,]  488  871  537  815  817  661  847  145  317    29
## [14,]  824  253  693  264  204  849  680  766  299   816
## [15,]  702  291   35  634  795  319  316  771  901    51
## [16,]  185  621   81    6  289  309  412  968  719   728
## [17,]  698  594  278  685  343  965  861  683  338   323
## [18,]  927  815  617  745  902  535  873  871  154    13
## [19,]  510  175  257  157  950   43  552  635  165   536
## [20,]  622  749  500  610  313  743  876   27  736   333
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.16 3.7 3.42 2.78 3.04 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]
##  [1,] 3.163055 3.674974 3.678692 3.994928 4.009643 4.103495 4.125000
##  [2,] 3.699966 3.845257 4.089461 4.091291 4.095246 4.108874 4.369179
##  [3,] 3.419116 3.626014 4.277316 4.286301 4.339516 4.430295 4.484132
##  [4,] 2.781732 2.935804 3.051874 3.129580 3.131815 3.139441 3.237568
##  [5,] 3.037948 3.302855 3.344558 3.382285 3.419764 3.453594 3.460411
##  [6,] 2.567725 2.732229 2.764853 2.769046 2.769133 2.776092 2.826313
##  [7,] 3.442041 3.701995 3.819065 3.840330 3.879517 3.939425 4.036865
##  [8,] 2.485762 3.154017 3.372972 3.429964 3.506096 3.527210 3.660883
##  [9,] 3.896089 4.161308 4.428155 4.444999 4.664446 4.706743 4.726543
## [10,] 4.108895 4.257397 4.360755 4.365505 4.437687 4.477520 4.479676
## [11,] 4.371005 4.375177 4.386573 4.437174 4.471504 4.510921 4.542604
## [12,] 3.044497 3.169950 3.223405 3.246766 3.289788 3.454380 3.476104
## [13,] 2.289549 2.710660 2.767685 2.817048 2.916928 2.946039 2.967586
## [14,] 3.556735 3.630682 3.632301 3.712904 3.761041 3.784941 3.871217
## [15,] 3.708601 3.884775 3.960447 4.028409 4.073326 4.124767 4.156030
## [16,] 2.850939 2.880144 2.919602 2.931802 2.976749 2.977181 2.977382
## [17,] 3.906039 4.012393 4.072270 4.079715 4.573888 4.690086 4.867458
## [18,] 2.394399 2.778345 2.788882 2.839030 2.921404 2.997739 3.024625
## [19,] 2.739680 2.799856 2.948125 3.093891 3.114458 3.164888 3.190506
## [20,] 3.527260 3.536258 3.538865 3.552419 3.559358 3.615154 3.740749
##           [,8]     [,9]    [,10]
##  [1,] 4.149065 4.161751 4.203414
##  [2,] 4.382126 4.465774 4.502410
##  [3,] 4.564862 4.712444 4.769048
##  [4,] 3.261503 3.294610 3.320959
##  [5,] 3.573983 3.624240 3.653846
##  [6,] 2.873548 2.892904 2.931802
##  [7,] 4.076319 4.077486 4.215599
##  [8,] 3.669237 3.707808 3.742693
##  [9,] 4.804098 4.834206 4.856048
## [10,] 4.490707 4.491358 4.515552
## [11,] 4.587073 4.636025 4.638315
## [12,] 3.569579 3.627767 3.635642
## [13,] 3.100222 3.107892 3.144814
## [14,] 3.882875 3.926375 3.960578
## [15,] 4.221158 4.396115 4.490877
## [16,] 3.087312 3.092471 3.129033
## [17,] 4.877204 5.018824 5.072023
## [18,] 3.142601 3.287488 3.322590
## [19,] 3.239989 3.242177 3.256460
## [20,] 3.816203 3.823781 3.869796

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.964            0.945            0.877            0.947
##  2            0.998            1                0.490            1    
##  3            0.998            0.945            0.397            0.980
##  4            0.998            0.972            0.889            0.986
##  5            0.998            0.945            0.864            1    
##  6            0.998            0.945            0.940            0.947
##  7            0.998            1                0.541            0.980
##  8            0.998            0.945            0.919            0.947
##  9            0.998            0.945            0.998            0.947
## 10            0.998            0.931            0.515            0.982
## # … 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.131          -0.106         -0.294             0.683
##  2       -0.506          -0.686         -1.31             -0.265
##  3        0.540          -0.181          0.0845            0.266
##  4       -0.254          -0.193         -0.121            -0.267
##  5       -0.258          -0.304         -0.192            -0.370
##  6       -0.263          -0.0122        -0.107             0.281
##  7       -0.113          -0.176          0.456            -0.695
##  8       -0.00923        -0.0951        -0.0519           -0.802
##  9       -0.213           0.314         -0.206            -0.502
## 10       -0.156          -0.380         -0.384             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.235 0.221 0.21 0.294 0.263 ...