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] 575 838 590 179 651 277 319 579 447 411 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  575  894  721   40  874  271  315  120  650   337
##  [2,]  838  915  177  249  774  577  703  488  815   353
##  [3,]  590  574  385  268  369  734  753  972  724   676
##  [4,]  179   56  500  800  326  736  420  389  727    74
##  [5,]  651  930  386  744  125  200  699  393   17   712
##  [6,]  277  527  184  739  789   40  497  625  583   308
##  [7,]  319  864  304  207  955  932  401  205  371   862
##  [8,]  579  426  806  365  752  916  433  568   73   552
##  [9,]  447  364  351  314  860  327  301  300   25   677
## [10,]  411  730  818  886  533  470  323  424   28   670
## [11,]   86  930  115   88  676  131   21  369  640   460
## [12,]  649  301  506  730   58  768  310  650  677   827
## [13,]  738  734  914   14  614  631  291  796  226   745
## [14,]  734  131  745  578  235  859  796  919  718   109
## [15,]  530  287  758  676  744  757  878  969  574   911
## [16,]  848  484  154  891  774  488  660  525  223   469
## [17,]  386  930  200   20  744  764  965  439  651     5
## [18,]  671  889  779  363  353  598   84  865  825   222
## [19,]  335  274  833  890  556   32  633  191  445   128
## [20,]  930  536  393   17  875  460  988  386  780   383
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.49 6.18 3.03 5.34 2.99 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.485015 2.653001 2.684383 2.740851 2.851191 2.865239 2.956880 2.973821
##  [2,] 6.176424 6.214240 6.343355 6.373304 6.382418 6.504011 6.509076 6.572248
##  [3,] 3.029227 3.308825 3.321665 3.368900 3.447640 3.514371 3.606039 3.622772
##  [4,] 5.335489 5.424193 5.718757 5.742143 5.897403 6.194792 6.389182 6.405905
##  [5,] 2.994185 3.127637 3.327055 3.335291 3.350924 3.474288 3.524829 3.548353
##  [6,] 3.289788 3.473572 3.561651 3.718976 3.785921 3.885938 3.934462 3.970791
##  [7,] 2.999142 3.688778 3.740716 3.855780 4.012733 4.108843 4.119357 4.237563
##  [8,] 2.508237 2.740607 3.055956 3.219435 3.576495 3.584136 3.648261 3.708789
##  [9,] 3.810576 4.064084 4.185329 4.300051 4.365769 4.469385 4.484508 4.613099
## [10,] 2.998301 3.002740 3.042024 3.120326 3.189666 3.206119 3.270726 3.285611
## [11,] 2.705975 2.815296 3.060713 3.228404 3.275969 3.371336 3.421595 3.481313
## [12,] 3.104258 3.148919 3.166097 3.212910 3.250618 3.251474 3.253303 3.377122
## [13,] 3.425833 3.873374 3.922112 3.963417 4.032484 4.137645 4.144629 4.174068
## [14,] 2.822860 2.897202 3.199358 3.320592 3.325157 3.354150 3.360490 3.363290
## [15,] 2.139081 2.595782 2.607109 2.702714 2.707360 2.774993 2.812816 2.823113
## [16,] 4.693712 4.821125 4.882209 5.013395 5.046608 5.080887 5.156047 5.171896
## [17,] 2.764662 2.805579 3.036909 3.260322 3.561725 3.564839 3.607840 3.611303
## [18,] 3.132371 3.180812 3.241773 3.324268 3.391398 3.445678 3.486064 3.595115
## [19,] 4.040213 4.751248 4.859596 4.962199 5.154861 5.165437 5.172362 5.202560
## [20,] 2.951628 3.048558 3.177894 3.260322 3.295268 3.319166 3.329592 3.343378
##           [,9]    [,10]
##  [1,] 2.978004 2.993153
##  [2,] 6.593048 6.613165
##  [3,] 3.684643 3.711374
##  [4,] 6.551413 6.618650
##  [5,] 3.636292 3.649137
##  [6,] 3.988338 3.989147
##  [7,] 4.292497 4.308653
##  [8,] 3.734865 3.881041
##  [9,] 4.619474 4.697437
## [10,] 3.315812 3.362373
## [11,] 3.509693 3.528192
## [12,] 3.389490 3.404695
## [13,] 4.208464 4.216751
## [14,] 3.555936 3.577952
## [15,] 2.867829 2.908637
## [16,] 5.268617 5.285077
## [17,] 3.625078 3.636292
## [18,] 3.610472 3.610507
## [19,] 5.286850 5.293647
## [20,] 3.416937 3.443737

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.977                      0.978                  0.910
##  2                       0.977                      0.978                  0.963
##  3                       0.977                      0.886                  1    
##  4                       0.977                      0.941                  0.635
##  5                       0.996                      0.886                  0.984
##  6                       0.977                      0.916                  0.970
##  7                       0.977                      0.910                  0.906
##  8                       0.977                      0.970                  0.968
##  9                       0.977                      0.896                  1    
## 10                       0.977                      1                      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.212         -0.465        -0.428                      0.137 
##  2       -0.124          0.525         0.280                     -0.231 
##  3       -0.478         -0.0126       -0.00905                    0.437 
##  4       -0.650         -0.110        -0.254                     -1.31  
##  5       -0.350         -0.0600       -0.464                      0.437 
##  6       -0.364         -0.151        -0.000809                   0.273 
##  7       -0.0612        -0.199        -0.499                     -0.615 
##  8       -0.0271         0.711         0.544                      0.904 
##  9       -0.203         -0.0141       -0.0407                     0.720 
## 10       -0.00581       -0.00893       0.254                      0.0550
## # ℹ 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.32 0.148 0.263 0.147 0.261 ...