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] 629 332 357 159 974 329 799 125 832 502 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 629 732 541 450 836 47 554 721 595 495
## [2,] 332 977 33 728 136 922 682 631 302 12
## [3,] 357 909 620 546 652 394 179 941 868 554
## [4,] 159 285 598 761 833 949 118 472 813 662
## [5,] 974 489 572 159 750 42 370 23 466 137
## [6,] 329 234 548 156 428 96 907 311 753 62
## [7,] 799 326 1 554 676 432 689 105 47 705
## [8,] 125 462 687 398 45 114 703 391 847 442
## [9,] 832 894 265 559 358 836 377 40 810 930
## [10,] 502 799 934 72 588 859 667 957 94 233
## [11,] 193 908 728 709 610 762 514 939 756 437
## [12,] 302 33 426 977 965 332 922 631 90 710
## [13,] 481 338 376 623 739 301 219 819 725 333
## [14,] 358 695 667 504 689 460 94 832 231 431
## [15,] 63 203 724 424 834 520 231 172 450 360
## [16,] 947 950 616 993 615 546 564 810 991 377
## [17,] 843 29 902 616 501 543 274 638 789 794
## [18,] 752 446 78 552 249 937 378 454 653 461
## [19,] 592 829 744 569 572 791 870 563 363 335
## [20,] 603 977 90 91 332 631 728 922 765 302
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.88 3.04 3.78 3.32 3.64 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.876745 2.900682 2.930500 2.990510 3.022705 3.125242 3.262494 3.273347
## [2,] 3.037507 3.867076 4.045497 4.064664 4.278096 4.323230 4.378074 4.398775
## [3,] 3.781224 3.990122 4.104321 4.403991 4.409067 4.441968 4.448236 4.487402
## [4,] 3.318934 3.436878 3.446704 3.467231 3.676215 3.726654 3.761974 3.765061
## [5,] 3.643228 3.889033 3.946700 3.970646 4.082931 4.093826 4.096569 4.191302
## [6,] 3.655312 3.669831 3.953308 4.046790 4.070798 4.199250 4.210395 4.274600
## [7,] 3.197729 3.328729 3.343889 3.361507 3.387417 3.422021 3.442892 3.599091
## [8,] 4.012912 4.016016 4.076723 4.208430 4.293380 4.300774 4.305244 4.341295
## [9,] 3.657625 3.702917 3.866909 3.930930 4.031354 4.042978 4.066817 4.148666
## [10,] 2.564196 2.844470 2.859183 2.933224 2.956409 3.001413 3.072683 3.106239
## [11,] 4.048453 4.495704 4.802219 4.904162 4.960884 4.968252 4.986754 4.996791
## [12,] 2.317429 2.618296 2.921927 3.597499 3.692919 3.988143 4.197507 4.348134
## [13,] 3.777098 3.863646 3.943662 4.009312 4.047676 4.059920 4.088545 4.114642
## [14,] 2.622165 2.792039 2.800239 3.054258 3.068432 3.109715 3.231831 3.235715
## [15,] 2.780195 3.037645 3.231563 3.254905 3.270187 3.434825 3.450488 3.451687
## [16,] 3.431334 3.646368 3.735293 3.905646 3.906731 3.963165 4.014124 4.101648
## [17,] 4.767352 4.826039 4.880188 4.880832 4.988991 5.147611 5.163869 5.172702
## [18,] 2.667950 2.832905 3.069267 3.180293 3.569947 3.630642 3.630663 3.710021
## [19,] 3.907017 3.968248 3.981291 3.994690 4.235535 4.278129 4.286119 4.300815
## [20,] 4.342464 4.510140 4.565450 4.738522 4.741335 4.743826 4.903699 4.918202
## [,9] [,10]
## [1,] 3.304652 3.326851
## [2,] 4.607451 4.656521
## [3,] 4.522778 4.540390
## [4,] 3.774666 3.783045
## [5,] 4.193373 4.215385
## [6,] 4.316811 4.449122
## [7,] 3.612803 3.614242
## [8,] 4.418231 4.431978
## [9,] 4.179939 4.182902
## [10,] 3.202296 3.217574
## [11,] 5.014418 5.089879
## [12,] 4.366957 4.515314
## [13,] 4.125417 4.192520
## [14,] 3.254322 3.260591
## [15,] 3.483039 3.493728
## [16,] 4.106953 4.118588
## [17,] 5.216448 5.243691
## [18,] 3.756821 3.759761
## [19,] 4.308902 4.347400
## [20,] 4.938301 5.049838
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.998 0.985 0.993
## 2 0.869 0.850 0.969
## 3 0.893 1 0.969
## 4 0.993 0.997 0.969
## 5 0.933 0.850 0.949
## 6 0.893 0.948 0.963
## 7 0.993 0.778 0.995
## 8 0.916 0.998 0.877
## 9 0.998 0.997 0.969
## 10 0.998 0.983 0.903
## # ℹ 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.00853 -0.190 -0.0477 0.212
## 2 -0.194 -0.475 -0.0613 -0.137
## 3 -0.0231 0.389 -0.151 -0.528
## 4 -0.0606 0.982 -0.493 -0.0102
## 5 -0.174 -0.292 -0.112 0.635
## 6 -1.15 -0.601 -0.117 -0.146
## 7 -0.492 -0.495 -0.352 -0.456
## 8 -1.00 -0.700 -0.236 -1.37
## 9 -0.287 -0.0939 -0.108 -1.65
## 10 -0.587 -0.161 -0.0899 -0.386
## # ℹ 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.298 0.207 0.215 0.26 0.232 ...