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] 417 800 782 384 975 373 12 712 60 483 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 417 61 938 237 750 848 420 653 460 783
## [2,] 800 807 342 864 638 946 182 20 906 813
## [3,] 782 934 444 731 633 312 699 772 82 243
## [4,] 384 197 293 57 926 600 243 652 594 360
## [5,] 975 277 625 638 286 906 534 118 800 255
## [6,] 373 411 462 659 370 965 725 944 857 44
## [7,] 12 323 829 161 832 523 402 99 160 312
## [8,] 712 978 103 921 641 138 955 826 791 102
## [9,] 60 249 260 764 262 239 211 981 259 141
## [10,] 483 469 695 320 108 687 403 913 227 787
## [11,] 440 785 761 845 163 783 542 149 460 729
## [12,] 160 914 323 7 832 732 349 834 358 694
## [13,] 832 473 216 829 160 697 323 613 349 452
## [14,] 693 936 63 381 666 459 590 873 916 691
## [15,] 516 832 991 518 208 829 379 870 636 410
## [16,] 624 351 680 108 319 636 222 673 928 278
## [17,] 209 859 550 348 110 660 755 423 777 529
## [18,] 793 617 684 945 880 544 215 867 59 673
## [19,] 420 750 511 210 185 800 988 421 783 848
## [20,] 185 638 674 606 21 761 534 800 107 533
## num [1:1000, 1:30] 4.98 3.83 3.29 4.37 3.78 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 4.977152 5.050559 5.063487 5.067586 5.084327 5.195131 5.301940 5.337030
## [2,] 3.827327 4.050866 4.076537 4.104316 4.112280 4.143719 4.242769 4.246076
## [3,] 3.285076 3.341014 3.427433 3.439879 3.578891 3.615235 3.722028 3.734385
## [4,] 4.368631 4.379309 4.480689 4.505853 4.526154 4.555257 4.613031 4.680920
## [5,] 3.783014 3.884775 4.026843 4.028409 4.052049 4.073326 4.266084 4.394186
## [6,] 2.213633 2.564574 2.696517 2.840044 2.873438 2.878373 2.878891 3.039091
## [7,] 2.690266 3.055655 3.085945 3.165143 3.180471 3.244668 3.265306 3.267101
## [8,] 3.672674 4.317249 4.482967 4.529364 4.532894 4.691997 4.718148 4.753953
## [9,] 2.754043 2.776866 2.961980 2.977156 2.990896 3.027949 3.112520 3.127787
## [10,] 2.399169 2.945146 3.165788 3.324877 3.327581 3.385618 3.470366 3.513273
## [11,] 4.032526 4.315727 4.420810 4.576001 4.816199 4.862952 4.996553 5.243080
## [12,] 2.310442 2.444981 2.637243 2.690266 2.735473 2.753542 2.948125 2.950425
## [13,] 3.243329 3.327155 3.334728 3.339836 3.352330 3.382202 3.388326 3.407887
## [14,] 2.736548 3.054867 3.453381 3.664290 3.780762 3.888269 3.938399 3.994460
## [15,] 2.537627 2.778345 2.788882 2.997739 3.079643 3.142601 3.287488 3.370361
## [16,] 3.219148 3.340385 3.398960 3.457596 3.540684 3.547917 3.555666 3.578018
## [17,] 3.514509 3.596605 3.634253 3.877377 3.900658 3.962708 3.963664 3.986597
## [18,] 3.077767 3.307896 3.311078 3.364964 3.410557 3.455188 3.460163 3.512682
## [19,] 5.067362 5.100859 5.105814 5.393335 5.465106 5.535884 5.543664 5.576934
## [20,] 3.197880 3.221092 3.337421 3.538971 3.637605 3.801203 3.825997 3.837001
## [,9] [,10]
## [1,] 5.338884 5.368402
## [2,] 4.287425 4.287726
## [3,] 3.757769 3.770940
## [4,] 4.689403 4.728122
## [5,] 4.396115 4.404835
## [6,] 3.110122 3.153086
## [7,] 3.271496 3.273412
## [8,] 4.809174 4.809390
## [9,] 3.158027 3.202296
## [10,] 3.536828 3.561370
## [11,] 5.290724 5.319414
## [12,] 3.057087 3.059582
## [13,] 3.408973 3.417671
## [14,] 4.169192 4.197479
## [15,] 3.398351 3.402839
## [16,] 3.596309 3.626682
## [17,] 4.059318 4.118347
## [18,] 3.520587 3.535555
## [19,] 5.650580 5.725731
## [20,] 3.967697 3.983692
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.836 1 0.956
## 2 0.952 1 0.604
## 3 0.952 1 0.256
## 4 0.952 1 0.932
## 5 0.905 1 0.196
## 6 0.962 1 0.941
## 7 0.944 1 0.197
## 8 0.833 1 0.765
## 9 0.833 1 0.927
## 10 0.944 1 0.848
## # ℹ 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.250 0.195 -0.0294 -0.845
## 2 0.396 -0.107 -0.0873 -0.144
## 3 0.135 -0.0568 0.200 0.214
## 4 -0.259 0.765 -0.193 -0.342
## 5 -0.219 -0.126 0.0327 0.456
## 6 -0.257 -0.215 0.205 -0.996
## 7 -0.349 0.0137 -0.999 0.240
## 8 -0.0527 -0.0208 0.460 0.588
## 9 -0.420 -0.408 -0.514 -0.637
## 10 -0.823 -1.07 -1.29 -0.0692
## # ℹ 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.175 0.228 0.255 0.209 0.219 ...