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] 132 237 225 152 567 977 500 615 340 14 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 132 553 455 593 546 993 477 979 603 548
## [2,] 237 156 870 515 643 114 229 567 907 577
## [3,] 225 73 758 917 654 309 71 450 368 139
## [4,] 152 210 492 736 552 688 479 81 194 109
## [5,] 567 716 924 489 854 734 733 503 870 377
## [6,] 977 258 330 838 565 102 694 646 897 481
## [7,] 500 563 409 497 685 687 117 245 996 483
## [8,] 615 136 70 509 507 762 839 508 396 540
## [9,] 340 972 627 125 901 667 306 587 742 351
## [10,] 14 337 913 755 501 947 687 122 892 641
## [11,] 580 450 257 655 978 768 249 968 73 758
## [12,] 680 732 524 460 332 392 377 291 912 854
## [13,] 698 632 864 564 540 226 654 728 978 55
## [14,] 190 10 906 501 684 913 293 436 892 473
## [15,] 846 977 299 496 431 699 682 108 569 467
## [16,] 873 939 76 60 369 85 954 478 196 311
## [17,] 554 45 25 466 479 925 813 480 81 560
## [18,] 665 612 916 785 21 752 205 238 361 812
## [19,] 564 450 917 986 79 550 73 760 768 543
## [20,] 861 466 815 65 479 677 278 813 240 717
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.9 3.12 3.59 2.61 3.24 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.902729 2.917114 3.557851 3.601325 3.722776 3.807435 4.155591 4.203474
## [2,] 3.119796 3.128173 3.157952 3.235178 3.327577 3.334009 3.368453 3.397645
## [3,] 3.586859 4.177142 4.293058 4.378505 4.512164 4.573769 4.601587 4.625823
## [4,] 2.610518 3.045004 3.228404 3.275969 3.400690 3.466070 3.476170 3.481313
## [5,] 3.238448 3.531683 3.654889 3.856978 3.864263 3.899068 3.904249 3.929473
## [6,] 3.297419 3.340251 3.457041 3.484926 3.732310 3.902945 3.921310 3.970990
## [7,] 3.149844 3.181980 3.259314 3.280480 3.345491 3.474107 3.538963 3.548727
## [8,] 3.685918 3.896625 3.922677 3.939024 3.963700 3.975300 3.975664 3.986317
## [9,] 3.428157 4.057365 4.189505 4.387221 4.499547 4.536892 4.615946 4.710349
## [10,] 4.140878 4.408850 4.455586 4.468671 4.496290 4.564612 4.596109 4.599705
## [11,] 2.676100 2.741691 2.895198 3.002550 3.076873 3.173130 3.193548 3.250339
## [12,] 4.346599 4.442967 4.600549 4.623144 4.835635 4.877127 4.889187 4.916895
## [13,] 3.378475 3.423775 3.435403 3.571199 3.611080 3.622770 3.661394 3.777268
## [14,] 4.135165 4.140878 4.146091 4.198442 4.430188 4.720072 4.735282 4.761297
## [15,] 3.037817 3.323634 3.640663 3.687496 3.761022 3.820059 4.009023 4.021804
## [16,] 3.720459 4.049438 4.105591 4.154391 4.258111 4.401926 4.444575 4.539583
## [17,] 2.914838 3.036909 3.067011 3.259954 3.328766 3.344920 3.418359 3.468081
## [18,] 4.033836 4.244262 4.267418 4.375252 4.587822 4.676960 4.717137 4.722959
## [19,] 2.827524 3.004994 3.075816 3.079853 3.150315 3.363991 3.394503 3.411818
## [20,] 3.221538 3.651744 3.857558 3.914426 3.969115 4.034457 4.039353 4.080222
## [,9] [,10]
## [1,] 4.251814 4.322580
## [2,] 3.476889 3.486682
## [3,] 4.662360 4.681746
## [4,] 3.528192 3.540702
## [5,] 3.952657 3.954240
## [6,] 4.044705 4.072332
## [7,] 3.555555 3.562237
## [8,] 3.995078 4.130790
## [9,] 4.721329 4.776526
## [10,] 4.722035 4.740512
## [11,] 3.258761 3.263031
## [12,] 4.920839 4.969283
## [13,] 3.886309 3.898330
## [14,] 4.817959 4.843023
## [15,] 4.052655 4.098345
## [16,] 4.603931 4.650348
## [17,] 3.522563 3.609720
## [18,] 4.848288 4.872995
## [19,] 3.413131 3.440729
## [20,] 4.192146 4.197303
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.916 0.920 0.997
## 2 0.983 0.755 0.901
## 3 0.917 0.610 0.910
## 4 0.963 0.947 0.901
## 5 0.798 0.739 0.847
## 6 0.829 0.610 0.972
## 7 0.985 0.962 0.947
## 8 0.945 0.921 0.910
## 9 0.983 0.858 0.910
## 10 0.916 0.755 0.816
## # ℹ 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.0255 -0.414 -0.415 -0.484
## 2 -0.361 -0.360 -2.28 0.0225
## 3 -1.08 -1.02 -2.00 -2.00
## 4 -0.313 -0.360 -0.402 -1.07
## 5 -0.299 -0.862 -0.699 -0.587
## 6 -0.734 -0.420 -0.964 -0.402
## 7 -0.143 -0.207 -0.184 -0.790
## 8 -1.42 -0.883 -1.43 -1.02
## 9 -0.697 1.24 -1.26 -3.13
## 10 -0.413 -0.431 -0.697 -1.26
## # ℹ 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.231 0.282 0.21 0.277 0.25 ...