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] 385 523 842 370 459 786 545 185 390 185 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 385 302 253 704 156 392 853 857 187 922
## [2,] 523 900 384 560 790 599 495 489 644 565
## [3,] 842 548 6 74 372 612 824 550 929 167
## [4,] 370 592 518 60 367 747 786 22 357 537
## [5,] 459 645 904 606 677 42 292 779 426 101
## [6,] 786 651 842 510 386 563 768 727 3 976
## [7,] 545 890 84 366 243 509 333 238 934 766
## [8,] 185 998 489 9 26 748 852 169 565 646
## [9,] 390 8 728 66 819 389 998 78 650 722
## [10,] 185 2 282 852 851 560 376 59 82 565
## [11,] 345 687 180 177 392 704 195 288 633 531
## [12,] 117 429 611 196 241 463 886 27 328 513
## [13,] 956 491 614 554 757 680 795 914 682 298
## [14,] 72 349 309 164 267 117 429 217 162 326
## [15,] 599 722 63 851 26 646 644 2 439 565
## [16,] 879 924 856 832 419 388 319 314 622 659
## [17,] 850 84 284 369 934 444 911 51 70 360
## [18,] 255 988 122 691 830 780 329 521 77 754
## [19,] 411 869 67 787 518 354 60 691 521 122
## [20,] 519 340 199 416 875 342 713 544 938 37
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.93 3.35 3.29 4.21 4.02 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.934807 3.011332 3.016751 3.041276 3.115193 3.143164 3.178666 3.191518
## [2,] 3.351119 3.650990 4.095008 4.206182 4.235428 4.350250 4.373396 4.392919
## [3,] 3.290888 3.911146 3.926375 3.983958 4.119979 4.182650 4.189109 4.233163
## [4,] 4.209103 4.346599 4.969283 5.004522 5.022780 5.043362 5.046867 5.100007
## [5,] 4.024740 4.211203 4.460094 4.532959 4.568434 4.571980 4.623731 4.631122
## [6,] 3.285947 3.420973 3.556735 3.632301 3.712904 3.784941 3.871373 3.913881
## [7,] 3.348652 3.469219 4.023168 4.047665 4.187657 4.254687 4.396174 4.478943
## [8,] 4.582641 4.682708 4.938197 5.100889 5.189825 5.272487 5.286979 5.290724
## [9,] 4.506636 5.100889 5.324985 5.355285 5.491075 5.520704 5.607854 5.672028
## [10,] 5.652755 5.949307 5.958460 6.051648 6.078806 6.087200 6.206025 6.342065
## [11,] 2.780195 2.846517 3.282821 3.287391 3.450488 3.490798 3.493728 3.504874
## [12,] 2.973577 2.973670 3.013207 3.018629 3.126406 3.142318 3.152739 3.212486
## [13,] 2.947010 3.133154 3.361468 3.418536 3.426967 3.601187 3.696938 3.725041
## [14,] 2.603422 3.280572 3.478222 3.490121 3.492663 3.593597 3.646447 3.648670
## [15,] 3.408298 4.105685 4.237876 4.425122 4.502597 4.512711 4.569172 4.577794
## [16,] 3.266432 3.983039 4.199958 4.585742 4.722467 4.797333 4.846697 5.028735
## [17,] 4.302399 4.338485 4.466796 4.686618 4.709518 4.712367 4.720591 4.834547
## [18,] 3.805116 3.939331 3.963862 4.061989 4.116736 4.131508 4.138642 4.151666
## [19,] 3.174668 3.233375 3.262967 3.285306 3.307915 3.380307 3.411497 3.518862
## [20,] 3.425833 3.466023 3.619415 3.656178 3.873374 3.963417 4.032484 4.090213
## [,9] [,10]
## [1,] 3.242820 3.278678
## [2,] 4.438370 4.467971
## [3,] 4.264294 4.310539
## [4,] 5.106651 5.230399
## [5,] 4.648924 4.683237
## [6,] 3.926375 3.983435
## [7,] 4.555771 4.876762
## [8,] 5.378112 5.501689
## [9,] 5.698508 5.780566
## [10,] 6.371971 6.372856
## [11,] 3.618053 3.629939
## [12,] 3.238886 3.264479
## [13,] 3.734132 3.806068
## [14,] 3.650747 3.663923
## [15,] 4.714625 4.740133
## [16,] 5.034995 5.091078
## [17,] 4.931211 4.945244
## [18,] 4.185250 4.295264
## [19,] 3.537164 3.611575
## [20,] 4.137645 4.144629
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…¹ pCREB…² pBTK(…³ pS6(Y…⁴ cPARP…⁵ pPLCg…⁶ pSrc(…⁷ Ki67(…⁸ pErk1…⁹
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 0.915 1 0.716 1 0.984 0.998 0.978 1
## 2 0.932 0.960 0.989 1 1 0.998 0.975 0.924 1
## 3 0.854 0.842 0.989 0.751 1 0.984 0.914 0.836 1
## 4 0.923 0.915 0.958 0.882 1 0.911 0.940 1 1
## 5 0.854 0.915 0.915 0.939 1 0.971 0.940 0.994 1
## 6 0.887 0.915 0.958 0.945 0.0862 0.971 0.749 0.814 1
## 7 0.945 0.915 0.859 0.894 1 0.930 0.952 0.994 1
## 8 0.854 0.915 0.794 0.894 1 0.971 0.990 0.994 1
## 9 0.921 0.915 0.989 0.867 1 0.984 0.944 0.765 1
## 10 1 0.960 1 0.716 1 0.984 0.963 0.994 1
## # … with 990 more rows, 25 more variables: `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>, …
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)D…¹ CD3(C…² CD3(C…³ CD235…⁴ CD3(C…⁵ CD45(…⁶ CD19(…⁷ CD22(…⁸ IgD(N…⁹
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 -0.0784 1.08 0.860 -0.631 0.449 1.96 0.766 1.87 0.921
## 2 0.373 0.507 1.21 -0.671 1.01 2.67 2.14 1.66 -0.213
## 3 -0.170 -0.705 -0.617 -0.318 -0.209 0.429 1.16 0.191 -0.0542
## 4 0.233 -0.102 2.27 -0.120 0.775 3.54 1.57 -0.0189 -0.463
## 5 -0.240 1.83 0.154 -0.419 0.685 1.06 1.86 0.798 1.26
## 6 0.689 1.67 1.65 0.479 0.624 2.66 1.05 1.12 1.32
## 7 -0.131 -0.0374 0.673 -0.318 1.16 1.72 0.484 2.10 -0.240
## 8 0.438 0.288 0.570 -1.90 0.0718 2.38 2.51 -0.423 1.41
## 9 0.435 -0.253 0.570 0.243 -0.130 1.37 1.48 1.23 0.0774
## 10 -0.252 -0.143 1.82 0.560 0.670 1.15 0.320 -0.159 1.94
## # … with 20 more rows, 42 more variables: `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>, …
# 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.292 0.215 0.224 0.189 0.211 ...