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] 331 402 895 319 583 525 937 92 479 414 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  331   85  900  271  859  342   35  731  673   835
##  [2,]  402  220   35   85  900  611  859  295  685   976
##  [3,]  895  921  982  411  564  281  583  387  626   239
##  [4,]  319  824  175   39  172  667  281   32  192   147
##  [5,]  583  895  199  537  147   83  921  635  105   272
##  [6,]  525  976  673   85  651  307  489  891  425   547
##  [7,]  937  147  847  248  904  140  537  603  921   447
##  [8,]   92  638  744  199  840  281  359  921  111    41
##  [9,]  479  416  976  548  651  765  871  799  618   733
## [10,]  414   28  638  584  127  337  660  443  136   283
## [11,]  767  157  986  675  937  889  820  583  723   333
## [12,]  883  124  453  256  511  965  796   14 1000   204
## [13,]   67  132  767  672  319  937  831  615  200   564
## [14,]  965  941   12  370  453   62  409  361  439   980
## [15,]   72  662  773  928  535  778  264  901  171   144
## [16,]  444  174  912  968  717  520  648  563  691   944
## [17,]  869  251  180  237  833  615  495  186  936    95
## [18,]  667  892  390    7  952   38  939   34  319   870
## [19,]  519  359  386  608  565  809  589  414  527   332
## [20,]  537  739  112  574  518  637  615  118  682   143
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.06 3.59 2.59 3.11 3.72 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.061600 3.467129 3.947331 3.987906 4.044506 4.047196 4.078871 4.096658
##  [2,] 3.593361 3.671397 3.776295 3.900636 3.904336 3.967070 4.100474 4.178218
##  [3,] 2.587125 2.668626 2.702495 2.703718 2.952970 2.968647 2.970473 2.983170
##  [4,] 3.113533 3.318731 3.567572 3.609403 3.625169 3.636588 3.723023 3.757469
##  [5,] 3.721614 3.860959 3.921677 3.937205 3.938807 3.942885 3.963566 3.970520
##  [6,] 3.339616 4.188286 4.243879 4.321129 4.331707 4.366374 4.398688 4.560838
##  [7,] 3.063972 3.149381 3.246037 3.251005 3.377825 3.391909 3.407321 3.408640
##  [8,] 3.160703 3.437360 3.555439 3.589279 3.624309 3.700522 3.705418 3.782933
##  [9,] 3.943092 4.052555 4.059438 4.343641 4.346685 4.350506 4.434867 4.452189
## [10,] 3.687519 3.692020 3.713328 3.923946 3.965478 4.055321 4.062498 4.117792
## [11,] 4.500377 4.751897 4.788519 4.830815 4.834286 4.874713 4.883767 4.945858
## [12,] 3.005773 3.035394 3.050291 3.142457 3.183187 3.205621 3.277666 3.304815
## [13,] 2.595939 2.603422 2.929799 3.247262 3.265940 3.325128 3.457625 3.496824
## [14,] 3.012112 3.230329 3.304815 3.340812 3.447265 3.597304 3.606928 3.667046
## [15,] 2.874834 3.130284 3.171814 3.190768 3.247821 3.271862 3.284615 3.492266
## [16,] 4.327196 4.371005 4.375177 4.386573 4.464914 4.471504 4.476672 4.510921
## [17,] 4.163650 4.354378 4.371849 4.394360 4.411843 4.465548 4.471538 4.574524
## [18,] 3.954770 3.972435 4.125692 4.142760 4.291489 4.324602 4.352732 4.356745
## [19,] 2.948288 3.793528 3.826311 3.846804 3.909770 3.933604 3.940481 3.956164
## [20,] 2.501847 2.937926 2.968273 3.089898 3.124067 3.127436 3.149268 3.172053
##           [,9]    [,10]
##  [1,] 4.144787 4.154771
##  [2,] 4.227181 4.247132
##  [3,] 3.015582 3.040671
##  [4,] 3.818075 3.820665
##  [5,] 4.021386 4.034945
##  [6,] 4.635994 4.650246
##  [7,] 3.411293 3.427433
##  [8,] 3.798916 3.826682
##  [9,] 4.472377 4.480609
## [10,] 4.181831 4.195806
## [11,] 4.954369 4.969762
## [12,] 3.456875 3.474798
## [13,] 3.555845 3.558326
## [14,] 3.716201 3.727787
## [15,] 3.523420 3.553985
## [16,] 4.525895 4.544326
## [17,] 4.599718 4.607520
## [18,] 4.372232 4.403383
## [19,] 4.003848 4.013492
## [20,] 3.186410 3.265923

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                           1                      0.815                  0.901
##  2                           1                      0.690                  0.984
##  3                           1                      0.937                  0.927
##  4                           1                      0.937                  0.901
##  5                           1                      0.908                  0.773
##  6                           1                      0.937                  0.862
##  7                           1                      0.908                  0.862
##  8                           1                      0.908                  0.995
##  9                           1                      0.972                  0.773
## 10                           1                      0.974                  0.999
## # ℹ 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.131         -0.106          -0.294                   0.683 
##  3        -0.143         -0.484          -0.289                  -0.892 
##  4        -0.180         -0.216          -0.202                   0.0174
##  5        -0.299         -0.862          -0.699                  -0.587 
##  6        -0.305         -0.594           0.262                  -0.411 
##  7        -0.326         -0.0343          1.04                    0.157 
##  8        -0.234         -0.0633         -0.104                  -0.579 
##  9        -0.0210        -0.0601          0.432                  -0.906 
## 10        -0.239         -0.194          -0.237                  -0.850 
## # ℹ 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.233 0.226 0.326 0.259 0.245 ...