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] 622 317 87 228 35 322 126 623 240 303 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 622 131 178 273 567 680 149 24 766 224
## [2,] 317 608 496 62 593 643 962 10 237 745
## [3,] 87 926 580 239 233 947 435 833 15 273
## [4,] 228 187 438 272 967 337 320 972 347 143
## [5,] 35 330 947 797 426 416 824 660 148 850
## [6,] 322 305 902 966 540 122 634 831 557 178
## [7,] 126 148 116 261 274 38 442 736 47 58
## [8,] 623 57 129 847 205 56 254 558 655 851
## [9,] 240 859 414 344 532 990 90 738 519 201
## [10,] 303 643 663 237 317 62 334 593 459 631
## [11,] 537 457 849 592 543 581 493 648 294 215
## [12,] 376 243 657 251 258 632 170 411 558 819
## [13,] 325 839 35 351 10 555 614 746 660 427
## [14,] 230 150 993 814 33 697 430 546 838 553
## [15,] 725 626 614 452 17 149 759 631 766 309
## [16,] 520 500 153 185 312 744 367 139 799 941
## [17,] 15 48 335 127 452 634 639 282 100 459
## [18,] 736 701 442 518 926 890 788 149 125 756
## [19,] 77 63 487 40 599 216 120 533 159 65
## [20,] 608 540 671 182 79 335 346 593 952 439
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.52 2.97 2.91 3.07 3.45 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.517126 3.827344 3.962290 4.102763 4.109884 4.138264 4.215518 4.228391
## [2,] 2.969913 3.054975 3.167213 3.207533 3.260968 3.304491 3.371551 3.379221
## [3,] 2.907990 3.095234 3.123469 3.239427 3.246829 3.284504 3.322066 3.327836
## [4,] 3.069267 3.218228 3.241631 3.472081 3.482895 3.569087 3.599660 3.827555
## [5,] 3.448939 3.593471 3.654232 3.671652 3.699224 3.954656 3.965861 4.046524
## [6,] 2.756064 3.101829 3.240576 3.274835 3.340364 3.380470 3.397789 3.428570
## [7,] 3.632760 3.717725 3.920553 3.944156 3.951215 3.981323 3.991113 4.122481
## [8,] 3.469219 3.787296 3.821567 4.209688 4.396174 4.443402 4.478943 4.555771
## [9,] 3.639932 3.656104 3.697963 3.716740 3.717181 3.720733 3.872963 3.953520
## [10,] 2.505417 2.700998 2.768821 2.848474 2.860145 2.996733 3.053310 3.063015
## [11,] 4.510140 4.738522 4.743826 4.905895 4.918202 4.938301 5.049838 5.336097
## [12,] 5.336579 5.636779 5.891578 5.944053 5.965468 5.968511 5.981509 6.165635
## [13,] 2.472428 2.614026 3.135849 3.138471 3.197036 3.320140 3.336833 3.416042
## [14,] 3.405517 3.820600 4.164362 4.253011 4.281270 4.310794 4.333346 4.488840
## [15,] 2.728539 2.865841 2.887494 2.887831 2.948158 2.954585 2.988839 2.993791
## [16,] 3.395493 3.544002 3.642016 3.845087 3.912850 3.984562 3.987274 3.998168
## [17,] 2.948158 2.963352 2.967507 2.992707 3.055058 3.095227 3.144432 3.191961
## [18,] 2.663812 2.920562 3.020810 3.141899 3.203740 3.237663 3.267548 3.269940
## [19,] 3.834289 4.012912 4.016016 4.208430 4.244744 4.293380 4.300774 4.305244
## [20,] 2.579818 2.747512 3.059462 3.091524 3.115193 3.183310 3.202088 3.216525
## [,9] [,10]
## [1,] 4.243987 4.313544
## [2,] 3.412866 3.573606
## [3,] 3.374992 3.383435
## [4,] 3.840789 3.882210
## [5,] 4.058619 4.082604
## [6,] 3.429700 3.432843
## [7,] 4.157371 4.161722
## [8,] 4.766562 4.876762
## [9,] 3.957586 3.992811
## [10,] 3.102046 3.103549
## [11,] 5.384740 5.436961
## [12,] 6.188353 6.217078
## [13,] 3.419903 3.438753
## [14,] 4.533548 4.600156
## [15,] 3.024537 3.034984
## [16,] 4.098356 4.110130
## [17,] 3.201598 3.209696
## [18,] 3.292320 3.311971
## [19,] 4.341295 4.431978
## [20,] 3.231998 3.242709
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.797
## 2 1 1 0.983
## 3 1 1 0.983
## 4 1 1 0.983
## 5 1 1 1
## 6 1 1 0.983
## 7 1 1 1
## 8 0.882 1 0.983
## 9 1 1 0.983
## 10 1 1 0.928
## # ℹ 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.0155 -0.228 -0.330 0.293
## 2 -0.651 -0.351 -0.209 -1.25
## 3 -0.652 -0.114 -0.761 -0.693
## 4 -0.182 -0.243 -0.370 -0.473
## 5 0.0911 1.33 0.753 0.362
## 6 -0.252 -0.144 -0.0596 -0.140
## 7 -0.157 -0.262 -0.0956 -0.293
## 8 -0.142 -0.171 -0.0665 0.220
## 9 -0.171 -0.0136 -0.104 0.0824
## 10 -0.189 -0.191 -0.719 0.0682
## # ℹ 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.229 0.275 0.29 0.255 0.245 ...