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] 510 268 378 699 358 740 182 163 54 653 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 510 260 983 728 847 599 66 748 358 218
## [2,] 268 948 550 881 663 298 184 378 844 615
## [3,] 378 826 485 956 417 390 615 590 79 62
## [4,] 699 916 675 913 755 676 313 292 279 198
## [5,] 358 997 748 870 706 988 621 740 386 254
## [6,] 740 408 795 837 230 568 27 889 989 488
## [7,] 182 525 912 701 248 464 947 880 863 693
## [8,] 163 603 474 879 736 637 630 250 607 36
## [9,] 54 151 442 828 741 673 40 345 838 612
## [10,] 653 158 162 37 944 842 111 811 171 841
## [11,] 344 205 809 539 846 370 143 683 350 445
## [12,] 777 378 763 268 387 417 584 364 898 949
## [13,] 368 863 696 391 448 408 879 447 969 164
## [14,] 405 388 38 584 756 393 97 601 378 322
## [15,] 216 496 649 830 306 431 653 206 302 289
## [16,] 405 208 148 296 756 581 594 777 584 364
## [17,] 919 26 922 646 886 461 186 117 632 830
## [18,] 990 291 280 167 455 741 828 582 519 200
## [19,] 467 51 774 948 899 844 788 850 663 826
## [20,] 917 78 293 162 761 158 292 643 602 653
## num [1:1000, 1:30] 3.15 2.78 2.47 3.81 3.49 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.154132 3.367718 3.582910 3.743084 3.766107 3.965732 4.087099 4.139017
## [2,] 2.781966 2.911382 2.970242 2.995856 3.075129 3.088763 3.125124 3.185917
## [3,] 2.469439 2.475312 2.516752 2.599457 2.655885 2.690761 2.695743 2.860598
## [4,] 3.814431 3.881741 4.005479 4.091801 4.104668 4.128856 4.139117 4.140288
## [5,] 3.488930 3.555011 3.742919 4.013427 4.043410 4.061431 4.107679 4.140539
## [6,] 3.580379 3.918520 4.355036 4.360842 4.368750 4.384493 4.392274 4.453367
## [7,] 4.963588 4.978085 5.062771 5.066917 5.114901 5.126062 5.197413 5.211719
## [8,] 3.577959 3.685552 3.732034 3.875570 3.891951 3.905678 3.922941 3.984354
## [9,] 3.153661 3.255518 3.255525 3.425450 3.427047 3.635721 3.740599 3.869013
## [10,] 4.265772 4.368631 4.480689 4.502870 4.506799 4.555257 4.555662 4.680920
## [11,] 3.449080 3.679193 3.727940 3.762425 3.820788 3.865912 3.924909 3.965106
## [12,] 2.860810 2.916131 2.936378 2.941425 2.973164 3.023871 3.113233 3.145767
## [13,] 3.183397 3.242520 3.278325 3.324507 3.686812 3.839011 3.882930 3.947851
## [14,] 2.857515 2.896975 2.945146 2.978146 3.198153 3.264566 3.418513 3.484388
## [15,] 2.999142 3.127459 3.533226 3.757179 3.845536 3.904025 3.935988 3.936265
## [16,] 3.460197 3.493728 3.498565 3.499491 3.573260 3.610366 3.708349 3.763953
## [17,] 3.425230 3.882539 3.894914 3.935942 3.998150 4.040759 4.044000 4.075029
## [18,] 3.188749 3.321582 3.561743 3.598864 3.615391 3.690312 3.707063 3.720333
## [19,] 2.548095 2.671363 3.179717 3.207074 3.236562 3.265930 3.274097 3.323989
## [20,] 4.651768 5.038568 5.074714 5.470456 5.624392 5.662203 5.703896 5.718982
## [,9] [,10]
## [1,] 4.191824 4.432711
## [2,] 3.197360 3.225703
## [3,] 2.892306 2.908134
## [4,] 4.144786 4.162954
## [5,] 4.264184 4.288083
## [6,] 4.480499 4.494408
## [7,] 5.389147 5.486499
## [8,] 4.023391 4.058798
## [9,] 3.899381 4.028488
## [10,] 4.728122 4.742247
## [11,] 3.986425 3.992820
## [12,] 3.167673 3.261838
## [13,] 3.961548 3.986597
## [14,] 3.548607 3.566621
## [15,] 3.939764 3.959693
## [16,] 3.774208 3.799117
## [17,] 4.136767 4.138968
## [18,] 3.790985 3.821455
## [19,] 3.400477 3.406927
## [20,] 5.744352 5.744498
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.901 0.986 0.908
## 2 0.747 0.943 0.908
## 3 0.742 0.881 0.830
## 4 0.932 0.843 0.922
## 5 0.742 0.713 0.928
## 6 0.951 0.843 0.830
## 7 0.742 0.958 0.873
## 8 0.998 0.986 0.974
## 9 0.842 0.980 0.830
## 10 0.985 0.801 0.945
## # ℹ 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.223 0.425 -0.135 0.528
## 2 -0.287 -0.0939 -0.108 -1.65
## 3 -0.498 -0.506 -0.480 -0.698
## 4 0.264 1.63 -0.225 0.644
## 5 -0.0231 0.389 -0.151 -0.528
## 6 -0.0175 -0.213 -0.157 -0.482
## 7 0.367 1.50 -0.781 0.803
## 8 -0.165 1.48 -0.470 -0.545
## 9 -0.162 0.411 -0.196 -0.954
## 10 -0.169 -0.180 -0.0900 -0.779
## # ℹ 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.223 0.305 0.342 0.23 0.225 ...