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] 842 755 42 480 255 182 814 51 307 145 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 842 753 145 39 869 6 763 80 505 935
## [2,] 755 574 659 499 875 320 686 153 821 685
## [3,] 42 301 734 643 60 319 113 321 665 281
## [4,] 480 208 327 530 413 177 711 770 324 124
## [5,] 255 681 369 786 729 520 567 209 544 893
## [6,] 182 612 457 771 496 903 524 80 506 145
## [7,] 814 924 403 256 445 619 384 49 411 557
## [8,] 51 433 52 224 678 17 239 803 69 92
## [9,] 307 674 986 408 140 60 267 685 269 379
## [10,] 145 682 753 1 871 727 175 933 388 512
## [11,] 696 34 270 110 139 20 584 171 675 604
## [12,] 64 76 868 264 110 758 309 34 604 282
## [13,] 771 80 239 505 306 145 640 700 6 608
## [14,] 472 620 591 329 492 583 788 418 559 105
## [15,] 505 763 829 80 753 347 800 488 552 182
## [16,] 234 324 219 770 368 544 786 327 804 728
## [17,] 51 239 224 8 92 771 52 46 404 935
## [18,] 690 381 119 43 289 692 107 442 800 481
## [19,] 630 431 275 684 860 109 853 488 266 362
## [20,] 270 372 924 838 348 156 888 953 316 703
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.04 2.78 2.86 3.47 3.81 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.037948 3.244502 3.335536 3.453594 3.460411 3.573983 3.624240 3.639211
## [2,] 2.783608 2.794155 2.800484 2.855793 2.917053 2.989920 3.023547 3.043650
## [3,] 2.857515 2.896975 2.978146 3.145483 3.200580 3.233042 3.255954 3.264566
## [4,] 3.471605 3.491925 3.735966 3.958095 4.057822 4.120862 4.172630 4.193240
## [5,] 3.810307 3.862040 3.869815 4.085290 4.104447 4.200690 4.344866 4.496911
## [6,] 2.789619 2.951655 2.953268 2.972916 3.012184 3.079854 3.080089 3.104128
## [7,] 2.994185 3.127637 3.165548 3.251515 3.331643 3.524829 3.548353 3.580015
## [8,] 2.594500 3.075997 3.094221 3.136652 3.260248 3.331167 3.348188 3.393072
## [9,] 2.355356 2.880144 2.908774 2.919602 2.931802 2.977181 2.977382 3.015299
## [10,] 3.226267 3.923602 4.382208 4.423305 4.461656 4.607250 4.647018 4.673065
## [11,] 3.819593 3.912981 3.948973 3.998455 4.072789 4.135402 4.139994 4.205364
## [12,] 3.823747 4.097273 4.266755 4.304861 4.496306 4.499253 4.535784 4.553176
## [13,] 3.615463 3.753193 3.800756 3.981088 3.984069 4.124776 4.126504 4.168339
## [14,] 4.413266 4.676431 4.838354 4.895596 4.896079 4.908071 4.922836 4.952781
## [15,] 3.116733 3.116881 3.618006 3.629561 3.660118 3.735694 3.764790 3.771961
## [16,] 3.178829 3.374849 3.624553 3.850098 3.900636 4.100474 4.247132 4.247648
## [17,] 3.061246 3.262575 3.307469 3.331167 3.485061 3.543151 3.550177 3.569898
## [18,] 5.989672 6.197935 6.322531 6.343355 6.373304 6.382418 6.572248 6.585526
## [19,] 4.131753 4.153654 4.163283 4.221588 4.254944 4.263736 4.264703 4.269187
## [20,] 2.183445 2.213633 2.467871 2.548933 2.564574 2.696517 2.840044 3.011425
## [,9] [,10]
## [1,] 3.642213 3.726925
## [2,] 3.091964 3.124100
## [3,] 3.307652 3.316546
## [4,] 4.281796 4.316173
## [5,] 4.515263 4.516146
## [6,] 3.256362 3.315259
## [7,] 3.636292 3.731713
## [8,] 3.487505 3.507170
## [9,] 3.069113 3.074698
## [10,] 4.678632 4.730774
## [11,] 4.235914 4.289111
## [12,] 4.613245 4.689177
## [13,] 4.231129 4.246725
## [14,] 4.960942 5.005160
## [15,] 3.858373 3.878480
## [16,] 4.303545 4.305770
## [17,] 3.607025 3.639282
## [18,] 6.593048 6.613165
## [19,] 4.313082 4.336712
## [20,] 3.059421 3.063167
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.973 1 0.947
## 2 0.973 1 0.955
## 3 0.991 1 0.896
## 4 0.973 1 0.900
## 5 0.973 1 0.897
## 6 0.973 1 0.901
## 7 0.973 1 0.706
## 8 0.973 1 0.706
## 9 0.973 1 0.999
## 10 0.973 1 0.897
## # ℹ 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.218 -0.0910 -0.198 0.145
## 2 -0.203 -0.162 -0.360 0.346
## 3 -0.410 -0.179 -0.453 -0.810
## 4 -0.218 -0.0921 -0.259 -0.359
## 5 -0.115 -0.00632 -0.459 0.626
## 6 -0.0893 -0.0653 -0.0490 -0.135
## 7 -0.237 -0.189 0.0316 -0.142
## 8 -0.0228 -0.188 -0.207 0.441
## 9 -0.402 -0.356 -0.397 -0.883
## 10 -0.151 -0.0659 -0.236 0.309
## # ℹ 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.26 0.313 0.287 0.227 0.213 ...