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] 196 148 683 689 20 754 44 573 774 293 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  196  378  235  875  548  311  179  181  149   556
##  [2,]  148  189  714  152  795  957  308  562  498    62
##  [3,]  683   33  903  837  421  936  359  677  628   927
##  [4,]  689   11  639  321  348  203   18  747  749   178
##  [5,]   20  802  885  826  356   64  456  533   90   574
##  [6,]  754   91  207  542  635  625  192  213  686   716
##  [7,]   44  835  450  731  140  652  161  701  420   577
##  [8,]  573  398  534  654  802  885  133  795  330   465
##  [9,]  774  236  992  539  185  879  625  571  499   426
## [10,]  293   32  275  567  661  436  385  880  433   351
## [11,]  121  547  689  321  203    4  528  749  550    59
## [12,]  225  627  290  241  372  872  503  139  772   887
## [13,]  887  960  276  627  708  772   12  785  872    31
## [14,]  771  259  765  813  797  580  760  558  176   988
## [15,]  644  809  384  325  982  576  187  129  704   697
## [16,]  195  148  314  836  562  391  356  802  882   456
## [17,]  387  181  285  640  736  107  378   23  440   149
## [18,]  626  203  425  747  157  595  973   11  321   110
## [19,]  952  555  876  197  681  196  940  743  514    35
## [20,]    5  802  651  885  546  161  330  177  420   533
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.64 2.95 3.15 4.07 2.2 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.635267 3.159057 3.313186 3.326059 3.334992 3.368979 3.588107 3.628469
##  [2,] 2.954442 3.125124 3.126682 3.219256 3.253785 3.324642 3.453518 3.465046
##  [3,] 3.154132 3.677320 3.979203 4.139017 4.191824 4.239740 4.334593 4.367278
##  [4,] 4.065915 4.407605 4.990545 5.016599 5.145195 5.320813 5.351538 5.375015
##  [5,] 2.197611 2.366281 2.565037 2.573734 2.635397 2.650510 2.855092 2.869122
##  [6,] 3.955591 4.365962 4.390887 4.459106 4.617391 4.713135 4.716385 4.831987
##  [7,] 2.501066 2.723471 2.826313 2.885968 2.888455 2.951259 3.002221 3.098564
##  [8,] 2.737095 2.754134 2.757017 2.811023 2.812476 2.868728 2.881418 2.886851
##  [9,] 4.038439 4.161308 4.285868 4.428155 4.560822 4.706743 4.834206 4.844865
## [10,] 2.396558 3.103443 3.259011 3.304500 3.369705 3.482707 3.581698 3.653757
## [11,] 3.390878 4.022779 4.066337 4.142060 4.281859 4.407605 4.483772 4.518206
## [12,] 3.925311 4.078974 4.134332 4.171182 4.189340 4.298179 4.408899 4.422572
## [13,] 4.699835 4.721952 4.955175 4.974715 5.221231 5.298123 5.473469 5.488858
## [14,] 3.986641 4.134249 4.305541 4.367062 4.375356 4.459449 4.502357 4.534870
## [15,] 3.141165 3.314284 3.404386 3.515832 3.533695 3.595742 3.641223 3.645218
## [16,] 3.164105 3.172309 3.392121 3.443828 3.452237 3.534371 3.550348 3.695981
## [17,] 4.090237 4.294670 4.424466 4.469931 4.495317 4.507633 4.531170 4.550135
## [18,] 4.036209 4.131262 4.135165 4.331332 4.381185 4.456790 4.553719 4.737671
## [19,] 2.746469 3.076413 3.145634 3.175229 3.198276 3.291187 3.346875 3.364675
## [20,] 2.197611 2.402989 2.549984 2.638514 2.661766 2.706844 2.724573 2.806825
##           [,9]    [,10]
##  [1,] 3.680224 3.681337
##  [2,] 3.514027 3.519632
##  [3,] 4.393985 4.411200
##  [4,] 5.381195 5.421614
##  [5,] 2.885137 2.885680
##  [6,] 4.903150 4.935613
##  [7,] 3.184604 3.214041
##  [8,] 2.889691 2.927191
##  [9,] 4.930607 4.974619
## [10,] 3.678302 3.714439
## [11,] 4.525446 4.529158
## [12,] 4.432997 4.464119
## [13,] 5.525911 5.559348
## [14,] 4.822189 4.942998
## [15,] 3.702529 3.704667
## [16,] 3.702298 3.718399
## [17,] 4.564953 4.628969
## [18,] 4.818772 4.855006
## [19,] 3.392853 3.444106
## [20,] 2.895198 2.903233

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                       0.998                      0.856                  1    
##  2                       0.960                      0.955                  0.992
##  3                       0.958                      1                      0.791
##  4                       0.614                      0.971                  0.828
##  5                       0.958                      0.983                  0.730
##  6                       0.858                      0.902                  0.962
##  7                       1                          0.861                  0.777
##  8                       0.998                      0.856                  0.791
##  9                       0.982                      0.771                  0.966
## 10                       0.879                      0.771                  1    
## # ℹ 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.157        -0.168          -0.0231                  -0.0425
##  2        -0.0457       -0.215          -0.663                   -1.23  
##  3        -0.0369        0.745          -0.168                   -0.871 
##  4        -0.0710       -0.00365        -0.235                   -0.892 
##  5        -0.0179       -0.0305         -0.663                   -0.897 
##  6        -0.0160       -0.136          -0.204                   -0.392 
##  7        -0.458        -0.355          -0.366                    0.0316
##  8        -0.121        -0.0918         -0.283                   -1.11  
##  9        -0.0363       -0.184          -0.0884                   0.733 
## 10        -0.0403       -0.496          -0.108                   -1.01  
## # ℹ 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.264 0.276 0.219 0.179 0.336 ...