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] 395 478 292 161 813 966 532 439 348 946 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 395 690 500 869 890 542 842 268 103 96
## [2,] 478 554 230 938 317 577 155 851 635 972
## [3,] 292 841 867 159 618 31 779 899 784 636
## [4,] 161 247 253 887 378 333 930 667 964 169
## [5,] 813 873 396 232 299 411 673 837 902 728
## [6,] 966 672 304 66 639 225 540 78 278 853
## [7,] 532 13 509 71 437 771 653 358 397 462
## [8,] 439 939 872 876 735 937 588 688 511 50
## [9,] 348 610 702 131 10 653 142 33 35 657
## [10,] 946 653 33 142 460 348 306 714 501 52
## [11,] 128 54 156 726 502 826 697 942 920 438
## [12,] 754 621 11 319 666 128 438 495 893 284
## [13,] 586 725 532 398 910 653 358 714 938 652
## [14,] 487 270 697 622 128 156 134 431 284 752
## [15,] 832 911 391 347 200 949 967 422 87 271
## [16,] 640 941 673 756 232 678 102 648 103 838
## [17,] 170 519 493 489 646 295 720 643 470 107
## [18,] 888 407 78 798 501 24 185 261 412 304
## [19,] 104 336 482 977 888 527 633 86 908 692
## [20,] 52 657 841 35 779 142 348 618 861 525
## num [1:1000, 1:30] 2.75 4.19 3.77 5.09 2.91 ...
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.746459 3.094631 3.105474 3.189129 3.259517 3.321878 3.387917 3.403662
## [2,] 4.192058 4.199042 4.252569 4.365472 4.453105 4.500361 4.699085 4.762732
## [3,] 3.768423 4.465721 4.636100 4.657653 4.763463 4.845211 4.846490 4.847306
## [4,] 5.089879 5.377997 5.515286 5.615800 5.677817 5.758854 5.804874 5.843174
## [5,] 2.911092 2.971382 3.117694 3.222549 3.301510 3.367862 3.374281 3.427643
## [6,] 2.525279 2.922006 3.031190 3.077649 3.080451 3.085594 3.096479 3.104648
## [7,] 4.224265 4.550179 4.663500 4.746444 4.763281 4.781532 4.831634 4.858040
## [8,] 3.133154 3.615342 3.637208 3.783196 3.946407 3.952217 4.009532 4.019366
## [9,] 3.849783 3.906039 4.012393 4.550859 4.573888 4.877204 5.018824 5.046789
## [10,] 2.435961 2.645618 2.823612 3.340149 3.346877 3.374529 3.387533 3.451535
## [11,] 2.594500 3.094221 3.136652 3.228989 3.245368 3.363442 3.393072 3.404703
## [12,] 3.721674 3.949839 4.117505 4.135119 4.162436 4.197541 4.237911 4.354221
## [13,] 3.204043 3.309969 3.761445 3.902280 3.974058 4.005883 4.199570 4.232868
## [14,] 3.745536 3.786412 3.790699 3.790938 3.824388 3.875307 3.877579 3.879340
## [15,] 3.291283 3.422467 3.492138 3.517324 3.594279 3.867315 3.873742 3.918866
## [16,] 3.349702 3.791581 3.830255 3.833191 3.877324 3.882934 3.892192 3.893678
## [17,] 3.264204 3.437360 3.569888 3.589279 3.595931 3.616157 3.619084 3.624309
## [18,] 2.579818 2.589770 2.933224 2.972554 3.011654 3.026797 3.056611 3.091524
## [19,] 2.288550 2.737955 2.799610 2.816362 2.871542 2.881318 2.913241 2.931912
## [20,] 3.657086 3.898380 4.413524 4.545872 4.565102 4.628867 4.682259 4.710420
## [,9] [,10]
## [1,] 3.423346 3.432168
## [2,] 4.774115 4.789409
## [3,] 4.913367 4.973717
## [4,] 5.991430 6.026068
## [5,] 3.489395 3.504596
## [6,] 3.108314 3.164047
## [7,] 4.893656 4.922133
## [8,] 4.056031 4.068588
## [9,] 5.298361 5.304528
## [10,] 3.477433 3.494441
## [11,] 3.451025 3.487505
## [12,] 4.354957 4.387275
## [13,] 4.250446 4.323170
## [14,] 3.921876 3.935676
## [15,] 3.970579 4.059805
## [16,] 3.918526 3.929675
## [17,] 3.700522 3.743153
## [18,] 3.115193 3.127617
## [19,] 2.940155 2.955938
## [20,] 4.836712 4.863915
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.778 1 0.967
## 2 0.958 1 1
## 3 0.878 1 0.899
## 4 0.920 1 0.999
## 5 0.708 1 0.967
## 6 0.565 1 0.677
## 7 0.985 1 0.949
## 8 0.904 1 0.754
## 9 0.594 1 0.820
## 10 0.779 1 0.967
## # ℹ 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.361 0.902 1.38 0.337
## 2 -0.189 0.561 1.72 0.361
## 3 0.949 1.49 1.41 0.817
## 4 0.460 1.26 1.73 0.834
## 5 0.715 -0.226 1.81 0.659
## 6 -0.311 -0.148 -0.0418 0.670
## 7 0.108 1.26 1.93 -0.0644
## 8 0.0476 0.0127 2.66 -0.219
## 9 0.489 -0.235 -1.43 0.912
## 10 0.395 -0.120 0.634 0.989
## # ℹ 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.281 0.206 0.195 0.162 0.269 ...