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] 850 648 342 53 523 415 564 863 695 522 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  850  277   68  611  733  555  144  411  357    36
##  [2,]  648   14  387   43   17   95  912   66  420   810
##  [3,]  342  667  325  828  940  326  905  630  403    17
##  [4,]   53  817  192  541  575  724  299  467  492   215
##  [5,]  523  683  935  250  905  627  818  294  679   854
##  [6,]  415  704  453  924  977  118  662  558  709   191
##  [7,]  564  748  939   81  919  686  140  392  992   756
##  [8,]  863  556  760  908   20  676  664  649  798   623
##  [9,]  695  931  162  106  354  353  467  209  509   215
## [10,]  522  607   66  195  611   55  480  437  552   394
## [11,]  424  692  522   54  784  674  608  611  460    55
## [12,]  224  766  603  997  535  488  904  765   31   461
## [13,]  732  953  405  652  692  595  349   74  697   234
## [14,]    2  648  996  320  810   47  821  912  646   460
## [15,]  336  586  388  516  881  504  902  452  754   231
## [16,]  335  613  764  592  698  904  362  690  868   654
## [17,]  421  912  262  273  955  977  611  672  437    95
## [18,]  373  569  513  511  343  862  979  375  851   960
## [19,]  455  304  504  601  257  436  909  429  336   563
## [20,]  908    8  507  676  863  649  664  198  760   167
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 4.13 3.01 2.77 4.13 3.4 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 4.130523 4.167087 4.170654 4.445916 4.510949 4.515071 4.533419 4.589469
##  [2,] 3.013207 3.018629 3.105979 3.122978 3.126406 3.142318 3.152739 3.212486
##  [3,] 2.766774 2.910265 2.969226 3.123300 3.168725 3.195781 3.200762 3.208651
##  [4,] 4.130042 4.286646 4.348400 4.379537 4.383952 4.411795 4.514507 4.624474
##  [5,] 3.395219 3.589995 3.604157 3.641887 3.719833 3.781183 3.793780 3.794525
##  [6,] 4.465635 4.540669 4.569023 4.577746 4.637489 4.664614 4.666003 4.741700
##  [7,] 2.634033 2.881735 2.887600 2.927257 2.972622 3.021849 3.030286 3.043581
##  [8,] 3.318566 3.564479 3.593188 3.612330 3.640663 3.747639 3.985513 4.258449
##  [9,] 4.082943 4.173731 4.230231 4.238371 4.385018 4.530642 4.598379 4.672785
## [10,] 3.056680 3.485689 3.573676 3.814698 3.836351 4.000317 4.004756 4.048655
## [11,] 3.370914 3.582281 3.620696 3.654803 3.694001 3.696477 3.748353 3.851958
## [12,] 3.471128 3.631353 3.834755 3.849196 3.913398 3.935926 3.962134 4.009265
## [13,] 3.568042 3.610267 3.667464 3.784714 3.934576 3.960539 4.056113 4.063669
## [14,] 3.018629 3.182182 3.297410 3.328056 3.335644 3.412268 3.438324 3.482824
## [15,] 3.127203 3.167518 3.324695 3.324986 3.445876 3.477833 3.527446 3.536305
## [16,] 4.797162 5.096301 5.203686 5.312953 5.387677 5.455912 5.542862 5.551291
## [17,] 2.310442 2.464186 2.637243 2.637653 2.697258 2.701062 2.735473 2.753542
## [18,] 3.067721 3.165625 3.175566 3.215821 3.244561 3.260511 3.262475 3.276575
## [19,] 3.732413 4.051274 4.217811 4.381576 4.400542 4.409877 4.423352 4.465460
## [20,] 3.323634 3.640663 3.655973 3.820059 3.861440 3.898579 4.009023 4.009327
##           [,9]    [,10]
##  [1,] 4.624781 4.649591
##  [2,] 3.238886 3.278219
##  [3,] 3.223906 3.293347
##  [4,] 4.726387 4.841182
##  [5,] 3.829402 3.884063
##  [6,] 4.774678 4.807101
##  [7,] 3.043799 3.075083
##  [8,] 4.285443 4.324350
##  [9,] 4.711693 4.781220
## [10,] 4.072125 4.114151
## [11,] 3.944160 4.029057
## [12,] 4.033075 4.171481
## [13,] 4.104889 4.123284
## [14,] 3.524066 3.532004
## [15,] 3.607147 3.650100
## [16,] 5.561407 5.604068
## [17,] 2.797390 2.865239
## [18,] 3.285467 3.376237
## [19,] 4.474524 4.488485
## [20,] 4.052655 4.142594

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.943                      0.999                  0.920
##  2                       0.541                      0.999                  0.656
##  3                       0.830                      0.952                  0.534
##  4                       0.830                      0.944                  0.739
##  5                       0.830                      0.999                  0.831
##  6                       0.703                      0.999                  0.534
##  7                       0.930                      0.967                  0.873
##  8                       0.989                      1                      0.674
##  9                       0.932                      0.999                  0.460
## 10                       0.967                      0.999                  0.992
## # ℹ 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.0527        -0.0208         0.460                    0.588 
##  2        -0.0120        -0.240         -0.215                   -0.0888
##  3        -0.434         -0.170         -0.150                    0.905 
##  4         0.540         -0.181          0.0845                   0.266 
##  5        -0.257         -0.215          0.205                   -0.996 
##  6        -0.259          0.765         -0.193                   -0.342 
##  7        -0.219         -0.126          0.0327                   0.456 
##  8        -0.0951         0.290         -0.0805                   0.143 
##  9        -0.477         -0.489         -0.486                    0.880 
## 10        -0.288          0.929          0.653                    0.654 
## # ℹ 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.214 0.296 0.299 0.204 0.255 ...