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] 447 214 724 958 931 829 70 103 863 780 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 447 608 808 22 935 130 977 761 411 855
## [2,] 214 647 365 405 640 393 218 91 274 744
## [3,] 724 815 665 131 77 711 51 375 830 245
## [4,] 958 846 566 218 17 61 745 759 317 217
## [5,] 931 273 460 610 445 151 389 807 275 106
## [6,] 829 30 705 210 545 933 653 940 125 427
## [7,] 70 785 637 84 954 732 687 993 890 456
## [8,] 103 139 912 795 841 147 493 639 467 269
## [9,] 863 339 790 936 657 149 559 519 124 773
## [10,] 780 550 566 487 993 365 175 627 954 864
## [11,] 765 530 373 105 680 876 505 851 341 870
## [12,] 841 135 584 373 788 569 158 505 867 676
## [13,] 431 478 975 696 315 110 327 70 55 890
## [14,] 310 977 783 579 911 751 808 713 632 332
## [15,] 397 265 805 156 648 676 870 158 584 622
## [16,] 202 492 534 631 641 305 540 957 832 41
## [17,] 72 687 57 70 993 217 4 394 938 365
## [18,] 108 447 380 639 439 872 269 360 564 324
## [19,] 337 365 478 677 993 917 426 585 550 780
## [20,] 332 58 130 510 326 195 22 514 919 375
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.18 3.94 3.74 3.54 3.49 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.178829 3.518837 3.644989 3.697669 3.710303 3.794629 3.819647 3.822057
## [2,] 3.940889 3.967855 4.010675 4.211274 4.212640 4.276874 4.282479 4.338199
## [3,] 3.736119 3.911825 3.951824 3.964374 4.038856 4.435834 4.471956 4.495380
## [4,] 3.543540 3.753477 3.851708 3.916699 3.945518 3.953413 4.140932 4.218734
## [5,] 3.493480 3.683455 3.789282 3.848228 3.921325 3.962869 3.979688 4.129273
## [6,] 3.071717 3.756475 3.909412 3.981704 3.990046 4.063349 4.092452 4.260064
## [7,] 2.213633 2.502818 2.693816 2.696411 2.834718 2.841634 2.859277 2.904089
## [8,] 3.428157 4.646404 4.721329 4.746571 4.897551 4.919758 4.936809 5.021461
## [9,] 3.801019 3.816136 3.912120 3.925946 3.930539 3.955137 4.020050 4.147782
## [10,] 3.130284 3.171814 3.271862 3.310888 3.492266 3.495468 3.523420 3.555144
## [11,] 3.672674 4.317249 4.474719 4.482967 4.529364 4.718148 4.753953 4.809174
## [12,] 3.426233 3.460088 3.566005 3.576664 3.747974 3.754792 3.778793 3.809381
## [13,] 3.238647 4.090073 4.108332 4.205574 4.272677 4.302237 4.333769 4.367250
## [14,] 4.040213 4.555489 4.859596 4.962199 5.126782 5.136586 5.172362 5.228120
## [15,] 3.618361 3.836290 3.844795 3.888269 3.976899 4.347911 4.362560 4.442633
## [16,] 2.636345 2.677292 2.759145 2.976826 3.055297 3.061104 3.067768 3.092942
## [17,] 3.284541 3.383747 3.555936 3.718018 3.830086 3.846594 3.945518 3.951162
## [18,] 5.719366 5.841000 5.948293 5.976489 6.146462 6.209535 6.336659 6.362762
## [19,] 3.432329 3.475996 3.645820 3.692707 3.709470 3.743823 3.774978 3.789584
## [20,] 3.061600 3.284390 3.467129 3.947331 3.987906 4.047196 4.183539 4.318731
## [,9] [,10]
## [1,] 4.034297 4.118974
## [2,] 4.368811 4.399277
## [3,] 4.604388 4.749710
## [4,] 4.266103 4.349297
## [5,] 4.149359 4.196376
## [6,] 4.288347 4.418799
## [7,] 3.096632 3.106401
## [8,] 5.102729 5.104464
## [9,] 4.166320 4.195253
## [10,] 3.573442 3.585295
## [11,] 4.872632 4.913037
## [12,] 3.829707 3.870680
## [13,] 4.369689 4.395364
## [14,] 5.305812 5.371879
## [15,] 4.509977 4.510202
## [16,] 3.100806 3.105991
## [17,] 3.964335 3.989374
## [18,] 6.381264 6.423392
## [19,] 3.803116 3.820932
## [20,] 4.339341 4.430295
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 1 1 0.995
## 2 1 1 0.969
## 3 0.948 1 0.982
## 4 1 1 0.995
## 5 1 1 0.906
## 6 0.948 1 0.982
## 7 1 1 0.967
## 8 0.948 1 0.982
## 9 1 1 0.995
## 10 1.00 1 0.940
## # ℹ 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.444 -0.212 -0.508 -0.572
## 2 -0.576 -0.712 -0.473 0.658
## 3 -0.0700 -0.183 -0.375 -0.716
## 4 -0.100 -0.277 -0.327 -0.837
## 5 -0.0228 -0.188 -0.207 0.441
## 6 -0.171 0.0286 -0.508 -0.823
## 7 -0.218 -0.0910 -0.198 0.145
## 8 -0.0349 -0.518 -0.490 0.615
## 9 -0.180 -0.101 -0.123 -1.61
## 10 0.124 -0.486 -0.944 -1.23
## # ℹ 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.239 0.226 0.205 0.23 0.239 ...