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] 151 182 98 329 516 998 768 788 83 827 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 151 211 879 631 967 229 456 846 778 618
## [2,] 182 628 154 24 526 112 42 44 756 296
## [3,] 98 105 204 927 880 290 193 152 665 906
## [4,] 329 435 163 414 26 281 804 128 821 758
## [5,] 516 430 130 35 392 129 505 568 929 163
## [6,] 998 476 7 871 462 171 613 17 372 695
## [7,] 768 388 876 161 690 531 17 294 871 904
## [8,] 788 698 438 544 637 493 578 303 477 560
## [9,] 83 804 680 732 770 710 542 505 163 382
## [10,] 827 231 832 477 146 619 419 578 400 416
## [11,] 207 684 955 694 580 982 453 260 813 481
## [12,] 115 373 712 956 977 862 103 893 312 258
## [13,] 573 975 912 402 300 985 183 355 964 226
## [14,] 397 603 835 430 629 420 186 163 929 185
## [15,] 230 57 170 231 192 509 98 927 485 983
## [16,] 985 306 543 914 871 161 998 38 876 369
## [17,] 876 531 7 497 68 296 161 768 455 401
## [18,] 912 594 190 468 949 801 820 658 300 498
## [19,] 807 404 735 400 697 319 205 373 920 320
## [20,] 355 805 479 825 985 38 396 161 16 871
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.83 3.07 2.76 3.18 4.26 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.830166 3.118466 3.309226 3.417577 3.470278 3.473444 3.514027 3.573422
## [2,] 3.069267 3.148104 3.338633 3.472081 3.482895 3.569087 3.599660 3.617268
## [3,] 2.756064 3.101829 3.208460 3.240576 3.340364 3.379686 3.380470 3.409709
## [4,] 3.178829 3.374849 3.593361 3.624553 3.671397 3.776295 3.850098 3.853116
## [5,] 4.263774 4.359537 4.392323 4.449738 4.471956 4.574700 4.610302 4.611345
## [6,] 2.995882 3.238647 3.863275 3.933989 4.090073 4.096591 4.205574 4.272677
## [7,] 3.306132 3.319166 3.462312 3.478612 3.495325 3.584387 3.591139 3.655244
## [8,] 3.900709 4.359579 4.370300 4.377665 4.443450 4.444762 4.544791 4.557106
## [9,] 3.471128 3.631353 3.834755 3.836526 3.849196 3.913398 3.962134 4.153947
## [10,] 2.898468 3.268903 3.350932 3.380427 3.411248 3.450779 3.513848 3.534031
## [11,] 3.183397 3.205816 3.242520 3.278325 3.421960 3.475945 3.640327 3.876835
## [12,] 2.871365 3.102100 3.109491 3.547660 3.580165 3.672785 3.693242 3.719736
## [13,] 3.461833 4.020903 4.048636 4.128494 4.143845 4.172098 4.260912 4.331258
## [14,] 4.061455 4.304531 4.321065 4.435504 4.498026 4.509705 4.525498 4.608804
## [15,] 2.396558 3.103443 3.105622 3.259011 3.324790 3.459256 3.467396 3.479397
## [16,] 2.912644 3.025148 3.175566 3.201198 3.215821 3.244561 3.285467 3.411283
## [17,] 3.136938 3.339877 3.591139 3.694872 3.746387 3.752660 3.760497 3.886972
## [18,] 3.859810 3.882399 3.910523 4.090203 4.100428 4.168162 4.175512 4.199732
## [19,] 2.761806 2.800912 2.865841 2.954585 2.999526 3.034984 3.064435 3.111426
## [20,] 3.458017 3.485632 3.550795 3.611620 3.653345 3.700618 3.732034 3.739261
## [,9] [,10]
## [1,] 3.582380 3.619918
## [2,] 3.635197 3.862350
## [3,] 3.428570 3.453297
## [4,] 3.967070 4.100474
## [5,] 4.723101 4.796276
## [6,] 4.302237 4.326643
## [7,] 3.678692 3.718329
## [8,] 4.573960 4.582614
## [9,] 4.171481 4.231706
## [10,] 3.550266 3.579120
## [11,] 3.882930 3.912304
## [12,] 3.725640 3.734865
## [13,] 4.365954 4.384890
## [14,] 4.716813 4.728539
## [15,] 3.554798 3.653757
## [16,] 3.412400 3.427536
## [17,] 3.896328 3.919972
## [18,] 4.275730 4.358250
## [19,] 3.116395 3.119821
## [20,] 3.819886 3.825954
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.484 1 0.988
## 2 0.906 0.985 0.985
## 3 0.918 0.651 0.934
## 4 0.875 0.872 0.972
## 5 0.760 0.952 0.984
## 6 0.938 0.907 0.954
## 7 0.888 0.848 0.989
## 8 0.533 0.736 0.972
## 9 0.982 0.886 0.972
## 10 0.586 0.736 0.972
## # ℹ 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.219 -0.126 0.0327 0.456
## 2 -0.00923 -0.0951 -0.0519 -0.802
## 3 -0.308 -0.503 -0.592 0.431
## 4 -0.185 0.101 -0.121 -0.442
## 5 -0.288 0.929 0.653 0.654
## 6 0.486 0.946 0.318 0.123
## 7 -0.131 -0.106 -0.294 0.683
## 8 0.640 0.203 0.757 0.141
## 9 -0.113 -0.176 0.456 -0.695
## 10 0.396 -0.107 -0.0873 -0.144
## # ℹ 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.268 0.255 0.281 0.234 0.202 ...