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"          
##  [3] "CD3(Cd112)Di"           "CD235-61-7-15(In113)Di"
##  [5] "CD3(Cd114)Di"           "CD45(In115)Di"         
##  [7] "CD19(Nd142)Di"          "CD22(Nd143)Di"         
##  [9] "IgD(Nd145)Di"           "CD79b(Nd146)Di"        
## [11] "CD20(Sm147)Di"          "CD34(Nd148)Di"         
## [13] "CD179a(Sm149)Di"        "CD72(Eu151)Di"         
## [15] "IgM(Eu153)Di"           "Kappa(Sm154)Di"        
## [17] "CD10(Gd156)Di"          "Lambda(Gd157)Di"       
## [19] "CD24(Dy161)Di"          "TdT(Dy163)Di"          
## [21] "Rag1(Dy164)Di"          "PreBCR(Ho165)Di"       
## [23] "CD43(Er167)Di"          "CD38(Er168)Di"         
## [25] "CD40(Er170)Di"          "CD33(Yb173)Di"         
## [27] "HLA-DR(Yb174)Di"       
## 
## $functional
##  [1] "pCrkL(Lu175)Di"  "pCREB(Yb176)Di"  "pBTK(Yb171)Di"  
##  [4] "pS6(Yb172)Di"    "cPARP(La139)Di"  "pPLCg2(Pr141)Di"
##  [7] "pSrc(Nd144)Di"   "Ki67(Sm152)Di"   "pErk12(Gd155)Di"
## [10] "pSTAT3(Gd158)Di" "pAKT(Tb159)Di"   "pBLNK(Gd160)Di" 
## [13] "pP38(Tm169)Di"   "pSTAT5(Nd150)Di" "pSyk(Dy162)Di"  
## [16] "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] 871 628 147 351 895 994 478 2 635 625 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  871  628  773  436  735  155  804  798  910   235
##  [2,]  628    8  922   65  842  153  839   88  310   156
##  [3,]  147  315  661  655  102  285  609  905  922   888
##  [4,]  351  231  740  671  983  820  468  341  993   875
##  [5,]  895  528  232  579  981  930  700  167  970   743
##  [6,]  994  943  262  947  831  909  952  789  979   318
##  [7,]  478  666  274  914  928  772   30  925  577    26
##  [8,]    2  310  409  922   88  284  628  153  507   842
##  [9,]  635  485   36  173  920  412  539  398  265   694
## [10,]  625  202  770  329  264  396  609  123  253   580
## [11,]  832  533  388  339   88  730  555  295  463   550
## [12,]  971   89  946  811  947  179  292  161  589   104
## [13,]  409  510  622  285  533  460  449  288  467   628
## [14,]  880   40  796  772  256  797  363   94  767   928
## [15,]  163  504  337  772  464   51  675  548  706   261
## [16,]  792  970  790  385  743  499  547  579  180   895
## [17,]  896  774  607  332  112  499  970  895  260   475
## [18,]  218  774  272  499  701  743  607  242  492   358
## [19,]  389  399  612  516  407  587   45  586  119   676
## [20,]  996  781  148  339  153  832  735  467  773   529
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.76 2.41 3.17 2.86 3.51 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]
##  [1,] 2.755977 2.766653 2.926942 2.964926 2.979968 2.985558 3.106700
##  [2,] 2.407407 2.775803 2.815737 2.919770 3.008812 3.015529 3.068766
##  [3,] 3.169344 3.289579 3.307120 3.394217 3.465452 3.473550 3.498359
##  [4,] 2.864089 2.953933 2.978737 2.993218 2.997617 3.205448 3.217035
##  [5,] 3.511983 3.600126 3.710470 3.795016 3.807232 3.881188 3.903024
##  [6,] 1.991096 2.139081 2.595782 2.607109 2.774993 2.812816 2.823113
##  [7,] 4.074958 4.114655 4.159029 4.167930 4.238503 4.339856 4.674532
##  [8,] 2.775803 2.900045 3.019769 3.145088 3.195962 3.221582 3.267354
##  [9,] 3.392699 3.467530 3.586636 3.682744 3.800792 3.823090 3.888237
## [10,] 3.052123 3.776271 3.863842 4.128827 4.157861 4.233044 4.238328
## [11,] 2.435656 2.630898 2.637653 2.701264 2.750633 2.854395 2.907796
## [12,] 2.343261 2.696411 2.809258 2.967441 3.159903 3.174668 3.275361
## [13,] 3.584366 3.735117 3.786670 3.864378 3.879198 3.916573 3.931665
## [14,] 2.948288 3.363588 3.490019 3.694749 3.748988 3.773205 3.778793
## [15,] 3.650100 3.678814 3.708406 3.713332 3.717871 3.740663 3.782321
## [16,] 3.708601 3.783014 3.896379 3.960447 4.028409 4.052049 4.073326
## [17,] 3.943752 4.112826 4.170143 4.332826 4.336712 4.391361 4.393850
## [18,] 4.304730 4.468533 4.497335 4.541565 4.577728 4.847785 4.857236
## [19,] 3.810662 4.029647 4.324104 4.352560 4.473270 4.751188 4.770297
## [20,] 2.990672 3.125352 3.158027 3.190994 3.211358 3.323551 3.338076
##           [,8]     [,9]    [,10]
##  [1,] 3.128773 3.145789 3.310189
##  [2,] 3.071325 3.072693 3.195735
##  [3,] 3.515097 3.608535 3.609777
##  [4,] 3.223906 3.291146 3.331427
##  [5,] 3.994113 4.012849 4.154509
##  [6,] 2.841366 2.908637 2.978875
##  [7,] 4.694901 4.753449 4.758575
##  [8,] 3.354773 3.411198 3.460021
##  [9,] 4.024737 4.049032 4.106147
## [10,] 4.247532 4.266171 4.270631
## [11,] 2.914027 2.919281 2.930220
## [12,] 3.368900 3.462550 3.568787
## [13,] 3.935371 3.963441 3.963589
## [14,] 3.790889 3.883496 3.891902
## [15,] 3.816347 3.848661 3.906551
## [16,] 4.075601 4.156030 4.205589
## [17,] 4.493268 4.514198 4.523136
## [18,] 4.895267 4.932999 4.938203
## [19,] 4.954891 5.079034 5.148119
## [20,] 3.375435 3.388791 3.397870

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 x 34
##    `pCrkL(Lu175)Di~ `pCREB(Yb176)Di~ `pBTK(Yb171)Di.~ `pS6(Yb172)Di.I~
##               <dbl>            <dbl>            <dbl>            <dbl>
##  1                1                1            0.991            0.907
##  2                1                1            0.991            0.951
##  3                1                1            0.991            0.948
##  4                1                1            0.991            0.977
##  5                1                1            0.991            0.957
##  6                1                1            0.991            0.948
##  7                1                1            0.991            0.857
##  8                1                1            0.991            0.907
##  9                1                1            0.991            0.852
## 10                1                1            0.554            0.978
## # ... with 990 more rows, and 30 more variables:
## #   `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>, `pAKT(Tb159)Di.IL7.qvalue` <dbl>,
## #   `pBLNK(Gd160)Di.IL7.qvalue` <dbl>, `pP38(Tm169)Di.IL7.qvalue` <dbl>,
## #   `pSTAT5(Nd150)Di.IL7.qvalue` <dbl>, `pSyk(Dy162)Di.IL7.qvalue` <dbl>,
## #   `tIkBa(Er166)Di.IL7.qvalue` <dbl>, `pCrkL(Lu175)Di.IL7.change` <dbl>,
## #   `pCREB(Yb176)Di.IL7.change` <dbl>, `pBTK(Yb171)Di.IL7.change` <dbl>,
## #   `pS6(Yb172)Di.IL7.change` <dbl>, `cPARP(La139)Di.IL7.change` <dbl>,
## #   `pPLCg2(Pr141)Di.IL7.change` <dbl>, `pSrc(Nd144)Di.IL7.change` <dbl>,
## #   `Ki67(Sm152)Di.IL7.change` <dbl>, `pErk12(Gd155)Di.IL7.change` <dbl>,
## #   `pSTAT3(Gd158)Di.IL7.change` <dbl>, `pAKT(Tb159)Di.IL7.change` <dbl>,
## #   `pBLNK(Gd160)Di.IL7.change` <dbl>, `pP38(Tm169)Di.IL7.change` <dbl>,
## #   `pSTAT5(Nd150)Di.IL7.change` <dbl>, `pSyk(Dy162)Di.IL7.change` <dbl>,
## #   `tIkBa(Er166)Di.IL7.change` <dbl>, IL7.fraction.cond.2 <dbl>,
## #   density <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 x 51
##    `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(~
##             <dbl>          <dbl>          <dbl>            <dbl>
##  1        -0.362         -0.423         -1.01             -0.787
##  2        -0.0595        -0.264         -0.454            -1.16 
##  3        -0.106          0.388         -0.143             0.665
##  4        -0.0160        -0.136         -0.204            -0.392
##  5        -0.180         -0.255         -0.0147           -0.455
##  6         0.622          1.01          -0.262            -0.427
##  7        -0.0179        -0.0305        -0.663            -0.897
##  8        -0.352         -0.137         -0.458            -0.859
##  9        -0.659          1.29          -0.955            -0.209
## 10        -0.388         -0.839         -0.392             0.560
## # ... with 20 more rows, and 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>, `PreBCR(Ho165)Di` <dbl>, `CD43(Er167)Di` <dbl>,
## #   `CD38(Er168)Di` <dbl>, `CD40(Er170)Di` <dbl>, `CD33(Yb173)Di` <dbl>,
## #   `HLA-DR(Yb174)Di` <dbl>, Time <dbl>, Cell_length <dbl>,
## #   `cPARP(La139)Di` <dbl>, `pPLCg2(Pr141)Di` <dbl>,
## #   `pSrc(Nd144)Di` <dbl>, `pSTAT5(Nd150)Di` <dbl>, `Ki67(Sm152)Di` <dbl>,
## #   `pErk12(Gd155)Di` <dbl>, `pSTAT3(Gd158)Di` <dbl>,
## #   `pAKT(Tb159)Di` <dbl>, `pBLNK(Gd160)Di` <dbl>, `pSyk(Dy162)Di` <dbl>,
## #   `tIkBa(Er166)Di` <dbl>, `pP38(Tm169)Di` <dbl>, `pBTK(Yb171)Di` <dbl>,
## #   `pS6(Yb172)Di` <dbl>, `pCrkL(Lu175)Di` <dbl>, `pCREB(Yb176)Di` <dbl>,
## #   `DNA1(Ir191)Di` <dbl>, `DNA2(Ir193)Di` <dbl>,
## #   `Viability1(Pt195)Di` <dbl>, `Viability2(Pt196)Di` <dbl>,
## #   wanderlust <dbl>, condition <chr>
# 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.302 0.31 0.266 0.295 0.235 ...