Giter VIP home page Giter VIP logo

Comments (1)

egustavsson avatar egustavsson commented on June 17, 2024

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)}

from ggtranscript.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.