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] 977 467 830 57 102 884 118 246 929 477 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 977 761 591 427 14 473 554 634 105 204
## [2,] 467 752 386 315 621 575 499 661 98 769
## [3,] 830 614 111 877 105 610 487 248 219 736
## [4,] 57 884 211 377 408 829 439 98 461 865
## [5,] 102 278 780 393 63 161 100 919 244 164
## [6,] 884 679 360 980 713 233 94 461 500 58
## [7,] 118 796 296 979 677 107 149 158 977 591
## [8,] 246 50 74 645 379 700 720 107 176 886
## [9,] 929 230 895 483 894 624 788 880 426 40
## [10,] 477 743 682 551 774 110 888 737 253 468
## [11,] 620 811 110 594 391 774 188 82 744 303
## [12,] 25 383 407 984 110 611 257 35 572 304
## [13,] 750 520 827 727 23 100 898 627 73 461
## [14,] 487 723 550 877 348 496 761 800 876 634
## [15,] 232 138 610 452 478 410 460 259 496 941
## [16,] 496 41 232 99 357 877 583 555 845 758
## [17,] 422 42 415 844 321 876 299 353 881 311
## [18,] 765 214 426 726 67 640 894 650 786 880
## [19,] 767 40 388 198 466 109 974 810 987 262
## [20,] 223 522 829 631 404 461 455 161 294 727
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.67 3.16 3.3 3.68 3.64 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.666835 3.726584 3.958652 4.060437 4.105350 4.139561 4.144768 4.190476
## [2,] 3.159057 3.334992 3.615929 3.680224 3.681337 3.689039 3.759648 3.835075
## [3,] 3.302046 3.591900 3.722861 3.750884 3.906987 3.924154 4.155524 4.162248
## [4,] 3.678958 3.689174 3.913557 3.955965 4.023613 4.116374 4.149045 4.195495
## [5,] 3.637631 4.045851 4.369343 4.394792 4.545654 4.627091 4.651798 4.666973
## [6,] 3.083706 3.457156 3.457709 3.543018 3.606937 3.619041 3.623170 3.695119
## [7,] 4.114331 4.450696 4.570147 4.620270 4.646754 4.764384 4.802017 4.873611
## [8,] 3.805075 4.009335 4.065416 4.142112 4.188165 4.203437 4.204764 4.278912
## [9,] 2.440778 2.509628 2.693748 2.739427 2.769781 2.870486 2.897538 3.075204
## [10,] 4.527361 4.789237 5.330974 5.348276 5.477655 5.611114 5.650293 5.672119
## [11,] 4.383109 4.467764 4.509495 4.509913 4.561511 4.567260 4.577932 4.600141
## [12,] 4.059664 4.067759 4.142060 4.170939 4.191703 4.291209 4.323342 4.331572
## [13,] 3.338399 3.475765 3.486147 3.566865 3.622264 3.624838 3.629024 3.704721
## [14,] 2.746459 2.915979 2.931016 2.958245 3.037768 3.105474 3.230213 3.259517
## [15,] 4.115580 4.162285 4.316630 4.407267 4.458134 4.469305 4.499587 4.515450
## [16,] 3.024043 3.036909 3.067011 3.174962 3.328766 3.344920 3.401148 3.418359
## [17,] 2.697609 2.832905 3.069267 3.180293 3.310888 3.510578 3.569947 3.589520
## [18,] 3.120715 3.135509 3.169330 3.204341 3.359119 3.474137 3.547987 3.593457
## [19,] 2.435987 2.787150 3.013319 3.031373 3.037619 3.148368 3.151578 3.200521
## [20,] 2.172608 2.539375 2.706849 2.992774 3.045836 3.077080 3.126652 3.189087
## [,9] [,10]
## [1,] 4.236370 4.259810
## [2,] 3.863112 3.900888
## [3,] 4.175373 4.180663
## [4,] 4.229447 4.273846
## [5,] 4.742350 4.786867
## [6,] 3.697610 3.736123
## [7,] 4.924406 4.990934
## [8,] 4.285100 4.289391
## [9,] 3.106030 3.117838
## [10,] 5.723587 5.768794
## [11,] 4.606108 4.629623
## [12,] 4.372369 4.395517
## [13,] 3.763912 3.800639
## [14,] 3.321878 3.369827
## [15,] 4.517677 4.555320
## [16,] 3.437772 3.466670
## [17,] 3.630642 3.759761
## [18,] 3.944938 3.954952
## [19,] 3.232251 3.280142
## [20,] 3.203300 3.227914
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.991 0.466
## 2 1 0.988 0.779
## 3 1 0.994 1
## 4 1 0.988 1
## 5 1 0.988 0.710
## 6 1 1 0.990
## 7 1 0.988 0.450
## 8 1 0.988 0.772
## 9 1 1 0.613
## 10 1 0.994 0.996
## # ℹ 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.433 -0.705 0.931 -1.30
## 2 -0.387 -0.505 -0.150 -1.33
## 3 -0.142 0.598 -0.200 -0.616
## 4 -0.128 -0.141 -0.246 0.273
## 5 0.210 -0.0719 1.27 0.974
## 6 0.414 -0.583 -0.352 -0.298
## 7 -0.152 -0.0145 -0.180 0.358
## 8 0.296 1.00 1.28 -0.0860
## 9 0.240 0.483 0.110 -2.15
## 10 -0.0231 0.389 -0.151 -0.528
## # ℹ 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.232 0.254 0.237 0.227 0.209 ...