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] 70 771 623 448 473 26 344 977 643 495 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 70 75 321 983 827 911 443 646 531 673
## [2,] 771 982 410 828 328 71 89 647 996 735
## [3,] 623 914 411 905 586 644 708 198 372 698
## [4,] 448 549 189 67 768 414 99 240 220 655
## [5,] 473 32 936 38 324 593 162 516 590 583
## [6,] 26 780 368 771 853 179 889 410 659 526
## [7,] 344 915 431 950 488 784 199 140 809 17
## [8,] 977 367 643 912 271 9 622 738 588 493
## [9,] 643 588 685 8 52 912 442 994 367 942
## [10,] 495 409 893 369 295 957 13 492 26 176
## [11,] 666 597 635 895 868 277 624 801 15 971
## [12,] 598 557 696 290 638 793 489 571 733 694
## [13,] 850 65 175 853 10 953 278 927 320 345
## [14,] 162 55 649 435 828 32 5 785 296 346
## [15,] 426 11 578 930 895 635 666 810 992 761
## [16,] 384 909 467 233 231 829 538 369 116 998
## [17,] 784 175 618 137 434 488 481 799 766 288
## [18,] 623 698 586 411 876 905 708 963 372 262
## [19,] 924 214 317 322 533 83 456 71 334 471
## [20,] 147 969 689 974 999 802 671 223 598 388
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.99 2.94 3.71 2.73 3.28 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.991583 3.045367 3.072616 3.100710 3.258759 3.343534 3.373036 3.390994
## [2,] 2.936505 2.942758 2.981166 3.181385 3.397790 3.425969 3.442462 3.468957
## [3,] 3.706606 3.993242 4.140512 4.153796 4.176097 4.180736 4.300989 4.343596
## [4,] 2.733156 3.141290 3.751039 3.829118 3.932041 3.974868 4.064854 4.101355
## [5,] 3.284251 3.653273 3.847342 3.868038 3.929797 3.955489 3.995078 4.081719
## [6,] 2.564697 2.793839 2.806661 2.895936 2.987004 2.991700 2.993643 3.167068
## [7,] 5.001998 5.049518 5.252624 5.284338 5.386282 5.576789 5.615163 5.659937
## [8,] 2.953560 3.074132 3.133982 3.214965 3.293389 3.458017 3.478402 3.528708
## [9,] 2.915530 3.064093 3.278002 3.458017 3.503581 3.611620 3.627522 3.653345
## [10,] 2.697392 3.576304 3.599633 3.604800 3.605568 3.682489 3.704882 3.803745
## [11,] 3.600803 3.656943 3.824428 3.932091 3.947861 4.009307 4.051824 4.078542
## [12,] 2.617387 2.863066 2.970982 3.054148 3.177500 3.300264 3.333147 3.345075
## [13,] 3.505638 3.555033 3.636335 3.655267 3.704882 3.810580 3.857386 3.861605
## [14,] 3.655312 3.806788 4.316811 4.449122 4.463766 4.470530 4.479801 4.523224
## [15,] 4.078480 4.080412 4.332501 4.338435 4.422504 4.481948 4.504484 4.617718
## [16,] 4.073705 4.132365 4.184798 4.298854 4.301064 4.312950 4.383109 4.388127
## [17,] 2.702670 2.871365 3.013279 3.102100 3.206514 3.353857 3.429956 3.497314
## [18,] 3.211137 3.266432 3.639487 3.668870 4.199958 4.530719 4.546176 4.585742
## [19,] 3.130829 3.168493 3.216842 3.256615 3.374054 3.489961 3.510688 3.627383
## [20,] 3.255518 3.425450 3.450891 3.544296 3.635721 3.762113 3.838453 3.896203
## [,9] [,10]
## [1,] 3.454167 3.589389
## [2,] 3.529840 3.596590
## [3,] 4.366957 4.371364
## [4,] 4.136630 4.189944
## [5,] 4.149534 4.157011
## [6,] 3.170648 3.174836
## [7,] 5.696293 5.750487
## [8,] 3.587734 3.603934
## [9,] 3.700618 3.756622
## [10,] 3.807332 3.897629
## [11,] 4.080412 4.130972
## [12,] 3.347548 3.374106
## [13,] 3.870567 3.877345
## [14,] 4.579138 4.580091
## [15,] 4.627918 4.649156
## [16,] 4.395743 4.455883
## [17,] 3.522967 3.537343
## [18,] 4.696108 4.722467
## [19,] 3.628469 3.645865
## [20,] 3.899381 3.968074
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.994 0.162
## 2 1 0.994 0.450
## 3 1 0.994 0.842
## 4 1 1 0.638
## 5 1 0.994 0.827
## 6 1 0.994 0.783
## 7 1 0.994 1
## 8 1 1 0.915
## 9 1 0.994 0.738
## 10 1 1 0.864
## # ℹ 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.439 -0.422 -0.257 -0.478
## 2 -1.00 -0.700 -0.236 -1.37
## 3 -0.492 -0.495 -0.352 -0.456
## 4 -0.474 -0.306 -0.739 -0.342
## 5 -0.00853 -0.190 -0.0477 0.212
## 6 -0.498 -0.506 -0.480 -0.698
## 7 -0.236 -0.0443 -0.0258 0.237
## 8 -0.494 -0.108 0.702 -0.969
## 9 -0.0616 -0.587 1.47 -0.490
## 10 -0.0279 -0.0157 -0.175 -0.557
## # ℹ 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.278 0.276 0.211 0.236 0.237 ...