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] 200 242 276 151 233 979 320 542 161 264 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 200 931 885 627 608 761 821 476 430 462
## [2,] 242 659 424 233 332 845 276 210 402 451
## [3,] 276 496 163 897 571 392 997 364 242 54
## [4,] 151 650 384 719 505 449 799 495 396 374
## [5,] 233 451 924 166 592 57 790 706 115 889
## [6,] 979 227 175 445 931 973 910 761 90 165
## [7,] 320 738 637 543 541 683 406 297 558 360
## [8,] 542 310 872 516 706 723 834 197 571 969
## [9,] 161 733 718 310 610 969 872 104 527 348
## [10,] 264 779 987 581 634 649 982 815 191 601
## [11,] 437 143 477 734 700 740 78 762 980 263
## [12,] 799 437 384 417 980 78 477 762 719 272
## [13,] 148 549 327 732 710 26 423 860 159 17
## [14,] 746 951 106 657 756 931 175 910 885 23
## [15,] 274 261 36 335 202 969 527 39 733 551
## [16,] 223 884 483 781 841 546 735 73 965 112
## [17,] 148 386 13 698 752 889 395 808 816 250
## [18,] 319 750 816 741 86 866 271 831 967 643
## [19,] 268 640 761 885 748 608 418 103 275 318
## [20,] 106 531 191 409 393 292 634 581 92 69
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 4.03 3.93 3.65 3.24 3.87 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 4.032526 4.574095 4.576001 4.632136 4.862952 4.906549 4.996553 5.006532
## [2,] 3.931624 3.957039 3.959967 3.992877 4.018576 4.035777 4.041363 4.058339
## [3,] 3.649257 3.664541 3.786412 3.790938 3.820514 3.822202 3.822912 3.829362
## [4,] 3.239590 3.336475 3.491167 3.544177 3.658615 3.725396 3.754457 3.849651
## [5,] 3.871079 3.947773 4.211203 4.452781 4.460094 4.535187 4.554976 4.568434
## [6,] 4.520879 4.658434 4.908853 5.006399 5.102403 5.275184 5.307600 5.404872
## [7,] 4.209103 4.346599 4.352202 4.442967 4.840011 4.848014 4.889187 4.916895
## [8,] 3.734699 3.955643 3.976458 4.030236 4.046038 4.092036 4.125926 4.175864
## [9,] 2.760765 2.793947 2.959107 2.964287 2.981408 2.982636 3.056066 3.056611
## [10,] 2.742813 3.258614 3.339310 3.578623 3.589798 3.680584 3.727517 3.749202
## [11,] 3.667559 3.789129 3.863646 3.877260 4.009312 4.047676 4.088545 4.125417
## [12,] 3.170699 3.591812 3.833688 3.884142 3.891591 4.037333 4.070449 4.096203
## [13,] 2.172608 2.539375 2.733156 2.756374 2.992774 3.077080 3.126652 3.184171
## [14,] 4.331377 4.495704 4.868063 4.904162 4.960884 4.986754 5.010924 5.014418
## [15,] 2.505417 2.576869 2.746293 2.895432 3.024012 3.042425 3.109751 3.138187
## [16,] 2.803685 3.336644 3.337072 3.370530 3.575070 3.738081 3.749763 3.763579
## [17,] 2.686813 3.229940 3.268903 3.327614 3.350932 3.571644 3.579120 3.639552
## [18,] 3.145082 3.193171 3.198276 3.346875 3.374543 3.431275 3.452709 3.459705
## [19,] 4.506636 4.709822 4.894669 5.477977 5.607854 5.638222 5.672028 5.703252
## [20,] 3.403529 4.105932 4.258111 4.278096 4.318640 4.664271 4.954599 4.972463
## [,9] [,10]
## [1,] 5.059808 5.098635
## [2,] 4.064772 4.118433
## [3,] 3.875307 3.879340
## [4,] 3.870417 3.955225
## [5,] 4.571980 4.587900
## [6,] 5.416086 5.459774
## [7,] 4.970772 5.004522
## [8,] 4.223991 4.242659
## [9,] 3.125860 3.242820
## [10,] 3.761407 3.964953
## [11,] 4.230339 4.248899
## [12,] 4.111805 4.282334
## [13,] 3.203300 3.268903
## [14,] 5.065200 5.089879
## [15,] 3.180088 3.213223
## [16,] 3.940382 3.942419
## [17,] 3.653892 3.680346
## [18,] 3.546433 3.573318
## [19,] 5.714204 5.739920
## [20,] 4.975213 5.034340
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.958 0.903 0.997
## 2 1 1 0.997
## 3 0.989 0.965 0.997
## 4 0.989 0.903 1
## 5 0.812 0.971 0.997
## 6 0.700 0.964 0.903
## 7 0.601 0.965 0.997
## 8 0.916 0.971 0.978
## 9 1 0.984 0.990
## 10 0.906 1 0.997
## # ℹ 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.643 -0.195 -0.297 0.284
## 2 0.0465 -0.0188 -0.146 -1.15
## 3 -0.00931 -0.0592 -0.430 0.207
## 4 -0.0728 -0.253 -0.426 -0.429
## 5 -0.0995 0.198 0.429 -1.23
## 6 -0.386 -0.0937 -0.443 0.854
## 7 -0.0716 -0.0682 0.0256 0.402
## 8 -0.663 -0.394 -0.675 -1.18
## 9 -0.279 -0.187 -0.0181 -0.117
## 10 0.197 -0.177 -0.0855 -0.343
## # ℹ 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.193 0.237 0.252 0.247 0.214 ...