K-nearest neighbors:

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] 633 827 125 365 640 713 342 741 640 469 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  633  476  207  741   65  794  927  131  828    30
##  [2,]  827  181  983  160  471  600  446  630  951    60
##  [3,]  125   69  545  160  856  485  951  770  961   570
##  [4,]  365  429  851  824  690  207  634  367  412   935
##  [5,]  640   59  362  743  955  561  972  772  547   862
##  [6,]  713  210  768  835  759  208  453  774  310   527
##  [7,]  342  687  808  536  655  417   95  474  744    23
##  [8,]  741  766  606  598  325  927   87  558   68   355
##  [9,]  640  955  973  745  217  349  262  770  104    77
## [10,]  469  770   69  961  526  485  738  732  545   834
## [11,]  382  530  423  232  974  474  105  279  219   752
## [12,]  396  793  965  603  356   58  511  890  220   437
## [13,]  112  860  362  600   77  557  433   59  145   862
## [14,]   50   37  877  726  656  781  528  969  806   914
## [15,]  533  111  537  239  904  731  918  993  876    80
## [16,]  384  676  328  488  976  217  947  561  628   745
## [17,]  529  125  901  757  770  745  945  966  818   420
## [18,]  685  766  106  434  611  427   87  675  169     8
## [19,]  926  287  673  902   71  138  149  732  629   669
## [20,]  933  941  320  637  396  656  271  793  668   721
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 4.86 3.98 2.71 5.72 3.4 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 4.861345 5.184222 5.312682 5.317264 5.553150 5.585368 5.623047 5.636201
##  [2,] 3.975606 4.026514 4.054488 4.129565 4.221898 4.285979 4.287049 4.357351
##  [3,] 2.709035 2.802767 2.904681 3.016592 3.046709 3.211902 3.280049 3.287317
##  [4,] 5.719366 5.948293 5.976489 6.209535 6.234971 6.276826 6.287511 6.336659
##  [5,] 3.395219 3.487585 3.497812 3.589995 3.604157 3.719833 3.781183 3.793780
##  [6,] 2.668782 3.197880 3.221092 3.337421 3.538971 3.589628 3.637605 3.717566
##  [7,] 3.657222 4.090961 4.133583 4.152012 4.204723 4.228240 4.346444 4.487537
##  [8,] 2.948288 3.048076 3.380441 3.405524 3.748988 3.773205 3.778793 3.824310
##  [9,] 2.844415 3.055669 3.067158 3.334884 3.370648 3.384074 3.433616 3.453077
## [10,] 1.977624 2.684652 2.728091 2.733886 2.854364 2.976749 3.013004 3.037051
## [11,] 3.472081 3.556273 3.644647 3.803833 3.873390 3.900894 3.902911 3.935712
## [12,] 2.102466 2.657977 2.941177 2.969952 3.098878 3.121466 3.123740 3.129891
## [13,] 3.629164 3.845850 3.859642 3.964399 4.331667 4.352946 4.443610 4.457692
## [14,] 4.327196 4.371005 4.375177 4.386573 4.437174 4.444409 4.476672 4.510921
## [15,] 2.980235 3.074132 3.468961 3.487093 3.524122 3.618231 3.682096 3.812958
## [16,] 2.781353 2.960756 2.992707 3.015991 3.096518 3.118008 3.148277 3.274980
## [17,] 1.758925 2.482125 2.625114 2.686341 2.827630 2.847653 2.921518 2.929386
## [18,] 3.531789 3.543183 3.548345 3.969390 3.974825 4.050110 4.057591 4.062028
## [19,] 4.127802 4.208518 4.227662 4.264990 4.350383 4.372530 4.456313 4.539851
## [20,] 4.162920 4.332648 4.471731 4.778760 4.841182 4.875379 4.928045 4.935723
##           [,9]    [,10]
##  [1,] 5.703959 5.705084
##  [2,] 4.370370 4.518312
##  [3,] 3.328266 3.333672
##  [4,] 6.362762 6.558309
##  [5,] 3.884063 3.900559
##  [6,] 3.759464 3.801203
##  [7,] 4.578400 4.595127
##  [8,] 3.826311 3.826477
##  [9,] 3.531297 3.541155
## [10,] 3.067935 3.087184
## [11,] 3.993443 4.001449
## [12,] 3.193380 3.256615
## [13,] 4.511718 4.525616
## [14,] 4.587073 4.636025
## [15,] 3.920528 3.985371
## [16,] 3.343021 3.361775
## [17,] 2.961424 2.967181
## [18,] 4.067571 4.092243
## [19,] 4.541981 4.550227
## [20,] 4.951130 4.993592

Finding scone values:

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.936                      1                      0.913
##  2                       0.831                      0.949                  0.932
##  3                       0.668                      1                      0.849
##  4                       0.673                      0.929                  0.849
##  5                       1                          1                      1    
##  6                       1                          0.902                  0.987
##  7                       0.965                      0.960                  1    
##  8                       1                          0.902                  0.876
##  9                       0.856                      0.902                  0.980
## 10                       0.766                      0.902                  0.849
## # ℹ 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>, …

For programmers: performing additional per-KNN statistics

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.0951          0.290        -0.0805                    0.143
##  2        -0.0645         -0.152        -0.178                     0.173
##  3         0.0680          0.521         0.261                     0.316
##  4        -0.477          -0.489        -0.486                     0.880
##  5        -0.254          -0.193        -0.121                    -0.267
##  6        -0.420          -0.408        -0.514                    -0.637
##  7        -0.248          -0.217        -0.352                     0.628
##  8         0.718           0.475         1.05                      0.665
##  9         0.486           0.946         0.318                     0.123
## 10        -0.467          -0.127        -0.522                     0.527
## # ℹ 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.172 0.215 0.296 0.152 0.253 ...