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] 311 669 564 685 920 139 976 319 805 192 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 311 846 515 196 163 571 182 169 139 138
## [2,] 669 840 63 321 81 859 114 50 410 925
## [3,] 564 357 1000 964 438 239 662 456 649 836
## [4,] 685 575 68 294 977 954 800 366 614 469
## [5,] 920 458 844 501 197 811 125 268 159 20
## [6,] 139 196 571 90 747 970 809 1 876 301
## [7,] 976 252 385 322 537 265 840 323 270 410
## [8,] 319 189 416 264 88 513 63 825 944 488
## [9,] 805 312 636 886 402 17 931 776 437 606
## [10,] 192 179 197 419 976 766 339 546 44 669
## [11,] 622 602 934 787 439 734 143 187 870 133
## [12,] 282 975 277 265 533 593 384 644 560 246
## [13,] 677 464 974 489 951 67 908 201 320 970
## [14,] 142 276 443 264 815 637 439 585 326 746
## [15,] 930 820 554 577 76 529 200 452 905 168
## [16,] 934 959 791 874 143 356 584 547 269 260
## [17,] 402 965 805 9 404 568 752 310 804 541
## [18,] 998 603 783 925 770 317 181 360 753 374
## [19,] 519 484 227 113 844 246 842 419 252 728
## [20,] 728 519 844 449 125 421 599 298 303 410
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.17 2.61 5.29 3.65 3.17 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.171068 3.481178 3.600803 3.817606 3.921847 3.947861 3.960518 4.009307
## [2,] 2.614026 2.804278 3.086236 3.138471 3.197036 3.274039 3.438990 3.492433
## [3,] 5.288472 5.367204 6.070138 6.219095 6.235203 6.346594 6.353196 6.417676
## [4,] 3.654436 3.799726 3.855296 4.044009 4.097500 4.127788 4.154500 4.163842
## [5,] 3.172684 3.211234 3.333152 3.351309 3.481355 3.615715 3.643893 3.675832
## [6,] 4.088346 4.125997 4.186742 4.189745 4.224849 4.270545 4.344830 4.352267
## [7,] 3.285076 3.380446 3.427433 3.555003 3.608696 3.615235 3.722028 3.757769
## [8,] 3.950426 3.982444 3.983523 4.271339 4.306284 4.319607 4.352936 4.358861
## [9,] 3.029227 3.067163 3.284825 3.308825 3.321665 3.378611 3.514371 3.548801
## [10,] 3.607652 3.618035 3.666589 3.672020 3.809696 3.970047 4.213986 4.283156
## [11,] 2.407407 2.815737 2.827644 2.844380 2.919770 2.949460 3.015529 3.017756
## [12,] 4.579636 4.691063 4.721317 4.751897 4.814718 4.837764 4.871265 4.883767
## [13,] 4.699835 5.174059 5.298123 5.399836 5.488858 5.559348 5.650207 5.677497
## [14,] 3.356851 3.359119 3.494441 3.786363 3.876545 4.081137 4.158189 4.189339
## [15,] 4.065915 4.497995 4.535445 4.667527 4.684504 4.724088 4.912392 4.980528
## [16,] 4.241486 4.397172 4.398451 4.412831 4.415038 4.419607 4.506987 4.588290
## [17,] 3.056717 3.066980 3.145096 3.378611 3.543692 3.579748 3.679669 3.708573
## [18,] 2.907041 3.102249 3.157273 3.246241 3.253448 3.269542 3.311457 3.327819
## [19,] 3.017974 3.268297 3.320573 3.381191 3.481008 3.627836 3.677416 3.701739
## [20,] 3.036677 3.241655 3.253749 3.273673 3.316434 3.324695 3.335381 3.563293
## [,9] [,10]
## [1,] 4.019552 4.033075
## [2,] 3.496222 3.507000
## [3,] 6.489301 6.611625
## [4,] 4.192262 4.214410
## [5,] 3.695514 3.719047
## [6,] 4.403568 4.440960
## [7,] 3.770940 3.786052
## [8,] 4.391597 4.401570
## [9,] 3.585331 3.608424
## [10,] 4.288488 4.352584
## [11,] 3.068766 3.071325
## [12,] 4.920338 4.940955
## [13,] 5.684162 5.699640
## [14,] 4.216063 4.236636
## [15,] 4.990545 5.145195
## [16,] 4.669358 4.690217
## [17,] 3.709851 3.711832
## [18,] 3.346413 3.357396
## [19,] 3.745634 3.759993
## [20,] 3.564146 3.665252
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…¹ pCREB…² pBTK(…³ pS6(Y…⁴ cPARP…⁵ pPLCg…⁶ pSrc(…⁷ Ki67(…⁸ pErk1…⁹
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.996 1 0.963 0.976 1 0.849 0.931 0.992 1
## 2 0.994 1 0.878 0.995 0.938 0.849 0.993 0.992 1
## 3 0.943 1 0.878 1 0.932 1 0.943 0.992 1
## 4 0.996 1 0.878 1 1 0.797 0.931 0.992 1
## 5 0.982 1 0.963 0.976 0.932 0.976 0.925 0.992 0.998
## 6 1 1 0.949 0.996 1 0.797 0.946 0.992 1
## 7 0.939 1 0.878 0.976 0.989 0.794 0.981 0.992 0.977
## 8 0.982 1 0.878 1 1 0.895 0.944 0.894 0.977
## 9 0.982 1 0.963 0.714 0.989 0.960 0.931 0.997 0.977
## 10 0.943 1 0.975 0.976 0.984 0.834 0.992 0.920 0.977
## # … with 990 more rows, 25 more variables: `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>, …
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)…¹ CD3(C…² CD3(C…³ CD235…⁴ CD3(C…⁵ CD45(…⁶ CD19(…⁷ CD22(…⁸ IgD(Nd…⁹
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 -0.157 -0.0810 -0.163 -0.0833 0.511 2.87 0.301 1.15 -0.0625
## 2 0.00900 -0.242 -0.198 -0.0516 0.0926 2.50 2.33 -0.0390 0.486
## 3 -0.224 -0.501 -0.133 -0.477 -0.678 3.49 2.08 1.02 -0.138
## 4 -0.208 -0.0944 -0.163 -0.440 -0.0278 1.83 1.26 -0.250 -0.00915
## 5 -0.402 -0.356 -0.397 -0.883 -0.497 3.13 2.61 1.11 -0.103
## 6 -0.231 -0.449 -0.894 0.285 -0.401 3.14 2.03 1.79 -0.0556
## 7 -0.398 -0.252 -0.830 -0.816 -0.492 3.06 1.97 1.36 -0.369
## 8 -0.255 -0.0930 -0.0922 0.367 -0.0480 2.39 0.337 0.254 0.860
## 9 -0.144 -0.0808 -0.103 -1.13 -0.181 2.49 2.42 0.273 -0.336
## 10 -0.218 -0.0910 -0.198 0.145 -0.417 3.08 2.56 1.07 -0.0836
## # … with 20 more rows, 42 more variables: `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>, …
# 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.242 0.285 0.152 0.231 0.262 ...