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] 753 475 520 847 992 398 463 586 374 494 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 753 494 289 485 326 549 200 300 27 826
## [2,] 475 263 656 833 670 231 294 100 761 78
## [3,] 520 561 806 907 304 748 791 681 177 154
## [4,] 847 203 249 408 543 778 496 888 695 763
## [5,] 992 975 832 183 642 512 213 999 756 721
## [6,] 398 244 174 372 406 673 810 315 989 351
## [7,] 463 309 759 35 826 663 900 348 517 281
## [8,] 586 63 92 991 789 835 764 509 309 458
## [9,] 374 912 51 672 104 667 922 630 773 965
## [10,] 494 702 247 68 880 652 253 94 149 633
## [11,] 59 46 845 252 507 81 459 68 427 826
## [12,] 939 692 845 826 427 252 486 925 658 378
## [13,] 43 176 273 268 348 571 566 764 735 619
## [14,] 795 912 579 380 691 627 155 773 29 51
## [15,] 57 105 165 566 45 222 422 400 87 285
## [16,] 294 340 4 847 491 859 122 952 511 284
## [17,] 595 773 224 476 643 231 244 136 624 18
## [18,] 874 44 374 263 376 761 595 670 773 51
## [19,] 466 100 152 936 347 848 890 867 198 937
## [20,] 561 553 951 806 879 520 830 278 748 324
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.73 3.98 3.24 3.61 5.1 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.731706 2.754043 2.776866 2.779366 2.961980 2.977156 2.990896 3.027949
## [2,] 3.984278 3.996332 4.328697 4.392967 4.439480 4.538187 4.585490 4.596258
## [3,] 3.244238 3.659957 3.815576 3.850592 3.939764 3.974999 4.003249 4.082309
## [4,] 3.606343 3.742459 4.011622 4.037374 4.095586 4.141763 4.222709 4.252544
## [5,] 5.096301 5.203686 5.312953 5.346273 5.387677 5.455912 5.481237 5.561407
## [6,] 3.555676 4.064845 4.213306 4.304861 4.374337 4.378823 4.412804 4.419537
## [7,] 3.475064 3.599945 3.635163 3.674064 3.728996 3.732867 3.784510 3.817044
## [8,] 3.172684 3.211234 3.292320 3.303263 3.333152 3.351309 3.528282 3.554566
## [9,] 2.903163 3.105208 3.493578 3.602325 3.604967 3.607594 3.644417 3.649726
## [10,] 3.617761 3.623503 3.665716 3.697564 3.754726 3.827799 3.840619 3.892174
## [11,] 3.242709 3.349494 3.408268 3.464100 3.548138 3.574754 3.583722 3.612292
## [12,] 3.561527 3.656104 3.720733 3.738319 3.822681 3.835872 3.872963 3.875122
## [13,] 2.614302 2.663104 2.670281 3.004736 3.172732 3.201109 3.265436 3.365401
## [14,] 3.288704 3.308502 3.411552 3.464850 3.592468 3.598036 3.683985 3.684131
## [15,] 3.340251 3.412893 3.457041 3.497966 3.574911 3.632760 3.680311 3.884876
## [16,] 4.133291 4.519412 4.521023 4.581570 4.679746 4.715303 4.747115 4.772071
## [17,] 3.179104 3.188749 3.239590 3.322334 3.524686 3.561743 3.598864 3.615391
## [18,] 2.474032 2.908386 2.956495 2.969394 3.008786 3.025276 3.165508 3.252539
## [19,] 4.989852 5.204365 5.494584 5.524886 5.551937 5.621657 5.629288 5.649932
## [20,] 3.846971 4.149865 4.152537 4.465981 4.664139 4.759512 4.787987 4.839777
## [,9] [,10]
## [1,] 3.040271 3.198957
## [2,] 4.612405 4.649337
## [3,] 4.109970 4.204069
## [4,] 4.260511 4.307663
## [5,] 5.689126 5.701672
## [6,] 4.438153 4.441986
## [7,] 3.951036 3.971680
## [8,] 3.565380 3.604993
## [9,] 3.654425 3.694518
## [10,] 3.902825 4.045153
## [11,] 3.649849 3.669487
## [12,] 3.912308 3.940771
## [13,] 3.399457 3.408421
## [14,] 3.694385 3.699110
## [15,] 3.902945 3.914471
## [16,] 4.832044 4.841647
## [17,] 3.674816 3.683914
## [18,] 3.257129 3.326228
## [19,] 5.727689 5.765416
## [20,] 4.855429 4.979139
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.891 1 0.924
## 2 0.988 1 0.977
## 3 0.976 1 1
## 4 0.984 1 0.924
## 5 0.986 1 0.977
## 6 0.977 1 0.983
## 7 0.913 1 0.880
## 8 0.891 1 0.924
## 9 0.977 1 0.977
## 10 0.927 1 0.983
## # ℹ 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.846 -0.313 -0.262 -0.207
## 2 0.0970 0.530 -0.0602 0.280
## 3 -0.151 -0.252 -0.123 -0.168
## 4 -0.438 -0.0758 -0.118 -0.678
## 5 -0.136 -0.242 0.532 0.0579
## 6 0.186 0.432 0.564 0.557
## 7 -0.0498 0.201 0.573 -0.355
## 8 -0.465 -0.459 0.787 -0.299
## 9 -0.452 -0.117 -0.127 0.540
## 10 0.432 0.694 0.801 -0.453
## # ℹ 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.312 0.21 0.235 0.228 0.167 ...