Step 2: The Scone Workflow

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] 309 873 240 865 638 38 389 980 495 968 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  309  855  887  141  329  395  435  474  685   924
##  [2,]  873  317  696   60  690  843  779   27  205   805
##  [3,]  240  227  236  976   78   38  107  714  303   671
##  [4,]  865  141   27  217  946  330  828  911  518   476
##  [5,]  638  455  704  982  441  524  228  388  832   128
##  [6,]   38   55  833  730   85  706  261  467  784   270
##  [7,]  389  805  188  937  297  102  530  334  713    88
##  [8,]  980   38  222  164  726  189  683  593  554   331
##  [9,]  495  117   85  839  863  773  554  333  574   137
## [10,]  968  915  456  607  914  417  288  894  776   239
## [11,]  910  111   98  783  630  846  675  592   75   239
## [12,]  442  738  595  294   39  285  470  974  510   754
## [13,]  423   23  512  785  209  595  355  487   39   121
## [14,]  863  270   65  653  117  523  929  784  302   345
## [15,]  638  771  407  128  741  167  455  704    5   578
## [16,]  267  658  729   28  949  221  510  634  311   272
## [17,]   72  159  503  592   58  637  100  910  354   324
## [18,]  718  868  174  616  502  639  422   88  905   873
## [19,]  914   72  968  284  218  585  829  520  327    75
## [20,]  156   27  808  551  977  796  865  651  454   235
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 5.39 2.66 2.97 2.94 3 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 5.390803 5.540123 5.818519 6.146132 6.171004 6.216003 6.242732 6.258208
##  [2,] 2.663107 3.117694 3.323556 3.331109 3.379523 3.402983 3.403950 3.434218
##  [3,] 2.969913 3.207533 3.222807 3.260968 3.370018 3.412866 3.509718 3.582781
##  [4,] 2.936871 3.503581 3.589198 3.615391 3.704460 3.812188 3.822302 3.926506
##  [5,] 2.999932 3.006798 3.223405 3.246766 3.320653 3.569579 3.603670 3.667501
##  [6,] 4.543026 4.678576 4.748999 4.927525 4.938888 5.004810 5.011992 5.024494
##  [7,] 3.229654 3.417045 3.446511 3.581236 3.585237 3.628094 3.641761 3.656716
##  [8,] 4.383755 4.466796 4.564162 4.650775 4.709518 4.712367 4.720591 4.767499
##  [9,] 2.735375 3.050343 3.109112 3.146573 3.148475 3.254967 3.264843 3.304042
## [10,] 3.073766 3.880396 4.017572 4.341929 4.411343 4.716671 4.816199 4.851748
## [11,] 3.839412 3.950076 4.134983 4.188525 4.189104 4.235302 4.290163 4.321174
## [12,] 2.544907 2.904092 3.094208 3.324121 3.436294 3.530716 3.548810 3.584404
## [13,] 3.729572 3.931624 3.936064 3.957039 3.992877 4.012199 4.018576 4.041363
## [14,] 2.623459 2.826313 2.888455 2.963436 3.002221 3.098564 3.178326 3.214041
## [15,] 4.236799 4.238615 4.329188 4.365104 4.452540 4.492300 4.529948 4.571635
## [16,] 3.613840 3.823286 3.837713 3.990173 4.059068 4.132200 4.149736 4.193422
## [17,] 3.925311 4.095008 4.260545 4.467971 4.600207 4.703646 4.705292 4.788767
## [18,] 3.340989 3.596605 3.648404 3.873034 3.889519 3.897943 3.900658 3.986597
## [19,] 4.031811 4.221582 4.282473 4.364293 4.468301 4.484552 4.529442 4.678131
## [20,] 3.214287 3.252302 3.272900 3.287073 3.417971 3.436439 3.514234 3.584064
##           [,9]    [,10]
##  [1,] 6.282054 6.282294
##  [2,] 3.461892 3.491080
##  [3,] 3.615966 3.618376
##  [4,] 3.968345 3.985265
##  [5,] 3.671480 3.689459
##  [6,] 5.029906 5.037992
##  [7,] 3.670929 3.702407
##  [8,] 4.915030 4.931211
##  [9,] 3.308123 3.347984
## [10,] 4.870489 4.997906
## [11,] 4.350126 4.364901
## [12,] 3.634390 3.636062
## [13,] 4.058339 4.064772
## [14,] 3.221525 3.249002
## [15,] 4.579444 4.590735
## [16,] 4.339484 4.340030
## [17,] 4.827746 4.877181
## [18,] 4.037006 4.103669
## [19,] 4.780792 4.823908
## [20,] 3.624421 3.630796

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.957                      1                      0.988
##  2                       0.890                      0.983                  0.988
##  3                       0.767                      1                      0.970
##  4                       0.908                      1                      0.988
##  5                       0.974                      1                      0.988
##  6                       0.974                      1                      0.955
##  7                       0.832                      1                      0.988
##  8                       0.890                      1                      1    
##  9                       0.974                      0.832                  0.955
## 10                       0.974                      1                      0.988
## # ℹ 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.0272         -0.447         1.48                      -0.342
##  2       -0.0957         -0.176        -0.199                     -0.493
##  3       -0.0161         -1.07          0.552                      0.693
##  4       -0.236          -0.0121       -0.0770                     0.169
##  5       -0.142          -0.171        -0.0665                     0.220
##  6        0.482          -0.348        -0.164                     -0.504
##  7       -0.0590         -0.108        -0.269                     -0.937
##  8       -0.00887         0.180        -0.0171                     0.365
##  9        0.631          -0.470        -0.00925                   -0.703
## 10       -0.506          -0.356        -0.299                      0.884
## # ℹ 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.153 0.278 0.272 0.237 0.269 ...