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] 897 855 72 957 937 438 194 338 369 320 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 897 281 248 611 754 962 134 894 983 233
## [2,] 855 278 716 235 392 904 72 812 873 917
## [3,] 72 57 516 61 830 291 856 993 166 768
## [4,] 957 234 443 237 59 990 27 841 566 224
## [5,] 937 354 543 560 566 491 639 176 109 286
## [6,] 438 295 542 795 311 86 569 483 684 280
## [7,] 194 422 362 638 440 576 807 538 527 774
## [8,] 338 329 590 533 23 520 598 641 571 239
## [9,] 369 684 907 87 918 463 54 986 69 910
## [10,] 320 326 698 236 416 396 157 131 820 308
## [11,] 881 615 281 834 414 71 593 691 106 178
## [12,] 410 283 388 687 538 197 236 467 48 242
## [13,] 96 262 800 506 936 122 908 213 131 32
## [14,] 630 804 985 59 968 117 330 4 841 957
## [15,] 446 947 162 738 848 918 910 279 566 241
## [16,] 881 91 637 471 922 958 53 834 281 997
## [17,] 273 565 296 755 884 398 955 131 654 320
## [18,] 834 691 212 809 123 881 744 922 888 851
## [19,] 358 839 122 547 816 515 257 406 640 604
## [20,] 293 743 290 724 657 250 759 188 313 882
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.55 3.92 3.36 2.47 2.21 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.551057 2.877558 3.149285 3.359316 3.636517 3.670123 3.673624 3.676229
## [2,] 3.924930 4.020163 4.329155 4.502649 4.511740 4.601139 4.612490 4.701388
## [3,] 3.363588 3.453381 3.554492 3.636421 3.657169 3.700616 3.761648 3.798015
## [4,] 2.474032 2.908386 2.956495 2.960426 2.969394 3.055646 3.252302 3.252539
## [5,] 2.206203 2.756434 2.829583 3.034378 3.093143 3.129549 3.197459 3.241631
## [6,] 3.905281 4.209103 4.245753 4.711475 4.723569 4.775084 4.775266 4.778753
## [7,] 2.548095 2.671363 2.843630 2.922730 3.015991 3.061599 3.179717 3.208087
## [8,] 3.911108 4.140878 4.229922 4.398263 4.408850 4.423437 4.455586 4.468671
## [9,] 3.097720 4.202864 4.206411 4.207700 4.414333 4.515455 4.621298 4.661396
## [10,] 2.668344 2.735375 3.030267 3.053310 3.090663 3.109112 3.114050 3.122590
## [11,] 3.028344 3.747673 3.958279 4.011496 4.038856 4.106526 4.110042 4.214606
## [12,] 2.857515 2.896975 2.945146 2.978146 3.126776 3.200580 3.217015 3.264566
## [13,] 4.841182 4.928045 4.951130 4.993592 5.019089 5.032813 5.084089 5.150506
## [14,] 3.849132 3.890322 4.520806 4.597226 4.742255 4.766004 4.890565 4.899291
## [15,] 2.832967 2.966594 2.978875 3.035715 3.064958 3.259878 3.317191 3.339832
## [16,] 4.520879 4.658434 4.907575 5.006399 5.102403 5.159696 5.375401 5.404872
## [17,] 3.351930 3.756897 3.768833 3.782414 3.800538 3.806209 3.820494 3.859219
## [18,] 4.067747 4.321824 4.353073 4.419587 4.529473 4.646032 4.739847 4.745397
## [19,] 3.486064 3.505017 3.516958 3.559220 3.628879 3.649255 3.656602 3.678844
## [20,] 3.603838 3.720459 3.761407 3.850478 3.875571 3.898668 4.049438 4.258111
## [,9] [,10]
## [1,] 3.748873 3.794629
## [2,] 4.713851 4.731090
## [3,] 3.800711 3.856201
## [4,] 3.257129 3.308979
## [5,] 3.252635 3.266114
## [6,] 4.804957 4.851907
## [7,] 3.234322 3.287074
## [8,] 4.496290 4.547326
## [9,] 4.671356 4.826079
## [10,] 3.146573 3.172517
## [11,] 4.353556 4.391577
## [12,] 3.305846 3.316546
## [13,] 5.160283 5.165224
## [14,] 5.004431 5.025780
## [15,] 3.405926 3.435292
## [16,] 5.459774 5.524838
## [17,] 3.870613 3.958678
## [18,] 4.752040 4.858189
## [19,] 3.689623 3.770356
## [20,] 4.401926 4.443299
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.IL… `pCREB(Yb176)Di.IL… `pBTK(Yb171)Di.IL… `pS6(Yb172)Di.IL7…
## <dbl> <dbl> <dbl> <dbl>
## 1 1 1 0.714 0.868
## 2 1 1 0.837 0.878
## 3 1 1 0.968 0.775
## 4 1 1 0.884 0.955
## 5 1 1 0.929 0.985
## 6 1 1 0.763 0.985
## 7 1 1 0.944 0.894
## 8 1 0.387 0.954 0.912
## 9 1 1 0.973 0.899
## 10 1 0.903 0.751 0.899
## # … 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(I… `CD3(Cd114)Di`
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 -0.212 -0.0522 0.302 0.479 0.346
## 2 0.771 -0.0114 -0.226 0.760 -0.360
## 3 -0.218 -0.0773 0.369 -0.167 -0.384
## 4 -0.0854 -0.247 0.780 -0.302 -0.581
## 5 -0.236 -0.109 0.00696 -1.02 0.734
## 6 -0.234 -0.273 -1.18 -1.08 -0.873
## 7 0.782 0.779 0.143 0.0405 0.115
## 8 -1.38 -1.14 -1.33 0.799 -1.03
## 9 -0.452 -0.231 -0.304 -0.543 -0.370
## 10 -0.204 -0.0512 -0.240 -1.90 -0.375
## # … with 20 more rows, and 46 more variables: 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.261 0.209 0.252 0.297 0.3 ...