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] 404 705 292 388 959 345 725 766 692 897 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 404 927 851 484 217 401 743 51 141 622
## [2,] 705 856 993 289 687 904 863 495 542 500
## [3,] 292 729 601 22 896 833 907 174 196 751
## [4,] 388 309 534 578 611 853 757 588 314 494
## [5,] 959 696 374 413 965 130 836 213 977 570
## [6,] 345 364 852 528 900 10 317 182 199 359
## [7,] 725 844 152 925 807 69 602 90 161 413
## [8,] 766 327 295 441 566 159 812 729 818 907
## [9,] 692 291 941 519 985 723 208 48 852 672
## [10,] 897 359 565 775 169 484 865 885 199 387
## [11,] 31 788 591 825 663 456 466 904 736 133
## [12,] 140 944 558 711 785 254 791 381 640 186
## [13,] 365 951 872 369 195 495 965 696 674 17
## [14,] 799 695 352 855 115 667 134 620 892 171
## [15,] 723 677 948 379 775 251 36 364 556 678
## [16,] 492 131 896 4 793 734 641 46 754 886
## [17,] 696 720 365 965 607 979 785 570 603 195
## [18,] 637 842 470 300 101 247 571 268 646 194
## [19,] 477 615 780 976 828 457 939 306 343 817
## [20,] 855 5 965 667 558 352 839 476 681 225
## num [1:1000, 1:30] 2.91 3.32 3.23 2.4 2.52 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.907990 3.315326 3.494212 3.599459 3.634984 3.653628 3.661115 3.726571
## [2,] 3.322654 3.936249 4.043499 4.061484 4.088056 4.710420 4.836712 4.917987
## [3,] 3.233651 3.637817 3.730389 4.000872 4.082943 4.173731 4.191401 4.238371
## [4,] 2.397645 2.616470 2.634033 2.887600 2.972622 3.034286 3.119821 3.134913
## [5,] 2.522522 2.594668 2.654381 2.673904 2.730371 2.776866 2.795728 2.805107
## [6,] 3.507298 3.844216 3.861746 3.970851 4.028671 4.422518 4.426944 4.447827
## [7,] 3.095493 3.748259 3.776606 3.786670 3.822604 3.864378 3.879198 3.916573
## [8,] 3.304337 3.411283 3.435706 3.487093 3.524122 3.568235 3.618231 3.682096
## [9,] 4.181244 4.308902 4.463714 4.659617 4.740539 4.801515 4.872632 4.898085
## [10,] 2.421302 2.902257 2.951655 2.972916 2.987907 3.012184 3.078784 3.080089
## [11,] 3.030025 3.032456 3.305017 3.310881 3.352035 3.403182 3.464850 3.500371
## [12,] 3.422645 3.426998 3.578335 3.581049 3.638779 3.703243 3.709984 3.735610
## [13,] 2.921099 3.135549 3.151006 3.331935 3.383372 3.386441 3.396874 3.437473
## [14,] 2.577371 2.680930 2.732229 2.742776 2.825349 2.854281 2.930088 3.027255
## [15,] 3.900709 3.913386 4.312620 4.370300 4.544791 4.557106 4.571585 4.605655
## [16,] 3.432329 3.475996 3.525090 3.645820 3.672116 3.692707 3.743823 3.760203
## [17,] 1.758925 2.339503 2.571217 2.625114 2.686341 2.689488 2.807265 2.929386
## [18,] 3.426378 3.571378 3.660410 3.791979 3.843100 4.040735 4.044435 4.169817
## [19,] 4.125997 4.212677 4.224849 4.313776 4.344830 4.352267 4.371517 4.396136
## [20,] 2.897782 2.980361 3.043255 3.063609 3.099520 3.120598 3.124115 3.134536
## [,9] [,10]
## [1,] 3.767352 3.813664
## [2,] 4.953249 4.960026
## [3,] 4.385018 4.426047
## [4,] 3.163289 3.328066
## [5,] 2.816208 2.875299
## [6,] 4.472996 4.535436
## [7,] 3.931665 3.935371
## [8,] 3.812958 3.844736
## [9,] 4.975107 4.981736
## [10,] 3.104128 3.105719
## [11,] 3.561672 3.618338
## [12,] 3.760073 3.787883
## [13,] 3.438808 3.522970
## [14,] 3.042406 3.115715
## [15,] 4.615802 4.625045
## [16,] 3.789584 3.820932
## [17,] 2.961424 2.967181
## [18,] 4.176826 4.177199
## [19,] 4.440960 4.545927
## [20,] 3.159029 3.208409
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.000 0.970 0.978
## 2 0.827 1 0.784
## 3 0.970 0.970 0.830
## 4 0.778 0.970 0.740
## 5 0.953 1 0.830
## 6 0.958 1 0.912
## 7 0.994 1 0.941
## 8 0.952 0.970 0.968
## 9 0.937 1 0.912
## 10 0.953 1 0.941
## # ℹ 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.492 -0.495 -0.352 -0.456
## 2 -0.194 -0.475 -0.0613 -0.137
## 3 -0.541 0.434 -0.508 -0.981
## 4 -0.620 -1.06 -0.0891 0.454
## 5 -0.165 -0.0847 -0.159 -1.76
## 6 -0.360 -0.314 -0.345 -1.33
## 7 -0.494 -0.108 0.702 -0.969
## 8 -0.287 -0.0939 -0.108 -1.65
## 9 -0.128 -0.141 -0.246 0.273
## 10 -0.374 -0.508 0.725 -1.20
## # ℹ 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.26 0.201 0.221 0.298 0.34 ...