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] 846 584 891 149 566 112 128 152 76 976 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  846  504  995  948  688  493  887   77  731   547
##  [2,]  584  952  917  119  601  498  468  291  700   459
##  [3,]  891  943    4  676  771  149   27  750  425   641
##  [4,]  149  361    3  438  583  956   83  515  641   943
##  [5,]  566  692  882  626  322  730   76  888  486   253
##  [6,]  112  535  918  353  981  579  325  540  223   446
##  [7,]  128  238  154  157  993  602  513  360  585   266
##  [8,]  152  133  499  797  535  954  999  522  784   333
##  [9,]   76  119  896  698  204  441  359  297  239   429
## [10,]  976   85  607  502   97   60  718  676  823   832
## [11,]  654   54  484  586  112  722  108  401  490   709
## [12,]  923  953  361  109  873  942  198  690  669   970
## [13,]  373  230  208  925  841  198  484   54   20   635
## [14,]  319  713  903  567  680  629  170  545  591   138
## [15,]  696  845  944  659  159  589  200  512  727   263
## [16,]  498  123  555  601  952  917  429   91  462   337
## [17,]  415  493  938  504  505   89  761   77  715   948
## [18,]  241  384  468  608  479  630   29  318  615   555
## [19,]  808  775  246  701  878  328  741  157  298   275
## [20,]  230  524  784  442  756  198  208  963   23   732
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 4.4 3.31 2.53 2.33 3.22 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 4.398798 4.578948 4.627504 4.640163 4.708752 4.783559 5.046358 5.234115
##  [2,] 3.306132 3.480990 3.584387 3.655244 3.697649 3.709681 3.756065 3.784321
##  [3,] 2.533775 2.582945 2.670389 2.671984 2.709469 2.734650 2.760390 2.777761
##  [4,] 2.326419 2.552153 2.670389 2.767063 2.811830 2.876781 2.899300 2.920069
##  [5,] 3.221757 3.361387 3.546973 3.550768 3.582753 3.626467 3.626786 3.634353
##  [6,] 2.544907 3.250859 3.397247 3.437658 3.565004 3.565658 3.637125 3.729441
##  [7,] 3.391197 4.109364 4.116124 4.241636 4.276462 4.285681 4.306341 4.326114
##  [8,] 3.344829 3.395116 3.411248 3.412474 3.550818 3.585570 3.591775 3.599950
##  [9,] 3.036909 3.091061 3.561725 3.564839 3.611303 3.636292 3.660321 3.694385
## [10,] 3.067959 3.227690 3.528960 3.552619 3.598409 3.685850 3.693053 3.739082
## [11,] 4.013864 4.066876 4.076489 4.101227 4.117041 4.142000 4.157371 4.205308
## [12,] 3.203656 3.545104 3.588573 3.598605 3.627669 3.680677 3.736156 3.737150
## [13,] 3.985561 4.153654 4.163283 4.254944 4.264703 4.374184 4.423634 4.502160
## [14,] 3.468951 3.515847 3.640917 4.036209 4.062286 4.331332 4.381185 4.456790
## [15,] 2.892078 3.092743 3.359153 3.507238 3.608953 3.639802 3.649601 3.658235
## [16,] 2.809988 2.946328 3.107255 3.217970 3.230808 3.244798 3.304969 3.308979
## [17,] 4.737435 4.788924 4.797339 4.807582 4.938139 5.012716 5.105142 5.178775
## [18,] 4.126217 4.368160 4.372758 4.444069 4.471064 4.522414 4.570687 4.653829
## [19,] 2.488492 3.327672 3.339597 3.556047 3.662678 3.712260 3.712295 3.793516
## [20,] 3.560172 3.610449 3.680563 3.793470 3.817142 3.833874 3.837056 3.846906
##           [,9]    [,10]
##  [1,] 5.312010 5.376992
##  [2,] 3.850989 3.855120
##  [3,] 2.781966 2.790082
##  [4,] 2.923723 2.934396
##  [5,] 3.719566 3.807734
##  [6,] 3.753837 3.777968
##  [7,] 4.509305 4.587822
##  [8,] 3.617930 3.633145
##  [9,] 3.706666 3.713536
## [10,] 3.742533 3.783128
## [11,] 4.227726 4.241303
## [12,] 3.748853 3.779984
## [13,] 4.530583 4.544072
## [14,] 4.483626 4.552660
## [15,] 3.671037 3.701915
## [16,] 3.341227 3.358280
## [17,] 5.308749 5.338287
## [18,] 4.691917 4.706558
## [19,] 4.005549 4.016861
## [20,] 3.864883 3.873455

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.902                          1                  0.934
##  2                       1                              1                  1    
##  3                       0.902                          1                  1    
##  4                       0.665                          1                  0.955
##  5                       0.930                          1                  0.692
##  6                       0.996                          1                  0.880
##  7                       0.996                          1                  1    
##  8                       1                              1                  0.692
##  9                       0.797                          1                  1    
## 10                       0.902                          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.0300        -0.0665           0.406                   0.365 
##  2       -0.0390        -0.100            0.438                  -0.509 
##  3       -0.0559        -0.157            0.375                  -0.539 
##  4       -0.136         -0.242            0.532                   0.0579
##  5       -0.126         -0.207            0.611                  -0.307 
##  6        0.189         -0.299            0.639                   0.736 
##  7        0.862         -0.0213          -0.235                   0.232 
##  8       -0.0598        -0.0786          -0.276                   0.582 
##  9       -0.00581       -0.00893          0.254                   0.0550
## 10        0.678          0.724            0.488                  -0.258 
## # ℹ 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.183 0.257 0.342 0.335 0.257 ...