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] 767 310 116 123 517 577 658 381 83 153 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  767  846  404  975  730  236  717  958  961   687
##  [2,]  310  157  663  648   86  378  994  738   80   248
##  [3,]  116  794  718  868  553  447  134  968  267   315
##  [4,]  123  950  604  680  516  678  200  426  627   620
##  [5,]  517  492  788  484   33  996  214  467  455    98
##  [6,]  577  301  987  138  818  413  705  560  813   467
##  [7,]  658  519  202  737   16  325  356  455  294   193
##  [8,]  381  810  638  730   97  896   82  975  792    41
##  [9,]   83  353  557  216  225  970  692  234  354   629
## [10,]  153   55  818  579  396  398  670  390  293   684
## [11,]  826  515  759  948  892  525  217  364  945   175
## [12,]  615  348  546  255  464  381  980  164  541   555
## [13,]  836  344  959  478  784  472  588  871  134   178
## [14,]  504  653  317  673  966  952  794  588  666   783
## [15,]  319  377  416  248  918  566   29  487  105    80
## [16,]  737  432  587  633  202  930  844  579  658   243
## [17,]   22  111  136   85  858  903  214  922  106   996
## [18,]  182  174  398  397  203  342  722  435  752   475
## [19,]  652   94  403   79  873  782  553  287  905   340
## [20,]  291  715  205  822  786  342  596  829  475   837
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.6 3.06 2.91 3.2 3.65 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.601931 3.734699 3.794082 3.915970 3.976458 3.996245 4.004307 4.022556
##  [2,] 3.061600 3.467129 3.573239 3.947331 3.987906 3.989755 4.044506 4.144787
##  [3,] 2.908461 2.996471 3.205213 3.212433 3.272868 3.274541 3.359775 3.377650
##  [4,] 3.199075 3.287635 3.425696 3.488692 3.620936 3.629050 3.670389 3.671473
##  [5,] 3.654094 3.678958 3.875789 4.023613 4.051438 4.116374 4.195495 4.208327
##  [6,] 2.999438 3.090575 3.127478 3.170588 3.220559 3.221523 3.300110 3.377360
##  [7,] 2.999932 3.044497 3.169950 3.246766 3.320653 3.473322 3.549419 3.603670
##  [8,] 2.556705 2.649796 2.794155 2.800484 2.855793 2.986650 2.989920 3.023547
##  [9,] 3.473602 3.696610 3.766708 3.841489 3.845948 3.930391 4.035887 4.048120
## [10,] 3.637631 4.394792 4.591897 4.621630 4.627091 4.651798 4.670793 4.692810
## [11,] 3.079229 3.165821 3.170567 3.178897 3.196916 3.266841 3.269239 3.428236
## [12,] 3.234716 3.426301 3.434113 3.503835 3.611451 3.612683 3.623632 3.629190
## [13,] 3.016322 3.263121 3.277102 3.291503 3.309315 3.330960 3.385717 3.439355
## [14,] 2.893655 3.021908 3.030025 3.263179 3.305017 3.310881 3.352035 3.386877
## [15,] 5.552992 5.652755 5.805457 5.842389 6.078806 6.087200 6.122799 6.191659
## [16,] 2.724922 2.853686 2.872871 2.903003 2.943644 2.960460 3.082562 3.110730
## [17,] 3.121466 3.127203 3.312508 3.445453 3.452956 3.455038 3.459874 3.554004
## [18,] 4.120388 4.148914 4.162334 4.238401 4.272652 4.427347 4.464406 4.558033
## [19,] 3.566107 3.738404 3.778731 3.789020 3.803238 3.818419 3.954976 4.026742
## [20,] 3.576371 4.386751 4.700618 5.209585 5.219471 5.268104 5.365884 5.376842
##           [,9]    [,10]
##  [1,] 4.030236 4.092036
##  [2,] 4.165210 4.183539
##  [3,] 3.399999 3.431571
##  [4,] 3.696871 3.712238
##  [5,] 4.229447 4.240194
##  [6,] 3.427583 3.435826
##  [7,] 3.667501 3.689459
##  [8,] 3.083338 3.107908
##  [9,] 4.049054 4.067861
## [10,] 4.715257 4.723453
## [11,] 3.434775 3.475361
## [12,] 3.632135 3.651992
## [13,] 3.615325 3.645284
## [14,] 3.500371 3.646421
## [15,] 6.206025 6.371971
## [16,] 3.252051 3.270113
## [17,] 3.555447 3.575888
## [18,] 4.590076 4.590767
## [19,] 4.070396 4.075780
## [20,] 5.541786 5.564380

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.959                      0.928                  0.888
##  2                       0.798                      0.939                  0.989
##  3                       0.928                      0.997                  0.937
##  4                       0.913                      0.964                  0.937
##  5                       0.896                      0.928                  0.971
##  6                       0.905                      0.928                  0.995
##  7                       0.941                      0.928                  1    
##  8                       0.898                      0.928                  1    
##  9                       0.896                      0.965                  0.971
## 10                       0.999                      0.928                  0.964
## # ℹ 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.180         -0.255         -0.0147                   -0.455 
##  2       -0.419         -0.166         -0.611                    -0.716 
##  3        0.197         -0.177         -0.0855                   -0.343 
##  4       -0.00931       -0.0592        -0.430                     0.207 
##  5       -0.0663        -0.0284        -0.180                     0.385 
##  6       -0.133         -0.0245        -0.0975                    0.157 
##  7       -0.153         -0.217         -0.192                     0.753 
##  8        0.478         -0.132          0.0318                   -0.0733
##  9       -0.0785        -0.00929       -0.00777                  -0.802 
## 10        0.372         -0.138          0.107                     0.194 
## # ℹ 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.239 0.232 0.282 0.267 0.23 ...