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] 810 811 833 903 391 699 143 92 314 391 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 810 319 776 261 37 166 492 840 848 964
## [2,] 811 894 946 896 272 596 379 978 898 799
## [3,] 833 956 47 852 281 548 540 472 789 718
## [4,] 903 428 573 867 750 408 462 593 202 546
## [5,] 391 178 981 10 19 324 437 537 585 301
## [6,] 699 895 816 569 719 390 892 21 432 828
## [7,] 143 868 910 982 861 44 94 724 271 635
## [8,] 92 467 737 671 210 441 298 665 595 926
## [9,] 314 342 952 125 955 469 843 113 520 189
## [10,] 391 506 682 692 379 680 313 657 771 237
## [11,] 965 974 197 403 775 862 595 332 434 67
## [12,] 56 690 276 937 880 728 337 211 93 873
## [13,] 673 41 961 58 65 56 635 36 356 853
## [14,] 548 400 891 427 729 675 958 335 484 750
## [15,] 850 905 637 513 907 455 355 490 259 382
## [16,] 757 619 842 883 588 28 317 352 272 692
## [17,] 466 595 766 965 27 694 860 812 457 550
## [18,] 650 841 562 940 634 444 178 455 219 150
## [19,] 499 940 418 437 752 360 504 259 703 537
## [20,] 91 172 221 688 432 39 38 378 616 960
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.09 2.75 2.55 4.32 4.43 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.088610 3.291602 3.397197 3.402837 3.527949 3.643457 3.748741 3.849347
## [2,] 2.751551 3.288399 3.621606 3.634743 3.670826 3.674067 3.794746 3.826737
## [3,] 2.551057 2.934124 3.149285 3.301250 3.339767 3.431880 3.614783 3.626039
## [4,] 4.315262 4.802885 4.915666 5.047837 5.096255 5.447977 5.478974 5.661682
## [5,] 4.430561 4.952011 5.055575 5.067698 5.153642 5.163699 5.194464 5.207046
## [6,] 3.747357 4.188512 4.305541 4.367062 4.375356 4.427646 4.492321 4.817787
## [7,] 4.180347 4.491967 4.530094 4.651364 4.787794 4.810937 4.826039 4.848569
## [8,] 3.757888 4.463803 4.466517 4.510118 4.593129 4.650437 4.667709 4.676632
## [9,] 3.182050 3.419672 3.429446 3.792205 3.832810 3.981910 3.998083 4.052769
## [10,] 4.371005 4.471504 4.510921 4.525895 4.542604 4.544326 4.552659 4.625730
## [11,] 2.287895 2.692867 2.697609 3.095989 3.180094 3.201879 3.315097 3.319824
## [12,] 3.280988 3.352551 3.425248 3.467818 3.527758 3.628436 3.635164 3.656948
## [13,] 3.493480 3.789282 3.901218 3.933761 3.962869 3.979688 4.041869 4.085356
## [14,] 5.003977 5.061926 5.254434 5.508202 5.601588 5.630743 5.723902 5.745775
## [15,] 4.249507 4.962714 5.097261 5.265381 5.265734 5.333571 5.379952 5.387872
## [16,] 4.065915 4.497995 4.535445 4.667527 4.724088 4.980528 5.145195 5.217181
## [17,] 2.766007 2.966594 3.035715 3.048748 3.222205 3.311565 3.405469 3.405926
## [18,] 3.244238 3.740008 3.815576 3.850592 4.003249 4.177621 4.182420 4.204069
## [19,] 3.470828 3.543151 3.565500 3.569898 3.643701 3.698633 3.698943 3.784600
## [20,] 3.475343 3.669566 3.814607 3.926141 4.306589 4.364447 4.388845 4.401926
## [,9] [,10]
## [1,] 3.866521 3.905109
## [2,] 4.173876 4.379086
## [3,] 3.636517 3.670123
## [4,] 5.749056 5.778200
## [5,] 5.226651 5.229419
## [6,] 4.822189 4.838125
## [7,] 4.890339 4.946070
## [8,] 4.688115 4.691614
## [9,] 4.108694 4.285727
## [10,] 4.636025 4.669470
## [11,] 3.392430 3.433740
## [12,] 3.669893 3.693790
## [13,] 4.105321 4.110710
## [14,] 5.770430 5.792429
## [15,] 5.402247 5.408303
## [16,] 5.284864 5.291309
## [17,] 3.435292 3.458569
## [18,] 4.207536 4.213908
## [19,] 3.795480 3.833160
## [20,] 4.500277 4.633239
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.809 1 0.978
## 2 0.800 1 0.928
## 3 0.800 1 1
## 4 0.765 1 0.995
## 5 0.800 1 0.988
## 6 0.819 1 0.995
## 7 0.889 1 0.988
## 8 0.622 1 0.995
## 9 0.703 1 0.928
## 10 1 1 0.995
## # ℹ 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 -1.15 -0.601 -0.117 -0.146
## 2 -0.00107 -0.0374 -0.491 0.572
## 3 -0.0616 -0.587 1.47 -0.490
## 4 -0.991 -0.718 -0.545 -1.04
## 5 -0.320 -0.238 -0.350 0.997
## 6 -1.23 -0.458 -0.527 -2.70
## 7 0.361 0.902 1.38 0.337
## 8 0.810 0.0649 1.85 0.338
## 9 -0.217 -0.227 -0.353 -0.0294
## 10 -0.189 0.561 1.72 0.361
## # ℹ 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.252 0.225 0.265 0.168 0.188 ...