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] 911 830 245 977 432 418 250 193 382 119 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  911  264  359  957  370  224  129  175  877   997
##  [2,]  830  952  330  514   70  464  367   61  747   575
##  [3,]  245  707  689  565  763  996  228  553  559   384
##  [4,]  977  466  999  889  746   27  760  568  565   912
##  [5,]  432  136  102  593  769  282  756  663  517   954
##  [6,]  418  171  361  825  563   75  334  733   80   897
##  [7,]  250  152  279  160   85  891  161  368  878   332
##  [8,]  193  167  705  957  886   57  613  656  205    10
##  [9,]  382  498  933  306  584  949  917  242  317   204
## [10,]  119  205  194  377  705   77  145  166  789   860
## [11,]  153  345  768  116  405  655  980  694  844   484
## [12,]  931  503  893  605  834  363  936  964  598   461
## [13,]  778  700  666  846  720  283  750  268  144   900
## [14,]  205  611  719  180  161   80  616  252  119   957
## [15,]  586  380  618  215  921  195  416  824  500   661
## [16,]  665  142  945  914  705  152  703  324  270    57
## [17,]  878  719  213  842  179  457  860  185   14    58
## [18,]  911  530  957  164   31  708  549  611  892   457
## [19,]  591  369  146  329  902  635  712  212  950   227
## [20,]  200  900  316  434  823  368  530  172   57   880
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.59 4.68 2.17 3.17 3.85 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.586537 3.729572 3.891359 3.900928 3.931624 3.992877 4.018576 4.058339
##  [2,] 4.682708 4.821225 4.938197 5.065203 5.100889 5.122794 5.168790 5.189825
##  [3,] 2.166226 2.417205 2.533775 2.565229 2.582945 2.602986 2.670389 2.696508
##  [4,] 3.166097 3.250618 3.331922 3.377122 3.404695 3.511901 3.516361 3.519392
##  [5,] 3.850334 4.131262 4.191703 4.206895 4.236631 4.281859 4.301663 4.440255
##  [6,] 3.865158 4.031615 4.159029 4.364631 4.475063 4.544673 4.559770 4.573801
##  [7,] 3.717725 3.845536 3.920553 3.951215 3.991113 4.008832 4.059086 4.157371
##  [8,] 3.160703 3.264204 3.437360 3.555439 3.595931 3.616157 3.619084 3.624309
##  [9,] 3.980797 4.139264 4.217798 4.391363 4.427340 4.482436 4.516196 4.527090
## [10,] 2.979887 3.003804 3.013099 3.063431 3.129153 3.193783 3.221482 3.252045
## [11,] 3.155625 3.266114 3.397197 3.643457 3.748741 3.849347 3.886697 3.905109
## [12,] 3.862040 4.085290 4.089073 4.104447 4.200690 4.310497 4.355745 4.516146
## [13,] 3.104207 3.332471 3.463413 3.522466 3.580990 3.669855 3.684584 3.713510
## [14,] 2.686813 3.203988 3.206925 3.218411 3.229940 3.268903 3.350932 3.380427
## [15,] 3.348177 3.504714 3.789941 3.832154 3.942463 3.982561 4.035097 4.164026
## [16,] 3.284649 3.550185 3.659957 3.815576 3.850592 3.939764 4.082309 4.109970
## [17,] 3.172684 3.333152 3.565380 3.609622 3.643893 3.675832 3.679565 3.695514
## [18,] 3.802872 4.107313 4.148939 4.291177 4.387384 4.400052 4.444378 4.654596
## [19,] 2.936871 3.195534 3.589198 3.615391 3.704460 3.735535 3.926506 3.958820
## [20,] 3.772988 3.919439 4.108895 4.182027 4.257397 4.347584 4.360755 4.477520
##           [,9]    [,10]
##  [1,] 4.160121 4.175377
##  [2,] 5.272487 5.285888
##  [3,] 2.709469 2.734650
##  [4,] 3.594389 3.642005
##  [5,] 4.451459 4.516786
##  [6,] 4.624760 4.713728
##  [7,] 4.173900 4.177814
##  [8,] 3.690309 3.705418
##  [9,] 4.554737 4.620931
## [10,] 3.262275 3.278747
## [11,] 3.916193 3.954783
## [12,] 4.533751 4.551000
## [13,] 3.761874 3.770497
## [14,] 3.390087 3.412378
## [15,] 4.165959 4.167254
## [16,] 4.173615 4.177621
## [17,] 3.698709 3.707276
## [18,] 4.717707 4.720670
## [19,] 3.968345 3.985265
## [20,] 4.479676 4.490707

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.999                      0.886                  0.827
##  2                       0.999                      0.824                  0.782
##  3                       0.999                      0.933                  0.773
##  4                       0.999                      0.824                  0.994
##  5                       0.999                      0.824                  0.973
##  6                       0.999                      0.750                  0.873
##  7                       0.999                      0.833                  0.993
##  8                       0.999                      0.961                  0.953
##  9                       0.999                      0.796                  0.994
## 10                       0.954                      0.926                  0.708
## # ℹ 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.0567         0.718          0.428                     0.899
##  2        -0.215         -0.250         -0.0990                   -0.294
##  3        -0.131         -0.106         -0.294                     0.683
##  4        -0.143         -0.484         -0.289                    -0.892
##  5         0.733         -0.0173         0.321                     0.889
##  6        -0.641         -0.621         -0.456                     0.695
##  7        -0.139         -0.148         -0.652                    -0.308
##  8        -0.299         -0.862         -0.699                    -0.587
##  9        -0.263         -0.0122        -0.107                     0.281
## 10        -0.326         -0.0343         1.04                      0.157
## # ℹ 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.238 0.18 0.348 0.27 0.215 ...