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] 516 323 518 711 534 61 962 16 442 177 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  516  880  758  889  161  591  701  481  949   381
##  [2,]  323  640  407  268  621  288  145   90  777   844
##  [3,]  518  290  466  425  474  793  257   51  924    27
##  [4,]  711  569  193  595  968  556  140  605  119   154
##  [5,]  534  553  259  784  726  844  557  268  832   662
##  [6,]   61  452  650  727  443   18  885  734  483   368
##  [7,]  962  404  296   95  620  298  226   39  896   608
##  [8,]   16  484  117  292  537  391  418  816  980   408
##  [9,]  442  643  271  902  609  165  994  387  709   435
## [10,]  177   54  325  229  890  940  975  760  993  1000
## [11,]  303  744  270  929  351  415  473   50  915   957
## [12,]  367  556   62  251  768  167  727  619  675   119
## [13,]  747  951  579  950  804  132  590   58   41   167
## [14,]   18  119  878   23  545  813  101  670  364   551
## [15,]  927  957   77  818  160  410   73  758  481   624
## [16,]    8  839  638  583  156  816  484  581  958   194
## [17,]  278  647  131  981  115  732  790  781  481   833
## [18,]  119  313   14   23  476  772  755  236  443   551
## [19,]  937  369  872  527  989  280   30  864  886    74
## [20,]  790  131  270  734  957  115  613  481  473   647
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.52 2.51 5.13 2.83 2.6 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.523987 3.710101 3.761739 3.764044 3.793746 3.817603 3.842643 3.847133
##  [2,] 2.505417 2.514919 2.576869 2.636345 2.848480 2.905679 2.994278 3.024012
##  [3,] 5.131131 5.636779 5.690738 5.703112 5.890783 5.891578 5.944053 5.965468
##  [4,] 2.832318 2.891351 2.943991 3.055931 3.098479 3.119748 3.132981 3.142482
##  [5,] 2.601577 2.849126 2.877447 2.916837 2.928482 2.929289 2.973848 3.004992
##  [6,] 3.715483 3.917719 4.248476 4.265772 4.463071 4.505853 4.526154 4.555257
##  [7,] 3.137002 3.149332 3.176083 3.228795 3.332833 3.340149 3.392172 3.419672
##  [8,] 2.595939 2.603422 3.021559 3.124713 3.154145 3.265306 3.324790 3.351291
##  [9,] 3.260322 3.348557 3.545791 3.561725 3.564839 3.611303 3.625078 3.636292
## [10,] 3.707049 3.725396 3.817305 4.366362 4.384092 4.451116 4.554311 4.662361
## [11,] 4.474719 4.529364 4.691997 4.809174 4.809390 4.810910 4.881991 4.972762
## [12,] 5.074714 5.624392 5.637483 5.703896 5.718982 5.738483 5.744498 5.752139
## [13,] 5.136586 5.163205 5.172362 5.228120 5.305812 5.371879 5.387086 5.406438
## [14,] 2.742485 2.766774 2.910265 2.969226 3.116936 3.123300 3.141183 3.168725
## [15,] 3.936057 4.540333 4.803985 4.938966 4.994535 5.020366 5.021473 5.082858
## [16,] 2.595939 2.881293 2.972628 2.973328 3.022016 3.068729 3.157461 3.193950
## [17,] 4.074958 4.114655 4.159029 4.167930 4.289968 4.609212 4.633243 4.661457
## [18,] 2.277209 2.670281 2.742485 2.924695 3.015123 3.046227 3.057473 3.186649
## [19,] 2.766635 3.316723 3.439839 3.459512 3.520120 3.677320 3.851038 3.854498
## [20,] 3.913386 4.031615 4.142594 4.312620 4.377665 4.444762 4.457054 4.573960
##           [,9]    [,10]
##  [1,] 3.944542 4.022318
##  [2,] 3.027096 3.042425
##  [3,] 5.968511 5.981509
##  [4,] 3.161492 3.207026
##  [5,] 3.019546 3.027916
##  [6,] 4.592141 4.650517
##  [7,] 3.572659 3.590294
##  [8,] 3.365187 3.387537
##  [9,] 3.694811 3.706666
## [10,] 4.673308 4.729963
## [11,] 5.004279 5.036290
## [12,] 5.815358 5.817851
## [13,] 5.474942 5.482269
## [14,] 3.195781 3.200762
## [15,] 5.175700 5.176622
## [16,] 3.205609 3.292603
## [17,] 4.674532 4.792648
## [18,] 3.205201 3.223317
## [19,] 3.978886 4.061484
## [20,] 4.605655 4.625045

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 x 34
##    `pCrkL(Lu175)Di… `pCREB(Yb176)Di… `pBTK(Yb171)Di.… `pS6(Yb172)Di.I…
##               <dbl>            <dbl>            <dbl>            <dbl>
##  1            0.994            0.954            0.942            1    
##  2            0.974            0.623            0.861            0.989
##  3            1                0.980            0.981            0.997
##  4            0.987            0.980            0.661            1    
##  5            0.974            0.615            0.379            1    
##  6            1                0.984            0.968            0.997
##  7            0.994            0.701            0.481            0.989
##  8            0.987            0.984            0.602            1    
##  9            1                1                0.795            0.957
## 10            0.974            0.971            0.994            1    
## # … 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>,
## #   `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>,
## #   `pSrc(Nd144)Di.IL7.change` <dbl>, `Ki67(Sm152)Di.IL7.change` <dbl>,
## #   `pErk12(Gd155)Di.IL7.change` <dbl>, `pSTAT3(Gd158)Di.IL7.change` <dbl>,
## #   `pAKT(Tb159)Di.IL7.change` <dbl>, `pBLNK(Gd160)Di.IL7.change` <dbl>,
## #   `pP38(Tm169)Di.IL7.change` <dbl>, `pSTAT5(Nd150)Di.IL7.change` <dbl>,
## #   `pSyk(Dy162)Di.IL7.change` <dbl>, `tIkBa(Er166)Di.IL7.change` <dbl>,
## #   IL7.fraction.cond.2 <dbl>, density <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 x 51
##    `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(… `CD3(Cd114)Di`
##             <dbl>          <dbl>          <dbl>            <dbl>          <dbl>
##  1        -0.0606        -0.206         -0.235           0.331          -0.103 
##  2        -0.128         -0.120         -0.357          -1.51           -0.483 
##  3         0.398         -0.483         -0.148           0.0198         -0.0588
##  4        -0.452         -0.117         -0.127           0.540          -0.388 
##  5         0.319         -0.0282        -0.0360          0.349          -0.472 
##  6        -0.169         -0.254          0.111          -0.379          -0.168 
##  7        -0.200         -0.436         -0.365           0.276          -0.153 
##  8        -0.204         -0.113         -0.156          -0.0615         -0.486 
##  9        -0.0811         1.33          -0.0166          0.00188        -0.180 
## 10        -0.478         -0.264         -0.853          -0.812          -0.396 
## # … 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>,
## #   `CD40(Er170)Di` <dbl>, `CD33(Yb173)Di` <dbl>, `HLA-DR(Yb174)Di` <dbl>,
## #   Time <dbl>, Cell_length <dbl>, `cPARP(La139)Di` <dbl>,
## #   `pPLCg2(Pr141)Di` <dbl>, `pSrc(Nd144)Di` <dbl>, `pSTAT5(Nd150)Di` <dbl>,
## #   `Ki67(Sm152)Di` <dbl>, `pErk12(Gd155)Di` <dbl>, `pSTAT3(Gd158)Di` <dbl>,
## #   `pAKT(Tb159)Di` <dbl>, `pBLNK(Gd160)Di` <dbl>, `pSyk(Dy162)Di` <dbl>,
## #   `tIkBa(Er166)Di` <dbl>, `pP38(Tm169)Di` <dbl>, `pBTK(Yb171)Di` <dbl>,
## #   `pS6(Yb172)Di` <dbl>, `pCrkL(Lu175)Di` <dbl>, `pCREB(Yb176)Di` <dbl>,
## #   `DNA1(Ir191)Di` <dbl>, `DNA2(Ir193)Di` <dbl>, `Viability1(Pt195)Di` <dbl>,
## #   `Viability2(Pt196)Di` <dbl>, wanderlust <dbl>, condition <chr>
# 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.243 0.317 0.163 0.303 0.316 ...