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] 768 800 355 145 41 350 311 879 595 595 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  768  242  528  960  491  913  628  281   51   265
##  [2,]  800  258  145  672  289  718    4  571  782   891
##  [3,]  355  123  771  134  229   17  376  790  646   451
##  [4,]  145  162  835  961  258  672  428  483  492   584
##  [5,]   41  588  609  951  501  331  919  304  391   935
##  [6,]  350  371  577  222  477  609  733  291  775    74
##  [7,]  311  361  352  710  763  396  888  285  575   267
##  [8,]  879  741  339   84  354  795  368  158  136   655
##  [9,]  595  112  446  411  636  784  325  266  177   505
## [10,]  595   84  114  642  354  159  871  822  655   468
## [11,]  662  239  370  759  730  272  859  155  803   652
## [12,]  109  496  953  981  570  978  564  469  356   149
## [13,]  120  239  611  455  503   22  640  859  950   271
## [14,]  535  687  727  302  749  388  985  786  674   483
## [15,]  318   56  714  533  728  807   50  600  225   955
## [16,]  423   60   93  485  442  708  368  606  878   917
## [17,]  355  123  688   57  319   58  415  756  215   376
## [18,]  514  835   48  416  857  629  741  162  114   261
## [19,]  160  997  801  733  470  178  739  885  716   589
## [20,]  875  611  190  493  464  599  707   75  723   561
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.44 4.13 3.67 3.17 3.26 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.441515 3.774343 3.954770 3.972435 4.142760 4.145777 4.153422 4.157856
##  [2,] 4.133917 4.413266 4.526329 4.535665 4.657785 4.838354 4.896079 4.908071
##  [3,] 3.667559 3.777098 3.852472 3.863646 3.905320 3.943662 3.977819 4.009312
##  [4,] 3.169950 3.454380 3.635642 3.671480 3.689459 3.711450 3.715013 3.748594
##  [5,] 3.256717 3.402131 3.673282 3.696853 3.770339 3.796290 3.811141 3.823605
##  [6,] 3.501768 3.503807 3.571807 3.728724 3.873347 3.918159 3.927282 3.936384
##  [7,] 3.095164 3.098691 3.379533 3.386359 3.394414 3.448939 3.491857 3.653628
##  [8,] 2.597415 2.630898 2.737955 2.749611 2.750633 2.907796 2.919281 2.958004
##  [9,] 3.795952 3.798232 3.881656 3.921431 4.014543 4.040281 4.065431 4.065983
## [10,] 2.811354 2.848022 2.954676 3.048902 3.049587 3.091457 3.133318 3.156968
## [11,] 3.817037 3.965085 4.018558 4.053127 4.067644 4.130807 4.170941 4.285856
## [12,] 3.588107 3.613840 3.725892 3.837713 4.123938 4.150513 4.193422 4.196769
## [13,] 4.543723 4.616482 4.706540 4.710219 4.730245 4.752023 4.753758 4.779302
## [14,] 3.423775 3.571199 3.661394 3.898330 3.951087 3.974977 3.976495 3.978676
## [15,] 4.032526 4.588079 4.734903 5.160121 5.164474 5.234448 5.237000 5.333032
## [16,] 3.653273 3.850621 3.857852 3.868038 3.929797 3.955489 4.108982 4.131797
## [17,] 2.825372 2.950315 3.119114 3.431811 3.453694 3.478947 3.542899 3.589635
## [18,] 3.095493 3.549029 3.584366 3.636777 3.822604 3.878217 3.879198 3.931665
## [19,] 3.025148 3.255099 3.323617 3.340992 3.388649 3.421620 3.486982 3.559977
## [20,] 3.654629 3.865313 3.918009 3.935926 4.009307 4.065756 4.094772 4.316173
##           [,9]    [,10]
##  [1,] 4.239341 4.243279
##  [2,] 4.960942 5.105017
##  [3,] 4.047676 4.059920
##  [4,] 3.786677 3.796154
##  [5,] 3.894711 3.903688
##  [6,] 3.937841 3.938787
##  [7,] 3.785731 3.820318
##  [8,] 2.973164 2.981620
##  [9,] 4.073482 4.076129
## [10,] 3.164357 3.198239
## [11,] 4.425686 4.511457
## [12,] 4.241979 4.276869
## [13,] 4.813634 4.844629
## [14,] 3.997601 4.015064
## [15,] 5.377997 5.412234
## [16,] 4.139464 4.149534
## [17,] 3.646179 3.677121
## [18,] 3.963441 4.007886
## [19,] 3.613140 3.651005
## [20,] 4.350931 4.369961

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.248                          1                  0.641
##  2                       0.934                          1                  0.944
##  3                       0.523                          1                  0.887
##  4                       0.943                          1                  0.990
##  5                       0.887                          1                  0.691
##  6                       0.523                          1                  0.850
##  7                       0.823                          1                  0.662
##  8                       0.637                          1                  0.858
##  9                       0.823                          1                  0.547
## 10                       0.677                          1                  0.929
## # ℹ 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.446         -0.204         -0.0761                  -0.996 
##  2        -0.105         -0.202         -0.202                   -0.564 
##  3         0.0465        -0.0188        -0.146                   -1.15  
##  4        -0.411         -0.160         -0.219                   -0.816 
##  5        -0.180         -0.255         -0.0147                  -0.455 
##  6         0.632          0.338         -0.451                   -0.378 
##  7        -0.148          0.816         -0.0442                  -1.36  
##  8        -0.0749        -0.118         -0.282                   -1.01  
##  9        -0.458         -0.355         -0.366                    0.0316
## 10         0.787         -0.239          0.0882                   0.0555
## # ℹ 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.232 0.193 0.232 0.256 0.249 ...