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] 713 845 522 617 923 956 542 854 805 440 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 713 435 96 559 145 498 71 320 428 242
## [2,] 845 410 62 754 859 263 104 280 796 663
## [3,] 522 110 751 565 33 397 501 69 898 700
## [4,] 617 44 202 593 88 926 525 257 397 565
## [5,] 923 765 466 202 925 350 138 504 267 869
## [6,] 956 295 106 280 416 732 176 858 930 835
## [7,] 542 560 629 656 11 819 335 58 771 868
## [8,] 854 112 781 671 340 594 373 784 35 756
## [9,] 805 757 639 866 927 957 806 367 662 958
## [10,] 440 36 229 180 397 483 593 889 267 705
## [11,] 874 172 724 560 338 771 7 656 901 804
## [12,] 150 925 536 282 278 64 301 427 88 547
## [13,] 103 742 695 59 330 84 34 533 164 518
## [14,] 941 464 701 953 948 860 883 673 39 762
## [15,] 673 953 313 762 178 14 460 733 464 146
## [16,] 88 904 207 601 681 185 64 654 593 101
## [17,] 207 88 151 382 547 885 469 282 904 476
## [18,] 562 857 283 310 856 73 583 767 798 568
## [19,] 98 858 194 280 849 879 845 41 564 802
## [20,] 629 407 724 542 656 936 417 107 777 997
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.45 3.45 3.64 3.14 2.81 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.453201 2.974989 2.993984 3.167093 3.191709 3.255873 3.264745 3.343012
## [2,] 3.450199 3.622264 3.624838 3.629024 3.704721 3.757948 3.763912 3.800639
## [3,] 3.638902 3.709156 3.920126 4.029866 4.133649 4.134326 4.144389 4.192520
## [4,] 3.135595 3.182555 3.319273 3.378801 3.435176 3.459564 3.497410 3.497890
## [5,] 2.809595 3.084950 3.262967 3.310221 3.311277 3.340979 3.351861 3.394686
## [6,] 3.301887 3.345912 3.351763 3.488228 3.561365 3.597586 3.605771 3.644124
## [7,] 2.934984 3.764248 3.967805 4.012219 4.076537 4.215822 4.229107 4.233413
## [8,] 4.062286 4.135165 4.456790 4.551161 4.552660 4.553719 4.818772 4.888683
## [9,] 2.782334 2.822860 3.199358 3.286340 3.354150 3.360490 3.363290 3.380142
## [10,] 2.610518 3.067207 3.284541 3.341811 3.516643 3.555936 3.830086 3.853622
## [11,] 3.494201 3.797314 3.827327 3.904092 3.994113 4.050866 4.076537 4.093090
## [12,] 2.912934 3.012112 3.230329 3.238801 3.304815 3.340812 3.447265 3.567269
## [13,] 3.576371 3.866266 4.166911 4.269376 4.409877 4.592158 4.620142 4.681892
## [14,] 3.417577 3.473444 3.484612 3.582380 3.598714 3.619918 3.663096 3.670983
## [15,] 3.426378 3.571378 3.660410 3.703144 3.728955 3.791979 3.843100 3.868399
## [16,] 2.766007 2.799618 2.978875 3.048748 3.064958 3.222205 3.339832 3.405469
## [17,] 3.337121 3.419711 3.579958 3.624421 3.696849 3.855991 3.861370 3.907346
## [18,] 2.588691 2.701912 2.783561 2.862886 2.885986 2.887831 2.919410 2.931054
## [19,] 3.107219 3.376791 3.683455 3.694217 3.713090 3.766105 3.854338 3.867101
## [20,] 3.077660 3.318162 3.368151 3.518837 3.584649 3.644989 3.697669 3.729892
## [,9] [,10]
## [1,] 3.343813 3.402697
## [2,] 3.822214 3.824715
## [3,] 4.238596 4.296554
## [4,] 3.541882 3.542501
## [5,] 3.397861 3.404432
## [6,] 3.654163 3.661292
## [7,] 4.295168 4.311736
## [8,] 4.912399 4.958404
## [9,] 3.467987 3.555936
## [10,] 3.854260 3.922177
## [11,] 4.104316 4.143719
## [12,] 3.604383 3.638785
## [13,] 4.765325 4.767480
## [14,] 3.729084 3.751466
## [15,] 3.955882 4.040735
## [16,] 3.405926 3.431967
## [17,] 3.915937 3.940176
## [18,] 2.956127 3.029521
## [19,] 3.941998 3.948060
## [20,] 3.819647 3.838092
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.732 1 0.903
## 2 0.859 1 0.903
## 3 0.779 1 1
## 4 1 1 0.930
## 5 0.799 1 0.808
## 6 0.437 1 0.859
## 7 0.779 1 0.734
## 8 0.749 1 0.943
## 9 0.875 1 0.903
## 10 0.969 1 0.965
## # ℹ 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.313 -0.0154 0.0303 0.120
## 2 0.771 -0.0114 -0.226 0.760
## 3 0.272 -0.133 0.572 0.471
## 4 -0.888 -0.810 -0.381 -0.521
## 5 -0.645 -0.311 -0.677 -0.544
## 6 -0.196 -0.0581 -0.0928 0.957
## 7 -0.262 -0.0444 -0.140 -1.74
## 8 -0.0385 -0.224 -0.194 0.227
## 9 -0.173 -0.127 -0.149 -0.246
## 10 -0.00750 -0.589 -0.810 0.738
## # ℹ 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.29 0.258 0.229 0.276 0.285 ...