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] 898 868 716 396 988 433 662 56 264 743 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  898  326  770  231   39   38  890  424  862   995
##  [2,]  868  791  576  774  567  467  735  569  192   829
##  [3,]  716  433  316    6   24  145  368  518  375   962
##  [4,]  396  995  876  765   73  864   19  109  326   486
##  [5,]  988  111  347  161  877  654  972  732  656   321
##  [6,]  433   49  789  834  375  137  206  454  102   877
##  [7,]  662  215  441  141  561  417  221  906  799   539
##  [8,]   56  337  763  745  767  116  872  183  403   827
##  [9,]  264  194  750  337  393  595  763    8  116   197
## [10,]  743  889  423  479  828  239  806  558  584   641
## [11,]  184  815  610  515  579   57  652  119   40   942
## [12,]  185   68  908  759  419  618  696  302  612   408
## [13,]  969  955   17  924   66  147  411  330  667   555
## [14,]  972  586  350  496  707  428  820  226  983   311
## [15,]  623  124  931  323  457  740  973  737  964   684
## [16,]  953  239  706  484  222  740  690  931  826   721
## [17,]  955  963  922   13  730   71  667  550  147    66
## [18,]  680  599  396  703  351  437  311  995  427   200
## [19,]   73  127  862  876  311  211  362  673  749    42
## [20,]  734  103  836  367  319  269  810  887  569    57
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.34 3.35 3.16 3.1 2.94 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.344319 2.501066 2.733622 2.994278 2.995655 3.037645 3.207173 3.218674
##  [2,] 3.349702 3.791581 3.799887 3.830255 3.874075 3.877324 3.882934 3.891190
##  [3,] 3.163055 3.381191 3.678692 3.867695 3.913557 4.009643 4.012110 4.031744
##  [4,] 3.104493 3.114722 3.120017 3.151310 3.259400 3.302124 3.329819 3.362425
##  [5,] 2.936505 3.175475 3.397790 3.425969 3.468957 3.471918 3.511781 3.529840
##  [6,] 3.320573 3.399586 3.465319 3.472019 3.595827 3.601996 3.617146 3.624391
##  [7,] 2.951628 3.177894 3.241036 3.260322 3.299411 3.319166 3.324928 3.329592
##  [8,] 3.261762 3.577959 3.828365 3.869837 3.883219 3.900658 3.905342 3.912190
##  [9,] 4.051932 4.330786 4.343024 4.391363 4.399225 4.427340 4.482436 4.527090
## [10,] 4.759412 4.822521 4.977152 4.989323 5.022956 5.138258 5.223982 5.310784
## [11,] 2.908461 3.416077 3.569294 3.595286 3.612274 3.615328 3.652722 3.688410
## [12,] 3.629164 3.859642 3.964399 4.321267 4.331667 4.352946 4.475555 4.511718
## [13,] 3.480490 3.641394 3.681034 3.718881 4.064854 4.101355 4.116444 4.136630
## [14,] 2.975113 3.619089 3.678307 3.700558 3.709579 3.732674 3.818235 3.831053
## [15,] 4.921883 5.057847 5.293889 5.325238 5.463509 5.633669 5.665634 5.758366
## [16,] 4.268692 4.314909 4.366468 4.555297 4.558253 4.601060 4.609032 4.646497
## [17,] 3.204930 3.406087 3.456594 3.681034 3.862740 3.907548 3.936249 3.961922
## [18,] 2.071327 2.180085 2.575365 2.637831 2.769970 2.803407 2.871051 2.873465
## [19,] 2.639657 2.781353 2.976959 3.107513 3.146670 3.230445 3.235311 3.259912
## [20,] 3.290888 3.414196 3.955070 4.119979 4.138253 4.147798 4.182650 4.189109
##           [,9]    [,10]
##  [1,] 3.253107 3.269419
##  [2,] 3.892192 3.929675
##  [3,] 4.037393 4.077564
##  [4,] 3.383731 3.400968
##  [5,] 3.596590 3.617175
##  [6,] 3.676392 3.707303
##  [7,] 3.354560 3.406365
##  [8,] 3.923806 4.007549
##  [9,] 4.554737 4.620931
## [10,] 5.423766 5.557649
## [11,] 3.698854 3.705198
## [12,] 4.516237 4.566306
## [13,] 4.163707 4.229181
## [14,] 3.840967 3.876327
## [15,] 5.868754 5.969259
## [16,] 4.647992 4.737137
## [17,] 3.984685 3.987965
## [18,] 2.964791 2.978666
## [19,] 3.296608 3.301372
## [20,] 4.233163 4.310539

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.IL~ `pCREB(Yb176)Di.IL~ `pBTK(Yb171)Di.IL~ `pS6(Yb172)Di.IL7~
##                  <dbl>               <dbl>              <dbl>              <dbl>
##  1               0.984               0.940              0.951              0.873
##  2               0.603               0.959              0.999              0.907
##  3               0.984               0.987              0.841              0.987
##  4               1                   0.959              0.999              0.973
##  5               0.780               1                  0.921              0.973
##  6               0.975               0.794              0.805              0.924
##  7               0.603               0.987              0.805              1    
##  8               0.966               0.988              0.960              0.973
##  9               0.994               0.988              0.927              1    
## 10               0.992               0.968              0.849              0.998
## # ... 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(I~ `CD3(Cd114)Di`
##             <dbl>          <dbl>          <dbl>             <dbl>          <dbl>
##  1         0.287          0.346           0.304            -0.284          0.965
##  2         0.580          1.46           -1.21             -1.10           0.467
##  3        -0.0224        -0.156           1.50              0.356          0.563
##  4         0.290          0.0235          0.968             0.112          1.29 
##  5         0.367          1.50           -0.781             0.803          1.39 
##  6         0.747          0.799           1.86              0.597          0.199
##  7        -0.0231         0.389          -0.151            -0.528         -0.257
##  8        -0.142          0.598          -0.200            -0.616          1.11 
##  9        -0.186          0.564           0.358             0.764         -0.397
## 10         0.264          1.63           -0.225             0.644          0.723
## # ... 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.306 0.235 0.24 0.283 0.276 ...