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] 422 84 716 276 614 115 832 378 116 273 ...
wand.nn[[1]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 422 979 252 356 700 901 107 170 921 582
## [2,] 84 621 781 669 323 402 460 506 246 908
## [3,] 716 882 632 699 143 546 688 423 614 459
## [4,] 276 740 115 677 863 572 243 19 6 731
## [5,] 614 599 615 601 916 699 575 882 459 233
## [6,] 115 276 863 740 537 19 623 188 282 731
## [7,] 832 694 874 716 882 61 585 639 263 862
## [8,] 378 20 52 613 387 30 627 654 815 893
## [9,] 116 359 692 598 830 523 252 617 704 789
## [10,] 273 929 679 39 26 156 293 484 94 757
## [11,] 379 751 362 744 231 891 810 892 24 573
## [12,] 765 109 559 659 401 504 425 990 67 968
## [13,] 436 734 422 501 359 252 415 550 966 208
## [14,] 371 365 954 218 575 110 965 421 388 887
## [15,] 292 732 611 309 618 614 986 971 548 83
## [16,] 633 887 954 645 726 441 95 374 213 942
## [17,] 488 599 255 817 396 435 882 79 674 601
## [18,] 487 862 452 464 753 212 287 615 923 926
## [19,] 115 373 740 797 282 276 925 840 863 938
## [20,] 613 8 234 893 687 750 30 387 378 939
# Distance
str(wand.nn[[2]])
## num [1:1000, 1:30] 3.86 2.62 3.33 2.9 3.07 ...
wand.nn[[2]][1:20, 1:10]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 3.862997 3.865313 3.918009 3.935926 3.986428 4.094772 4.125478 4.197144
## [2,] 2.622165 2.678530 2.792039 2.800239 2.810747 2.985558 3.109715 3.124076
## [3,] 3.332471 3.522466 3.566407 3.580990 3.669855 3.684584 3.700287 3.713510
## [4,] 2.902729 3.200399 3.557851 3.601325 3.612108 3.722776 3.786677 3.856667
## [5,] 3.066839 3.318031 3.472346 3.497388 3.536131 3.594913 3.619428 3.672726
## [6,] 3.241093 3.331857 3.613441 3.650933 3.809013 3.811762 3.892141 4.000463
## [7,] 2.469708 2.561204 2.565487 2.695595 2.886617 3.040671 3.093601 3.108622
## [8,] 2.370303 3.025148 3.196974 3.255099 3.388649 3.421620 3.486982 3.514672
## [9,] 3.061600 3.827220 3.928069 3.960628 3.980267 4.134095 4.200801 4.260540
## [10,] 4.556114 4.574178 4.628427 4.907949 5.107309 5.123233 5.180396 5.187915
## [11,] 3.618361 3.844795 3.888269 3.976899 4.009838 4.053995 4.196532 4.224075
## [12,] 3.901637 4.121177 4.194784 4.260650 4.336908 4.352691 4.470466 4.476576
## [13,] 4.119575 4.349510 4.533777 4.548128 4.661790 4.716389 4.914221 4.962969
## [14,] 3.729572 3.891359 3.957039 3.992877 4.012199 4.018576 4.035777 4.041363
## [15,] 5.367204 5.700925 5.774552 5.835787 6.070138 6.138841 6.210533 6.219095
## [16,] 2.758565 3.245562 3.399222 3.453275 3.534975 3.589407 3.594865 3.605801
## [17,] 3.279469 3.330503 3.366520 3.444464 3.460163 3.529149 3.603576 3.607961
## [18,] 2.935590 3.484421 3.496558 3.520905 3.539332 3.571924 3.608873 3.634938
## [19,] 2.068677 2.803685 2.904984 3.263760 3.310175 3.357944 3.407100 3.433735
## [20,] 2.912644 3.025148 3.067721 3.175566 3.201198 3.215821 3.256762 3.387620
## [,9] [,10]
## [1,] 4.243892 4.324696
## [2,] 3.160227 3.235715
## [3,] 3.722789 3.782478
## [4,] 4.187781 4.251814
## [5,] 3.675817 3.750799
## [6,] 4.005300 4.010851
## [7,] 3.126057 3.153840
## [8,] 3.524301 3.594484
## [9,] 4.273335 4.280235
## [10,] 5.227210 5.258301
## [11,] 4.341237 4.347911
## [12,] 4.545089 4.547160
## [13,] 4.996517 5.032681
## [14,] 4.058339 4.127169
## [15,] 6.232083 6.235203
## [16,] 3.615929 3.616262
## [17,] 3.658713 3.682184
## [18,] 3.642226 3.689198
## [19,] 3.433897 3.441573
## [20,] 3.411283 3.412400
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.992 0.987 0.985 0.925
## 2 0.646 0.998 0.928 0.999
## 3 0.782 0.821 0.947 1
## 4 0.962 0.952 0.955 0.841
## 5 0.750 0.925 0.853 1
## 6 0.986 0.938 0.947 0.739
## 7 0.860 0.987 1 1
## 8 0.880 0.925 0.938 0.841
## 9 0.782 0.925 0.989 1
## 10 1 0.987 0.867 0.996
## # … 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 -1.00 -0.700 -0.236 -1.37 -1.03
## 2 -0.194 -0.475 -0.0613 -0.137 -0.262
## 3 -1.15 -0.601 -0.117 -0.146 -0.832
## 4 -0.459 -0.356 -0.230 -0.152 0.436
## 5 -0.327 -0.764 -0.384 -1.06 0.309
## 6 -0.620 -1.06 -0.0891 0.454 -1.22
## 7 -0.439 -0.422 -0.257 -0.478 -0.501
## 8 -0.244 -0.226 -0.142 0.893 -0.375
## 9 -0.168 -0.0518 -0.321 0.161 0.652
## 10 -0.162 0.411 -0.196 -0.954 -0.168
## # … 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.226 0.31 0.258 0.231 0.258 ...