---
title: "tRNA alignment"
date: "`r format(Sys.time(), '%d %B, %Y')`"
author: "`r Sys.info()[[7]]`"
output:
  html_document:
    toc: true
    toc_float: true
    toc_collapsed: true
    theme: yeti
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, fig.align="center", warning=FALSE, message=FALSE)
```

```{r libraries, include=FALSE}
library("ggplot2")
library("tidyverse")
library("ggpubr")
library("knitr")
library(reshape2)
library(stringr)
library(DT)
library(RColorBrewer)
library(ggplot2)
library(plotly)
```

```{r by_codon, echo=FALSE}

df_mRNA <- read.table(gzfile("../merged_idxstats.txt.gz"), sep = "\t", header = TRUE, row.names = 1)

 

data <- as.data.frame(str_match(rownames(df_mRNA), "(.*[0-9]+):(.*)-"))

data <- cbind(data, df_mRNA)

 

data$V1 <- NULL

data$V2 <- NULL

data_1 <- data %>%  dplyr::group_by(V3)

 

data_1 %>%  summarise_all(sum) -> data_1

 

data_1 <- na.omit(data_1)

 

rownames(data_1) <- data_1$V3

data_1$V3 <- NULL

data_1 %>%  rownames_to_column("id") %>% filter_all(all_vars(. > 0)) %>%

  column_to_rownames("id") -> final

final <- as.data.frame(final)[order(as.data.frame(final[1]), decreasing = TRUE),][1:10,]

n <- 10
qual_cols_pal <- brewer.pal.info[brewer.pal.info$category == "qual",]
col_vector <- unlist(mapply(brewer.pal, qual_cols_pal$maxcolors, rownames(qual_cols_pal)))
features <- melt(as.matrix(final), by="Row.names")

plot <- ggplot(features, aes(x = Var2, y = value, fill = Var1)) +

       geom_bar(stat = 'identity') + scale_fill_manual(values=col_vector)+
  theme(axis.text.x = element_text(angle=90, hjust=1))+ theme_bw()+ theme(axis.text.x = element_text(angle = 90, hjust = 1))

ggplotly(plot)
```

```{r}
num_samples <- length(colnames(data_1))

feature_count_tibble_all_samples = data.frame(matrix(ncol = 0, nrow=length(rownames(data_1))))

rownames(feature_count_tibble_all_samples) <- rownames(data_1)

 

for(i in 1:num_samples){

  sample <- data_1[i]

  total_reads = sum(sample)

  for(n in dim(features)[1]){

  sample_name <- colnames(sample)

  sample$Percentages <- 100*( sample[sample_name] / total_reads)

  feature_count_tibble_all_samples = bind_cols(feature_count_tibble_all_samples,sample$Percentages)

  }
}

rownames(feature_count_tibble_all_samples) <- rownames(data_1)

fc_tibble_all_samples_nonzero <- feature_count_tibble_all_samples[rowSums(feature_count_tibble_all_samples > 0)==dim(feature_count_tibble_all_samples)[2],]

#write.csv(fc_tibble_all_samples_nonzero, "percentages-tRNA-amino-acid.csv")

fc_tibble_all_samples_nonzero <- fc_tibble_all_samples_nonzero %>%

  rownames_to_column("RNA")

plot_me <- melt(fc_tibble_all_samples_nonzero, by="Row.names")
qual_cols_pal <- brewer.pal.info[brewer.pal.info$category == "qual",]
col_vector <- unlist(mapply(brewer.pal, qual_cols_pal$maxcolors, rownames(qual_cols_pal)))


plot <- ggplot(plot_me, aes(x = variable, y = value, fill = RNA)) +

       geom_bar(stat = 'identity') + scale_fill_manual(values=col_vector)+
  theme(axis.text.x = element_text(angle=90, hjust=1))  + theme_bw() + theme(axis.text.x = element_text(angle = 90, hjust = 1))

ggplotly(plot)
```



# Percentage of tRNA by amino acid

```{r}
data <- as.data.frame(str_match(rownames(df_mRNA), "(.*[0-9]+):([A-Z][a-z][a-z]).*-"))

data <- cbind(data, df_mRNA)

 

data$V1 <- NULL

data$V2 <- NULL

data_1 <- data %>%  dplyr::group_by(V3)

 

data_1 %>%  summarise_all(sum) -> data_1

 

data_1 <- na.omit(data_1)

 

rownames(data_1) <- data_1$V3

data_1$V3 <- NULL

 

num_samples <- length(colnames(data_1))

feature_count_tibble_all_samples = data.frame(matrix(ncol = 0, nrow=length(rownames(data_1))))

rownames(feature_count_tibble_all_samples) <- rownames(data_1)

 

for(i in 1:num_samples){

  sample <- data_1[i]

  total_reads = sum(sample)

  for(n in dim(features)[1]){

  sample_name <- colnames(sample)

  sample$Percentages <- 100*( sample[sample_name] / total_reads)

  feature_count_tibble_all_samples = bind_cols(feature_count_tibble_all_samples,sample$Percentages)

  }
}

rownames(feature_count_tibble_all_samples) <- rownames(data_1)

fc_tibble_all_samples_nonzero <- feature_count_tibble_all_samples[rowSums(feature_count_tibble_all_samples > 0)==dim(feature_count_tibble_all_samples)[2],]

#write.csv(fc_tibble_all_samples_nonzero, "percentages-tRNA-amino-acid.csv")

fc_tibble_all_samples_nonzero <- fc_tibble_all_samples_nonzero %>%

  rownames_to_column("RNA")

plot_me <- melt(fc_tibble_all_samples_nonzero, by="Row.names")
qual_cols_pal <- brewer.pal.info[brewer.pal.info$category == "qual",]
col_vector <- unlist(mapply(brewer.pal, qual_cols_pal$maxcolors, rownames(qual_cols_pal)))


plot <- ggplot(plot_me, aes(x = variable, y = value, fill = RNA)) +

       geom_bar(stat = 'identity') + scale_fill_manual(values=col_vector)+
  theme(axis.text.x = element_text(angle=90, hjust=1)) + theme_bw() + theme(axis.text.x = element_text(angle = 90, hjust = 1))


ggplotly(plot)
```

# Total tRNA fragments

```{r}
file_paths = list.files(path= "../tRNA-mapping.dir",pattern='_coverage.bed$', recursive=TRUE, full.names = TRUE)
num_samples <- length(file_paths)
feature_count_tibble = data.frame(matrix(ncol = 0, nrow=7))


for(i in 1:num_samples){
  sample_name <- sub("../tRNA-mapping.dir/(.*)?_fragment_coverage.bed", "\\1", file_paths[i])
  
  data <- read.table(file_paths[i])
  data_1 <- data %>%  dplyr::group_by(V4)
  data_1 %>%  summarise_at("V7",sum) %>%  rename(fragment = V4, Count = V7) -> data_1
  
  total_reads = sum(data_1$Count)

  data_1 %>%  mutate(Sample = as.character(sample_name)) -> data_1
  
  data_1$Percentages <- 100*( data_1$Count / total_reads)
  
  feature_count_tibble = rbind(feature_count_tibble, data_1)

  }





qual_cols_pal <- brewer.pal.info[brewer.pal.info$category == "qual",]
col_vector <- unlist(mapply(brewer.pal, qual_cols_pal$maxcolors, rownames(qual_cols_pal)))

plot <- ggplot(feature_count_tibble, aes(x = Sample, y = Percentages, fill = fragment)) +

       geom_bar(stat = 'identity') + scale_fill_manual(values=col_vector)+
  theme(axis.text.x = element_text(angle=90, hjust=1)) + theme_bw() + theme(axis.text.x = element_text(angle = 90, hjust = 1))

ggplotly(plot)

```

# tRNA halves analysis

```{r}
file_paths = list.files(path= "../tRNA-mapping.dir",pattern='_coverage.bed$', recursive=TRUE, full.names = TRUE)
num_samples <- length(file_paths)
feature_count_tibble = data.frame(matrix(ncol = 0, nrow=7))


for(i in 1:num_samples){
  sample_name <- sub("../tRNA-mapping.dir/(.*)?_fragment_coverage.bed", "\\1", file_paths[i])
  
  data <- read.table(file_paths[i])
  data_1 <- data %>%  dplyr::group_by(V4)
  data_1 %>%  summarise_at("V7",sum) %>%  rename(fragment = V4, Count = V7) -> data_1
  data_1 <- data_1[!data_1$fragment == "tRF-3'",]
  data_1 <- data_1[!data_1$fragment == "tRF-5'",]
  data_1 <- data_1[!data_1$fragment == "tRF-A",]
  data_1 <- data_1[!data_1$fragment == "tRF-AT",]
  data_1 <- data_1[!data_1$fragment == "tRF-T",]
  data_1 <- data_1[!data_1$fragment == "tRF-DA",]
  data_1 <- data_1[!data_1$fragment == "tRF-D",]
  
  total_reads = sum(data_1$Count)

  data_1 %>%  mutate(Sample = as.character(sample_name)) -> data_1
  
  data_1$Percentages <- 100*( data_1$Count / total_reads)
  
  feature_count_tibble = rbind(feature_count_tibble, data_1)

  }





qual_cols_pal <- brewer.pal.info[brewer.pal.info$category == "qual",]
col_vector <- unlist(mapply(brewer.pal, qual_cols_pal$maxcolors, rownames(qual_cols_pal)))

plot <- ggplot(feature_count_tibble, aes(x = Sample, y = Percentages, fill = fragment)) +

       geom_bar(stat = 'identity') + scale_fill_manual(values=col_vector)+
  theme(axis.text.x = element_text(angle=90, hjust=1)) + theme_bw() + theme(axis.text.x = element_text(angle = 90, hjust = 1))

ggplotly(plot)

```

# tRNA fragment analysis

```{r}
file_paths = list.files(path= "../tRNA-mapping.dir",pattern='_coverage.bed$', recursive=TRUE, full.names = TRUE)
num_samples <- length(file_paths)
feature_count_tibble = data.frame(matrix(ncol = 0, nrow=7))


for(i in 1:num_samples){
  sample_name <- sub("../tRNA-mapping.dir/(.*)?_fragment_coverage.bed", "\\1", file_paths[i])
  
  data <- read.table(file_paths[i])
  data_1 <- data %>%  dplyr::group_by(V4)
  data_1 %>%  summarise_at("V7",sum) %>%  rename(fragment = V4, Count = V7) -> data_1
  data_1 <- data_1[!data_1$fragment == "tRH-3'",]
  data_1 <- data_1[!data_1$fragment == "tRH-DA",]
  data_1 <- data_1[!data_1$fragment == "tRH-DTA",]
  data_1 <- data_1[!data_1$fragment == "tRH-AT",]
  data_1 <- data_1[!data_1$fragment == "tRH-5'",]
  
  total_reads = sum(data_1$Count)

  data_1 %>%  mutate(Sample = as.character(sample_name)) -> data_1
  
  data_1$Percentages <- 100*( data_1$Count / total_reads)
  
  feature_count_tibble = rbind(feature_count_tibble, data_1)

  }





qual_cols_pal <- brewer.pal.info[brewer.pal.info$category == "qual",]
col_vector <- unlist(mapply(brewer.pal, qual_cols_pal$maxcolors, rownames(qual_cols_pal)))

plot <- ggplot(feature_count_tibble, aes(x = Sample, y = Percentages, fill = fragment)) +

       geom_bar(stat = 'identity') + scale_fill_manual(values=col_vector)+
  theme(axis.text.x = element_text(angle=90, hjust=1)) + theme_bw() + theme(axis.text.x = element_text(angle = 90, hjust = 1))

ggplotly(plot)

```