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] 936 988 396 545 716 275 518 16 616 861 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  936  498  741  638  377  422   22  500  333   137
##  [2,]  988  621  258  512   12  489  863  873  950   215
##  [3,]  396  801  523   42  331  548  509  734  780   939
##  [4,]  545   69  408  224  445  784  601  885  417   853
##  [5,]  716  772  823  549  317  594   39   11  432   938
##  [6,]  275  700  354  168  807  999  976  592  278   600
##  [7,]  518  208  795  492  326  752  526  331  168   910
##  [8,]   16  584  339  733  240  601  657  647  686   452
##  [9,]  616  217  419  139  310  133  363  756  973    83
## [10,]  861  497  773  767   24  160  805  921  632   982
## [11,]  772  842  594  224  851  408  598  423  845   858
## [12,]  464  810  580  258  433  890   68  873  439   587
## [13,]  520   80  425  635  514  252  418  227  445   346
## [14,]  621  146  569  282  637  587  983   55  873   750
## [15,]  184  107  416  864   73  571  242  399  110   192
## [16,]  584  764  590    8  733  521  663  213  647   957
## [17,]  783   20  758  879  437  235  187  322  681   668
## [18,]  636  504  900  521  906  590  996  133  911   920
## [19,]  706  650  233  708  514  544  904  369  776   561
## [20,]  856  234  559  839  956  298  875   25  713   729
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.71 3.91 2.57 2.49 4.08 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.707680 2.821755 3.080338 3.187454 3.208096 3.214857 3.265534 3.319018
##  [2,] 3.914548 4.268692 4.314909 4.366468 4.555297 4.558253 4.601060 4.609032
##  [3,] 2.574222 2.743768 2.923213 3.031135 3.189341 3.235170 3.347033 3.385220
##  [4,] 2.485762 3.154017 3.185010 3.527210 3.660883 3.669237 3.785822 3.810819
##  [5,] 4.076856 4.116485 4.159575 4.171534 4.172872 4.225500 4.272803 4.321498
##  [6,] 2.370303 3.304337 3.411283 3.435706 3.468961 3.524122 3.618231 3.682096
##  [7,] 3.819593 3.897873 3.909321 3.998455 4.135402 4.139994 4.206469 4.218315
##  [8,] 2.663104 3.128321 3.172732 3.201109 3.256129 3.265436 3.369040 3.408421
##  [9,] 3.048366 3.227690 3.285664 3.331935 3.577482 3.598409 3.685850 3.693053
## [10,] 2.977112 3.115130 3.115229 3.125238 3.274541 3.351861 3.415559 3.416516
## [11,] 2.796071 2.898579 3.008502 3.176828 3.521962 3.546036 3.577782 3.585439
## [12,] 2.867231 3.220076 3.493143 3.534774 3.589628 3.591454 3.593361 3.619259
## [13,] 2.898020 3.277176 3.593471 3.601077 3.738085 3.931162 3.947595 4.080164
## [14,] 4.046617 4.106526 4.415533 4.495225 4.720460 4.854444 4.866282 4.933615
## [15,] 4.192822 4.335475 4.370983 4.411843 4.471538 4.474514 4.603385 4.607520
## [16,] 2.454328 2.592222 2.644167 2.663104 2.714878 2.810516 2.825407 2.853666
## [17,] 3.052123 3.924477 4.111991 4.238328 4.247532 4.266171 4.530476 4.645340
## [18,] 3.583994 3.698748 3.739959 3.743319 3.770411 3.835693 3.860176 3.907122
## [19,] 3.685447 3.730388 3.731102 3.779581 3.843437 3.852244 3.906250 3.922677
## [20,] 2.897730 2.899267 3.236289 3.407082 3.498950 3.562572 3.584150 3.589647
##           [,9]    [,10]
##  [1,] 3.331857 3.380915
##  [2,] 4.646497 4.767159
##  [3,] 3.463256 3.550264
##  [4,] 3.918454 3.951798
##  [5,] 4.415725 4.425867
##  [6,] 3.700618 3.796981
##  [7,] 4.223790 4.235914
##  [8,] 3.415186 3.432905
##  [9,] 3.742533 3.868288
## [10,] 3.486595 3.509272
## [11,] 3.669028 3.717865
## [12,] 3.660755 3.715657
## [13,] 4.089714 4.145359
## [14,] 5.020411 5.024372
## [15,] 4.617145 4.632785
## [16,] 2.879802 2.956599
## [17,] 4.695619 4.725894
## [18,] 3.981942 4.024973
## [19,] 3.968094 3.975874
## [20,] 3.612289 3.675965

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.984                      0.951                  1    
##  2                       1                          0.951                  0.870
##  3                       0.998                      0.959                  0.874
##  4                       0.978                      0.951                  0.981
##  5                       0.989                      0.951                  0.943
##  6                       0.978                      0.959                  0.974
##  7                       0.978                      1                      0.981
##  8                       0.978                      0.959                  0.273
##  9                       0.989                      0.959                  0.836
## 10                       0.978                      0.959                  0.943
## # ℹ 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.218         -0.0910        -0.198                     0.145
##  2        -0.0209        -0.0434        -0.245                    -0.331
##  3        -0.215         -0.250         -0.0990                   -0.294
##  4        -0.474          0.346         -0.507                     0.964
##  5        -0.869         -0.427          0.197                    -1.07 
##  6        -0.104         -0.0145         0.276                    -0.288
##  7        -0.171         -0.126         -0.0613                    0.187
##  8        -0.234         -0.0633        -0.104                    -0.579
##  9        -0.0255        -0.414         -0.415                    -0.484
## 10         0.0970         0.530         -0.0602                    0.280
## # ℹ 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.284 0.205 0.282 0.253 0.221 ...