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] 975 783 871 911 678 561 174 527 152 952 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 975 321 732 221 49 816 394 819 700 13
## [2,] 783 635 277 697 125 244 667 644 883 404
## [3,] 871 362 803 163 121 589 663 947 354 526
## [4,] 911 358 305 708 429 680 122 794 681 242
## [5,] 678 278 617 94 26 7 868 985 487 816
## [6,] 561 136 165 152 713 169 876 586 400 647
## [7,] 174 976 395 662 742 625 787 278 868 685
## [8,] 527 428 485 215 749 53 489 451 16 240
## [9,] 152 455 791 728 468 136 113 166 561 28
## [10,] 952 234 174 7 770 395 209 782 781 285
## [11,] 861 382 696 231 879 432 549 412 206 994
## [12,] 285 909 792 232 852 595 24 651 490 782
## [13,] 757 81 952 324 669 395 651 852 751 511
## [14,] 979 122 996 735 410 327 580 667 281 827
## [15,] 856 830 609 794 565 576 861 696 271 258
## [16,] 517 749 512 722 215 428 64 485 913 53
## [17,] 496 661 814 793 575 95 806 670 552 182
## [18,] 318 320 830 696 15 856 994 608 854 918
## [19,] 44 827 295 549 588 281 936 944 327 735
## [20,] 369 652 452 310 387 812 191 492 651 389
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.72 3.87 2.96 2.88 2.58 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.723428 3.760809 3.914977 4.018204 4.112165 4.138905 4.143516 4.178350
## [2,] 3.869308 4.242402 4.335811 4.353181 4.402773 4.543152 4.557389 4.573768
## [3,] 2.961884 3.265195 3.267385 3.340423 3.379025 3.389055 3.428510 3.462020
## [4,] 2.877684 3.498250 3.527949 3.606120 3.622966 3.720628 3.788851 3.882383
## [5,] 2.579222 3.234159 3.265297 3.351763 3.428287 3.503782 3.504496 3.510774
## [6,] 2.485762 3.154017 3.429964 3.506096 3.669237 3.742693 3.785822 3.810819
## [7,] 2.166226 2.375891 2.469439 2.482662 2.585580 2.608916 2.742256 2.750633
## [8,] 5.131131 5.336579 5.703112 5.890783 5.891578 5.944053 5.981509 5.999859
## [9,] 3.292670 3.736173 3.849177 3.940421 3.944156 4.069371 4.124803 4.235465
## [10,] 3.264471 3.287994 3.303976 3.355336 3.378307 3.385581 3.405819 3.413346
## [11,] 2.491643 3.037649 3.392959 3.399999 3.434338 3.511985 3.525676 3.530482
## [12,] 3.815895 3.951312 3.978292 4.009095 4.078269 4.095559 4.102395 4.291013
## [13,] 3.063972 3.163539 3.217766 3.239427 3.246037 3.251005 3.281831 3.294002
## [14,] 4.017774 4.407108 4.443624 4.477922 4.497634 4.548213 4.590462 4.645345
## [15,] 1.991096 2.537125 2.702714 2.774993 2.812816 2.841366 2.844674 2.867829
## [16,] 3.694205 3.714277 3.778572 4.095550 4.686074 5.051553 5.193347 5.311071
## [17,] 5.835787 6.100983 6.138841 6.210533 6.219095 6.346594 6.389606 6.511899
## [18,] 3.087960 3.255221 3.284561 3.303000 3.566917 3.597512 3.708648 3.720469
## [19,] 3.526604 4.371984 4.421682 4.597442 4.597678 4.627651 4.629036 4.680737
## [20,] 2.869548 2.876851 3.074811 3.266154 3.292981 3.377377 3.407014 3.443274
## [,9] [,10]
## [1,] 4.222727 4.226467
## [2,] 4.620796 4.631275
## [3,] 3.467390 3.497579
## [4,] 3.903263 3.915071
## [5,] 3.532714 3.540736
## [6,] 3.857591 3.965888
## [7,] 2.763797 2.894083
## [8,] 6.019691 6.165635
## [9,] 4.274129 4.288405
## [10,] 3.431261 3.463302
## [11,] 3.579136 3.772070
## [12,] 4.299045 4.310667
## [13,] 3.377825 3.407321
## [14,] 4.665953 4.680896
## [15,] 2.978875 2.996549
## [16,] 5.410265 5.433805
## [17,] 6.526201 6.531005
## [18,] 3.733213 3.753155
## [19,] 4.741965 4.758651
## [20,] 3.462538 3.474743
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.950 0.840 0.623
## 2 0.886 0.999 0.674
## 3 0.896 1 0.875
## 4 0.875 0.949 0.617
## 5 0.964 1 0.552
## 6 0.803 1 0.909
## 7 0.886 0.840 0.674
## 8 0.976 1 0.575
## 9 0.803 0.981 0.969
## 10 0.886 1 0.623
## # ℹ 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.482 -0.448 -0.482 -1.63
## 2 -0.236 -0.0968 -0.220 -0.340
## 3 -0.630 -0.0807 0.0549 -1.49
## 4 -0.0656 -0.471 -0.0767 0.202
## 5 -0.650 -0.0338 -0.779 -1.05
## 6 -0.375 -0.291 -0.559 0.863
## 7 0.565 -0.432 -0.352 -0.366
## 8 -0.330 -0.382 -1.10 -0.459
## 9 -0.00120 -0.0421 -0.0498 -1.38
## 10 -0.124 -0.370 -0.210 -0.860
## # ℹ 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.234 0.21 0.278 0.249 0.28 ...