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] 558 790 878 565 919 619 881 622 544 399 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  558  651  766  154  627  729   74  546  712   762
##  [2,]  790  161  694  200  550  545  894  757  441   282
##  [3,]  878  557  131  836  909  804  278  948  577   142
##  [4,]  565  496  135  785  964  113  899  462  184   136
##  [5,]  919  605  780  127  230  703  544  900  992   517
##  [6,]  619  360  312  538   11  699  846  959   37   485
##  [7,]  881  949  319  757  741  803  674  518  790   942
##  [8,]  622  697  311  795  285  902  349  594  345   229
##  [9,]  544  371  517  660  146  788  667  459  236   856
## [10,]  399  946  699  850  923  875  705  513  885   604
## [11,]  699  399  289  793  155  513  312  360   10   570
## [12,]  946  699  215  861  535  549   10  538  814   405
## [13,]  623  810  609  233  460  747  176  641  582   625
## [14,]  684  277  411  807  240  823  129  709  628   389
## [15,]  833  385  784   27  453  822  486  317  790   282
## [16,]  379  803  154  976  969  450  239  562  693   402
## [17,]  888  144  935   19  664  739  368  768   47   363
## [18,]  710  699   11  642  289  431  846  861  372   104
## [19,]  368  888   24   17  416  272  771  664  298   707
## [20,]  150  598  792  542   34  192  825  743  663   646
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.83 2.35 3.39 5.32 4.07 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.827524 2.894920 3.079853 3.154524 3.174339 3.363991 3.394503 3.399172
##  [2,] 2.349204 2.639848 2.737095 2.754134 2.757017 2.811023 2.812476 2.862488
##  [3,] 3.392868 3.395322 3.581615 3.708442 3.768758 3.777349 3.828076 3.847425
##  [4,] 5.324985 5.355285 5.520704 5.607854 5.638222 5.672028 5.698508 5.703252
##  [5,] 4.070849 4.318927 4.369049 4.394431 4.458590 4.520748 4.557760 4.666088
##  [6,] 4.047676 4.059920 4.098572 4.114642 4.216622 4.230339 4.280281 4.305231
##  [7,] 2.424238 2.700455 2.933224 3.001413 3.010443 3.070859 3.106239 3.126310
##  [8,] 4.050647 4.296608 4.339992 4.404500 4.637186 5.001013 5.307969 5.445113
##  [9,] 3.988519 4.119575 4.349510 4.398724 4.533777 4.803351 4.834455 4.839624
## [10,] 2.134410 2.205577 2.279481 2.373197 2.570987 2.682794 2.695323 2.699456
## [11,] 2.608587 2.620092 2.626894 2.649969 2.665047 2.731307 2.763009 2.867010
## [12,] 2.583246 2.614677 2.914022 2.948166 3.087103 3.134600 3.164791 3.165685
## [13,] 3.616236 3.965962 4.168260 4.186605 4.213913 4.259835 4.284406 4.294126
## [14,] 4.057206 4.335591 4.338888 4.385952 4.634958 4.675726 4.695690 4.711066
## [15,] 3.638352 3.684760 3.706393 3.729572 3.783449 3.810258 3.822452 3.835881
## [16,] 2.706844 2.852736 2.911103 2.970434 3.026926 3.050343 3.092596 3.156216
## [17,] 3.160619 3.206958 3.276811 3.382013 3.404873 3.461562 3.521048 3.566801
## [18,] 2.994185 3.127637 3.165548 3.295153 3.327055 3.331643 3.340350 3.524829
## [19,] 2.974989 3.139709 3.358817 3.382013 3.487800 3.513848 3.535705 3.621205
## [20,] 4.388845 4.793897 4.827571 4.879548 4.976408 5.034941 5.067369 5.175346
##           [,9]    [,10]
##  [1,] 3.446175 3.457934
##  [2,] 2.881418 2.927191
##  [3,] 3.956950 3.974883
##  [4,] 5.714204 5.739920
##  [5,] 4.673122 4.786557
##  [6,] 4.307037 4.333251
##  [7,] 3.146414 3.168111
##  [8,] 5.536845 5.646037
##  [9,] 4.914221 4.962969
## [10,] 2.717498 2.759566
## [11,] 2.874532 2.888542
## [12,] 3.260139 3.281108
## [13,] 4.304534 4.354315
## [14,] 4.717029 4.782837
## [15,] 3.884427 3.914170
## [16,] 3.163865 3.184915
## [17,] 3.576753 3.578287
## [18,] 3.548353 3.636292
## [19,] 3.678958 3.706706
## [20,] 5.224433 5.341541

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.942                          1                  1    
##  2                       0.772                          1                  0.934
##  3                       1                              1                  1    
##  4                       0.951                          1                  1    
##  5                       0.999                          1                  0.540
##  6                       1                              1                  1    
##  7                       0.755                          1                  1    
##  8                       1                              1                  1    
##  9                       0.999                          1                  1    
## 10                       0.755                          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.697         -0.789         -0.706                    -1.01 
##  2        -0.0101        -0.406         -0.128                    -0.603
##  3        -0.0656        -0.471         -0.0767                    0.202
##  4         0.272         -0.133          0.572                     0.471
##  5        -0.173         -0.127         -0.149                    -0.246
##  6        -0.967         -0.424         -1.12                     -0.497
##  7        -0.230         -0.227         -0.117                     0.240
##  8         0.291         -0.161         -0.104                    -1.50 
##  9        -0.121         -0.0609        -0.297                     0.445
## 10        -0.113         -0.0204        -0.118                    -0.894
## # ℹ 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.29 0.325 0.239 0.171 0.203 ...