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] 832 817 38 471 797 758 807 999 165 75 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  832  666  302  130  625  468  939  912  594   504
##  [2,]  817  273  134   69   73  549  314  707  488   210
##  [3,]   38  200  225   10  639   52  637   75  281   319
##  [4,]  471   57   13  946  337   69  418  131  652   473
##  [5,]  797  464  653  998   28   34  615  201   82   544
##  [6,]  758   30  608  708  903  870  413  229  833   853
##  [7,]  807  238  724  581  480   25  863   11  214   184
##  [8,]  999  827  273  475   63  186  768  269  355   301
##  [9,]  165  900  687  316   96  757  178  329   23   958
## [10,]   75  685  323   92   38    3  794  667  281    52
## [11,]  807  887    7  238  762  970  422  581  441    61
## [12,]  214  264  916  298  950  181   51  326  819   425
## [13,]  496  471  102  564  131  764  265  867  326   348
## [14,]  201  615  280  710  628  636  653  677  190   991
## [15,]  696  240  938  520  666  335  888  477  250    33
## [16,]  227  618  283  856   74  232  840  513   77   208
## [17,]  304  954  461  453  801  985  100  846  508   845
## [18,]  260  666  611  702  692  625  939  916  912   554
## [19,]  210  707  770  817  881  371  293    2  297   312
## [20,]  606  431  784  644  844  297  436  878  770   371
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.76 2.87 3.48 2.84 3.73 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.755977 2.766653 2.809017 2.869076 2.926942 2.985558 3.030535 3.106700
##  [2,] 2.869122 3.292420 3.386856 3.444082 3.674934 3.693634 3.697950 3.715950
##  [3,] 3.482542 3.669566 4.183429 4.395033 4.589795 4.603207 4.672440 4.740314
##  [4,] 2.835728 2.840489 3.429947 3.508064 3.564146 3.601238 3.706476 3.749412
##  [5,] 3.732467 4.391361 4.393850 4.493268 4.514198 4.610233 4.687230 4.755811
##  [6,] 3.997052 4.150343 4.262393 4.370475 4.532881 4.559057 4.600119 4.707549
##  [7,] 2.941805 2.962524 3.005583 3.187454 3.208096 3.214857 3.250388 3.265534
##  [8,] 3.738085 3.799218 3.931162 3.947595 4.080164 4.089714 4.090986 4.136228
##  [9,] 4.599705 4.955823 5.273953 5.333594 5.451349 5.498463 5.515224 5.595893
## [10,] 3.679249 3.904158 3.942074 4.236226 4.313055 4.395033 4.456942 4.469473
## [11,] 2.852235 3.064180 3.265534 3.401118 3.516531 3.597213 3.641495 3.725871
## [12,] 2.973577 2.973670 3.013207 3.018629 3.025042 3.105979 3.122978 3.126406
## [13,] 2.698241 2.742485 2.924695 2.947488 2.951141 2.960109 3.015123 3.046227
## [14,] 4.543723 4.584606 4.681733 4.706540 4.730245 4.753758 4.798590 4.813634
## [15,] 2.310230 2.809737 3.018988 3.051324 3.072755 3.098712 3.110989 3.114917
## [16,] 2.849562 3.087177 3.229654 3.268915 3.320396 3.401931 3.417045 3.620138
## [17,] 4.658434 4.907575 4.908853 5.142862 5.181301 5.275184 5.375401 5.404872
## [18,] 2.242857 2.465524 2.541607 2.646095 2.681402 2.687919 2.795479 2.946453
## [19,] 4.478560 4.660287 4.682335 4.897369 5.028948 5.046384 5.056376 5.085931
## [20,] 3.844795 3.888269 3.976899 4.005640 4.224075 4.341237 4.428430 4.510202
##           [,9]    [,10]
##  [1,] 3.111678 3.145789
##  [2,] 3.744294 3.761022
##  [3,] 4.924132 4.950767
##  [4,] 3.752011 3.754423
##  [5,] 4.774496 4.776929
##  [6,] 4.722819 4.759005
##  [7,] 3.292698 3.319018
##  [8,] 4.141041 4.145359
##  [9,] 5.597697 5.634922
## [10,] 4.472910 4.473370
## [11,] 3.882224 3.914558
## [12,] 3.152739 3.238886
## [13,] 3.186649 3.203498
## [14,] 4.896688 5.032718
## [15,] 3.139106 3.156406
## [16,] 3.639266 3.681633
## [17,] 5.518690 5.564337
## [18,] 2.955116 3.032133
## [19,] 5.087400 5.114662
## [20,] 4.527934 4.557106

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.IL… `pCREB(Yb176)Di.IL… `pBTK(Yb171)Di.IL… `pS6(Yb172)Di.IL7…
##                  <dbl>               <dbl>              <dbl>              <dbl>
##  1               1                   0.999              0.967              0.998
##  2               0.968               0.999              0.727              0.973
##  3               0.968               0.999              0.823              0.920
##  4               0.957               0.999              0.474              0.881
##  5               1                   0.999              1                  0.730
##  6               0.968               0.999              0.907              0.843
##  7               0.968               0.999              0.986              0.968
##  8               1                   0.999              0.908              0.969
##  9               1                   0.999              0.871              0.966
## 10               0.897               0.999              0.973              0.928
## # … 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(I… `CD3(Cd114)Di`
##             <dbl>          <dbl>          <dbl>             <dbl>          <dbl>
##  1        -0.442         -0.271         -0.854            -1.83          -0.126 
##  2        -0.181          0.134          0.679             0.0851        -0.235 
##  3        -0.243         -0.0594        -0.402            -1.19          -0.225 
##  4        -0.126         -0.0820        -0.0859           -1.75          -0.132 
##  5        -0.248         -0.217         -0.352             0.628         -0.400 
##  6        -0.113         -0.176          0.456            -0.695         -0.0897
##  7         0.0523        -0.379         -0.550            -0.632         -0.604 
##  8        -0.908         -0.813         -0.287            -1.83          -0.271 
##  9        -0.254         -0.193         -0.121            -0.267         -0.0189
## 10        -0.443          0.648         -0.354            -2.12          -0.0684
## # … with 20 more rows, and 46 more variables: 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.311 0.259 0.196 0.262 0.204 ...