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] 997 601 225 487 75 126 571 866 541 865 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  997  998  448  646  145  876  425  298   98   289
##  [2,]  601  433  510  810  685  218  656  306  541   850
##  [3,]  225  340  337  959  737  243  246  512   98   217
##  [4,]  487  955  939  534  794   32  289  959  579   482
##  [5,]   75  463  571  270  395  576  870  489  367   151
##  [6,]  126  108  350  136  665   67  821  418  670   362
##  [7,]  571   21  649  314  835  687  365  688  720   177
##  [8,]  866  907  375  153  272  702  837  478   82   779
##  [9,]  541   90  850  964  213  738  212  223  736   357
## [10,]  865  956  970  654  555   48  996  897  132   241
## [11,]  289  419   77  959  798  897  225  298  876   181
## [12,]  513  704  613  623  814  714  803   63  515   941
## [13,]  174  678  307  769  818  887  365  213   50   717
## [14,]   60  827  793  784  141  585  574  218  807   682
## [15,]  171  209    3  246  323  480  196  172  728   624
## [16,]  821  670  506  699  391  136  585  509  408   260
## [17,]  693   84  755  980  726  387  152   61  239   126
## [18,]  660   14  321  807  568  336  152  615  242   415
## [19,]  675  783  501  841  323   72   10  276   11   658
## [20,]  911  608  984  649  720  688  301   59  306   373
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.92 4.25 2.63 2.72 4 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.915530 3.485632 3.550795 3.611620 3.627522 3.653345 3.732034 3.739261
##  [2,] 4.250474 4.348658 4.402308 4.468885 4.514770 4.551554 4.680834 4.795196
##  [3,] 2.626894 2.764662 2.917199 3.068922 3.154746 3.206544 3.222205 3.266362
##  [4,] 2.718018 2.887090 3.048336 3.106157 3.110425 3.237219 3.256762 3.330299
##  [5,] 4.002850 4.335591 4.385952 4.394052 4.702753 4.711066 4.717029 4.782837
##  [6,] 4.713210 5.203935 5.282490 5.285954 5.484486 5.560440 5.619334 5.637751
##  [7,] 2.881178 2.948158 2.950295 2.963352 3.002213 3.027890 3.055058 3.096559
##  [8,] 3.955591 4.267588 4.365962 4.390887 4.498784 4.520751 4.552377 4.577760
##  [9,] 3.102051 3.208460 3.238188 3.240576 3.283706 3.340364 3.379686 3.409709
## [10,] 3.315832 3.341492 3.400104 3.450653 3.467755 3.501768 3.639763 3.673895
## [11,] 3.067721 3.272169 3.357823 3.365739 3.470978 3.480921 3.497629 3.506892
## [12,] 3.525737 3.624438 3.780374 4.227533 4.247038 4.282566 4.348662 4.369423
## [13,] 2.595939 2.748842 2.869548 2.871399 2.879931 2.889363 3.022016 3.103919
## [14,] 3.020810 3.036677 3.253749 3.273271 3.324695 3.328922 3.335381 3.391398
## [15,] 3.791387 4.126217 4.316565 4.368160 4.372758 4.408446 4.471064 4.653829
## [16,] 2.758565 2.998026 3.229995 3.245562 3.292420 3.302855 3.396851 3.453275
## [17,] 4.395264 4.680639 4.925839 4.953778 5.048789 5.065909 5.138321 5.197353
## [18,] 3.691295 4.034049 4.117505 4.134153 4.192404 4.197541 4.237911 4.244176
## [19,] 4.141066 4.158359 4.244920 4.246897 4.338693 4.347362 4.359265 4.389687
## [20,] 3.517541 3.721614 3.810580 3.817298 3.860959 3.876880 3.900910 3.921677
##           [,9]    [,10]
##  [1,] 3.774769 3.819886
##  [2,] 4.905384 4.988442
##  [3,] 3.275223 3.291774
##  [4,] 3.416803 3.421620
##  [5,] 4.818844 4.824271
##  [6,] 5.644808 5.687220
##  [7,] 3.177411 3.201598
##  [8,] 4.611566 4.723704
##  [9,] 3.429700 3.443903
## [10,] 3.677091 3.752576
## [11,] 3.572926 3.589359
## [12,] 4.454048 4.494401
## [13,] 3.157461 3.193950
## [14,] 3.447363 3.481293
## [15,] 4.668483 4.691692
## [16,] 3.453405 3.527462
## [17,] 5.238911 5.278256
## [18,] 4.259900 4.277835
## [19,] 4.392127 4.393451
## [20,] 3.942885 4.021386

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…¹ pCREB…² pBTK(…³ pS6(Y…⁴ cPARP…⁵ pPLCg…⁶ pSrc(…⁷ Ki67(…⁸ pErk1…⁹
##            <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1         0.716       1   0.839   1           1   0.986   0.327   1       0.947
##  2         0.718       1   0.946   1           1   0.912   0.934   0.745   0.812
##  3         1           1   0.867   0.999       1   0.872   0.307   1       0.920
##  4         0.846       1   0.979   1           1   0.839   0.213   1       0.920
##  5         0.544       1   0.813   1           1   0.761   0.606   1       0.949
##  6         1           1   0.914   1           1   0.572   0.720   1       1    
##  7         0.939       1   0.821   1           1   0.724   1       1       1    
##  8         1           1   0.813   0.970       1   0.985   0.606   1       0.892
##  9         1           1   0.821   1           1   0.602   0.955   1       0.974
## 10         0.582       1   0.961   1           1   0.814   0.627   1       0.812
## # … with 990 more rows, 25 more variables: `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>, …

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)…¹ CD3(C…² CD3(C…³ CD235…⁴ CD3(C…⁵ CD45(…⁶ CD19(…⁷ CD22(…⁸ IgD(Nd…⁹
##           <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>    <dbl>
##  1      -0.213   0.605   1.08    0.295    0.937  0.690   1.22    0.834   0.00592
##  2      -0.267  -0.158   0.863  -0.698    0.601  1.77    0.255  -0.139   0.198  
##  3      -0.239   1.46    0.441  -0.146    0.479 -0.0408  0.663   0.222   0.458  
##  4       0.412  -0.297  -0.0404 -0.957   -0.627 -0.626  -0.0828  0.350   0.993  
##  5       0.180  -0.104   1.48    0.121    1.25  -1.59    1.49   -0.0308  0.408  
##  6      -0.685  -0.326  -0.428  -0.0866   0.402 -0.357   1.15    1.16    1.01   
##  7      -0.0664 -0.184  -0.0623 -0.262   -0.124  1.37    0.165   0.137  -0.246  
##  8      -0.0268 -0.0651  0.829  -1.76    -0.493  1.35   -0.0624 -0.184  -0.00980
##  9       0.697   0.461   1.19    0.0728   0.291  2.07   -0.259   0.0135 -0.0756 
## 10       0.118  -0.399   0.551  -1.56     0.472 -1.54   -0.0988 -0.265   1.18   
## # … with 20 more rows, 42 more variables: `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>, …
# 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.255 0.202 0.296 0.28 0.198 ...