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] 664 606 621 775 490 108 412 265 580 606 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  664  208  732  152   49   46   44  438  641    24
##  [2,]  606  448  819  117  893  647  765  258  490   785
##  [3,]  621  504   83  482  872  733  926  942  331   568
##  [4,]  775  931  808  392  241  624  474  629  212   281
##  [5,]  490  823  700  606  785  117   10  448  453     2
##  [6,]  108  397  283  159  403  296  324  797  423    34
##  [7,]  412  131  159  282  747   57  753 1000  373    23
##  [8,]  265  329  131  582  196  795  302  770  619   977
##  [9,]  580  728  677  470  267  497  117  732  700   113
## [10,]  606  830  785  765    2   12  675  969  117   514
## [11,]  117  448  576  298  689  453  362   82  745   528
## [12,]  525  700  670   10  545  433  285  243  459   345
## [13,]  509  976  498  177  537  403  410  270   57   571
## [14,]  333   82  555  512  387  985  488  449  486   337
## [15,]  975  357  670  480  555  856   82   59  811   512
## [16,]  518    2  965  958  785  193  956  692  969   606
## [17,]  401  328   84  983  422  416   96   50  759   791
## [18,]   27  843  865  736  109  707  766  628  446   618
## [19,]  114  531  501  421  516  610  530  664  890   973
## [20,]  807  560  473  445  133  420  219  230   29   585
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.52 2.42 3.85 3.94 3.41 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]
##  [1,] 3.522297 3.585750 3.605184 3.628740 3.693694 3.854822 3.880637
##  [2,] 2.421302 2.760060 2.951655 3.012184 3.078784 3.079854 3.080089
##  [3,] 3.850458 3.936117 4.201927 4.248591 4.255944 4.289726 4.324657
##  [4,] 3.939728 4.022139 4.126250 4.271325 4.335344 4.385860 4.592491
##  [5,] 3.410314 3.523987 3.567784 3.761739 3.764044 3.793746 3.847133
##  [6,] 4.126217 4.162691 4.316565 4.368160 4.372758 4.469300 4.471064
##  [7,] 3.195534 3.503581 3.812188 3.845948 3.968345 4.047296 4.108990
##  [8,] 4.393115 4.758992 4.775084 4.863757 4.894715 5.091875 5.134436
##  [9,] 2.999932 3.044497 3.223405 3.320653 3.544441 3.549419 3.603670
## [10,] 2.802886 3.000942 3.215899 3.266859 3.319362 3.380427 3.404438
## [11,] 3.129680 3.188596 3.289729 3.336713 3.423831 3.447816 3.462920
## [12,] 2.898468 3.206925 3.327614 3.380427 3.383156 3.409308 3.411248
## [13,] 2.864099 3.357628 3.644647 3.713032 3.830255 3.991679 4.012792
## [14,] 4.090237 4.294670 4.343375 4.409506 4.424466 4.469931 4.507633
## [15,] 3.013031 3.714742 3.779462 3.883724 3.912209 4.089496 4.217696
## [16,] 3.054867 3.664290 3.780762 3.790289 3.814452 3.852749 3.901354
## [17,] 4.418826 4.652209 4.859045 5.096850 5.107900 5.231935 5.444937
## [18,] 3.149285 3.476955 3.595509 3.751721 3.782683 3.851345 3.990705
## [19,] 2.469692 2.544611 2.623631 2.661384 2.847252 2.858227 2.912111
## [20,] 3.831873 4.309240 4.472985 4.639011 4.843249 4.881974 4.892853
##           [,8]     [,9]    [,10]
##  [1,] 3.897496 3.942260 3.968413
##  [2,] 3.105719 3.207663 3.249908
##  [3,] 4.326873 4.421326 4.437259
##  [4,] 4.683479 4.758725 4.760341
##  [5,] 3.861519 4.022318 4.026012
##  [6,] 4.522414 4.618036 4.638863
##  [7,] 4.136718 4.186277 4.278127
##  [8,] 5.142083 5.156782 5.166006
##  [9,] 3.635642 3.671480 3.709553
## [10,] 3.406158 3.435323 3.474796
## [11,] 3.489961 3.527320 3.559208
## [12,] 3.428945 3.513848 3.550266
## [13,] 4.026742 4.089018 4.102203
## [14,] 4.564953 4.658255 4.704461
## [15,] 4.256358 4.264037 4.277128
## [16,] 3.917767 3.971170 3.994460
## [17,] 5.477928 5.585931 5.650160
## [18,] 4.113093 4.154497 4.243374
## [19,] 2.928395 2.994446 3.047483
## [20,] 4.900333 4.915724 4.937156

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.910            1                0.997            0.979
##  2            0.944            1                0.997            0.948
##  3            0.791            1                0.997            0.979
##  4            0.818            1                0.997            0.948
##  5            0.800            1                0.989            1    
##  6            0.800            1                0.984            0.955
##  7            0.989            1                0.984            0.968
##  8            0.685            1                0.858            0.955
##  9            0.896            1                0.997            0.955
## 10            0.989            0.981            0.997            0.979
## # … 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.861         -1.09          -1.17            0.285  
##  2        -0.941         -0.661         -1.05           -0.796  
##  3        -0.498         -0.506         -0.480          -0.698  
##  4        -0.0231         0.389         -0.151          -0.528  
##  5        -0.181         -0.206         -0.132          -0.518  
##  6        -0.414         -0.0421        -0.158          -0.888  
##  7        -0.176         -0.252         -0.0586         -0.0942 
##  8        -0.291         -0.0432        -0.0699         -1.46   
##  9        -0.541         -0.367         -0.808           0.00252
## 10        -0.226         -0.114         -0.0588         -0.486  
## # … 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.251 0.298 0.22 0.2 0.241 ...