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] 569 163 378 1 374 691 762 618 588 592 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  569  549  762  701  126  274    4  287  983   371
##  [2,]  163  918  267   72  517  569  996  161  436   809
##  [3,]  378  113  683  311  221  806  964  527    6   848
##  [4,]    1  569  148  579  319  125  864  379  549   702
##  [5,]  374  281  531  626   88  443  234  209  654   637
##  [6,]  691  324  664  984  311  695  848  407  958   284
##  [7,]  762  371   19  973  380  274  453   66  549   608
##  [8,]  618  552  638  537  673  384  465  926  519   721
##  [9,]  588   37  662  101  136  994  787  394  360   834
## [10,]  592  412  918    2  498  629  998  163  353   745
## [11,]  983  722  452  569  534  631  838  579  273   549
## [12,]  556  485  814  897  698  655  439  203  559   671
## [13,]  632   66  674  934  760  579  137  549  452   501
## [14,]  888  823  549  380   35  961  989  810  558    19
## [15,]  391  100   29  709  242  238  423  893  361   828
## [16,]  749  811  321  741   73  296  785  320  724   451
## [17,]  761  691  564  682  717  830  898  507  746   881
## [18,]   40  935  506  588  570  196  354  101  364   382
## [19,]  371  898  558  823  379  762  549  232  453   651
## [20,]  836  785  689  862  609  465  552  780  329   243
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.89 2.76 4.43 3.4 2.65 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.889047 2.959003 3.051892 3.231673 3.375428 3.382220 3.399978 3.442843
##  [2,] 2.760060 2.976200 3.003804 3.063431 3.084616 3.098669 3.129153 3.192362
##  [3,] 4.430607 4.460762 4.726316 4.789495 4.866138 4.933621 5.033107 5.050487
##  [4,] 3.399978 3.578798 3.594594 3.652195 3.692372 3.788450 3.801918 3.847031
##  [5,] 2.654513 2.677292 2.748174 2.759145 2.860018 2.874252 2.954664 2.972508
##  [6,] 3.764624 3.774343 3.972435 4.087410 4.125692 4.145777 4.153422 4.157856
##  [7,] 3.068845 3.104493 3.135584 3.151310 3.259400 3.329819 3.362425 3.383731
##  [8,] 3.504955 3.690812 3.705962 3.779936 3.789844 3.790647 3.860449 3.980798
##  [9,] 3.195219 3.222712 3.531575 3.765714 3.782648 3.867581 3.884722 3.980986
## [10,] 3.372035 3.417346 3.489331 3.500729 3.507048 3.553182 3.591305 3.612839
## [11,] 3.936064 3.957039 4.018576 4.078126 4.127169 4.160121 4.175377 4.241703
## [12,] 2.231078 3.030025 3.263179 3.352035 3.500371 3.561672 3.628872 3.674316
## [13,] 3.060229 3.265195 3.267385 3.380296 3.389055 3.408295 3.408781 3.428510
## [14,] 2.953928 3.455763 3.472346 3.675817 3.750799 3.784599 3.791097 3.859296
## [15,] 4.543723 4.616482 4.681733 4.753758 4.844629 4.896688 4.943850 5.032718
## [16,] 3.611571 4.081170 4.117257 4.178623 4.198171 4.273988 4.314511 4.326576
## [17,] 2.704594 3.024071 3.156020 3.288490 3.366081 3.379164 3.412627 3.448923
## [18,] 3.656178 4.156667 4.202554 4.228298 4.262527 4.345102 4.354946 4.393399
## [19,] 2.732229 2.867876 2.895732 2.899957 2.970063 3.027255 3.042406 3.080280
## [20,] 2.288887 2.671711 2.714452 2.772077 2.781540 2.874490 2.890439 2.948353
##           [,9]    [,10]
##  [1,] 3.455540 3.506941
##  [2,] 3.252045 3.262275
##  [3,] 5.070263 5.076888
##  [4,] 3.847290 3.868582
##  [5,] 2.976826 3.037446
##  [6,] 4.278616 4.324602
##  [7,] 3.496010 3.504053
##  [8,] 4.000098 4.006072
##  [9,] 4.071600 4.097719
## [10,] 3.637218 3.662847
## [11,] 4.270395 4.273027
## [12,] 3.682604 3.701159
## [13,] 3.429724 3.462020
## [14,] 3.909524 3.909804
## [15,] 5.062698 5.064583
## [16,] 4.375059 4.383333
## [17,] 3.476167 3.485351
## [18,] 4.487575 4.505185
## [19,] 3.115715 3.116108
## [20,] 2.978974 2.994048

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.899                      0.569                  0.974
##  2                       0.700                      1                      0.668
##  3                       0.649                      1                      0.908
##  4                       0.911                      1                      0.515
##  5                       0.919                      1                      0.720
##  6                       0.727                      1                      0.732
##  7                       0.968                      1                      0.994
##  8                       0.853                      1                      0.903
##  9                       0.899                      1                      0.979
## 10                       0.987                      1                      0.748
## # ℹ 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.0568         1.23            0.630                 -0.192  
##  2        -0.222         -0.693          -0.594                  0.169  
##  3         0.487         -0.180          -0.186                  0.304  
##  4        -0.109         -0.148          -0.151                  0.917  
##  5        -0.147         -0.0909          0.554                  0.0533 
##  6         0.341          0.373          -0.306                 -0.800  
##  7        -0.136          0.489           0.243                  0.434  
##  8        -1.17          -0.490          -0.413                 -0.714  
##  9        -0.0211        -0.160          -0.410                 -0.305  
## 10        -0.541         -0.367          -0.808                  0.00252
## # ℹ 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.286 0.303 0.19 0.252 0.325 ...