---
title: "Importing from to tRNAdb and mitotRNAdb as `GRanges`"
author: "Felix G.M. Ernst"
date: "`r Sys.Date()`"
package: tRNAdbImport
output:
  BiocStyle::html_document:
    toc: true
    toc_float: true
    df_print: paged
vignette: >
  %\VignetteIndexEntry{tRNAdbImport}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
bibliography: references.bib
---

```{r style, echo = FALSE, results = 'asis'}
BiocStyle::markdown(css.files = c('custom.css'))
```

# Introduction

The tRNAdb and mttRNAdb [@Juehling.2009] is a compilation of tRNA sequences and 
tRNA genes. It is a follow up version of the database of Sprinzl et al. 
[@Sprinzl.2005].

Using `tRNAdbImport` the tRNAdb can be accessed as outlined on the website 
[`r tRNAdbImport::TRNA_DB_URL`](`r tRNAdbImport::TRNA_DB_URL`) and the results
are returned as a `GRanges` object.

## Status 2024
The tRNAdb Server is currently not available. Some chunks of the code in this 
vignette are currently not avilable.
See [https://www.bioinf.uni-leipzig.de/services/webservices](https://www.bioinf.uni-leipzig.de/services/webservices)
for more information.

# Importing as `GRanges`

```{r, echo=FALSE}
suppressPackageStartupMessages(
  library(tRNAdbImport)
)
```
```{r eval=FALSE}
library(tRNAdbImport)
```
```{r}
# accessing tRNAdb
# tRNA from yeast for Alanine and Phenylalanine
gr <- import.tRNAdb(organism = "Saccharomyces cerevisiae",
                    aminoacids = c("Phe","Ala"))
```
```{r eval=FALSE}
# get a Phenylalanine tRNA from yeast
gr <- import.tRNAdb.id(tdbID = gr[gr$tRNA_type == "Phe",][1L]$tRNAdb_ID)
```
```{r eval=FALSE}
# find the same tRNA via blast
gr <- import.tRNAdb.blast(blastSeq = gr$tRNA_seq)
```
```{r}
# accessing mtRNAdb
# get the mitochrondrial tRNA for Alanine in Bos taurus
gr <- import.mttRNAdb(organism = "Bos taurus", 
                      aminoacids = "Ala")
```
```{r eval=FALSE}
# get one mitochrondrial tRNA in Bos taurus. 
gr <- import.mttRNAdb.id(mtdbID = gr[1L]$tRNAdb_ID)
```
```{r}
# check that the result has the appropriate columns
istRNAdbGRanges(gr)
```

# Importing as `GRanges` from the RNA database

The tRNAdb offers two different sets of data, one containing DNA sequences and
one containing RNA sequences. Depending on the database selected, `DNA` as 
default, the GRanges will contain a `DNAStringSet` or a `ModRNAStringSet` as
the `tRNA_seq` column. Because the RNA sequences can contain modified 
nucleotides, the `ModRNAStringSet` class is used instead of the `RNAStringSet` 
class to store the sequences correctly with all information intact.

```{r eval=FALSE}
gr <- import.tRNAdb(organism = "Saccharomyces cerevisiae",
                    aminoacids = c("Phe","Ala"),
                    database = "RNA")
gr$tRNA_seq
```

The special characters in the sequence might no exactly match the ones shown on
the website, since they are sanitized internally to a unified dictionary defined
in the `Modstrings` package. However, the type of modification encoded will 
remain the same (See the `Modstrings` package for more details).

The information on the position and type of the modifications can also be 
converted into a tabular format using the `separate` function from the 
`Modstrings` package.

```{r eval=FALSE}
separate(gr$tRNA_seq)
```

# Further analysis

The output can be saved or directly used for further analysis.

```{r, echo=FALSE}
suppressPackageStartupMessages({
  library(Biostrings)
  library(rtracklayer)
})
```
```{r eval=FALSE}
library(Biostrings)
library(rtracklayer)
# saving the tRAN sequences as fasta file
writeXStringSet(gr$tRNA_seq, filepath = tempfile())
# converting tRNAdb information to GFF compatible values
gff <- tRNAdb2GFF(gr)
gff
# Saving the information as gff3 file
export.gff3(gff, con = tempfile())
```

Please have a look at the `tRNA` package for further analysis of the tRNA
sequences.

# Session info

```{r}
sessionInfo()
```

# References