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] 457 624 351 903 625 471 130 44 259 665 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  457  682  481   67  484  867   42  674  713   342
##  [2,]  624  960  484   94  457  336  713  113  405   682
##  [3,]  351  604  695  397  804  565  391  978  888   303
##  [4,]  903  755  841  791  783  432  894   30  790   915
##  [5,]  625  603  492  193  853  667  442  398  650   801
##  [6,]  471  835  243  107  688  858  812  257  393   577
##  [7,]  130  785  215  308  709  360  419  606   65    26
##  [8,]   44  920  772  312  982  921  175  146  127   593
##  [9,]  259  667  650  440   29  124  915  774  511   407
## [10,]  665  589  362  970  791  944  457  103  753   913
## [11,]  242  937  278  211  154  641  188  525  509   493
## [12,]  151  477  587  972  876  182  898  674   92   545
## [13,]  935  160  858  358  393  114  320  749  644   688
## [14,]  713  475  867    1  105  682  843  262  955     2
## [15,]  113  105  262  421  517  773  240   92  336   636
## [16,]  804  820  147  528  979  862  397  364   88   548
## [17,]  797   29  231  650  736  541  230  670  567   212
## [18,]  710  106  846  674  914  273   54  256   72   462
## [19,]  962  627  398  581  797  136  801  331  709    20
## [20,]  711  331  603  129  625  442  348  608   41   371
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.06 2.52 2.54 3.6 2.07 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.061246 3.180421 3.262575 3.331167 3.485061 3.543151 3.550177 3.553320
##  [2,] 2.522069 2.996389 3.121466 3.127203 3.312508 3.444082 3.452956 3.453405
##  [3,] 2.539880 2.750091 3.005326 3.125896 3.188564 3.204252 3.215832 3.257949
##  [4,] 3.597990 3.612581 3.631417 3.686504 3.704716 3.768494 3.823646 3.862133
##  [5,] 2.071327 2.180085 2.575365 2.589770 2.620281 2.648569 2.760765 2.803407
##  [6,] 3.488930 3.742919 3.789020 3.879786 4.013427 4.107679 4.263874 4.288083
##  [7,] 2.803685 3.180609 3.602938 3.641495 3.749763 3.763579 3.942419 4.142052
##  [8,] 4.085178 4.401853 4.488538 4.521688 4.586298 4.595482 4.619212 4.783136
##  [9,] 3.135509 3.206864 3.514399 3.549906 3.589964 3.697963 3.775141 3.835667
## [10,] 2.765675 2.832318 2.943991 3.013196 3.098479 3.119748 3.161492 3.207026
## [11,] 3.471605 3.688802 3.735966 4.057822 4.120862 4.193240 4.277440 4.320054
## [12,] 3.132644 3.277176 3.574940 3.639797 3.831307 3.868862 3.922722 4.004922
## [13,] 3.012112 3.304815 3.418581 3.447265 3.597304 3.604383 3.716201 3.727787
## [14,] 3.658589 3.687519 3.713328 3.849979 3.902831 3.923946 3.962801 3.965478
## [15,] 3.251393 3.344558 3.444666 3.638739 3.727765 3.728085 3.760028 3.910739
## [16,] 2.893655 3.021908 3.030025 3.263179 3.310881 3.320233 3.403182 3.628872
## [17,] 2.308497 2.482125 2.739427 2.754457 2.762795 2.786634 2.860018 2.936392
## [18,] 3.172684 3.292320 3.351309 3.528282 3.554566 3.565380 3.604993 3.707276
## [19,] 2.608074 2.793839 2.806661 2.895936 2.987004 2.991700 2.993643 2.998185
## [20,] 2.158073 2.342175 2.418549 2.609882 2.654143 2.662743 2.674326 2.692768
##           [,9]    [,10]
##  [1,] 3.564391 3.569898
##  [2,] 3.459874 3.510417
##  [3,] 3.280882 3.330960
##  [4,] 3.904556 3.909994
##  [5,] 2.876484 2.943282
##  [6,] 4.327964 4.358622
##  [7,] 4.153447 4.204074
##  [8,] 4.901480 4.905373
##  [9,] 3.838373 3.858565
## [10,] 3.215355 3.232540
## [11,] 4.445431 4.521886
## [12,] 4.074269 4.098974
## [13,] 3.809217 3.816823
## [14,] 4.062498 4.096594
## [15,] 3.917435 4.115166
## [16,] 3.641564 3.646421
## [17,] 2.941144 2.977965
## [18,] 3.719047 3.731281
## [19,] 3.122293 3.143366
## [20,] 2.753930 2.754043

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.qval…` `pCREB(Yb176)D…` `pBTK(Yb171)Di…` `pS6(Yb172)Di.…`
##                         <dbl>            <dbl>            <dbl>            <dbl>
##  1                      0.603                1            1                0.969
##  2                      0.895                1            0.995            0.748
##  3                      0.940                1            0.995            0.999
##  4                      0.961                1            0.995            0.894
##  5                      0.996                1            1                0.906
##  6                      0.948                1            0.995            0.570
##  7                      0.969                1            0.995            0.894
##  8                      0.954                1            1                0.894
##  9                      0.996                1            0.995            0.821
## 10                      0.961                1            0.995            0.681
## # … 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>, …

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(…` `CD3(Cd114)Di`
##             <dbl>          <dbl>          <dbl>             <dbl>          <dbl>
##  1        0.416          -0.0134          2.30            -0.206          2.39  
##  2        0.618          -0.191           0.235           -1.03           1.93  
##  3        0.381           0.864          -0.346            0.0486         0.385 
##  4       -0.660          -0.364          -0.141            0.450         -0.0846
##  5       -0.120          -0.218           1.30             0.962          0.390 
##  6        0.900           0.483          -0.110           -0.415          1.02  
##  7       -0.00743        -0.0606         -0.335            0.0742         0.354 
##  8       -0.0784          1.08            0.860           -0.631          0.449 
##  9       -0.0337         -0.117          -0.583            0.0137        -0.0686
## 10        0.689           1.67            1.65             0.479          0.624 
## # … 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>, …
# 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.269 0.282 0.287 0.254 0.336 ...