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] 882 113 526 832 795 761 541 999 551 61 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 882 725 534 429 474 140 903 640 64 334
## [2,] 113 387 364 208 909 82 228 865 763 255
## [3,] 526 947 498 250 976 60 53 809 658 179
## [4,] 832 547 739 42 519 500 234 48 334 186
## [5,] 795 832 750 500 42 668 501 336 588 617
## [6,] 761 965 80 447 495 796 256 492 507 286
## [7,] 541 503 20 169 499 579 206 275 303 344
## [8,] 999 199 153 604 800 464 453 112 794 56
## [9,] 551 257 886 230 151 433 706 885 120 58
## [10,] 61 180 505 78 441 661 723 979 895 919
## [11,] 270 848 503 877 912 813 20 206 35 23
## [12,] 373 502 922 3 906 498 930 333 698 745
## [13,] 261 501 619 305 207 588 191 25 649 832
## [14,] 778 385 477 279 31 29 711 425 705 432
## [15,] 272 376 562 535 969 426 262 569 666 308
## [16,] 788 112 604 256 424 969 684 569 392 453
## [17,] 539 643 112 688 535 405 281 681 16 604
## [18,] 666 199 774 424 569 840 627 353 826 492
## [19,] 395 207 584 672 906 807 96 384 67 492
## [20,] 683 579 819 344 503 541 221 7 128 772
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.94 3.06 3.2 3.34 2.94 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.936057 4.818958 4.938966 4.975562 5.020366 5.021473 5.044814 5.053021
## [2,] 3.061246 3.307469 3.331167 3.470828 3.643701 3.687836 3.698633 3.698943
## [3,] 3.195276 3.301789 3.310175 3.357944 3.407100 3.483313 3.538887 3.591911
## [4,] 3.341014 3.439879 3.578891 3.615235 3.721213 3.757769 3.770940 3.786052
## [5,] 2.936505 2.942758 3.147516 3.175475 3.425969 3.468957 3.499053 3.623105
## [6,] 3.804883 3.975199 4.641285 4.665559 4.701645 4.711032 4.846632 5.023005
## [7,] 2.642065 2.768876 3.006399 3.030025 3.105921 3.199420 3.275014 3.338633
## [8,] 3.319466 3.540251 3.544454 3.545528 3.644055 3.655483 3.705929 3.723749
## [9,] 3.958606 3.980797 4.217798 4.330786 4.337176 4.399225 4.467726 4.482436
## [10,] 3.645440 4.392323 4.445316 4.471956 4.500594 4.610302 4.611345 4.724322
## [11,] 2.995699 3.143790 3.172041 3.268888 3.321594 3.337925 3.365813 3.380381
## [12,] 3.425248 3.431334 3.641398 3.866902 3.906731 3.961563 4.106953 4.145391
## [13,] 2.881072 3.015790 3.063677 3.068584 3.090575 3.091802 3.118784 3.167195
## [14,] 3.181706 3.356851 3.359119 3.657086 3.786363 3.844428 3.876545 3.985128
## [15,] 3.874802 3.931893 4.046980 4.175979 4.228581 4.305845 4.344556 4.348705
## [16,] 2.736076 2.932863 2.975477 3.000608 3.003486 3.008601 3.044186 3.099771
## [17,] 3.616236 3.887278 3.935782 3.965962 4.088930 4.186605 4.213913 4.235365
## [18,] 2.860810 2.916131 2.936378 3.023871 3.113233 3.203687 3.210857 3.265621
## [19,] 3.259314 3.280480 3.288206 3.317002 3.474107 3.505896 3.543742 3.548727
## [20,] 1.991096 2.139081 2.141978 2.595782 2.867829 2.908637 2.978875 3.006399
## [,9] [,10]
## [1,] 5.095593 5.164546
## [2,] 3.743360 3.757137
## [3,] 3.601772 3.634230
## [4,] 3.953905 4.005098
## [5,] 3.630752 3.667818
## [6,] 5.060767 5.094215
## [7,] 3.390195 3.394642
## [8,] 3.728963 3.791444
## [9,] 4.554737 4.582723
## [10,] 4.802364 4.835690
## [11,] 3.381674 3.403471
## [12,] 4.230430 4.248557
## [13,] 3.245717 3.246812
## [14,] 3.999606 4.020765
## [15,] 4.360477 4.381247
## [16,] 3.109655 3.120947
## [17,] 4.284406 4.304534
## [18,] 3.290374 3.350413
## [19,] 3.713421 3.723879
## [20,] 3.073372 3.074654
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.939 0.994 0.931
## 2 0.971 0.951 0.941
## 3 0.964 0.994 0.980
## 4 0.998 0.951 0.931
## 5 0.939 0.994 0.921
## 6 0.971 0.994 1
## 7 0.998 1 1
## 8 0.909 0.951 0.956
## 9 0.971 0.994 0.947
## 10 0.971 0.994 0.975
## # ℹ 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.0528 -0.313 -0.273 -1.77
## 2 -0.180 -0.255 -0.0147 -0.455
## 3 -0.0957 -0.176 -0.199 -0.493
## 4 0.787 -0.239 0.0882 0.0555
## 5 -0.0728 -0.253 -0.426 -0.429
## 6 -0.597 -0.242 -0.106 -0.624
## 7 -0.148 0.816 -0.0442 -1.36
## 8 -0.0855 -0.406 -0.487 -0.833
## 9 -0.174 -0.613 -0.0850 -0.783
## 10 -0.125 -0.0787 -0.174 -1.12
## # ℹ 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.191 0.258 0.265 0.25 0.271 ...