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] 692 910 69 794 546 381 490 507 682 711 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 692 180 134 605 990 997 878 865 628 741
## [2,] 910 532 513 22 594 584 757 902 696 446
## [3,] 69 7 668 525 579 207 458 245 587 806
## [4,] 794 462 390 30 905 51 399 864 700 391
## [5,] 546 776 396 503 125 94 78 863 678 200
## [6,] 381 690 129 985 470 807 556 693 391 208
## [7,] 490 519 348 587 882 758 926 954 3 668
## [8,] 507 668 89 909 376 83 207 724 704 806
## [9,] 682 341 206 396 768 990 899 505 634 645
## [10,] 711 426 869 416 473 733 181 176 368 725
## [11,] 312 836 921 817 877 270 814 651 684 29
## [12,] 146 744 810 658 482 81 140 163 933 476
## [13,] 564 156 552 893 891 844 810 578 163 457
## [14,] 742 631 464 168 768 206 486 545 338 178
## [15,] 922 500 284 702 544 109 217 574 870 249
## [16,] 631 32 355 693 257 438 924 692 635 869
## [17,] 851 553 825 943 783 836 310 959 270 740
## [18,] 296 636 763 444 512 723 569 480 786 553
## [19,] 847 463 659 579 69 232 144 3 7 133
## [20,] 817 967 669 944 785 713 440 714 128 907
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.32 4.07 3.47 3.34 2.38 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.324007 3.511635 3.575698 3.579314 3.680856 3.722603 3.723496 3.759774
## [2,] 4.067747 4.321824 4.354522 4.529473 4.858189 4.917033 4.946630 4.963609
## [3,] 3.471128 3.929520 3.950912 3.962134 4.009265 4.033075 4.040213 4.231706
## [4,] 3.339712 3.536500 3.569078 3.640033 3.666499 3.704852 3.721144 3.805600
## [5,] 2.379315 2.923955 3.011268 3.035327 3.081000 3.088763 3.129264 3.216845
## [6,] 3.117810 3.287697 3.320573 3.380606 3.447816 3.481008 3.486953 3.642018
## [7,] 3.072292 3.489735 3.514234 3.582117 3.685677 3.748258 3.824062 3.850488
## [8,] 3.581189 3.627823 3.668141 3.703606 3.901613 3.902774 4.110172 4.131634
## [9,] 3.495569 3.818980 3.842952 4.016127 4.070209 4.189340 4.289584 4.296608
## [10,] 3.037817 3.761022 3.898579 4.098345 4.142594 4.156749 4.223230 4.265053
## [11,] 2.210230 2.288887 2.772077 2.781540 2.869389 2.874490 2.890439 2.950053
## [12,] 3.273305 3.496418 3.499258 3.543294 3.549349 3.563866 3.672853 3.745756
## [13,] 2.958366 3.310175 3.644414 3.683244 3.695702 3.716734 3.721237 3.754304
## [14,] 4.141562 4.148297 4.247029 4.338482 4.351993 4.385360 4.422976 4.424390
## [15,] 3.482856 3.756475 3.768423 3.830020 4.092452 4.176408 4.425750 4.528185
## [16,] 3.371937 3.375490 3.378451 3.417463 3.443673 3.469108 3.474380 3.477685
## [17,] 3.136938 3.278147 3.339877 3.884283 3.886972 3.890158 3.896328 3.935037
## [18,] 2.539880 2.750091 2.931691 2.969394 3.014138 3.188564 3.204252 3.358280
## [19,] 3.796821 3.884775 4.429177 4.512180 4.664446 4.830093 4.835052 4.839944
## [20,] 3.349702 3.456222 3.595167 3.799887 3.833191 3.874075 3.877324 3.882934
## [,9] [,10]
## [1,] 3.760152 3.778633
## [2,] 4.971256 4.976674
## [3,] 4.243348 4.294548
## [4,] 3.846483 3.870368
## [5,] 3.232554 3.265382
## [6,] 3.647594 3.656394
## [7,] 3.929520 3.984988
## [8,] 4.146137 4.301980
## [9,] 4.336782 4.363452
## [10,] 4.284476 4.290094
## [11,] 3.002259 3.006277
## [12,] 3.810625 3.870339
## [13,] 3.755291 3.813738
## [14,] 4.458244 4.497944
## [15,] 4.820698 4.822509
## [16,] 3.539122 3.553833
## [17,] 3.974056 4.001891
## [18,] 3.393188 3.467801
## [19,] 4.848027 4.881011
## [20,] 3.892192 3.893678
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.927 0.905 0.979
## 2 0.766 0.943 0.996
## 3 0.792 0.791 0.996
## 4 1 0.766 0.996
## 5 1 0.481 0.979
## 6 0.499 0.616 0.996
## 7 0.887 0.826 0.994
## 8 0.932 0.508 1
## 9 1 0.901 0.979
## 10 0.919 0.486 0.996
## # ℹ 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.110 -0.00933 1.03 -0.0130
## 2 -0.267 -0.158 0.863 -0.698
## 3 0.431 -0.0942 -0.148 -1.56
## 4 -0.0949 0.966 0.577 -1.15
## 5 -0.249 -0.258 -0.0365 -0.467
## 6 -0.110 0.373 0.316 0.891
## 7 -0.249 -0.105 0.988 -0.718
## 8 0.902 0.203 1.80 0.597
## 9 0.254 0.371 0.0930 -0.145
## 10 -0.439 -0.826 -0.997 -0.814
## # ℹ 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.257 0.199 0.226 0.253 0.303 ...