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] 135 258 943 604 846 229 711 275 814 632 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 135 559 45 393 758 913 676 304 881 455
## [2,] 258 768 240 874 541 311 549 789 694 104
## [3,] 943 832 519 953 207 582 740 610 84 51
## [4,] 604 265 326 918 969 163 501 852 716 320
## [5,] 846 627 630 511 989 773 420 4 163 297
## [6,] 229 47 724 210 705 870 709 257 682 49
## [7,] 711 709 250 103 594 897 74 341 464 174
## [8,] 275 203 341 711 659 661 822 7 476 644
## [9,] 814 558 490 581 363 653 657 574 847 226
## [10,] 632 932 703 56 172 624 726 36 820 247
## [11,] 803 892 225 554 334 950 183 55 248 920
## [12,] 380 166 130 545 150 195 797 180 285 542
## [13,] 861 926 11 183 516 999 237 51 161 803
## [14,] 565 720 417 983 437 613 447 826 865 466
## [15,] 716 367 722 67 772 667 181 371 241 258
## [16,] 193 770 88 325 376 505 67 378 412 756
## [17,] 636 381 275 278 577 660 680 923 911 311
## [18,] 265 368 349 457 716 737 333 604 326 252
## [19,] 280 563 30 317 929 98 390 977 456 957
## [20,] 827 802 481 915 18 951 374 253 918 604
## num [1:1000, 1:30] 3.31 3.19 3.95 2.61 3.43 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.307951 3.546798 3.898668 4.137591 4.140438 4.373694 4.423461 4.436593
## [2,] 3.190382 3.202113 3.400571 3.477230 3.506575 3.515697 3.524951 3.543076
## [3,] 3.952653 4.491266 4.607518 4.615528 4.685829 4.832555 4.844766 5.069151
## [4,] 2.612928 2.709035 2.802767 2.904681 3.009110 3.016592 3.046709 3.070737
## [5,] 3.426378 3.480374 3.571378 3.616311 3.703144 3.765286 3.791979 3.955882
## [6,] 3.154017 3.185010 3.372972 3.707808 3.785822 3.810819 3.838643 3.857591
## [7,] 2.628028 2.976200 3.013099 3.063431 3.084616 3.124423 3.188596 3.193783
## [8,] 3.159509 3.181523 3.218693 3.301469 3.696267 3.703596 3.716144 3.739260
## [9,] 3.157952 3.235178 3.280142 3.327577 3.334009 3.397645 3.432855 3.476889
## [10,] 4.050647 4.135238 4.296608 4.339992 4.536367 5.001013 5.120571 5.307969
## [11,] 2.551057 2.934124 3.246497 3.301250 3.467129 3.528934 3.537213 3.626039
## [12,] 3.122392 3.466023 3.758921 3.853933 3.890838 3.986126 4.018560 4.033475
## [13,] 3.703606 3.898671 3.901613 3.902774 3.989778 3.992463 4.087599 4.110172
## [14,] 4.262442 4.535107 4.842285 5.081027 5.094219 5.145122 5.243460 5.434153
## [15,] 2.786216 2.987076 3.062838 3.086236 3.089990 3.216017 3.216842 3.297911
## [16,] 3.732413 4.381576 4.465460 4.474524 4.581580 4.642551 4.674345 4.678632
## [17,] 4.176456 4.184986 4.366096 4.387111 4.555794 4.594899 4.616670 4.707321
## [18,] 2.977965 2.984140 3.091233 3.311228 3.320118 3.348014 3.355058 3.393778
## [19,] 3.802450 3.819593 3.912981 3.998455 4.008490 4.142636 4.205364 4.206469
## [20,] 3.628261 3.706452 3.792651 3.824039 3.953584 3.955012 3.959543 4.059143
## [,9] [,10]
## [1,] 4.464983 4.514860
## [2,] 3.578920 3.598481
## [3,] 5.082720 5.157007
## [4,] 3.206543 3.211902
## [5,] 4.022172 4.040735
## [6,] 3.917244 3.918454
## [7,] 3.221482 3.252045
## [8,] 3.970952 3.987337
## [9,] 3.486682 3.495449
## [10,] 5.445113 5.536845
## [11,] 3.673624 3.676229
## [12,] 4.083185 4.114034
## [13,] 4.120862 4.155166
## [14,] 5.498970 5.543954
## [15,] 3.399645 3.430603
## [16,] 4.679538 4.722383
## [17,] 4.770444 4.811990
## [18,] 3.400751 3.413641
## [19,] 4.223790 4.235914
## [20,] 4.118343 4.128355
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 0.966 0.924
## 2 0.989 0.930 0.962
## 3 0.989 0.962 0.870
## 4 0.989 0.996 0.870
## 5 0.989 0.969 0.962
## 6 0.989 0.965 0.962
## 7 1 0.992 0.891
## 8 0.989 0.992 0.870
## 9 1 0.962 0.958
## 10 0.989 0.965 0.924
## # ℹ 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.138 -0.170 0.651 -0.824
## 2 -0.248 0.710 0.475 0.105
## 3 -0.0477 -0.0514 -0.0339 -0.474
## 4 -0.0498 0.201 0.573 -0.355
## 5 -0.0648 -0.0166 -0.0818 -0.681
## 6 -0.0153 0.269 -0.0503 0.918
## 7 0.588 0.207 1.50 -0.411
## 8 -0.326 -0.0343 1.04 0.157
## 9 0.394 0.636 0.527 0.686
## 10 -0.165 -0.0208 0.0296 -0.129
## # ℹ 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.211 0.275 0.192 0.302 0.239 ...