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] 649 94 567 138 14 67 458 298 486 231 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  649  557  775  915  790   33  838  773  602   788
##  [2,]   94  569   33  713  133  545  791  416  100    98
##  [3,]  567  550  724  308  964  247    5  770  754   722
##  [4,]  138  889  911  356  273  885  110  810  236   689
##  [5,]   14  308  443  707   54  724   86  986  508   229
##  [6,]   67  427  166  119  855  699  378  642  798   610
##  [7,]  458  163  453  195  138  923  778  627  513   872
##  [8,]  298  510  550  964  842  523  962  299  258    32
##  [9,]  486  748  995  912  768  370   71  405  986   342
## [10,]  231  526  230  414  801  471  539  105  247   974
## [11,]   62  472  982  683  594  916   59  506   46   633
## [12,]  199   34  691  816  711  708  712  519  174   149
## [13,]  644  998  853  962  575  367  953  522 1000   620
## [14,]  229  575   42  522  193  962  134  964  550   995
## [15,]  418  846  905  657   48  362  615  350  521   393
## [16,]  979  859  372  606  984  467  799  117  636   494
## [17,]  810  138  911  129  930  498  504  647  195   303
## [18,]   38  364  462  829  978  854  637  111  917   498
## [19,]  508  572  412  768   28  359  929  707  509   993
## [20,]  163  275  195  453  355  403  764  513  138   692
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 4.31 3.85 3.6 3.66 3.06 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 4.310167 5.096301 5.203686 5.312953 5.387677 5.441803 5.561407 5.577394
##  [2,] 3.849542 3.872867 4.159745 4.359839 4.386872 4.387797 4.409507 4.488994
##  [3,] 3.597885 3.627223 3.655385 3.852221 3.936064 3.977396 4.036954 4.141474
##  [4,] 3.657222 4.090961 4.222360 4.228240 4.448111 4.578400 4.595127 4.601476
##  [5,] 3.061246 3.470828 3.485061 3.543151 3.550177 3.564391 3.607025 3.639282
##  [6,] 3.880574 5.335489 5.718757 5.897403 5.927919 6.194792 6.335815 6.551413
##  [7,] 3.024043 3.036909 3.067011 3.174962 3.328766 3.401148 3.418359 3.437772
##  [8,] 2.668782 2.852806 2.889047 2.959003 3.051892 3.231673 3.357158 3.375428
##  [9,] 3.666589 3.836659 3.850942 3.961121 3.997233 4.151868 4.230629 4.235298
## [10,] 3.160703 3.532920 3.536769 3.702470 3.728523 3.842336 3.855961 3.876903
## [11,] 4.590500 5.118697 5.793105 5.869661 5.872313 6.157258 6.158341 6.166119
## [12,] 3.331935 3.548807 3.552619 3.693053 3.739082 3.872594 3.971408 3.984146
## [13,] 2.695595 2.737319 2.886617 2.987053 3.049882 3.061995 3.090920 3.101901
## [14,] 2.277209 2.644167 2.663104 2.810516 2.825349 2.835163 2.864089 2.879802
## [15,] 4.037374 4.387087 4.626051 5.029371 5.034855 5.083421 5.125432 5.158505
## [16,] 6.741890 7.008756 7.031263 7.277038 7.533316 8.187366 8.223200 8.282400
## [17,] 2.533157 2.867010 3.214327 3.572627 3.637745 3.643436 3.653801 3.696293
## [18,] 3.494389 3.631369 3.891933 3.901637 4.194784 4.352691 4.460457 4.470466
## [19,] 3.807383 3.968494 4.077283 4.176180 4.200089 4.281042 4.286627 4.321288
## [20,] 3.190878 3.598313 3.605968 3.737485 3.807738 3.855312 4.041281 4.049655
##           [,9]    [,10]
##  [1,] 5.689126 5.773673
##  [2,] 4.493017 4.494916
##  [3,] 4.149979 4.243515
##  [4,] 4.605055 4.623874
##  [5,] 3.643701 3.687836
##  [6,] 6.582609 6.690902
##  [7,] 3.465128 3.466670
##  [8,] 3.382220 3.399978
##  [9,] 4.262868 4.271356
## [10,] 3.881098 3.914490
## [11,] 6.224886 6.260775
## [12,] 3.998507 4.014903
## [13,] 3.102828 3.108622
## [14,] 2.886320 2.887425
## [15,] 5.167399 5.167750
## [16,] 8.492539 8.537099
## [17,] 3.749424 3.779568
## [18,] 4.476576 4.545089
## [19,] 4.327995 4.330380
## [20,] 4.058931 4.105608

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.947                      1                      0.998
##  2                       0.947                      0.995                  0.998
##  3                       0.650                      0.995                  1    
##  4                       0.784                      0.995                  1    
##  5                       0.784                      1                      0.998
##  6                       0.947                      0.995                  0.975
##  7                       0.784                      0.995                  1    
##  8                       0.828                      0.995                  0.998
##  9                       0.947                      1                      1    
## 10                       0.855                      0.995                  0.975
## # ℹ 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.325          -0.320        -0.906                     -0.460
##  2       -0.110          -0.204        -0.00716                    0.176
##  3       -0.0699         -0.221         0.959                      0.396
##  4       -0.234          -0.0633       -0.104                     -0.579
##  5       -0.171          -0.126        -0.0613                     0.187
##  6        0.103          -0.250         0.368                      0.146
##  7       -0.00931        -0.0592       -0.430                      0.207
##  8       -0.180          -0.255        -0.0147                    -0.455
##  9       -0.00101        -0.182        -0.0879                     0.411
## 10       -0.869          -0.427         0.197                     -1.07 
## # ℹ 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.168 0.214 0.237 0.216 0.263 ...