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] 432 318 719 352 899 821 598 539 956 593 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 432 546 572 228 114 266 714 280 420 959
## [2,] 318 3 593 779 719 10 357 392 606 738
## [3,] 719 779 928 318 271 757 844 561 593 529
## [4,] 352 525 982 818 234 350 402 449 250 746
## [5,] 899 857 675 832 340 264 981 267 23 662
## [6,] 821 881 476 964 500 716 556 334 818 796
## [7,] 598 857 195 661 790 926 249 268 763 581
## [8,] 539 887 94 626 726 291 540 802 780 166
## [9,] 956 722 563 35 504 112 440 674 619 901
## [10,] 593 368 792 950 570 879 975 529 530 844
## [11,] 736 67 185 770 196 932 453 40 348 128
## [12,] 464 680 555 946 388 965 798 639 342 743
## [13,] 79 562 317 98 543 210 527 119 783 101
## [14,] 270 672 678 253 122 386 753 879 529 284
## [15,] 977 70 76 321 356 846 944 422 781 329
## [16,] 876 742 652 775 219 20 873 186 583 203
## [17,] 728 626 978 614 333 343 599 780 173 539
## [18,] 569 341 725 812 815 412 326 858 278 25
## [19,] 84 728 662 599 239 474 961 802 546 527
## [20,] 647 696 737 827 809 775 943 989 583 848
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 4.57 3.47 3.08 3.18 4.07 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 4.565050 4.925839 4.953778 5.065909 5.080504 5.138321 5.190719 5.219531
## [2,] 3.471605 4.057822 4.163044 4.172630 4.193240 4.205990 4.281796 4.316173
## [3,] 3.079625 3.176084 3.232337 3.622983 3.626039 3.731153 3.825092 3.868395
## [4,] 3.177894 3.288704 3.308502 3.411552 3.431811 3.548353 3.645218 3.699110
## [5,] 4.071853 4.076856 4.116485 4.159575 4.172872 4.215889 4.225500 4.272803
## [6,] 2.750091 2.858838 2.969394 3.005326 3.125896 3.199752 3.204252 3.215832
## [7,] 4.304656 4.575817 4.710038 4.712575 4.766294 4.766520 4.893295 5.010810
## [8,] 3.824408 3.974328 4.103848 4.151942 4.277612 4.289968 4.291323 4.403202
## [9,] 3.208698 3.315047 3.319047 3.332938 3.349659 3.381871 3.413148 3.450119
## [10,] 3.337421 3.637605 3.801203 3.825997 3.837001 3.941702 3.997109 4.009321
## [11,] 5.174369 5.636779 5.703112 5.882752 5.890783 5.921251 5.965468 5.981509
## [12,] 4.582641 4.682708 4.821225 4.918050 4.938197 4.958174 4.961522 5.272487
## [13,] 3.064435 3.125480 3.140150 3.181523 3.189593 3.461623 3.488078 3.507925
## [14,] 3.819791 3.901314 4.050608 4.161308 4.360281 4.493344 4.569239 4.594287
## [15,] 3.631617 3.739465 3.749448 3.864758 3.891089 3.947432 3.984764 4.018140
## [16,] 3.601931 3.619611 3.680338 3.734699 3.750548 3.794082 3.915970 3.955643
## [17,] 2.672903 2.784804 3.114912 3.409051 3.485302 3.550185 3.578359 3.578420
## [18,] 3.607179 3.955137 4.073838 4.225662 4.242402 4.335811 4.402773 4.404524
## [19,] 3.683454 4.021509 4.092748 4.100073 4.103565 4.111879 4.146756 4.271825
## [20,] 2.480504 2.646200 2.880935 2.887437 2.911389 2.934531 2.963617 2.973513
## [,9] [,10]
## [1,] 5.238911 5.265439
## [2,] 4.385108 4.545373
## [3,] 3.899205 3.973965
## [4,] 3.703370 3.711111
## [5,] 4.321498 4.365737
## [6,] 3.237093 3.257949
## [7,] 5.037444 5.050847
## [8,] 4.427610 4.444762
## [9,] 3.457839 3.500390
## [10,] 4.059328 4.067687
## [11,] 6.109332 6.165635
## [12,] 5.290724 5.378112
## [13,] 3.600948 3.619886
## [14,] 4.820482 4.828114
## [15,] 4.056766 4.066978
## [16,] 3.967931 3.996245
## [17,] 3.591392 3.638308
## [18,] 4.411018 4.516321
## [19,] 4.321661 4.341861
## [20,] 3.018678 3.051957
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 1 1 0.960 1
## 2 1 1 0.978 0.733
## 3 0.977 1 0.993 0.704
## 4 0.906 1 0.967 0.523
## 5 1 1 0.850 0.647
## 6 1 1 0.921 0.722
## 7 0.974 1 0.967 0.504
## 8 1 1 0.940 0.930
## 9 1 1 0.925 0.983
## 10 1 1 0.960 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>, …
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.0663 -0.0288 -0.0952 0.459 -0.0273
## 2 0.718 0.475 1.05 0.665 1.22
## 3 0.703 0.108 -0.903 -0.659 0.347
## 4 0.540 -0.181 0.0845 0.266 -0.235
## 5 -0.163 1.09 -0.330 0.895 -0.148
## 6 0.412 -0.491 1.60 -0.671 2.40
## 7 -0.130 -0.190 1.51 0.155 -0.212
## 8 -0.185 0.101 -0.121 -0.442 -0.0108
## 9 0.220 -0.411 0.00183 0.740 -0.390
## 10 -0.460 1.67 0.910 -0.576 1.27
## # … 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.185 0.219 0.236 0.263 0.22 ...