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] 728 600 472 977 173 799 564 485 52 176 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 728 428 975 191 324 840 639 261 174 272
## [2,] 600 470 214 872 989 833 952 257 414 482
## [3,] 472 872 838 855 670 909 19 651 482 170
## [4,] 977 392 611 610 976 444 821 827 932 651
## [5,] 173 22 767 588 852 785 396 496 401 784
## [6,] 799 668 572 224 123 941 494 702 802 755
## [7,] 564 528 810 92 292 902 301 892 424 344
## [8,] 485 213 745 841 677 492 323 548 631 217
## [9,] 52 853 534 759 346 568 724 932 50 673
## [10,] 176 949 314 567 352 571 695 131 38 819
## [11,] 869 595 260 434 818 234 269 405 462 300
## [12,] 622 728 428 637 116 446 975 200 261 369
## [13,] 869 876 969 129 494 787 895 738 409 617
## [14,] 785 972 385 78 221 650 898 292 516 966
## [15,] 803 368 225 959 618 565 253 191 780 541
## [16,] 841 631 473 300 846 680 506 26 190 398
## [17,] 226 793 211 780 428 908 191 467 261 134
## [18,] 952 656 560 833 172 214 426 628 156 676
## [19,] 800 724 638 89 651 170 283 863 872 381
## [20,] 550 903 652 496 965 80 944 788 661 860
## num [1:1000, 1:30] 2.63 3.63 4.21 2.34 3.07 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.628028 2.760060 2.976200 3.013099 3.063431 3.084616 3.098669 3.193783
## [2,] 3.630493 3.647824 3.978432 3.993102 4.012787 4.014178 4.093523 4.099559
## [3,] 4.209103 4.346599 4.352202 4.442967 4.600549 4.848014 4.877127 4.969283
## [4,] 2.343261 2.696411 2.809258 2.961742 2.967441 3.137996 3.154311 3.159903
## [5,] 3.072292 3.338310 3.490052 3.572306 3.673769 3.692126 3.729749 3.827220
## [6,] 3.653781 3.736796 3.782037 3.816203 3.837499 4.078010 4.079693 4.083511
## [7,] 4.631537 5.024567 5.657185 5.830067 6.037061 6.106859 6.207261 6.271510
## [8,] 3.018629 3.079598 3.190289 3.297410 3.335644 3.394667 3.412268 3.482824
## [9,] 3.488930 3.517163 3.555011 3.837582 3.879786 4.043410 4.073645 4.140539
## [10,] 4.376914 4.574178 5.123233 5.164057 5.180396 5.180790 5.223843 5.227210
## [11,] 3.247260 3.369159 3.412549 3.657511 3.708235 3.760682 3.867757 3.958814
## [12,] 2.453201 2.967051 3.104128 3.167093 3.185010 3.189332 3.206891 3.218411
## [13,] 2.612928 2.724873 2.775316 2.802767 2.941141 3.009110 3.046709 3.070737
## [14,] 3.933836 4.226732 4.237876 4.526058 4.617088 4.699386 4.702130 4.705292
## [15,] 3.850458 4.187139 4.248591 4.255944 4.289726 4.326873 4.328134 4.390694
## [16,] 2.366281 2.630707 2.635397 2.667544 2.752493 2.869122 2.914047 2.964926
## [17,] 3.745536 3.764372 3.786412 3.790699 3.822202 3.822912 3.824388 3.875307
## [18,] 2.951628 3.037363 3.048558 3.090264 3.177894 3.241036 3.260322 3.299411
## [19,] 2.915530 3.064093 3.485632 3.503581 3.653345 3.700618 3.732034 3.756622
## [20,] 4.681334 4.720201 4.728447 4.841910 4.903387 4.983522 4.996874 5.001183
## [,9] [,10]
## [1,] 3.221482 3.252045
## [2,] 4.115680 4.190158
## [3,] 5.004522 5.022780
## [4,] 3.462435 3.465246
## [5,] 3.932257 4.028702
## [6,] 4.089239 4.103829
## [7,] 6.422010 6.426686
## [8,] 3.515227 3.532004
## [9,] 4.288083 4.358622
## [10,] 5.313258 5.365150
## [11,] 3.990841 3.996589
## [12,] 3.255873 3.264745
## [13,] 3.142829 3.206543
## [14,] 4.757414 4.793385
## [15,] 4.429798 4.437444
## [16,] 2.968273 2.972508
## [17,] 3.899263 3.917406
## [18,] 3.319166 3.329592
## [19,] 3.785342 3.910815
## [20,] 5.066946 5.081251
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.840 0.964 0.810
## 2 0.840 0.964 0.981
## 3 0.978 0.762 0.963
## 4 0.954 1 0.967
## 5 0.840 0.986 0.784
## 6 0.840 1 0.989
## 7 0.871 1 0.614
## 8 0.893 0.964 1
## 9 0.897 0.964 0.863
## 10 0.840 0.982 0.973
## # ℹ 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.234 0.373 0.244 -0.252
## 2 0.0465 -0.0188 -0.146 -1.15
## 3 -0.0710 -0.00365 -0.235 -0.892
## 4 -0.0179 -0.0305 -0.663 -0.897
## 5 0.237 -0.0447 -0.119 -0.717
## 6 -0.506 -0.0602 0.145 0.158
## 7 -0.148 0.816 -0.0442 -1.36
## 8 -0.179 -0.131 -0.159 -0.550
## 9 -0.474 -0.231 -0.312 -0.419
## 10 -0.236 -0.109 0.00696 -1.02
## # ℹ 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.3 0.227 0.197 0.283 0.239 ...