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] 699 446 672 713 378 103 990 215 478 855 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 699 267 343 825 377 520 425 903 907 700
## [2,] 446 669 607 648 113 272 643 368 495 296
## [3,] 672 766 818 331 408 145 638 553 801 310
## [4,] 713 631 47 768 819 194 851 920 193 998
## [5,] 378 74 392 558 449 465 924 472 105 911
## [6,] 103 840 956 522 99 27 520 639 871 489
## [7,] 990 851 4 672 47 65 193 128 983 713
## [8,] 215 635 247 699 471 827 545 161 442 795
## [9,] 478 18 682 532 378 34 558 538 858 392
## [10,] 855 920 212 970 779 819 224 430 752 625
## [11,] 403 782 856 371 998 749 711 819 14 523
## [12,] 940 852 525 107 982 248 989 393 29 119
## [13,] 520 669 566 218 387 138 550 261 1 871
## [14,] 933 403 11 998 856 874 596 782 224 445
## [15,] 840 176 524 284 903 570 794 272 306 949
## [16,] 519 402 61 558 503 158 123 465 378 688
## [17,] 76 746 881 803 642 222 90 282 609 268
## [18,] 9 621 459 778 915 34 963 558 538 549
## [19,] 436 911 778 627 34 158 859 605 858 519
## [20,] 274 583 302 417 584 190 974 537 722 71
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.84 4.48 2.95 2.72 3.73 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.839818 3.019769 3.061995 3.123004 3.185397 3.211395 3.211940 3.237000
## [2,] 4.477855 4.512164 4.550462 4.550679 4.565812 4.681746 4.719068 4.735083
## [3,] 2.951628 3.037363 3.048558 3.241036 3.260322 3.343378 3.354560 3.406365
## [4,] 2.722951 2.842039 2.874365 2.967493 2.992920 3.017727 3.078796 3.096412
## [5,] 3.732413 4.764091 4.930693 4.975502 4.982312 5.093407 5.219101 5.220080
## [6,] 3.529149 3.603576 3.658713 3.682184 3.731102 3.743560 3.837096 3.846615
## [7,] 3.195627 3.244952 3.269440 3.275306 3.341227 3.402837 3.474173 3.489659
## [8,] 2.399169 3.165788 3.324877 3.524343 3.536828 3.561370 3.664144 3.676124
## [9,] 3.215614 3.576371 3.774560 4.396100 4.409877 4.620142 4.668892 4.681892
## [10,] 2.954565 2.988297 3.020797 3.049526 3.089964 3.141279 3.306521 3.339356
## [11,] 2.397645 2.798396 2.950448 2.965590 3.009530 3.109029 3.238542 3.280094
## [12,] 4.119439 4.215966 4.224265 4.656054 4.663500 4.746444 4.781532 4.858040
## [13,] 3.711839 3.907756 3.964631 4.022863 4.027375 4.065656 4.093712 4.106356
## [14,] 2.971382 3.084338 3.301510 3.374281 3.427643 3.489395 3.521066 3.635662
## [15,] 2.461213 3.137766 3.220843 3.308511 3.336355 3.368790 3.409879 3.417319
## [16,] 3.923602 4.001273 4.373896 4.439934 4.501913 4.516977 4.593997 4.647018
## [17,] 3.149285 3.595509 3.990705 4.113093 4.118880 4.119479 4.154497 4.197147
## [18,] 3.576371 4.386751 4.993707 5.199900 5.219471 5.268104 5.365884 5.517923
## [19,] 4.474719 4.529364 4.718148 4.788508 4.810910 4.872632 4.885538 4.913037
## [20,] 3.381325 4.287278 4.289126 4.460192 4.480005 4.605682 4.669324 4.676081
## [,9] [,10]
## [1,] 3.350592 3.358886
## [2,] 4.736092 4.778492
## [3,] 3.416937 3.443737
## [4,] 3.153086 3.170707
## [5,] 5.315915 5.322156
## [6,] 3.853468 3.882920
## [7,] 3.500995 3.553795
## [8,] 3.699885 3.792353
## [9,] 4.767480 4.769514
## [10,] 3.407447 3.408916
## [11,] 3.301510 3.323589
## [12,] 4.893656 4.922133
## [13,] 4.114466 4.145067
## [14,] 3.717587 3.757225
## [15,] 3.417989 3.440920
## [16,] 4.678632 4.730774
## [17,] 4.243374 4.262171
## [18,] 5.524044 5.525519
## [19,] 4.945290 4.949405
## [20,] 4.707251 4.728339
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.991 1 0.966
## 2 0.991 1 0.919
## 3 0.991 1 0.698
## 4 0.991 1 1
## 5 0.991 0.984 0.919
## 6 0.991 0.997 0.919
## 7 0.993 0.997 0.981
## 8 0.993 0.878 1
## 9 0.991 0.720 0.919
## 10 0.991 0.997 0.860
## # ℹ 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.352 -0.137 -0.458 -0.859
## 2 -0.0179 -0.0305 -0.663 -0.897
## 3 -0.255 0.179 -0.241 -1.58
## 4 -0.0695 -0.197 -0.213 0.774
## 5 0.539 -0.240 -0.312 0.132
## 6 -0.0160 -0.136 -0.204 -0.392
## 7 -0.275 -0.295 -0.633 0.187
## 8 -0.180 -0.255 -0.0147 -0.455
## 9 -0.409 -0.277 -0.137 -1.11
## 10 -0.704 -0.358 -0.886 0.254
## # ℹ 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.293 0.207 0.282 0.31 0.187 ...