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] 934 96 858 865 344 146 584 689 312 786 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 934 274 198 748 721 690 49 799 101 163
## [2,] 96 132 83 323 529 303 612 961 390 940
## [3,] 858 218 428 273 77 487 850 600 295 502
## [4,] 865 805 891 641 595 769 311 463 526 817
## [5,] 344 783 886 717 342 838 699 982 910 747
## [6,] 146 840 16 536 508 115 297 1000 705 67
## [7,] 584 530 869 478 540 847 878 597 65 583
## [8,] 689 722 927 88 797 120 450 524 84 407
## [9,] 312 224 285 737 956 536 63 876 399 150
## [10,] 786 146 488 872 712 644 128 992 16 419
## [11,] 902 311 328 580 224 512 63 374 740 364
## [12,] 240 915 831 193 598 777 212 503 733 511
## [13,] 874 89 534 147 395 65 440 413 778 833
## [14,] 253 975 1000 872 504 405 40 17 786 712
## [15,] 878 478 65 140 147 413 869 743 905 837
## [16,] 758 146 627 83 6 781 439 508 419 391
## [17,] 660 253 678 504 443 222 883 949 585 854
## [18,] 960 893 168 617 136 554 549 878 869 293
## [19,] 846 943 380 744 351 294 873 889 662 667
## [20,] 404 430 765 982 111 448 827 740 224 379
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 4.4 4.47 3.8 2.56 4.19 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 4.404695 4.430561 4.514579 4.623108 4.673146 4.732992 5.067698 5.082232
## [2,] 4.471818 4.594279 4.928534 5.465078 5.468912 5.471035 5.476611 5.510139
## [3,] 3.796821 3.884775 4.664446 4.783366 4.839944 4.848027 4.921099 5.004448
## [4,] 2.561204 2.737319 3.089798 3.090920 3.093601 3.108622 3.167068 3.179231
## [5,] 4.192058 4.338093 4.365472 4.380111 4.395456 4.694595 4.703711 4.707195
## [6,] 3.135509 3.514399 3.549906 3.669989 3.715573 3.858548 3.858565 3.862696
## [7,] 3.020926 3.160619 3.276811 3.301880 3.315433 3.404934 3.461562 3.481479
## [8,] 3.408716 3.416077 3.569294 3.597304 3.612274 3.615328 3.630796 3.642499
## [9,] 2.639848 2.743920 2.754134 2.757017 2.811023 2.862488 2.881418 2.886851
## [10,] 4.273915 4.335591 4.394052 4.675726 4.711066 4.765673 4.818844 4.821064
## [11,] 3.374277 3.521089 3.625169 3.636588 3.661332 3.747647 3.794338 3.822374
## [12,] 3.505909 3.534125 3.625601 3.646689 3.660121 3.688418 3.711446 3.743238
## [13,] 3.083827 3.453594 3.782882 3.837222 3.893162 3.947835 3.980565 4.003967
## [14,] 2.843630 2.858990 3.101917 3.230254 3.302193 3.329606 3.358645 3.374341
## [15,] 4.093142 4.243222 4.253033 4.306909 4.372952 4.381893 4.459908 4.546279
## [16,] 3.181706 3.359119 3.477465 3.494441 3.549906 3.662189 3.844428 3.974488
## [17,] 2.378604 2.548095 2.692719 2.791295 2.798292 2.821334 2.848890 2.853085
## [18,] 3.543183 3.544929 3.819409 3.852089 3.878558 3.974825 4.012285 4.055261
## [19,] 3.736119 3.951824 4.059307 4.426026 4.495380 4.586409 4.604388 4.836423
## [20,] 3.149844 3.259314 3.323011 3.352579 3.538963 3.543742 3.745777 3.800139
## [,9] [,10]
## [1,] 5.083216 5.153642
## [2,] 5.610695 5.656683
## [3,] 5.050680 5.075569
## [4,] 3.191474 3.207526
## [5,] 4.715928 4.731493
## [6,] 3.890021 3.921065
## [7,] 3.521048 3.566801
## [8,] 3.648497 3.657917
## [9,] 2.927191 3.037619
## [10,] 4.824271 4.911455
## [11,] 3.838907 3.840243
## [12,] 3.871621 3.872564
## [13,] 4.075706 4.091761
## [14,] 3.381033 3.392441
## [15,] 4.554148 4.570580
## [16,] 4.020765 4.052769
## [17,] 2.936514 2.985000
## [18,] 4.141194 4.171002
## [19,] 4.949299 4.987097
## [20,] 3.804498 3.805742
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 1 1
## 2 1 1 0.599
## 3 1 1 1
## 4 1 1 1
## 5 1 1 1
## 6 1 1 1
## 7 1 1 1
## 8 1 1 1
## 9 1 1 0.958
## 10 1 1 0.819
## # ℹ 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.439 -0.422 -0.257 -0.478
## 2 0.949 1.49 1.41 0.817
## 3 0.523 0.752 0.664 0.820
## 4 0.508 0.176 1.57 -0.464
## 5 -0.356 1.72 2.54 0.413
## 6 -0.216 0.867 0.0915 -0.167
## 7 -0.165 1.48 -0.470 -0.545
## 8 -0.0175 -0.213 -0.157 -0.482
## 9 -0.167 0.549 1.49 -0.830
## 10 -0.0366 0.395 0.898 -0.523
## # ℹ 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.193 0.176 0.194 0.312 0.207 ...