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] 795 241 526 577 12 851 201 732 136 923 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 795 351 491 896 376 578 673 228 973 785
## [2,] 241 590 337 153 736 35 318 524 338 606
## [3,] 526 37 953 262 259 317 384 139 927 462
## [4,] 577 798 263 979 831 290 121 681 300 752
## [5,] 12 878 970 545 583 404 870 556 242 514
## [6,] 851 799 43 30 952 740 724 465 93 711
## [7,] 201 173 500 821 753 237 650 79 392 436
## [8,] 732 906 525 369 281 692 304 542 27 967
## [9,] 136 353 284 922 908 669 320 474 22 53
## [10,] 923 498 834 729 675 678 859 745 791 635
## [11,] 792 939 114 946 308 672 145 928 879 876
## [12,] 83 272 545 130 743 655 808 242 1000 547
## [13,] 260 315 84 676 267 353 294 127 235 336
## [14,] 644 584 636 247 306 194 169 367 234 281
## [15,] 396 274 768 330 472 823 902 400 17 197
## [16,] 365 64 571 182 666 442 450 460 441 427
## [17,] 318 274 993 330 2 534 241 902 524 309
## [18,] 850 62 410 136 320 747 908 831 676 922
## [19,] 724 382 809 504 234 399 756 113 90 306
## [20,] 840 185 818 383 566 835 198 234 981 81
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.29 2.18 2.58 3.57 4.75 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.290888 3.414196 3.900711 3.911146 3.983958 4.167455 4.182650 4.213088
## [2,] 2.183445 2.756893 2.809988 2.818641 2.843641 2.861485 3.004018 3.010573
## [3,] 2.579222 3.234159 3.265297 3.279824 3.345636 3.351763 3.398584 3.407045
## [4,] 3.568042 3.610267 3.730640 3.953613 3.989898 4.056113 4.061409 4.087309
## [5,] 4.751248 4.859596 4.962199 5.136586 5.154861 5.165437 5.172362 5.228120
## [6,] 6.463770 6.683202 6.938729 7.053923 7.059653 7.245525 7.246955 7.414659
## [7,] 4.076489 4.274600 4.423634 4.925839 5.190719 5.219531 5.265439 5.278256
## [8,] 3.802872 3.915565 4.148939 4.291177 4.387384 4.400052 4.413866 4.444378
## [9,] 3.415443 3.617761 3.618898 3.623503 3.630198 3.754726 3.821986 3.892174
## [10,] 3.891206 4.495225 4.720460 4.854444 4.865578 4.866282 4.870006 4.935016
## [11,] 2.617387 2.863066 3.054148 3.073789 3.125896 3.300264 3.315473 3.347548
## [12,] 3.413191 3.810307 3.930539 3.983984 4.012622 4.057321 4.332587 4.339549
## [13,] 2.919401 2.938252 2.951220 3.247975 3.324109 3.352014 3.353873 3.425003
## [14,] 2.306681 2.808949 3.127478 3.170588 3.199996 3.221523 3.280480 3.366557
## [15,] 4.139264 4.337176 4.343024 4.360294 4.399225 4.427340 4.516196 4.527090
## [16,] 3.760809 3.842983 3.936046 4.018204 4.042841 4.112165 4.193027 4.226467
## [17,] 3.802450 3.897873 3.909321 3.912981 3.948973 4.008490 4.135402 4.206469
## [18,] 3.067959 3.285664 3.331935 3.548807 3.598409 3.739082 3.783128 3.816269
## [19,] 3.399958 3.563293 3.666377 3.790918 3.804146 3.835806 3.880675 3.933761
## [20,] 3.787225 3.804993 4.002830 4.061257 4.108713 4.110622 4.127757 4.131733
## [,9] [,10]
## [1,] 4.233163 4.264294
## [2,] 3.017980 3.042647
## [3,] 3.426415 3.428287
## [4,] 4.123284 4.130651
## [5,] 5.286850 5.305812
## [6,] 7.418476 7.515099
## [7,] 5.419546 5.448885
## [8,] 4.597965 4.606002
## [9,] 3.902825 3.957933
## [10,] 4.955969 4.959065
## [11,] 3.390372 3.392534
## [12,] 4.341292 4.376458
## [13,] 3.437349 3.471917
## [14,] 3.409308 3.413676
## [15,] 4.582723 4.620931
## [16,] 4.229835 4.230951
## [17,] 4.214673 4.218315
## [18,] 3.817576 3.828673
## [19,] 4.050061 4.054023
## [20,] 4.144173 4.186433
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.927 0.970 0.966
## 2 0.743 0.973 0.966
## 3 0.776 0.973 0.966
## 4 0.987 0.970 0.992
## 5 0.938 0.970 1
## 6 0.987 0.899 0.966
## 7 0.978 0.980 0.992
## 8 0.987 0.892 0.966
## 9 0.628 0.951 0.992
## 10 0.978 0.892 0.966
## # ℹ 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.159 -0.235 -0.0423 -1.17
## 2 -0.161 0.888 -1.28 -0.470
## 3 -0.143 -0.484 -0.289 -0.892
## 4 -1.07 -0.650 0.0482 -1.15
## 5 0.946 -0.155 -0.0662 -1.13
## 6 -0.201 -0.220 0.723 -1.64
## 7 -0.221 -0.00163 -0.186 -1.05
## 8 -1.26 -0.980 -0.244 -1.77
## 9 0.392 -0.0429 -0.335 -2.89
## 10 -0.0995 0.198 0.429 -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.225 0.317 0.285 0.238 0.179 ...