---
title: "Exon Annotations"
author: "Shian Su"
output: html_document
vignette: >
  %\VignetteIndexEntry{Exon Annotations}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

# preload to avoid loading messages
library(NanoMethViz)
```

```{r}
library(NanoMethViz)
library(dplyr)
```

# Importing Annotations

This package comes with helper functions that import exon annotations from the Bioconductor packages `Homo.sapiens` and `Mus.musculus`. The functions `get_exons_homo_sapiens()` and `get_exons_mus_musculus()` simply take data from the respective packages, and reorganise the columns such that we have seven columns

-   gene_id
-   chr
-   strand
-   start
-   end
-   transcript_id
-   symbol

This is used to provide gene annotations for the gene or region plots.

For other annotations, they will most likely be able to be imported using `rtracklayer::import()` and manipulated into the desired format. As an example, we can use a small sample of the C. Elegans gene annotation provided by ENSEMBL. `rtracklayer` will import the annotation as a `GRanges` object, this can be coerced into a data.frame and manipuated using `dplyr`.

```{r}
anno <- rtracklayer::import(system.file(package = "NanoMethViz", "c_elegans.gtf.gz"))

head(anno)
```

```{r}
anno <- anno %>%
    as.data.frame() %>%
    dplyr::rename(
        chr = "seqnames",
        symbol = "gene_name"
    ) %>%
    dplyr::select("gene_id", "chr", "strand", "start", "end", "transcript_id", "symbol")

head(anno)
```

# Alternative Annotations

Annotations can be simplified if full exon and isoform information is not required. For example, genebody annotation can be represented as single exon genes. For example we can take the example dataset and transform the isoform annotations of Peg3 into a single genebody block. The helper function `exons_to_genes()` can help with this common conversion.

```{r, message = FALSE}
nmr <- load_example_nanomethresult()

plot_gene(nmr, "Peg3")
```

```{r}
new_exons <- NanoMethViz::exons(nmr) %>%
    exons_to_genes() %>%
    mutate(transcript_id = gene_id)

NanoMethViz::exons(nmr) <- new_exons

plot_gene(nmr, "Peg3")
```

