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] 317 563 497 698 772 997 168 81 195 767 ...wand.nn[[1]][1:20, 1:10]##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  317  664   74  839  453  313  632  535   69    92
##  [2,]  563  539  753  640  926  353  623  862   49   780
##  [3,]  497  937  882  503  968   89   90  205  165   499
##  [4,]  698  416  551  435  131   86  544  946  377   906
##  [5,]  772  377  286  416  679  935  133  999  542   136
##  [6,]  997  168  191  387  549  167  649  899  376   216
##  [7,]  168  736  376  427  997   76  185    6  510   371
##  [8,]   81  924  933  368  128   64  619  362   72   509
##  [9,]  195  491   70  585  987  986  217  153  178    54
## [10,]  767  130  174  962  346  791  765  818  423   749
## [11,]  153  651  840  485  981  961  148  847  550   489
## [12,]  589  314   91  170  549  349  525  687  216   622
## [13,]  806  115   38  993  512  865  259  495  336   823
## [14,]  642  959  627  775  145  352  430  369  908   401
## [15,]   76  928  589  207  768  413  914  539  670   371
## [16,]  304  643  222  300  933  355  538  842  187   924
## [17,]  622  521   63  841  881  100   71  371  978   956
## [18,]  376  736  387  678  899  204  564  265    6   577
## [19,]  691  433  304   46  424  187  738 1000  988   458
## [20,]  389  660  782  776  219  806   96  843  790   186# Distance
str(wand.nn[[2]])##  num [1:1000, 1:30] 2.62 3.71 2.53 3.22 2.94 ...wand.nn[[2]][1:20, 1:10]##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.622165 2.792039 2.857515 2.919274 2.920141 2.985558 3.054258 3.104714
##  [2,] 3.706606 3.783263 3.993242 4.153796 4.176097 4.333011 4.371364 4.436539
##  [3,] 2.531550 2.680070 2.901289 2.959863 2.990677 3.067011 3.181871 3.206544
##  [4,] 3.216050 3.676839 3.708048 3.810186 3.813260 3.887431 3.911686 3.918353
##  [5,] 2.936505 3.175475 3.397790 3.442462 3.499053 3.511781 3.523775 3.630752
##  [6,] 3.642676 3.863808 3.958279 4.019967 4.050739 4.106526 4.110042 4.214606
##  [7,] 3.645440 4.285236 4.359537 4.392323 4.445316 4.449738 4.500594 4.578365
##  [8,] 4.085345 4.093142 4.241363 4.243222 4.253033 4.306909 4.309967 4.333221
##  [9,] 3.656178 3.762669 3.980797 4.156667 4.228298 4.262527 4.266058 4.354946
## [10,] 2.967065 3.094524 3.149332 3.290095 3.307555 3.352743 3.356033 3.449334
## [11,] 3.577952 3.590617 3.695503 3.787307 3.798852 3.998825 4.011109 4.017709
## [12,] 3.379999 3.632355 3.786198 3.924678 3.957302 3.969359 3.974060 4.005788
## [13,] 2.710660 2.824527 2.880800 2.924190 2.935566 2.967586 3.107892 3.113533
## [14,] 3.714277 4.095550 4.996903 5.021013 5.183568 5.193347 5.433805 5.434760
## [15,] 3.197880 3.920033 4.024391 4.148769 4.154574 4.308642 4.343474 4.401813
## [16,] 3.132644 3.478325 3.576664 3.612197 3.754792 3.778793 3.809381 3.829707
## [17,] 3.631353 3.836526 3.935926 3.967010 4.009265 4.033075 4.040213 4.171481
## [18,] 4.859045 5.031070 5.231935 5.243134 5.332073 5.610731 5.686843 5.714301
## [19,] 3.541029 3.548345 4.059065 4.151482 4.213986 4.230856 4.438286 4.565029
## [20,] 3.031373 3.182182 3.190289 3.335644 3.394667 3.438324 3.450229 3.482824
##           [,9]    [,10]
##  [1,] 3.109715 3.134536
##  [2,] 4.487755 4.575667
##  [3,] 3.222205 3.262160
##  [4,] 4.008407 4.012537
##  [5,] 3.637565 3.664416
##  [6,] 4.353556 4.404047
##  [7,] 4.611345 4.796276
##  [8,] 4.350494 4.360575
##  [9,] 4.393399 4.422883
## [10,] 3.564171 3.575291
## [11,] 4.069016 4.165658
## [12,] 4.134983 4.169041
## [13,] 3.161823 3.241092
## [14,] 5.522612 5.808574
## [15,] 4.410626 4.419296
## [16,] 3.842770 3.880138
## [17,] 4.269381 4.294548
## [18,] 5.769977 5.792674
## [19,] 4.579494 4.588447
## [20,] 3.515227 3.532004This 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.995                      0.961                  0.937
##  2                       0.998                      0.961                  0.981
##  3                       0.998                      0.974                  0.870
##  4                       0.995                      0.974                  0.944
##  5                       0.995                      0.974                  0.722
##  6                       0.998                      0.974                  0.937
##  7                       0.995                      0.974                  0.953
##  8                       0.998                      0.974                  1    
##  9                       0.995                      1                      0.937
## 10                       0.995                      0.974                  0.937
## # ℹ 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>, …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.138         -0.170          0.651                   -0.824 
##  2        -0.0648        -0.0166        -0.0818                  -0.681 
##  3        -0.0498         0.201          0.573                   -0.355 
##  4        -0.302         -0.677          0.955                   -1.54  
##  5        -0.262         -0.454         -0.110                   -0.447 
##  6        -0.0604        -0.246         -0.243                   -0.914 
##  7        -1.44          -0.308         -0.640                    0.571 
##  8         0.610         -0.718          1.59                    -1.71  
##  9        -0.823         -1.07          -1.29                    -0.0692
## 10        -0.610         -0.613          1.13                    -2.39  
## # ℹ 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.313 0.206 0.298 0.247 0.272 ...