# 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] 119 97 696 943 170 33 726 231 631 411 ...

    wand.nn[[1]][1:20, 1:10]

    ##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
    ##  [1,]  119  186  664   97  428  630   29  980  949   594
    ##  [2,]   97  714  352  726  133  205    7  949  630   493
    ##  [3,]  696  369  527  140  744  253  443  119  320   714
    ##  [4,]  943   51  891  523  420  328  433  374  494    52
    ##  [5,]  170   17  184  435  275  850  557  928  319   623
    ##  [6,]   33   71  964  885  485  196   56   51  825   930
    ##  [7,]  726  442   27  744  185  834   97  725  207   154
    ##  [8,]  231  643  351  753  450  815  820  393  467   458
    ##  [9,]  631  675  822  258  413  652  676  989  878   262
    ## [10,]  411  271  420  104  176  750  151  117  195   561
    ## [11,]  834  136  910  702  567  701  251  294  401   613
    ## [12,]  525  329  687  963  173  858  903  494  591   473
    ## [13,]  447  502  485  962   71  514  196   32  761   312
    ## [14,]  413  631  989  337  244  808  262  565  764   542
    ## [15,]  356  217  947  785  458  254   66  698  259    93
    ## [16,]  185  624   11  593  776   21  538   64  974   499
    ## [17,]  783  209  578  928  727  435  275  978  855   882
    ## [18,]  439  815  376  307  338   80  790  246  678   231
    ## [19,]  565  334   14  262  890  878  232  631  265   141
    ## [20,]  938  701  645  985  905  705  834  446  997   955

    # Distance
    str(wand.nn[[2]])

    ##  num [1:1000, 1:30] 4.86 3.16 3.5 3.22 2.81 ...

    wand.nn[[2]][1:20, 1:10]

    ##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
    ##  [1,] 4.862226 4.930693 4.975502 4.982312 5.150431 5.219101 5.220080 5.220268
    ##  [2,] 3.159057 3.688778 3.740716 3.797004 3.855780 3.891089 3.995146 4.012733
    ##  [3,] 3.496553 3.522506 3.559725 3.707276 3.808543 3.821651 3.969425 4.045849
    ##  [4,] 3.221538 3.651744 3.735856 3.857558 3.867068 3.914426 4.048233 4.084207
    ##  [5,] 2.806825 2.998118 3.415443 3.459202 3.573208 3.577782 3.586295 3.620091
    ##  [6,] 3.166559 3.241059 3.314776 3.318765 3.333096 3.340232 3.350924 3.386908
    ##  [7,] 3.130829 3.256615 3.325420 3.432159 3.510688 3.622510 3.628469 3.643350
    ##  [8,] 4.543026 4.554202 4.578876 4.659204 4.707463 4.748999 4.752195 4.833016
    ##  [9,] 3.520498 3.598385 3.600803 3.817606 3.921847 3.932091 3.947861 4.035223
    ## [10,] 3.707049 3.725396 3.817305 4.086391 4.178302 4.306932 4.366362 4.408984
    ## [11,] 2.839993 2.905517 2.933055 2.955616 3.063677 3.084615 3.090575 3.118784
    ## [12,] 3.263144 3.501768 3.571807 3.833117 3.867082 3.918159 3.927282 3.936384
    ## [13,] 3.174668 3.233375 3.262967 3.285306 3.380307 3.380331 3.411497 3.526652
    ## [14,] 3.391197 4.062841 4.081626 4.145820 4.230996 4.241192 4.241636 4.273084
    ## [15,] 2.046750 2.735375 2.994343 3.030267 3.109112 3.114050 3.122590 3.148475
    ## [16,] 2.907990 3.349961 3.497916 3.535462 3.562311 3.653628 3.661115 3.726571
    ## [17,] 2.532573 2.548095 2.692719 2.721244 2.791295 2.798292 2.829769 2.843118
    ## [18,] 3.968356 3.991153 4.477519 4.547056 4.579580 4.596414 4.605214 4.665375
    ## [19,] 3.746514 4.233413 4.336576 4.432759 4.459076 4.489870 4.493819 4.707209
    ## [20,] 3.039226 3.156356 3.183396 3.195779 3.215493 3.307687 3.434380 3.484379
    ##           [,9]    [,10]
    ##  [1,] 5.291917 5.389369
    ##  [2,] 4.108843 4.119357
    ##  [3,] 4.053177 4.115151
    ##  [4,] 4.142253 4.192146
    ##  [5,] 3.629711 3.654687
    ##  [6,] 3.494126 3.507926
    ##  [7,] 3.654135 3.698499
    ##  [8,] 4.884382 4.938888
    ##  [9,] 4.052411 4.054034
    ## [10,] 4.536312 4.565656
    ## [11,] 3.167195 3.212876
    ## [12,] 3.937841 3.938787
    ## [13,] 3.537164 3.611575
    ## [14,] 4.285681 4.306341
    ## [15,] 3.172517 3.250618
    ## [16,] 3.813664 3.864313
    ## [17,] 2.853085 2.936514
    ## [18,] 4.733855 4.770703
    ## [19,] 4.762073 4.864674
    ## [20,] 3.498521 3.524622

### 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.958                      0.949                  0.533
    ##  2                       0.994                      0.995                  0.785
    ##  3                       0.922                      0.985                  1
    ##  4                       0.930                      1                      0.972
    ##  5                       0.958                      1                      0.565
    ##  6                       1                          1                      0.972
    ##  7                       1                          0.985                  1
    ##  8                       0.907                      0.985                  1
    ##  9                       1                          0.985                  0.781
    ## 10                       0.921                      1                      0.907
    ## # ℹ 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.0957         -0.176         -0.199                    -0.493
    ##  2       -0.174          -0.613         -0.0850                   -0.783
    ##  3       -0.674          -0.635         -0.565                    -0.594
    ##  4        0.697          -0.115          0.278                    -0.896
    ##  5       -0.0403         -0.496         -0.108                    -1.01
    ##  6       -0.202          -0.0294        -0.289                    -0.534
    ##  7       -0.289          -0.490         -0.121                     0.646
    ##  8       -0.741          -0.805         -1.12                     -1.02
    ##  9       -0.00552        -0.187         -0.142                    -1.89
    ## 10        0.372          -0.138          0.107                     0.194
    ## # ℹ 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.183 0.234 0.242 0.24 0.265 ...