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] 267 609 899 731 196 597 8 7 64 827 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 267 282 995 850 438 623 325 86 947 483
## [2,] 609 434 408 570 841 392 888 427 164 576
## [3,] 899 742 331 480 942 323 200 598 879 151
## [4,] 731 131 924 397 615 568 271 968 625 503
## [5,] 196 174 363 11 925 739 745 622 545 559
## [6,] 597 214 64 245 69 8 181 341 616 559
## [7,] 8 9 197 99 672 934 64 616 170 727
## [8,] 7 34 64 353 6 99 616 141 768 69
## [9,] 64 616 353 869 181 597 696 110 170 950
## [10,] 827 417 442 553 897 450 634 67 308 170
## [11,] 622 196 947 363 807 195 174 438 172 5
## [12,] 33 435 144 320 645 73 733 48 869 354
## [13,] 620 586 418 109 990 679 921 538 234 919
## [14,] 902 218 822 687 834 865 764 673 941 394
## [15,] 363 839 739 460 623 864 883 804 5 947
## [16,] 424 559 479 934 739 877 584 983 515 307
## [17,] 479 269 102 55 883 221 704 332 372 300
## [18,] 534 431 296 263 352 41 503 127 815 846
## [19,] 355 440 266 721 689 79 568 525 843 330
## [20,] 745 195 11 293 290 438 821 894 947 363
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.56 4.51 4.35 3.08 3.12 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.564155 3.339232 3.678814 3.705585 3.708406 3.713332 3.713587 3.717871
## [2,] 4.510690 4.550859 4.573888 4.579418 4.867458 5.046789 5.086669 5.174128
## [3,] 4.352202 4.600549 4.623144 4.835635 4.848014 4.889187 4.916895 4.969283
## [4,] 3.075094 3.503807 3.726603 3.792677 3.820885 3.877766 3.879786 3.952217
## [5,] 3.123578 3.126652 3.281805 3.343012 3.425534 3.555591 3.571135 3.599752
## [6,] 2.473366 2.743782 2.823186 2.855793 2.897474 2.952073 2.975193 3.008461
## [7,] 2.641899 3.189302 3.239236 3.298887 3.321811 3.404989 3.431209 3.445228
## [8,] 2.641899 2.824527 2.877447 2.908134 2.952073 2.961004 2.973848 3.015275
## [9,] 2.545865 2.840335 2.995501 3.019087 3.040590 3.058576 3.079463 3.119821
## [10,] 2.707680 2.821755 2.902729 2.941805 3.080338 3.214857 3.250388 3.292698
## [11,] 2.453201 2.470231 2.967051 3.104128 3.167093 3.185010 3.191709 3.218411
## [12,] 2.046750 2.505417 2.739721 2.746293 2.848480 2.895432 3.004853 3.024012
## [13,] 4.321551 4.502277 4.552564 4.603309 4.636783 4.656170 4.680338 4.709247
## [14,] 2.049721 2.134410 2.373197 2.399796 2.570987 2.642107 2.659695 2.759566
## [15,] 3.382568 3.486147 3.566865 3.624838 3.629024 3.704721 3.824715 3.830374
## [16,] 2.690266 3.042903 3.055655 3.085945 3.112783 3.180471 3.219082 3.244668
## [17,] 3.095164 3.098691 3.297911 3.379533 3.394414 3.448939 3.491857 3.563927
## [18,] 3.205816 3.702407 3.744287 3.759066 3.839011 3.881543 3.882930 3.952275
## [19,] 3.526604 4.241187 4.311942 4.371984 4.429282 4.508412 4.597442 4.622240
## [20,] 3.615463 3.707808 3.753193 3.800756 3.984069 4.088078 4.126504 4.168339
## [,9] [,10]
## [1,] 3.816347 3.825843
## [2,] 5.298361 5.304528
## [3,] 4.970772 4.997647
## [4,] 4.019366 4.028348
## [5,] 3.601231 3.642188
## [6,] 3.048458 3.060811
## [7,] 3.490355 3.505098
## [8,] 3.045286 3.062585
## [9,] 3.125789 3.163069
## [10,] 3.319018 3.340254
## [11,] 3.255873 3.343012
## [12,] 3.088299 3.138187
## [13,] 4.737838 4.755523
## [14,] 2.777708 2.795061
## [15,] 3.842300 3.850378
## [16,] 3.254884 3.265306
## [17,] 3.635052 3.653628
## [18,] 3.975931 4.013522
## [19,] 4.629036 4.634261
## [20,] 4.213230 4.231129
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 1 0.943
## 2 1 1 0.761
## 3 1 1 0.974
## 4 1 1 1
## 5 0.999 1 0.804
## 6 0.976 1 0.578
## 7 0.966 1 0.866
## 8 0.966 1 0.517
## 9 0.966 1 0.852
## 10 0.976 1 0.981
## # ℹ 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.557 -0.0292 -0.621 0.469
## 2 -0.0914 -0.135 -0.271 0.558
## 3 -0.180 -0.255 -0.0147 -0.455
## 4 -0.0403 -0.496 -0.108 -1.01
## 5 -0.0663 -0.0284 -0.180 0.385
## 6 0.702 -0.114 0.282 0.181
## 7 0.702 -0.162 -0.412 0.563
## 8 -0.0296 -0.0715 -0.589 -0.786
## 9 -0.0882 1.04 0.385 -0.361
## 10 -0.216 0.771 0.140 0.125
## # ℹ 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.253 0.183 0.197 0.244 0.263 ...