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] 827 790 284 511 466 783 624 628 196 514 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 827 743 841 284 382 309 751 384 802 220
## [2,] 790 508 120 237 851 586 29 718 145 239
## [3,] 284 646 318 19 127 215 827 835 802 61
## [4,] 511 739 625 803 630 17 547 871 713 451
## [5,] 466 768 978 534 880 118 349 952 742 788
## [6,] 783 175 911 346 607 627 543 880 26 952
## [7,] 624 183 581 521 681 739 414 716 621 184
## [8,] 628 697 969 24 732 126 759 619 406 575
## [9,] 196 685 519 425 433 12 763 666 657 898
## [10,] 514 442 50 339 56 878 218 186 549 609
## [11,] 251 738 547 84 816 679 760 403 624 277
## [12,] 657 433 23 626 425 9 638 685 146 508
## [13,] 523 435 285 204 409 962 102 422 667 331
## [14,] 201 560 848 780 391 172 342 399 175 110
## [15,] 303 685 261 850 745 940 196 193 478 29
## [16,] 400 409 510 304 130 401 203 20 437 876
## [17,] 183 595 348 100 963 970 625 673 895 739
## [18,] 911 897 169 374 946 434 326 345 361 121
## [19,] 528 946 646 253 455 61 958 434 215 765
## [20,] 381 331 343 633 525 418 357 468 401 714
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.07 4.1 3.13 3.72 3.62 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.071717 3.482856 3.696256 3.756475 3.768423 3.981704 4.063349 4.176408
## [2,] 4.104066 4.234811 4.277316 4.314909 4.391883 4.436591 4.496290 4.573288
## [3,] 3.133906 3.145573 3.405517 3.875179 3.902213 4.154854 4.169846 4.253011
## [4,] 3.718052 3.770411 3.835693 3.919161 3.981942 4.010090 4.021221 4.042208
## [5,] 3.622950 3.631121 3.736735 3.749652 3.830653 3.867628 3.869372 3.881259
## [6,] 3.238448 3.431571 3.468780 3.526893 3.531683 3.654889 3.714632 3.755645
## [7,] 2.266397 2.469692 2.471813 2.508298 2.544611 2.623631 2.661384 2.847252
## [8,] 3.400413 3.614318 3.638779 3.703243 3.734166 3.735610 3.790119 3.792057
## [9,] 4.125997 4.212677 4.344830 4.396136 4.397874 4.448766 4.612523 4.655435
## [10,] 2.866492 3.475343 3.556980 3.613709 3.855725 3.914504 3.986689 4.166008
## [11,] 3.149332 3.188151 3.290095 3.316939 3.379998 3.456063 3.575291 3.589994
## [12,] 3.808868 4.258084 4.281820 4.302814 4.333545 4.448766 4.483504 4.497381
## [13,] 3.923602 4.186758 4.373896 4.379717 4.423305 4.461656 4.635691 4.647018
## [14,] 3.183397 3.242520 3.278325 3.421960 3.640327 3.702407 3.744287 3.759066
## [15,] 3.849542 4.028213 4.159745 4.387797 4.398015 4.409507 4.488994 4.493017
## [16,] 4.713210 5.285954 5.484486 5.560440 5.637751 5.687220 5.694227 5.830702
## [17,] 2.541346 2.775083 2.861422 2.862886 2.878302 2.901064 2.919410 2.973405
## [18,] 3.341492 3.450653 3.501768 3.581125 3.673895 3.674263 3.677091 3.684620
## [19,] 2.548350 2.887469 2.911608 3.205037 3.217258 3.314874 3.334937 3.350510
## [20,] 3.707808 3.753193 3.984069 4.168339 4.213230 4.231129 4.278318 4.302357
## [,9] [,10]
## [1,] 4.260064 4.288347
## [2,] 4.582641 4.606139
## [3,] 4.310794 4.333346
## [4,] 4.072347 4.124709
## [5,] 3.942098 3.965490
## [6,] 3.776265 3.807573
## [7,] 2.858227 2.919281
## [8,] 3.807089 3.817468
## [9,] 4.709470 4.753769
## [10,] 4.278016 4.306040
## [11,] 3.604977 3.619126
## [12,] 4.521345 4.550144
## [13,] 4.788668 4.866211
## [14,] 3.839011 3.876835
## [15,] 4.494916 4.549111
## [16,] 6.053192 6.057584
## [17,] 3.034362 3.034531
## [18,] 3.716201 3.748484
## [19,] 3.363391 3.399957
## [20,] 4.340030 4.349927
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.989 0.933 0.867
## 2 0.867 0.951 1
## 3 0.951 1 0.790
## 4 0.951 0.957 0.549
## 5 1 1 1
## 6 0.867 0.997 0.842
## 7 0.867 0.933 0.744
## 8 0.951 0.951 0.582
## 9 0.951 0.913 0.953
## 10 0.867 0.957 1
## # ℹ 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.358 -0.109 -0.131 0.124
## 2 -0.0552 -0.136 -0.0639 0.270
## 3 -0.0785 -0.00929 -0.00777 -0.802
## 4 -0.458 -0.355 -0.366 0.0316
## 5 -0.133 -0.0245 -0.0975 0.157
## 6 -0.125 -0.0787 -0.174 -1.12
## 7 0.428 0.291 -0.231 0.479
## 8 -0.176 -0.246 -0.196 0.974
## 9 -0.180 -0.255 -0.0147 -0.455
## 10 -0.149 -0.213 -0.180 0.951
## # ℹ 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.216 0.213 0.228 0.241 0.238 ...