Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding coverage #9

Closed
nmukherjee opened this issue Feb 15, 2023 · 2 comments
Closed

adding coverage #9

nmukherjee opened this issue Feb 15, 2023 · 2 comments

Comments

@nmukherjee
Copy link

Is there a way to add a read coverage track above (or below) the transcripts?
Either from a bigwig or bam file?

@egustavsson
Copy link
Contributor

I am not sure if this a solution you are after but you can use e.g. plot_grid() to plot coverage above or below the ggtranscript plot. I would just make sure to use the same x-axis ccordinates for both plots. Here is an example function to plot coverage from a bigwig:

  TranscriptCoveragePlot <-
  function(seqnames, start, end, strand, gene_id, gtf, coverage) {
    
    # Filter long read gtf/gff for gene of interest
    gtf_filtered <- gtf[gtf$gene_id == gene_id]
    
    # loci used to filter data
    locus_subset <- GRanges(seqnames = seqnames, ranges = IRanges(start = start, end = end), strand = strand)
    
    # coverage data
    coverage_data <- rtracklayer::import.bw(coverage) %>% subsetByOverlaps(., locus_subset) %>% as.data.frame()
    
    # Plot transcripts
    exons <- data.frame(gtf_filtered) %>% dplyr::filter(type == "exon")
    introns <- exons %>% to_intron(group_var = "transcript_id")
    CDS <- data.frame(gtf_filtered) %>% dplyr::filter(type == "CDS")
    
    transcript_plot <-
      exons %>%
      ggplot(aes(
        xstart = start, 
        xend = end, 
        y = transcript_id)) +
      geom_range(fill = "white",
                 height = 0.25) +
      geom_range(data = CDS) +
      geom_intron(
        data = introns,
        arrow.min.intron.length = 500,
        arrow = grid::arrow(ends = "first", length = grid::unit(0.1, "inches"))
        ) +
      labs(y = "Transcript name", x = "") +
      xlim(start(locus_subset), end(locus_subset))
    
    # coverage data
    coverage_plot <-
      coverage_data %>%
      ggplot(aes(
        xmin = start,
        xmax = end,
        ymin = 0,
        ymax = score
        )) +
      geom_rect(show.legend = F, alpha = 0.8) +
      xlim(start(locus_subset), end(locus_subset))
    
    # Final plot
    transcript_coverage_plot <-
      plot_grid(
        transcript_plot,
        coverage_plot,
        ncol = 1,
        align = "hv",
        rel_heights = c(1, 3.5),
        axis = "lr"
        )
    
    return(transcript_coverage_plot)}

@dzhang32
Copy link
Owner

Thanks @egustavsson for the response! Closing this as resolved, happy to reopen if not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants