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] 337 274 246 766 539 329 593 796 922 34 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 337 678 354 485 237 552 186 884 315 458
## [2,] 274 851 933 976 563 434 306 179 460 109
## [3,] 246 107 335 819 504 307 119 664 809 266
## [4,] 766 66 835 542 482 873 508 243 94 633
## [5,] 539 181 266 78 694 818 56 449 833 236
## [6,] 329 388 842 960 243 154 93 456 808 134
## [7,] 593 23 667 830 470 14 892 996 799 712
## [8,] 796 498 614 174 531 34 453 179 273 9
## [9,] 922 544 174 34 491 182 902 498 290 278
## [10,] 34 900 532 413 225 153 572 843 902 386
## [11,] 176 845 100 20 745 832 523 534 823 479
## [12,] 139 357 856 498 370 85 182 278 224 220
## [13,] 470 778 465 811 961 823 141 302 104 773
## [14,] 593 693 7 534 374 20 321 830 503 844
## [15,] 111 500 583 798 958 512 681 634 215 820
## [16,] 979 408 641 581 372 825 898 644 627 18
## [17,] 676 927 538 994 922 150 182 672 364 324
## [18,] 408 525 966 787 768 609 898 430 148 654
## [19,] 571 240 676 199 994 314 460 17 577 284
## [20,] 100 534 61 112 192 479 465 996 302 14
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 4.4 3.64 3.67 3.05 3.09 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 4.395033 4.589795 4.603207 4.664271 4.740314 4.767237 4.924132 4.933183
## [2,] 3.637631 4.259387 4.369343 4.480685 4.621630 4.627091 4.707778 4.718599
## [3,] 3.669831 3.806788 4.199250 4.210395 4.463766 4.470530 4.479801 4.523224
## [4,] 3.053378 3.261854 3.287635 3.333300 3.555113 3.629050 3.641341 3.670389
## [5,] 3.092051 3.425935 3.477628 3.530486 3.562070 3.589284 3.618062 3.675332
## [6,] 2.735375 2.780195 2.846517 3.118594 3.287391 3.443227 3.451687 3.483039
## [7,] 2.788923 3.036149 3.119447 3.180229 3.262160 3.291503 3.364669 3.392516
## [8,] 3.786505 3.862875 3.875960 3.880346 3.945621 4.072147 4.088545 4.152295
## [9,] 2.453201 2.967051 2.974989 3.104128 3.185010 3.191709 3.206891 3.255873
## [10,] 3.560927 3.627175 3.658589 3.692020 3.836290 3.849979 3.962801 3.975738
## [11,] 3.397861 3.480259 3.542899 3.599337 3.656047 3.664770 3.736488 3.753828
## [12,] 2.853666 2.940074 2.977736 3.059686 3.105622 3.139441 3.203300 3.283071
## [13,] 2.531550 2.950760 3.255711 3.294081 3.299178 3.330589 3.461365 3.490697
## [14,] 3.073767 3.277102 3.291503 3.309315 3.330960 3.373050 3.419556 3.428579
## [15,] 4.398089 4.463140 4.501910 4.682387 4.750415 4.847870 4.950003 4.951450
## [16,] 3.424894 3.452362 3.692459 3.708476 3.782683 3.796636 3.830579 3.890667
## [17,] 2.905118 3.353017 3.399958 3.666377 3.920321 3.933761 4.020214 4.066022
## [18,] 3.072292 3.485924 3.489735 3.583584 3.685677 3.691550 3.721304 3.729892
## [19,] 4.913575 5.054053 5.287737 5.310950 5.346754 5.389461 5.479962 5.568654
## [20,] 2.825372 2.887469 3.174962 3.228291 3.251515 3.284541 3.314874 3.334937
## [,9] [,10]
## [1,] 4.950767 5.048756
## [2,] 4.742350 4.763184
## [3,] 4.559657 4.579138
## [4,] 3.671473 3.712238
## [5,] 3.684008 3.686851
## [6,] 3.569336 3.597690
## [7,] 3.395017 3.423078
## [8,] 4.199830 4.262573
## [9,] 3.264745 3.343813
## [10,] 3.981353 4.055321
## [11,] 3.813897 3.854260
## [12,] 3.346715 3.447538
## [13,] 3.527052 3.543694
## [14,] 3.428636 3.439355
## [15,] 4.989978 5.027995
## [16,] 3.905504 4.036675
## [17,] 4.135119 4.154608
## [18,] 3.735966 3.824062
## [19,] 5.569669 5.578319
## [20,] 3.363391 3.373050
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.880 1 1
## 2 1 1 1
## 3 0.880 1 1
## 4 1.00 1 1
## 5 1.00 1 1
## 6 0.880 1 1
## 7 0.880 1 1
## 8 1 1 1
## 9 0.924 1 1
## 10 0.954 1 1
## # ℹ 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.0307 -0.00396 -0.0498 -2.64
## 2 -0.0477 -0.0514 -0.0339 -0.474
## 3 -0.146 -0.238 0.164 -2.29
## 4 -0.0603 0.798 0.460 -1.21
## 5 -0.0648 -0.0166 -0.0818 -0.681
## 6 -0.254 -0.193 -0.121 -0.267
## 7 -0.0322 -0.451 0.308 -1.70
## 8 -0.161 -0.241 -0.232 -1.92
## 9 -0.138 -0.170 0.651 -0.824
## 10 -0.262 -0.454 -0.110 -0.447
## # ℹ 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.189 0.209 0.216 0.27 0.266 ...