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] 750 68 433 452 554 273 608 802 172 596 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  750  658  709  702  575  170  796   68  730   347
##  [2,]   68  484   64  166  538   97  382  345  803   683
##  [3,]  433  549  645   65  420  104  667  284  331   115
##  [4,]  452  843  669  948  769  564  961  791   26   212
##  [5,]  554  690  692  249  492  694  842   34  671   885
##  [6,]  273  315  357  762  381  382  783  829  688   960
##  [7,]  608  814  822  167  900   96  941  240  792    86
##  [8,]  802  769  905  553  753  783  332  190  763   105
##  [9,]  172  713   79  883  121  687  610  107  671   776
## [10,]  596  317  825   88  640  501  840  515  122   597
## [11,]  696  607  188  775  777  455   97  583  463   180
## [12,]   23  384  870  224  520   74   73  368   14   844
## [13,]  440  922  436  580  792  380  822  662  608   565
## [14,]  461   74  744  523  224  765  146  925  151   959
## [15,]  316  588  365  341  481  419  351  811  447   649
## [16,]  338  143  866  331  575  660  104  509  930   755
## [17,]  135   33  121  981  687  499  568  776  813   394
## [18,]  367  670   77  204   51  229  991   42  130   685
## [19,]  299  363  254  803  661  484  647    2   84   307
## [20,]  277  177   42   71  112  956  986  407  460   448
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.95 3.05 2.9 2.92 2.34 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]
##  [1,] 3.951312 4.078269 4.090827 4.128266 4.291013 4.299045 4.310667
##  [2,] 3.045367 3.072616 3.165143 3.169765 3.390994 3.406348 3.425642
##  [3,] 2.902729 2.917114 3.200399 3.226009 3.545225 3.557851 3.612108
##  [4,] 2.921099 2.936044 2.995608 3.135549 3.151006 3.396874 3.437473
##  [5,] 2.336095 2.998369 3.052081 3.105921 3.361319 3.499439 3.511252
##  [6,] 3.380156 3.595051 3.611403 3.650910 3.651106 3.702436 3.792771
##  [7,] 3.732467 3.943752 4.170143 4.336712 4.493268 4.539107 4.560543
##  [8,] 3.583994 3.698748 3.718052 3.739959 3.770411 3.835693 3.860846
##  [9,] 3.503807 3.571807 3.728724 3.833117 3.873347 3.918159 3.927282
## [10,] 3.844795 3.888269 3.976899 4.009838 4.053995 4.224075 4.347911
## [11,] 3.320610 3.457156 3.457709 3.470432 3.543018 3.623170 3.695119
## [12,] 3.174668 3.262967 3.307915 3.411497 3.595608 3.611575 3.612689
## [13,] 4.161973 4.374602 4.714625 4.768746 4.972629 4.979035 5.152720
## [14,] 2.183445 2.213633 2.467871 2.548933 2.564574 2.624695 2.696517
## [15,] 3.664541 4.140936 4.241979 4.269572 4.278298 4.292908 4.325495
## [16,] 2.740607 2.853686 3.536080 3.781405 3.902603 3.916976 3.954297
## [17,] 3.526604 4.241187 4.311942 4.366695 4.371984 4.421682 4.429282
## [18,] 3.198415 3.234716 3.302682 3.345636 3.426301 3.445440 3.484984
## [19,] 4.317703 4.344680 4.522058 4.589563 4.601528 4.613518 4.684042
## [20,] 2.399169 2.767063 2.896945 2.896975 3.015529 3.070590 3.089206
##           [,8]     [,9]    [,10]
##  [1,] 4.314095 4.341169 4.344483
##  [2,] 3.447915 3.454167 3.571054
##  [3,] 3.623875 3.856667 4.187781
##  [4,] 3.468788 3.475168 3.487585
##  [5,] 3.523420 3.583414 3.628449
##  [6,] 3.829575 3.833993 3.834789
##  [7,] 4.679822 4.687230 4.718514
##  [8,] 3.907122 3.981942 3.998136
##  [9,] 3.936384 3.937841 3.992439
## [10,] 4.381019 4.428430 4.444949
## [11,] 3.696015 3.697610 3.706676
## [12,] 3.623273 3.628592 3.660328
## [13,] 5.219919 5.325664 5.327789
## [14,] 2.865699 2.873438 2.878373
## [15,] 4.392679 4.401311 4.449122
## [16,] 4.026121 4.067289 4.124898
## [17,] 4.597442 4.629036 4.741965
## [18,] 3.611451 3.629190 3.651992
## [19,] 4.722269 4.745259 4.746871
## [20,] 3.115053 3.180848 3.202088

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            0.933            0.850            1                0.934
##  2            0.615            0.987            1                1    
##  3            0.933            0.784            1                0.867
##  4            1                1                1                1    
##  5            1                0.913            1                0.989
##  6            0.776            0.904            1                0.949
##  7            0.980            0.851            1                0.989
##  8            0.776            0.816            0.933            0.521
##  9            0.794            0.751            1                0.985
## 10            0.913            0.751            1                0.892
## # ... 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.659           1.29          -0.955            -0.209
##  2       -0.189          -0.369         -0.587             0.291
##  3       -0.138          -0.155         -0.0782            0.305
##  4       -0.105           0.773          0.645            -0.587
##  5        0.237          -0.0447        -0.119            -0.717
##  6       -0.00438        -0.304         -0.248             0.347
##  7        0.504          -0.0952        -0.0422            0.453
##  8       -0.0363         -0.184         -0.0884            0.733
##  9       -0.438          -0.0758        -0.118            -0.678
## 10       -0.491           1.75          -0.411            -0.945
## # ... 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.226 0.277 0.238 0.277 0.266 ...