---
title: "15_GO-MWU_prep"
author: "Aspen Coyle"
date: "Last compiled on `r format(Sys.time(), '%Y-%m-%d')`"
output: html_document
---

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

Aspen Coyle, afcoyle@uw.edu

Originally written 2021/01/29

Roberts Lab, UW-SAFS

Written for analysis of _Hematodinium_ differential gene expression

# Purpose:

This script produces the CSV needed for GO-MWU, which is a 2-column table of accession IDs and log2 fold changes. A header line is required, although the contents are irrelevant.

GO-MWU also requires another input file which we already created - a tab-delimited 2-column table of accession IDs and GO terms with no header and only 1 line per gene

# Explanation of geneIDs_foldchange():

This script utilizes a function built for this analysis by me - geneids_foldchange(). It is defined in hematodinium_analysis_functions.R

### Inputs and Outputs

input_file: DESeq2 output file containing transcript IDs and log2 fold changes

blast_file: path that leads to transcript ID/accession ID table. Optimally, use an annotated BLASTx table of the same transcriptome used to create the kallisto index

output_file: path to the output file - a 2-column CSV of accession IDs and log2 fold changes with a header line. This should be written to the same directory that you will run GO-MWU in.

```{r libraries, message = FALSE, warning=FALSE}
# Add all required libraries here
list.of.packages <- "tidyverse"
# Get names of all required packages that aren't installed
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[, "Package"])]
# Install all new packages
if(length(new.packages)) install.packages(new.packages)


# Load all required libraries
lapply(list.of.packages, FUN = function(X) {
  do.call("require", list(X))
})

# Functions are defined in hematodinium_analysis_functions.R
source("hematodinium_analysis_functions.R")
```

# Obtaining CSVs

```{r geneIDs_foldchange} 
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/elev2_vs_amb02_indiv_only/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_elev2_vs_amb02_indiv_l2FC.csv")

# Elevated Day 2 vs. Ambient Day 0+2+17 + Elevated Day 0 + Lowered Day 0
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/amb0217_elev0_low0_vs_elev2/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_amb0217_elev0_low0_vs_elev2_l2FC.csv")

# Elevated Day 0 vs. Elevated Day 2, indiv. libraries only
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/elev0_vs_elev2_indiv/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_elev0_vs_elev2_indiv_l2FC.csv")

# Ambient Day 0 vs. Ambient Day 2, indiv. libraries only
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/amb0_vs_amb2_indiv/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_amb0_vs_amb2_indiv_l2FC.csv")

# Ambient Day 0 vs. Ambient Day 17, indiv. libraries only
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/amb0_vs_amb17_indiv/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_amb0_vs_amb17_indiv_l2FC.csv")

# Ambient Day 2 vs. Ambient Day 17, indiv. libraries only
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/amb2_vs_amb17_indiv/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_amb2_vs_amb17_indiv_l2FC.csv")

# Ambient Day 2 vs. Elevated Day 2, indiv. libraries only
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/amb2_vs_elev2_indiv/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_amb2_vs_elev2_indiv_l2FC.csv")

# Lowered Day 0 vs. Lowered Day 2
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/low0_vs_low2_indiv/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_low0_vs_low2_indiv_l2FC.csv")

# Lowered Day 0 vs. Lowered Day 2 + 17
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/low0_vs_low217_indiv/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_low0_vs_low217_indiv_l2FC.csv")

# Lowered Day 0 + Ambient Day 0 + 2 + 17 vs. Lowered Day 2 + 17
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/low0_amb0217_vs_low217_indiv/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_low0_amb0217_vs_low217_indiv_l2FC.csv")

# Lowered Day 2 + 17 vs. Ambient Day 2+17
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/low217_vs_amb217_indiv/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_low217_vs_amb217_indiv_l2FC.csv")

# Lowered Day 0 vs. Ambient Day 0
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/low0_vs_amb0_indiv/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_low0_vs_amb0_indiv_l2FC.csv")

# Lowered Day 0 vs. Lowered Day 17
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/low0_vs_low17_indiv/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_low0_vs_low17_indiv_l2FC.csv")

# Lowered Day 0 + Ambient Day 0 vs. Lowered Day 17 + Ambient Day 17
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/low0_amb0_vs_low17_amb17_indiv/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_low0_amb0_vs_low17_amb17_indiv_l2FC.csv")

# Lowered Day 0 + Elevated Day 0 vs. Lowered Day 2 + Elevated Day 2
geneIDs_foldchange(input_file = "../graphs/DESeq2_output/cbai_transcriptomev2.0/low0_elev0_vs_low2_elev2_indiv/AllGenes_wcols.txt",
              blast_file = "../data/cbai_diamond_blastx_table_transcriptomev2.0.txt",
              output_file = "../scripts/1_6_running_GO-MWU/cbai2.0_low0_elev0_vs_low2_elev2_indiv_l2FC.csv")

```