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] 228 824 576 240 33 232 767 299 642 40 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 228 904 356 884 616 710 945 858 56 287
## [2,] 824 940 316 98 811 986 129 82 516 372
## [3,] 576 625 407 222 711 959 680 64 906 913
## [4,] 240 370 794 655 129 859 411 535 605 29
## [5,] 33 887 404 997 102 882 966 282 401 719
## [6,] 232 836 566 645 548 292 865 453 481 356
## [7,] 767 238 457 690 235 692 984 399 419 902
## [8,] 299 335 62 803 796 277 110 847 521 324
## [9,] 642 29 87 760 889 264 42 370 352 872
## [10,] 40 368 790 412 429 302 602 967 205 330
## [11,] 991 982 50 482 172 102 255 518 432 241
## [12,] 379 224 136 878 15 941 67 754 681 664
## [13,] 412 1000 958 451 694 520 120 330 661 284
## [14,] 327 936 335 973 709 508 227 948 233 296
## [15,] 431 211 681 379 178 30 664 12 878 595
## [16,] 625 962 875 74 515 357 620 780 719 401
## [17,] 904 339 993 228 892 554 177 896 104 272
## [18,] 744 829 236 818 912 778 560 294 753 264
## [19,] 406 775 200 879 360 47 667 262 198 679
## [20,] 987 121 222 401 959 887 424 193 620 46
## num [1:1000, 1:30] 4.86 4.69 3.29 4.14 2.55 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 4.859045 5.031070 5.107900 5.243134 5.381309 5.505378 5.554128 5.556125
## [2,] 4.686174 4.693712 4.882209 4.888400 5.013395 5.046608 5.080887 5.084413
## [3,] 3.293351 3.334921 3.413921 3.503532 3.529605 3.544249 3.547146 3.548921
## [4,] 4.138642 4.337072 4.456780 4.458585 4.519103 4.659862 4.781641 4.972149
## [5,] 2.548095 2.721244 2.798292 2.936514 2.954664 2.985000 2.990961 3.004170
## [6,] 4.040213 4.555489 5.136586 5.163205 5.172362 5.293647 5.305812 5.335828
## [7,] 3.198300 3.264120 3.419665 3.476192 3.568235 3.802368 3.842844 3.886701
## [8,] 2.840489 3.429947 3.564146 3.594907 3.752011 3.761596 3.887862 3.905674
## [9,] 2.810334 3.183397 3.381325 3.702542 3.756188 3.765027 3.796539 3.883741
## [10,] 3.045342 3.208412 3.214327 3.270711 3.572627 3.643436 3.653801 3.702478
## [11,] 3.318731 3.374277 3.521089 3.567572 3.661332 3.712061 3.723023 3.778574
## [12,] 2.396558 3.105622 3.259011 3.304500 3.324790 3.369705 3.396080 3.459256
## [13,] 3.255525 3.427047 3.544296 3.740599 3.762113 3.838453 3.869013 3.896203
## [14,] 3.806788 3.953308 4.070798 4.210395 4.316811 4.559657 4.580091 4.624255
## [15,] 2.595939 2.603422 2.929799 3.021559 3.114917 3.124713 3.247262 3.324790
## [16,] 3.174615 3.281136 3.470278 3.473444 3.484612 3.514027 3.552353 3.582380
## [17,] 4.006025 4.116124 4.143006 4.217497 4.465602 4.571698 4.574700 4.663483
## [18,] 3.603593 3.648593 3.820388 3.828365 3.839198 3.850349 3.865013 3.866061
## [19,] 3.444666 3.683269 3.760028 3.807134 3.910739 3.917435 3.921483 4.119357
## [20,] 2.440778 2.509628 2.571217 2.739427 2.769781 2.870486 2.919770 2.921099
## [,9] [,10]
## [1,] 5.714301 5.732577
## [2,] 5.156047 5.268617
## [3,] 3.571564 3.629365
## [4,] 4.985732 4.995221
## [5,] 3.091327 3.096275
## [6,] 5.371879 5.406438
## [7,] 4.007513 4.009299
## [8,] 3.917413 3.929804
## [9,] 3.890838 3.929554
## [10,] 3.779568 3.782557
## [11,] 3.794338 3.818075
## [12,] 3.479397 3.527588
## [13,] 3.899381 3.905923
## [14,] 4.624524 4.703190
## [15,] 3.325128 3.369506
## [16,] 3.598714 3.661306
## [17,] 4.666080 4.895267
## [18,] 3.869837 3.912850
## [19,] 4.161181 4.161785
## [20,] 3.040246 3.117838
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.957 0.938 0.875
## 2 0.838 0.920 0.936
## 3 0.838 0.911 0.936
## 4 0.923 1 0.936
## 5 0.841 0.938 0.978
## 6 0.841 0.993 0.936
## 7 0.838 1 0.936
## 8 0.787 0.938 0.936
## 9 0.865 0.938 0.936
## 10 0.727 0.989 0.970
## # ℹ 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.410 -0.179 -0.453 -0.810
## 2 -0.402 -0.356 -0.397 -0.883
## 3 -0.258 -0.304 -0.192 -0.370
## 4 -0.139 -0.148 -0.652 -0.308
## 5 0.376 0.143 0.132 0.877
## 6 -0.151 -0.252 -0.123 -0.168
## 7 0.476 0.467 -0.477 -0.0572
## 8 -0.237 -0.320 -0.131 -0.249
## 9 -0.000955 -0.170 0.106 -0.148
## 10 -0.258 -0.677 -0.316 -0.442
## # ℹ 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.172 0.182 0.268 0.197 0.319 ...