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] 950 818 238 51 481 615 489 659 320 63 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 950 390 333 161 363 316 326 545 923 842
## [2,] 818 687 518 133 742 291 86 677 149 621
## [3,] 238 515 697 546 523 872 403 448 782 863
## [4,] 51 276 272 816 826 723 168 481 391 517
## [5,] 481 937 758 489 390 447 394 388 170 666
## [6,] 615 828 658 116 790 763 782 332 577 489
## [7,] 489 582 171 843 673 948 272 869 137 683
## [8,] 659 211 881 503 98 746 405 863 448 47
## [9,] 320 272 866 835 869 817 170 65 71 350
## [10,] 63 375 515 332 730 403 392 238 272 336
## [11,] 362 778 452 50 478 60 189 529 744 646
## [12,] 975 717 797 271 263 219 987 997 126 722
## [13,] 693 362 728 747 275 498 452 478 711 801
## [14,] 550 805 822 945 294 996 566 429 329 189
## [15,] 537 426 270 438 726 928 82 500 916 874
## [16,] 362 296 899 747 36 510 50 634 961 532
## [17,] 77 927 118 194 382 767 786 360 472 830
## [18,] 789 27 524 442 615 937 9 1 363 476
## [19,] 116 782 790 616 234 615 976 497 868 127
## [20,] 323 192 659 466 308 934 488 66 544 879
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.45 3.18 2.78 2.81 3.97 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.453201 2.470231 3.191709 3.218411 3.255873 3.343012 3.402697 3.412175
## [2,] 3.177718 3.300765 3.383804 3.415477 3.494288 3.496798 3.540498 3.576874
## [3,] 2.781966 2.911382 2.957198 2.970242 2.995856 3.075129 3.125124 3.128377
## [4,] 2.811794 3.320573 3.399586 3.465319 3.472019 3.478667 3.552670 3.595827
## [5,] 3.970646 4.014822 4.082931 4.087671 4.093826 4.171065 4.193373 4.228592
## [6,] 3.092417 3.111860 3.183026 3.218674 3.278997 3.310448 3.358336 3.567453
## [7,] 2.300343 2.532541 2.760352 2.955509 3.040854 3.043363 3.057087 3.124119
## [8,] 5.288472 5.367204 5.700925 5.806172 5.835787 6.070138 6.166682 6.210533
## [9,] 2.907990 3.287269 3.315326 3.349961 3.494212 3.543102 3.634984 3.726571
## [10,] 2.709808 2.728091 2.750523 2.860145 2.894769 2.954442 2.969913 2.979683
## [11,] 3.969115 4.034457 4.039353 4.048233 4.084207 4.138196 4.142253 4.180874
## [12,] 3.133915 3.417192 3.529116 3.966638 4.099818 4.140183 4.164315 4.520699
## [13,] 3.129159 3.622966 3.693368 3.882383 3.936580 3.937566 3.948989 4.009144
## [14,] 3.415029 3.443344 3.511060 3.611068 4.090203 4.175512 4.259924 4.275730
## [15,] 3.311876 3.492138 3.594279 3.697607 3.867315 3.873742 3.918866 4.059805
## [16,] 2.867010 3.045342 3.204341 3.214327 3.572627 3.637745 3.643436 3.653801
## [17,] 4.130807 4.188286 4.253292 4.293531 4.321129 4.331707 4.338776 4.366374
## [18,] 3.385651 3.815453 3.898912 3.950120 4.046524 4.082604 4.094249 4.119896
## [19,] 2.501066 2.623459 2.723471 2.885968 2.926205 2.963436 3.178326 3.184604
## [20,] 3.182050 3.419672 3.429446 3.640109 4.052769 4.108694 4.379979 4.421793
## [,9] [,10]
## [1,] 3.433905 3.494258
## [2,] 3.584747 3.617330
## [3,] 3.196968 3.197360
## [4,] 3.601996 3.707303
## [5,] 4.264294 4.268515
## [6,] 3.574648 3.579448
## [7,] 3.124713 3.134375
## [8,] 6.259396 6.268555
## [9,] 3.767352 3.864313
## [10,] 3.063120 3.069113
## [11,] 4.193828 4.197303
## [12,] 4.595665 4.611223
## [13,] 4.104012 4.148727
## [14,] 4.358250 4.400418
## [15,] 4.181931 4.191853
## [16,] 3.696293 3.702478
## [17,] 4.398688 4.662700
## [18,] 4.134622 4.151868
## [19,] 3.214041 3.221525
## [20,] 4.455717 4.482405
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 1 0.887 0.688
## 2 0.722 0.926 0.855
## 3 0.902 0.887 0.891
## 4 0.912 0.884 0.396
## 5 1 1 0.985
## 6 0.945 0.853 0.812
## 7 0.897 0.926 0.342
## 8 0.949 0.913 0.973
## 9 1 0.884 0.776
## 10 0.912 0.853 0.701
## # ℹ 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.138 -0.0650 -0.152 -1.10
## 2 -0.00742 -0.533 -0.410 -0.736
## 3 -1.06 -0.558 -1.19 -1.37
## 4 -0.302 -0.671 -1.29 -1.11
## 5 -0.191 -0.263 -0.250 -0.303
## 6 -0.887 -0.329 -0.459 -0.982
## 7 -0.652 -0.114 -0.761 -0.693
## 8 -0.0398 -0.0000977 -0.615 -0.474
## 9 -0.0416 -0.182 -0.232 -1.03
## 10 -0.00766 -0.464 0.00117 0.524
## # ℹ 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.285 0.269 0.304 0.266 0.229 ...