# 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] 418 1000 290 535 614 314 238 523 195 109 ...

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

    ##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
    ##  [1,]  418  874  628  942   71  156  788  577  399   124
    ##  [2,] 1000  733  767  821  727  866  755  107   81   806
    ##  [3,]  290  590  488  821  607  806  208  404   37   733
    ##  [4,]  535  613  801  672  627  366  914  451  163   573
    ##  [5,]  614  119  999  755  767  821  139  173  773   866
    ##  [6,]  314  733  607  153  664 1000   65  529   56     2
    ##  [7,]  238  250   55  955  506  717  331  796  113   722
    ##  [8,]  523  725  160   39  730    9  896  436  431   522
    ##  [9,]  195   12  523  126  431  511    8  205  910   730
    ## [10,]  109  267  231  111  346  701  946  397  271   592
    ## [11,]  853  760  612  399  186  729  532  505  405   176
    ## [12,]    9  744  511  491  823  195  384  963  187   126
    ## [13,]  905  183  514  264   33  339  439  749  836   731
    ## [14,]  208  837  655  675  722  929  135  787  147   415
    ## [15,]  812  710   40   89  402  979  530  952   19   450
    ## [16,]  961  795  773  189  821  685  119  727  767   139
    ## [17,]  498  769 1000  717  555  660   65  929  656   117
    ## [18,]  370   33  905  264  749  220   13  495   72   265
    ## [19,]  638  380   40  710  686  284  473  718  657   770
    ## [20,]  689  674   68  743  486   82  559  801  451   778

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

    ##  num [1:1000, 1:30] 3.63 3.29 2.37 3.79 2.23 ...

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

    ##           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
    ##  [1,] 3.632355 3.708491 3.728559 3.751613 3.786198 3.924678 3.957302 4.005788
    ##  [2,] 3.287073 3.462682 3.478402 3.508446 3.514234 3.534949 3.584064 3.621060
    ##  [3,] 2.370303 3.255099 3.421620 3.473889 3.486982 3.559977 3.595751 3.670150
    ##  [4,] 3.787672 4.033836 4.193843 4.444538 4.472436 4.587822 4.676960 4.722959
    ##  [5,] 2.231078 3.030025 3.032456 3.263179 3.305017 3.320233 3.403182 3.618338
    ##  [6,] 2.908461 3.375307 3.408716 3.474126 3.483984 3.569294 3.597304 3.612274
    ##  [7,] 2.574222 2.620092 2.626894 2.649969 2.712833 2.720363 2.763009 2.966334
    ##  [8,] 3.259553 3.409890 3.587018 3.638739 3.739323 3.781679 4.033151 4.083570
    ##  [9,] 3.063779 3.229940 3.535084 3.620843 3.706292 3.745903 3.781679 3.842906
    ## [10,] 4.076489 4.274600 4.424466 4.680639 4.921531 4.925839 4.953778 5.138321
    ## [11,] 3.079625 3.232337 3.580175 3.626039 3.797314 3.973965 3.977959 4.034255
    ## [12,] 3.229940 3.327614 3.350932 3.380427 3.383156 3.390087 3.411248 3.412378
    ## [13,] 2.519572 2.564950 2.587125 2.637243 2.644419 2.854140 2.968647 2.970473
    ## [14,] 2.993627 3.185238 3.501026 3.503116 3.596555 3.600127 3.638911 3.682761
    ## [15,] 2.815737 2.827644 2.919770 3.015529 3.082324 3.129668 3.150681 3.221705
    ## [16,] 3.130284 3.247821 3.271862 3.284615 3.474307 3.479933 3.553985 3.554226
    ## [17,] 3.114888 3.213654 3.222439 3.414547 3.510649 3.544157 3.567269 3.587427
    ## [18,] 3.101829 3.145621 3.160221 3.163256 3.253455 3.267101 3.285374 3.286599
    ## [19,] 2.673262 2.709035 2.739427 2.762795 2.786634 2.825403 2.898502 2.936392
    ## [20,] 4.342825 4.718955 4.846609 4.975868 4.977152 5.050559 5.195131 5.289988
    ##           [,9]    [,10]
    ##  [1,] 4.128160 4.134983
    ##  [2,] 3.624421 3.625465
    ##  [3,] 3.708585 3.738727
    ##  [4,] 4.848288 4.860880
    ##  [5,] 3.646421 3.664331
    ##  [6,] 3.615328 3.630796
    ##  [7,] 3.039027 3.101229
    ##  [8,] 4.099165 4.119141
    ##  [9,] 3.896887 3.902276
    ## [10,] 5.238911 5.265439
    ## [11,] 4.054686 4.057822
    ## [12,] 3.428945 3.449180
    ## [13,] 3.015428 3.015582
    ## [14,] 3.682817 3.740260
    ## [15,] 3.235812 3.250871
    ## [16,] 3.555144 3.779061
    ## [17,] 3.589238 3.631749
    ## [18,] 3.288427 3.327889
    ## [19,] 3.024934 3.076591
    ## [20,] 5.333017 5.337030

### 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.790                          1                  0.902
    ##  2                       0.953                          1                  0.854
    ##  3                       0.984                          1                  0.953
    ##  4                       0.909                          1                  0.979
    ##  5                       0.834                          1                  0.979
    ##  6                       0.984                          1                  0.991
    ##  7                       0.790                          1                  0.839
    ##  8                       0.809                          1                  0.717
    ##  9                       0.984                          1                  0.849
    ## 10                       0.984                          1                  0.749
    ## # ℹ 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.396         -0.107         -0.0873                   -0.144
    ##  2        -0.0527        -0.0208         0.460                     0.588
    ##  3        -0.306         -0.664         -0.507                    -1.23
    ##  4        -0.443          0.648         -0.354                    -2.12
    ##  5        -0.259          0.765         -0.193                    -0.342
    ##  6        -0.0291        -0.152          0.0859                   -0.389
    ##  7        -0.589         -0.573          0.995                    -1.22
    ##  8        -0.154          1.04           0.596                    -1.28
    ##  9        -0.0291         0.994          0.234                     0.594
    ## 10        -0.434         -0.170         -0.150                     0.905
    ## # ℹ 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.234 0.267 0.26 0.203 0.27 ...