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] 359 212 570 322 24 34 354 152 373 199 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 359 273 973 622 537 567 704 531 589 251
## [2,] 212 544 985 512 256 675 208 978 900 710
## [3,] 570 38 949 337 634 360 964 484 408 649
## [4,] 322 530 292 956 710 883 361 663 850 512
## [5,] 24 888 650 646 32 803 247 641 962 670
## [6,] 34 7 288 906 517 354 172 588 881 912
## [7,] 354 912 288 6 881 172 34 445 517 836
## [8,] 152 98 519 800 794 15 913 218 763 606
## [9,] 373 323 333 20 759 727 832 404 51 654
## [10,] 199 318 954 984 428 184 212 975 492 951
## [11,] 952 391 260 689 799 913 840 388 717 401
## [12,] 352 131 125 585 130 558 869 449 998 561
## [13,] 166 436 41 773 350 371 267 586 173 972
## [14,] 792 456 952 649 964 116 506 15 794 969
## [15,] 964 456 792 606 952 56 260 152 669 717
## [16,] 834 161 718 886 306 991 71 813 606 52
## [17,] 540 784 848 365 44 495 894 249 562 217
## [18,] 225 852 545 935 605 65 146 975 763 508
## [19,] 989 438 531 523 273 261 990 118 107 69
## [20,] 741 404 891 9 945 207 780 419 270 727
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 4.04 2.64 4.07 2.75 3.71 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 4.035305 4.038179 4.177173 4.372719 4.448643 4.471075 4.475194 4.526836
## [2,] 2.641899 3.104684 3.221942 3.237992 3.239236 3.301745 3.317642 3.326276
## [3,] 4.065915 4.407605 4.497995 4.505718 4.667527 4.684504 4.724088 4.912392
## [4,] 2.754331 2.918729 2.964791 3.064875 3.133546 3.140060 3.180808 3.225316
## [5,] 3.710470 3.795016 3.807232 3.887025 4.108874 4.154509 4.174313 4.200468
## [6,] 3.388935 3.556980 3.772751 4.105836 4.136748 4.336553 4.539583 4.630311
## [7,] 3.265561 3.288425 3.475343 3.556980 3.750875 3.855725 4.066634 4.107363
## [8,] 3.586636 3.682744 3.759096 3.800485 3.800792 4.024737 4.031784 4.049032
## [9,] 3.036149 3.150189 3.182555 3.291503 3.364669 3.395017 3.423078 3.692382
## [10,] 2.636345 2.677292 2.759145 2.812937 2.954664 2.972508 3.004992 3.037446
## [11,] 3.877440 4.057885 4.191101 4.250197 4.308944 4.366451 4.417027 4.456726
## [12,] 3.418311 3.466023 3.659268 3.853933 3.890838 3.931818 3.955120 3.986126
## [13,] 3.473602 3.709277 4.064624 4.405604 4.443858 4.472271 4.487576 4.531966
## [14,] 3.626690 3.781224 3.847783 3.990122 4.104321 4.270507 4.324263 4.374040
## [15,] 2.707680 2.821755 2.941805 3.187454 3.250388 3.292698 3.380915 3.392029
## [16,] 3.231923 4.291241 4.429173 4.587355 5.065862 5.317487 5.576074 5.605470
## [17,] 5.774074 5.989672 6.176424 6.197935 6.322531 6.343355 6.347331 6.373304
## [18,] 2.921099 2.936044 2.995608 3.135549 3.151006 3.357157 3.386441 3.396874
## [19,] 3.409890 3.638739 3.739323 3.769065 3.781679 4.001721 4.033151 4.067418
## [20,] 3.016322 3.263121 3.277102 3.291503 3.309315 3.330960 3.439355 3.489788
## [,9] [,10]
## [1,] 4.548545 4.597727
## [2,] 3.365591 3.369532
## [3,] 4.980528 4.990545
## [4,] 3.267076 3.287998
## [5,] 4.279224 4.284625
## [6,] 4.752419 4.804014
## [7,] 4.215118 4.400167
## [8,] 4.080342 4.106147
## [9,] 3.707049 3.712159
## [10,] 3.061104 3.087089
## [11,] 4.632899 4.649404
## [12,] 4.013946 4.018560
## [13,] 4.623144 4.651978
## [14,] 4.403991 4.409067
## [15,] 3.424335 3.521479
## [16,] 5.750151 5.791681
## [17,] 6.504011 6.512254
## [18,] 3.419248 3.468788
## [19,] 4.083570 4.099165
## [20,] 3.615325 3.617184
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 1 0.618 0.992
## 2 0.983 0.983 0.825
## 3 0.983 0.952 0.970
## 4 1 0.736 0.607
## 5 0.983 0.758 0.910
## 6 0.873 0.932 0.880
## 7 0.983 0.952 0.861
## 8 1 0.823 0.970
## 9 0.871 0.759 0.964
## 10 0.949 0.991 0.607
## # ℹ 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.158 -0.0951 -0.235 -0.369
## 2 -0.0954 -0.168 0.308 0.133
## 3 -0.0111 -0.248 0.302 0.587
## 4 -0.0437 -0.244 0.831 0.451
## 5 -0.0860 -0.237 0.360 0.254
## 6 -0.218 -0.233 -0.0306 -1.52
## 7 -0.365 0.577 0.910 -0.409
## 8 -0.180 0.697 0.571 -0.287
## 9 -0.241 -0.0568 -0.148 -0.0476
## 10 -0.484 -0.309 -0.582 -0.901
## # ℹ 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.213 0.287 0.191 0.302 0.224 ...