Skip to contents

For a given set of exons and introns, shorten_gaps() reduces the width of gaps (regions that do not overlap any exons) to a user-inputted target_gap_width. This can be useful when visualizing transcripts that have long introns, to hone in on the regions of interest (i.e. exons) and better compare between transcript structures.

Usage

shorten_gaps(exons, introns, group_var = NULL, target_gap_width = 100L)

Arguments

exons

data.frame() contains exons which can originate from multiple transcripts differentiated by group_var.

introns

data.frame() the intron co-ordinates corresponding to the input exons. This can be created by applying to_intron() to the exons. If introns originate from multiple transcripts, they must be differentiated using group_var. If a user is not using to_intron(), they must make sure intron start/ends are defined precisely as the adjacent exon boundaries (rather than exon end + 1 and exon start - 1).

group_var

character() if input data originates from more than 1 transcript, group_var must specify the column that differentiates transcripts (e.g. "transcript_id").

target_gap_width

integer() the width in base pairs to shorten the gaps to.

Value

data.frame() contains the re-scaled co-ordinates of introns and exons of each input transcript with shortened gaps.

Details

After shorten_gaps() reduces the size of gaps, it will re-scale exons and introns to preserve exon alignment. This process will only reduce the width of input introns, never exons. Importantly, the outputted re-scaled co-ordinates should only be used for visualization as they will not match the original genomic coordinates.

Examples


library(magrittr)
library(ggplot2)

# to illustrate the package's functionality
# ggtranscript includes example transcript annotation
pknox1_annotation %>% head()
#> # A tibble: 6 × 8
#>   seqnames  start    end strand type  gene_name transcript_name transcript_biot…
#>   <fct>     <int>  <int> <fct>  <fct> <chr>     <chr>           <chr>           
#> 1 21       4.30e7 4.30e7 +      gene  PKNOX1    NA              NA              
#> 2 21       4.30e7 4.30e7 +      tran… PKNOX1    PKNOX1-203      protein_coding  
#> 3 21       4.30e7 4.30e7 +      exon  PKNOX1    PKNOX1-203      protein_coding  
#> 4 21       4.30e7 4.30e7 +      exon  PKNOX1    PKNOX1-203      protein_coding  
#> 5 21       4.30e7 4.30e7 +      exon  PKNOX1    PKNOX1-203      protein_coding  
#> 6 21       4.30e7 4.30e7 +      exon  PKNOX1    PKNOX1-203      protein_coding  

# extract exons
pknox1_exons <- pknox1_annotation %>% dplyr::filter(type == "exon")
pknox1_exons %>% head()
#> # A tibble: 6 × 8
#>   seqnames  start    end strand type  gene_name transcript_name transcript_biot…
#>   <fct>     <int>  <int> <fct>  <fct> <chr>     <chr>           <chr>           
#> 1 21       4.30e7 4.30e7 +      exon  PKNOX1    PKNOX1-203      protein_coding  
#> 2 21       4.30e7 4.30e7 +      exon  PKNOX1    PKNOX1-203      protein_coding  
#> 3 21       4.30e7 4.30e7 +      exon  PKNOX1    PKNOX1-203      protein_coding  
#> 4 21       4.30e7 4.30e7 +      exon  PKNOX1    PKNOX1-203      protein_coding  
#> 5 21       4.30e7 4.30e7 +      exon  PKNOX1    PKNOX1-203      protein_coding  
#> 6 21       4.30e7 4.30e7 +      exon  PKNOX1    PKNOX1-203      protein_coding  

# to_intron() is a helper function included in ggtranscript
# which is useful for converting exon co-ordinates to introns
pknox1_introns <- pknox1_exons %>% to_intron(group_var = "transcript_name")
pknox1_introns %>% head()
#> # A tibble: 6 × 8
#>   seqnames strand type  gene_name transcript_name transcript_biot…  start    end
#>   <fct>    <fct>  <chr> <chr>     <chr>           <chr>             <int>  <int>
#> 1 21       +      intr… PKNOX1    PKNOX1-203      protein_coding   4.30e7 4.30e7
#> 2 21       +      intr… PKNOX1    PKNOX1-202      retained_intron  4.30e7 4.30e7
#> 3 21       +      intr… PKNOX1    PKNOX1-201      protein_coding   4.30e7 4.30e7
#> 4 21       +      intr… PKNOX1    PKNOX1-206      retained_intron  4.30e7 4.30e7
#> 5 21       +      intr… PKNOX1    PKNOX1-204      retained_intron  4.30e7 4.30e7
#> 6 21       +      intr… PKNOX1    PKNOX1-203      protein_coding   4.30e7 4.30e7

# for transcripts with long introns, the exons of interest
# can be difficult to visualize clearly when using the default scale
pknox1_exons %>%
    ggplot(aes(
        xstart = start,
        xend = end,
        y = transcript_name
    )) +
    geom_range() +
    geom_intron(
        data = pknox1_introns,
        arrow.min.intron.length = 3500
    )


# in such cases it can be useful to rescale the exons and introns
# using shorten_gaps() which shortens regions that do not overlap an exon
pknox1_rescaled <-
    shorten_gaps(pknox1_exons, pknox1_introns, group_var = "transcript_name")

pknox1_rescaled %>% head()
#> # A tibble: 6 × 8
#>   seqnames start   end strand type   gene_name transcript_name transcript_bioty…
#>   <fct>    <dbl> <dbl> <fct>  <chr>  <chr>     <chr>           <chr>            
#> 1 21          54   156 +      exon   PKNOX1    PKNOX1-201      protein_coding   
#> 2 21         156   694 +      intron PKNOX1    PKNOX1-201      protein_coding   
#> 3 21         694   800 +      exon   PKNOX1    PKNOX1-201      protein_coding   
#> 4 21         800   901 +      intron PKNOX1    PKNOX1-201      protein_coding   
#> 5 21         901  1028 +      exon   PKNOX1    PKNOX1-201      protein_coding   
#> 6 21        1028  1223 +      intron PKNOX1    PKNOX1-201      protein_coding   

# this allows us to visualize differences in exonic structure more clearly
pknox1_rescaled %>%
    dplyr::filter(type == "exon") %>%
    ggplot(aes(
        xstart = start,
        xend = end,
        y = transcript_name
    )) +
    geom_range() +
    geom_intron(
        data = pknox1_rescaled %>% dplyr::filter(type == "intron"),
        arrow.min.intron.length = 300
    )


# shorten_gaps() can be used in combination with to_diff()
# to further highlight differences in exon structure
# here, all other transcripts are compared to the MANE-select transcript
pknox1_rescaled_diffs <- to_diff(
    exons = pknox1_rescaled %>%
        dplyr::filter(type == "exon", transcript_name != "PKNOX1-201"),
    ref_exons = pknox1_rescaled %>%
        dplyr::filter(type == "exon", transcript_name == "PKNOX1-201"),
    group_var = "transcript_name"
)

pknox1_rescaled %>%
    dplyr::filter(type == "exon") %>%
    ggplot(aes(
        xstart = start,
        xend = end,
        y = transcript_name
    )) +
    geom_range() +
    geom_intron(
        data = pknox1_rescaled %>% dplyr::filter(type == "intron"),
        arrow.min.intron.length = 300
    ) +
    geom_range(
        data = pknox1_rescaled_diffs,
        aes(fill = diff_type),
        alpha = 0.2
    )