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"          
##  [3] "CD3(Cd112)Di"           "CD235-61-7-15(In113)Di"
##  [5] "CD3(Cd114)Di"           "CD45(In115)Di"         
##  [7] "CD19(Nd142)Di"          "CD22(Nd143)Di"         
##  [9] "IgD(Nd145)Di"           "CD79b(Nd146)Di"        
## [11] "CD20(Sm147)Di"          "CD34(Nd148)Di"         
## [13] "CD179a(Sm149)Di"        "CD72(Eu151)Di"         
## [15] "IgM(Eu153)Di"           "Kappa(Sm154)Di"        
## [17] "CD10(Gd156)Di"          "Lambda(Gd157)Di"       
## [19] "CD24(Dy161)Di"          "TdT(Dy163)Di"          
## [21] "Rag1(Dy164)Di"          "PreBCR(Ho165)Di"       
## [23] "CD43(Er167)Di"          "CD38(Er168)Di"         
## [25] "CD40(Er170)Di"          "CD33(Yb173)Di"         
## [27] "HLA-DR(Yb174)Di"       
## 
## $functional
##  [1] "pCrkL(Lu175)Di"  "pCREB(Yb176)Di"  "pBTK(Yb171)Di"  
##  [4] "pS6(Yb172)Di"    "cPARP(La139)Di"  "pPLCg2(Pr141)Di"
##  [7] "pSrc(Nd144)Di"   "Ki67(Sm152)Di"   "pErk12(Gd155)Di"
## [10] "pSTAT3(Gd158)Di" "pAKT(Tb159)Di"   "pBLNK(Gd160)Di" 
## [13] "pP38(Tm169)Di"   "pSTAT5(Nd150)Di" "pSyk(Dy162)Di"  
## [16] "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] 796 942 789 984 370 330 134 566 161 863 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  796  888  533   16  855  173  607  955  616   691
##  [2,]  942  659  280  489  813  356  913  722  332   222
##  [3,]  789  132   76  975  949  664  825  206  675   721
##  [4,]  984   30  514  205   35  406  484  746  152   888
##  [5,]  370  775  328   26  946  521   64    9  905   753
##  [6,]  330  988  566  827  136  403  928   57  784   608
##  [7,]  134  712  646  679  680  880  672  619  772   977
##  [8,]  566  366  827  140   34  989  988  814  454   698
##  [9,]  161  165  843   16  312  618   26  691  731   263
## [10,]  863  772  675  101  934  679  949  603  643   882
## [11,]  506  189  109  367  881  875  317   45   78   434
## [12,]  628  147   48   41  733  474  701  741  346   130
## [13,]  739  374  725  810  405  701  638  147  360   742
## [14,]  813  913  659  392  277  505  103  691   68   753
## [15,]  353  611  658  936  919   75  632  777   91    36
## [16,]    9  731  940   83  165  575  312  379  607   491
## [17,]  136  735  698  325  222  305  429  477  448   706
## [18,]  613  185  455  555   74  461  411  816  180    79
## [19,]  577   96  137  737  874  261  872  624   78   151
## [20,]  620  274   37  198  409  609  821  447  254   383
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.25 3.95 3.32 3.18 2.29 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]
##  [1,] 3.248685 3.317628 3.373888 3.413921 3.482912 3.503532 3.529605
##  [2,] 3.953308 4.210395 4.274600 4.449122 4.463766 4.479801 4.523224
##  [3,] 3.322252 3.427536 3.838429 3.991600 4.082627 4.195671 4.265636
##  [4,] 3.181706 3.356851 3.477465 3.494441 3.549906 3.617902 3.657086
##  [5,] 2.289549 2.622504 2.747565 2.752907 2.919913 2.956911 2.977494
##  [6,] 3.063779 3.522686 3.525555 3.535084 3.620843 3.706292 3.745903
##  [7,] 2.912934 2.964452 3.072944 3.205213 3.212433 3.272868 3.294551
##  [8,] 3.631617 3.739465 3.840959 3.864758 3.875702 3.901105 3.935169
##  [9,] 2.166226 2.469439 2.482662 2.545865 2.648087 2.763797 2.811036
## [10,] 2.903652 3.185238 3.392146 3.414274 3.501026 3.600127 3.619979
## [11,] 2.287895 2.697609 3.095989 3.155849 3.180094 3.315097 3.319824
## [12,] 3.654629 3.862997 3.918009 3.935926 3.973030 4.009307 4.063449
## [13,] 3.195710 4.140898 4.255452 4.282300 4.425870 4.431478 4.439363
## [14,] 2.561204 2.565487 2.886617 2.981166 3.040671 3.049882 3.061995
## [15,] 4.392323 4.487633 4.516056 4.593959 4.643178 4.658434 4.740319
## [16,] 2.545865 2.780422 2.920747 2.995162 2.995501 3.019087 3.040590
## [17,] 3.917719 4.248476 4.265772 4.368631 4.463071 4.502870 4.592141
## [18,] 2.954614 3.207663 3.315205 3.432608 3.472269 3.585439 3.597923
## [19,] 3.071717 3.756475 3.768423 3.830020 3.909412 4.239613 4.260064
## [20,] 4.022139 4.184798 4.271325 4.335344 4.378699 4.385860 4.534095
##           [,8]     [,9]    [,10]
##  [1,] 3.544249 3.547146 3.557917
##  [2,] 4.559657 4.579138 4.580091
##  [3,] 4.279151 4.281251 4.387463
##  [4,] 3.662189 3.844428 3.876545
##  [5,] 3.073188 3.090613 3.097979
##  [6,] 3.810975 3.896887 3.966544
##  [7,] 3.348177 3.377650 3.402435
##  [8,] 3.984764 4.018140 4.056766
##  [9,] 2.826947 2.845754 2.916131
## [10,] 3.640331 3.671128 3.682761
## [11,] 3.392430 3.467719 3.627843
## [12,] 4.065756 4.241986 4.392166
## [13,] 4.479635 4.480454 4.482924
## [14,] 3.093601 3.141183 3.150317
## [15,] 4.758214 4.763608 4.817173
## [16,] 3.065002 3.079463 3.125789
## [17,] 4.613031 4.650517 4.680920
## [18,] 3.602431 3.613011 3.622569
## [19,] 4.338036 4.418799 4.425750
## [20,] 4.592491 4.758725 4.760341

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 x 34
##    `pCrkL(Lu175)Di… `pCREB(Yb176)Di… `pBTK(Yb171)Di.… `pS6(Yb172)Di.I…
##               <dbl>            <dbl>            <dbl>            <dbl>
##  1            0.973            0.965            0.941            0.947
##  2            0.973            1                0.633            0.893
##  3            0.982            0.965            0.893            0.898
##  4            0.982            0.970            0.814            0.893
##  5            0.973            0.970            1                0.893
##  6            0.973            0.934            0.975            0.970
##  7            0.973            0.980            0.814            0.896
##  8            0.982            0.934            0.842            0.893
##  9            0.982            0.934            0.825            0.893
## 10            1                0.934            0.825            0.987
## # … 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>

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 x 51
##    `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(…
##             <dbl>          <dbl>          <dbl>            <dbl>
##  1        -0.420          -0.408        -0.514            -0.637
##  2        -0.923          -0.105         0.824             0.756
##  3         0.396          -0.107        -0.0873           -0.144
##  4        -0.730          -0.234        -0.223             0.764
##  5        -0.251          -0.109        -0.0732           -0.277
##  6         0.0680          0.521         0.261             0.316
##  7        -0.0645         -0.152        -0.178             0.173
##  8        -0.264          -0.269        -0.344             0.671
##  9        -0.306          -0.664        -0.507            -1.23 
## 10        -0.0700         -0.183        -0.375            -0.716
## # … with 20 more rows, and 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>, `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.272 0.214 0.223 0.252 0.316 ...