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] 577 252 998 353 468 179 83 836 233 553 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 577 24 868 627 377 490 640 452 308 649
## [2,] 252 296 521 902 921 18 892 468 247 686
## [3,] 998 511 498 737 872 649 184 154 500 634
## [4,] 353 986 706 926 533 834 976 125 146 106
## [5,] 468 871 700 839 31 296 165 841 892 247
## [6,] 179 288 521 694 252 391 726 469 82 942
## [7,] 83 553 185 201 806 552 358 222 498 871
## [8,] 836 715 548 289 910 530 418 213 121 42
## [9,] 233 43 242 112 206 144 86 650 22 84
## [10,] 553 13 201 284 892 921 185 62 405 998
## [11,] 552 201 258 222 521 358 185 553 264 102
## [12,] 456 641 756 265 545 381 947 917 413 939
## [13,] 871 839 700 247 553 284 617 511 535 892
## [14,] 609 324 163 847 533 66 421 136 793 125
## [15,] 927 430 773 728 285 647 758 542 235 30
## [16,] 155 812 235 520 993 864 571 913 254 503
## [17,] 555 988 519 822 53 350 215 885 912 709
## [18,] 713 328 468 840 921 855 418 165 733 166
## [19,] 441 90 287 658 596 646 595 667 539 585
## [20,] 504 600 261 749 938 315 665 670 678 587
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.76 3.02 2.46 4.03 2.98 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.762653 3.199871 3.203300 3.223112 3.268338 3.380915 3.409136 3.561651
## [2,] 3.018629 3.079598 3.182182 3.190289 3.297410 3.328056 3.335644 3.394667
## [3,] 2.461213 2.828404 2.856397 3.099537 3.289984 3.409879 3.417989 3.470655
## [4,] 4.032526 4.315727 4.420810 4.574095 4.632136 4.862952 4.906549 5.006532
## [5,] 2.979586 2.984123 3.005838 3.036749 3.109655 3.112481 3.112711 3.164958
## [6,] 3.149844 3.259314 3.317002 3.352579 3.538963 3.548727 3.555555 3.678307
## [7,] 3.442563 3.541359 3.553116 3.575274 3.615118 3.743540 3.766065 3.775543
## [8,] 3.042903 3.112783 3.156877 3.165143 3.169237 3.254884 3.265306 3.267101
## [9,] 5.603717 5.764127 5.764146 5.897896 5.918278 5.986271 6.001588 6.045350
## [10,] 3.408268 3.574754 3.583722 3.612292 3.669487 3.703260 3.741044 3.748880
## [11,] 3.247260 3.412549 3.657511 3.823118 3.831817 3.864908 3.867757 3.956265
## [12,] 3.442119 3.605673 3.801019 3.816136 3.820451 3.883057 3.912120 3.930539
## [13,] 2.564196 2.700455 2.844470 2.989359 3.001413 3.063995 3.070859 3.106239
## [14,] 5.354341 5.949307 5.958460 6.051648 6.087200 6.191659 6.206025 6.371971
## [15,] 3.414208 3.505909 3.534125 3.625601 3.646689 3.660121 3.688418 3.743238
## [16,] 2.183445 2.467871 2.564574 2.624695 2.698236 2.840044 2.865699 2.878891
## [17,] 3.597885 3.655385 3.684409 3.771032 3.852221 3.952906 3.977396 4.085777
## [18,] 2.310230 2.809737 2.873922 2.969204 3.072755 3.110989 3.114917 3.123732
## [19,] 3.500854 3.768423 3.873558 4.130891 4.465721 4.636100 4.763463 4.789015
## [20,] 2.672903 3.355523 3.485302 3.510808 3.512063 3.550185 3.578359 3.578420
## [,9] [,10]
## [1,] 3.563866 3.604024
## [2,] 3.412268 3.451099
## [3,] 3.478296 3.547772
## [4,] 5.098635 5.243080
## [5,] 3.259964 3.261074
## [6,] 3.713421 3.800139
## [7,] 3.781334 3.797641
## [8,] 3.271496 3.273412
## [9,] 6.161786 6.182026
## [10,] 3.763887 3.793307
## [11,] 3.958814 3.996589
## [12,] 4.065284 4.101748
## [13,] 3.126310 3.146414
## [14,] 6.372856 6.448907
## [15,] 3.841774 3.842138
## [16,] 2.903163 3.011425
## [17,] 4.141474 4.149979
## [18,] 3.126840 3.139106
## [19,] 4.954560 5.057949
## [20,] 3.609483 3.630727
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.935 0.999 1.000
## 2 0.730 0.979 0.896
## 3 0.516 0.979 0.854
## 4 0.817 0.979 0.896
## 5 0.287 0.979 0.974
## 6 0.680 0.979 0.991
## 7 0.232 0.996 0.875
## 8 0.988 0.979 0.412
## 9 0.425 0.979 0.953
## 10 0.536 0.979 0.968
## # ℹ 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>, …
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.319 -0.0282 -0.0360 0.349
## 2 -0.473 -0.140 -0.487 -0.122
## 3 0.632 0.338 -0.451 -0.378
## 4 -0.0923 0.710 -0.0527 0.627
## 5 -0.643 -0.195 -0.297 0.284
## 6 0.361 -0.0554 -0.339 0.340
## 7 -0.174 -0.245 -0.200 -0.434
## 8 -0.797 -0.398 -1.04 -1.20
## 9 -0.0614 0.446 0.616 -0.254
## 10 -0.163 -0.145 -0.441 -0.0194
## # ℹ 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.262 0.282 0.28 0.192 0.302 ...