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] 748 357 639 206 890 862 532 212 547 704 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 748 797 503 407 563 98 374 607 570 114
## [2,] 357 282 515 286 17 886 903 244 465 536
## [3,] 639 887 4 560 554 441 148 578 370 585
## [4,] 206 947 578 547 815 85 533 3 731 497
## [5,] 890 400 94 99 675 455 965 810 823 540
## [6,] 862 700 156 14 452 720 881 437 59 896
## [7,] 532 725 106 823 408 216 577 394 99 159
## [8,] 212 179 100 71 286 17 515 2 410 465
## [9,] 547 815 319 914 965 873 544 875 731 533
## [10,] 704 96 428 571 949 494 140 287 952 128
## [11,] 341 500 806 126 178 379 821 959 140 923
## [12,] 365 928 373 542 755 983 524 79 706 227
## [13,] 801 228 276 602 568 893 73 698 828 143
## [14,] 768 875 764 236 705 753 720 991 619 108
## [15,] 923 140 959 663 851 582 126 235 128 603
## [16,] 228 67 1000 893 300 20 229 73 80 371
## [17,] 2 778 536 286 8 357 282 212 100 929
## [18,] 721 825 436 873 588 644 69 57 470 104
## [19,] 412 68 709 136 657 522 267 300 535 108
## [20,] 893 1000 796 772 602 188 936 227 67 80
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.75 2.55 3.69 3.28 3.48 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 3.747977 3.993102 4.014178 4.190158 4.444663 4.465577 4.538928
## [2,] 2.554459 2.618296 3.174172 3.265561 3.679249 3.785996 3.909199
## [3,] 3.685447 3.730388 3.731102 3.779581 3.843437 3.870212 3.878723
## [4,] 3.279469 3.444464 3.510274 3.545277 3.603576 3.607961 3.682184
## [5,] 3.475765 3.588612 3.622264 3.624838 3.740483 3.763912 3.822214
## [6,] 3.857160 3.898985 4.026514 4.270014 4.276164 4.291213 4.390021
## [7,] 2.758565 2.998026 3.137071 3.229995 3.292420 3.358817 3.412648
## [8,] 3.388935 3.582441 3.738259 3.814607 4.066634 4.146279 4.150085
## [9,] 2.525279 2.584284 3.031190 3.071461 3.080451 3.084824 3.096479
## [10,] 4.225018 4.237876 4.617088 4.699386 4.757414 4.861939 4.920620
## [11,] 4.355181 4.392323 4.445316 4.449738 4.471956 4.578365 4.611345
## [12,] 3.289579 3.394217 3.465452 3.498359 3.515097 3.585324 3.664394
## [13,] 3.408984 3.460197 3.498565 3.556694 3.568768 3.573260 3.586969
## [14,] 3.281136 3.309226 3.417577 3.514027 3.548161 3.552353 3.573422
## [15,] 4.028213 4.211292 4.352254 4.359839 4.398015 4.409507 4.488994
## [16,] 2.857515 2.896975 2.978146 3.198153 3.217015 3.233042 3.305846
## [17,] 3.679249 3.904158 3.943316 3.986689 4.146279 4.236226 4.253201
## [18,] 2.810474 2.824842 2.843630 3.102102 3.230254 3.241720 3.329606
## [19,] 3.370914 3.654803 3.694001 3.821292 3.851958 3.855553 3.923677
## [20,] 2.071327 2.180085 2.330242 2.620281 2.637831 2.648087 2.648569
## [,8] [,9] [,10]
## [1,] 4.554106 4.589350 4.598470
## [2,] 3.928391 3.945356 3.997474
## [3,] 3.906250 3.922677 4.049898
## [4,] 3.731102 3.837096 3.839162
## [5,] 3.830374 3.849046 3.883309
## [6,] 4.396711 4.460467 4.474204
## [7,] 3.453405 3.589407 3.615929
## [8,] 4.198159 4.323566 4.625197
## [9,] 3.136830 3.164047 3.236257
## [10,] 4.921028 5.082532 5.089164
## [11,] 4.724322 4.796276 4.802364
## [12,] 3.691435 3.787543 3.793872
## [13,] 3.610366 3.633601 3.654356
## [14,] 3.582380 3.598714 3.661306
## [15,] 4.531708 4.549111 4.642835
## [16,] 3.330853 3.368265 3.418513
## [17,] 4.313055 4.395033 4.469473
## [18,] 3.333952 3.349297 3.358645
## [19,] 3.958140 4.021636 4.029057
## [20,] 2.668764 2.759300 2.760765
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 1 1 0.945 0.719
## 2 1 1 1 0.975
## 3 1 1 0.948 0.899
## 4 1 1 0.968 0.971
## 5 1 1 0.805 0.814
## 6 1 1 0.748 0.806
## 7 1 1 1 0.985
## 8 1 0.943 1 1
## 9 1 1 0.698 0.971
## 10 1 0.883 1 0.674
## # ... 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.358 -0.126 -0.0997 -1.14
## 2 -1.13 -0.888 0.165 -1.49
## 3 -1.06 -1.16 -1.39 -1.54
## 4 -0.660 -0.364 -0.141 0.450
## 5 -0.0337 -0.117 -0.583 0.0137
## 6 -0.207 -0.184 -0.562 -2.39
## 7 -0.0894 -0.309 -0.000283 0.253
## 8 -0.0638 -0.235 -0.265 -0.240
## 9 -0.138 1.73 -0.0919 -0.000499
## 10 -0.692 -0.0685 0.141 -0.633
## # ... 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.218 0.232 0.241 0.26 0.255 ...