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] 59 9 194 890 846 83 331 314 981 875 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]   59  928  714  649   15  943  715  284  925    27
##  [2,]    9  350  749  149  689   48  934  455  864   453
##  [3,]  194  943  136   27  511  710  284  928  892    56
##  [4,]  890  638  422  708  672  364  701   95  496   789
##  [5,]  846  667  862  856  201  607  125  297  825    42
##  [6,]   83  313  718  579  525  845  835  925  688   486
##  [7,]  331  347  128  369  641  421  717  287  899   285
##  [8,]  314  592  565  575  251  528  949  912  516    13
##  [9,]  981    2  876  934  623  732   48  454  389   864
## [10,]  875  506  458  527  545  132  417  832  590   958
## [11,]  213  449  866  278  858  774  178  983  578   157
## [12,]  820  297  967  632  286    5  415  856  862   387
## [13,]  942  565  189  592   70    8  912  370  683   255
## [14,]  422  133  820  856  725  902  987  890  828  1000
## [15,]  843  497   70  928  184   67  137  565  463   711
## [16,]  163    2  515  953    9  149  738  934  864   811
## [17,]  363   59    1  661  604  566   81  817  649   271
## [18,]  846  346  967  476  772  101  247  856  133   339
## [19,]  487  925  598  835  751  525  592  353  405   898
## [20,]   57  840  497  131  252  488   31  205  626   144
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.58 2.66 3.66 3.58 2.31 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.577959 3.828365 3.850349 3.865013 3.866061 3.869837 3.883219 3.900658
##  [2,] 2.661384 2.674326 2.816362 2.846001 2.881318 2.931912 2.940155 2.948449
##  [3,] 3.656178 3.873374 3.963417 3.998168 4.032484 4.137645 4.147744 4.174068
##  [4,] 3.578335 3.581049 3.601583 3.614318 3.638779 3.703243 3.734166 3.735610
##  [5,] 2.306681 2.808949 3.124734 3.127478 3.199996 3.220559 3.341306 3.366557
##  [6,] 2.370303 3.388649 3.473889 3.486982 3.594484 3.613140 3.651005 3.686943
##  [7,] 3.356851 3.359119 3.549906 3.786363 3.876545 3.906281 3.954731 3.985128
##  [8,] 2.539880 2.702144 2.722951 2.749710 2.789880 2.806574 2.874492 2.967493
##  [9,] 2.266397 2.661384 2.847252 2.858227 2.941750 2.964757 2.986400 3.093295
## [10,] 3.204930 3.438415 3.719250 3.862740 3.894480 3.896668 3.907548 3.919718
## [11,] 3.741816 4.348160 4.356210 4.569023 4.577746 4.637489 4.664614 4.671925
## [12,] 3.517541 3.651199 3.817298 3.860959 3.900910 3.921677 3.951490 3.963566
## [13,] 2.744202 2.887557 2.921166 2.921370 2.979355 2.991175 3.000049 3.001232
## [14,] 2.818500 2.896909 2.917864 2.976726 2.997273 3.005976 3.022129 3.140557
## [15,] 2.931016 3.037768 3.105474 3.230213 3.401931 3.423346 3.530482 3.550263
## [16,] 3.018629 3.079598 3.182182 3.190289 3.412268 3.450229 3.451099 3.482824
## [17,] 3.526604 3.982846 4.241187 4.429282 4.597442 4.597678 4.627651 4.758651
## [18,] 2.810474 2.843630 3.101917 3.197762 3.302193 3.323135 3.329606 3.331071
## [19,] 2.994119 3.411322 3.522647 3.782354 3.818311 3.880291 3.886062 3.922244
## [20,] 3.461351 3.645975 4.444098 4.448529 4.470466 4.543469 4.677396 4.684257
##           [,9]    [,10]
##  [1,] 3.905342 3.912850
##  [2,] 2.955938 2.958371
##  [3,] 4.223608 4.257679
##  [4,] 3.787883 3.790119
##  [5,] 3.377360 3.409308
##  [6,] 3.706352 3.708585
##  [7,] 3.999606 4.052769
##  [8,] 2.989109 2.991175
##  [9,] 3.138187 3.146696
## [10,] 3.961922 3.967638
## [11,] 4.774678 4.807101
## [12,] 3.970520 4.021386
## [13,] 3.073528 3.275839
## [14,] 3.164171 3.188933
## [15,] 3.573400 3.605165
## [16,] 3.515227 3.524066
## [17,] 4.760091 4.780274
## [18,] 3.333952 3.349297
## [19,] 4.096030 4.122375
## [20,] 4.717027 4.830975

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 × 34
##    `pCrkL(Lu175)Di.IL7.qvalue` pCREB(Yb176)Di.IL7.qvalu…¹ pBTK(Yb171)Di.IL7.qv…²
##                          <dbl>                      <dbl>                  <dbl>
##  1                           1                       1                     0.987
##  2                           1                       1                     0.993
##  3                           1                       1.00                  0.993
##  4                           1                       1                     0.973
##  5                           1                       1.00                  0.866
##  6                           1                       1.00                  0.882
##  7                           1                       1                     1    
##  8                           1                       1                     0.927
##  9                           1                       1                     0.976
## 10                           1                       1                     0.993
## # ℹ 990 more rows
## # ℹ abbreviated names: ¹​`pCREB(Yb176)Di.IL7.qvalue`,
## #   ²​`pBTK(Yb171)Di.IL7.qvalue`
## # ℹ 31 more variables: `pS6(Yb172)Di.IL7.qvalue` <dbl>,
## #   `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>, …

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 × 51
##    `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(In113)Di`
##             <dbl>          <dbl>          <dbl>                    <dbl>
##  1        -0.194         -0.262         -0.0632                    0.868
##  2        -0.102         -0.118         -0.230                     0.193
##  3        -0.728         -0.322         -0.524                     0.465
##  4        -0.163          0.432         -0.403                     0.189
##  5        -0.541          0.434         -0.508                    -0.981
##  6        -0.188         -0.622         -0.355                     0.142
##  7        -0.0674         0.695         -0.229                     0.867
##  8        -0.0681        -0.0129         0.280                     0.574
##  9        -0.0504        -0.252          0.912                    -0.790
## 10        -0.515         -0.501         -0.675                     0.417
## # ℹ 20 more rows
## # ℹ 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>, …
# 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.241 0.326 0.229 0.255 0.291 ...