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] 960 727 924 571 874 651 200 454 426 329 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 960 661 330 214 221 942 352 875 769 308
## [2,] 727 436 970 434 802 894 302 483 885 326
## [3,] 924 196 147 229 110 209 619 645 541 984
## [4,] 571 901 205 561 539 603 303 239 488 48
## [5,] 874 739 925 553 537 443 772 530 703 457
## [6,] 651 332 755 948 296 906 903 795 512 943
## [7,] 200 778 552 253 807 695 31 204 850 558
## [8,] 454 31 406 807 564 142 910 205 272 250
## [9,] 426 766 784 431 948 753 6 257 49 628
## [10,] 329 832 259 966 935 898 188 964 669 344
## [11,] 810 898 294 525 929 762 543 626 715 233
## [12,] 233 724 405 957 243 350 294 450 626 594
## [13,] 502 60 332 701 787 649 193 645 710 633
## [14,] 727 483 340 202 652 90 584 526 326 260
## [15,] 848 3 267 666 984 785 169 945 609 623
## [16,] 600 483 972 45 834 336 629 353 326 967
## [17,] 96 892 198 822 238 164 871 757 269 743
## [18,] 643 438 192 754 709 919 44 498 185 39
## [19,] 893 624 487 728 705 740 737 998 946 622
## [20,] 716 457 205 893 910 206 250 783 443 622
## num [1:1000, 1:30] 3.02 4.52 3.43 2.45 3.06 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.021091 3.316838 3.340988 3.388964 3.396474 3.425448 3.432843 3.507701
## [2,] 4.520879 4.658434 4.907575 5.006399 5.159696 5.275184 5.375401 5.459774
## [3,] 3.434016 3.754113 3.763387 3.860863 3.905281 3.933323 3.936357 3.978739
## [4,] 2.445958 2.572952 2.594500 3.228989 3.247383 3.393072 3.484621 3.543450
## [5,] 3.060229 3.224915 3.267385 3.389055 3.401454 3.469919 3.488956 3.497579
## [6,] 3.154132 3.367718 3.379957 3.582910 3.678452 3.766107 3.965732 4.139017
## [7,] 3.772988 3.919439 4.257397 4.347584 4.360253 4.360755 4.437687 4.477520
## [8,] 3.804993 3.890469 4.061257 4.110622 4.131106 4.131733 4.144173 4.186433
## [9,] 4.396174 6.150058 6.327994 6.434244 6.508282 6.651589 6.655409 7.077338
## [10,] 3.565462 3.610449 3.661371 3.680563 3.686514 3.746277 3.763411 3.817142
## [11,] 3.175226 3.266918 3.319018 3.618556 3.627434 3.688390 3.694279 3.719334
## [12,] 2.068677 2.317734 2.803685 3.034342 3.199871 3.272868 3.357944 3.415477
## [13,] 3.691483 3.795969 3.998774 4.067460 4.114849 4.167996 4.173893 4.240712
## [14,] 3.028344 3.958279 4.019967 4.038856 4.050739 4.106526 4.113139 4.262485
## [15,] 4.162629 4.350369 4.431408 4.498161 4.581570 4.596017 4.679746 4.715303
## [16,] 3.322967 3.598777 4.015577 4.140898 4.255452 4.315535 4.439363 4.479635
## [17,] 4.448051 4.553199 4.621575 4.623556 4.660331 4.700681 4.850029 4.855307
## [18,] 3.166222 3.461670 3.579958 3.602633 3.628453 3.713589 3.732426 3.750626
## [19,] 2.382848 2.787093 2.816422 2.866770 2.880935 2.971006 3.036737 3.057822
## [20,] 2.577371 2.732229 2.825349 2.854281 2.899957 2.930088 2.943991 2.945117
## [,9] [,10]
## [1,] 3.512956 3.518717
## [2,] 5.518690 5.561610
## [3,] 3.992020 3.998920
## [4,] 3.562495 3.569174
## [5,] 3.573926 3.612122
## [6,] 4.236880 4.239740
## [7,] 4.564986 4.566436
## [8,] 4.226078 4.230676
## [9,] 7.098653 7.224055
## [10,] 3.822280 3.833874
## [11,] 3.725180 3.740811
## [12,] 3.433735 3.433897
## [13,] 4.244569 4.265978
## [14,] 4.313934 4.395319
## [15,] 4.761266 4.772071
## [16,] 4.480454 4.587838
## [17,] 4.883069 4.904913
## [18,] 3.759532 3.786793
## [19,] 3.060117 3.068315
## [20,] 2.970063 3.027255
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.622 0.953 0.871
## 2 0.964 0.919 0.831
## 3 1 0.956 0.931
## 4 0.806 0.972 0.882
## 5 0.990 0.972 1
## 6 0.951 0.956 0.639
## 7 0.956 0.956 0.758
## 8 0.964 1 0.634
## 9 0.909 0.967 0.871
## 10 0.956 1 0.758
## # ℹ 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.291 -0.0899 -0.355 -0.511
## 2 -0.179 -0.350 -0.120 -1.28
## 3 -0.365 -0.565 0.383 -2.30
## 4 0.594 0.146 0.120 0.381
## 5 -0.226 0.115 -0.957 0.138
## 6 -0.0559 -0.157 0.375 -0.539
## 7 -0.00438 -0.304 -0.248 0.347
## 8 -0.218 -0.0910 -0.198 0.145
## 9 -0.188 -0.667 0.0952 -1.31
## 10 0.445 0.0987 -0.146 -0.958
## # ℹ 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.276 0.179 0.244 0.282 0.275 ...