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] 340 569 680 873 194 593 268 861 606 300 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  340  276  449  632  721  652  241   98  637   413
##  [2,]  569  215  593  203  710  515  394  550  626   143
##  [3,]  680  397  217  804  792  293  713  961  879   736
##  [4,]  873  664  217  680  119  195  622  814  529   609
##  [5,]  194  452  653  104  353  582  435  232  320   973
##  [6,]  593  957  203  975  637   22  580  906  720   595
##  [7,]  268  717  219  562  397  945  757  293  764   638
##  [8,]  861  942   38  352  679  420  498  839  433   964
##  [9,]  606  757  726  609  252  541  505  464    4   999
## [10,]  300  992  600  289  770  537  504  925  987   454
## [11,]  455  786  122  660  503  584  805  117  480   833
## [12,]  319  786  443  685  553   32  440  122  615   805
## [13,]  141  829  418  244  654  779  451  926  781   372
## [14,]  607  392  919  882  822  975  591  507  436   658
## [15,]  685  698  220  492  116  518  595  964  531   534
## [16,]  370  843  185  994   35  846  583   59   41   278
## [17,]  963  322  944  718  608  451  466   25  837   864
## [18,]   50  957  447  615  758  763   75  201  617   959
## [19,]   76  238  293  719  743  365  871  361  432   987
## [20,]  891  745  691  828  388   37  807  876  118   110
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.02 3.3 2.47 2.88 4.05 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.018629 3.031373 3.079598 3.182182 3.190289 3.270748 3.311817 3.328056
##  [2,] 3.300249 3.364253 3.408951 3.410414 3.434031 3.439393 3.477720 3.530245
##  [3,] 2.469631 2.539880 2.551225 2.670193 2.722951 2.789880 2.842039 2.967493
##  [4,] 2.884935 3.034378 3.075735 3.093143 3.129549 3.252635 3.268616 3.300838
##  [5,] 4.045851 4.171463 4.480685 4.521321 4.545654 4.591897 4.603009 4.651798
##  [6,] 2.715516 2.818171 3.031190 3.031923 3.071348 3.181328 3.254148 3.291665
##  [7,] 3.244079 3.849132 4.107612 4.123144 4.597226 4.665406 4.742255 4.899291
##  [8,] 2.880464 2.975964 2.992927 3.134780 3.149033 3.209440 3.351930 3.369912
##  [9,] 3.200608 3.556273 3.599590 3.632178 3.644647 3.651096 3.681008 3.797230
## [10,] 2.370303 3.255099 3.340992 3.388649 3.473889 3.524301 3.559977 3.613140
## [11,] 3.672853 3.877440 4.191101 4.233251 4.250197 4.308944 4.417027 4.431338
## [12,] 3.786465 3.840168 3.903663 3.909107 3.990217 3.995549 4.123284 4.136472
## [13,] 5.449527 6.001808 6.480283 6.545356 6.729681 6.852658 6.872787 7.032034
## [14,] 3.400413 3.497519 3.581049 3.614318 3.734166 3.760073 3.790119 3.792057
## [15,] 2.649857 2.839818 2.862886 3.061995 3.185397 3.191899 3.209112 3.237000
## [16,] 3.719502 4.187192 4.364447 4.372877 4.395679 4.407047 4.466695 4.473370
## [17,] 3.824482 4.020163 4.291948 4.329155 4.420129 4.502649 4.511740 4.530550
## [18,] 3.284251 3.747092 3.929797 3.964912 3.971378 3.973010 3.995078 4.044295
## [19,] 3.084338 3.222549 3.367862 3.374281 3.427643 3.432455 3.635662 3.717587
## [20,] 3.647931 3.910030 3.994346 4.355517 4.392176 4.588122 4.601240 4.661430
##           [,9]    [,10]
##  [1,] 3.335644 3.412268
##  [2,] 3.574460 3.619868
##  [3,] 2.989109 3.013243
##  [4,] 3.315502 3.385059
##  [5,] 4.657912 4.670793
##  [6,] 3.338679 3.359385
##  [7,] 4.938847 4.947225
##  [8,] 3.573674 3.599806
##  [9,] 3.803833 3.873390
## [10,] 3.686943 3.708585
## [11,] 4.456726 4.488361
## [12,] 4.189893 4.233011
## [13,] 7.064499 7.080149
## [14,] 3.860964 3.907350
## [15,] 3.259374 3.323875
## [16,] 4.603207 4.617451
## [17,] 4.533160 4.560717
## [18,] 4.071243 4.081719
## [19,] 3.760651 3.762696
## [20,] 4.747423 4.753867

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.982                  0.989
##  2                           1                      0.982                  0.978
##  3                           1                      0.998                  0.978
##  4                           1                      0.982                  0.989
##  5                           1                      0.982                  0.985
##  6                           1                      0.896                  0.978
##  7                           1                      0.982                  0.989
##  8                           1                      0.982                  1    
##  9                           1                      0.982                  0.978
## 10                           1                      0.941                  0.989
## # ℹ 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.202         -0.0294        -0.289                     -0.534
##  2       -0.142         -0.171         -0.0665                     0.220
##  3       -0.252         -0.144         -0.0596                    -0.140
##  4       -0.125         -0.148         -0.00157                    0.554
##  5       -0.00887        0.180         -0.0171                     0.365
##  6       -0.161         -0.470         -0.558                      0.230
##  7       -0.326         -0.358          1.02                       0.296
##  8       -0.108         -0.00340       -0.0878                    -1.03 
##  9       -0.0416        -0.182         -0.232                     -1.03 
## 10       -0.0759        -0.478          0.314                     -0.159
## # ℹ 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.287 0.267 0.323 0.288 0.211 ...