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] 539 949 944 824 14 322 195 31 656 607 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  539  177  541  202  725  637  950   13  350   482
##  [2,]  949   33  758  366  116  382   50  767  534   478
##  [3,]  944  942  289  146  878  632  757  394  853   477
##  [4,]  824  281  695  502  403  270  533  713  850   679
##  [5,]   14  423  966  430  472   15  928  435  393    30
##  [6,]  322  536  377  444  628  178  613  959  827   589
##  [7,]  195  544  820  876  275  971  379  623  133   739
##  [8,]   31  238  964  329  751  555  804  666  560   505
##  [9,]  656  707  197  198  454  578  797  321  680    66
## [10,]  607  739  430  133  833  332  824  147  423   264
## [11,]  113  669   57  270  668  701  730  937  491   653
## [12,]   30  633  848  406  661  608  349   96  805   469
## [13,]  637  360  574  553  509  904   72  741  371   839
## [14,]    5  423  928  533  174  562  472   81  793   275
## [15,]  496  897  705  830  531  111  488  726  827   176
## [16,]  695  713  622  156  626  898  119  434  789   821
## [17,]  685  935  681  528  393  739  199  745  174   719
## [18,]  197  334  901  481  680  421  417  454  994    55
## [19,]  894  158  864  492  813  369  483  520  641   171
## [20,]  854  794  560  105  545  706  555  505  804   551
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.54 2.87 3.53 3.23 2.91 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]
##  [1,] 3.543540 3.650297 3.753477 3.908166 4.071483 4.137077 4.140932
##  [2,] 2.866492 3.265561 3.288425 3.475343 3.556980 3.613709 3.855725
##  [3,] 3.525737 3.624438 3.970664 4.027025 4.125868 4.227533 4.318366
##  [4,] 3.227081 3.425248 3.467818 3.474846 3.669893 3.691244 3.693790
##  [5,] 2.907990 3.077282 3.123469 3.239427 3.301202 3.322066 3.327836
##  [6,] 3.135509 3.206864 3.549906 3.589964 3.669989 3.686851 3.838373
##  [7,] 3.947773 4.211203 4.367830 4.393915 4.452781 4.532959 4.571980
##  [8,] 3.236308 3.491925 3.494201 3.797314 4.087384 4.093090 4.104316
##  [9,] 3.653781 3.686222 3.837499 3.994575 4.079693 4.083511 4.089239
## [10,] 2.803137 3.125680 3.166143 3.188596 3.225273 3.406788 3.423831
## [11,] 3.440584 4.092943 4.184986 4.252569 4.365472 4.380111 4.484500
## [12,] 3.874265 4.276295 4.280278 4.444715 4.557055 4.564580 4.572503
## [13,] 2.881456 3.470804 3.545122 3.562418 3.565207 3.584916 3.630682
## [14,] 2.907990 3.287269 3.315326 3.494212 3.497916 3.535462 3.634984
## [15,] 2.639657 2.780463 3.071611 3.172208 3.192469 3.219713 3.241928
## [16,] 2.724922 2.872871 2.903003 2.943644 2.960460 3.181405 3.208096
## [17,] 2.250208 2.733413 2.807196 2.816794 2.954585 3.001345 3.065151
## [18,] 3.352649 3.573608 3.601324 3.697964 3.729397 3.783338 3.820485
## [19,] 3.159695 3.461351 4.130096 4.146185 4.286646 4.312566 4.348400
## [20,] 3.464508 3.529550 3.731366 3.816896 3.877793 4.045353 4.154364
##           [,8]     [,9]    [,10]
##  [1,] 4.215474 4.265870 4.313219
##  [2,] 3.914504 3.952250 3.986689
##  [3,] 4.334310 4.348662 4.365517
##  [4,] 3.719178 3.835923 3.862040
##  [5,] 3.372381 3.374992 3.383435
##  [6,] 3.890021 3.929707 4.004217
##  [7,] 4.620909 4.623731 4.631122
##  [8,] 4.112280 4.143719 4.150795
##  [9,] 4.103440 4.103829 4.110755
## [10,] 3.447816 3.462920 3.489961
## [11,] 4.500361 4.699085 4.703711
## [12,] 4.573815 4.582971 4.720024
## [13,] 3.689163 3.700240 3.724500
## [14,] 3.775698 3.821178 3.864313
## [15,] 3.253768 3.300186 3.320537
## [16,] 3.219435 3.252051 3.280635
## [17,] 3.098751 3.118784 3.198298
## [18,] 3.827913 3.830787 3.856318
## [19,] 4.383952 4.411795 4.514507
## [20,] 4.179797 4.244423 4.410003

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                1            0.920
##  2                1                1                1            0.576
##  3                1                1                1            1    
##  4                1                1                1            0.948
##  5                1                1                1            0.714
##  6                1                1                1            0.867
##  7                1                1                1            0.782
##  8                1                1                1            0.714
##  9                1                1                1            0.980
## 10                1                1                1            0.568
## # ... 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.312          1.10           0.906             0.572
##  2         0.122         -0.0340         0.123            -0.593
##  3        -0.250          1.02           0.301            -0.561
##  4        -0.122         -0.171         -0.103             0.222
##  5        -0.0328        -0.406         -0.0232           -0.197
##  6        -0.0664        -0.184         -0.0623           -0.262
##  7        -0.382         -0.420          0.620             0.133
##  8        -0.106         -0.0302         0.0451            0.109
##  9         0.253          0.492         -0.367            -0.737
## 10        -0.0186        -0.159         -0.210             0.675
## # ... 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.23 0.238 0.224 0.257 0.29 ...