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] 309 576 185 762 906 317 658 52 200 594 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 309 752 152 772 288 130 560 817 380 818
## [2,] 576 104 863 678 152 24 772 880 965 818
## [3,] 185 538 392 81 948 443 939 702 506 789
## [4,] 762 447 937 682 453 820 471 492 462 868
## [5,] 906 567 20 322 489 481 967 990 360 855
## [6,] 317 738 471 412 769 414 272 606 517 79
## [7,] 658 288 237 309 681 989 560 611 874 772
## [8,] 52 570 285 181 314 321 567 416 206 120
## [9,] 200 29 251 110 895 878 513 167 93 41
## [10,] 594 144 572 699 359 996 750 715 327 75
## [11,] 94 713 805 594 573 478 144 921 524 715
## [12,] 481 987 616 522 766 791 720 455 266 881
## [13,] 684 818 752 184 309 380 152 817 944 561
## [14,] 127 865 124 232 282 611 612 784 901 770
## [15,] 865 688 282 630 595 901 380 635 612 965
## [16,] 978 894 752 959 989 697 269 104 152 165
## [17,] 770 295 348 952 274 450 430 129 811 946
## [18,] 646 828 238 886 224 928 940 300 289 138
## [19,] 764 970 836 469 82 543 497 301 402 107
## [20,] 334 501 428 481 398 111 276 76 318 169
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.59 3.46 3.44 3.15 3.59 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.591812 3.891591 4.033835 4.111805 4.119174 4.313440 4.359794 4.379746
## [2,] 3.458344 3.473465 3.744522 3.833741 3.918422 3.942595 3.946341 3.965424
## [3,] 3.440584 4.092943 4.184986 4.192058 4.199042 4.252569 4.338093 4.365472
## [4,] 3.145088 3.221582 3.244287 3.314718 3.354773 3.437614 3.453370 3.504264
## [5,] 3.586537 3.936064 3.959967 4.018576 4.041363 4.118433 4.160121 4.194898
## [6,] 1.977624 2.684652 2.767854 2.846347 2.854364 2.889148 2.919401 3.013004
## [7,] 3.088610 3.266114 3.527949 3.807489 3.827857 3.849347 3.886697 3.905109
## [8,] 3.034984 3.227914 3.297089 3.370297 3.382220 3.442843 3.455540 3.467135
## [9,] 3.669566 4.183429 4.603207 4.650348 4.672440 4.770431 4.933183 4.950767
## [10,] 3.731366 3.816896 3.877793 4.244423 4.259205 4.296280 4.410003 4.451484
## [11,] 3.535263 3.647931 3.910030 4.160745 4.269226 4.392176 4.529670 4.601240
## [12,] 3.947773 4.211203 4.321813 4.568434 4.620909 4.648086 4.664120 4.683237
## [13,] 2.701170 2.914838 2.954875 3.024043 3.174962 3.259954 3.328766 3.401148
## [14,] 4.606516 5.062771 5.114901 5.126062 5.167766 5.197413 5.211719 5.328068
## [15,] 3.833191 3.874075 3.877324 3.882934 3.891190 3.893678 3.918526 3.929675
## [16,] 3.302046 3.591900 3.730543 3.966585 3.996024 4.060669 4.162248 4.175373
## [17,] 3.514534 3.577183 3.650517 3.671534 3.738404 3.778731 3.789020 3.792677
## [18,] 4.354256 4.439486 4.460192 4.480005 4.605682 4.669324 4.676081 4.728339
## [19,] 3.237406 3.531859 3.838998 3.939603 4.018374 4.094238 4.234621 4.310100
## [20,] 3.169058 3.322713 3.481355 3.518717 3.522506 3.613722 3.666589 3.677028
## [,9] [,10]
## [1,] 4.451792 4.452920
## [2,] 3.972447 4.041538
## [3,] 4.380111 4.694595
## [4,] 3.535628 3.546414
## [5,] 4.198108 4.200540
## [6,] 3.013594 3.040271
## [7,] 3.916193 3.986988
## [8,] 3.506941 3.534486
## [9,] 5.048756 5.098942
## [10,] 4.484917 4.496911
## [11,] 4.634236 4.661430
## [12,] 4.732648 4.828554
## [13,] 3.418359 3.437772
## [14,] 5.346938 5.389147
## [15,] 3.933491 4.059169
## [16,] 4.192477 4.237619
## [17,] 3.818419 3.852145
## [18,] 4.804382 4.877563
## [19,] 4.385194 4.453528
## [20,] 3.690773 3.756877
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.995
## 2 0.955 0.958 1
## 3 0.844 1 0.999
## 4 0.955 1 0.995
## 5 0.844 1 0.995
## 6 0.895 1 1
## 7 0.987 0.958 0.995
## 8 0.867 1 0.995
## 9 0.844 0.958 0.995
## 10 0.918 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 -0.208 -0.0770 -0.255 0.271
## 2 0.237 -0.0447 -0.119 -0.717
## 3 -0.397 -0.363 0.554 -0.649
## 4 0.946 -0.155 -0.0662 -1.13
## 5 -0.00101 -0.182 -0.0879 0.411
## 6 -0.0575 -0.166 -0.232 0.201
## 7 0.478 -0.132 0.0318 -0.0733
## 8 -0.237 -0.349 0.478 0.682
## 9 -0.0645 -0.488 -0.289 -0.325
## 10 -0.153 -0.217 -0.192 0.753
## # ℹ 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.223 0.241 0.214 0.273 0.234 ...