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"
## [3] "CD3(Cd112)Di" "CD235-61-7-15(In113)Di"
## [5] "CD3(Cd114)Di" "CD45(In115)Di"
## [7] "CD19(Nd142)Di" "CD22(Nd143)Di"
## [9] "IgD(Nd145)Di" "CD79b(Nd146)Di"
## [11] "CD20(Sm147)Di" "CD34(Nd148)Di"
## [13] "CD179a(Sm149)Di" "CD72(Eu151)Di"
## [15] "IgM(Eu153)Di" "Kappa(Sm154)Di"
## [17] "CD10(Gd156)Di" "Lambda(Gd157)Di"
## [19] "CD24(Dy161)Di" "TdT(Dy163)Di"
## [21] "Rag1(Dy164)Di" "PreBCR(Ho165)Di"
## [23] "CD43(Er167)Di" "CD38(Er168)Di"
## [25] "CD40(Er170)Di" "CD33(Yb173)Di"
## [27] "HLA-DR(Yb174)Di"
##
## $functional
## [1] "pCrkL(Lu175)Di" "pCREB(Yb176)Di" "pBTK(Yb171)Di"
## [4] "pS6(Yb172)Di" "cPARP(La139)Di" "pPLCg2(Pr141)Di"
## [7] "pSrc(Nd144)Di" "Ki67(Sm152)Di" "pErk12(Gd155)Di"
## [10] "pSTAT3(Gd158)Di" "pAKT(Tb159)Di" "pBLNK(Gd160)Di"
## [13] "pP38(Tm169)Di" "pSTAT5(Nd150)Di" "pSyk(Dy162)Di"
## [16] "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] 480 787 974 187 13 549 635 756 320 306 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 480 482 934 60 433 441 259 902 40 493
## [2,] 787 257 634 816 702 839 815 143 644 970
## [3,] 974 335 884 757 136 836 730 965 455 856
## [4,] 187 404 984 457 653 387 212 982 945 934
## [5,] 13 519 995 442 794 959 940 455 32 536
## [6,] 549 994 957 522 986 583 30 102 120 647
## [7,] 635 766 143 755 786 815 617 859 682 686
## [8,] 756 367 403 463 823 405 320 874 239 315
## [9,] 320 541 614 265 422 252 10 908 306 810
## [10,] 306 723 951 614 501 450 172 662 568 589
## [11,] 326 897 453 485 833 949 880 412 869 725
## [12,] 470 712 907 391 870 300 397 715 781 653
## [13,] 630 442 680 134 794 5 237 700 251 396
## [14,] 884 586 294 960 935 217 501 841 679 974
## [15,] 965 225 773 101 53 935 841 400 264 761
## [16,] 534 667 529 450 172 884 838 673 968 329
## [17,] 702 189 644 638 650 2 634 920 86 787
## [18,] 884 782 355 225 63 455 408 662 821 856
## [19,] 701 714 503 186 83 518 473 421 161 308
## [20,] 387 128 592 187 607 654 462 300 907 29
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.58 3.33 2.59 2.55 3.29 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 3.579092 3.637451 3.748994 3.883009 4.027999 4.048328 4.090647
## [2,] 3.327672 3.339597 3.556047 3.600312 3.662678 3.692267 3.712295
## [3,] 2.587125 2.637243 2.668626 2.702495 2.703718 2.793637 2.952970
## [4,] 2.551225 2.955893 2.961839 2.968610 3.108063 3.118125 3.157874
## [5,] 3.289788 3.473572 3.473815 3.561651 3.680828 3.718976 3.785921
## [6,] 3.049277 3.504955 3.537386 3.705962 3.779936 3.789844 3.790647
## [7,] 3.862025 4.074709 5.265335 5.364972 5.434077 5.499014 5.531422
## [8,] 4.092943 4.184986 4.192058 4.252569 4.338093 4.395456 4.453105
## [9,] 2.998301 3.042024 3.120326 3.206119 3.270726 3.315812 3.326392
## [10,] 2.556705 2.649796 2.794155 2.986650 3.030603 3.043650 3.080150
## [11,] 3.431571 3.518805 3.526893 3.617614 3.654889 3.714632 3.755645
## [12,] 2.994119 3.522647 3.782354 3.811089 3.880291 3.922244 3.971995
## [13,] 2.999932 3.006798 3.044497 3.169950 3.246766 3.289788 3.454380
## [14,] 2.716895 3.053206 3.085479 3.150775 3.188968 3.252136 3.254383
## [15,] 3.586537 3.729572 3.900928 3.931624 3.959967 4.012199 4.035777
## [16,] 3.219148 3.355825 3.398960 3.540684 3.555666 3.638664 3.657513
## [17,] 3.747788 3.906058 3.960628 3.980267 4.026421 4.102520 4.134095
## [18,] 3.418597 3.647291 3.755778 3.798833 3.837787 3.838603 3.880798
## [19,] 3.211137 3.266432 3.639487 3.668870 3.928391 4.199958 4.593757
## [20,] 3.606321 3.712502 3.967855 4.010675 4.212640 4.273506 4.276874
## [,8] [,9] [,10]
## [1,] 4.190563 4.206754 4.251649
## [2,] 3.793516 3.832197 3.938747
## [3,] 2.970473 2.983170 3.015582
## [4,] 3.191967 3.196639 3.200104
## [5,] 3.885938 3.934462 3.971722
## [6,] 3.860449 3.977665 4.006072
## [7,] 5.554769 5.599368 5.655597
## [8,] 4.694595 4.699085 4.707577
## [9,] 3.330879 3.334825 3.362373
## [10,] 3.083338 3.091964 3.125666
## [11,] 3.885352 3.886642 3.899068
## [12,] 3.986756 4.010023 4.028750
## [13,] 3.476104 3.544441 3.585621
## [14,] 3.284016 3.392694 3.397599
## [15,] 4.041363 4.064772 4.127169
## [16,] 3.679266 3.693353 3.698848
## [17,] 4.162057 4.200801 4.249254
## [18,] 3.908863 3.911043 3.923105
## [19,] 4.611769 4.696108 4.828115
## [20,] 4.277263 4.317648 4.338199
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 x 34
## `pCrkL(Lu175)Di… `pCREB(Yb176)Di… `pBTK(Yb171)Di.… `pS6(Yb172)Di.I…
## <dbl> <dbl> <dbl> <dbl>
## 1 0.967 1 1 1
## 2 0.773 1 0.943 1
## 3 0.765 1 0.943 1
## 4 0.902 1 0.973 1
## 5 0.940 1 0.943 1
## 6 0.583 1 0.943 1
## 7 0.970 1 0.943 1
## 8 0.989 1 0.973 1
## 9 0.855 1 0.943 1
## 10 0.989 1 0.960 1
## # ... with 990 more rows, and 30 more variables:
## # `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>, `pAKT(Tb159)Di.IL7.qvalue` <dbl>,
## # `pBLNK(Gd160)Di.IL7.qvalue` <dbl>, `pP38(Tm169)Di.IL7.qvalue` <dbl>,
## # `pSTAT5(Nd150)Di.IL7.qvalue` <dbl>, `pSyk(Dy162)Di.IL7.qvalue` <dbl>,
## # `tIkBa(Er166)Di.IL7.qvalue` <dbl>, `pCrkL(Lu175)Di.IL7.change` <dbl>,
## # `pCREB(Yb176)Di.IL7.change` <dbl>, `pBTK(Yb171)Di.IL7.change` <dbl>,
## # `pS6(Yb172)Di.IL7.change` <dbl>, `cPARP(La139)Di.IL7.change` <dbl>,
## # `pPLCg2(Pr141)Di.IL7.change` <dbl>, `pSrc(Nd144)Di.IL7.change` <dbl>,
## # `Ki67(Sm152)Di.IL7.change` <dbl>, `pErk12(Gd155)Di.IL7.change` <dbl>,
## # `pSTAT3(Gd158)Di.IL7.change` <dbl>, `pAKT(Tb159)Di.IL7.change` <dbl>,
## # `pBLNK(Gd160)Di.IL7.change` <dbl>, `pP38(Tm169)Di.IL7.change` <dbl>,
## # `pSTAT5(Nd150)Di.IL7.change` <dbl>, `pSyk(Dy162)Di.IL7.change` <dbl>,
## # `tIkBa(Er166)Di.IL7.change` <dbl>, IL7.fraction.cond.2 <dbl>,
## # density <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 x 51
## `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(…
## <dbl> <dbl> <dbl> <dbl>
## 1 -0.0113 -0.0986 -0.181 0.514
## 2 -0.455 -0.0338 -0.0623 -0.400
## 3 -0.180 0.697 0.571 -0.287
## 4 -0.173 -0.0759 1.28 -0.0842
## 5 0.138 -0.363 -0.707 -0.649
## 6 -0.181 -0.166 -0.0167 -0.848
## 7 -0.138 0.0267 -0.215 0.756
## 8 -0.228 0.610 -0.115 -0.396
## 9 -0.132 0.408 0.925 -0.124
## 10 -0.761 -0.724 -0.677 -1.57
## # ... with 20 more rows, and 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>, `PreBCR(Ho165)Di` <dbl>, `CD43(Er167)Di` <dbl>,
## # `CD38(Er168)Di` <dbl>, `CD40(Er170)Di` <dbl>, `CD33(Yb173)Di` <dbl>,
## # `HLA-DR(Yb174)Di` <dbl>, Time <dbl>, Cell_length <dbl>,
## # `cPARP(La139)Di` <dbl>, `pPLCg2(Pr141)Di` <dbl>,
## # `pSrc(Nd144)Di` <dbl>, `pSTAT5(Nd150)Di` <dbl>, `Ki67(Sm152)Di` <dbl>,
## # `pErk12(Gd155)Di` <dbl>, `pSTAT3(Gd158)Di` <dbl>,
## # `pAKT(Tb159)Di` <dbl>, `pBLNK(Gd160)Di` <dbl>, `pSyk(Dy162)Di` <dbl>,
## # `tIkBa(Er166)Di` <dbl>, `pP38(Tm169)Di` <dbl>, `pBTK(Yb171)Di` <dbl>,
## # `pS6(Yb172)Di` <dbl>, `pCrkL(Lu175)Di` <dbl>, `pCREB(Yb176)Di` <dbl>,
## # `DNA1(Ir191)Di` <dbl>, `DNA2(Ir193)Di` <dbl>,
## # `Viability1(Pt195)Di` <dbl>, `Viability2(Pt196)Di` <dbl>,
## # wanderlust <dbl>, condition <chr>
# 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.232 0.243 0.33 0.302 0.248 ...