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] 522 331 748 797 783 769 134 302 782 108 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 522 745 135 899 54 94 981 367 780 363
## [2,] 331 840 858 562 515 345 909 703 814 100
## [3,] 748 24 250 7 392 479 137 579 810 736
## [4,] 797 758 30 292 823 983 82 398 437 306
## [5,] 783 520 191 154 319 957 738 91 608 588
## [6,] 769 115 997 474 768 629 935 509 133 244
## [7,] 134 1000 736 568 510 954 902 748 294 859
## [8,] 302 114 389 743 772 660 904 17 549 245
## [9,] 782 151 488 135 161 823 191 319 552 610
## [10,] 108 495 124 729 469 260 721 357 887 511
## [11,] 471 357 343 201 632 53 241 574 315 866
## [12,] 8 706 904 983 506 531 285 426 302 383
## [13,] 692 604 18 571 713 588 617 766 577 976
## [14,] 173 529 799 949 902 519 859 514 162 736
## [15,] 342 945 588 26 730 230 526 863 380 991
## [16,] 958 190 227 657 485 424 768 115 560 256
## [17,] 228 992 588 772 487 732 8 526 762 156
## [18,] 588 342 577 732 660 980 487 668 566 766
## [19,] 741 966 674 371 513 874 749 541 215 370
## [20,] 892 61 393 783 801 172 422 835 607 628
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 4.06 3.13 3.94 3.42 3.29 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 4.057365 4.499547 4.615946 4.746571 4.764987 4.776526 4.826832 4.830917
## [2,] 3.132644 3.201004 3.574940 3.639797 3.831307 3.868862 3.922722 3.997139
## [3,] 3.938531 3.943092 3.960518 4.052555 4.075821 4.247613 4.351326 4.409553
## [4,] 3.418597 3.647291 3.719420 3.755778 3.764763 3.780412 3.784215 3.798833
## [5,] 3.294101 3.540613 3.558196 3.593978 3.652203 3.698915 3.737825 3.759327
## [6,] 3.238448 3.431571 3.518805 3.526893 3.582698 3.654889 3.707655 3.864263
## [7,] 3.072292 3.485924 3.489735 3.685677 3.721304 3.824062 3.828479 3.869371
## [8,] 2.469692 2.525892 2.541346 2.568863 2.609080 2.759327 2.781595 2.840275
## [9,] 3.062838 3.141044 3.195781 3.216842 3.297911 3.356542 3.399645 3.403091
## [10,] 3.605968 3.656766 3.807738 3.813461 3.905923 4.008878 4.041281 4.058931
## [11,] 3.045342 3.553603 3.592151 3.756674 3.780027 3.999451 4.020970 4.067377
## [12,] 3.475064 3.635163 3.728996 3.732867 3.775467 3.802628 3.817044 3.928596
## [13,] 3.616236 3.709744 3.837977 3.887278 3.927038 4.026565 4.088930 4.235365
## [14,] 3.236308 3.669423 3.931054 3.978212 4.056790 4.155755 4.395414 4.429955
## [15,] 2.622165 2.678530 2.800239 2.910490 2.919274 2.920141 3.068432 3.104714
## [16,] 2.491643 3.037649 3.230650 3.331466 3.359248 3.381674 3.392959 3.399999
## [17,] 2.166226 2.608916 2.742256 2.763797 2.811036 2.826947 2.840275 2.845754
## [18,] 2.117253 2.342175 2.522522 2.552946 2.592490 2.594668 2.654381 2.816208
## [19,] 2.516009 2.712644 2.734073 2.908386 3.024370 3.160874 3.166317 3.269707
## [20,] 3.301789 3.357944 3.415477 3.433735 3.433897 3.478375 3.483313 3.568558
## [,9] [,10]
## [1,] 4.851867 4.862725
## [2,] 4.004922 4.059065
## [3,] 4.472377 4.480609
## [4,] 3.837787 3.838603
## [5,] 3.767822 3.805221
## [6,] 3.865901 3.885352
## [7,] 3.929520 3.984988
## [8,] 2.854118 2.873922
## [9,] 3.423982 3.445764
## [10,] 4.059009 4.105608
## [11,] 4.124997 4.132393
## [12,] 3.971680 3.972110
## [13,] 4.259835 4.293666
## [14,] 4.523136 4.529453
## [15,] 3.109715 3.235715
## [16,] 3.434338 3.511985
## [17,] 2.874981 2.916131
## [18,] 2.858085 2.910121
## [19,] 3.286231 3.297299
## [20,] 3.601772 3.606580
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.973 0.981 0.942
## 2 0.973 0.515 0.523
## 3 1 0.848 0.354
## 4 0.569 0.924 0.143
## 5 1 0.836 0.503
## 6 1 0.902 0.445
## 7 0.973 0.822 0.917
## 8 1 0.802 0.424
## 9 0.973 0.822 0.464
## 10 1 0.761 0.772
## # ℹ 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.148 0.816 -0.0442 -1.36
## 2 -0.557 -0.0292 -0.621 0.469
## 3 -0.0494 -0.196 -0.0961 0.996
## 4 -0.179 -0.131 -0.159 -0.550
## 5 -0.105 -0.202 -0.202 -0.564
## 6 0.787 -0.239 0.0882 0.0555
## 7 0.237 -0.0447 -0.119 -0.717
## 8 -0.180 -0.255 -0.0147 -0.455
## 9 -0.121 -0.0918 -0.283 -1.11
## 10 -0.170 -0.0392 -0.270 -0.653
## # ℹ 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.199 0.241 0.215 0.253 0.258 ...