# Berger et al. - Shifts in morphology, gene expression, and selection underlie web loss in Hawaiian Tetragnatha spiders

##R code used for differential expression analysis in DESeq2. File paths should be modified to match the local file structure.

##The following files are available on the gitlab page: https://gitlab.com/Brevver/tetragnathasilks
# og_parsed.txt --> relates transcript names for each species to orthogroup ID's

# RGCB_design.txt --> contains library ID's, species, and group (Spiny Leg or web builder)

library(ggplot2)
library(dplyr)
library(tidyr)
library(tximport)
library(readr)
library(DESeq2)

# set working dir
setwd("F:/RGCB/salmon_counts/mar2020/")

# read in transcript-gene file
tx2gene <- read.csv("og_parsed.txt", header=FALSE)
names(tx2gene)=c("sequence", "gene")

## point to folder containing all counts files (quant.sf files from Salmon)
sort_files  <- file.path("F:/RGCB/salmon_counts/mar2020/counts/", list.files("F:/RGCB/salmon_counts/mar2020/counts/"))

# creat tximport object
tx2gene$sequence <- tools::file_path_sans_ext(tx2gene$sequence)
txi <- tximport(sort_files, type="salmon", tx2gene = tx2gene, countsFromAbundance="no")

design <- read.table("RGCB_design.txt", stringsAsFactors=FALSE)

colnames(txi$counts) <- design$V1
sample_table <- data.frame(species = design$V2, group = design$V3)
rownames(sample_table) <- colnames(txi$counts)

# Create DESeq object
dds <- DESeqDataSetFromTximport(txi, sample_table, ~group)

dds <- DESeqDataSet(dds, ~group)

dds <- DESeq(dds)

## Explore and save results
res <- results(dds, contrast=c("group", "WB", "SL"), alpha=0.05, lfcThreshold = 0.5)
res <- res[order(res$padj),]

# How many DE genes?
length(which(res$padj < 0.05))

write.csv(res, file="WB_vs_SL.csv")

# Calculate shrunken LFC for use with gene ontology

res_shrunk <- lfcShrink(dds, coef="group_WB_vs_SL", type="apeglm", lfcThreshold = 0.5)
res_shrunk <- res_shrunk[order(res_shrunk$svalue),]

write.csv(res_shrunk, file="WB_vs_SL_shrunk.csv")
