---
title: "Creation of comprehensive CSV"
author: "Sam White"
date: "3/1/2023"
output: html_document
---

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

# Load libraries
```{r load-libraries}
library("tidyverse")
```

# Load transcript FPKMs
```{r load-transcript-FPKMs}
transcripts_fpkm <- read.csv("../data/fpkm-all_transcripts.csv")

head(transcripts_fpkm)
```
# Load gene FPKMS
```{r load-gene-FPKMs}
genes_fpkm <- read.csv("../data/whole_gx_table.csv")

head(genes_fpkm)
```

# Load gene methylation
```{r load-gene-methylation}
genes_methylation <- read.csv("../output/40-gene-methylation/40-gene-methylation.csv")

head(genes_methylation)
```

# Rename transcript columns/rows
```{r rename-transcript-columns-and-rows}
# Remove "FPKM." from all column names
colnames(transcripts_fpkm) <- gsub("FPKM.", "", colnames(transcripts_fpkm))

head(transcripts_fpkm)

# Add "t_id." to beginning ("^") of first column data
transcripts_fpkm$t_id <- sub("^", "t_id-", transcripts_fpkm$t_id)

head(transcripts_fpkm)

# Add ".FPKM" to end ("$") of "t_id"
transcripts_fpkm$t_id <- sub("$", ".FPKM", transcripts_fpkm$t_id)

head(transcripts_fpkm)
```

# Transpose transcript FPKM
```{r transpose-transcripts-fpkm}
# Transpose to have sample names as rows and transcript FPKM in columns
transposed.transcripts_fpkm <- as.data.frame(t(transcripts_fpkm))

head(transposed.transcripts_fpkm)

# Convert first row to column names
colnames(transposed.transcripts_fpkm) <- transposed.transcripts_fpkm[1,]
transposed.transcripts_fpkm <- transposed.transcripts_fpkm[-1,]

head(transposed.transcripts_fpkm)
```


# Rename gene columns/rows
```{r rename-transcript-columns-and-rows}
# Remove summary first row
genes_fpkm <- genes_fpkm[-1,]

head(genes_fpkm)

# Remove "FPKM." from all column names
colnames(genes_fpkm) <- gsub("FPKM.", "", colnames(genes_fpkm))

head(genes_fpkm)

# Add ".FPKM" to end ("$") of "name"
genes_fpkm$name <- sub("$", ".FPKM", genes_fpkm$name)

head(genes_fpkm)
```

# Transpose gene FPKM
```{r transpose-genes-fpkm}
# Transpose to have sample names as rows and transcript FPKM in columns
transposed.genes_fpkm <- as.data.frame(t(genes_fpkm))

head(transposed.genes_fpkm)

# Convert first row to column names
colnames(transposed.genes_fpkm) <- transposed.genes_fpkm[1,]
transposed.genes_fpkm <- transposed.genes_fpkm[-1,]

head(transposed.genes_fpkm)
```

# Rename methylation columns/rows
```{r rename-transcript-columns-and-rows}
# Remove extra first column
genes_methylation <- genes_methylation[,-1]

head(genes_methylation)

# Add "S" to beginning ("^") of sample names for consistency
genes_methylation$sample_id <- sub("^", "S", genes_methylation$sample_id)

head(genes_methylation)

# Replace "gene." with "gene-" from all column names for consistency
colnames(genes_methylation) <- gsub("gene.", "gene-", colnames(genes_methylation))


head(genes_methylation)

# Add ".meth" to end ("$") of all columns
colnames(genes_methylation) <- sub("$", ".meth", colnames(genes_methylation))

head(genes_methylation)

# Convert first column to rownames
genes_methylation <- genes_methylation %>%
  remove_rownames%>%
  column_to_rownames(var="sample_id.meth")

head(genes_methylation)
```
# Join data frames

Joining solution taken from here:

https://stackoverflow.com/a/49911159/3470742
```{r join}
# Create list of dataframes
df_list <- list(transposed.transcripts_fpkm, transposed.genes_fpkm, genes_methylation)

# Join all data frames by sample

merged_by_sample <- df_list %>%
  map(~ .x %>% 
        as.data.frame %>% 
        rownames_to_column('sample')
      ) %>% 
  reduce(full_join, by = 'sample') %>% 
  column_to_rownames('sample')
```

## Write joined data frames to CSV
```{r write-joined-dfs-to-CSV}
write.csv(merged_by_sample,
          file = file.path("../output/comprehensive.csv"),
          quote = FALSE,
          row.names = TRUE)
```
