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] 354 69 852 955 44 92 915 371 369 84 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  354  951  407  232  698  776  347   11  977   539
##  [2,]   69  255  999  997  591  752  867  398  986   727
##  [3,]  852  641  698  347  834   93  977   11   23   446
##  [4,]  955  889   84  620  932  835  669  267  847   340
##  [5,]   44  474  312  266  957  515  692  541  931    76
##  [6,]   92  221  280  412  126  136  371  717  469   746
##  [7,]  915  586  701  241   18  256  356  360  483   334
##  [8,]  371  710  300  265  815  746  624  588  830   631
##  [9,]  369  883  520  943  923  954  904  349  712   684
## [10,]   84  112  570  128  835  510   38  263  937   492
## [11,]  698  354  852   23  648  347  407   89  977   693
## [12,]  998  995  519  987  103  968  721   27  188   870
## [13,]  917  157  142   66  896   29  608  926  565   812
## [14,]  535  199  102  851  236   22  612  692  567   101
## [15,]  709  187  369  520  830  265  898  256  223   684
## [16,]  123  964  707  885  850   40  155  331  126   837
## [17,]  497  552  837  746  223  686  964  628  830   877
## [18,]  835    7   23   41   93  797  368   32   52   449
## [19,]  982  749  187  300  606   90   49   86  198   759
## [20,]  750   76  931  630  793  325   44  457  524   100
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.57 3.39 2.96 5.32 3.88 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]
##  [1,] 3.565829 3.718881 3.751039 3.829118 3.932041 3.987027 4.064854
##  [2,] 3.390046 3.404682 3.511438 3.659687 3.687154 3.712973 3.731367
##  [3,] 2.962117 3.020926 3.160619 3.206958 3.276811 3.301880 3.404873
##  [4,] 5.317264 5.323891 5.486029 5.623047 5.703959 5.803333 5.848009
##  [5,] 3.882224 4.450845 4.463140 4.472787 4.501910 4.682387 4.750415
##  [6,] 2.823612 2.929884 2.992927 3.194494 3.346877 3.374529 3.386441
##  [7,] 3.285076 3.439879 3.722028 3.757769 3.770940 3.786052 3.816174
##  [8,] 3.331935 3.528960 3.535991 3.598409 3.693053 3.739082 3.872594
##  [9,] 3.645782 4.070209 4.189340 4.283296 4.289584 4.336782 4.370202
## [10,] 5.038568 5.239474 5.624392 5.703896 5.744498 5.759549 5.794609
## [11,] 2.453201 2.470231 2.967051 3.189332 3.206891 3.264745 3.343012
## [12,] 3.030203 3.596605 3.611057 3.634253 3.877377 3.889519 3.897943
## [13,] 3.066980 3.220361 3.313665 3.466077 3.579748 3.656134 3.679669
## [14,] 2.704594 2.799328 2.982515 3.050722 3.061985 3.122108 3.156020
## [15,] 3.202113 3.365734 3.400571 3.418780 3.477230 3.493082 3.506575
## [16,] 2.501066 2.623459 2.723471 2.837718 2.885968 2.926205 3.184604
## [17,] 2.781966 2.970242 2.995856 3.072350 3.088763 3.154764 3.196968
## [18,] 3.682086 3.770940 3.786448 3.816526 3.929530 4.022458 4.096753
## [19,] 3.069141 3.108465 3.151816 3.174602 3.180546 3.223073 3.233584
## [20,] 3.819830 4.032106 4.236799 4.238615 4.240554 4.329188 4.365104
##           [,8]     [,9]    [,10]
##  [1,] 4.101355 4.121206 4.189944
##  [2,] 3.795927 3.878207 3.887542
##  [3,] 3.521048 3.578380 3.595170
##  [4,] 5.854434 5.884751 5.888397
##  [5,] 4.847870 4.950003 4.989978
##  [6,] 3.451535 3.494441 3.551883
##  [7,] 3.869995 3.894953 3.953905
##  [8,] 3.898339 3.971408 4.027438
##  [9,] 4.381893 4.390743 4.393045
## [10,] 5.838683 5.872848 5.881461
## [11,] 3.343813 3.412175 3.430637
## [12,] 3.926383 3.986597 4.037351
## [13,] 3.709851 3.728160 3.731336
## [14,] 3.179708 3.308576 3.346737
## [15,] 3.515697 3.599172 3.622454
## [16,] 3.249002 3.272250 3.287321
## [17,] 3.205865 3.213173 3.225703
## [18,] 4.176571 4.180475 4.226132
## [19,] 3.285030 3.307263 3.339399
## [20,] 4.440023 4.440879 4.492300

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                1                1            0.946            0.869
##  2                1                1            0.974            0.984
##  3                1                1            0.881            0.518
##  4                1                1            1                0.998
##  5                1                1            0.881            1    
##  6                1                1            0.851            0.931
##  7                1                1            0.946            0.761
##  8                1                1            0.881            1    
##  9                1                1            1                0.966
## 10                1                1            0.974            0.959
## # ... 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.143        -0.182           0.539            -0.134
##  2        -0.232        -0.238          -0.208            -0.952
##  3         0.539        -0.240          -0.312             0.132
##  4        -0.0363       -0.184          -0.0884            0.733
##  5         0.697        -0.115           0.278            -0.896
##  6        -0.174        -0.613          -0.0850           -0.783
##  7        -0.184        -0.00776        -0.235            -0.850
##  8        -0.0179       -0.0305         -0.663            -0.897
##  9         0.197        -0.177          -0.0855           -0.343
## 10        -0.116        -0.0150         -0.0765           -0.465
## # ... 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.232 0.246 0.266 0.168 0.197 ...