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] 763 459 711 95 310 834 421 189 506 504 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 763 951 663 6 402 878 492 243 949 67
## [2,] 459 272 506 575 786 140 399 943 569 886
## [3,] 711 652 506 194 9 488 101 87 855 980
## [4,] 95 506 391 684 386 74 284 571 9 106
## [5,] 310 441 970 85 943 458 448 529 459 703
## [6,] 834 402 939 436 306 916 67 1 950 608
## [7,] 421 48 5 131 183 390 448 84 617 388
## [8,] 189 973 424 928 809 768 367 360 74 906
## [9,] 506 652 291 894 803 474 265 505 85 922
## [10,] 504 156 405 844 893 561 367 514 946 400
## [11,] 856 386 798 905 571 803 689 135 209 738
## [12,] 341 446 703 695 994 447 529 670 176 587
## [13,] 635 845 971 181 694 750 645 680 51 986
## [14,] 240 988 511 107 748 618 500 431 546 933
## [15,] 974 984 591 119 958 263 874 715 769 998
## [16,] 500 860 64 67 950 878 816 788 328 978
## [17,] 857 611 200 591 614 195 609 153 728 661
## [18,] 950 492 590 939 369 878 978 402 30 490
## [19,] 859 958 339 759 254 263 345 769 111 998
## [20,] 161 402 816 511 500 763 565 328 988 752
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.73 2.31 3.01 2.67 3.56 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.732467 3.943752 3.969009 4.332826 4.336712 4.393850 4.422141 4.514198
## [2,] 2.310442 2.740851 2.815045 2.828404 2.973589 2.983170 3.006233 3.039439
## [3,] 3.011302 3.101360 3.196670 3.284251 3.323518 3.405819 3.464398 3.587676
## [4,] 2.668782 2.959003 3.399978 3.450353 3.495232 3.506941 3.535020 3.536589
## [5,] 3.560172 3.569068 3.610449 3.661371 3.680563 3.763411 3.793470 3.817142
## [6,] 3.817037 4.018558 4.067644 4.130807 4.154497 4.220931 4.285856 4.332826
## [7,] 3.632760 3.845536 3.951215 3.991113 4.059086 4.157371 4.174669 4.235983
## [8,] 2.961004 3.127436 3.182050 3.186410 3.200009 3.212491 3.258179 3.258794
## [9,] 2.457702 2.733413 3.118784 3.193276 3.198298 3.218097 3.237597 3.248938
## [10,] 3.217416 3.351930 3.694704 3.756897 3.773349 3.800538 3.806209 3.820494
## [11,] 2.572952 2.594500 2.955330 3.228989 3.245368 3.247383 3.260248 3.331167
## [12,] 4.327196 4.371005 4.375177 4.464914 4.510921 4.552659 4.587073 4.597608
## [13,] 3.033480 3.747357 4.188512 4.317661 4.367062 4.459449 4.502357 4.845573
## [14,] 4.032526 4.420810 4.576001 4.816199 4.862952 4.996553 5.006532 5.098635
## [15,] 2.469631 2.722951 2.749710 2.874365 2.874492 2.963328 2.967493 2.989109
## [16,] 2.867231 3.077660 3.178829 3.390046 3.418372 3.518837 3.584649 3.644989
## [17,] 2.603127 2.961742 3.137996 3.154311 3.159903 3.368900 3.465246 3.560424
## [18,] 3.746514 4.233413 4.459076 4.489870 4.493819 4.517013 4.536105 4.671120
## [19,] 2.864099 3.357628 3.713032 3.830255 3.918866 3.964005 4.026742 4.102203
## [20,] 3.284390 3.849542 3.872867 4.078992 4.211292 4.352254 4.359839 4.386872
## [,9] [,10]
## [1,] 4.561329 4.610233
## [2,] 3.072468 3.080809
## [3,] 3.597270 3.616170
## [4,] 3.538380 3.539819
## [5,] 3.833874 3.836570
## [6,] 4.378235 4.425686
## [7,] 4.321731 4.342622
## [8,] 3.271165 3.280457
## [9,] 3.269940 3.270958
## [10,] 3.847745 3.859671
## [11,] 3.348188 3.393072
## [12,] 4.760918 4.802921
## [13,] 4.977026 5.003251
## [14,] 5.129706 5.150931
## [15,] 2.992920 3.013243
## [16,] 3.697669 3.710303
## [17,] 3.570024 3.602934
## [18,] 4.720122 4.720609
## [19,] 4.126963 4.134691
## [20,] 4.398015 4.409507
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 x 34
## `pCrkL(Lu175)Di.IL~ `pCREB(Yb176)Di.IL~ `pBTK(Yb171)Di.IL~ `pS6(Yb172)Di.IL7~
## <dbl> <dbl> <dbl> <dbl>
## 1 0.963 0.847 0.971 1
## 2 0.813 0.897 0.794 1
## 3 0.853 0.867 0.912 0.930
## 4 0.945 0.897 0.895 0.906
## 5 0.923 0.847 0.903 0.997
## 6 0.991 0.750 1 0.985
## 7 0.887 0.897 0.965 0.894
## 8 0.923 0.918 0.870 0.985
## 9 1 0.897 0.583 1
## 10 0.844 0.897 0.965 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>,
## # 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>,
## # pSrc(Nd144)Di.IL7.change <dbl>, Ki67(Sm152)Di.IL7.change <dbl>,
## # pErk12(Gd155)Di.IL7.change <dbl>, pSTAT3(Gd158)Di.IL7.change <dbl>,
## # pAKT(Tb159)Di.IL7.change <dbl>, pBLNK(Gd160)Di.IL7.change <dbl>,
## # pP38(Tm169)Di.IL7.change <dbl>, pSTAT5(Nd150)Di.IL7.change <dbl>,
## # pSyk(Dy162)Di.IL7.change <dbl>, tIkBa(Er166)Di.IL7.change <dbl>,
## # IL7.fraction.cond.2 <dbl>, density <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 x 51
## `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(I~ `CD3(Cd114)Di`
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 -0.0657 -0.243 -0.0264 -0.319 -0.187
## 2 0.332 -0.438 -0.294 -0.210 -0.329
## 3 0.00357 -0.0435 -0.777 -0.183 -0.448
## 4 -0.234 -0.0953 -0.451 -0.721 -0.393
## 5 -0.0695 -0.197 -0.213 0.774 -0.304
## 6 -0.446 -0.204 -0.0761 -0.996 -0.288
## 7 -0.0363 -0.184 -0.0884 0.733 -0.0411
## 8 -0.255 0.179 -0.241 -1.58 -0.159
## 9 -0.0645 -0.488 -0.289 -0.325 0.186
## 10 -0.0882 1.04 0.385 -0.361 -0.492
## # ... 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>,
## # CD40(Er170)Di <dbl>, CD33(Yb173)Di <dbl>, HLA-DR(Yb174)Di <dbl>,
## # Time <dbl>, Cell_length <dbl>, cPARP(La139)Di <dbl>, pPLCg2(Pr141)Di <dbl>,
## # pSrc(Nd144)Di <dbl>, pSTAT5(Nd150)Di <dbl>, Ki67(Sm152)Di <dbl>,
## # pErk12(Gd155)Di <dbl>, pSTAT3(Gd158)Di <dbl>, pAKT(Tb159)Di <dbl>,
## # pBLNK(Gd160)Di <dbl>, pSyk(Dy162)Di <dbl>, tIkBa(Er166)Di <dbl>,
## # pP38(Tm169)Di <dbl>, pBTK(Yb171)Di <dbl>, pS6(Yb172)Di <dbl>,
## # pCrkL(Lu175)Di <dbl>, pCREB(Yb176)Di <dbl>, DNA1(Ir191)Di <dbl>,
## # DNA2(Ir193)Di <dbl>, Viability1(Pt195)Di <dbl>, Viability2(Pt196)Di <dbl>,
## # wanderlust <dbl>, condition <chr>
# 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.213 0.318 0.272 0.28 0.253 ...