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] 76 932 198 574 324 429 360 138 796 274 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 76 567 674 292 125 714 947 739 995 613
## [2,] 932 592 418 315 172 290 291 500 743 624
## [3,] 198 212 376 60 295 992 729 31 671 303
## [4,] 574 548 766 435 166 944 997 798 506 488
## [5,] 324 395 479 716 437 886 884 929 904 805
## [6,] 429 358 316 586 348 173 715 846 976 335
## [7,] 360 540 259 974 797 882 808 903 371 39
## [8,] 138 810 682 29 96 322 830 27 887 37
## [9,] 796 204 276 398 512 23 244 562 154 219
## [10,] 274 877 728 299 433 560 296 639 977 419
## [11,] 343 660 527 994 404 344 598 42 546 831
## [12,] 851 858 20 521 181 75 824 986 948 630
## [13,] 54 897 191 546 319 714 672 379 217 344
## [14,] 506 153 693 735 992 397 642 630 547 295
## [15,] 302 40 480 711 482 76 415 739 44 152
## [16,] 792 951 127 7 859 540 572 331 917 188
## [17,] 938 736 770 340 997 627 506 845 533 585
## [18,] 99 776 289 903 852 622 311 261 61 932
## [19,] 614 958 277 33 291 591 202 990 475 316
## [20,] 802 212 3 735 273 992 295 991 204 729
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.9 4.09 2.52 3.65 4.11 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 2.903163 3.190725 3.346728 3.543066 3.587619 3.602325 3.607594
## [2,] 4.093142 4.241363 4.253033 4.277992 4.306909 4.333221 4.372952
## [3,] 2.523659 2.867231 3.178829 3.318162 3.390046 3.518837 3.568717
## [4,] 3.648497 4.285868 4.428155 4.560822 4.664446 4.706743 4.804098
## [5,] 4.114331 4.144629 4.400274 4.450696 4.570147 4.620270 4.705938
## [6,] 3.018432 3.206609 3.290427 3.443968 3.447538 3.463247 3.481977
## [7,] 2.595939 2.748842 2.869548 2.871399 2.879931 2.881293 2.972628
## [8,] 3.420631 3.756897 3.773349 3.806209 3.859219 3.859671 3.870613
## [9,] 4.535107 4.842285 4.933615 5.094219 5.145122 5.301940 5.307418
## [10,] 4.291241 4.429173 4.509749 4.796773 5.065862 5.317487 5.687485
## [11,] 4.508172 4.516729 4.548852 4.597105 4.679682 4.775977 4.813346
## [12,] 4.916860 4.982559 5.136715 5.143827 5.191339 5.206551 5.291771
## [13,] 3.238647 3.248423 3.793809 3.939331 4.059348 4.061989 4.116736
## [14,] 3.096696 3.842865 3.904964 4.031992 4.142554 4.318562 4.352212
## [15,] 3.897873 3.912981 3.954685 4.008490 4.072789 4.135402 4.205364
## [16,] 3.832822 4.127802 4.208518 4.216739 4.227662 4.264990 4.294982
## [17,] 5.096301 5.203686 5.312953 5.346273 5.455912 5.604068 5.623401
## [18,] 3.101829 3.102051 3.208460 3.274835 3.282958 3.340364 3.380470
## [19,] 4.109448 4.163996 4.278298 4.292908 4.325495 4.392679 4.426677
## [20,] 3.703559 3.898640 3.950076 4.023790 4.078480 4.188525 4.241100
## [,8] [,9] [,10]
## [1,] 3.704443 3.732782 3.734646
## [2,] 4.382291 4.459908 4.546279
## [3,] 3.697669 3.710303 3.729892
## [4,] 4.844865 4.856048 4.930607
## [5,] 4.753374 4.924406 5.008210
## [6,] 3.491684 3.578993 3.609493
## [7,] 2.993510 3.022016 3.144814
## [8,] 3.912247 3.935354 3.958678
## [9,] 5.322587 5.334413 5.459404
## [10,] 5.750151 5.927361 5.935412
## [11,] 4.886832 4.983323 5.205886
## [12,] 5.392086 5.445631 5.451377
## [13,] 4.129074 4.185224 4.185250
## [14,] 4.445199 4.529453 4.536179
## [15,] 4.214673 4.218315 4.223790
## [16,] 4.304390 4.317096 4.372530
## [17,] 5.651117 5.684103 5.825025
## [18,] 3.432843 3.443903 3.474294
## [19,] 4.449122 4.478456 4.487642
## [20,] 4.290163 4.364901 4.390428
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.997 0.889 0.665 0.994
## 2 0.978 0.932 0.975 0.994
## 3 0.978 0.751 0.957 1
## 4 0.978 0.974 1 0.994
## 5 0.978 0.936 0.945 0.955
## 6 0.978 0.761 0.808 0.974
## 7 0.978 0.811 0.612 0.974
## 8 0.978 0.896 0.830 1
## 9 0.978 0.856 0.709 0.994
## 10 0.978 0.936 0.886 0.929
## # ... 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.203 -0.105 -0.347 -0.137
## 2 -0.0851 -0.0979 -0.178 -0.258
## 3 -0.0785 -0.00929 -0.00777 -0.802
## 4 -0.0363 -0.184 -0.0884 0.733
## 5 -0.358 -0.252 -0.000680 0.181
## 6 -0.0855 -0.406 -0.487 -0.833
## 7 -0.262 -0.335 0.212 -0.866
## 8 -0.0575 -0.166 -0.232 0.201
## 9 -0.0398 -0.0000977 -0.615 -0.474
## 10 -0.125 -0.0787 -0.174 -1.12
## # ... 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.267 0.217 0.256 0.198 0.201 ...