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] 918 777 989 36 579 908 924 209 189 917 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  918  280  109  116  628  225  792  794  747   579
##  [2,]  777  171  500  567  969  294  629   43  998   108
##  [3,]  989  788  885  699   11  366  343  829  825    59
##  [4,]   36  755  426   55  873  232  867  623  989   790
##  [5,]  579  233  750  152  615  287  661  133  901   866
##  [6,]  908  625  138  616  755  467  442   57  829    48
##  [7,]  924  269  687   62   44  419  304  458  520   845
##  [8,]  209  192   76  552  417  744  326  814  680   602
##  [9,]  189  139  987  469  364  666   97  847  118   958
## [10,]  917  702  970  774   28  698  656  665  455   769
## [11,]  827  587  885    3  401  572  332  925 1000   651
## [12,]   93  461  767  349  964  535  634  641  954   616
## [13,]  987  545  123  197  720   73  446  591  696   971
## [14,]   43  662   47  617  369  735  960  819  863   431
## [15,]  621  485  463  703  310  537  879  601  734   235
## [16,]  367  456  387  518  180  784  742  471  439   927
## [17,]  592   89  399  372  877  112   78   23  696   214
## [18,]  580  891  963  374   64  623  775  476   41   943
## [19,] 1000  873  421  613  359   77  107  867  875   232
## [20,]  544  135   90  166  483  582  562  186   33   439
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.71 2.62 3.38 2.18 4.84 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.708425 4.305388 4.785147 4.914107 5.064622 5.066507 5.106599 5.137192
##  [2,] 2.622504 2.752907 2.845141 2.919913 2.956911 2.977494 2.992832 3.073188
##  [3,] 3.375307 3.408716 3.483984 3.595286 3.597304 3.630796 3.642016 3.642499
##  [4,] 2.183445 2.648219 2.705975 2.818641 2.843641 3.017980 3.057751 3.116323
##  [5,] 4.840401 4.939676 5.022750 5.534483 5.752062 5.794363 5.869359 5.908344
##  [6,] 3.733845 3.747977 3.978432 4.012787 4.093523 4.099559 4.115680 4.227425
##  [7,] 3.043696 3.169950 3.182576 3.241093 3.337072 3.355986 3.410895 3.443274
##  [8,] 3.419766 3.493557 4.073705 4.127432 4.301064 4.312950 4.326322 4.359535
##  [9,] 4.481757 4.551554 4.622989 4.637174 4.662373 4.697840 4.699965 4.751231
## [10,] 2.765675 2.832318 2.891351 2.893196 2.943991 3.033790 3.098479 3.119748
## [11,] 3.012112 3.165423 3.418581 3.597304 3.604383 3.638785 3.716201 3.806617
## [12,] 4.118989 4.380745 4.443624 4.497634 4.590462 4.645345 4.680896 4.705178
## [13,] 3.014610 3.128950 3.183438 3.273488 3.277175 3.340385 3.369577 3.371839
## [14,] 2.675868 2.773033 2.790624 3.114887 3.160026 3.223892 3.288548 3.315661
## [15,] 3.660582 3.794746 3.832154 3.888612 4.029559 4.061997 4.062962 4.083400
## [16,] 4.671480 4.702873 4.803985 4.938966 5.021473 5.044814 5.082858 5.095593
## [17,] 3.866841 3.941597 4.072078 4.086691 4.148297 4.220570 4.225712 4.338482
## [18,] 3.071717 3.482856 3.696256 3.756475 3.768423 3.830020 3.909412 3.981704
## [19,] 2.958679 2.969214 3.011940 3.039933 3.119796 3.155625 3.205827 3.246120
## [20,] 3.642712 3.649257 3.664541 3.726866 3.745536 3.764372 3.822912 3.829362
##           [,9]    [,10]
##  [1,] 5.161567 5.332888
##  [2,] 3.125862 3.178267
##  [3,] 3.727824 3.758921
##  [4,] 3.175258 3.197433
##  [5,] 5.991524 6.023610
##  [6,] 4.444663 4.465577
##  [7,] 3.469314 3.535463
##  [8,] 4.383109 4.455883
##  [9,] 4.795196 4.822890
## [10,] 3.132981 3.162675
## [11,] 3.809217 3.816823
## [12,] 4.783132 4.794852
## [13,] 3.379519 3.400905
## [14,] 3.329536 3.351647
## [15,] 4.195691 4.276164
## [16,] 5.175700 5.176622
## [17,] 4.351993 4.368190
## [18,] 4.016532 4.063349
## [19,] 3.279501 3.300440
## [20,] 3.915433 3.917260

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            0.961            0.981            0.970
##  2                1            1                0.952            0.874
##  3                1            1                1                0.710
##  4                1            1                0.821            0.976
##  5                1            1                0.927            0.945
##  6                1            1                0.985            0.966
##  7                1            1                0.970            0.833
##  8                1            0.858            0.769            0.848
##  9                1            1                0.952            0.966
## 10                1            1                0.606            0.869
## # ... 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.173          -0.127         -0.149            -0.246        -0.175 
##  2       -0.140          -0.162         -0.153             0.922        -0.280 
##  3       -0.124           0.525          0.280            -0.231        -0.250 
##  4       -0.0253          0.406         -0.237             0.565        -0.799 
##  5       -0.149          -0.297         -0.542             0.689        -0.0541
##  6       -0.0111         -0.248          0.302             0.587        -0.286 
##  7       -0.113          -0.0797         0.697             0.904        -0.119 
##  8       -0.171          -0.399         -0.0564           -0.580        -0.215 
##  9       -0.0113         -0.0986        -0.181             0.514        -0.0744
## 10       -0.00750        -0.589         -0.810             0.738         0.334 
## # ... 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.188 0.309 0.261 0.309 0.165 ...