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] 426 503 720 404 487 708 175 817 323 772 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  426  750  552  290  603  578  386  637  276    34
##  [2,]  503  391  211   60  937  280  835  695  294   315
##  [3,]  720  808  185  722  523  348  921  360  802   685
##  [4,]  404  441  749  254  137  380   59  840  764   782
##  [5,]  487  350  783  953  988  615  143  767   14   217
##  [6,]  708  599  885  139  877  974  163  256  827   698
##  [7,]  175  895  974  730  469  557  435  363   18   317
##  [8,]  817  449  115   80  813  696   25  356   66   348
##  [9,]  323  351   51  148  889  527  873  558  528   478
## [10,]  772  280  495  503  397    2  739   60  859    91
## [11,]  424  106  337  924   89  219  978  387  230   711
## [12,]  498   96   54  988  223  918  369  158  123    14
## [13,]  233  315  520   60   40  282   52   94  903   616
## [14,]  858  741  988  177  682  706  350  978   81   204
## [15,]  831   41  613  270  337  988  953  219  858   634
## [16,]  944  629  184  712  122  423  249  272  515   443
## [17,]  325  124  913  832  742  316  266  936  562   680
## [18,]  652  170  393  469  163  974  895  363  435     7
## [19,]  632  190  244  262  210  898  192  968  849   726
## [20,]  891  329  766  691  117  169  481  897  221   615
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.38 2.52 3.02 4.23 2.99 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.381325 4.146984 4.354256 4.400671 4.407818 4.439486 4.568097 4.669324
##  [2,] 2.522069 3.121466 3.127203 3.444082 3.452956 3.453405 3.455038 3.554004
##  [3,] 3.016322 3.073767 3.263121 3.309315 3.314453 3.330960 3.385717 3.419556
##  [4,] 4.227626 4.448529 4.672951 4.677396 4.736512 4.822613 4.830975 4.850460
##  [5,] 2.992707 3.118008 3.148277 3.274980 3.361775 3.372370 3.439153 3.455383
##  [6,] 3.819830 3.969982 3.985412 4.032106 4.236799 4.452540 4.492300 4.529948
##  [7,] 4.140878 4.198442 4.430188 4.663886 4.703392 4.720072 4.761297 4.803194
##  [8,] 3.357628 3.589494 3.644647 3.713032 3.830255 3.874822 3.918866 3.964005
##  [9,] 3.344829 3.364253 3.395116 3.411248 3.412474 3.550818 3.589862 3.591775
## [10,] 3.631617 3.734908 3.749448 3.863303 3.864758 3.875702 3.935169 4.018140
## [11,] 3.968356 4.449110 4.477519 4.518838 4.547056 4.579580 4.605214 4.665375
## [12,] 2.616732 3.348047 3.383497 3.397420 3.399174 3.420635 3.532288 3.533299
## [13,] 4.966901 4.999660 5.039738 5.085931 5.087400 5.114662 5.172870 5.205922
## [14,] 2.337340 2.619225 2.633365 2.636770 2.699855 2.752843 2.829769 2.853038
## [15,] 3.690485 3.699995 3.782600 3.800346 3.897406 3.916336 4.009586 4.116667
## [16,] 4.346599 4.600549 4.835635 4.840011 4.889187 4.916895 4.997647 5.025768
## [17,] 3.403529 3.556980 3.914122 4.105836 4.136748 4.220445 4.313055 4.457099
## [18,] 4.090966 4.184798 4.201195 4.335344 4.378699 4.560742 4.626412 4.758725
## [19,] 2.862890 3.043581 3.073014 3.206544 3.244210 3.270410 3.340979 3.483959
## [20,] 2.958130 3.136652 3.205617 3.294737 3.307469 3.312932 3.313815 3.411723
##           [,9]    [,10]
##  [1,] 4.804382 4.900333
##  [2,] 3.555447 3.577432
##  [3,] 3.428579 3.428636
##  [4,] 4.868546 4.928710
##  [5,] 3.463760 3.643865
##  [6,] 4.559865 4.571635
##  [7,] 4.843023 5.123233
##  [8,] 3.989260 4.012792
##  [9,] 3.599950 3.633145
## [10,] 4.070766 4.073460
## [11,] 4.721702 4.733855
## [12,] 3.599159 3.647355
## [13,] 5.265535 5.289222
## [14,] 2.855092 2.924205
## [15,] 4.152861 4.162815
## [16,] 5.046867 5.106651
## [17,] 4.630311 4.633842
## [18,] 4.765291 4.843023
## [19,] 3.499257 3.557708
## [20,] 3.413683 3.441045

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.912                          1                  1    
##  2                       0.984                          1                  1    
##  3                       0.986                          1                  1    
##  4                       1                              1                  1    
##  5                       0.984                          1                  0.938
##  6                       1                              1                  1    
##  7                       0.986                          1                  1    
##  8                       0.986                          1                  1    
##  9                       0.986                          1                  1    
## 10                       0.963                          1                  1    
## # ℹ 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.0674         0.695         -0.229                     0.867
##  2        -0.242         -0.107         -0.0237                    0.451
##  3        -0.176         -0.130         -0.221                    -0.428
##  4        -0.249         -0.157         -0.275                     0.608
##  5        -0.227         -0.219         -0.0323                   -0.489
##  6         0.911          0.270         -0.567                    -1.66 
##  7        -0.392         -0.129          0.311                     0.689
##  8        -0.0548        -0.249         -0.118                    -0.301
##  9         0.943         -0.0216        -0.0820                   -0.741
## 10        -0.194         -0.262         -0.0632                    0.868
## # ℹ 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.203 0.273 0.275 0.195 0.274 ...