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] 662 710 76 995 705 690 283 712 32 322 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  662  391  444  387   60  931  690  614  643   311
##  [2,]  710  912  673  138  486  446  267  875  503   992
##  [3,]   76  909   74  418  552  836  354  676  215   889
##  [4,]  995  501  500  939  596   76   78  418  215    40
##  [5,]  705  192  182  247  183  961  704  819  773   360
##  [6,]  690  934  769  978  722  902  694  703   60   233
##  [7,]  283  688  704  176  433  183  830  321  217   599
##  [8,]  712  823  382   21  446  882  673  472  267   710
##  [9,]   32  441   95   86  932  795  829  579   90   752
## [10,]  322  333  970  661   51  966  186  585  738    11
## [11,]  740  626  650  518  333  585  714  210  961   841
## [12,]  898  196  688  360  839  705  582  439  764   261
## [13,]  256  491  605  793  152  724  206  712  316   267
## [14,]  792  791  287  414  388  722  694  488  253   402
## [15,]  137  585  974  234  186  518  933  740  738   770
## [16,]  791  691  414  681  282  460  154  448  900   631
## [17,]   54  525  366  421  247  770  217  598  234   186
## [18,]  242  165  986  930  193  528  827  913  520   152
## [19,]  127  170  549  274    1  726  587  289  534   488
## [20,]  907  546  511  500  734  995  579  863  684   441
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.47 3.69 4.33 4.08 2.87 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.474032 2.789880 2.956495 2.960426 3.008786 3.132630 3.304342 3.348713
##  [2,] 3.686659 3.807383 3.898689 3.966064 3.998661 4.073505 4.077283 4.176180
##  [3,] 4.325594 4.822521 4.943263 4.989323 5.008097 5.022956 5.034340 5.091783
##  [4,] 4.079191 4.104734 4.304730 4.468533 4.847785 4.895267 4.916688 4.932999
##  [5,] 2.873100 2.917214 2.930088 2.979536 3.183717 3.235999 3.237961 3.291589
##  [6,] 2.798396 3.137523 3.238542 3.266212 3.280094 3.323589 3.367035 3.410949
##  [7,] 2.516752 2.739680 3.061777 3.070146 3.093891 3.099083 3.115024 3.122734
##  [8,] 3.026220 3.165599 3.368979 3.435780 3.445453 3.531836 3.574098 3.633856
##  [9,] 4.484265 5.038568 5.295480 5.352405 5.363247 5.455077 5.476144 5.479535
## [10,] 3.244902 3.370914 3.598682 3.620696 3.654803 3.748353 3.801327 3.851958
## [11,] 2.608074 2.793839 2.895936 2.987004 2.991700 3.122293 3.130170 3.167068
## [12,] 3.601931 3.750548 3.915970 3.955643 3.967931 3.976458 3.988736 3.996245
## [13,] 3.865158 4.159029 4.304882 4.475063 4.505099 4.528031 4.559770 4.588397
## [14,] 3.654436 3.799726 3.876344 3.980986 4.044009 4.127788 4.154500 4.163842
## [15,] 3.267832 3.271425 3.400857 3.555033 3.636335 3.655267 3.685537 3.748522
## [16,] 2.660734 2.972224 3.222712 3.262778 3.298726 3.299002 3.300886 3.330355
## [17,] 2.743782 3.002740 3.269234 3.270726 3.315812 3.330879 3.334825 3.362373
## [18,] 2.614302 2.670281 3.115997 3.133923 3.172732 3.313952 3.369040 3.398042
## [19,] 3.473602 4.064624 4.239851 4.487576 4.645882 4.651978 4.679581 4.835348
## [20,] 3.952653 4.491266 4.665894 4.685829 4.791210 4.898619 4.910816 4.912577
##           [,9]    [,10]
##  [1,] 3.361317 3.434218
##  [2,] 4.249133 4.277344
##  [3,] 5.138258 5.223982
##  [4,] 4.938203 4.990717
##  [5,] 3.298971 3.311429
##  [6,] 3.469062 3.475971
##  [7,] 3.164888 3.221862
##  [8,] 3.646836 3.659967
##  [9,] 5.486029 5.579526
## [10,] 3.914378 3.923677
## [11,] 3.174836 3.190156
## [12,] 4.012887 4.175864
## [13,] 4.739035 4.801101
## [14,] 4.214410 4.244086
## [15,] 3.750444 3.796365
## [16,] 3.387118 3.410273
## [17,] 3.367654 3.371004
## [18,] 3.399457 3.432905
## [19,] 4.859462 4.880660
## [20,] 4.976591 5.074714

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.892                      0.662                  0.878
##  2                       0.995                      0.941                  0.878
##  3                       0.892                      0.902                  0.941
##  4                       0.810                      0.883                  0.941
##  5                       0.627                      0.976                  0.759
##  6                       0.628                      0.870                  0.885
##  7                       0.855                      0.912                  0.773
##  8                       0.988                      0.579                  0.998
##  9                       0.932                      0.769                  0.938
## 10                       0.876                      1                      0.934
## # ℹ 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.152          0.384          0.702                     0.529
##  2         0.396         -0.107         -0.0873                   -0.144
##  3        -0.929         -0.570         -0.345                    -0.446
##  4        -0.306         -0.664         -0.507                    -1.23 
##  5        -0.251         -0.109         -0.0732                   -0.277
##  6        -0.115         -0.219          0.367                    -0.444
##  7        -0.420         -0.408         -0.514                    -0.637
##  8        -0.151         -0.0659        -0.236                     0.309
##  9        -0.263         -0.0122        -0.107                     0.281
## 10        -0.0951         0.290         -0.0805                    0.143
## # ℹ 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.289 0.232 0.183 0.199 0.3 ...