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] 564 819 317 892 747 994 200 219 202 130 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 564 428 130 594 527 242 180 725 702 569
## [2,] 819 743 930 507 42 760 742 601 997 998
## [3,] 317 895 292 952 384 655 417 163 713 797
## [4,] 892 567 489 424 64 811 809 592 488 235
## [5,] 747 350 133 604 459 887 710 260 852 777
## [6,] 994 658 718 804 471 979 843 889 884 311
## [7,] 200 143 541 563 152 545 451 551 604 303
## [8,] 219 874 959 650 99 30 483 85 319 878
## [9,] 202 33 26 505 333 582 178 302 107 775
## [10,] 130 71 460 685 694 122 236 653 394 702
## [11,] 592 394 339 939 344 340 946 702 691 77
## [12,] 887 459 16 929 116 604 834 51 438 7
## [13,] 848 984 115 189 24 827 292 846 793 840
## [14,] 25 202 619 31 33 925 778 505 950 870
## [15,] 30 869 801 408 295 582 564 212 803 950
## [16,] 152 604 541 451 747 350 762 456 438 459
## [17,] 899 990 343 29 909 538 463 50 871 247
## [18,] 818 568 501 364 712 776 611 405 921 856
## [19,] 551 367 887 456 184 167 466 690 502 834
## [20,] 51 433 350 541 563 910 214 898 459 442
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 4.51 3.99 2.5 3.2 2.53 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 4.505724 4.671480 4.803985 4.818958 4.938966 4.975562 4.994535 5.044814
## [2,] 3.992142 4.029944 4.130523 4.188303 4.279225 4.445916 4.471003 4.502416
## [3,] 2.501066 2.837718 2.926205 2.963436 3.002221 3.178326 3.184604 3.265195
## [4,] 3.203988 3.206925 3.268903 3.390087 3.411248 3.412378 3.450779 3.534031
## [5,] 2.533157 2.867010 3.045342 3.204341 3.208412 3.214327 3.637745 3.643436
## [6,] 3.417192 3.529116 3.873524 3.922527 4.099818 4.118173 4.418518 4.427304
## [7,] 2.849562 3.319166 3.352480 3.454767 3.478612 3.480990 3.528192 3.582077
## [8,] 3.549076 3.711839 3.771055 3.795447 3.861940 4.022863 4.029100 4.031841
## [9,] 2.662743 2.960885 2.964287 2.981408 3.042744 3.056066 3.056611 3.092657
## [10,] 3.767171 3.793214 4.375456 4.393305 4.394399 4.432943 4.446212 4.451966
## [11,] 2.485762 3.429964 3.506096 3.669237 3.707808 3.742693 3.810819 3.838643
## [12,] 3.389547 3.539433 3.550295 3.601436 3.649779 3.658860 3.665247 3.732783
## [13,] 3.378475 3.435403 3.622770 3.677866 3.678454 3.777268 3.778818 3.798292
## [14,] 3.340329 3.350017 3.408984 3.460197 3.498565 3.553991 3.568768 3.573260
## [15,] 3.339896 3.376791 3.443025 3.593604 3.694217 3.706624 3.713090 3.722900
## [16,] 2.183445 2.568532 2.648219 2.705975 2.745820 2.763009 2.843641 2.861485
## [17,] 5.208228 5.322125 5.332606 5.372345 5.380729 5.394190 5.442963 5.445606
## [18,] 3.523182 3.688987 3.704448 3.710470 3.819890 3.940180 4.019706 4.110932
## [19,] 3.088610 3.155625 3.397197 3.402837 3.527949 3.748741 3.767717 3.827857
## [20,] 2.531550 2.616470 2.626894 2.680070 2.959863 2.990677 3.067011 3.089988
## [,9] [,10]
## [1,] 5.082858 5.186565
## [2,] 4.515071 4.577937
## [3,] 3.287321 3.370140
## [4,] 3.550266 3.579120
## [5,] 3.653801 3.702478
## [6,] 4.449264 4.475207
## [7,] 3.584387 3.591139
## [8,] 4.053408 4.093712
## [9,] 3.151846 3.159281
## [10,] 4.475409 4.522515
## [11,] 3.846423 3.907548
## [12,] 3.780607 3.781998
## [13,] 3.862912 3.968145
## [14,] 3.610366 3.667477
## [15,] 3.766105 3.797831
## [16,] 2.960426 3.004018
## [17,] 5.529862 5.544765
## [18,] 4.153218 4.188857
## [19,] 3.866521 3.886697
## [20,] 3.108414 3.181871
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.912 0.996 0.966
## 2 0.974 0.996 1
## 3 0.990 1 0.966
## 4 0.971 0.996 1
## 5 0.768 0.996 1
## 6 0.963 0.996 1
## 7 0.967 0.996 1
## 8 0.835 0.996 1
## 9 0.974 0.996 1
## 10 0.857 0.996 0.966
## # ℹ 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.688 -0.293 -1.28 -0.313
## 2 -0.00438 -0.304 -0.248 0.347
## 3 -0.0222 -0.235 -0.374 0.722
## 4 -0.388 -0.839 -0.392 0.560
## 5 -0.0575 -0.166 -0.232 0.201
## 6 -0.283 -0.202 -0.283 0.537
## 7 -0.0494 -0.196 -0.0961 0.996
## 8 -0.00101 -0.182 -0.0879 0.411
## 9 -0.458 -0.355 -0.366 0.0316
## 10 0.787 -0.239 0.0882 0.0555
## # ℹ 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.19 0.215 0.297 0.274 0.268 ...