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] 636 284 509 737 238 257 971 202 841 170 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 636 805 437 199 838 111 725 795 593 243
## [2,] 284 504 572 258 726 992 690 905 35 991
## [3,] 509 133 97 17 22 597 926 974 969 703
## [4,] 737 688 71 757 531 687 656 827 791 570
## [5,] 238 33 156 699 116 536 714 18 447 357
## [6,] 257 706 851 916 760 221 446 529 968 309
## [7,] 971 60 596 453 118 246 647 462 280 937
## [8,] 202 53 58 415 206 278 321 542 254 515
## [9,] 841 718 97 387 370 152 974 926 414 147
## [10,] 170 675 602 374 330 938 590 795 657 967
## [11,] 478 863 375 371 974 234 861 382 69 825
## [12,] 697 62 456 996 594 860 944 719 435 509
## [13,] 119 531 800 581 656 925 360 129 240 648
## [14,] 343 793 322 70 594 216 17 83 249 803
## [15,] 917 792 994 192 51 663 210 221 373 734
## [16,] 82 341 276 651 274 1000 751 797 132 27
## [17,] 594 3 133 322 70 969 83 509 911 38
## [18,] 805 111 845 965 715 330 357 10 725 170
## [19,] 992 155 854 607 880 848 912 984 751 185
## [20,] 579 425 557 855 656 674 664 980 452 707
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.23 3.91 2.76 3.86 3.91 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.233940 3.247491 3.432329 3.475996 3.525090 3.645820 3.686215 3.704800
## [2,] 3.911108 4.229922 4.398263 4.408850 4.455586 4.549342 4.557152 4.722035
## [3,] 2.758565 2.966734 3.137071 3.302855 3.358817 3.379048 3.396851 3.399222
## [4,] 3.862025 4.677194 5.034077 5.364972 5.434077 5.512953 5.554769 5.670642
## [5,] 3.905281 4.209103 4.451718 4.499129 4.724508 4.775266 4.804957 4.851907
## [6,] 4.138642 4.227155 4.458585 4.604992 4.659862 4.781641 4.943459 4.985732
## [7,] 3.137002 3.228795 3.332833 3.392172 3.419672 3.617902 3.670411 3.675168
## [8,] 3.173675 3.224588 3.400965 3.597171 3.616083 3.641057 3.701916 3.751109
## [9,] 2.813801 3.322713 3.365303 3.481355 3.518717 3.522506 3.537244 3.601106
## [10,] 2.210230 2.288887 2.714452 2.772969 2.874490 2.948353 2.950053 3.002259
## [11,] 2.907990 3.494212 3.497916 3.599459 3.626131 3.653628 3.661115 3.726571
## [12,] 3.913386 4.031615 4.142594 4.359579 4.377665 4.443450 4.533079 4.544791
## [13,] 4.872277 4.966864 4.972235 5.160121 5.164474 5.234448 5.431538 5.444645
## [14,] 4.295953 4.351428 4.375250 4.384059 4.401406 4.482967 4.498550 4.568035
## [15,] 5.024567 5.234342 5.346780 5.566567 5.600063 5.657185 5.698041 5.802450
## [16,] 5.336579 5.636779 5.690738 5.882752 5.890783 5.891578 5.905012 5.921251
## [17,] 3.037948 3.302855 3.335536 3.417346 3.453594 3.460411 3.639211 3.642213
## [18,] 2.509635 2.594963 2.795106 2.849930 2.982564 3.019074 3.059421 3.110251
## [19,] 4.097389 4.400658 4.429491 4.518798 4.543052 4.562916 4.714266 4.717455
## [20,] 3.096696 3.236308 3.669423 3.890517 3.904964 3.931054 4.142554 4.395414
## [,9] [,10]
## [1,] 3.743823 3.774978
## [2,] 4.738761 4.740512
## [3,] 3.412648 3.453275
## [4,] 5.734580 5.755767
## [5,] 4.923635 5.004256
## [6,] 5.055807 5.198930
## [7,] 3.761874 3.810846
## [8,] 3.822607 3.825760
## [9,] 3.613722 3.651251
## [10,] 3.048631 3.092664
## [11,] 3.767352 3.776084
## [12,] 4.557106 4.582614
## [13,] 5.447365 5.463677
## [14,] 4.618178 4.716171
## [15,] 5.830067 5.970378
## [16,] 5.944053 5.999859
## [17,] 3.653846 3.678988
## [18,] 3.146352 3.161656
## [19,] 4.722761 4.723819
## [20,] 4.526058 4.529453
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 0.870 0.774
## 2 1 0.870 0.915
## 3 1 0.946 0.770
## 4 1 0.895 0.894
## 5 1 0.870 0.816
## 6 1 1 1
## 7 1 0.902 0.828
## 8 1 1 0.748
## 9 1 0.946 0.471
## 10 1 0.984 0.974
## # ℹ 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.278 -0.00435 -0.131 -1.06
## 2 -0.0749 -0.118 -0.282 -1.01
## 3 -0.0982 -0.256 0.216 -0.554
## 4 -0.0957 -0.176 -0.199 -0.493
## 5 -0.0855 -0.406 -0.487 -0.833
## 6 -0.0595 -0.264 -0.454 -1.16
## 7 0.962 -0.350 -0.716 -1.11
## 8 -0.221 -0.132 -0.427 -0.636
## 9 -0.228 -0.581 -0.237 0.150
## 10 -0.388 -0.839 -0.392 0.560
## # ℹ 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.26 0.208 0.282 0.174 0.198 ...