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] 125 783 191 294 580 414 169 126 225 545 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  125  977  252  384  306  500  191  676  330   974
##  [2,]  783  691  290  277   79  751  833  661  759   652
##  [3,]  191  201  583  371  790  373  113   63  633    53
##  [4,]  294  790  237  229  157  155  746  768  108   886
##  [5,]  580  870   15  830  581   59  764  345  782   115
##  [6,]  414  746  568  958  237  826   89  253  449   942
##  [7,]  169  712  547  450  245  401  915  447  776   761
##  [8,]  126  887  257   89  297  683  513  637  580   368
##  [9,]  225  750  538  440  634  870  198  115  768   201
## [10,]  545  459  367  233  661  102  320  490  310    40
## [11,]  306  321  826  843  693  863  620  855   12   372
## [12,]  904  516  786  615  577  850  929  552  732   808
## [13,]  894   34  393   47   98  844  437   52  365   622
## [14,]  376  747  716  368  568  843  692  621  485   919
## [15,]  642  981  870  568    5  581  830  115  750   527
## [16,]  737  684  233  823  727  964  928  193  535     2
## [17,]  290  256  332  661  579  343  751  298  816   755
## [18,]  838  605  261   79  727  290  535  403   87   953
## [19,]  375  743  858  818  278  761  232  116  576   584
## [20,]  901  257  397  126  394  263  297  655  681   632
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.45 2.73 3.64 3.76 2.58 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.449080 4.050947 4.052120 4.224166 4.354567 4.379335 4.388616 4.409965
##  [2,] 2.726731 2.818536 3.048675 3.281805 3.343012 3.425534 3.571135 3.599752
##  [3,] 3.637631 4.045851 4.369343 4.394792 4.481937 4.545654 4.603009 4.651798
##  [4,] 3.755041 3.760528 4.192822 4.335475 4.354378 4.394360 4.411843 4.465548
##  [5,] 2.579818 2.747512 2.933224 2.972554 3.011654 3.056611 3.059462 3.222036
##  [6,] 3.549076 3.657143 3.755292 3.771055 3.861940 3.881868 4.065656 4.101676
##  [7,] 3.756475 3.958137 3.981918 3.998771 4.028293 4.057891 4.090333 4.314644
##  [8,] 2.954442 3.190342 3.219256 3.434119 3.519632 3.542688 3.640804 3.651357
##  [9,] 2.756064 3.102051 3.152072 3.208460 3.331179 3.340364 3.380470 3.409709
## [10,] 3.662806 3.745176 3.818838 3.835806 3.862861 3.970704 4.026576 4.103007
## [11,] 3.348207 3.380156 3.399359 3.590860 3.614056 3.635727 3.640107 3.650910
## [12,] 2.382848 2.787093 2.880935 2.887437 2.911389 2.972881 2.973513 3.018678
## [13,] 4.076361 4.139264 4.330786 4.337176 4.391363 4.516196 4.582723 4.620931
## [14,] 2.951655 3.014610 3.126766 3.162042 3.204783 3.226841 3.277175 3.345964
## [15,] 2.424238 2.700455 2.844470 2.859183 2.933224 2.956409 3.070859 3.104648
## [16,] 3.708425 4.258050 4.305388 4.807121 5.064622 5.106599 5.161567 5.424645
## [17,] 3.565462 3.595170 3.661371 3.822280 3.836570 3.837056 3.846906 3.864883
## [18,] 3.116733 3.116881 3.448237 3.629561 3.632146 3.771961 3.913452 3.922810
## [19,] 2.183445 2.467871 2.656266 2.840044 2.873438 2.878373 2.903163 2.956495
## [20,] 2.707680 2.821755 2.962524 3.292698 3.319018 3.340254 3.392029 3.418958
##           [,9]    [,10]
##  [1,] 4.417019 4.467850
##  [2,] 3.638666 3.642188
##  [3,] 4.670793 4.692810
##  [4,] 4.574524 4.599718
##  [5,] 3.231998 3.253966
##  [6,] 4.114466 4.145067
##  [7,] 4.357567 4.401240
##  [8,] 3.673408 3.675400
##  [9,] 3.429700 3.443903
## [10,] 4.175135 4.234616
## [11,] 3.702436 3.751228
## [12,] 3.036737 3.057822
## [13,] 4.624479 4.665781
## [14,] 3.369577 3.371839
## [15,] 3.146414 3.168111
## [16,] 5.459953 5.464197
## [17,] 3.873455 3.877967
## [18,] 3.932644 4.062990
## [19,] 3.022827 3.027098
## [20,] 3.424335 3.521479

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.989                      0.908                  0.873
##  2                       0.989                      0.951                  0.844
##  3                       0.989                      0.951                  0.844
##  4                       0.989                      0.906                  0.893
##  5                       0.993                      0.930                  1    
##  6                       0.989                      0.885                  0.964
##  7                       0.989                      0.997                  1    
##  8                       0.989                      0.970                  0.685
##  9                       0.989                      0.797                  0.873
## 10                       0.989                      0.904                  0.495
## # ℹ 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.0595        -0.264          -0.454                    -1.16 
##  2       -0.146         -0.139          -0.167                    -0.937
##  3       -0.00552       -0.187          -0.142                    -1.89 
##  4       -0.278         -0.00435        -0.131                    -1.06 
##  5       -0.158         -0.0951         -0.235                    -0.369
##  6       -0.843         -0.326          -0.320                    -0.157
##  7       -0.184         -0.00776        -0.235                    -0.850
##  8       -0.116         -0.0150         -0.0765                   -0.465
##  9       -0.0179        -0.0305         -0.663                    -0.897
## 10       -0.168         -0.0463         -0.177                    -0.181
## # ℹ 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.221 0.267 0.213 0.219 0.299 ...