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] 111 393 329 479 220 267 817 385 6 622 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 111 551 951 21 393 309 307 443 710 225
## [2,] 393 632 377 163 767 631 904 660 762 76
## [3,] 329 727 824 38 979 622 747 788 781 662
## [4,] 479 568 761 309 624 616 140 832 964 951
## [5,] 220 792 433 779 981 507 943 636 86 898
## [6,] 267 9 305 224 279 347 552 464 646 742
## [7,] 817 609 436 685 804 59 550 756 661 902
## [8,] 385 347 552 530 224 464 516 46 702 742
## [9,] 6 224 464 279 267 646 967 305 347 121
## [10,] 622 747 467 376 830 255 185 613 37 886
## [11,] 962 718 362 202 198 900 795 951 704 247
## [12,] 320 146 749 426 977 498 476 53 363 881
## [13,] 621 401 582 980 213 123 707 339 675 33
## [14,] 441 698 116 295 321 650 429 145 369 546
## [15,] 736 124 252 544 206 55 346 705 642 397
## [16,] 603 235 42 39 862 195 114 91 380 751
## [17,] 771 56 520 261 161 209 926 338 794 637
## [18,] 695 691 765 621 715 948 306 467 830 673
## [19,] 605 813 484 130 791 946 504 327 216 351
## [20,] 978 554 254 719 208 821 443 111 205 270
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 2.9 2.79 3.47 2.78 3.4 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 2.895198 3.002550 3.080490 3.184604 3.193548 3.258761 3.328288 3.338686
## [2,] 2.787150 3.080818 3.280142 3.285030 3.311190 3.362343 3.379551 3.380944
## [3,] 3.473465 3.756365 3.790943 3.816626 3.833741 3.869900 3.918043 3.946341
## [4,] 2.781353 2.876176 2.976959 2.995162 3.071176 3.107513 3.231125 3.259912
## [5,] 3.395116 3.549475 3.570003 3.585570 3.591775 3.640325 3.651202 3.739791
## [6,] 3.388935 3.403529 3.482542 3.556980 4.220445 4.336553 4.457099 4.539583
## [7,] 4.120911 4.415533 4.750902 4.865578 4.870006 4.933615 4.935016 4.955969
## [8,] 2.317429 2.618296 3.597499 3.692919 3.988143 4.105591 4.119062 4.124570
## [9,] 3.403529 4.166008 4.258111 4.278096 4.318640 4.400288 4.403793 4.664271
## [10,] 2.695392 2.961800 2.972418 3.073372 3.144543 3.176760 3.190561 3.286952
## [11,] 3.035757 3.153393 3.175229 3.299160 3.307555 3.413175 3.418544 3.516271
## [12,] 3.905281 4.209103 4.245753 4.451718 4.499129 4.723569 4.724508 4.758992
## [13,] 2.782354 3.510749 3.543710 3.559376 3.631369 3.915101 3.921525 4.011109
## [14,] 4.969233 5.021415 5.260817 5.330267 5.399785 5.469066 5.470642 5.500520
## [15,] 4.505724 4.671480 4.803985 4.938966 4.975562 4.994535 5.021473 5.044814
## [16,] 2.885774 3.358817 3.487800 3.678958 3.711000 3.712156 3.715603 3.740663
## [17,] 3.862779 4.235053 4.321065 4.509705 4.608804 4.728539 4.799891 4.805413
## [18,] 2.947010 3.361468 3.418536 3.601187 3.764586 3.818412 3.841144 3.964735
## [19,] 4.090966 4.126250 4.271325 4.335344 4.534095 4.626412 4.683479 4.691542
## [20,] 2.844415 3.067158 3.169344 3.175073 3.315478 3.401002 3.414808 3.433616
## [,9] [,10]
## [1,] 3.364915 3.420631
## [2,] 3.381849 3.382394
## [3,] 3.972447 4.089640
## [4,] 3.293423 3.318214
## [5,] 3.821998 3.854713
## [6,] 4.752419 4.804014
## [7,] 4.959065 5.078920
## [8,] 4.321430 4.348134
## [9,] 4.874679 4.893000
## [10,] 3.300886 3.335064
## [11,] 3.519228 3.519340
## [12,] 4.775084 4.804957
## [13,] 4.039606 4.069782
## [14,] 5.504254 5.514457
## [15,] 5.053021 5.095593
## [16,] 3.750163 3.772547
## [17,] 4.824944 4.826902
## [18,] 4.055869 4.078780
## [19,] 4.758725 4.871558
## [20,] 3.477077 3.483039
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 x 34
## `pCrkL(Lu175)Di… `pCREB(Yb176)Di… `pBTK(Yb171)Di.… `pS6(Yb172)Di.I…
## <dbl> <dbl> <dbl> <dbl>
## 1 1 1 0.878 0.903
## 2 0.994 1 0.518 1
## 3 0.994 1 0.587 0.987
## 4 0.994 1 0.940 1
## 5 1 1 0.834 0.977
## 6 0.994 1 0.906 1
## 7 0.994 1 0.852 1
## 8 0.994 1 0.944 1
## 9 0.994 1 0.896 0.961
## 10 0.998 1 0.851 1
## # … with 990 more rows, and 30 more variables:
## # `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>,
## # `pAKT(Tb159)Di.IL7.qvalue` <dbl>, `pBLNK(Gd160)Di.IL7.qvalue` <dbl>,
## # `pP38(Tm169)Di.IL7.qvalue` <dbl>, `pSTAT5(Nd150)Di.IL7.qvalue` <dbl>,
## # `pSyk(Dy162)Di.IL7.qvalue` <dbl>, `tIkBa(Er166)Di.IL7.qvalue` <dbl>,
## # `pCrkL(Lu175)Di.IL7.change` <dbl>, `pCREB(Yb176)Di.IL7.change` <dbl>,
## # `pBTK(Yb171)Di.IL7.change` <dbl>, `pS6(Yb172)Di.IL7.change` <dbl>,
## # `cPARP(La139)Di.IL7.change` <dbl>, `pPLCg2(Pr141)Di.IL7.change` <dbl>,
## # `pSrc(Nd144)Di.IL7.change` <dbl>, `Ki67(Sm152)Di.IL7.change` <dbl>,
## # `pErk12(Gd155)Di.IL7.change` <dbl>, `pSTAT3(Gd158)Di.IL7.change` <dbl>,
## # `pAKT(Tb159)Di.IL7.change` <dbl>, `pBLNK(Gd160)Di.IL7.change` <dbl>,
## # `pP38(Tm169)Di.IL7.change` <dbl>, `pSTAT5(Nd150)Di.IL7.change` <dbl>,
## # `pSyk(Dy162)Di.IL7.change` <dbl>, `tIkBa(Er166)Di.IL7.change` <dbl>,
## # IL7.fraction.cond.2 <dbl>, density <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 x 51
## `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(… `CD3(Cd114)Di`
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 -0.0230 -0.197 -0.110 -0.901 0.0434
## 2 -0.0638 -0.235 -0.265 -0.240 -0.159
## 3 -0.114 -0.0571 -0.215 -0.887 -0.226
## 4 -0.116 -0.170 -0.402 -0.816 -0.441
## 5 -0.0337 -0.117 -0.583 0.0137 -0.0686
## 6 -0.0458 -0.195 -0.180 -0.708 0.329
## 7 -0.0744 0.170 0.0230 -0.481 0.694
## 8 -0.0698 -0.164 -0.0780 -0.688 0.894
## 9 -0.370 -0.470 -0.638 -0.102 1.96
## 10 0.200 0.333 0.227 0.779 0.358
## # … with 20 more rows, and 46 more variables: `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>,
## # `PreBCR(Ho165)Di` <dbl>, `CD43(Er167)Di` <dbl>, `CD38(Er168)Di` <dbl>,
## # `CD40(Er170)Di` <dbl>, `CD33(Yb173)Di` <dbl>, `HLA-DR(Yb174)Di` <dbl>,
## # Time <dbl>, Cell_length <dbl>, `cPARP(La139)Di` <dbl>,
## # `pPLCg2(Pr141)Di` <dbl>, `pSrc(Nd144)Di` <dbl>, `pSTAT5(Nd150)Di` <dbl>,
## # `Ki67(Sm152)Di` <dbl>, `pErk12(Gd155)Di` <dbl>, `pSTAT3(Gd158)Di` <dbl>,
## # `pAKT(Tb159)Di` <dbl>, `pBLNK(Gd160)Di` <dbl>, `pSyk(Dy162)Di` <dbl>,
## # `tIkBa(Er166)Di` <dbl>, `pP38(Tm169)Di` <dbl>, `pBTK(Yb171)Di` <dbl>,
## # `pS6(Yb172)Di` <dbl>, `pCrkL(Lu175)Di` <dbl>, `pCREB(Yb176)Di` <dbl>,
## # `DNA1(Ir191)Di` <dbl>, `DNA2(Ir193)Di` <dbl>, `Viability1(Pt195)Di` <dbl>,
## # `Viability2(Pt196)Di` <dbl>, wanderlust <dbl>, condition <chr>
# 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.288 0.288 0.243 0.295 0.256 ...