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] 27 84 409 11 691 769 208 870 452 855 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]   27  140  453  565  528   71  847  683  692   613
##  [2,]   84  137  507  848  715  566  671  564  230   981
##  [3,]  409  838  815  703  706  544  923  725  784   489
##  [4,]   11  647  313  609   70  133  770   21  739    53
##  [5,]  691  489  659  232  633  855  894  852  501   346
##  [6,]  769  273  643  892  482  912  662  758  114   751
##  [7,]  208  546  225  494  816  143  196  218  695   936
##  [8,]  870  281  904  364   14  612  207  137  589   439
##  [9,]  452  197  680  333  532  387  768  934  380   594
## [10,]  855   81  135  691  404  698  149  341  549   536
## [11,]  313  647  770    4   70   26  679  609  315    21
## [12,]  524  526  839  818  870  987  142  154  707    40
## [13,]  503  322  390  528  613  972  396  410  685   257
## [14,]    8  281  137  432  743  904  981  566  589   239
## [15,]  395  356  588  898  787  399  339  815  122   325
## [16,]  951  870  583  150  639  904  185  504  526   183
## [17,]  677  735  349  981  743  564  535  137    2   230
## [18,]  755  686  241  665  122  815  588  991  816   881
## [19,]  998  503  824   13  717  418  957  172  322   575
## [20,]  140  960  170  297  470  226  752  324  641    27
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 2.74 4.17 3.55 2.73 3.31 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 2.742485 2.766774 2.835728 2.875798 2.910265 2.969226 3.116936 3.123300
##  [2,] 4.171182 4.277316 4.391883 4.434783 4.436591 4.501409 4.573288 4.627988
##  [3,] 3.549980 3.606343 3.974030 4.082678 4.095586 4.121492 4.141763 4.286759
##  [4,] 2.733156 3.141290 3.565829 3.641394 3.751039 3.829118 3.932041 4.136630
##  [5,] 3.314284 3.404386 3.533695 3.645218 3.686009 3.702529 3.747067 3.792218
##  [6,] 3.036092 3.184080 3.224061 3.241312 3.470804 3.497179 3.549005 3.569829
##  [7,] 4.064624 4.154563 4.309365 4.463803 4.466517 4.645089 4.650437 4.680702
##  [8,] 4.028213 4.159745 4.387797 4.488994 4.493017 4.645867 4.747459 4.779241
##  [9,] 3.911108 4.140878 4.253867 4.408850 4.455586 4.547326 4.549342 4.596109
## [10,] 3.564791 3.605968 3.656766 3.813461 3.855312 3.898207 3.905923 4.008878
## [11,] 2.172608 2.539375 2.706849 2.733156 3.126652 3.203300 3.227914 3.241524
## [12,] 4.070849 4.318927 4.557760 4.724732 4.840559 4.851289 4.917062 4.982922
## [13,] 2.522496 2.639657 2.953933 3.020224 3.169419 3.192469 3.219713 3.221525
## [14,] 4.493017 4.747423 4.787138 4.799307 4.936175 4.964602 5.022785 5.090182
## [15,] 2.554459 2.921927 3.042187 3.887901 3.971168 4.059081 4.236226 4.258966
## [16,] 3.961965 4.085290 4.104447 4.281820 4.344866 4.355745 4.496911 4.656194
## [17,] 3.917878 4.124809 4.148614 4.252493 4.515683 4.552795 4.863481 4.923347
## [18,] 4.000033 4.017774 4.380745 4.477922 4.497634 4.500190 4.640594 4.645345
## [19,] 3.072694 3.400413 3.581049 3.601583 3.614318 3.654247 3.709984 3.735610
## [20,] 3.061246 3.180421 3.307469 3.331167 3.485061 3.550177 3.564391 3.569898
##           [,9]    [,10]
##  [1,] 3.168725 3.200762
##  [2,] 4.690008 4.706954
##  [3,] 4.355668 4.371376
##  [4,] 4.163707 4.248110
##  [5,] 3.820327 3.864376
##  [6,] 3.581676 3.582693
##  [7,] 4.691614 4.706833
##  [8,] 4.801004 4.821759
##  [9,] 4.721041 4.722035
## [10,] 4.041281 4.059009
## [11,] 3.259011 3.329119
## [12,] 4.996851 5.072893
## [13,] 3.231821 3.240587
## [14,] 5.159841 5.334006
## [15,] 4.353519 4.453613
## [16,] 4.666186 4.830093
## [17,] 4.934852 4.980330
## [18,] 4.680896 4.688391
## [19,] 3.787883 3.817468
## [20,] 3.639282 3.687836

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.800            0.844            0.931                1
##  2            0.800            0.972            1                    1
##  3            0.797            0.972            0.968                1
##  4            0.496            0.972            0.931                1
##  5            0.512            0.946            0.900                1
##  6            0.820            0.934            1                    1
##  7            0.737            0.972            0.977                1
##  8            0.851            0.844            0.966                1
##  9            0.962            0.844            1                    1
## 10            0.496            0.972            0.966                1
## # ... 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(~ `CD3(Cd114)Di`
##             <dbl>          <dbl>          <dbl>            <dbl>          <dbl>
##  1        -0.106         -0.0302         0.0451            0.109        -0.0728
##  2         0.371         -0.111         -0.854            -1.21         -0.197 
##  3        -0.198          0.584         -0.0366           -0.899        -0.262 
##  4        -0.0144        -0.262         -0.116            -0.640         0.834 
##  5        -0.584         -0.534         -0.657            -1.25         -0.912 
##  6        -0.0328        -0.406         -0.0232           -0.197         0.554 
##  7        -0.428         -0.258         -0.225            -0.130        -0.224 
##  8        -0.170         -0.705         -0.617            -0.318        -0.209 
##  9         0.118         -0.399          0.551            -1.56          0.472 
## 10        -0.642         -0.273         -0.430            -0.772        -1.05  
## # ... 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.305 0.21 0.224 0.237 0.253 ...