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] 397 493 328 257 66 831 467 719 629 797 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 397 248 595 226 310 745 476 241 730 809
## [2,] 493 176 966 7 559 149 594 66 477 614
## [3,] 328 742 147 136 594 89 329 558 914 297
## [4,] 257 316 458 761 523 729 633 547 681 722
## [5,] 66 618 222 167 47 435 437 663 654 82
## [6,] 831 144 780 437 772 85 752 655 505 52
## [7,] 467 251 559 142 522 829 631 950 665 590
## [8,] 719 660 56 557 267 773 530 822 499 735
## [9,] 629 726 212 502 109 394 920 257 567 78
## [10,] 797 541 164 645 398 973 587 252 515 543
## [11,] 849 904 606 387 492 856 909 450 511 957
## [12,] 428 954 755 309 36 200 427 997 245 356
## [13,] 617 515 751 350 833 974 704 798 362 631
## [14,] 974 957 429 53 43 457 602 663 613 781
## [15,] 485 459 956 97 844 572 386 615 38 645
## [16,] 783 838 87 576 990 899 949 148 612 835
## [17,] 791 268 961 845 79 375 733 411 666 139
## [18,] 780 218 917 116 952 908 82 203 276 657
## [19,] 911 704 454 974 833 286 120 104 294 350
## [20,] 241 220 729 158 820 951 595 764 706 487
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.65 3.76 2.95 3.26 2.84 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.645414 3.709156 4.029866 4.133649 4.134326 4.144389 4.150857 4.153028
## [2,] 3.760809 3.952757 3.985408 3.989476 4.047919 4.062999 4.075113 4.199003
## [3,] 2.945146 3.165788 3.324877 3.327581 3.385618 3.470366 3.536828 3.561370
## [4,] 3.259101 3.261762 3.666321 3.687049 3.791091 3.802243 3.802274 3.805394
## [5,] 2.835728 3.500711 3.552670 3.585326 3.706476 3.749412 3.754423 3.769125
## [6,] 3.338399 3.382568 3.566865 3.624838 3.704721 3.757948 3.822214 3.824715
## [7,] 2.767854 2.806661 2.987004 2.991700 3.122293 3.130170 3.170648 3.189350
## [8,] 3.915565 4.107313 4.291177 4.400052 4.413866 4.444378 4.574260 4.593292
## [9,] 3.499473 3.594728 3.677234 3.760076 3.805075 4.009335 4.018692 4.065416
## [10,] 2.958366 3.266185 3.310175 3.432619 3.561533 3.644414 3.664861 3.682744
## [11,] 3.139290 3.254699 3.264064 3.399754 3.474589 3.505483 3.516638 3.627091
## [12,] 2.554459 3.042187 3.873224 4.134249 4.236226 4.353519 4.603931 4.636049
## [13,] 2.551453 2.957247 3.008799 3.145082 3.148818 3.165821 3.178897 3.266841
## [14,] 3.465510 3.536116 3.616258 3.618091 3.643756 3.741816 3.762439 3.767415
## [15,] 3.719667 4.026514 4.176736 4.270014 4.291213 4.377162 4.378579 4.390021
## [16,] 3.708425 4.258050 4.305388 4.807121 5.106599 5.137192 5.161567 5.257490
## [17,] 5.163658 5.323673 5.328068 5.352525 5.355876 5.384779 5.425869 5.463509
## [18,] 3.276811 3.301880 3.315433 3.382013 3.481479 3.566801 3.578287 3.578380
## [19,] 3.137002 3.332833 3.392172 3.572659 3.670411 3.715041 3.813584 3.840656
## [20,] 3.802450 3.897873 3.909321 3.912981 4.139994 4.206469 4.214673 4.235914
## [,9] [,10]
## [1,] 4.296063 4.296554
## [2,] 4.203501 4.215571
## [3,] 3.664144 3.676124
## [4,] 3.814260 3.843571
## [5,] 3.779799 3.785346
## [6,] 3.830374 3.831685
## [7,] 3.201493 3.217548
## [8,] 4.597965 4.654596
## [9,] 4.204764 4.285100
## [10,] 3.683244 3.695702
## [11,] 3.689276 3.712615
## [12,] 4.679682 4.708707
## [13,] 3.269239 3.335537
## [14,] 3.797406 3.816846
## [15,] 4.436698 4.441487
## [16,] 5.287662 5.368870
## [17,] 5.481784 5.485570
## [18,] 3.595170 3.608271
## [19,] 3.868350 3.893680
## [20,] 4.291757 4.372950
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.911 1
## 2 0.892 0.973 0.944
## 3 1 0.954 0.778
## 4 0.876 0.997 0.604
## 5 0.995 0.950 0.990
## 6 1 0.997 0.857
## 7 0.892 0.988 0.911
## 8 1 0.950 0.519
## 9 1 0.973 0.970
## 10 1 1 0.663
## # ℹ 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.161 0.888 -1.28 -0.470
## 2 -0.147 -0.234 -0.0636 -2.14
## 3 -1.07 -0.650 0.0482 -1.15
## 4 -0.201 -0.220 0.723 -1.64
## 5 -0.221 -0.00163 -0.186 -1.05
## 6 -1.26 -0.980 -0.244 -1.77
## 7 -0.438 -0.0758 -0.118 -0.678
## 8 -0.233 0.549 0.631 0.0991
## 9 0.392 -0.0429 -0.335 -2.89
## 10 0.371 -0.357 0.355 -0.604
## # ℹ 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.229 0.236 0.265 0.252 0.257 ...