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] 132 197 785 405 497 976 32 872 559 644 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 132 212 427 331 521 215 682 690 13 28
## [2,] 197 573 102 712 552 731 103 541 754 986
## [3,] 785 470 619 877 84 847 623 823 906 173
## [4,] 405 199 57 312 372 798 547 745 874 671
## [5,] 497 896 310 910 584 956 90 96 519 165
## [6,] 976 46 608 246 839 501 120 797 644 114
## [7,] 32 227 59 620 13 430 434 776 88 792
## [8,] 872 529 360 695 786 509 34 931 742 911
## [9,] 559 197 221 108 636 350 208 37 573 976
## [10,] 644 717 839 687 88 56 305 26 277 430
## [11,] 891 620 584 993 992 667 966 148 430 224
## [12,] 548 985 854 300 967 454 4 518 969 457
## [13,] 960 656 427 59 584 190 891 869 323 7
## [14,] 695 192 786 352 911 677 872 544 611 186
## [15,] 28 429 581 79 335 510 404 41 717 121
## [16,] 581 776 476 32 323 6 717 13 390 644
## [17,] 724 493 428 287 640 411 808 790 200 964
## [18,] 870 731 250 488 712 232 754 972 492 375
## [19,] 85 264 921 392 731 438 746 66 64 773
## [20,] 938 766 600 42 925 881 973 835 442 147
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.28 1.98 2.47 3.53 4.59 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.279469 3.486064 3.496170 3.516958 3.559220 3.565464 3.628879 3.649255
## [2,] 1.977624 2.117253 2.342175 2.552946 2.564315 2.594668 2.673904 2.730371
## [3,] 2.469631 2.551225 2.670193 2.749710 2.789880 2.842039 2.874492 2.967493
## [4,] 3.534774 4.093714 4.171182 4.277316 4.314909 4.436591 4.501409 4.554011
## [5,] 4.590500 5.086023 5.844662 5.872313 6.113705 6.157258 6.158341 6.164762
## [6,] 3.030462 3.348207 3.368165 3.379533 3.390118 3.405629 3.413683 3.472031
## [7,] 3.063779 3.224171 3.229940 3.535084 3.620843 3.706292 3.904025 3.966544
## [8,] 3.096696 3.904964 3.924678 4.031992 4.057378 4.165454 4.186194 4.352212
## [9,] 2.831978 2.919401 2.951220 2.951836 3.004728 3.072123 3.088001 3.161285
## [10,] 3.779462 4.025404 4.057977 4.089496 4.195918 4.234549 4.256358 4.318616
## [11,] 3.831822 3.846971 3.974534 4.465981 4.741421 4.759512 4.787987 4.839777
## [12,] 4.103218 4.115781 4.401853 4.521688 4.595482 4.619212 4.783136 4.785965
## [13,] 3.274362 3.367727 3.383435 3.449180 3.464612 3.507152 3.553833 3.590915
## [14,] 4.243879 4.253292 4.365635 4.366374 4.560838 4.609614 4.650246 4.662700
## [15,] 3.187590 3.207663 3.219120 3.315205 3.410314 3.454411 3.472269 3.585439
## [16,] 3.313034 3.544929 3.548345 3.878558 3.908734 3.969390 3.973971 4.050110
## [17,] 2.862890 3.043581 3.073014 3.241036 3.270410 3.403662 3.429560 3.452528
## [18,] 2.482796 2.790580 2.888455 2.911103 2.939770 2.967181 3.016083 3.042371
## [19,] 3.135509 3.359119 3.474137 3.525108 3.547987 3.560113 3.607436 3.612299
## [20,] 3.115229 3.125238 3.248057 3.274541 3.291762 3.387902 3.486595 3.509272
## [,9] [,10]
## [1,] 3.678844 3.724938
## [2,] 2.763451 2.791955
## [3,] 2.989109 2.991175
## [4,] 4.627988 4.706954
## [5,] 6.260775 6.297633
## [6,] 3.481928 3.502404
## [7,] 3.971563 3.974701
## [8,] 4.395414 4.429955
## [9,] 3.247975 3.265766
## [10,] 4.331026 4.356892
## [11,] 4.979139 4.985220
## [12,] 4.851172 4.901480
## [13,] 3.607652 3.620843
## [14,] 4.664854 4.669488
## [15,] 3.609303 3.622569
## [16,] 4.130769 4.133179
## [17,] 3.460359 3.499257
## [18,] 3.131104 3.154524
## [19,] 3.732477 3.848146
## [20,] 3.510749 3.539927
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 1 1 0.839
## 2 1 1 1
## 3 1 1 0.752
## 4 1 1 0.992
## 5 1 1 0.968
## 6 1 1 0.931
## 7 1 1 0.810
## 8 1 1 0.810
## 9 1 1 0.914
## 10 1 1 0.931
## # ℹ 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.253 0.492 -0.367 -0.737
## 2 -0.228 0.278 -0.234 -0.615
## 3 -0.450 -0.0549 0.936 0.281
## 4 -0.198 0.584 -0.0366 -0.899
## 5 -0.0664 -0.184 -0.0623 -0.262
## 6 -0.159 0.756 -0.618 -0.190
## 7 -0.291 1.46 0.454 0.840
## 8 -0.0515 -0.101 1.57 0.00975
## 9 -0.267 -0.158 0.863 -0.698
## 10 -0.382 -0.420 0.620 0.133
## # ℹ 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.263 0.347 0.325 0.213 0.157 ...