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] 295 246 858 505 175 272 212 323 561 565 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 295 79 248 246 73 839 196 69 238 265
## [2,] 246 751 502 807 632 399 546 50 839 780
## [3,] 858 595 62 441 980 262 488 187 811 671
## [4,] 505 736 898 478 845 915 354 602 14 74
## [5,] 175 305 875 454 935 548 292 655 681 339
## [6,] 272 554 248 783 198 312 990 729 941 46
## [7,] 212 298 406 995 605 863 666 132 158 217
## [8,] 323 462 316 437 831 847 646 760 698 529
## [9,] 561 299 976 529 162 167 397 630 667 905
## [10,] 565 309 118 737 360 1000 286 582 363 218
## [11,] 113 763 801 154 250 685 182 700 218 837
## [12,] 953 814 991 666 138 811 978 789 980 677
## [13,] 270 698 707 603 905 587 277 209 165 817
## [14,] 845 74 354 486 979 487 499 433 706 841
## [15,] 453 170 199 275 623 941 963 896 260 803
## [16,] 869 990 871 400 993 790 802 479 822 408
## [17,] 909 272 850 871 248 79 623 400 203 990
## [18,] 936 315 220 361 633 881 654 401 261 313
## [19,] 693 437 619 579 213 384 998 362 740 529
## [20,] 928 686 127 711 649 831 388 947 761 781
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.24 3.51 3.04 3.66 3.86 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.239590 3.328692 3.367127 3.706561 3.725396 3.754457 3.805835 3.849651
## [2,] 3.508186 3.729070 3.744079 3.774444 3.957960 4.106159 4.128604 4.131779
## [3,] 3.037948 3.244502 3.302855 3.344558 3.382285 3.419764 3.453594 3.573983
## [4,] 3.660694 3.920476 4.085756 4.471818 4.837811 4.927633 4.932196 4.937358
## [5,] 3.862732 3.976396 4.054677 4.114932 4.129718 4.249349 4.300524 4.313850
## [6,] 3.354947 3.465497 3.880269 3.938036 3.950092 3.958856 3.972644 3.989307
## [7,] 4.035305 4.038179 4.164679 4.372719 4.471075 4.475194 4.526836 4.567160
## [8,] 2.783561 2.861422 2.862886 2.887831 2.888813 2.901064 2.919410 2.931054
## [9,] 2.754050 3.115374 3.223861 3.241488 3.301564 3.325852 3.361672 3.372266
## [10,] 5.354341 5.652755 5.842389 6.078806 6.122799 6.185217 6.206025 6.372856
## [11,] 3.537185 3.676100 4.122731 4.148769 4.230996 4.404330 4.421862 4.481948
## [12,] 3.974534 4.049181 4.149865 4.152537 4.341237 4.465981 4.546151 4.609312
## [13,] 2.644419 2.793642 2.846724 2.911324 2.985548 2.996715 3.002449 3.071579
## [14,] 2.916096 3.194494 3.392172 3.515120 3.549773 3.640109 3.684189 3.688551
## [15,] 3.325104 3.442041 3.581125 3.747965 3.840330 3.879517 3.890052 4.076319
## [16,] 3.854761 3.914595 3.980755 3.997920 4.036475 4.080697 4.091582 4.100875
## [17,] 3.221538 3.651744 3.735856 3.780094 3.867068 3.969115 4.034457 4.039353
## [18,] 3.564047 3.604865 3.635257 3.764945 3.783351 3.809013 3.932273 3.933328
## [19,] 3.430484 3.674064 3.775467 3.802628 3.817044 3.850144 3.951036 3.971680
## [20,] 3.400857 3.406403 3.636335 3.639760 3.655267 3.685537 3.704882 3.748522
## [,9] [,10]
## [1,] 3.870417 3.942595
## [2,] 4.177409 4.186052
## [3,] 3.624240 3.653846
## [4,] 5.002254 5.118528
## [5,] 4.343597 4.370671
## [6,] 3.997985 4.006665
## [7,] 4.578166 4.597727
## [8,] 2.956127 2.973405
## [9,] 3.375413 3.397162
## [10,] 6.448740 6.448907
## [11,] 4.584665 4.617718
## [12,] 4.653371 4.741421
## [13,] 3.145621 3.175189
## [14,] 3.771089 3.812522
## [15,] 4.077486 4.103671
## [16,] 4.128063 4.167545
## [17,] 4.048233 4.080222
## [18,] 3.945773 4.059664
## [19,] 4.019391 4.032939
## [20,] 3.800983 3.810580
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.997 1 0.839
## 2 1 0.966 0.838
## 3 0.997 0.792 0.954
## 4 1 0.849 0.573
## 5 0.997 0.575 0.678
## 6 1 0.942 0.897
## 7 0.997 0.803 0.678
## 8 0.997 0.966 0.756
## 9 1 0.663 0.718
## 10 0.997 0.994 0.976
## # ℹ 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.254 -0.100 0.174 0.822
## 2 -0.00479 -0.250 -0.237 0.867
## 3 -0.106 0.388 -0.143 0.665
## 4 -0.208 -0.0770 -0.255 0.271
## 5 -0.116 -0.0150 -0.0765 -0.465
## 6 -0.525 -0.617 -1.03 -0.189
## 7 -0.00438 -0.304 -0.248 0.347
## 8 -0.178 -0.00738 -0.0135 0.435
## 9 -0.245 -0.0629 -0.193 -0.179
## 10 -0.00931 -0.0592 -0.430 0.207
## # ℹ 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.25 0.234 0.262 0.192 0.221 ...