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"
## [3] "CD3(Cd112)Di" "CD235-61-7-15(In113)Di"
## [5] "CD3(Cd114)Di" "CD45(In115)Di"
## [7] "CD19(Nd142)Di" "CD22(Nd143)Di"
## [9] "IgD(Nd145)Di" "CD79b(Nd146)Di"
## [11] "CD20(Sm147)Di" "CD34(Nd148)Di"
## [13] "CD179a(Sm149)Di" "CD72(Eu151)Di"
## [15] "IgM(Eu153)Di" "Kappa(Sm154)Di"
## [17] "CD10(Gd156)Di" "Lambda(Gd157)Di"
## [19] "CD24(Dy161)Di" "TdT(Dy163)Di"
## [21] "Rag1(Dy164)Di" "PreBCR(Ho165)Di"
## [23] "CD43(Er167)Di" "CD38(Er168)Di"
## [25] "CD40(Er170)Di" "CD33(Yb173)Di"
## [27] "HLA-DR(Yb174)Di"
##
## $functional
## [1] "pCrkL(Lu175)Di" "pCREB(Yb176)Di" "pBTK(Yb171)Di"
## [4] "pS6(Yb172)Di" "cPARP(La139)Di" "pPLCg2(Pr141)Di"
## [7] "pSrc(Nd144)Di" "Ki67(Sm152)Di" "pErk12(Gd155)Di"
## [10] "pSTAT3(Gd158)Di" "pAKT(Tb159)Di" "pBLNK(Gd160)Di"
## [13] "pP38(Tm169)Di" "pSTAT5(Nd150)Di" "pSyk(Dy162)Di"
## [16] "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] 209 806 350 77 845 898 811 638 369 383 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 209 691 763 199 884 463 638 292 917 12
## [2,] 806 792 686 422 938 758 591 768 11 633
## [3,] 350 588 776 56 362 639 280 111 226 379
## [4,] 77 414 612 273 297 336 457 820 847 410
## [5,] 845 347 691 699 720 199 159 884 12 763
## [6,] 898 270 778 91 153 640 869 163 331 26
## [7,] 811 750 719 138 446 327 592 564 507 364
## [8,] 638 284 123 356 842 399 999 532 731 953
## [9,] 369 894 73 1000 555 424 329 703 98 759
## [10,] 383 722 864 606 207 262 483 255 256 679
## [11,] 830 792 768 841 897 422 590 633 511 866
## [12,] 347 199 498 92 292 80 463 924 845 991
## [13,] 325 51 819 730 88 666 949 433 825 516
## [14,] 236 887 289 380 81 230 642 687 321 651
## [15,] 366 618 274 308 201 602 302 810 738 620
## [16,] 142 111 246 341 95 600 576 908 233 57
## [17,] 633 682 349 783 792 437 591 441 83 511
## [18,] 803 466 959 775 191 781 197 508 84 594
## [19,] 236 651 513 858 289 595 791 352 107 219
## [20,] 145 336 706 779 477 273 636 300 355 855
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.47 4.26 4.06 4.13 4.17 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 3.468951 3.640917 4.075417 4.131262 4.331332 4.381185 4.456790
## [2,] 4.257666 4.997540 5.163658 5.323673 5.454072 5.470879 5.561278
## [3,] 4.064624 4.169140 4.405604 4.443858 4.472271 4.687441 4.733158
## [4,] 4.126776 4.184986 4.227208 4.363565 4.366096 4.387111 4.536982
## [5,] 4.167909 4.492409 4.555008 4.593553 4.614907 4.635549 4.648565
## [6,] 2.614052 2.780300 2.907041 2.936318 3.092596 3.157273 3.184971
## [7,] 3.183396 3.219186 3.307687 3.447516 3.452982 3.484379 3.498521
## [8,] 4.404500 4.536367 4.637186 4.796344 5.120571 5.307969 5.317012
## [9,] 2.874441 3.546798 3.898668 4.140438 4.295106 4.436593 4.472910
## [10,] 2.912934 3.012112 3.165423 3.230329 3.340812 3.447265 3.597304
## [11,] 3.920033 4.024391 4.220534 4.234330 4.258739 4.308642 4.314411
## [12,] 4.059664 4.191703 4.323342 4.331572 4.515605 4.524020 4.556114
## [13,] 2.616470 2.626894 2.680070 2.901289 2.917199 2.990677 3.029837
## [14,] 3.862732 4.026170 4.114932 4.159887 4.210450 4.247808 4.249349
## [15,] 2.396558 2.781732 2.816422 3.174945 3.200948 3.348340 3.372321
## [16,] 4.133291 4.350369 4.431408 4.457864 4.521023 4.581570 4.675234
## [17,] 3.408298 4.105685 4.237876 4.512711 4.569172 4.714625 4.740133
## [18,] 2.897730 2.899267 3.066022 3.206636 3.294599 3.305260 3.349499
## [19,] 3.132644 3.201004 3.639797 3.831475 3.868862 3.922722 3.997139
## [20,] 3.586636 3.682744 3.800485 3.800792 3.944320 4.024833 4.034886
## [,8] [,9] [,10]
## [1,] 4.552660 4.657486 4.818772
## [2,] 5.605012 5.620357 5.695750
## [3,] 4.824137 4.835348 4.880660
## [4,] 4.594899 4.616670 4.631144
## [5,] 4.740029 4.807212 5.062603
## [6,] 3.269542 3.297179 3.364361
## [7,] 3.513714 3.550226 3.617443
## [8,] 5.324060 5.436872 5.445113
## [9,] 4.506289 4.547244 4.639227
## [10,] 3.667046 3.716201 3.779358
## [11,] 4.343474 4.352183 4.410626
## [12,] 4.610779 4.626337 4.668932
## [13,] 3.067011 3.068922 3.154746
## [14,] 4.259102 4.300524 4.313850
## [15,] 3.400523 3.412980 3.421243
## [16,] 4.715303 4.733182 4.747115
## [17,] 4.991035 5.012193 5.012972
## [18,] 3.407082 3.497627 3.498950
## [19,] 4.010884 4.059065 4.074269
## [20,] 4.040303 4.049032 4.080342
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 x 34
## `pCrkL(Lu175)Di~ `pCREB(Yb176)Di~ `pBTK(Yb171)Di.~ `pS6(Yb172)Di.I~
## <dbl> <dbl> <dbl> <dbl>
## 1 0.955 0.946 1 0.752
## 2 0.924 0.946 1 0.923
## 3 0.824 0.993 1 0.752
## 4 0.869 0.946 1 0.617
## 5 0.993 0.955 1 0.579
## 6 0.937 1 1 0.692
## 7 0.956 0.972 1 0.868
## 8 0.926 0.955 1 0.724
## 9 0.788 0.946 1 0.631
## 10 0.869 0.946 1 0.822
## # ... with 990 more rows, and 30 more variables:
## # `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>, `pAKT(Tb159)Di.IL7.qvalue` <dbl>,
## # `pBLNK(Gd160)Di.IL7.qvalue` <dbl>, `pP38(Tm169)Di.IL7.qvalue` <dbl>,
## # `pSTAT5(Nd150)Di.IL7.qvalue` <dbl>, `pSyk(Dy162)Di.IL7.qvalue` <dbl>,
## # `tIkBa(Er166)Di.IL7.qvalue` <dbl>, `pCrkL(Lu175)Di.IL7.change` <dbl>,
## # `pCREB(Yb176)Di.IL7.change` <dbl>, `pBTK(Yb171)Di.IL7.change` <dbl>,
## # `pS6(Yb172)Di.IL7.change` <dbl>, `cPARP(La139)Di.IL7.change` <dbl>,
## # `pPLCg2(Pr141)Di.IL7.change` <dbl>, `pSrc(Nd144)Di.IL7.change` <dbl>,
## # `Ki67(Sm152)Di.IL7.change` <dbl>, `pErk12(Gd155)Di.IL7.change` <dbl>,
## # `pSTAT3(Gd158)Di.IL7.change` <dbl>, `pAKT(Tb159)Di.IL7.change` <dbl>,
## # `pBLNK(Gd160)Di.IL7.change` <dbl>, `pP38(Tm169)Di.IL7.change` <dbl>,
## # `pSTAT5(Nd150)Di.IL7.change` <dbl>, `pSyk(Dy162)Di.IL7.change` <dbl>,
## # `tIkBa(Er166)Di.IL7.change` <dbl>, IL7.fraction.cond.2 <dbl>,
## # density <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 x 51
## `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(~
## <dbl> <dbl> <dbl> <dbl>
## 1 0.272 -0.133 0.572 0.471
## 2 -0.0797 -0.258 -0.168 0.840
## 3 -0.173 -0.127 -0.149 -0.246
## 4 -0.295 -0.577 -0.119 -0.740
## 5 -0.0385 -0.224 -0.194 0.227
## 6 -0.230 -0.227 -0.117 0.240
## 7 -0.190 -0.0925 -0.479 0.764
## 8 -0.243 -0.229 -0.141 -0.781
## 9 -0.388 0.832 1.07 0.247
## 10 0.291 -0.161 -0.104 -1.50
## # ... with 20 more rows, and 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>, `PreBCR(Ho165)Di` <dbl>, `CD43(Er167)Di` <dbl>,
## # `CD38(Er168)Di` <dbl>, `CD40(Er170)Di` <dbl>, `CD33(Yb173)Di` <dbl>,
## # `HLA-DR(Yb174)Di` <dbl>, Time <dbl>, Cell_length <dbl>,
## # `cPARP(La139)Di` <dbl>, `pPLCg2(Pr141)Di` <dbl>,
## # `pSrc(Nd144)Di` <dbl>, `pSTAT5(Nd150)Di` <dbl>, `Ki67(Sm152)Di` <dbl>,
## # `pErk12(Gd155)Di` <dbl>, `pSTAT3(Gd158)Di` <dbl>,
## # `pAKT(Tb159)Di` <dbl>, `pBLNK(Gd160)Di` <dbl>, `pSyk(Dy162)Di` <dbl>,
## # `tIkBa(Er166)Di` <dbl>, `pP38(Tm169)Di` <dbl>, `pBTK(Yb171)Di` <dbl>,
## # `pS6(Yb172)Di` <dbl>, `pCrkL(Lu175)Di` <dbl>, `pCREB(Yb176)Di` <dbl>,
## # `DNA1(Ir191)Di` <dbl>, `DNA2(Ir193)Di` <dbl>,
## # `Viability1(Pt195)Di` <dbl>, `Viability2(Pt196)Di` <dbl>,
## # wanderlust <dbl>, condition <chr>
# 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.205 0.171 0.202 0.211 0.193 ...