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] 744 32 856 51 608 632 879 4 318 18 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  744  818  728  335  934   22  762  306  202   587
##  [2,]   32  306  886  818  475  314  161  819  124   671
##  [3,]  856  948  391  359  281  497  895  692  707   659
##  [4,]   51  127  757  583  324  964  340  581  771   240
##  [5,]  608  645  426  650  493  422  502  917  903    73
##  [6,]  632  839  271  857  455  977   61  300  234   438
##  [7,]  879  911  552  226  547  750  372  541  567   492
##  [8,]    4  126  456  298  581  237  583  393  324   127
##  [9,]  318  166  310  343  360  647   48  280  635   653
## [10,]   18   99  370  980  555  315  903  365  731   502
## [11,]  226  879  678  359  461  922  734   39    5     7
## [12,]  596  269   98  690  282  978   95   60  243   148
## [13,]   63  904  754   61  625  910  462  434  203   938
## [14,]  672  644  763   29  297  278  909  296  821    52
## [15,]  130  418  407  927  846  166  900  232  181   688
## [16,]  615  822  138  169   92  341  938  486  175   791
## [17,]    6  999  866  782  889  500  429  102   89   455
## [18,]  731  715   10  418  543  422  493  650   76   468
## [19,]   82  766  967  710  550  146  783  971   22   574
## [20,]  878  497  522  563  935  968  416  452  415   702
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.95 3.21 2.18 2.58 3.78 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.950659 3.083706 3.457156 3.623170 3.656035 3.695119 3.696015 3.706676
##  [2,] 3.211234 3.292320 3.303263 3.333152 3.481355 3.604993 3.643893 3.656955
##  [3,] 2.184495 2.398957 2.551225 2.853031 2.968610 2.996549 3.028090 3.065379
##  [4,] 2.582158 2.709808 2.750523 2.797013 2.802767 2.803004 2.860145 2.862379
##  [5,] 3.783014 3.896379 4.052049 4.075601 4.124767 4.156030 4.205589 4.221158
##  [6,] 3.208516 3.210629 3.229654 3.233527 3.312319 3.317971 3.346789 3.406806
##  [7,] 3.526917 3.758880 3.810975 4.070028 4.117044 4.181184 4.394502 4.414028
##  [8,] 2.954442 3.082324 3.125124 3.219256 3.324642 3.434119 3.465046 3.471081
##  [9,] 4.398899 4.409877 4.465460 4.466785 4.593583 4.642551 4.674345 4.681658
## [10,] 3.534774 4.171182 4.314909 4.434783 4.436591 4.496290 4.554011 4.588079
## [11,] 4.337072 4.456780 4.458585 4.781641 4.913038 5.052050 5.072518 5.078147
## [12,] 5.330974 5.433805 5.611114 5.650293 5.661905 5.672119 5.723587 5.768794
## [13,] 3.672089 3.697218 3.779669 3.922627 4.048636 4.073322 4.128494 4.260912
## [14,] 3.249785 3.733174 4.238197 4.365529 4.599008 4.628928 4.658578 4.697713
## [15,] 5.719366 5.841000 6.146462 6.209535 6.234971 6.276826 6.287511 6.336659
## [16,] 3.831873 4.309240 4.472985 4.843249 4.881974 4.915724 4.999745 5.157341
## [17,] 4.969233 5.021415 5.088030 5.140528 5.145146 5.260817 5.330267 5.359440
## [18,] 3.220076 3.493143 3.534774 3.593361 3.619259 3.627505 3.660338 3.722327
## [19,] 2.102466 2.659198 3.344346 3.348197 3.484666 3.491188 3.584454 3.662005
## [20,] 3.166222 3.461670 3.602633 3.628453 3.713589 3.724005 3.732426 3.750626
##           [,9]    [,10]
##  [1,] 3.711048 3.789614
##  [2,] 3.675832 3.679565
##  [3,] 3.078487 3.127765
##  [4,] 2.894769 2.900045
##  [5,] 4.293006 4.394186
##  [6,] 3.500036 3.514821
##  [7,] 4.488203 4.548506
##  [8,] 3.519632 3.592783
##  [9,] 4.714946 4.722383
## [10,] 4.620060 4.677227
## [11,] 5.208353 5.235435
## [12,] 5.863151 5.879997
## [13,] 4.331258 4.365954
## [14,] 4.760430 4.769281
## [15,] 6.423392 6.558309
## [16,] 5.198759 5.239382
## [17,] 5.399785 5.416715
## [18,] 3.818443 3.862997
## [19,] 3.678749 3.697780
## [20,] 3.796046 3.819036

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               0.985               1                  0.935              0.847
##  2               1                   1                  0.673              0.982
##  3               0.985               1                  0.784              1    
##  4               0.985               0.858              0.433              0.954
##  5               0.985               1                  1                  1    
##  6               0.985               0.969              0.590              1    
##  7               0.985               1                  0.784              1    
##  8               0.985               0.969              0.331              0.888
##  9               0.985               0.949              0.798              1    
## 10               0.985               1                  0.806              0.718
## # … 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.0595        -0.264           -0.454           -1.16          -0.416 
##  2       -0.362         -0.423           -1.01            -0.787         -1.07  
##  3        0.247         -0.498           -0.639           -0.0719        -0.495 
##  4       -0.00552       -0.187           -0.142           -1.89           0.102 
##  5       -0.0759        -0.478            0.314           -0.159         -0.314 
##  6       -0.278         -0.00435         -0.131           -1.06          -0.292 
##  7       -0.241         -0.0568          -0.148           -0.0476        -0.0679
##  8       -0.0980        -0.00653         -0.116           -0.826         -0.133 
##  9       -0.158         -0.0951          -0.235           -0.369         -0.246 
## 10       -0.0957        -0.176           -0.199           -0.493         -0.0135
## # … 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.262 0.268 0.32 0.332 0.22 ...