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] 984 309 792 46 402 608 468 478 859 95 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 984 479 470 402 430 22 79 782 246 422
## [2,] 309 25 871 725 249 306 727 718 285 801
## [3,] 792 170 53 496 943 93 913 994 233 374
## [4,] 46 176 336 672 75 139 793 897 885 395
## [5,] 402 782 519 19 703 873 350 79 974 659
## [6,] 608 928 705 706 683 32 785 311 894 878
## [7,] 468 376 802 532 620 858 163 795 922 186
## [8,] 478 805 843 480 341 202 401 312 666 536
## [9,] 859 746 483 801 813 492 593 662 456 721
## [10,] 95 480 133 668 221 824 314 371 748 806
## [11,] 657 350 19 240 932 467 782 659 873 771
## [12,] 82 604 30 531 133 988 381 465 625 221
## [13,] 613 49 883 563 569 228 196 900 665 991
## [14,] 480 506 221 401 941 776 536 919 806 542
## [15,] 512 947 578 490 716 946 322 259 805 463
## [16,] 941 946 688 335 480 759 976 742 202 133
## [17,] 718 261 191 206 645 276 229 101 249 879
## [18,] 956 545 684 666 301 264 677 748 204 133
## [19,] 350 782 519 5 11 643 657 404 974 703
## [20,] 688 312 463 52 493 536 255 962 843 929
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.18 2.56 4.13 4.19 2.18 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.179104 3.239590 3.322334 3.382186 3.561743 3.615391 3.683914 3.707063
## [2,] 2.564155 3.339232 3.387191 3.678814 3.740663 3.741770 3.782321 3.816347
## [3,] 4.133917 4.296035 4.413266 4.472787 4.535665 4.657785 4.779627 4.838354
## [4,] 4.187657 4.861884 4.928534 5.385785 5.426930 5.471035 5.476611 5.510139
## [5,] 2.183445 2.467871 2.548933 2.564574 2.624695 2.656266 2.873438 2.956495
## [6,] 3.211137 3.639487 3.983039 4.199958 4.844523 4.884269 4.924772 4.977026
## [7,] 4.031615 4.159029 4.328804 4.364631 4.475063 4.528031 4.559770 4.573801
## [8,] 2.314570 2.990672 3.058306 3.058458 3.158027 3.190994 3.192741 3.264128
## [9,] 4.205782 4.536892 4.615946 4.721329 4.746571 4.776526 4.826832 4.897551
## [10,] 3.270726 3.315812 3.326392 3.330879 3.334825 3.362373 3.362938 3.382394
## [11,] 2.187504 2.723375 2.789527 2.865358 2.904483 2.960871 2.967851 2.998460
## [12,] 3.190382 3.202113 3.305286 3.333369 3.365734 3.418780 3.493082 3.495900
## [13,] 4.924318 4.983522 5.001183 5.035517 5.066946 5.197326 5.248303 5.323472
## [14,] 2.848022 2.975231 3.049587 3.056745 3.233157 3.299841 3.302737 3.303199
## [15,] 2.804204 2.983640 3.119714 3.145059 3.161211 3.163343 3.225933 3.250238
## [16,] 2.749829 2.818448 2.873100 2.930088 2.979536 3.162047 3.172413 3.219628
## [17,] 4.241363 4.277992 4.333221 4.350494 4.360575 4.372952 4.381893 4.414951
## [18,] 2.943314 3.074297 3.172212 3.197729 3.343889 3.355104 3.387417 3.398580
## [19,] 2.134410 2.478696 2.560700 2.564574 2.789527 2.793614 2.854047 2.858374
## [20,] 3.241488 3.243865 3.301564 3.325852 3.361672 3.375413 3.432257 3.436741
## [,9] [,10]
## [1,] 3.796854 3.821455
## [2,] 3.825843 3.848661
## [3,] 4.927379 4.952781
## [4,] 5.662498 5.697991
## [5,] 3.022827 3.027098
## [6,] 5.053909 5.091078
## [7,] 4.588397 4.624760
## [8,] 3.323551 3.338076
## [9,] 4.919758 5.023818
## [10,] 3.433720 3.470568
## [11,] 3.010996 3.019052
## [12,] 3.524951 3.534038
## [13,] 5.437753 5.446464
## [14,] 3.312834 3.324134
## [15,] 3.262286 3.352786
## [16,] 3.285046 3.290347
## [17,] 4.441140 4.554148
## [18,] 3.422021 3.432248
## [19,] 2.874898 2.880039
## [20,] 3.439791 3.451233
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.986 0.989 1 0.977 1 0.521 0.809 0.984
## 2 1 0.972 0.989 0.885 0.950 1 0.788 0.932 0.984
## 3 1 0.972 0.989 0.989 1 1 0.900 0.639 0.984
## 4 1 0.972 0.989 0.987 0.918 1 0.802 0.936 0.984
## 5 1 0.972 0.989 0.987 0.825 1 0.462 0.541 0.997
## 6 1 0.986 0.989 0.987 0.950 1 0.788 0.883 0.984
## 7 1 0.929 0.989 0.917 0.908 1 0.0487 0.817 0.984
## 8 1 0.972 0.989 1 0.856 1 0.939 0.842 0.984
## 9 1 0.972 1 1 0.825 1 0.659 0.641 0.984
## 10 1 0.986 0.989 0.987 0.825 1 0.848 1 0.958
## # … 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…¹ CD3(C…² CD3(C…³ CD235…⁴ CD3(Cd…⁵ CD45(…⁶ CD19(…⁷ CD22(…⁸ IgD(Nd…⁹
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 -0.409 -0.277 -0.137 -1.11 -0.331 2.72 1.61 0.727 0.474
## 2 -0.199 -0.0196 0.0463 -1.03 -0.426 2.36 2.70 0.254 -0.00295
## 3 -0.446 -0.204 -0.0761 -0.996 -0.288 3.41 2.07 0.841 0.0288
## 4 -0.411 -0.160 -0.219 -0.816 -0.224 3.65 1.92 -0.203 -0.158
## 5 -0.0815 -0.437 0.363 -2.20 -0.251 2.63 2.21 -0.354 -0.182
## 6 -0.245 -0.291 -0.306 -1.09 -0.166 2.78 2.21 -0.251 -0.141
## 7 -0.460 -0.201 -0.460 -0.741 -0.511 2.51 2.29 -0.116 0.185
## 8 0.0465 -0.0188 -0.146 -1.15 -0.00510 3.02 1.82 0.287 0.416
## 9 -0.0258 -0.0935 -0.234 -1.74 -0.435 3.53 2.29 -0.188 -0.130
## 10 -0.234 -0.0953 -0.451 -0.721 -0.393 1.78 2.09 0.426 -0.0725
## # … 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.259 0.255 0.199 0.173 0.329 ...