Step 2: The Scone Workflow

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] 365 962 501 430 540 670 431 485 897 737 ...
wand.nn[[1]][1:20, 1:10]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]  365  824  975  797  566  750  424  754  965    45
##  [2,]  962  819  554  379  598  431  812  103  180     7
##  [3,]  501  242  332  450  299  724  131  354  427   789
##  [4,]  430  601  297  605  984   26  247  346  701   497
##  [5,]  540  707  716  466  827  664  918  619  813   629
##  [6,]  670  113  920  207  296  794  842  493  947   127
##  [7,]  431  554  962   67    2  902  607  180  276   812
##  [8,]  485  514  975   24  409  824   63  274  992   882
##  [9,]  897  177  677  608  560  808  582  378   23   921
## [10,]  737  974  160  943  611  297  445  346  223   895
## [11,]   61  335  366  471  216  100  161  226  204   953
## [12,]   87  859  425  770  791  688  874  779  816   710
## [13,]  987  627   48   16  367  861  783  239   14   213
## [14,]  426  987  372  318  191  213   13   16  779   653
## [15,]  695  233  293  889  968  450   63  195  702   661
## [16,]  987  783   13  625  676  627   14   48  589   598
## [17,]  508  427   60  501  999  132  212  238  549   794
## [18,]  938  338  541  459  407  698  604  361  514    58
## [19,]  522  404  282  413  771  515  753  322  692   680
## [20,]  368  678  106  335  314  872  704  862  221   693
# Distance
str(wand.nn[[2]])
##  num [1:1000, 1:30] 3.32 3.38 4.09 3.57 3.13 ...
wand.nn[[2]][1:20, 1:10]
##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
##  [1,] 3.318934 3.401067 3.444134 3.715850 3.755753 3.785314 3.804993 3.825436
##  [2,] 3.382410 3.581189 3.627823 3.668141 3.703606 3.902774 3.989778 4.034297
##  [3,] 4.093142 4.243222 4.277992 4.306909 4.350494 4.360575 4.372952 4.381893
##  [4,] 3.569947 3.583181 3.637451 3.663413 3.748994 3.908166 3.910539 3.939806
##  [5,] 3.126276 4.091058 4.097302 4.552861 4.853895 4.958662 5.075716 5.155080
##  [6,] 3.264471 3.327889 3.328334 3.405819 3.455521 3.475804 3.496189 3.497169
##  [7,] 3.491925 3.688802 3.958095 4.057822 4.120862 4.172630 4.193240 4.205990
##  [8,] 4.423634 4.953778 5.048789 5.065909 5.138321 5.190719 5.197353 5.219531
##  [9,] 4.162920 4.471731 4.698248 4.738115 4.778760 4.819611 4.875379 4.924781
## [10,] 2.206203 2.829583 3.075735 3.093143 3.137864 3.176760 3.184080 3.241631
## [11,] 2.289549 2.752907 2.977494 3.039642 3.178267 3.193419 3.201240 3.233633
## [12,] 3.810307 3.862040 3.869815 3.961965 4.085290 4.089073 4.104447 4.200690
## [13,] 3.736119 3.951824 3.964374 4.017572 4.059307 4.074490 4.426026 4.471956
## [14,] 3.419116 3.626014 4.277316 4.286301 4.430295 4.443091 4.495380 4.525554
## [15,] 3.297419 3.340251 3.412893 3.484926 3.497966 3.574911 3.632760 3.884876
## [16,] 3.073766 3.880396 4.017572 4.251542 4.341929 4.411343 4.525554 4.796342
## [17,] 3.285076 3.380446 3.427433 3.721213 3.722028 3.757769 3.770940 3.816174
## [18,] 3.770685 3.868862 4.019431 4.023260 4.188502 4.243499 4.556795 4.572322
## [19,] 3.495569 3.645782 3.758190 3.818980 4.289584 4.318885 4.370202 4.381893
## [20,] 2.635397 2.736980 2.759300 2.767063 3.015529 3.064830 3.070590 3.089206
##           [,9]    [,10]
##  [1,] 3.880909 3.903274
##  [2,] 4.087599 4.120862
##  [3,] 4.382291 4.554148
##  [4,] 3.977939 4.027999
##  [5,] 5.342173 5.420322
##  [6,] 3.513007 3.530486
##  [7,] 4.277440 4.320054
##  [8,] 5.265439 5.281255
##  [9,] 4.928045 4.993592
## [10,] 3.266114 3.335019
## [11,] 3.257728 3.261959
## [12,] 4.344866 4.355745
## [13,] 4.495380 4.586409
## [14,] 4.564862 4.599099
## [15,] 3.921310 3.944253
## [16,] 4.849083 4.948623
## [17,] 3.849029 3.912379
## [18,] 4.622136 4.761092
## [19,] 4.390743 4.393045
## [20,] 3.099248 3.185518

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                       0.933                      1                      0.806
##  2                       0.811                      0.842                  1    
##  3                       0.936                      1                      0.772
##  4                       0.997                      0.895                  1    
##  5                       0.933                      1                      0.772
##  6                       0.674                      0.981                  0.675
##  7                       0.901                      0.981                  0.986
##  8                       0.874                      0.899                  0.956
##  9                       0.811                      0.826                  0.786
## 10                       0.720                      0.899                  0.899
## # ℹ 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.722         -0.570          -1.80                   -1.15  
##  2        -0.442         -0.271          -0.854                  -1.83  
##  3        -0.226          0.115          -0.957                   0.138 
##  4        -1.08          -0.915          -1.30                   -2.13  
##  5        -0.402         -0.356          -0.397                  -0.883 
##  6         0.0523        -0.379          -0.550                  -0.632 
##  7        -0.223         -0.865          -1.16                   -1.42  
##  8        -0.341         -0.0113         -1.50                   -0.0668
##  9        -1.28          -1.09           -2.00                   -0.803 
## 10        -1.31          -1.77           -2.67                    0.773 
## # ℹ 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.254 0.239 0.214 0.241 0.178 ...