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] 724 278 757 826 605 384 918 732 299 605 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 724 847 609 205 578 254 652 449 963 871
## [2,] 278 174 672 359 1000 409 927 924 74 84
## [3,] 757 425 952 19 951 826 456 983 251 337
## [4,] 826 766 90 660 757 886 785 423 829 952
## [5,] 605 744 892 180 868 973 472 689 699 609
## [6,] 384 461 773 301 907 914 874 910 624 877
## [7,] 918 144 309 732 790 96 619 194 872 624
## [8,] 732 301 790 828 488 533 918 7 96 297
## [9,] 299 326 605 237 636 783 43 151 123 36
## [10,] 605 254 382 909 656 783 9 322 428 299
## [11,] 430 341 972 84 885 98 672 814 88 296
## [12,] 968 871 963 836 382 494 583 537 178 449
## [13,] 256 518 915 126 505 816 616 757 571 526
## [14,] 517 534 686 318 525 55 247 851 122 980
## [15,] 841 572 491 482 702 42 569 759 99 868
## [16,] 300 850 514 730 129 47 647 572 848 21
## [17,] 768 524 643 502 703 797 889 877 34 67
## [18,] 996 820 784 330 56 179 347 535 864 646
## [19,] 425 337 3 251 456 664 539 102 938 633
## [20,] 829 952 337 275 905 456 660 513 86 221
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.45 3.87 3.31 3.51 3.16 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.454328 2.592222 2.644167 2.663104 2.714878 2.799801 2.825407 2.852405
## [2,] 3.874265 4.109645 4.280278 4.351091 4.435342 4.444715 4.557055 4.564580
## [3,] 3.312534 3.435652 3.616347 3.624553 3.627424 3.637013 3.711650 3.730789
## [4,] 3.511983 3.600126 3.710470 3.807232 3.881188 3.887025 3.903024 4.108874
## [5,] 3.156068 3.318731 3.392997 3.397660 3.439641 3.450455 3.484488 3.538548
## [6,] 3.563357 3.839062 3.875981 3.927310 4.017907 4.169586 4.174466 4.197146
## [7,] 2.660734 3.222712 3.330355 3.410273 3.489173 3.543849 3.544633 3.668412
## [8,] 4.027801 4.070074 4.092915 4.093203 4.130104 4.164760 4.273963 4.471929
## [9,] 2.396558 3.259011 3.304500 3.324790 3.369705 3.479397 3.554798 3.678302
## [10,] 3.341014 3.380446 3.427433 3.578891 3.722028 3.734385 3.757769 3.770940
## [11,] 3.576761 3.686981 4.013864 4.028906 4.109036 4.117041 4.163028 4.165814
## [12,] 2.852806 2.889047 2.959003 3.231673 3.231683 3.382220 3.450353 3.455540
## [13,] 4.797162 5.096301 5.203686 5.481237 5.551291 5.561407 5.577394 5.689126
## [14,] 4.631537 5.346780 5.584621 5.600063 5.657185 5.698041 5.802450 5.970378
## [15,] 3.095493 3.549029 3.611467 3.636777 3.735117 3.776606 3.786670 3.822604
## [16,] 2.921099 2.936044 2.995608 3.135549 3.331935 3.386441 3.396874 3.419248
## [17,] 3.849132 3.890322 4.123144 4.640677 4.665406 5.025780 5.071413 5.109841
## [18,] 3.714277 4.095550 5.021013 5.138767 5.183568 5.311071 5.433805 5.522612
## [19,] 3.374849 3.593361 3.624553 3.729749 3.776295 3.967070 4.100474 4.178218
## [20,] 3.832430 4.081626 4.109364 4.145820 4.241192 4.241636 4.273084 4.306341
## [,9] [,10]
## [1,] 2.879802 2.886320
## [2,] 4.573815 4.626615
## [3,] 3.748499 3.809925
## [4,] 4.160522 4.176827
## [5,] 3.553685 3.605198
## [6,] 4.403295 4.415708
## [7,] 3.710827 3.729044
## [8,] 4.595689 4.625179
## [9,] 3.714439 3.747621
## [10,] 3.816174 3.869995
## [11,] 4.205308 4.227726
## [12,] 3.467135 3.469263
## [13,] 5.701672 5.773673
## [14,] 6.207261 6.314105
## [15,] 3.864378 3.878217
## [16,] 3.460739 3.487585
## [17,] 5.343919 5.390074
## [18,] 5.575583 5.666185
## [19,] 4.290559 4.305770
## [20,] 4.312616 4.326114
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.990 0.983 0.999
## 2 0.990 0.983 0.999
## 3 0.990 1 1
## 4 0.999 1 1
## 5 0.990 1.00 0.999
## 6 0.990 0.983 1
## 7 0.990 1.00 0.999
## 8 0.990 0.983 0.999
## 9 0.990 1.00 0.999
## 10 0.990 1 0.999
## # ℹ 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.548 0.632 1.62 0.647
## 2 -0.210 -0.236 0.862 -0.753
## 3 0.0596 0.185 -0.218 0.0135
## 4 -0.273 -0.534 -0.0498 0.203
## 5 0.823 1.28 0.729 -0.0541
## 6 0.819 0.372 0.575 0.906
## 7 0.256 -0.104 1.02 -0.537
## 8 -0.0637 0.823 0.552 -0.372
## 9 0.367 0.421 1.35 -0.847
## 10 -0.135 -0.00296 0.420 0.791
## # ℹ 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.331 0.212 0.243 0.229 0.27 ...