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] 335 283 316 63 411 494 686 95 862 422 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 335 561 113 86 757 687 8 798 345 686
## [2,] 283 323 799 892 370 570 460 427 759 318
## [3,] 316 753 648 599 706 760 847 494 555 635
## [4,] 63 988 924 446 546 461 507 206 254 725
## [5,] 411 735 949 352 126 269 865 420 830 810
## [6,] 494 183 763 374 343 875 716 515 861 414
## [7,] 686 113 880 870 711 609 684 605 580 353
## [8,] 95 259 614 519 683 889 501 907 714 375
## [9,] 862 180 208 772 238 543 132 580 476 64
## [10,] 422 193 841 111 898 224 508 759 67 77
## [11,] 391 805 256 400 733 117 29 262 412 868
## [12,] 825 144 881 67 224 377 384 422 980 54
## [13,] 36 286 663 855 551 235 604 35 134 127
## [14,] 173 503 531 51 61 537 858 265 641 802
## [15,] 518 956 596 625 519 375 779 889 173 591
## [16,] 905 287 888 378 605 880 164 133 580 30
## [17,] 979 999 316 351 762 129 132 114 829 462
## [18,] 398 574 135 809 873 539 461 250 651 546
## [19,] 169 133 312 627 532 291 394 887 770 976
## [20,] 931 888 472 248 862 231 133 933 760 114
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 4.52 3.42 3.41 2.8 2.33 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 4.518016 5.976489 6.336659 6.362762 6.381264 6.464346 6.506224 6.558309
## [2,] 3.422646 3.647824 3.733845 3.747977 4.009889 4.012787 4.170238 4.258487
## [3,] 3.408781 3.583994 3.698748 3.718052 3.739959 3.770411 3.835693 4.010090
## [4,] 2.803685 3.180609 3.336644 3.337072 3.575070 3.641495 3.749763 3.893021
## [5,] 2.326419 2.620280 2.731706 2.734650 2.776866 2.779366 2.977156 2.990896
## [6,] 3.482264 3.572148 3.582993 3.602643 3.623902 3.627003 3.633833 3.726887
## [7,] 4.386751 4.700618 5.111118 5.219471 5.517923 5.525519 5.564380 5.578774
## [8,] 4.778674 5.206551 5.392086 5.445631 5.461972 5.480027 5.566109 5.636048
## [9,] 3.754273 4.023260 4.188502 4.243499 4.598457 4.735384 4.761092 4.779272
## [10,] 3.466023 3.659268 3.853933 3.943375 3.955120 3.986126 4.013946 4.018560
## [11,] 3.531208 3.552976 3.591812 3.684393 3.686622 3.833688 3.884142 4.070449
## [12,] 3.831873 4.472985 4.587128 4.805073 4.881974 4.893539 4.900333 4.915724
## [13,] 2.317429 2.623359 3.258614 3.578623 3.589798 3.668870 3.749202 3.761407
## [14,] 3.736748 3.999497 4.026252 4.364343 4.392323 4.487633 4.643523 4.658434
## [15,] 4.089461 4.091291 4.095246 4.231604 4.256208 4.382126 4.465774 4.475564
## [16,] 3.026220 3.118686 3.164888 3.368979 3.408749 3.574098 3.633856 3.646836
## [17,] 3.647291 3.780412 3.784215 3.798833 3.800394 3.808936 3.837787 3.838603
## [18,] 3.440584 3.464503 3.467530 3.620408 3.860991 3.889541 3.914558 3.933328
## [19,] 3.154017 3.527210 3.707808 3.810819 3.846423 3.857591 3.907548 3.917244
## [20,] 3.252701 3.333899 3.507925 3.597923 3.632416 3.636694 3.679354 3.696364
## [,9] [,10]
## [1,] 6.590326 6.638444
## [2,] 4.444663 4.538928
## [3,] 4.021221 4.042208
## [4,] 4.079641 4.090389
## [5,] 3.069281 3.128062
## [6,] 3.760918 3.766559
## [7,] 5.594992 5.607769
## [8,] 5.668140 5.682244
## [9,] 4.831119 4.838182
## [10,] 4.033475 4.083185
## [11,] 4.082297 4.096203
## [12,] 4.999745 5.157341
## [13,] 3.942074 4.054187
## [14,] 4.740319 4.758214
## [15,] 4.512959 4.555739
## [16,] 3.659967 3.735694
## [17,] 3.882623 3.894779
## [18,] 4.009392 4.047670
## [19,] 3.979883 3.980366
## [20,] 3.730862 3.734410
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.qval…` `pCREB(Yb176)D…` `pBTK(Yb171)Di…` `pS6(Yb172)Di.…`
## <dbl> <dbl> <dbl> <dbl>
## 1 0.998 0.953 1 0.956
## 2 0.776 1 1 1
## 3 0.901 0.784 0.888 1
## 4 0.901 1 0.945 0.862
## 5 0.773 0.886 1 1
## 6 0.678 1.00 0.909 1
## 7 0.934 0.651 1 1
## 8 0.932 0.887 1 0.666
## 9 0.887 0.922 1 1
## 10 0.773 0.651 1 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>, …
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(…` `CD3(Cd114)Di`
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.419 -0.114 1.17 -0.824 0.398
## 2 0.122 -0.0340 0.123 -0.593 1.43
## 3 -0.0130 -0.0100 -0.412 -0.347 1.73
## 4 -0.102 0.551 0.358 -0.566 -0.230
## 5 -0.180 0.0896 0.521 -0.551 0.346
## 6 -0.0664 -0.184 -0.0623 -0.262 -0.124
## 7 -0.306 1.61 0.211 -2.20 1.18
## 8 -0.111 0.498 1.64 -0.768 -0.397
## 9 0.0602 0.854 0.0326 0.563 -0.182
## 10 -0.0323 -0.335 -0.476 -0.855 1.19
## # … with 20 more rows, and 46 more variables: `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>, …
# 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.151 0.224 0.247 0.245 0.314 ...