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] 213 503 979 665 326 259 918 237 110 563 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  213  320  133  331  694  761  724  201  520   963
##  [2,]  503  138  592  881  934  900  219  255  804   922
##  [3,]  979  749  777  988  252  310  246  900  422   687
##  [4,]  665  222  397  614  547  555  709  129   12   696
##  [5,]  326  911  879  717  669  782  427  682  231   210
##  [6,]  259  653  675  227  747  826  342  670  990   150
##  [7,]  918  707   11  306  630  226  960  233  135   595
##  [8,]  237  460  492  151  473  235  886  883  933    52
##  [9,]  110  331  457  113  721  598   83   27  839   816
## [10,]  563  864  928  585  593  723  857  854  272   374
## [11,]   31   27  336  590  960  226  111  966  248   875
## [12,]  796  397  344  651  165  210   77  884  529    95
## [13,]  285  939  203  212  325  363  482  192  891   244
## [14,]  754   95  326  466  938  316  917  529  637   793
## [15,]  684  141  219  484  794  493  900  385  582   752
## [16,]  414  730  289  988  881  412  951  219  123   517
## [17,]   66  489  931  442   54  153  661  347  934   168
## [18,]  949  323  228  999  607  511  177  639  849   140
## [19,]  861  314  843  181  136  531  194  287  523   578
## [20,]  523   74  175  161  691  765  529   80  651   314
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.22 2.54 3.6 3.87 3.32 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.224171 3.781679 3.814151 3.842906 3.896887 3.920367 3.966544 3.984069
##  [2,] 2.543838 3.007084 3.043255 3.048118 3.063609 3.099520 3.120598 3.134536
##  [3,] 3.597990 3.612581 3.631417 3.704716 3.724610 3.768494 3.784145 3.817330
##  [4,] 3.873524 3.966638 4.099818 4.118173 4.164315 4.335471 4.449264 4.475207
##  [5,] 3.319166 3.454767 3.462312 3.480990 3.495325 3.584387 3.591139 3.678692
##  [6,] 4.744116 4.759412 4.822521 5.034340 5.223982 5.343851 5.401241 5.423766
##  [7,] 3.285076 3.341014 3.380446 3.427433 3.608696 3.615235 3.721213 3.757769
##  [8,] 3.182050 3.887973 3.985128 4.163017 4.234621 4.265337 4.358083 4.419198
##  [9,] 3.664541 4.241979 4.278298 4.292908 4.392679 4.449122 4.465376 4.487642
## [10,] 3.644204 4.122683 4.217476 4.265636 4.293315 4.331980 4.333862 4.351938
## [11,] 2.698241 2.799801 3.059686 3.062834 3.103149 3.136298 3.193783 3.253652
## [12,] 2.187504 2.392560 2.960871 2.998460 3.019414 3.058477 3.093719 3.101004
## [13,] 3.719667 3.857160 4.026514 4.204732 4.270014 4.276164 4.436698 4.460467
## [14,] 3.564791 3.737485 3.807738 3.855312 3.898207 4.008878 4.041281 4.049655
## [15,] 2.943314 3.121772 3.328729 3.355104 3.361507 3.422021 3.442892 3.488347
## [16,] 2.935189 3.105588 3.114884 3.277301 3.282398 3.374517 3.390027 3.402984
## [17,] 3.149844 3.670407 3.732428 3.792964 3.796161 3.916240 3.916421 3.966286
## [18,] 4.681334 4.841910 4.849899 4.903387 4.996874 5.035517 5.197326 5.323472
## [19,] 2.660734 2.744103 2.972224 3.222712 3.230650 3.262778 3.298726 3.299002
## [20,] 3.580379 3.956137 3.959542 3.988474 4.294317 4.355036 4.360842 4.368750
##           [,9]    [,10]
##  [1,] 3.991925 4.057609
##  [2,] 3.188910 3.199834
##  [3,] 3.883986 3.923170
##  [4,] 4.595665 4.611223
##  [5,] 3.697357 3.709681
##  [6,] 5.513702 5.565066
##  [7,] 3.869995 4.024257
##  [8,] 4.434484 4.529207
##  [9,] 4.521537 4.523671
## [10,] 4.357092 4.415192
## [11,] 3.255873 3.257816
## [12,] 3.102715 3.181871
## [13,] 4.474204 4.538710
## [14,] 4.059009 4.135997
## [15,] 3.503308 3.545867
## [16,] 3.460375 3.461995
## [17,] 4.014342 4.037680
## [18,] 5.579534 5.621358
## [19,] 3.410273 3.489173
## [20,] 4.384493 4.401984

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.602                  0.981
##  2                       0.875                      0.774                  1    
##  3                       1                          0.882                  0.982
##  4                       0.939                      0.937                  0.969
##  5                       0.800                      0.949                  0.981
##  6                       0.758                      0.882                  0.999
##  7                       1                          0.980                  0.847
##  8                       1                          0.712                  0.847
##  9                       1                          0.666                  1    
## 10                       1                          0.649                  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.0492        -0.161          -0.220                    -0.807
##  2       -0.157         -0.262          -0.0956                   -0.293
##  3       -0.00169       -0.00767        -0.207                    -1.19 
##  4       -0.236         -0.0121         -0.0770                    0.169
##  5       -0.547          0.474          -0.983                    -0.494
##  6       -0.185         -0.101          -0.350                    -0.128
##  7       -0.202         -0.0294         -0.289                    -0.534
##  8       -0.142         -0.171          -0.0665                    0.220
##  9       -0.00819        0.104          -0.155                     0.131
## 10       -0.741         -0.805          -1.12                     -1.02 
## # ℹ 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.244 0.306 0.253 0.217 0.265 ...