# Step 3: Post-Processing

#### Tyler J Burns

#### October 2, 2017

### The post-processing function:

This vignette covers what takes place following the generation of SCONE output detailed in TheSconeWorkflow.Rmd. The obvious step that needs to take place is the Scone generated columns being merged into the original input data. The user gets the option of log base 10 transforming q values, which is easier to visualize. The user also gets the option to run t-SNE on the data, such that said maps can be colored by SCONE generated values. In this case, t-SNE is run utilizing the Rtsne package, using the same markers that were used as input for the KNN. generation.

    library(Sconify)
    wand.final <- PostProcessing(scone.output = wand.scone,
                             cell.data = wand.combined,
                             input = input.markers)

    ## Read the 1000 x 27 data matrix successfully!
    ## OpenMP is working. 1 threads.
    ## Using no_dims = 2, perplexity = 30.000000, and theta = 0.500000
    ## Computing input similarities...
    ## Building tree...
    ## Done in 0.24 seconds (sparsity = 0.126934)!
    ## Learning embedding...
    ## Iteration 50: error is 65.535620 (50 iterations in 0.24 seconds)
    ## Iteration 100: error is 60.885018 (50 iterations in 0.17 seconds)
    ## Iteration 150: error is 60.566666 (50 iterations in 0.13 seconds)
    ## Iteration 200: error is 60.424322 (50 iterations in 0.13 seconds)
    ## Iteration 250: error is 60.365133 (50 iterations in 0.20 seconds)
    ## Iteration 300: error is 1.442835 (50 iterations in 0.13 seconds)
    ## Iteration 350: error is 1.312323 (50 iterations in 0.10 seconds)
    ## Iteration 400: error is 1.277630 (50 iterations in 0.14 seconds)
    ## Iteration 450: error is 1.263804 (50 iterations in 0.14 seconds)
    ## Iteration 500: error is 1.252771 (50 iterations in 0.13 seconds)
    ## Iteration 550: error is 1.239990 (50 iterations in 0.15 seconds)
    ## Iteration 600: error is 1.234518 (50 iterations in 0.14 seconds)
    ## Iteration 650: error is 1.230580 (50 iterations in 0.11 seconds)
    ## Iteration 700: error is 1.227399 (50 iterations in 0.14 seconds)
    ## Iteration 750: error is 1.222729 (50 iterations in 0.13 seconds)
    ## Iteration 800: error is 1.220770 (50 iterations in 0.15 seconds)
    ## Iteration 850: error is 1.218593 (50 iterations in 0.14 seconds)
    ## Iteration 900: error is 1.216572 (50 iterations in 0.15 seconds)
    ## Iteration 950: error is 1.214453 (50 iterations in 0.16 seconds)
    ## Iteration 1000: error is 1.212710 (50 iterations in 0.13 seconds)
    ## Fitting performed in 2.92 seconds.

    wand.combined # input data

    ## # A tibble: 1,000 × 51
    ##    `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(In113)Di`
    ##             <dbl>          <dbl>          <dbl>                    <dbl>
    ##  1        -0.908         -0.813         -0.287                    -1.83
    ##  2        -0.369         -0.201         -0.123                     0.237
    ##  3        -0.0661        -0.248         -0.213                    -1.64
    ##  4         0.122         -0.0340         0.123                    -0.593
    ##  5        -0.215         -0.0412        -0.247                    -0.229
    ##  6        -0.254         -0.193         -0.121                    -0.267
    ##  7        -0.0685        -0.252         -0.257                     0.457
    ##  8         0.0745        -0.523         -0.316                     0.207
    ##  9        -0.201         -0.250         -0.0683                   -1.41
    ## 10        -0.0463        -0.222         -0.163                    -3.31
    ## # ℹ 990 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>, …

    wand.scone # scone-generated data

    ## # 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.973                          1                  0.947
    ##  2                       0.973                          1                  0.955
    ##  3                       0.991                          1                  0.896
    ##  4                       0.973                          1                  0.900
    ##  5                       0.973                          1                  0.897
    ##  6                       0.973                          1                  0.901
    ##  7                       0.973                          1                  0.706
    ##  8                       0.973                          1                  0.706
    ##  9                       0.973                          1                  0.999
    ## 10                       0.973                          1                  0.897
    ## # ℹ 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>, …

    wand.final # the data after post-processing

    ## # A tibble: 1,000 × 87
    ##    `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(In113)Di`
    ##             <dbl>          <dbl>          <dbl>                    <dbl>
    ##  1        -0.908         -0.813         -0.287                    -1.83
    ##  2        -0.369         -0.201         -0.123                     0.237
    ##  3        -0.0661        -0.248         -0.213                    -1.64
    ##  4         0.122         -0.0340         0.123                    -0.593
    ##  5        -0.215         -0.0412        -0.247                    -0.229
    ##  6        -0.254         -0.193         -0.121                    -0.267
    ##  7        -0.0685        -0.252         -0.257                     0.457
    ##  8         0.0745        -0.523         -0.316                     0.207
    ##  9        -0.201         -0.250         -0.0683                   -1.41
    ## 10        -0.0463        -0.222         -0.163                    -3.31
    ## # ℹ 990 more rows
    ## # ℹ 83 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>, …

    # tSNE map shows highly responsive population of interest
    TsneVis(wand.final,
            "pSTAT5(Nd150)Di.IL7.change",
            "IL7 -> pSTAT5 change")

    # tSNE map now colored by q value
    TsneVis(wand.final,
            "pSTAT5(Nd150)Di.IL7.qvalue",
            "IL7 -> pSTAT5 -log10(qvalue)")

    # tSNE map colored by KNN density estimation
    TsneVis(wand.final, "density")

### Subsampling your data prior to running t-SNE:

If one has a large number of cells in the dataset (>100K), then t-SNE can become time-consuming and produce results that are less clean. As such, I provide a wrapper that allows one to subsample the final data and run t-SNE on the subsampled data, producing a new tibble that contains the subsampled data along with two t-SNE dimensions added to it. Note the two added dimensions at the end of the tibble are called “bh-SNE11” and “bh-SNE21”. This is because the dimensions “bh-SNE1” and “bh-SNE2” are already in the data, because t-SNE was run during the post processing step in this example. As I have stated, a user would realistically use this function with a much larger number of cells, in which case the user would have selected “tsne = FALSE” in the post.processing function detailed above in this vignette.

    wand.final.sub <- SubsampleAndTsne(dat = wand.final,
                                       input = input.markers,
                                       numcells = 500)

    ## Read the 500 x 27 data matrix successfully!
    ## OpenMP is working. 1 threads.
    ## Using no_dims = 2, perplexity = 30.000000, and theta = 0.500000
    ## Computing input similarities...
    ## Building tree...
    ## Done in 0.10 seconds (sparsity = 0.243384)!
    ## Learning embedding...
    ## Iteration 50: error is 54.734533 (50 iterations in 0.08 seconds)
    ## Iteration 100: error is 53.637427 (50 iterations in 0.09 seconds)
    ## Iteration 150: error is 53.411396 (50 iterations in 0.09 seconds)
    ## Iteration 200: error is 53.343871 (50 iterations in 0.11 seconds)
    ## Iteration 250: error is 53.305937 (50 iterations in 0.10 seconds)
    ## Iteration 300: error is 0.942421 (50 iterations in 0.09 seconds)
    ## Iteration 350: error is 0.867636 (50 iterations in 0.06 seconds)
    ## Iteration 400: error is 0.852670 (50 iterations in 0.08 seconds)
    ## Iteration 450: error is 0.845172 (50 iterations in 0.08 seconds)
    ## Iteration 500: error is 0.838944 (50 iterations in 0.08 seconds)
    ## Iteration 550: error is 0.834828 (50 iterations in 0.08 seconds)
    ## Iteration 600: error is 0.833162 (50 iterations in 0.08 seconds)
    ## Iteration 650: error is 0.830533 (50 iterations in 0.08 seconds)
    ## Iteration 700: error is 0.828634 (50 iterations in 0.08 seconds)
    ## Iteration 750: error is 0.825818 (50 iterations in 0.08 seconds)
    ## Iteration 800: error is 0.824863 (50 iterations in 0.08 seconds)
    ## Iteration 850: error is 0.823675 (50 iterations in 0.08 seconds)
    ## Iteration 900: error is 0.823350 (50 iterations in 0.08 seconds)
    ## Iteration 950: error is 0.822726 (50 iterations in 0.08 seconds)
    ## Iteration 1000: error is 0.821286 (50 iterations in 0.07 seconds)
    ## Fitting performed in 1.63 seconds.

    wand.final.sub

    ## # A tibble: 500 × 89
    ##    `CD3(Cd110)Di` `CD3(Cd111)Di` `CD3(Cd112)Di` `CD235-61-7-15(In113)Di`
    ##             <dbl>          <dbl>          <dbl>                    <dbl>
    ##  1         -0.373         1.01           2.49                      0.955
    ##  2         -0.190        -0.0580        -0.112                    -0.842
    ##  3          0.556        -0.234          0.438                     0.846
    ##  4          0.433        -0.705          0.931                    -1.30
    ##  5         -0.650        -0.110         -0.254                    -1.31
    ##  6         -0.201        -0.250         -0.0683                   -1.41
    ##  7         -0.643        -0.195         -0.297                     0.284
    ##  8         -0.370        -0.470         -0.638                    -0.102
    ##  9         -0.271        -0.209          1.99                      0.553
    ## 10         -1.11         -0.0874        -0.826                    -1.34
    ## # ℹ 490 more rows
    ## # ℹ 85 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>, …