ggcoverage R Package

repository·main·Indexed 43 days ago

https://github.com/showteeth/ggcoverage

An R package for visualizing and annotating omics coverage data (genomics, transcriptomics, proteomics) using ggplot2. It supports loading BAM, BigWig, BedGraph, and text/xlsx files via LoadTrackFile(). Key features include coverage plotting with ggcoverage(), gene and transcript annotations via geom_gene() and geom_transcript(), CNV overlays with geom_cnv(), single-nucleotide resolution with geom_base(), and protein peptide coverage visualization using ggprotein(). It also integrates with ggbio for ideograms and HiCBricks for Hi-C contact maps via geom_tad() and geom_link().

Tokens
4.7K
Snippets
15
Records
16
Agent score
5%

What's inside ggcoverage

  1. Customize base and amino acid color schemes

    main

    The package provides default color schemes for base and amino acid annotations. You can override these using the nuc.color (for bases) and aa.color (for amino acids) parameters in geom_base().

    • Base colors: Typically follows a Clustal-style scheme.
    • Amino acid colors: Follows the aminochromography proposal.
    # Customizing base colors
    nuc_color <- c(
      "A" = "#ff2b08", "C" = "#009aff", "G" = "#ffb507", "T" = "#00bc0d"
    )
    
    # Use in geom_base (implied usage)
    # geom_base(..., nuc.color = nuc_color)
  2. Install ggcoverage via GitHub

    main

    It is recommended to install ggcoverage from the GitHub repository to ensure you have the most recent updates. You will need the remotes package installed to perform the installation.

    # Install remotes if not already installed
    install.package("remotes")
    
    # Install ggcoverage from GitHub
    remotes::install_github("showteeth/ggcoverage")
  3. Visualize ChIP-seq data with ggcoverage

    main

    To visualize ChIP-seq data, you need to prepare a metadata data frame, load your track files (e.g., BigWig) using LoadTrackFile, and optionally define regions to highlight using a mark_region data frame.

    1. Create Metadata: A data frame containing SampleName, Type, and Group columns to categorize your samples.
    2. Load Tracks: Use LoadTrackFile specifying the track.folder, the file format (e.g., "bw"), the genomic region, and the meta.info (your metadata data frame).
    3. Define Mark Regions: A data frame with start, end, and label columns to annotate specific genomic features like promoters.
    4. Generate Plot: Use the ggcoverage() function with your loaded track data and mark regions.
    # 1. Create metadata
    sample_meta <- data.frame(
      SampleName = c("Chr18_MCF7_ER_1", "Chr18_MCF7_ER_2", "Chr18_MCF7_ER_3", "Chr18_MCF7_input"),
      Type = c("MCF7_ER_1", "MCF7_ER_2", "MCF7_ER_3", "MCF7_input"),
      Group = c("IP", "IP", "IP", "Input")
    )
    
    # 2. Load track files
    track_df <- LoadTrackFile(
      track.folder = "path/to/tracks",
      format = "bw",
      region = "chr18:76822285-76900000",
      meta.info = sample_meta
    )
    
    # 3. Prepare mark region
    mark_region <- data.frame(
      start = c(76822533),
      end = c(76823743),
      label = c("Promoter")
    )
    
    # 4. Basic coverage plot
    basic_coverage <- ggcoverage(
      data = track_df,
      mark.region = mark_region,
      show.mark.label = FALSE
    )
    basic_coverage
  4. Visualize Hi-C data and contact maps

    main

    To visualize Hi-C data, you can combine coverage tracks with contact information. This requires the HiCBricks package (a suggested dependency).

    1. Load Coverage Track: Use LoadTrackFile to load BigWig files representing signal intensity.
    2. Prepare Hi-C Matrix: Load your contact matrix (e.g., from a .txt file) as an R matrix.
    3. Prepare Bin Table: Load the bin information and convert it to a GRanges object.
    4. Prepare Links: Load link files (e.g., .bedpe) to visualize specific chromatin interactions.
    5. Plotting: Use ggcoverage() for the base plot, then add geom_tad() for the contact map (TAD visualization) and geom_link() for specific interaction arcs.

    Note: geom_tad requires a transform.fun to handle data scaling (e.g., a log transformation).

    library(HiCBricks)
    
    # 1. Load track data
    track_df <- LoadTrackFile(track.file = "path/to/H3K36me3.bw", format = "bw", region = "chr2L:8050000-8300000")
    
    # 2. Load Matrix and Bins
    hic_mat <- as.matrix(read.table(file = "path/to/HiC_mat.txt", sep = "\t"))
    hic_bin <- read.table(file = "path/to/HiC_bin.txt", sep = "\t")
    colnames(hic_bin) <- c("chr", "start", "end")
    hic_bin_gr <- GenomicRanges::makeGRangesFromDataFrame(df = hic_bin)
    
    # 3. Load Links
    link_file <- "path/to/HiC_link.bedpe"
    
    # 4. Transformation function for TAD
    failsafe_log10 <- function(x) {
      x[is.na(x) | is.nan(x) | is.infinite(x)] <- 0
      return(log10(x + 1))
    }
    
    # 5. Generate Plot
    basic_coverage <- ggcoverage(data = track_df, color = "grey", mark.region = NULL, range.position = "out")
    
    basic_coverage +
      geom_tad(
        matrix = hic_mat,
        granges = hic_bin_gr,
        value.cut = 0.99,
        color.palette = "viridis",
        transform.fun = failsafe_log10,
        top = FALSE,
        show.rect = TRUE
      ) +
      geom_link(
        link.file = link_file,
        file.type = "bedpe",
        show.rect = TRUE
      )
  5. Visualize mass spectrometry protein coverage with ggprotein()

    main

    You can use ggprotein() to inspect peptide coverage of a protein using coverage data (e.g., exported from Proteome Discoverer) and a protein FASTA file. This allows you to visualize which parts of a protein sequence are covered by identified peptides.

    To use this function, you need:

    1. A coverage dataframe (coverage.df).
    2. A path to a protein FASTA file (fasta.file).
    3. The specific protein ID to visualize (protein.id).
    4. A range position specification (range.position), such as "out" to show the full protein sequence.

    Example workflow:

    1. Load coverage data using openxlsx::read.xlsx().
    2. Load the FASTA file using Biostrings::readAAStringSet().
    3. Call ggprotein() to create the coverage object.
    library(openxlsx)
    
    # 1. Prepare coverage dataframe
    coverage_file <- system.file("extdata", "Proteomics", "MS_BSA_coverage.xlsx", package = "ggcoverage")
    coverage_df <- openxlsx::read.xlsx(coverage_file, sheet = "Sheet1")
    
    # 2. Prepare protein fasta
    fasta_file <- system.file("extdata", "Proteomics", "MS_BSA_coverage.fasta", package = "ggcoverage")
    
    # 3. Generate protein coverage plot
    protein_coverage <- ggprotein(
      coverage.df = coverage_df,
      fasta.file = fasta_file,
      protein.id = "sp|P02769|ALBU_BOVIN",
      range.position = "out"
    )
    
    protein_coverage
  6. Annotate protein coverage plots with geom_feature()

    main

    You can add functional annotations (like signal peptides, domains, or chains) to a ggprotein object using geom_feature(). This helps explain gaps or specific patterns in the peptide coverage.

    To annotate, provide a dataframe (feature.df) containing:

    • ProteinID: The ID matching your protein.
    • start: The start position of the feature.
    • end: The end position of the feature.
    • Type: A label for the feature type.

    You can also specify feature.color to assign specific colors to the features.

    Example usage:

    # Define protein features (e.g., from UniProt)
    protein_feature_df <- data.frame(
      ProteinID = "sp|P02769|ALBU_BOVIN",
      start = c(1, 19, 25),
      end = c(18, 24, 607),
      Type = c("Signal", "Propeptide", "Chain")
    )
    
    # Add annotation to the existing coverage plot
    protein_coverage +
      geom_feature(
        feature.df = protein_feature_df,
        feature.color = c("#4d81be", "#173b5e", "#6a521d")
      )
    # protein feature obtained from UniProt
    protein_feature_df <- data.frame(
      ProteinID = "sp|P02769|ALBU_BOVIN",
      start = c(1, 19, 25),
      end = c(18, 24, 607),
      Type = c("Signal", "Propeptide", "Chain")
    )
    
    # add annotation
    protein_coverage +
      geom_feature(
        feature.df = protein_feature_df,
        feature.color = c("#4d81be", "#173b5e", "#6a521d")
      )
  7. Add annotations to ChIP-seq coverage plots

    main

    You can enrich your ggcoverage plots by adding various annotation layers using + syntax. Supported annotations include:

    • geom_gene(gtf.gr = ...): Adds gene models from a GRanges object.
    • geom_peak(bed.file = ...): Adds peak annotations from a BED file.
    • geom_ideogram(genome = ...): Adds a chromosomal ideogram.

    Note: For peak annotations, it is recommended to use consensus peaks (e.g., generated via MSPC).

    # Assuming basic_coverage is already created
    basic_coverage +
      geom_gene(gtf.gr = gtf_gr) +
      geom_peak(bed.file = peak_file) +
      geom_ideogram(genome = "hg19", plot.space = 0)
  8. Load DNA-seq track data with LoadTrackFile()

    main

    To visualize DNA-seq data (such as CNV or BigWig files), use the LoadTrackFile() function. You can provide metadata via a data frame to label samples by Type or Group.

    For text-based files, specify the format = "txt". For BigWig files, use format = "bw". You can also define a specific genomic region to load.

    # Example: Loading a text-based CNV track with metadata
    cnv_meta_info <- data.frame(
      SampleName = c("CNV_example"),
      Type = c("tumor"),
      Group = c("tumor")
    )
    
    track_df <- LoadTrackFile(
      track.file = "path/to/your/file.txt",
      format = "txt",
      region = "chr4:61750000-62700000",
      meta.info = cnv_meta_info
    )
  9. Visualize single-nucleotide level data with geom_base()

    main

    For single-nucleotide resolution (e.g., from BAM files), use LoadTrackFile() with single.nuc = TRUE. You can then use geom_base() to annotate bases and amino acids.

    geom_base() parameters:

    • bam.file: Path to the BAM file.
    • bs.fa.seq: The BSgenome object.
    • mark.type: Defines how to mark positions with SNVs. Options include:
      • "twill": Uses a twill mark.
      • "star": Uses a star mark.
      • "highlight": Highlights the specific base position.
    # Example: Single-nucleotide plot with star marks for SNVs
     ggcoverage(
      data = track_df,
      color = "grey",
      range.position = "out",
      single.nuc = TRUE,
      rect.color = "white"
    ) +
      geom_base(
        bam.file = bam_file,
        bs.fa.seq = BSgenome.Hsapiens.UCSC.hg19,
        mark.type = "star"
      ) +
      geom_ideogram(genome = "hg19", plot.space = 0)
  10. Add GC, Gene, and Ideogram annotations

    main

    You can enrich coverage plots by adding layers using + syntax.

    • geom_gc(): Adds GC content annotation. Requires a BSgenome object (e.g., BSgenome.Hsapiens.UCSC.hg19).
    • geom_gene(): Adds gene annotations. Requires a GRanges object (gtf.gr) derived from a GTF file.
    • geom_ideogram(): Adds a chromosomal ideogram. Use the genome parameter (e.g., "hg19").
    library("BSgenome.Hsapiens.UCSC.hg19")
    
    basic_coverage +
      geom_gc(bs.fa.seq = BSgenome.Hsapiens.UCSC.hg19) +
      geom_ideogram(genome = "hg19")
  11. Add CNV annotations with geom_cnv()

    main

    To overlay Copy Number Variation (CNV) data onto a coverage plot, use geom_cnv(). You must provide a data frame containing the CNV information and specify which columns contain the bin index and the copy number value.

    Parameters:

    • cnv.df: Data frame containing CNV data.
    • bin.col: The index of the column representing the bin.
    • cn.col: The index of the column representing the copy number value.
    # Assuming cnv_df has bin info in col 3 and copy number in col 4
    basic_coverage +
      geom_cnv(
        cnv.df = cnv_df,
        bin.col = 3,
        cn.col = 4
      )
  12. Add ideogram annotations

    main

    An ideogram provides a chromosomal overview of the current genomic position. This feature requires the ggbio package to be installed.

    Use geom_ideogram(genome = "...") to add the layer. The plot.space parameter controls the positioning of the ideogram relative to the coverage plot.

    library(ggbio)
    
    basic_coverage + 
      geom_gene(gtf.gr = gtf_gr) + 
      geom_ideogram(genome = "hg19", plot.space = 0)