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] 618 175 94 376 788 231 699 823 388 429 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  618  815  855  560  163  351  903  215   42    15
##  [2,]  175  797  722   94    3  298    9  966  315    55
##  [3,]   94  315    2  823  722  858  430  109  538   572
##  [4,]  376  715  784  244  429  569  713  138  292   584
##  [5,]  788  137  637  687  411   28  730  995  207   363
##  [6,]  231  512  126  319  593  460  874  632  629   348
##  [7,]  699  722  823  760  310  314  315  528  395   245
##  [8,]  823  540  945  699  668  252  760  796  116   603
##  [9,]  388  314  111  390   67  223  637  818  434   294
## [10,]  429  764  511  715  705  659  785  679  782   672
## [11,]  116  878  365  408  654  521   76  210  554   561
## [12,]  739  134  476  619  463  122  470  984  446   939
## [13,]  422  321  481  493  982  893  351  805  600   868
## [14,]   62  645  679  135  715   10  142  784  169   954
## [15,]   42  562  760  528  823  932  215  855  987   815
## [16,]  887  613  423  266  731  797  283   56  420   538
## [17,]  303   92 1000  783  869  138  261  967  160   569
## [18,]  346   56  218  473  810  506  287  602  573   613
## [19,]  561  654   76  789   11   55  318  637  778   855
## [20,]  520  995  818  216  137  422  250  730  983    77
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.95 2.74 3.02 4.13 2.4 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.950659 3.320610 3.470432 3.543018 3.619041 3.623170 3.696015 3.697610
##  [2,] 2.743782 3.042024 3.189666 3.269234 3.270726 3.285611 3.315812 3.362373
##  [3,] 3.021559 3.124713 3.270726 3.351291 3.365187 3.369506 3.387537 3.389270
##  [4,] 4.130807 4.188286 4.235551 4.253292 4.321129 4.331707 4.338776 4.398688
##  [5,] 2.397607 2.571217 2.693748 2.870486 2.919770 2.921099 3.040246 3.075204
##  [6,] 4.325594 4.759412 4.943263 4.989323 5.008097 5.034340 5.091783 5.310784
##  [7,] 3.098287 3.534448 3.593814 3.613282 3.669893 3.706039 3.737258 3.780601
##  [8,] 3.266918 3.271013 3.448616 3.627434 3.627512 3.672853 3.688390 3.694279
##  [9,] 2.326419 2.345793 2.552153 2.654381 2.799610 2.850939 2.923723 2.934396
## [10,] 3.339767 3.541037 3.625683 3.642903 3.659687 3.696173 3.712973 3.731367
## [11,] 2.068677 2.904984 3.199871 3.272868 3.407100 3.441573 3.483313 3.490977
## [12,] 2.628028 3.003804 3.063431 3.129153 3.188596 3.193783 3.221482 3.262275
## [13,] 2.548095 2.671363 3.015991 3.041277 3.107266 3.179717 3.183438 3.208087
## [14,] 3.537185 3.676100 4.122731 4.148769 4.161267 4.186317 4.230996 4.338435
## [15,] 2.847382 2.939688 2.989375 3.116903 3.123683 3.165366 3.188759 3.221968
## [16,] 3.092417 3.111860 3.138471 3.218674 3.310448 3.354764 3.403034 3.538629
## [17,] 3.964374 4.017572 4.074490 4.426026 4.435834 4.471956 4.495380 4.604388
## [18,] 3.169344 3.289579 3.357993 3.473550 3.498359 3.515097 3.582057 3.585324
## [19,] 3.392699 3.759096 3.823090 3.888237 3.944320 4.024833 4.049032 4.080342
## [20,] 3.507330 3.697760 3.795952 3.798232 3.816370 3.881656 3.965182 4.027721
##           [,9]    [,10]
##  [1,] 3.706676 3.711048
##  [2,] 3.362938 3.367654
##  [3,] 3.424862 3.434137
##  [4,] 4.509582 4.609614
##  [5,] 3.106030 3.117838
##  [6,] 5.343851 5.401241
##  [7,] 3.834045 3.852279
##  [8,] 3.740811 3.761720
##  [9,] 2.938252 2.942511
## [10,] 3.887542 3.897985
## [11,] 3.546263 3.606580
## [12,] 3.265916 3.273145
## [13,] 3.234322 3.236562
## [14,] 4.421862 4.422504
## [15,] 3.224208 3.256716
## [16,] 3.567453 3.574648
## [17,] 4.749710 4.830492
## [18,] 3.608535 3.609777
## [19,] 4.106147 4.135332
## [20,] 4.040281 4.065983

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 x 34
##    `pCrkL(Lu175)Di.IL~ `pCREB(Yb176)Di.IL~ `pBTK(Yb171)Di.IL~ `pS6(Yb172)Di.IL7~
##                  <dbl>               <dbl>              <dbl>              <dbl>
##  1                   1               0.990              0.928              0.878
##  2                   1               0.990              0.831              0.878
##  3                   1               0.990              0.697              0.998
##  4                   1               0.947              0.974              0.990
##  5                   1               0.990              0.831              0.998
##  6                   1               0.990              0.796              0.892
##  7                   1               0.954              0.937              0.998
##  8                   1               0.990              0.991              0.998
##  9                   1               0.993              0.571              0.998
## 10                   1               0.916              1                  0.878
## # ... with 990 more rows, and 30 more variables:
## #   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>,
## #   pAKT(Tb159)Di.IL7.qvalue <dbl>, pBLNK(Gd160)Di.IL7.qvalue <dbl>,
## #   pP38(Tm169)Di.IL7.qvalue <dbl>, pSTAT5(Nd150)Di.IL7.qvalue <dbl>,
## #   pSyk(Dy162)Di.IL7.qvalue <dbl>, tIkBa(Er166)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 x 51
##    `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(I~ `CD3(Cd114)Di`
##             <dbl>          <dbl>          <dbl>             <dbl>          <dbl>
##  1        -0.204         -0.113        -0.156             -0.0615        -0.486 
##  2        -0.192         -0.0231       -0.817             -0.539          0.109 
##  3        -0.185         -0.420        -1.34              -1.04          -0.894 
##  4        -0.478         -0.0126       -0.00905            0.437         -0.0886
##  5        -0.239         -0.354        -0.245              0.119         -0.289 
##  6        -0.128         -0.170        -0.125              0.0949         0.723 
##  7        -0.0216         0.363        -0.119             -0.993          0.375 
##  8        -0.137         -0.0365        0.193             -0.881          0.198 
##  9        -0.161         -0.0198       -0.339             -1.10          -0.419 
## 10         0.116         -0.176         0.390             -1.02           0.262 
## # ... with 20 more rows, and 46 more variables: 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>,
## #   PreBCR(Ho165)Di <dbl>, CD43(Er167)Di <dbl>, CD38(Er168)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.262 0.286 0.282 0.212 0.313 ...