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] 222 509 790 245 796 502 538 508 50 317 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 222 821 910 578 307 250 748 152 624 996
## [2,] 509 315 630 308 120 940 177 39 564 379
## [3,] 790 773 365 43 820 450 482 940 177 713
## [4,] 245 545 605 885 509 843 183 616 76 727
## [5,] 796 872 209 934 148 1000 815 100 700 719
## [6,] 502 430 877 905 56 355 721 872 79 148
## [7,] 538 456 356 674 613 336 749 462 726 874
## [8,] 508 763 800 882 978 858 731 392 929 698
## [9,] 50 903 864 804 531 49 778 260 924 460
## [10,] 317 886 376 799 366 251 946 550 165 656
## [11,] 960 152 64 125 447 571 785 222 683 619
## [12,] 80 184 172 223 14 637 385 245 703 280
## [13,] 614 56 562 930 744 872 852 690 780 576
## [14,] 80 711 210 110 855 227 669 600 172 382
## [15,] 987 313 970 984 527 270 463 348 912 732
## [16,] 744 353 805 264 804 765 754 998 529 615
## [17,] 213 528 185 600 512 972 648 349 753 38
## [18,] 843 250 577 990 365 466 398 170 125 564
## [19,] 110 210 80 492 44 166 902 184 711 514
## [20,] 600 687 12 14 175 245 878 855 274 952
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.95 3.64 3.04 3.29 2.53 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 3.947773 4.321813 4.367830 4.554976 4.571980 4.623731 4.648086
## [2,] 3.642712 3.649257 3.652448 3.745536 3.786412 3.790938 3.822202
## [3,] 3.037948 3.244502 3.302855 3.344558 3.382285 3.417346 3.419764
## [4,] 3.287269 3.315326 3.494212 3.535462 3.562311 3.626131 3.634984
## [5,] 2.527691 2.881735 2.927257 2.972622 3.030286 3.075083 3.119821
## [6,] 3.016322 3.263121 3.309315 3.314453 3.330960 3.373050 3.385717
## [7,] 5.003977 5.061926 5.185661 5.254434 5.382539 5.397472 5.491799
## [8,] 3.642857 4.050647 4.404500 5.120571 5.317012 5.425431 5.445113
## [9,] 3.510749 3.529860 3.540246 3.540643 3.559376 3.631369 3.788734
## [10,] 3.874611 3.895141 3.905281 3.920055 3.969588 3.980752 3.992020
## [11,] 4.250474 4.481757 4.551554 4.552649 4.637174 4.680834 4.697840
## [12,] 2.781037 3.011127 3.072591 3.122978 3.214481 3.295270 3.303830
## [13,] 3.258224 3.489008 3.635788 3.820010 3.826104 3.867156 3.885381
## [14,] 2.564697 2.608074 2.767854 2.806661 2.987004 2.991700 2.993643
## [15,] 4.257666 4.947874 4.997540 5.131117 5.234520 5.237404 5.259828
## [16,] 3.997052 4.060528 4.122820 4.244086 4.262393 4.335013 4.460616
## [17,] 2.803685 3.575070 3.602938 3.749763 3.918905 3.940382 4.090389
## [18,] 2.795223 2.979887 3.090327 3.105851 3.137071 3.262575 3.279787
## [19,] 2.850529 3.164105 3.172309 3.188833 3.221579 3.397758 3.421214
## [20,] 3.160723 3.540613 3.558196 3.593978 3.646674 3.652203 3.698915
## [,8] [,9] [,10]
## [1,] 4.648924 4.685547 4.694246
## [2,] 3.824388 3.827633 3.829362
## [3,] 3.460411 3.573983 3.642213
## [4,] 3.661115 3.726571 3.813664
## [5,] 3.163289 3.190914 3.238994
## [6,] 3.428579 3.599161 3.645284
## [7,] 5.554961 5.601588 5.630743
## [8,] 5.646037 5.699146 6.003307
## [9,] 3.845087 3.911953 3.915101
## [10,] 3.998920 4.013817 4.032743
## [11,] 4.699965 4.860398 4.861193
## [12,] 3.336839 3.372531 3.399645
## [13,] 3.911072 3.952541 3.977788
## [14,] 3.122293 3.143366 3.151570
## [15,] 5.323673 5.352525 5.454072
## [16,] 4.472868 4.532881 4.559057
## [17,] 4.102293 4.107369 4.153447
## [18,] 3.348188 3.365303 3.369229
## [19,] 3.422338 3.443828 3.452237
## [20,] 3.736120 3.761742 3.767822
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.641 0.977 0.899 1
## 2 0.733 0.993 0.766 0.916
## 3 0.929 0.975 1 0.596
## 4 0.790 0.977 1 1
## 5 1 1 0.966 0.996
## 6 0.903 1 0.907 0.950
## 7 0.942 1 0.766 1
## 8 0.822 0.977 0.980 1
## 9 0.771 0.953 0.519 0.916
## 10 0.984 0.977 0.908 1
## # … 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.313 -0.0154 0.0303 0.120
## 2 -0.697 -0.789 -0.706 -1.01
## 3 0.272 -0.133 0.572 0.471
## 4 -0.0385 -0.224 -0.194 0.227
## 5 -0.190 -0.0925 -0.479 0.764
## 6 -0.650 -0.0338 -0.779 -1.05
## 7 0.291 -0.161 -0.104 -1.50
## 8 -0.113 -0.0204 -0.118 -0.894
## 9 -0.0733 -0.0195 -0.207 -0.462
## 10 0.360 -0.113 0.894 -0.315
## # … 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.209 0.254 0.264 0.257 0.301 ...