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] 892 358 625 144 491 948 818 305 751 191 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  892  737   85  274  251   17  364  921  790   479
##  [2,]  358  515  610  689  664  672  801  646  679   143
##  [3,]  625  598  745  459  275  677  638  511  204   203
##  [4,]  144  945  374  811  520  514  305  895  528   884
##  [5,]  491  558  392  980  757  733  298  110  276   687
##  [6,]  948  427  899  695  830  724  934  804  504   516
##  [7,]  818  187  572  401  323  669   92  430  948   181
##  [8,]  305  646  517   56  895  194  494   73  897   873
##  [9,]  751  958  682  810  661  344  298  467  728   343
## [10,]  191  791  696  501  805   76  306  650  633   643
## [11,]  452   14  771  546  972  873  751  936  227   146
## [12,]  948   76  733  320  191  108  516  196  569   315
## [13,]  518  998  701  496  682  220   17  230  126    87
## [14,]  527  862  558   11  452  569  721  704  101   426
## [15,]  463  671  523   79  304  446  691  546  930    11
## [16,]  960  527  985  642  131  874  546  469  643   564
## [17,]  524  998  446   41  855  523  671  274  479   737
## [18,]  663  219  319  620  822  726  973  947  223   464
## [19,]  714  483  638  542  272  217  391  393  507    44
## [20,]  557  633  650  576  564  743  922   76  141    73
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.6 3.86 3.41 4.14 2.89 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.597517 3.682086 3.770940 3.786448 3.816526 3.884957 3.929530 4.022458
##  [2,] 3.862287 3.893723 4.004221 4.090277 4.118483 4.155429 4.189914 4.436641
##  [3,] 3.406636 3.923319 4.167996 4.173893 4.206920 4.214784 4.240712 4.265978
##  [4,] 4.140878 4.398263 4.408850 4.423437 4.455586 4.496290 4.547326 4.549342
##  [5,] 2.885137 2.964646 3.013319 3.151578 3.200521 3.228751 3.276451 3.285030
##  [6,] 2.614362 2.766335 2.814126 2.987915 3.039159 3.042328 3.060159 3.068015
##  [7,] 2.694178 3.209440 3.298959 3.421240 3.475168 3.478957 3.524819 3.612299
##  [8,] 4.067759 4.291209 4.310931 4.323342 4.452250 4.491660 4.515605 4.541525
##  [9,] 3.417541 3.597990 3.686504 3.704716 3.759019 3.768494 3.779582 3.784145
## [10,] 3.438563 3.653781 3.686222 3.736796 3.782037 3.837499 3.994575 4.078010
## [11,] 2.310442 2.740851 2.798149 2.815045 2.882021 2.882089 2.973589 2.977038
## [12,] 2.811354 2.848022 2.859005 3.049587 3.056745 3.091457 3.164357 3.198239
## [13,] 3.163784 3.495967 3.598992 3.599732 3.733317 3.767352 3.802625 3.807877
## [14,] 2.485015 2.653001 2.684383 2.740851 2.865239 2.991803 3.011045 3.107181
## [15,] 3.160703 3.399980 3.437360 3.569888 3.645062 3.705418 3.766952 3.782933
## [16,] 2.680930 2.854281 2.895732 2.899957 2.970063 3.027255 3.042406 3.115715
## [17,] 2.795223 2.973167 2.979887 3.063779 3.090327 3.105851 3.151453 3.156564
## [18,] 3.958606 3.980797 4.051932 4.076361 4.217798 4.343024 4.467726 4.516196
## [19,] 2.491643 3.037649 3.331466 3.392959 3.399999 3.511985 3.579136 3.615328
## [20,] 4.241486 4.334695 4.397172 4.456708 4.506987 4.588290 4.665674 4.690217
##           [,9]    [,10]
##  [1,] 4.054630 4.067000
##  [2,] 4.512263 4.578121
##  [3,] 4.269086 4.292833
##  [4,] 4.557152 4.564612
##  [5,] 3.302682 3.311190
##  [6,] 3.157734 3.166407
##  [7,] 3.658984 3.715041
##  [8,] 4.623213 4.641192
##  [9,] 3.823646 3.839488
## [10,] 4.089239 4.103829
## [11,] 2.985225 3.006233
## [12,] 3.233157 3.251773
## [13,] 3.866195 3.878691
## [14,] 3.110235 3.142318
## [15,] 3.818428 3.829799
## [16,] 3.116108 3.164540
## [17,] 3.262575 3.267078
## [18,] 4.554737 4.620931
## [19,] 3.702612 3.732389
## [20,] 4.716477 4.720992

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.958                      1                      0.925
##  2                       0.999                      1                      0.959
##  3                       0.938                      1                      0.972
##  4                       0.956                      0.819                  1    
##  5                       0.884                      1                      0.972
##  6                       0.880                      1                      0.990
##  7                       0.570                      1                      0.792
##  8                       0.999                      1                      1    
##  9                       0.999                      1                      0.959
## 10                       0.841                      1                      0.792
## # ℹ 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.204        -0.0512         -0.240                  -1.90  
##  2         -0.147        -0.264          -0.845                  -1.25  
##  3         -0.768        -0.580          -0.264                  -1.63  
##  4         -0.174        -0.0635         -0.219                  -0.776 
##  5         -0.207        -1.14           -0.779                  -0.737 
##  6         -0.658        -0.0448          0.580                  -1.21  
##  7         -0.645        -0.311          -0.677                  -0.544 
##  8         -0.650        -0.110          -0.254                  -1.31  
##  9         -0.937        -1.11            0.362                  -0.782 
## 10         -0.204        -0.113          -0.156                  -0.0615
## # ℹ 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.242 0.213 0.231 0.208 0.293 ...