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] 966 24 582 319 331 563 714 273 419 606 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  966  751  748  724  227  133  965  763  762   900
##  [2,]   24  895   99  606  970  216  533  101   15   953
##  [3,]  582  920  401  720  260    9  431  754  266   475
##  [4,]  319  290  314  951  368  253  164  585   33   362
##  [5,]  331  507  555  744  883  862   75  149  827   436
##  [6,]  563  238   66  306   17  538  891  923  750   689
##  [7,]  714  933  674  480  979  283  272   70  152   899
##  [8,]  273  466  551  507  394  495  685  733  675   262
##  [9,]  419  959  688  372  720  909  297   87  796   613
## [10,]  606  931  968  970   99  594  433  882  458   864
## [11,]  366  383  759  454  600  356  637  484  319   469
## [12,]   58  751  411  966  259  447   87  219  631   525
## [13,]  634  458  216  968  723  824  179   56  902   154
## [14,]  398  578  229  481   88  903  508  108  679   289
## [15,]  344  101  558  671  250  633  876  291  741   953
## [16,]  481  971   47  850   88  164  141  823  356   613
## [17,]  923  881  689   93  282  556  794  516  306   780
## [18,]  563  923  233  616  133  956  274  337   61   158
## [19,]  212   99  950  116  659  864  322  757  824   606
## [20,]  556  672  227  505  966  856  978   63  315   751
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.41 3.88 3.64 3.29 4.04 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.407407 2.637939 2.827644 2.949460 3.015529 3.072693 3.090644 3.150681
##  [2,] 3.875179 4.164362 4.191635 4.197356 4.253011 4.358723 4.411838 4.437622
##  [3,] 3.641054 4.078285 4.140878 4.146091 4.735282 4.793053 4.803194 4.817959
##  [4,] 3.292670 3.944156 4.124803 4.215529 4.235465 4.239000 4.271848 4.274129
##  [5,] 4.037374 4.150413 4.262698 4.387087 4.405986 4.626051 4.687488 4.946689
##  [6,] 3.094208 3.256243 3.530716 3.530851 3.548810 3.566553 3.636062 3.656537
##  [7,] 2.921590 2.936505 2.942758 2.981166 3.147516 3.175475 3.397790 3.425969
##  [8,] 2.623359 2.742813 3.339310 3.549813 3.613709 3.668870 3.680584 3.727517
##  [9,] 3.289788 3.473815 3.971722 3.990268 4.015470 4.059120 4.076742 4.077997
## [10,] 3.221538 3.735856 3.780094 3.857558 3.867068 4.034457 4.048233 4.180874
## [11,] 3.292420 3.674934 3.693634 3.697950 3.715950 3.744294 3.753055 3.761022
## [12,] 2.873922 2.969204 2.977494 3.072755 3.098712 3.123732 3.156406 3.199029
## [13,] 3.139344 3.498250 3.527949 3.606120 3.693368 3.720628 3.937566 4.061924
## [14,] 3.363588 3.415407 3.453381 3.520534 3.636421 3.643207 3.761648 3.780802
## [15,] 2.527205 2.564574 2.691874 2.718144 2.789527 2.813570 2.926609 2.927257
## [16,] 2.987907 3.286449 3.392992 3.507742 3.536301 3.563211 3.622569 3.718878
## [17,] 2.832318 2.891351 2.893196 2.902310 2.943991 3.013196 3.055931 3.119748
## [18,] 3.823550 3.895688 3.909433 3.959798 4.044857 4.079071 4.087764 4.142940
## [19,] 3.707049 3.817305 3.977319 4.086391 4.129631 4.306932 4.366362 4.565656
## [20,] 2.966159 3.089898 3.099248 3.102709 3.124067 3.200009 3.223002 3.229411
##           [,9]    [,10]
##  [1,] 3.184743 3.186283
##  [2,] 4.486098 4.488840
##  [3,] 4.843023 4.870586
##  [4,] 4.288405 4.351104
##  [5,] 5.029371 5.034855
##  [6,] 3.667137 3.709151
##  [7,] 3.442462 3.468957
##  [8,] 3.749202 3.761407
##  [9,] 4.078288 4.078329
## [10,] 4.192146 4.193828
## [11,] 3.843806 3.916749
## [12,] 3.210260 3.263643
## [13,] 4.104012 4.176411
## [14,] 3.798015 3.800711
## [15,] 3.014470 3.093675
## [16,] 3.722481 3.758942
## [17,] 3.161492 3.232540
## [18,] 4.181443 4.186960
## [19,] 4.673308 4.686859
## [20,] 3.231976 3.236763

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.652                          1                      1
##  2                       0.864                          1                      1
##  3                       0.989                          1                      1
##  4                       0.892                          1                      1
##  5                       0.954                          1                      1
##  6                       0.858                          1                      1
##  7                       0.877                          1                      1
##  8                       0.690                          1                      1
##  9                       0.925                          1                      1
## 10                       0.664                          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.0638        -0.235       -0.265                     -0.240 
##  2        -0.257         -0.311       -0.447                     -0.920 
##  3        -0.0744         0.170        0.0230                    -0.481 
##  4        -0.0894        -0.309       -0.000283                   0.253 
##  5        -0.0337        -0.117       -0.583                      0.0137
##  6        -0.130         -0.0688       0.433                     -0.518 
##  7        -0.364         -0.535       -0.490                     -0.698 
##  8        -0.370         -0.470       -0.638                     -0.102 
##  9        -0.147         -0.355        0.160                     -1.11  
## 10        -0.0458        -0.195       -0.180                     -0.708 
## # ℹ 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.308 0.218 0.197 0.225 0.199 ...