knitr::opts_chunk$set(
  collapse = TRUE,
  message = FALSE,
  warning = FALSE,
  comment = "#",
  fig.path = "tools/README-",
  fig.cap=""
)

Build Status CRAN_Status_Badge Downloads Total Downloads


fastqcr: Quality Control of Sequencing Data

The FastQC, written by Simon Andrews at the Babraham Institute, is the most widely used sequence quality assessment tool for evaluating the raw reads from high throughput sequencing data.

It produces, for each sample, an html report and a 'zip' file, which contains a file called fastqc_data.txt and summary.txt.

If you have hundreds of samples, you’re not going to open up each HTML page. You need some way of looking at these data in aggregate.

The fastqcr R package provides helper functions to easily and automatically parse, aggregate and analyze FastQC reports for large numbers of samples.

Additionally, the fastqcr package provides a convenient solution for building a multi-QC report and a one-sample FastQC report with the result interpretations. The online documentation is available at: https://rpkgs.datanovia.com/fastqcr/.

Examples of QC reports, generated automatically by the fastqcr R package, include:

fastqcr logo

Installation and loading

install.packages("fastqcr")
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/fastqcr")
library("fastqcr")

Quick Start

library(fastqcr)

# Aggregating Multiple FastQC Reports into a Data Frame 
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# Demo QC directory containing zipped FASTQC reports
qc.dir <- system.file("fastqc_results", package = "fastqcr")
qc <- qc_aggregate(qc.dir)
qc

# Inspecting QC Problems
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# See which modules failed in the most samples
qc_fails(qc, "module")
# Or, see which samples failed the most
qc_fails(qc, "sample")

# Building Multi QC Reports
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
qc_report(qc.dir, result.file = "multi-qc-report" )

# Building One-Sample QC Reports (+ Interpretation)
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
qc.file <- system.file("fastqc_results", "S1_fastqc.zip", package = "fastqcr")
qc_report(qc.file, result.file = "one-sample-report",
          interpret = TRUE)

Main Functions

1) Installing and Running FastQC

2) Aggregating and Summarizing Multiple FastQC Reports

3) Inspecting Problems

4) Importing and Plotting FastQC Reports

5) Building One-Sample and Multi-QC Reports

6) Others

Installing FastQC from R

You can install automatically the FastQC tool from R as follow:

fastqc_install()

Running FastQC from R

The supported file formats by FastQC include:

Suppose that your working directory is organized as follow:

where, FASTQ is the directory containing your FASTQ files, for which you want to perform the quality control check.

To run FastQC from R, type this:

fastqc(fq.dir = "~/Documents/FASTQ", # FASTQ files directory
       qc.dir = "~/Documents/FASTQC", # Results direcory
       threads = 4                    # Number of threads
       )

FastQC Reports

For each sample, FastQC performs a series of tests called analysis modules.

These modules include:

The interpretation of these modules are provided in the official documentation of the FastQC tool.

Aggregating Reports

Here, we provide an R function qc_aggregate() to walk the FastQC result directory, find all the FASTQC zipped output folders, read the fastqc_data.txt and the summary.txt files, and aggregate the information into a data frame.

In the example below, we'll use a demo FastQC output directory available in the fastqcr package.

library(fastqcr)
# Demo QC dir
qc.dir <- system.file("fastqc_results", package = "fastqcr")
qc.dir

# List of files in the directory
list.files(qc.dir)

The demo QC directory contains five zipped folders corresponding to the FastQC output for 5 samples.

Aggregating FastQC reports:

qc <- qc_aggregate(qc.dir)
qc
qc.dir <- "/Users/kassambara/Documents/R/MyPackages/fastqcr/inst/fastqc_results"
qc <- qc_aggregate(qc.dir, progressbar = FALSE)

The aggregated report looks like this:

knitr::kable(dplyr::sample_n(qc, 10))

Column names:

```{block, type = "block"}
The table shows, for each sample, the names of tested FastQC modules, the status of the test, as well as, some general statistics including the number of reads, the length of reads, the percentage of GC content and the percentage of duplicate reads.

Once you have the aggregated data you can use the **dplyr** package to easily inspect modules that failed or warned in samples. For example, the following R code shows samples with warnings and/or failures: 


```r
library(dplyr)
qc %>%
  select(sample, module, status) %>%    
  filter(status %in% c("WARN", "FAIL")) %>%
  arrange(sample)

```{block, type = "success"} In the next section, we'll describe some easy-to-use functions, available in the fastqcr package, for analyzing the aggregated data.

## Summarizing Reports

We start by presenting a summary and general statistics of the aggregated data.


### QC Summary   

- R function: **summary**()     
- Input data: aggregated data from **qc_aggregate**()


```r
# Summary of qc
summary(qc)

Column names:

```{block, type = "block"} The table shows, for each FastQC module, the number and the name of samples that failed or warned.

### General statistics

- R function: **qc_stats**()
- Input data: aggregated data from **qc_aggregate**()


```r
qc_stats(qc)

Column names:

```{block, type = "block"} The table shows, for each sample, some general statistics such as the total number of reads, the length of reads, the percentage of GC content and the percentage of duplicate reads

## Inspecting Problems

Once you’ve got this aggregated data, it’s easy to figure out what (if anything) is wrong with your data. 


**1) R functions**. You can inspect problems per either modules or samples using the following R functions:  

- **qc_fails**(qc): Displays samples or modules that failed.
- **qc_warns**(qc): Displays samples or modules that warned.
- **qc_problems**(qc): Union of **qc_fails**() and **qc_warns**(). Display which samples or modules that failed or warned.


**2) Input data**: aggregated data from **qc_aggregate**()


**3) Output data**: Returns samples or FastQC modules with failures or warnings. By default, these functions return a compact output format. If you want a stretched format, specify the argument *compact = FALSE*.

The format and the interpretation of the outputs depend on the additional argument *element*, which value is one of c("sample", "module").

- If **element = "sample"** (default), results are samples with failed and/or warned modules. The results contain the following columns: 
    - sample (sample names), 
    - nb_problems (the number of modules with problems), 
    - module (the name of modules with problems).
- If **element = "module"**, results are modules that failed and/or warned in the most samples. The results contain the following columns: 
    - module (the name of module with problems), 
    - nb_problems (the number of samples with problems),
    - sample (the name of samples with problems)




### Per Module Problems


- **Modules that failed in the most samples**: 

```r
# See which module failed in the most samples
qc_fails(qc, "module")

```{block, type = "success"} For each module, the number of problems (failures) and the name of samples, that failed, are shown.

- **Modules that warned in the most samples**:  

```r
# See which module warned in the most samples
qc_warns(qc, "module")
# See which modules failed or warned.
qc_problems(qc, "module")

The output above is in a compact format. For a stretched format, type this:

qc_problems(qc, "module", compact = FALSE)

```{block, type = "success"} In the the stretched format each row correspond to a unique sample. Additionally, the status of each module is specified.

It's also possible to display problems for one or more specified modules. For example,


```r
qc_problems(qc, "module",  name = "Per sequence GC content")

```{block, type = "warning"} Note that, partial matching of name is allowed. For example, name = "Per sequence GC content" equates to name = "GC content".

```r
qc_problems(qc, "module",  name = "GC content")

Per Sample Problems

# See which samples had one or more failed modules
qc_fails(qc, "sample")

```{block, type = "success"} For each sample, the number of problems (failures) and the name of modules, that failed, are shown.

- **Samples with failed or warned modules**:

```r
# See which samples had one or more module with failure or warning
qc_problems(qc, "sample", compact = FALSE)

To specify the name of a sample of interest, type this:

qc_problems(qc, "sample", name = "S1")

Building an HTML Report

The function qc_report() can be used to build a report of FastQC outputs. It creates an HTML file containing FastQC reports of one or multiple samples.

Inputs can be either a directory containing multiple FastQC reports or a single sample FastQC report.

Create a Multi-QC Report

We'll build a multi-qc report for the following demo QC directory:

# Demo QC Directory
qc.dir <- system.file("fastqc_results", package = "fastqcr")
qc.dir
# Build a report
qc_report(qc.dir, result.file = "~/Desktop/multi-qc-result",
          experiment = "Exome sequencing of colon cancer cell lines")

```{block, type = "success"} An example of report is available at: fastqcr multi-qc report

### Create a One-Sample Report


We'll build a report for the following demo QC file:   


```r
 qc.file <- system.file("fastqc_results", "S1_fastqc.zip", package = "fastqcr")
qc.file
 qc_report(qc.file, result.file = "one-sample-report-with-interpretation",
   interpret = TRUE)

```{block, type = "success"} An example of report is available at: One sample QC report with interpretation

- **One-Sample QC report without plot interpretations**:


```r
 qc_report(qc.file, result.file = "one-sample-report",
   interpret = FALSE)

```{block, type = "success"} An example of report is available at: One sample QC report without interpretation

## Importing and Plotting a FastQC QC Report


We'll visualize the output for sample 1:


```r
# Demo file
qc.file <- system.file("fastqc_results", "S1_fastqc.zip",  package = "fastqcr")
qc.file

We start by reading the output using the function qc_read(), which returns a list of tibbles containing the data for specified modules:

qc.file = "/Users/kassambara/Documents/R/MyPackages/fastqcr/inst/fastqc_results/S1_fastqc.zip"
# Read all modules
qc <- qc_read(qc.file)
# Elements contained in the qc object
names(qc)

The function qc_plot() is used to visualized the data of a specified module. Allowed values for the argument modules include one or the combination of:

qc_plot(qc, "Per sequence GC content")

qc_plot(qc, "Per base sequence quality")

qc_plot(qc, "Per sequence quality scores")

qc_plot(qc, "Per base sequence content")

qc_plot(qc, "Sequence duplication levels")

Useful Links



kassambara/fastqcr documentation built on Feb. 25, 2023, 7:36 a.m.