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] 365 286 436 391 77 43 776 192 311 864 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 365 211 103 148 229 238 981 188 732 67
## [2,] 286 21 262 833 928 944 982 98 576 524
## [3,] 436 950 148 363 492 323 77 54 252 914
## [4,] 391 960 845 744 842 437 614 723 353 196
## [5,] 77 900 90 785 510 741 913 261 59 73
## [6,] 43 177 320 689 941 187 907 344 429 607
## [7,] 776 750 682 331 642 807 578 661 801 971
## [8,] 192 242 362 570 564 996 868 384 568 748
## [9,] 311 805 675 934 898 83 91 426 476 179
## [10,] 864 480 618 366 934 921 667 675 629 444
## [11,] 687 902 78 907 597 647 320 38 768 689
## [12,] 557 427 55 842 721 552 53 613 568 671
## [13,] 430 99 17 708 769 580 661 631 164 801
## [14,] 575 181 948 318 528 290 679 114 349 374
## [15,] 315 994 154 863 312 379 373 453 16 193
## [16,] 445 739 512 51 205 77 869 767 423 824
## [17,] 13 661 631 416 138 267 161 987 769 933
## [18,] 994 20 623 157 738 26 135 680 223 274
## [19,] 107 543 925 479 53 56 838 483 683 865
## [20,] 738 840 856 18 274 549 677 830 680 398
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.46 3.53 3.82 3.11 3.29 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.457666 4.100294 4.185938 4.193071 4.284514 4.397856 4.407696 4.432943
## [2,] 3.526604 3.543692 4.097799 4.122683 4.217476 4.265636 4.293315 4.357092
## [3,] 3.818838 3.907562 3.970704 4.116406 4.155963 4.175135 4.193522 4.234616
## [4,] 3.105474 3.125755 3.230213 3.259517 3.321878 3.332636 3.369827 3.387917
## [5,] 3.287269 3.315326 3.497916 3.535462 3.562311 3.626131 3.677656 3.767352
## [6,] 3.582441 4.183429 4.388845 4.793897 4.827571 4.940233 5.067369 5.070554
## [7,] 3.523182 3.592953 3.688657 3.710470 3.756354 3.789179 3.819890 3.895851
## [8,] 2.718018 2.864140 2.965590 3.073566 3.106157 3.110425 3.150832 3.237219
## [9,] 2.589770 2.747512 2.972554 3.059462 3.088324 3.115193 3.127617 3.183310
## [10,] 2.865907 2.990510 3.067158 3.125141 3.175073 3.220253 3.220861 3.334884
## [11,] 3.706606 3.783263 3.859053 4.153796 4.176097 4.180736 4.343596 4.366957
## [12,] 3.127348 3.214287 3.252302 3.272900 3.319045 3.462682 3.474549 3.478402
## [13,] 4.490811 4.749738 4.778674 4.959962 4.981484 4.986135 4.989786 5.059060
## [14,] 3.571378 3.660410 3.703144 3.791979 3.868399 3.955882 4.044435 4.169817
## [15,] 2.603422 3.185501 3.280572 3.465840 3.593597 3.615787 3.646447 3.648670
## [16,] 2.616732 3.050702 3.079465 3.323135 3.327200 3.338836 3.357430 3.375353
## [17,] 4.778674 4.916860 4.982559 5.107493 5.136715 5.191339 5.259487 5.274708
## [18,] 2.942758 2.981166 3.147516 3.175475 3.181385 3.425969 3.523775 3.529840
## [19,] 4.086391 4.129631 4.200740 4.408984 4.409119 4.427310 4.554311 4.602414
## [20,] 2.561204 2.565487 2.695595 2.981166 3.061995 3.089798 3.090920 3.093601
## [,9] [,10]
## [1,] 4.451966 4.475409
## [2,] 4.437975 4.504148
## [3,] 4.269572 4.272317
## [4,] 3.401931 3.403662
## [5,] 3.775698 3.864313
## [6,] 5.151974 5.175346
## [7,] 4.019706 4.153218
## [8,] 3.268406 3.416803
## [9,] 3.202088 3.222036
## [10,] 3.401002 3.433616
## [11,] 4.371364 4.436539
## [12,] 3.508446 3.514234
## [13,] 5.104209 5.193526
## [14,] 4.177199 4.232175
## [15,] 3.650747 3.663923
## [16,] 3.387486 3.481242
## [17,] 5.291771 5.373455
## [18,] 3.617175 3.623105
## [19,] 4.616869 4.662361
## [20,] 3.101901 3.126057
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.963
## 2 1 1 0.963
## 3 1 1 0.988
## 4 1 1 0.981
## 5 1 1 0.991
## 6 0.774 1 0.944
## 7 1 1 0.963
## 8 1 1 0.963
## 9 0.868 1 0.944
## 10 0.996 1 0.948
## # ℹ 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.174 -0.292 -0.112 0.635
## 2 -0.720 -0.0535 -0.985 -1.08
## 3 -0.181 -0.206 -0.132 -0.518
## 4 0.229 -0.215 0.496 0.435
## 5 -0.104 -0.148 -0.470 0.478
## 6 -0.174 -0.175 -0.0945 0.612
## 7 -0.184 -0.0110 -0.128 -1.37
## 8 0.436 0.483 -1.25 0.0386
## 9 -0.236 -0.0443 -0.0258 0.237
## 10 -0.728 -0.322 -0.524 0.465
## # ℹ 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.218 0.219 0.23 0.282 0.257 ...