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] 134 690 710 773 508 252 274 339 270 149 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 134 475 912 374 136 272 298 922 539 812
## [2,] 690 175 18 177 767 396 410 979 985 693
## [3,] 710 431 921 933 741 657 491 389 83 998
## [4,] 773 632 449 630 777 859 570 295 247 901
## [5,] 508 632 933 772 491 906 956 165 973 319
## [6,] 252 361 51 31 49 10 664 149 391 825
## [7,] 274 62 788 688 570 661 449 739 777 914
## [8,] 339 875 93 843 394 776 437 794 46 478
## [9,] 270 566 758 241 915 606 868 507 119 911
## [10,] 149 808 292 650 6 213 252 51 775 49
## [11,] 310 271 329 174 730 142 95 245 97 470
## [12,] 178 926 707 646 818 838 116 698 568 705
## [13,] 264 709 755 806 828 540 795 801 68 263
## [14,] 868 501 184 428 321 618 965 970 55 215
## [15,] 56 137 903 392 164 468 528 133 398 112
## [16,] 627 670 654 83 967 212 761 533 266 736
## [17,] 101 729 606 438 720 321 679 259 538 702
## [18,] 979 403 396 985 353 984 175 82 94 263
## [19,] 867 78 567 48 123 162 964 865 364 685
## [20,] 261 456 814 310 236 561 275 402 242 947
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.66 3.56 2.3 3.08 4.64 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.655312 3.710415 3.821441 3.843127 3.883353 3.896625 3.922677 3.963700
## [2,] 3.564791 3.598313 3.605968 3.807738 3.898207 3.905923 4.008878 4.049655
## [3,] 2.296113 2.740851 2.798149 2.882021 2.882089 2.973589 3.006233 3.012123
## [4,] 3.075207 3.474140 3.850334 4.021719 4.173033 4.262167 4.418517 4.430607
## [5,] 4.639681 5.078659 5.102001 5.177237 5.177547 5.242917 5.292503 5.345661
## [6,] 3.388935 3.582441 3.738259 3.814607 4.137591 4.146279 4.150085 4.198159
## [7,] 3.419766 3.670826 3.786256 3.893723 3.937955 4.004221 4.051921 4.090277
## [8,] 3.902280 4.021114 4.170058 4.178921 4.200376 4.378969 4.463472 4.489206
## [9,] 3.169344 3.307120 3.394217 3.465452 3.498359 3.585324 3.608535 3.609777
## [10,] 3.679249 3.904158 3.942074 3.943316 4.146279 4.253201 4.313055 4.395033
## [11,] 4.001273 4.186758 4.379717 4.423305 4.501913 4.516977 4.593997 4.635691
## [12,] 3.066991 3.112308 3.132371 3.197260 3.241773 3.445678 3.478168 3.478756
## [13,] 3.432329 3.463242 3.525090 3.672116 3.686215 3.692707 3.743823 3.750575
## [14,] 2.565037 2.573734 2.635397 2.650510 2.667544 2.752493 2.832915 2.862492
## [15,] 3.113533 3.567572 3.636588 3.712061 3.723023 3.757469 3.778574 3.811762
## [16,] 2.948288 3.380441 3.490019 3.748988 3.760997 3.778793 3.824310 3.846804
## [17,] 3.209440 3.298959 3.321523 3.346877 3.410238 3.440586 3.538967 3.612299
## [18,] 2.887469 3.174962 3.205037 3.206316 3.228291 3.232466 3.284541 3.305843
## [19,] 3.938531 3.960518 4.059438 4.075821 4.125478 4.247613 4.343641 4.346685
## [20,] 2.999142 3.674934 3.688778 3.719685 3.740716 3.797004 3.846122 3.855780
## [,9] [,10]
## [1,] 3.975300 3.986317
## [2,] 4.105608 4.135997
## [3,] 3.012184 3.039439
## [4,] 4.453382 4.465567
## [5,] 5.372905 5.383269
## [6,] 4.318640 4.337483
## [7,] 4.155429 4.157550
## [8,] 4.643685 4.650219
## [9,] 3.689110 3.787543
## [10,] 4.469473 4.472910
## [11,] 4.678632 4.736042
## [12,] 3.595115 3.647111
## [13,] 3.760203 3.774978
## [14,] 2.964926 2.968273
## [15,] 3.818075 3.820665
## [16,] 3.904921 3.909770
## [17,] 3.632449 3.684777
## [18,] 3.314668 3.373050
## [19,] 4.351326 4.434867
## [20,] 3.892582 4.012733
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 0.891 1 0.994 0.979
## 2 0.997 1 0.691 0.905
## 3 0.567 1 0.781 1
## 4 0.966 1 0.853 0.979
## 5 0.891 1 0.707 0.904
## 6 0.418 1 1 0.997
## 7 0.969 1 0.873 0.979
## 8 0.756 1 0.691 0.997
## 9 0.916 1 0.619 0.979
## 10 0.567 1 0.594 0.906
## # … 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.121 -0.133 -0.0150 0.0321 0.292
## 2 -0.241 -0.0568 -0.148 -0.0476 -0.0679
## 3 0.481 0.141 -0.490 0.263 -0.234
## 4 -0.361 -0.272 -0.0987 -1.79 -0.0807
## 5 -0.0612 -0.199 -0.499 -0.615 0.132
## 6 -0.429 -0.0598 -0.688 0.367 0.0952
## 7 -0.248 -0.698 -0.0275 -1.22 -0.946
## 8 -0.0165 -0.0589 -0.00434 -1.10 0.978
## 9 -0.100 -0.0925 -0.224 -0.475 0.596
## 10 -0.280 -0.155 -0.494 -0.923 -0.347
## # … 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.239 0.236 0.323 0.22 0.184 ...