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] 860 781 786 99 844 812 196 50 349 101 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 860 742 636 483 680 209 298 183 676 330
## [2,] 781 917 370 88 786 856 768 436 557 773
## [3,] 786 370 955 749 856 37 88 773 941 815
## [4,] 99 262 180 994 967 648 169 36 233 688
## [5,] 844 321 774 678 720 948 85 289 906 999
## [6,] 812 142 113 736 997 653 446 440 554 984
## [7,] 196 684 784 620 673 18 582 643 790 322
## [8,] 50 310 525 45 212 785 606 631 630 958
## [9,] 349 468 47 424 431 820 76 877 767 943
## [10,] 101 246 15 725 753 12 281 722 842 404
## [11,] 938 103 746 886 777 251 84 974 844 321
## [12,] 842 301 62 725 35 778 101 466 880 329
## [13,] 383 986 476 37 220 781 3 295 785 912
## [14,] 559 234 484 60 423 39 871 67 494 224
## [15,] 872 722 23 867 281 392 753 618 324 926
## [16,] 561 795 959 400 95 985 964 485 118 643
## [17,] 802 261 817 683 615 136 46 969 263 863
## [18,] 371 130 344 250 168 314 909 620 605 248
## [19,] 836 647 556 516 208 544 984 554 773 587
## [20,] 646 261 547 992 580 864 369 640 197 83
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.82 2.7 3.34 3.83 2.82 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.822860 3.199358 3.203155 3.380142 3.467987 3.577952 3.648404 3.709984
## [2,] 2.703913 3.415407 3.520534 3.636421 3.657169 3.714996 3.761648 3.780802
## [3,] 3.338399 3.382568 3.450199 3.486147 3.566865 3.588612 3.624838 3.629024
## [4,] 3.834127 3.849081 3.870384 3.949373 3.963666 4.105233 4.177706 4.179584
## [5,] 2.822432 3.235660 3.414547 3.441022 3.475924 3.510649 3.567269 3.579654
## [6,] 4.317249 4.474719 4.482967 4.753953 4.788508 4.809174 4.810910 4.872632
## [7,] 4.048453 4.495704 4.779918 4.802219 4.834264 4.868063 4.904162 4.960884
## [8,] 2.663104 3.172732 3.201109 3.256129 3.351092 3.365401 3.369040 3.398042
## [9,] 2.396558 3.105622 3.175475 3.259011 3.324790 3.369705 3.396080 3.427165
## [10,] 2.998118 3.415443 3.450901 3.460480 3.523166 3.531277 3.555782 3.620091
## [11,] 2.660734 2.744103 2.846291 2.972224 3.262778 3.300886 3.330355 3.387118
## [12,] 2.449072 2.910490 3.105991 3.238202 3.261289 3.274475 3.292818 3.300390
## [13,] 3.444666 3.630412 3.639349 3.910801 3.916749 3.943683 3.977124 4.001227
## [14,] 3.237406 4.018374 4.072270 4.234621 4.385194 4.682608 4.716818 4.814454
## [15,] 2.814795 2.852736 2.911103 2.921690 2.932717 2.970100 2.970434 2.973127
## [16,] 3.339616 4.188286 4.235551 4.253292 4.293531 4.331707 4.338776 4.398688
## [17,] 3.056680 3.573676 3.588156 3.633740 3.662709 3.699644 3.814698 3.836351
## [18,] 3.403529 3.556980 4.220445 4.313055 4.336553 4.539583 4.672401 4.752419
## [19,] 3.976899 4.005640 4.196532 4.224075 4.341237 4.347911 4.362560 4.381019
## [20,] 4.398451 4.419607 4.456708 4.506987 4.588290 4.595795 4.716477 4.738217
## [,9] [,10]
## [1,] 3.715809 3.755195
## [2,] 3.798015 3.856201
## [3,] 3.704721 3.822214
## [4,] 4.208453 4.210155
## [5,] 3.584902 3.587427
## [6,] 4.885538 4.913037
## [7,] 4.968252 5.010924
## [8,] 3.399457 3.408421
## [9,] 3.459256 3.527588
## [10,] 3.654687 3.729691
## [11,] 3.410273 3.436548
## [12,] 3.338053 3.338601
## [13,] 4.046080 4.073120
## [14,] 4.821360 4.861884
## [15,] 3.002221 3.026926
## [16,] 4.509582 4.560838
## [17,] 3.926966 4.025523
## [18,] 4.804014 4.811654
## [19,] 4.509977 4.510202
## [20,] 4.754618 4.772693
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.810 1 0.988
## 2 0.983 1 0.988
## 3 0.885 1 0.988
## 4 0.915 1 0.988
## 5 0.666 1 0.992
## 6 0.975 1 0.988
## 7 0.848 1 0.992
## 8 0.957 1 0.988
## 9 0.972 1 0.988
## 10 0.726 1 0.988
## # ℹ 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.215 -0.250 -0.0990 -0.294
## 3 -0.474 0.346 -0.507 0.964
## 4 -0.869 -0.427 0.197 -1.07
## 5 -0.275 -0.330 -0.562 -0.595
## 6 -0.0498 0.201 0.573 -0.355
## 7 -0.171 -0.126 -0.0613 0.187
## 8 -0.143 -0.484 -0.289 -0.892
## 9 0.343 0.199 -0.302 -0.650
## 10 -0.234 -0.0633 -0.104 -0.579
## # ℹ 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.261 0.256 0.263 0.231 0.274 ...