Lorena Pantano Harvard TH Chan School of Public Health, Boston, US

``````library(DEGreport)
data(humanGender)``````

## General QC figures from DE analysis

We are going to do a differential expression analysis with edgeR/DESeq2. We have an object that is coming from the edgeR package. It contains a gene count matrix for 85 TSI HapMap individuals, and the gender information. With that, we are going to apply the `glmFit` function or DESeq2 to get genes differentially expressed between males and females.

``````library(DESeq2)
idx <- c(1:10, 75:85)
dds <- DESeqDataSetFromMatrix(assays(humanGender)[[1]][1:1000, idx],
colData(humanGender)[idx,], design=~group)
dds <- DESeq(dds)
res <- results(dds)``````

We need to extract the experiment design data.frame where the condition is Male or Female.

``````counts <- counts(dds, normalized = TRUE)
design <- as.data.frame(colData(dds))``````

### Size factor QC

A main assumption in library size factor calculation of edgeR and DESeq2 (and others) is that the majority of genes remain unchanged. Plotting the distribution of gene ratios between each gene and the average gene can show how true this is. Not super useful for many samples because the plot becomes crowed.

``degCheckFactors(counts[, 1:6])``

### Mean-Variance QC plots

p-value distribution gives an idea on how well you model is capturing the input data and as well whether it could be some problem for some set of genes. In general, you expect to have a flat distribution with peaks at 0 and 1. In this case, we add the mean count information to check if any set of genes are enriched in any specific p-value range.

Variation (dispersion) and average expression relationship shouldnâ€™t be a factor among the differentially expressed genes. When plotting average mean and standard deviation, significant genes should be randomly distributed.

In this case, it would be good to look at the ones that are totally outside the expected correlation.

You can put this tree plots together using `degQC`.

``degQC(counts, design[["group"]], pvalue = res[["pvalue"]])``
``````## Warning: Guides provided to `guides()` must be named.
## â„¹ All guides are unnamed.``````

### Covariates effect on count data

Another important analysis to do if you have covariates is to calculate the correlation between PCs from PCA analysis to different variables you may think are affecting the gene expression. This is a toy example of how the function works with raw data, where clearly library size correlates with some of the PCs.

``````resCov <- degCovariates(log2(counts(dds)+0.5),
colData(dds))``````
``````## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE``````

### Covariates correlation with metrics

Also, the correlation among covariates and metrics from the analysis can be tested. This is useful when the study has multiple variables, like in clinical trials. The following code will return a correlation table, and plot the correlation heatmap for all the covariates and metrics in a table.

``cor <- degCorCov(colData(dds))``
``````## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE``````
``## Warning: The input is a data frame-like object, convert it to a matrix.``

``names(cor)``
``## [1] "cor"    "corMat" "fdrMat" "plot"``

### QC report

A quick HTML report can be created with `createReport` to show whether a DE analysis is biased to a particular set of genes. It contains the output of `degQC,`degVB and `degMB`.

Note: Nozzle.R1 is not longer available since 3.16. You need to install the package manually before using this function.

``````createReport(colData(dds)[["group"]], counts(dds, normalized = TRUE),

## Report from DESeq2 analysis

Here, we show some useful plots for differentially expressed genes.

### Contrasts

`DEGSet` is a class to store the DE results like the one from `results` function. DESeq2 offers multiple way to ask for contrasts/coefficients. With `degComps` is easy to get multiple results in a single object:

``````degs <- degComps(dds, combs = "group",
contrast = list("group_Male_vs_Female",
c("group", "Female", "Male")))
names(degs)``````
``## [1] "group_Male_vs_Female" "group_Female_vs_Male"``

`degs` contains 3 elements, one for each contrast/coefficient asked for. It contains the results output in the element `raw` and the output of `lfcShrink` in the element `shrunken`. To obtain the results from one of them, use the method `dge`:

``deg(degs[[1]])``
``````## log2 fold change (MAP): group Male vs Female
## Wald test p-value: group Male vs Female
## DataFrame with 1000 rows and 6 columns
##                  baseMean log2FoldChange     lfcSE        stat       pvalue
##                 <numeric>      <numeric> <numeric>   <numeric>    <numeric>
## ENSG00000067048 1025.0378       1.939488 0.1006940    23.99438 3.18302e-127
## ENSG00000012817  411.5439       3.700564 0.0989888    21.80452 2.10214e-105
## ENSG00000067646  169.8148       3.322846 0.0995193    15.48385  4.45984e-54
## ENSG00000005889  670.8619      -0.489435 0.0929363    -5.26371  1.41179e-07
## ENSG00000006757   92.6611      -0.472926 0.0992749    -4.75701  1.96486e-06
## ...                   ...            ...       ...         ...          ...
## ENSG00000068120  1214.097   -0.000114322 0.0797227 -0.00143400     0.998856
## ENSG00000072062   935.317    0.000559219 0.0915352  0.00610872     0.995126
## ENSG00000076770  1019.796    0.000850082 0.1016663  0.00836193     0.993328
## ENSG00000078967   166.422    0.000415745 0.0959758  0.00432971     0.996545
## ENSG00000079246  5226.339    0.000149584 0.0926205  0.00161500     0.998711
##                    <numeric>
## ENSG00000067048 3.18302e-124
## ENSG00000012817 1.05107e-102
## ENSG00000067646  1.48661e-51
## ENSG00000005889  3.52946e-05
## ENSG00000006757  3.92971e-04
## ...                      ...
## ENSG00000068120     0.998856
## ENSG00000072062     0.998856
## ENSG00000076770     0.998856
## ENSG00000078967     0.998856
## ENSG00000079246     0.998856``````

By default it would output the `shrunken` table always, as defined by `degDefault`, that contains the default table to get.

To get the original results table, use the parameter as this:

``deg(degs[[1]], "raw", "tibble")``
``````## # A tibble: 1,000 Ã— 7
##    gene            baseMean log2FoldChange  lfcSE  stat    pvalue      padj
##    <chr>              <dbl>          <dbl>  <dbl> <dbl>     <dbl>     <dbl>
##  1 ENSG00000067048   1025.          10.2   0.423  24.0  3.18e-127 3.18e-124
##  2 ENSG00000012817    412.           9.24  0.424  21.8  2.10e-105 1.05e-102
##  3 ENSG00000067646    170.          10.2   0.658  15.5  4.46e- 54 1.49e- 51
##  4 ENSG00000005889    671.          -0.692 0.131  -5.26 1.41e-  7 3.53e-  5
##  5 ENSG00000006757     92.7         -0.767 0.161  -4.76 1.96e-  6 3.93e-  4
##  6 ENSG00000073282    220.          -1.87  0.421  -4.44 8.89e-  6 1.48e-  3
##  7 ENSG00000005302   2027.          -0.742 0.176  -4.21 2.59e-  5 3.69e-  3
##  8 ENSG00000005020   1234.           0.389 0.0952  4.08 4.44e-  5 5.56e-  3
##  9 ENSG00000003400    394.           0.680 0.177   3.85 1.17e-  4 1.31e-  2
## 10 ENSG00000069702    107.          -1.63  0.459  -3.56 3.71e-  4 3.71e-  2
## # â„¹ 990 more rows``````

Note that the format of the output can be changed to tibble, or data.frame with a third parameter `tidy`.

The table will be always sorted by padj.

And easy way to get significant genes is:

``significants(degs[[1]], fc = 0, fdr = 0.05)``
``````##  [1] "ENSG00000012817" "ENSG00000067646" "ENSG00000067048" "ENSG00000005889"
##  [5] "ENSG00000006757" "ENSG00000005302" "ENSG00000003400" "ENSG00000073282"
##  [9] "ENSG00000005020" "ENSG00000069702"``````

This function can be used as well for a list of comparisons:

``significants(degs, fc = 0, fdr = 0.05)``
``````##  [1] "ENSG00000012817" "ENSG00000067646" "ENSG00000067048" "ENSG00000005889"
##  [5] "ENSG00000006757" "ENSG00000005302" "ENSG00000003400" "ENSG00000073282"
##  [9] "ENSG00000005020" "ENSG00000069702"``````

And it can returns the full table for a list:

``significants(degs, fc = 0, fdr = 0.05, full = TRUE)``
``````## # A tibble: 10 Ã— 7
##    gene   log2FoldChange      padj log2FoldChange_groupâ€¦Â¹ log2FoldChange_groupâ€¦Â²
##    <chr>           <dbl>     <dbl>                  <dbl>                  <dbl>
##  1 ENSG0â€¦          0.388 1.31e-  2                 -0.494                  0.388
##  2 ENSG0â€¦          0.319 5.56e-  3                 -0.351                  0.319
##  3 ENSG0â€¦          0.541 3.69e-  3                  0.541                 -0.425
##  4 ENSG0â€¦          0.573 3.53e-  5                  0.573                 -0.489
##  5 ENSG0â€¦          0.585 3.93e-  4                  0.585                 -0.473
##  6 ENSG0â€¦          3.70  1.05e-102                 -5.01                   3.70
##  7 ENSG0â€¦          1.94  3.18e-124                 -3.63                   1.94
##  8 ENSG0â€¦          3.32  1.49e- 51                 -4.43                   3.32
##  9 ENSG0â€¦          0.453 3.71e-  2                  0.453                 -0.260
## 10 ENSG0â€¦          0.582 1.48e-  3                  0.582                 -0.339
## # â„¹ abbreviated names: Â¹â€‹log2FoldChange_group_Female_vs_Male,
## #   Â²â€‹log2FoldChange_group_Male_vs_Female
## # â„¹ 2 more variables: padj_group_Female_vs_Male <dbl>,

Since log2FoldChange are shrunken, the method for DEGSet class now can plot these changes as follow:

``degMA(degs[[1]], diff = 2, limit = 3)``

The blue arrows indicate how foldchange is affected by this new feature.

As well, it can plot the original MA plot:

``degMA(degs[[1]], diff = 2, limit = 3, raw = TRUE)``

or the correlation between the original log2FoldChange and the new ones:

``degMA(degs[[1]], limit = 3, correlation = TRUE)``