---
title: "Evolution of pedomorphic petals in C. hibiscifolia "
subtitle: "Supplementary Code and Results"
author: "Eduardo E. Zattara and Marina M. Strelin"
date: "9/21/2020"
output:
  html_document: default
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r libraries, include=FALSE}
#Load libraries
library(tidyverse) # The tidyverse set of packages
library(gt) # A layered 'grammar of tables' - think ggplot, but for tables
library(edgeR) # Empirical analysis of digital gene expression data in R
library(gridExtra) #Miscellaneous Functions for "Grid" Graphics
library(DESeq2) #DESeq2 package for differential analysis of count data
library(GOfuncR) #Gene Ontology Enrichment Using FUNC
library(wordcloud) #Plot a word cloud
library(tm) #Text Mining in R
library(corpus) # Text corpus analysis functions
library(viridis) #The viridis color palettes
library(clusterProfiler)
library(enrichplot)# for GSEA plot

```


## Library preparation and sequencing
RNA was extracted from petals of *Loasa heterophylla* (LOA) and *Caiophora hibiscifolia* (CAI) at two developmental stages: bud and flower (flo). 12 libraries were initially prepared, comprising 2 species x 2 stages x 3 biological replicates, and sequenced as 100 bp, single-end reads in the Illumina platform. After QC it was found that one of the LOA-bud libraries failed to be properly sequenced;  preliminary data analysis evidenced that another of the  LOA-bud libraries had been derived from a mislabeled sample, and corresponded to LOA flower tissue. This second sample was assigned to the LOA-flo group, and two additional libraries were prepared from frozen LOA bud tissues and sequenced as 150 bp, paired-end reads in the Illumina platform. All reads were stored as FastQ files.


## Initial Transcriptome Assembly
FastQ files for each species were quality trimmed with [Trimmomatic](http://www.usadellab.org/cms/?page=trimmomatic), and fed to [Trinity-v2.4.0](https://github.com/trinityrnaseq/trinityrnaseq/wiki) to assemble species-specific reference transcriptomes. Completeness of each assembly was assessed using [BUSCO](https://busco.ezlab.org) v4.1.3 with the embryophyta_odb10 lineage dataset.

```{r assembly_stats, include=FALSE, echo=FALSE}
trinity_stats <- read_tsv("assembly_stats.tsv")
gt(trinity_stats)
```

## *Camptotheca acuminata* genome
Since there is no available genome from any species within the family Loasaceae, available genomes from the closest possible families were considered as a source for annotated "common" reference. The genome from the Chinese Happy Tree, *Camptotheca acuminata* (**CAMac**, Family Nyssaceae) was chosen for this purpose <https://doi.org./10.1093/gigascience/gix065>. The gene models were annotated using [Trinotate](https://github.com/Trinotate/Trinotate.github.io/wiki). 

Fasta files containing transcript and peptide sequences for all gene models were retrieved from <http://dx.doi.org/10.5061/dryad.nc8qr>:
`cac_hc_gene_models.cdna.fa`  
`cac_hc_gene_models.pep.fa`

A protein database for blast searches was made using makeblastd (requires NCBI's Blast+ from <ftp://ftp.ncbi.nlm.nih.gov/blast/executables/blast+/LATEST/>)

```{bash makeblastdb, eval = FALSE}
     makeblastdb -in cac_hc_gene_models.pep.fa -dbtype prot
```
### Trinotate annotation of CAMac gene models

Although DataDryad's files include a functional annotation table for CAMac gene models, this annotation only includes putative gene names. To generate additional annotations linked to [Gene Ontology](http://geneontology.org/)(GO) terms, [Trinotate](https://github.com/Trinotate) was used, in combination with [Blast+](ftp://ftp.ncbi.nlm.nih.gov/blast/executables/blast+/LATEST/), [Transdecoder](http://transdecoder.github.io/) and [SQLite](http://www.sqlite.org/). [HHMER/PFAM](http://hmmer.org/) and other tools used by Trinotate were not used.

Once installed, Trinotate's boilerplate database, Uniprot/Swisprot and PFAM's reference databases can be downloaded using the command

```{bash, eval = FALSE}
$TRINOTATE_HOME/admin/Build_Trinotate_Boilerplate_SQLite_db.pl ../Trinotate_Boilerplate/Trinotate
```

This will yield three files:

* `Trinotate.sqlite`  Trinotate's boilerplate database
* `uniprot_sprot.pep` Uniprot/Sprot reference database
* `Pfam-A.hmm.gz`     Pfam's database (not used here)
      
The protein database is indexed for blast searches using `makeblastd` (requires [NCBI's Blast+](ftp://ftp.ncbi.nlm.nih.gov/blast/executables/blast+/LATEST/),
   
```{bash, eval = FALSE}
makeblastdb -in ../Trinotate_Boilerplate/uniprot_sprot.pep -dbtype prot
```
   
and then searched against using `cac_hc_gene_models.pep.fa` as query sequences.

```{bash, eval = FALSE}
blastp -query CAMac/cac_hc_gene_models.pep.fa \
   -db ../Trinotate_Boilerplate/uniprot_sprot.pep \
   -num_threads 6 -max_target_seqs 1 -outfmt 6 \
   -evalue 1e-3 > CAMac/CAMac.blastp_vs_uniprot_sprot.outfmt6 &
   
#Optional: Monitor output using tail
tail -f CAMac/CAMac.blastp_vs_uniprot_sprot.outfmt6
```

Trinotate needs peptide files with coordinates as given by the [TransDecoder](https://github.com/TransDecoder/TransDecoder/wiki) peptide prediction package
   

```{bash, eval = FALSE}
     $TRANSDECODER_HOME/TransDecoder.LongOrfs -t CAMac/cac_hc_gene_models.cdna.fa 
     $TRANSDECODER_HOME/TransDecoder.Predict -t CAMac/cac_hc_gene_models.cdna.fa
```

It also requires a gene-to-transcript table (since it is designed to deal with Trinity output). In this case, a list of all CAMac gene models repeated in two columns will serve.

First, we use bash `grep` to extract a list of gene names from the fasta file.

```{bash, eval = FALSE}
     grep '>' CAMac/cac_hc_gene_models.pep.fa | sed 's/>//g' > CAMac/cac_hc_gene_models.pep.names
```

That file is read in R, columns are duplicated and then exported.

```{r, eval = TRUE}
#   Read list of gene names and duplicate as columns to generate the gene-to-transcript map needed for Trinotate
CAMac_names <- readr::read_table("cac_hc_gene_models.pep.names", col_names = FALSE)
CAMac_names %>% mutate(X2 = X1) -> CAMac_names
write_tsv(CAMac_names, file = "CAMac_gene_to_transcript.map", col_names = F)

```

Now everything necessary to fill the Trinotate database is ready. 

```{bash, eval = FALSE}
#   Initialize Trinotate's database and load Camptotheca gene models
$TRINOTATE_HOME/Trinotate ../Trinotate_Boilerplate/Trinotate.sqlite init \
            --gene_trans_map CAMac/CAMac_gene_to_transcript.map \
            --transcript_fasta CAMac/cac_hc_gene_models.cdna.fa \
            --transdecoder_pep CAMac/cac_hc_gene_models.cdna.fa.transdecoder.pep

#   Load uniprot blast hits
$TRINOTATE_HOME/Trinotate ../Trinotate_Boilerplate/Trinotate.sqlite \
            LOAD_swissprot_blastp CAMac/CAMac.blastp_vs_uniprot_sprot.outfmt6

#   Export Trinotate's annotation report
     $TRINOTATE_HOME/Trinotate ../Trinotate_Boilerplate/Trinotate.sqlite \
            report > CAMac/CAMac_trinotate_annotation_report.tsv


```

Now that we have the Trinotate annotation report, we can import it to R and merge it with the existing annotations. 

```{r, eval = TRUE}
#Read annotations from Camptotheca annotation table
CAMac_trinotate_annot <- read_tsv("CAMac_trinotate_annotation_report.tsv", na = ".",) %>%
  select(geneID,transcript_id,sprot_Top_BLASTX_hit,ARAth_BLASTX, Kegg, gene_ontology_BLASTX)

#and remove duplicate entries
CAMac_trinotate_annot <- CAMac_trinotate_annot[!duplicated(CAMac_trinotate_annot),]

#Include functional annotation table from http://dx.doi.org/10.5061/dryad.nc8qr
CAMac_annot <- read_delim("cac_hc_gene_models.func_anno.txt", delim = "\t", col_names=FALSE)
names(CAMac_annot)<-c("geneID","annotation")

#Merge both tables
CAMac_annot <- left_join(CAMac_annot, CAMac_trinotate_annot) %>% select(-transcript_id)
```


## Mapping *Loasa*'s and *Caiophora*'s transcripts to CAMac gene models and collapsing reads.

### Map *Loasa* counts to *Camptotheca* blast hits

Perform a blast search of all Trinity transcripts from *Loasa heterophylla* against *Camptotheca*'s peptides

```{bash, eval = FALSE}

blastx -query LOAhet/LOAhet.v3.Trinity.fasta \
       -db CAMac/cac_hc_gene_models.pep.fa \
       -num_threads 6 -max_target_seqs 1 -outfmt 6 \
       -evalue 1e-3 > LOAhet/LOAhet.v3.blastx_vs_CAMac_peptides.outfmt6 &

#Optional: Monitor output using tail
tail -f LOAhet/LOAhet.v3.blastx_vs_CAMac_peptides.outfmt6
```

Once the blast search is complete, results are imported.

```{r, eval = TRUE}
#Import blast output
blast_outfmt6_headers <- c("query_id","CAMac_id","pident","length", "mismatch", "gapopen",                                                                "qstart","qend","sstart","send","evalue","bitscore")

loa_to_cam <- read_tsv("LOAhet.v3.blastx_vs_CAMac_peptides.outfmt6", 
               col_names = blast_outfmt6_headers)

loa_to_cam$geneID <- str_replace(loa_to_cam$query_id, "TRINITY", "LOAhetv3")


```

Then the RSEM count matrices at the isoform level are imported, and the `loa_to_cam` table is used to assign a CAMac gene model to *Loasa*'s isoforms that had a blast hit. Since an `inner_join` operation is used, all isoforms without a blast hit are discarded. Then, reads for all isoforms matching a single CAMac gene are collapsed by summing. 

```{r, eval = TRUE}
#Read count tables
loa_isoform_cts <- read.delim("LOAhet.RSEM.isoform.counts.matrix", row.names = 1) %>%
  as_tibble(rownames = "query_id") 

#Check that total fragment counts per sample are mostly similar
colSums(loa_isoform_cts[,2:8], na.rm = T)

# Inner join to generate a CAI isoform to CAM transcript
loa_isof_cts_cam <- loa_to_cam %>% 
  select(query_id, CAMac_id) %>%
  inner_join(loa_isoform_cts)

# Replace the word "TRINITY" to a species-specific term, useful if transcript names from both species are merged
loa_isof_cts_cam$query_id <- str_replace(loa_isof_cts_cam$query_id, "TRINITY", "LOAhetv3")

# Collapse all counts from isoforms mapping to the same Camptotheca gene
loa_isof_cts_cam %>% 
  select(-query_id) %>%
  group_by(CAMac_id) %>% 
  summarise_all(list(sum = sum)) -> loa_gene_cts_cam
loa_gene_cts_cam
```

### Map *Caiophora* counts to *Camptotheca* blast hits

Perform a blast search of all Trinity transcripts from *Caiophora hibiscifolia* against *Camptotheca*'s peptides

```{bash, eval = FALSE}

blastx -query CAIhib.v2.Trinity.fasta -db cac_hc_gene_models.pep.fa -num_threads 6 -max_target_seqs 1 -outfmt 6 -evalue 1e-3 > CAIhib.v2.blastx_vs_CAMac_peptides.outfmt6 &

blastx -query CAIhib/CAIhib.v2.Trinity.fasta \
       -db CAMac/cac_hc_gene_models.pep.fa \
       -num_threads 6 -max_target_seqs 1 -outfmt 6 \
       -evalue 1e-3 > CAIhib/CAIhib.v2.blastx_vs_CAMac_peptides.outfmt6 &

#Optional: Monitor output using tail
tail -f CAIhib/CAIhib.v2.blastx_vs_CAMac_peptides.outfmt6
```

Once the blast search is complete, results are imported.

```{r, eval = TRUE}
#Import blast output
cai_to_cam <- read_tsv("CAIhib.v2.blastx_vs_CAMac_peptides.outfmt6", 
                       col_names = blast_outfmt6_headers)

```

Then the RSEM count matrices at the isoform level are imported, and the `cai_to_cam` table is used to assign a CAMac gene model to *Caiophora*'s isoforms that had a blast hit. Since an `inner_join` operation is used, all isoforms without a blast hit are discarded. Then, reads for all isoforms matching a single CAMac gene are collapsed by summing. 

```{r, eval = TRUE}
#Read count tables
cai_isoform_cts <- read.delim("CAIhib.RSEM.isoforms.counts.matrix", row.names = 1) %>%
  as_tibble(rownames = "query_id") 
#Check that total fragment counts per sample are mostly similar
colSums(cai_isoform_cts[,2:7], na.rm = T)

# Replace the word "TRINITY" to a species-specific term, useful if transcript names from both species are merged
cai_isoform_cts$query_id <- str_replace(cai_isoform_cts$query_id, "TRINITY", "CAIhibv2")

# Inner join to generate a CAI isoform to CAM transcript
cai_isof_cts_cam <- cai_to_cam %>% 
  select(query_id, CAMac_id) %>%
  inner_join(cai_isoform_cts)

# Collapse all counts from isoforms mapping to the same Camptotheca gene
cai_isof_cts_cam %>% 
  select(-query_id) %>%
  group_by(CAMac_id) %>% 
  summarise_all(list(sum = sum)) -> cai_gene_cts_cam

```


### Merge *Loasa* and *Caiophora* reads based on *Camptotheca* gene matches
Sample (row) labels are simplified and both count tables are merged using an inner join so only collections of transcript counts with hits to CAMac in both species are retained.

```{r, eval = TRUE}
sample.labels <- c("L.Bud.1","L.Bud.2","L.Bud.3","L.Flo.1","L.Flo.2","L.Flo.3","L.Flo.4","C.Bud.1","C.Bud.2", "C.Bud.3","C.Flo.1","C.Flo.2","C.Flo.3")
CAMac_loa_cai_cts <- inner_join(loa_gene_cts_cam, cai_gene_cts_cam)
names(CAMac_loa_cai_cts) <- c("geneID",sample.labels)
```


## Normalization and data exploration

### Normalizing using the trimmed mean of M-values

One simple yet robust way to estimate the ratio of RNA production uses a weighted trimmed mean of the log expression ratios (trimmed mean of M values (TMM) ([Robinson & Oshlack 2010](https://genomebiology.biomedcentral.com/articles/10.1186/gb-2010-11-3-r25))). This normalizes by effective library size, but not feature length.

```{r, eval = TRUE}
rnaseqMatrix <- CAMac_loa_cai_cts %>% select(-geneID) %>% as.matrix()
row.names(rnaseqMatrix) <-CAMac_loa_cai_cts$geneID
exp_study = edgeR::DGEList(counts=rnaseqMatrix, group=factor(colnames(rnaseqMatrix)))
exp_study = edgeR::calcNormFactors(exp_study)
exp_study$samples$eff.lib.size = exp_study$samples$lib.size * exp_study$samples$norm.factors
CAMac_loa_cai_TMM <- edgeR::cpm(rnaseqMatrix) %>% as_tibble(rownames = "geneID")
```


### Stats and data distribution 
After generating a `DGElist` from the counts, the counts are normalized to counts-per-million (cpm) using the `edgeR::cpm` function. A filter is applied to remove any genes not having at least 1 cpm in at least three samples. Counts are then log2 transformed, and the table is pivoted to generate violin plots showing the distribution 

```{r, eval = TRUE}
myDGEList <- edgeR::DGEList(rnaseqMatrix)
# take a look at the DGEList object 
myDGEList

#Get counts per million using the 'cpm' function from EdgeR
cpm <- edgeR::cpm(myDGEList) 
colSums(cpm)
log2.cpm <- cpm(myDGEList, log=TRUE)

# now set some cut-off to get rid of genes/transcripts with low counts
# again using rowSums to tally up the 'TRUE' results of a simple evaluation
# how many genes had more than 1e-7 CPM (TRUE) in all 13 samples
keepers <- rowSums(cpm>0.0000001)>=13 # Adjust this cutoff for the number of samples in the smallest group of comparisons.
# now use base R's simple subsetting method to filter your DGEList based on the logical produced above
myDGEList.filtered <- myDGEList[keepers,]
dim(myDGEList.filtered)

log2.cpm.filtered <- cpm(myDGEList.filtered, log=TRUE)
log2.cpm.filtered.df <- as_tibble(log2.cpm.filtered, rownames = "geneID")
colnames(log2.cpm.filtered.df) <- c("geneID", sample.labels)

# pivot this FILTERED data to make a tidyverse compatible table
log2.cpm.filtered.df.pivot <- pivot_longer(log2.cpm.filtered.df, # dataframe to be pivoted
                                           cols = 2:14, # column names to be stored as a SINGLE variable
                                           names_to = "samples", # name of that new variable (column)
                                           values_to = "expression") # name of new variable (column) storing all the values (data)
ggplot(log2.cpm.filtered.df.pivot) +
  aes(x=samples, y=expression, fill = samples) +
  geom_violin(trim = FALSE, show.legend = FALSE) +
  stat_summary(fun = "median", 
               geom = "point", 
               shape = 95, 
               size = 10, 
               color = "black", 
               show.legend = FALSE) +
  labs(y="log2 expression", x = "sample",
       title="Log2 Counts per Million (CPM)",
       subtitle="filtered, non-normalized",
       caption=paste0("produced on ", Sys.time())) +
  theme_bw()

```


### Principal component analysis (PCA)

After verifying that all samples have a similar distribution of expression values, a PCA is used to find which factors are driving the variance, test for potential batch effects, and examine overall distribution of samples in variable space.

```{r, eval = TRUE}
#Read in design table
targets <- read_tsv("samples.txt") %>% mutate(group = paste0(species,"-",stage))

#Identify variables of interest in study design file
group <- targets$group
group <- factor(group)

myDGEList.filtered.norm <- calcNormFactors(myDGEList.filtered, method = "TMM")

# use the 'cpm' function from EdgeR to get counts per million from your normalized data
log2.cpm.filtered.norm <- cpm(myDGEList.filtered.norm, log=TRUE)

pca.res <- prcomp(t(log2.cpm.filtered.norm), scale.=F, retx=T)

#look at the PCA result (pca.res) that you just created
summary(pca.res) # Prints variance summary for all principal components.
screeplot(pca.res) # A screeplot is a standard way to view eigenvalues for each PCA
pc.var<-pca.res$sdev^2 # sdev^2 captures these eigenvalues from the PCA result
pc.per<-round(pc.var/sum(pc.var)*100, 1) # we can then use these eigenvalues to calculate the percentage variance explained by each PC
pc.per
```

Half of the variance is explained by PC1, and another 18% is explained by PC2. To explore the influence of each grouping variable in a series of barplots of the loading of each sample. 

```{r, eval = TRUE}

# Create a PCA 'small multiples' chart ----
# this is another way to view PCA loading to understand impact of each sample on each principal component
pca.res.df <- pca.res$x[,1:4] %>% 
  as_tibble() %>%
  add_column(sample = sample.labels,
             group = group,
             stage = targets$stage,
             batch = as.factor(targets$batch),
             seqtype = targets$seqtype,
             species = targets$species)

pca.pivot <- pivot_longer(pca.res.df, # dataframe to be pivoted
                          cols = PC1:PC4, # column names to be stored as a SINGLE variable
                          names_to = "PC", # name of that new variable (column)
                          values_to = "loadings") # name of new variable (column) storing all the values (data)

pspecies <- ggplot(pca.pivot) +
  aes(x=sample, y=loadings, fill=species) +
  geom_bar(stat="identity") +
  facet_wrap(~PC) +
  labs(title="Species",
       caption=paste0("produced on ", Sys.time())) +
  theme_bw() +
  coord_flip()

pstage <- ggplot(pca.pivot) +
  aes(x=sample, y=loadings, fill=stage) + 
  geom_bar(stat="identity") +
  facet_wrap(~PC) +
  labs(title="Stage",
       caption=paste0("produced on ", Sys.time())) +
  theme_bw() +
  coord_flip()

pbatch <- ggplot(pca.pivot) +
  aes(x=sample, y=loadings, fill=batch) + 
  geom_bar(stat="identity") +
  facet_wrap(~PC) +
  labs(title="Batch",
       caption=paste0("produced on ", Sys.time())) +
  theme_bw() +
  coord_flip()
```

PC1 shows that half of the variance is clearly driven by species.

```{r, eval=TRUE}
pspecies
```

PC2, explaining 18.6% of the variance, responds to stage. The absolute value of the loadings are much larger for *Caiophora* samples.

```{r, eval=TRUE}
pstage
```

Finally, we can see that none of the first four PCs (which together explain 87.2% of the variance) is solely driven by batch, although PC3 (8%) seems to show some batch effect.

```{r, eval=TRUE}
pbatch
```

Thus, we can get an idea of how samples relate to each other by plotting them against the first two PCs, which cover 74.2% of the variance. 

```{r, eval=TRUE}
# Visualize your PCA result

pca.res.df <- as_tibble(pca.res$x)
ggplot(pca.res.df) +
  aes(x=PC1, y=PC2, label=sample.labels, color=group) +
  geom_point(size=4) +
  #  geom_label() +
#  stat_ellipse() +
  xlab(paste0("PC1 (",pc.per[1],"%",")")) + 
  ylab(paste0("PC2 (",pc.per[2],"%",")")) +
  labs(title="PCA plot",
       caption=paste0("produced on ", Sys.time())) +
  coord_fixed() +
  theme_bw()
```

As expected, samples from each species are widely separated by PC1, while PC2 separates stages. Interestingly the distance between stages is larger for *Caiophora* than for *Loasa*. Although there is a noticeable difference between the first **LOA-het-bud** sample and the next two (generated in a separate batch), they still point in the same direction; furthermore the latter are closer to the flower stage, thus reducing rather than biasing the stage-driven variance. 

From this exploratory results, it will be expected that most differential gene expression will be found when comparing across species, with all possible comparisons yielding approximately the similarly sized DEG repertoires. In contrast, we would expect that contrasts between stages will yield a larger DGE repertoire in *Caiophora* than in *Loasa*.


## Differential gene expression analyses

Differentially expressed genes (DEGs) can be detected using several approaches. Here we use the [DESeq2](https://doi.org/10.1186/s13059-014-0550-8) approach, which estimates variance-mean dependence in count data and tests for DEGs using a model based on the negative bionomial distribution.

### Gather sample design information, build dds object and run DESeq

```{r, eval=TRUE, warning = FALSE}
#Read sample information table
sampleColdata <- read.delim("samples.txt")
sampleColdata <- sampleColdata %>% mutate(sp_stage = paste(species,stage, sep = "_"))
sampleColdata$batch <- as.factor(sampleColdata$batch)

rownames(sampleColdata) <- sampleColdata$sample

gene_cts <- CAMac_loa_cai_cts %>% select(-geneID) %>% round()
gene_cts <- as.data.frame(gene_cts[,rownames(sampleColdata)])
rownames(gene_cts) <- CAMac_loa_cai_cts$geneID

#check all sample names are in column names, in the same order
all(rownames(sampleColdata) %in% colnames(gene_cts)) # Debe devolver "TRUE"
all(rownames(sampleColdata) == colnames(gene_cts)) # Debe devolver "TRUE"

#Assemble DESeq Data set object
dds_clc <- DESeqDataSetFromMatrix(countData = gene_cts,
                                  colData = sampleColdata,
                                  design = ~ sp_stage)
#Run DESeq 
dds_clc <- DESeq(dds_clc)

```

### DEGs between stages in *Loasa*

Once the DESeq object has been created and analyzed, it is possible to use `results` to extract the results from specific contrasts. The false discovery rate (FDR) threshold for significance is set to 0.05. 

```{r, eval=TRUE}

# Extract the results of the specific contrasts 
res.LOA.flo_vs_bud <- results(dds_clc, contrast = c("sp_stage", "LOAhet_flo", "LOAhet_bud"), alpha=0.05)

summary(res.LOA.flo_vs_bud)
```

Almost one-quarter of the genes are DE between bud and flower stages in *Loasa*, distributed about equally in up- and downregulated genes. 

An MA plot shows the log2 fold changes attributable to stage over the mean of normalized counts for all the samples. Colored points indicate DEGs.

```{r, eval=TRUE, warning = FALSE}
# MA Plot
plotMA(res.LOA.flo_vs_bud)
```

Another way of visualizing the distribution is a volcano plot. The volcano plot shows up- (right) or down- (left) regulation on an inverted log scale so that genes showing a smaller adjusted *p-value* are higher. DEGs at the chosen level are shown in red.

```{r, eval=TRUE, warning = FALSE}
#Volcano Plots
res.LOA.flo_vs_bud.tib <- as_tibble(res.LOA.flo_vs_bud, rownames = "geneID")

ggplot(res.LOA.flo_vs_bud.tib%>% filter(padj>=0.05)) +
  aes(y=-log10(padj), x=log2FoldChange) +
  geom_point(size=2, colour = "black", alpha=.2)+ 
  xlim (-10,10) + ylim(0,40)+ 
  geom_point(data=res.LOA.flo_vs_bud.tib %>% filter(padj<0.05), 
             colour = "red", alpha=.2) +
  labs(title="Loasa heterofila",
       subtitle = "Flower vs petal stage",
       caption=paste0("produced on ", Sys.time())) +
  theme_bw()

```

After defining the set of DEGs, they can be extracted and clustered to generate a heatmap of the *Loasa* samples.

```{r, eval=TRUE}

# Subset DEGs (padj < 0.05)
sig.05.res.LOA.flo_vs_bud.tib <- res.LOA.flo_vs_bud.tib %>% filter(padj<0.05) %>% arrange(desc(padj))

# Cluster DEGs and generate a heatmap for Loasa samples
#Convert TMM expression values into matrix
sig.05.res.LOA.flo_vs_bud.TMM <- CAMac_loa_cai_TMM[,1:8] %>% 
  filter(geneID %in% sig.05.res.LOA.flo_vs_bud.tib$geneID) 
geneIDs <- sig.05.res.LOA.flo_vs_bud.TMM$geneID

sig.05.res.LOA.flo_vs_bud.TMM.matrix <- sig.05.res.LOA.flo_vs_bud.TMM %>% 
  select(-geneID) %>% 
  as.matrix()
rownames(sig.05.res.LOA.flo_vs_bud.TMM.matrix) <- sig.05.res.LOA.flo_vs_bud.TMM$geneID

#clustering  genes (rows) in each DEG set
# we use the 'cor' function and the Pearson method for finding all pairwise correlations of genes
# '1-cor' converts this to a 0-2 scale for each of these correlations, which can then be used to 
# calculate a distance matrix using 'as.dist'
clustRows <- hclust(as.dist(1-cor(t(sig.05.res.LOA.flo_vs_bud.TMM.matrix), method="pearson")), method="complete") 

#clustering samples (columns) by spearman correlation
clustColumns <- hclust(as.dist(1-cor(sig.05.res.LOA.flo_vs_bud.TMM.matrix, method="spearman")), method="complete") #cluster columns 
#note: we use Spearman, instead of Pearson, for clustering samples because it gives equal weight to highly vs lowly expressed transcripts or genes

#Cut the resulting tree in k=2 clusters. k migh be changed to better fit the data   
module.assign <- cutree(clustRows, k=2)

#create color vector for clusters 
module.color <- rainbow(length(unique(module.assign)), start=0.1, end=0.9) 
module.color <- module.color[as.vector(module.assign)] 
myheatcolors2 <- colorRampPalette(colors=c("blue","white","red"))(100)

# Produce a static heatmap of DEGs 
gplots::heatmap.2(sig.05.res.LOA.flo_vs_bud.TMM.matrix, 
          Rowv=as.dendrogram(clustRows), 
          Colv=as.dendrogram(clustColumns),
          RowSideColors=module.color,
          col=myheatcolors2, scale='row', labRow=NA,
          density.info="none", trace="none",  
          cexRow=1, cexCol=1, margins=c(5,2),
          keysize = 1.0, key = FALSE,
          main = "Loasa heterophylla - Flower vs Bud Stage") 
```


The heatmap shows that the `r dim(sig.05.res.LOA.flo_vs_bud.tib)[1]` DEGs can be clustered into two main sets  whose expression is reversed between both stages. 

### DEGs between stages in *Caiophora*

Now, similar steps are repeated, this time comparing *Caiophora*'s bud and flower samples.As before, the false discovery rate (FDR) threshold for significance is set to 0.05. 

```{r, eval=TRUE}

# Extract the results of the specific contrasts ----
res.CAI.flo_vs_bud <- results(dds_clc, contrast = c("sp_stage", "CAIhib_flo", "CAIhib_bud"), alpha=0.05)

summary(res.CAI.flo_vs_bud)
```

For this comparison, 34% of the genes are DE between bud and flower stages, again distributed about equally in up- and down-regulated genes. This represents ~10% more DEGs than the same comparison across stages for the bee-pollinated species. 

An MA plot shows the log2 fold changes attributable to stage over the mean of normalized counts for all the samples. Colored points indicate DEGs.

```{r, eval=TRUE, warning = FALSE}
# MA Plot
plotMA(res.CAI.flo_vs_bud)
```

The volcano plot shows up- (right) or down- (left) regulation on an inverted log scale so that genes showing a smaller adjusted *p-value* are higher. DEGs at the chosen level are shown in red.

```{r, eval=TRUE, warning = FALSE}
#Volcano Plots
res.CAI.flo_vs_bud.tib <- as_tibble(res.CAI.flo_vs_bud, rownames = "geneID")

ggplot(res.CAI.flo_vs_bud.tib%>% filter(padj>=0.05)) +
  aes(y=-log10(padj), x=log2FoldChange) +
  geom_point(size=2, colour = "black", alpha=.2)+ 
  xlim (-10,10) + ylim(0,40)+
  geom_point(data=res.CAI.flo_vs_bud.tib %>% filter(padj<0.05), 
             colour = "red", alpha=.2)+
  labs(title="Caiophora hibiscifolia",
       subtitle = "Flower vs petal stage",
       caption=paste0("produced on ", Sys.time())) +
  theme_bw()

```

After defining the set of DEGs, they can be extracted and clustered to generate a heatmap of the *Caiophora* samples.

```{r, eval=TRUE}

# Subset DE genes (padj < 0.05)
sig.05.res.CAI.flo_vs_bud.tib <- res.CAI.flo_vs_bud.tib %>% filter(padj<0.05) %>% arrange(desc(padj))

#Convert TMM expression values into matrix
sig.05.res.CAI.flo_vs_bud.TMM <- CAMac_loa_cai_TMM[,c(1,9:14)] %>% 
  filter(geneID %in% sig.05.res.CAI.flo_vs_bud.tib$geneID) 
geneIDs <- sig.05.res.CAI.flo_vs_bud.TMM$geneID

sig.05.res.CAI.flo_vs_bud.TMM.matrix <- sig.05.res.CAI.flo_vs_bud.TMM %>% 
  select(-geneID) %>% 
  as.matrix()
rownames(sig.05.res.CAI.flo_vs_bud.TMM.matrix) <- sig.05.res.CAI.flo_vs_bud.TMM$geneID

#cluster genes (rows) 
clustRows <- hclust(as.dist(1-cor(t(sig.05.res.CAI.flo_vs_bud.TMM.matrix), method="pearson")), method="complete") 

# cluster samples (columns)
clustColumns <- hclust(as.dist(1-cor(sig.05.res.CAI.flo_vs_bud.TMM.matrix, method="spearman")), method="complete") 

#Cut the resulting tree and create color vector for clusters.  
module.assign <- cutree(clustRows, k=3)

module.color <- rainbow(length(unique(module.assign)), start=0.1, end=0.9) 
module.color <- module.color[as.vector(module.assign)] 
myheatcolors2 <- colorRampPalette(colors=c("blue","white","red"))(100)

# Produce a static heatmap of DEGs 
gplots::heatmap.2(sig.05.res.CAI.flo_vs_bud.TMM.matrix, 
          Rowv=as.dendrogram(clustRows), 
          Colv=as.dendrogram(clustColumns),
          RowSideColors=module.color,
          col=myheatcolors2, scale='row', labRow=NA,
          density.info="none", trace="none",  
          cexRow=1, cexCol=1, margins=c(5,2),
          keysize = 1.0, key = FALSE,
          main = "Caiophora hibiscifolia - Flower vs Bud Stage") 
```



The heatmap shows that the `r dim(sig.05.res.CAI.flo_vs_bud.tib)[1]` DEGs can be clustered into two main sets whose expression is reversed between both stages, a similar pattern to that shown by the other species. 

### Differentially expressed genes across stages for both species.


```{r}
#Convert TMM expression values into matrix
sig.05.res.flo_vs_bud.TMM <- CAMac_loa_cai_TMM %>% 
  filter(geneID %in% sig.05.res.CAI.flo_vs_bud.tib$geneID | geneID %in% sig.05.res.LOA.flo_vs_bud.tib$geneID) 

geneIDs <- sig.05.res.flo_vs_bud.TMM$geneID
sig.05.res.flo_vs_bud.TMM.matrix <- sig.05.res.flo_vs_bud.TMM %>% 
  select(-geneID) %>% 
  as.matrix()
rownames(sig.05.res.flo_vs_bud.TMM.matrix) <- sig.05.res.flo_vs_bud.TMM$geneID

#cluster genes (rows) 
clustRows <- hclust(as.dist(1-cor(t(sig.05.res.flo_vs_bud.TMM.matrix), method="pearson")), method="complete") 

# cluster samples (columns)
clustColumns <- hclust(as.dist(1-cor(sig.05.res.flo_vs_bud.TMM.matrix, method="spearman")), method="complete") 

#Cut the resulting tree and create color vector for clusters.  
module.assign <- cutree(clustRows, k=9)

module.color <- rainbow(length(unique(module.assign)), start=0.1, end=0.9) 
module.color <- module.color[as.vector(module.assign)] 
myheatcolors2 <- colorRampPalette(colors=c("blue","white","red"))(100)

# Produce a static heatmap of DEGs 
gplots::heatmap.2(sig.05.res.flo_vs_bud.TMM.matrix, 
          Rowv=as.dendrogram(clustRows), 
          Colv=as.dendrogram(clustColumns),
          RowSideColors=module.color,
          col=myheatcolors2, scale='row', labRow=NA,
          density.info="none", trace="none",  
          cexRow=1, cexCol=1, margins=c(5,2),
          keysize = 1.0, key = FALSE,
          main = "Flower vs Bud Stage DEGs") 



```

Out of `r dim(CAMac_loa_cai_TMM)[1]` sets of assembled transcripts that share a hit to a *Camptotheca acuminata* gene model, contrasting flower versus bud stage petal tissues and setting the FDR at 0.05 yields `r dim(sig.05.res.LOA.flo_vs_bud.TMM)[1]` DEGs for *Loasa heterophylla* and `r dim(sig.05.res.CAI.flo_vs_bud.TMM)[1]` DEGs for *Caiophora hibiscifolia*. 

The transcriptional profiles across all samples is less clear-cut than when analyzing separately by species, highlighting the differences between how transcription changes between stages in each of the two species.

```{r}
#Convert TMM expression values into matrix
common_DEGS <- inner_join(sig.05.res.LOA.flo_vs_bud.TMM, sig.05.res.CAI.flo_vs_bud.TMM)
sig.05.res.flo_vs_bud.TMM.common <- CAMac_loa_cai_TMM %>% 
  filter(geneID %in% common_DEGS$geneID) 

sig.05.res.flo_vs_bud.TMM.common.matrix <- sig.05.res.flo_vs_bud.TMM.common %>% 
  select(-geneID) %>% 
  as.matrix()
rownames(sig.05.res.flo_vs_bud.TMM.common.matrix) <- sig.05.res.flo_vs_bud.TMM.common$geneID

#cluster genes (rows) 
clustRows <- hclust(as.dist(1-cor(t(sig.05.res.flo_vs_bud.TMM.common.matrix), method="pearson")), method="complete") 

# cluster samples (columns)
clustColumns <- hclust(as.dist(1-cor(sig.05.res.flo_vs_bud.TMM.common.matrix, method="spearman")), method="complete") 

#Cut the resulting tree and create color vector for clusters.  
module.assign <- cutree(clustRows, k=9)

module.color <- rainbow(length(unique(module.assign)), start=0.1, end=0.9) 
module.color <- module.color[as.vector(module.assign)] 
myheatcolors2 <- colorRampPalette(colors=c("blue","white","red"))(100)

# Produce a static heatmap of DEGs 
gplots::heatmap.2(sig.05.res.flo_vs_bud.TMM.common.matrix, 
          Rowv=as.dendrogram(clustRows), 
          Colv=as.dendrogram(clustColumns),
          RowSideColors=module.color,
          col=myheatcolors2, scale='row', labRow=NA,
          density.info="none", trace="none",  
          cexRow=1, cexCol=1, margins=c(5,2),
          keysize = 1.0, key = FALSE,
          main = "Shared Flower vs Bud Stage DEGs") 

```

A total of  `r dim(inner_join(sig.05.res.LOA.flo_vs_bud.TMM, sig.05.res.CAI.flo_vs_bud.TMM))[1]` genes are shared across both sets of DEGs. Looking at the transcriptional profiles of this subset allows identification of DEG clusters, some showing more similar patterns of change between stages for both species (for example, the top blue and the bottom clusters), while others are more dissimilar.

### DEGs across species at similar stages


```{r}
# 3.6 DEG across species ----
res.bud.cai_vs_loa <- results(dds_clc, contrast = c("sp_stage", "CAIhib_bud", "LOAhet_bud"), alpha=0.05)
summary(res.bud.cai_vs_loa)

sig.05.res.bud.cai_vs_loa <- res.bud.cai_vs_loa %>% 
  as_tibble(rownames = "geneID") %>%
  filter(padj<0.05) %>% 
  arrange(padj)

res.flo.cai_vs_loa <- results(dds_clc, contrast = c("sp_stage", "CAIhib_flo", "LOAhet_flo"), alpha=0.05)
summary(res.flo.cai_vs_loa)

sig.05.res.flo.cai_vs_loa <- res.flo.cai_vs_loa %>% 
  as_tibble(rownames = "geneID") %>%
  filter(padj<0.05) %>% 
  arrange(padj)

```

There are `r dim(sig.05.res.bud.cai_vs_loa)[1]` DEGs between *Loasa* and *Caiophora* at the bud stage. This is almost half of the total annotated genome. At the flower stage, there are even more DEGs: `r dim(sig.05.res.flo.cai_vs_loa)[1]`. These results are consistent with the PCA plots, and are also consistent with the idea that transcriptional profiles diverge as organs develop and morphological divergence becomes more evident. 


## Functional enrichment analyses using GO terms

There are several ways to approach functional analyses. Here, subsets of differentially expressed genes for wich a GO term assignment is available (through Trinotate annotation of *Camptotheca*'s gene models) are tested for enrichment. Three different approaches to detecting enrichment are used:

* Enrichment of terms within a subset of DEGs determined by an FDR threshold using a hypergeometric test
* Enrichment of genes ranking highly when sorted by significance of fold-change using a Wilcoxon's sum-rank test
* Enrichment of genes ranking highly when sorted by fold-change value using a Wilcoxon's sum-rank test

To perform all tests for enrichment, the [GOfuncR package](https://www.bioconductor.org/packages/release/bioc/html/GOfuncR.html) will be used. 

### Generating a custom GO annotation table

Most packages used to test GO enrichment resource to existing annotation databases, a very handy approach when the study organism is included in the database. Unfortunately, that is usually **not** the case for most studies on non-traditional systems. Thus, before any testing can be performed, a GO annotation table specific for the reference species used must be generated and provided to the testing functions. 

Thus, the first step is building this database for *Camptotheca*'s gene models, using the Trinotate annotation table. Each gene model can have several GO terms and all GO terms are stored in a single element of the CAMac annotation table. The following code, adapted from a script by [Sarai H Stuart](https://github.com/saraihs2/GOsorting/blob/master/R/GOsorting_functions.R), extracts all terms and generates a table with a single GENEID<-GOTerm per row.

```{r}
#Extract GO blastx table from Trinotate annotation table
CAMac_annot_GO <- CAMac_annot %>% 
  select(geneID, gene_ontology_BLASTX) %>% 
  filter(!(is.na(gene_ontology_BLASTX)))

#size for preallocation of memory is the length of gene_id column because we are matching 
#gene id and it's associated GO terms
nrows <- length(CAMac_annot_GO$geneID)
#choose the GO:####### pattern retain only this pattern (remove biological process text, etc.)
pattern <- 'GO:[:digit:]{7}'
#create a variable called "myList" with which to populate a list of GO terms
myList <- list()
#make the length of "myList" that is equal to the length of "nrows"; gene id list will be equivalent in 
#length to list of GO terms for each gene id
length(myList) <- length(nrows)
#create list of the gene ids
#gene ids are not included if not identified by gene name
geneNames <- character(nrows)
#there is now an output list for all of the identified gene ids
#loop through the gene_ontology_blast to identify and retain only the GO terms 
for(i in 1:nrows){
    #move through gene id column and output names into geneNames list
  geneNames[i] <- CAMac_annot_GO$geneID[i]
  
  #move through column that contains the GO terms we want to parse output items in list
  GOblastCol <- CAMac_annot_GO$gene_ontology_BLASTX[i] 
  
  #output a vector of strings that matches the GO:####### pattern from GOblastCol
  GOtermVec <- as.vector(c(stringr::str_match_all(GOblastCol, pattern)[[1]]))
  
  #populated myList with the vector of GO terms listed for each gene
  myList[[i]] <- GOtermVec
}
names(myList) <- geneNames
#pivot named list into a tidy table
CAMac_go_annotations <- data.frame(lapply(myList, "length<-", max(lengths(myList)))) %>%
  pivot_longer(1:31068, names_to = "gene", values_to = "go_id") %>%
  filter(!(is.na(go_id)))
head(CAMac_go_annotations)
```

### Testing for term enrichment within DEG sets significant at a 0.05 FDR

This section aims to use an hypergeometric, or Fisher's exact test to identify if any GO terms within genes whose fold-change shows an adjusted *p-value* smaller than 0.05 or less are over-represented - i.e., found more frequently than expected by a random draw from the total gene pool.

We will compare the results of contrasting between stages for *Loasa heterophylla* and *Caiophora hibiscifolia*. 

```{r, message = FALSE, results = 'hide'}
#Test Loasa's and Caiophora's flower vs bud DEG set for enrichment
# Generate input dataframe: take all genes in matrix, then add a column to indicate if they are DE
input_hyper_LOA <- data.frame(CAMac_loa_cai_TMM %>% filter(geneID %in% CAMac_go_annotations$gene) %>% select(geneID))
input_hyper_LOA <- input_hyper_LOA %>% 
  mutate(is_candidate = if_else(geneID %in% sig.05.res.LOA.flo_vs_bud.tib$geneID,1,0))

input_hyper_CAI <- data.frame(CAMac_loa_cai_TMM %>% filter(geneID %in% CAMac_go_annotations$gene) %>% select(geneID))
input_hyper_CAI <- input_hyper_CAI %>% 
  mutate(is_candidate = if_else(geneID %in% sig.05.res.CAI.flo_vs_bud.tib$geneID,1,0))

#Run go_enrich, converting annotation to a dataframe as required by the function
#WARNING!!! This test takes a significant amount of time. To avoid running it every time, 
#the test result object is saved to an .RData file, so it can be re-read.
#
#IF THIS IS THE FIRST TIME RUNNING THE CODE IN THE CURRENT ENVIRONMENT, UNCOMMENT THE FOLLOWING FOUR LINES

#res_LOA.flo_vs_bud_hyper_bg <- go_enrich(input_hyper_LOA, test = "hyper", n_randsets = 1000, annotations = as.data.frame(CAMac_go_annotations))
#save(res_LOA.flo_vs_bud_hyper_bg, file = "res_LOA.flo_vs_bud_hyper_bg.Rdata")
#res_CAI.flo_vs_bud_hyper_bg <- go_enrich(input_hyper_CAI, test = "hyper", n_randsets = 1000, annotations = as.data.frame(CAMac_go_annotations))
#save(res_CAI.flo_vs_bud_hyper_bg, file = "res_CAI.flo_vs_bud_hyper_bg.Rdata")

load("res_LOA.flo_vs_bud_hyper_bg.Rdata")
write_tsv(res_LOA.flo_vs_bud_hyper_bg[[1]], "sig.05.res.LOA.flo_vs_bud.tib.GO_enrich.results.tsv")

load("res_CAI.flo_vs_bud_hyper_bg.Rdata")
write_tsv(res_CAI.flo_vs_bud_hyper_bg[[1]], "sig.05.res.CAI.flo_vs_bud.tib.GO_enrich.results.tsv")

#res_cai.flo_vs_bud_wilcox_bg_pvalue <- go_enrich(input_willi_cai, test = "wilcoxon", n_randsets = 1000, annotations = as.data.frame(CAMac_go_annotations))
#save(res_CAI.flo_vs_bud_wilcox_bg_pvalue, file = "res_CAI.flo_vs_bud_wilcox_bg_pvalue.Rdata")

```

#### Biological Process GO term enrichment
```{r}
#Generate tables comparing Biological Process terms
go_table_loa <-
res_LOA.flo_vs_bud_hyper_bg[[1]] %>% as_tibble %>%
  filter(FWER_overrep<=0.05, ontology=="biological_process") %>%
  select(-raw_p_underrep,-FWER_underrep, -ontology)

gt_go_table_loa <- gt(go_table_loa) %>%
  tab_header(
    title = "Biological Process - Enriched Terms",
    subtitle = md("*Loasa heterophylla* flower vs bud")
  )

go_table_cai <-
res_CAI.flo_vs_bud_hyper_bg[[1]] %>% as_tibble %>%
  filter(FWER_overrep<=0.05, ontology=="biological_process") %>%
  select(-raw_p_underrep,-FWER_underrep, -ontology)

gt_go_table_cai <- gt(go_table_cai) %>%
  tab_header(
    title = "Biological Process - Enriched Terms",
    subtitle = md("*Caiophora hibiscifolia* flower vs bud")
  )

gt_go_table_loa
gt_go_table_cai
```

Remember that here term enrichment is tested for the subsets of DEGs, irrespective of whether they are upregulated or downregulated.  

The odds ratio test result for each of *Loasa heterophylla*'s significantly enriched BP GO terms are plotted below. 

```{r, message = FALSE}
#Plot results for Loasa
bp_gos_hyper_LOA <- res_LOA.flo_vs_bud_hyper_bg[[1]] %>% 
  filter(ontology=="biological_process", FWER_overrep <0.05) %>%
  select(node_id)
bp_gos_hyper_LOA <- bp_gos_hyper_LOA[,'node_id']
plot_anno_scores(res_LOA.flo_vs_bud_hyper_bg, bp_gos_hyper_LOA, annotations = as.data.frame(CAMac_go_annotations))
```
The odds ratio test result for each of *Caiophora hibiscifolia*'s significantly enriched BP GO terms are plotted below.
```{r, message = FALSE}
#Plot results
bp_gos_hyper_CAI <- res_CAI.flo_vs_bud_hyper_bg[[1]] %>% 
  filter(ontology=="biological_process", FWER_overrep <0.05) %>%
  select(node_id)
bp_gos_hyper_CAI <- bp_gos_hyper_CAI[,'node_id']
plot_anno_scores(res_CAI.flo_vs_bud_hyper_bg, bp_gos_hyper_CAI, annotations = as.data.frame(CAMac_go_annotations))
```
Neither species shows many significantly enriched BP terms. Interestingly, *Caiophora* shows fewer terms than *Loasa*, despite its larger DEG repertoire.


#### Molecular Function GO term enrichment
```{r}
#Generate tables comparing Molecular Function terms
go_mf_table_loa <-
res_LOA.flo_vs_bud_hyper_bg[[1]] %>% as_tibble %>%
  filter(FWER_overrep<=0.05, ontology=="molecular_function") %>%
  select(-raw_p_underrep,-FWER_underrep, -ontology)

gt_go_mf_table_loa <- gt(go_mf_table_loa) %>%
  tab_header(
    title = "Molecular Function - Enriched Terms",
    subtitle = md("*Loasa heterophylla* flower vs bud")
  )

go_mf_table_cai <-
res_CAI.flo_vs_bud_hyper_bg[[1]] %>% as_tibble %>%
  filter(FWER_overrep<=0.05, ontology=="molecular_function") %>%
  select(-raw_p_underrep,-FWER_underrep, -ontology)

gt_go_mf_table_cai <- gt(go_mf_table_cai) %>%
  tab_header(
    title = "Molecular Function - Enriched Terms",
    subtitle = md("*Caiophora hibiscifolia* flower vs bud")
  )

gt_go_mf_table_loa
gt_go_mf_table_cai
```
The odds ratio test result for each of *Loasa heterophylla*'s significantly enriched BP GO terms are plotted below. 

```{r, message = FALSE}
#Plot results for Loasa
mf_gos_hyper_LOA <- res_LOA.flo_vs_bud_hyper_bg[[1]] %>% 
  filter(ontology=="molecular_function", FWER_overrep <0.05) %>%
  select(node_id)
mf_gos_hyper_LOA <- mf_gos_hyper_LOA[,'node_id']
plot_anno_scores(res_LOA.flo_vs_bud_hyper_bg, mf_gos_hyper_LOA, annotations = as.data.frame(CAMac_go_annotations))
```

The odds ratio test result for each of *Caiophora hibiscifolia*'s significantly enriched BP GO terms are plotted below.
```{r, message = FALSE}
#Plot results
mf_gos_hyper_CAI <- res_CAI.flo_vs_bud_hyper_bg[[1]] %>% 
  filter(ontology=="molecular_function", FWER_overrep <0.05) %>%
  select(node_id)
mf_gos_hyper_CAI <- mf_gos_hyper_CAI[,'node_id']
plot_anno_scores(res_CAI.flo_vs_bud_hyper_bg, mf_gos_hyper_CAI, annotations = as.data.frame(CAMac_go_annotations))
```

Again, neither species shows many significantly enriched MF terms. And again, *Caiophora* shows fewer terms than *Loasa*, despite its larger DEG repertoire.

#### GO term enrichment of inter-specific contrasts at the same stage

```{r, message = FALSE}
#Test inter-specific DEG set at bud stage for enrichment
# Generate input dataframe: take all genes in matrix, then add a column to indicate if they are DE
input_hyper_bud <- CAMac_loa_cai_TMM %>% 
  filter(geneID %in% CAMac_go_annotations$gene) %>% 
  select(geneID) %>%
  as.data.frame()

input_hyper_bud <- input_hyper_bud %>% 
  mutate(is_candidate = if_else(geneID %in% sig.05.res.bud.cai_vs_loa$geneID,1,0))

#Run go_enrich, converting annotation to a dataframe as required by the function
#WARNING!!! This test takes a significant amount of time. To avoid running it every time, 
#the test result object is saved to an .RData file, so it can be re-read.
#
#IF THIS IS THE FIRST TIME RUNNING THE CODE IN THE CURRENT ENVIRONMENT, UNCOMMENT THE FOLLOWING FOUR LINES

#res_bud_hyper_bg <- go_enrich(input_hyper_bud, test = "hyper", n_randsets = 1000, annotations = as.data.frame(CAMac_go_annotations))
#save(res_bud_hyper_bg, file = "res_bud_hyper_bg.Rdata")
load(file = "res_bud_hyper_bg.Rdata")
write_tsv(res_bud_hyper_bg[[1]], "sig.05.res.BUD.cai_vs_loa.tib.GO_enrich.results.tsv")
#Notice that no term is significantly enriched.

#IF THIS IS THE FIRST TIME RUNNING THE CODE IN THE CURRENT ENVIRONMENT, UNCOMMENT THE FOLLOWING TWO LINES
#res_flo_hyper_bg <- go_enrich(input_hyper_flo, test = "hyper", n_randsets = 1000, annotations = as.data.frame(CAMac_go_annotations))
#save(res_flo_hyper_bg, file = "res_flo_hyper_bg.Rdata")
load(file = "res_flo_hyper_bg.Rdata")
write_tsv(res_flo_hyper_bg[[1]], "sig.05.res.FLO.cai_vs_loa.tib.GO_enrich.results.tsv")

res_flo_hyper_bg[[1]] %>%
  filter(FWER_overrep <0.05) %>%
       select(-raw_p_underrep, -FWER_underrep)

```

No terms are enriched in the DEG set resulting from contrasting *Loasa* and *Caiophora* at bud stage. This is not surprising, given that this set is almost half of the genome. Interestingly though, a few terms are significant for the same comparison at the flower stage.

```{r, message = FALSE}
#Plot results
gos_hyper_FLO <- res_flo_hyper_bg[[1]] %>% 
  filter(FWER_overrep <0.05) %>%
  select(node_id)
gos_hyper_FLO <- gos_hyper_FLO[,'node_id']
plot_anno_scores(res_flo_hyper_bg, gos_hyper_FLO, annotations = as.data.frame(CAMac_go_annotations))
```


### Testing for term enrichment based on significance levels

The above approach uses a binary classification of genes: they are either DE or not, based on a cutoff threshold. An alternative approach is trying to identify enrichment based on a continuous variable. `GOfuncR` uses Wilcoxon's rank-sum statistic, which ranks all genes based on this variable, and then tests which terms are associated to genes that are higher or lower in the ranking than expected if they were randomly assorted. This approach can be used with any ranking variable; we first use *p*-value.

```{r}
# Generate input dataframe: extract genes and pvalues from DESeq results 
input_willi_loa <- res.LOA.flo_vs_bud %>% 
  as_tibble(rownames = 'geneID') %>% 
  filter(!(is.na(pvalue))) %>% 
  select (geneID, pvalue) %>% 
  as.data.frame()

input_willi_cai <- res.CAI.flo_vs_bud %>% 
  as_tibble(rownames = 'geneID') %>% 
  filter(!(is.na(pvalue))) %>% 
  select (geneID, pvalue) %>% 
  as.data.frame()


#Run Wilcoxon's sum-rank test
#WARNING!!! This test takes a significant amount of time. To avoid running it every time, 
#the test result object is saved to an .RData file, so it can be re-read.
#
#IF THIS IS THE FIRST TIME RUNNING THE CODE IN THE CURRENT ENVIRONMENT, UNCOMMENT THE FOLLOWING FOUR LINES

#res_LOA.flo_vs_bud_wilcox_bg_pvalue <- go_enrich(input_willi_loa, test = "wilcoxon", n_randsets = 1000, annotations = as.data.frame(CAMac_go_annotations))
#save(res_LOA.flo_vs_bud_wilcox_bg_pvalue, file = "res_LOA.flo_vs_bud_wilcox_bg_pvalue.Rdata")

#res_cai.flo_vs_bud_wilcox_bg_pvalue <- go_enrich(input_willi_cai, test = "wilcoxon", n_randsets = 1000, annotations = as.data.frame(CAMac_go_annotations))
#save(res_CAI.flo_vs_bud_wilcox_bg_pvalue, file = "res_CAI.flo_vs_bud_wilcox_bg_pvalue.Rdata")


load("res_LOA.flo_vs_bud_wilcox_bg_pvalue.Rdata")
write_tsv(res_LOA.flo_vs_bud_wilcox_bg_pvalue[[1]], "wilcox.LOA.flo_vs_bud_pvalue_GO_enrich.results.tsv")

load("res_CAI.flo_vs_bud_wilcox_bg_pvalue.Rdata")
write_tsv(res_CAI.flo_vs_bud_wilcox_bg_pvalue[[1]], "wilcox.LOA.flo_vs_bud_pvalue_GO_enrich.results.tsv")
```

#### Biological Process GO term enrichment
```{r}
#Generate tables comparing Biological Process terms
go_table_loa_w <-
res_LOA.flo_vs_bud_wilcox_bg_pvalue[[1]] %>% as_tibble %>%
  filter(FWER_low_rank<=0.05, ontology=="biological_process") %>%
  select(-raw_p_high_rank ,-FWER_high_rank, -ontology)

gt_go_table_loa_w <- gt(go_table_loa_w) %>%
  tab_header(
    title = "Biological Process - Enriched Terms",
    subtitle = md("*Loasa heterophylla* flower vs bud")
  )

go_table_cai_w <-
res_CAI.flo_vs_bud_wilcox_bg_pvalue[[1]] %>% as_tibble %>%
  filter(FWER_low_rank<=0.05, ontology=="biological_process") %>%
  select(-raw_p_high_rank ,-FWER_high_rank, -ontology)

gt_go_table_cai_w <- gt(go_table_cai_w) %>%
  tab_header(
    title = "Biological Process - Enriched Terms",
    subtitle = md("*Caiophora hibiscifolia* flower vs bud")
  )

gt_go_table_loa_w
gt_go_table_cai_w

```

The results obtained using this method are quite similar to those based on a threshold, which is reassuring. 

This time, plotting enriched terms shows violin plots with the distribution of rank-scores. Since more significant *p*-values are smaller, enriched terms have a median (white dot) well below the median score for the BP root node.

Below are the plots for *Loasa heterophylla*.

```{r, message = FALSE}
#Plot results
bp_gos_wilp_LOA <- res_LOA.flo_vs_bud_wilcox_bg_pvalue[[1]] %>% 
  filter(ontology=="biological_process", FWER_low_rank <0.05) %>%
  select(node_id)
bp_gos_wilp_LOA <- bp_gos_wilp_LOA[,'node_id']
plot_anno_scores(res_LOA.flo_vs_bud_wilcox_bg_pvalue, bp_gos_wilp_LOA, annotations = as.data.frame(CAMac_go_annotations))
```

And the plots for *Caiophora hibiscifolia*.

```{r, message = FALSE}
#Plot results
bp_gos_wilp_CAI <- res_CAI.flo_vs_bud_wilcox_bg_pvalue[[1]] %>% 
  filter(ontology=="biological_process", FWER_low_rank <0.05) %>%
  select(node_id)
bp_gos_wilp_CAI <- bp_gos_wilp_CAI[,'node_id']
plot_anno_scores(res_CAI.flo_vs_bud_wilcox_bg_pvalue, bp_gos_wilp_CAI, annotations = as.data.frame(CAMac_go_annotations))
```

#### Molecular Function GO term enrichment

```{r}
#Generate tables comparing Molecular Function terms
go_mf_table_loa_w <-
res_LOA.flo_vs_bud_wilcox_bg_pvalue[[1]] %>% as_tibble %>%
  filter(FWER_low_rank<=0.05, ontology=="molecular_function") %>%
  select(-raw_p_high_rank ,-FWER_high_rank, -ontology)

gt_go_mf_table_loa_w <- gt(go_mf_table_loa_w) %>%
  tab_header(
    title = "Molecular Function - Enriched Terms",
    subtitle = md("*Loasa heterophylla* flower vs bud")
  )

go_mf_table_cai_w <-
res_CAI.flo_vs_bud_wilcox_bg_pvalue[[1]] %>% as_tibble %>%
  filter(FWER_low_rank<=0.05, ontology=="molecular_function") %>%
  select(-raw_p_high_rank ,-FWER_high_rank, -ontology)

gt_go_mf_table_cai_w <- gt(go_mf_table_cai_w) %>%
  tab_header(
    title = "Molecular Function - Enriched Terms",
    subtitle = md("*Caiophora hibiscifolia* flower vs bud")
  )

gt_go_mf_table_loa_w
gt_go_mf_table_cai_w

```

Below  are the plots for *Loasa heterophylla*.

```{r, message = FALSE}
#Plot results
mf_gos_wilp_LOA <- res_LOA.flo_vs_bud_wilcox_bg_pvalue[[1]] %>% 
  filter(ontology=="biological_process", FWER_low_rank <0.05) %>%
  select(node_id)
mf_gos_wilp_LOA <- mf_gos_wilp_LOA[,'node_id']
plot_anno_scores(res_LOA.flo_vs_bud_wilcox_bg_pvalue, mf_gos_wilp_LOA, annotations = as.data.frame(CAMac_go_annotations))
```

And the plots for *Caiophora hibiscifolia*.

```{r, message = FALSE}
#Plot results
mf_gos_wilp_CAI <- res_CAI.flo_vs_bud_wilcox_bg_pvalue[[1]] %>% 
  filter(ontology=="biological_process", FWER_low_rank <0.05) %>%
  select(node_id)
mf_gos_wilp_CAI <- mf_gos_wilp_CAI[,'node_id']
plot_anno_scores(res_CAI.flo_vs_bud_wilcox_bg_pvalue, mf_gos_wilp_CAI, annotations = as.data.frame(CAMac_go_annotations))
```



### Testing for term enrichment based on fold change

Instead of using *p*-value as a ranking variable, it is also possible to test enrichment using log2 fold-change (l2fc). This is a little different since now significant terms might be either at the top (upregulated) or bottom (downregulated) of the rank. While this seems more informative since it could show whether processes are turned on or off, it is important to remember that often times process regulation involves multiple genes, some turning on while others turn off. Thus, using l2fc as ranking variable could obscure, rather than clarify functional implications of differential gene expression.

```{r, message=FALSE}
# Generate input dataframe: extract genes and pvalues from DESeq results 
input_willi_loa_fc <- res.LOA.flo_vs_bud %>% 
  as_tibble(rownames = 'geneID') %>% 
  filter(!(is.na(log2FoldChange))) %>% 
  select (geneID, log2FoldChange) %>% 
  as.data.frame()

input_willi_cai_fc <- res.CAI.flo_vs_bud %>% 
  as_tibble(rownames = 'geneID') %>% 
  filter(!(is.na(log2FoldChange))) %>% 
  select (geneID, log2FoldChange) %>% 
  as.data.frame()

#Run Wilcoxon's sum-rank test
#WARNING!!! This test takes a significant amount of time. To avoid running it every time, 
#the test result object is saved to an .RData file, so it can be re-read.
#
#IF THIS IS THE FIRST TIME RUNNING THE CODE IN THE CURRENT ENVIRONMENT, UNCOMMENT THE FOLLOWING FOUR LINES

#res_LOA.flo_vs_bud_wilcox_bg_l2fc <- go_enrich(input_willi_loa_fc, test = "wilcoxon", n_randsets = 1000, annotations = as.data.frame(CAMac_go_annotations))
#save(res_LOA.flo_vs_bud_wilcox_bg_l2fc, file = "res_LOA.flo_vs_bud_wilcox_bg_l2fc.Rdata")

#res_cai.flo_vs_bud_wilcox_bg_l2fc <- go_enrich(input_willi_cai_fc, test = "wilcoxon", n_randsets = 1000, annotations = as.data.frame(CAMac_go_annotations))
#save(res_cai.flo_vs_bud_wilcox_bg_l2fc, file = "res_CAI.flo_vs_bud_wilcox_bg_l2fc.Rdata")


load("res_LOA.flo_vs_bud_wilcox_bg_l2fc.Rdata")
write_tsv(res_LOA.flo_vs_bud_wilcox_bg_l2fc[[1]], "wilcox.LOA.flo_vs_bud_l2fc_GO_enrich.results.tsv")

load("res_CAI.flo_vs_bud_wilcox_bg_l2fc.Rdata")
write_tsv(res_cai.flo_vs_bud_wilcox_bg_l2fc[[1]], "wilcox.LOA.flo_vs_bud_pvalue_GO_enrich.results.tsv")

#Generate tables comparing Biological Process terms
go_table_loa_w_fc <-
res_LOA.flo_vs_bud_wilcox_bg_l2fc[[1]] %>% as_tibble %>%
  filter(FWER_low_rank<=0.05 | FWER_high_rank<=0.05, ontology=="biological_process") %>%
  select(-ontology,-raw_p_low_rank, -raw_p_high_rank)

gt_go_table_loa_w_fc <- gt(rbind(head(go_table_loa_w_fc,20),tail(go_table_loa_w_fc,20))) %>%
  tab_header(
    title = "Biological Process -  Top and Bottom Enriched Terms",
    subtitle = md("*Loasa heterophylla* flower vs bud")
  )

go_table_cai_w_fc <-
res_cai.flo_vs_bud_wilcox_bg_l2fc[[1]] %>% as_tibble %>%
  filter(FWER_low_rank<=0.05 | FWER_high_rank<=0.05, ontology=="biological_process") %>%
  select(-ontology,-raw_p_low_rank, -raw_p_high_rank)

gt_go_table_cai_w_fc <- gt(rbind(head(go_table_cai_w_fc,20),tail(go_table_cai_w_fc,20))) %>%
  tab_header(
    title = "Biological Process - Top and Bottom 20 Enriched Terms",
    subtitle = md("*Caiophora hibiscifolia* flower vs bud")
  )

gt_go_table_loa_w_fc
gt_go_table_cai_w_fc

```

This approach yields many, many more enriched go terms (`r dim(go_table_loa_w_fc)[1]`) for *Loasa* and (`r dim(go_table_cai_w_fc)[1]`) for *Caiophora*). This is because we are no longer requiring genes to pass a significance test before being eligible; instead, we are ranking and selecting them just based on the average effect size between stages. 


```{r, message = FALSE, warning = FALSE}

#Plot results - Top 5 up and downregulated genes
bp_gos_willi_LOA_5up5down <- c(head(go_table_loa_w_fc, 5)$node_id, tail(go_table_loa_w_fc, 5)$node_id)

rbind(head(go_table_loa_w_fc, 5),tail(go_table_loa_w_fc, 5)) %>%
  gt() %>%
  tab_header(
    title = "Biological Process - Enriched Terms",
    subtitle = md("*Loasa heterophylla* flower vs bud")
  )  
plot_anno_scores(res_LOA.flo_vs_bud_wilcox_bg_l2fc, bp_gos_willi_LOA_5up5down, annotations = as.data.frame(CAMac_go_annotations))

```

```{r, message = FALSE, warning = FALSE}

#Plot results - Top 5 up and downregulated genes
bp_gos_willi_CAI_5up5down <- c(head(go_table_cai_w_fc, 5)$node_id, tail(go_table_cai_w_fc, 5)$node_id)

rbind(head(go_table_cai_w_fc, 5),tail(go_table_cai_w_fc, 5)) %>% 
  gt() %>%
  tab_header(
    title = "Biological Process - Enriched Terms",
    subtitle = md("*Caiophora hibiscifolia* flower vs bud")
  )  
plot_anno_scores(res_cai.flo_vs_bud_wilcox_bg_l2fc, bp_gos_willi_CAI_5up5down, annotations = as.data.frame(CAMac_go_annotations))

```


The terms found using the log2fc analysis are quite different from the previous analysis. This is due to the presence of large average fold changes in genes with very large intra-group variance that are not filtered out by significance testing.


#Gene Set Enrichment Analyses

```{r}

#I generate two signatures related in the literature to cell wall lobeyness: "turgor pressure-cell wall interaction"
#and "intrinsic cell wall properties".

#I also generate a signature of genes that are related to cell ellongation

#and a signature that contains genes that are typically related to flower
#morphogenesis

#Protein names that correspond to genes in each
#signature were checked in uniprot.org

#Cell wall lobeyness proteins----

CAMac_trinotate_annot2 <- read_tsv("CAMac_trinotate_annotation_report.tsv", na = ".",) %>%
select(geneID,sprot_Top_BLASTX_hit)

kin<-grep("Kinesin-like protein|KIN", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
kin.d<-CAMac_trinotate_annot2[kin,]

rac<-grep("Rac-like GTP-binding protein|RAC", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
rac.d<-CAMac_trinotate_annot2[rac,]

act<-grep("Actin-related protein|ARP", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
act.d<-CAMac_trinotate_annot2[act,]

gaut<-grep("galacturonosyltransferase|GAUT", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
gaut.d<-CAMac_trinotate_annot2[gaut,]

pme<-grep("Pectinesterase|PME", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
pme.d<-CAMac_trinotate_annot2[pme,]

pmei<-grep("Pectinesterase inhibitor|PMEI", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
pmei.d<-CAMac_trinotate_annot2[pmei,]

rho<-grep("Rho of plants|ROP", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
rho.d<-CAMac_trinotate_annot2[rho,]

crib<-grep("CRIB domain-containing protein|RIC", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
crib.d<-CAMac_trinotate_annot2[crib,]

ABP1<-grep("Auxin-binding protein 1|ABP1", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
ABP1.d<-CAMac_trinotate_annot2[ABP1,]

PIN<-grep("Auxin efflux carrier component 1|PIN", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
PIN.d<-CAMac_trinotate_annot2[PIN,]

CESA<-grep("Cellulose synthase|CESA", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
CESA.d<-CAMac_trinotate_annot2[CESA,]

CLIP<-grep("CLIP-associated protein|CLASP", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
CLIP.d<-CAMac_trinotate_annot2[CLIP,]

#Cell elongation proteins----

RGA<-grep("DELLA protein RGA|RGA", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
RGA.d<-CAMac_trinotate_annot2[RGA,]

RGL<-grep("DELLA protein RGL|RGL", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
RGL.d<-CAMac_trinotate_annot2[RGL,]

PIP<-grep("Aquaporin PIP|PIP", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
PIP.d<-CAMac_trinotate_annot2[PIP,]

TIP<-grep("Aquaporin TIP|TIP", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
TIP.d<-CAMac_trinotate_annot2[TIP,]

NIP<-grep("Aquaporin NIP|NIP", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
NIP.d<-CAMac_trinotate_annot2[NIP,]

GASA<-grep("Gibberellin-regulated protein|GASA", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
GASA.d<-CAMac_trinotate_annot2[GASA,]


#Flower transcription factors----

BLH9<-grep("BEL1-like homeodomain protein|BLH9", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
BLH9.d<-CAMac_trinotate_annot2[BLH9,]

JAG<-grep("Zinc finger protein JAGGED|JAG", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
JAG.d<-CAMac_trinotate_annot2[JAG,]

ETTIN<-grep("Auxin response factor|ETTIN", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
ETTIN.d<-CAMac_trinotate_annot2[ETTIN,]

MADS<-grep("MADS-box protein", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
MADS.d<-CAMac_trinotate_annot2[MADS,]

TCPs<-grep("Transcription factor TCP|TCP", CAMac_trinotate_annot2$sprot_Top_BLASTX_hit)
TCPs.d<-CAMac_trinotate_annot2[TCPs,]

#Generation of signatures for GSEA ----


#This a signature were cell wall lobeyness is not related
#to intrinsic cell wall properties but to an interplay 
#between cell wall lobeyness and turgor pressure

data.GSEA.ROP.RIP.Aux.MT<-rbind(kin.d, rac.d, act.d, rho.d, crib.d, ABP1.d, PIN.d, CESA.d,
                                CLIP.d)

#This a signature were cell wall lobeyness related
#to intrinsic cell wall properties

data.GSEA.Pectin<-rbind(gaut.d, pme.d, pmei.d)

#This a signature corresponds to proteins related to cell elongation

data.GSEA.Elongation<-rbind(RGA.d, RGL.d, PIP.d, TIP.d, NIP.d, GASA.d)

#This a signature corresponds to flower transcription factors

data.GSEA.TF.Flower<-rbind(BLH9.d, JAG.d, ETTIN.d, MADS.d, TCPs.d)


geneID.GSEA.ROP.RIP.Aux.MT<-data.GSEA.ROP.RIP.Aux.MT$geneID
geneID.GSEA.ROP.RIP.Aux.MT<-unique(geneID.GSEA.ROP.RIP.Aux.MT)

geneID.GSEA.Pectin<-data.GSEA.Pectin$geneID
geneID.GSEA.Pectin<-unique(geneID.GSEA.Pectin)

geneID.GSEA.Elongation<-data.GSEA.Elongation$geneID
geneID.GSEA.Elongation<-unique(geneID.GSEA.Elongation)

geneID.GSEA.TF.Flower<-data.GSEA.TF.Flower$geneID
geneID.GSEA.TF.Flower<-unique(geneID.GSEA.TF.Flower)


#GSEA----


#1)you have to count on a variable that allows the ranking of DEGs
#Only genes for which DGE p< 0.05.are used in this ranking,
#that is based on logfold change. 

#Loasa flo vs. bud
sig.05.res.LOA.flo_vs_bud.tib
para.GSEA.Loa <- dplyr::select(sig.05.res.LOA.flo_vs_bud.tib, geneID, log2FoldChange)

# construct a named vector
para.GSEA.Loa.v <- para.GSEA.Loa$log2FoldChange
names(para.GSEA.Loa.v) <- as.character(para.GSEA.Loa$geneID)
para.GSEA.Loa.v <- sort(para.GSEA.Loa.v, decreasing = TRUE)


#Caiophora flo vs bud
sig.05.res.CAI.flo_vs_bud.tib
para.GSEA.Cai <- dplyr::select(sig.05.res.CAI.flo_vs_bud.tib, geneID, log2FoldChange)

# construct a named vector
para.GSEA.Cai.v <- para.GSEA.Cai$log2FoldChange
names(para.GSEA.Cai.v) <- as.character(para.GSEA.Cai$geneID)
para.GSEA.Cai.v <- sort(para.GSEA.Cai.v, decreasing = TRUE)

#Between flowers
sig.05.res.flo.cai_vs_loa
para.GSEA.Flor <- dplyr::select(sig.05.res.flo.cai_vs_loa, geneID, log2FoldChange)

# construct a named vector
para.GSEA.Flor.v <- para.GSEA.Flor$log2FoldChange
names(para.GSEA.Flor.v) <- as.character(para.GSEA.Flor$geneID)
para.GSEA.Flor.v <- sort(para.GSEA.Flor.v, decreasing = TRUE)


#Between buds
sig.05.res.bud.cai_vs_loa
para.GSEA.Bot <- dplyr::select(sig.05.res.bud.cai_vs_loa, geneID, log2FoldChange)

# construct a named vector
para.GSEA.Bot.v <- para.GSEA.Bot$log2FoldChange
names(para.GSEA.Bot.v) <- as.character(para.GSEA.Bot$geneID)
para.GSEA.Bot.v <- sort(para.GSEA.Bot.v, decreasing = TRUE)

#2)collections of signals 


#cell wall properties-turgor pressure interplat
gs_TCWI<-rep("TCWI", length(geneID.GSEA.ROP.RIP.Aux.MT))
#intrinsic cell wall properties
gs_ICWP<-rep("ICWP", length(geneID.GSEA.Pectin))
#cell elongation
gs_CE<-rep("CE", length(geneID.GSEA.Elongation))
#Flower transcription factors
gs_F.TF<-rep("F.TF", length(geneID.GSEA.TF.Flower))

gs_name<-c(gs_TCWI, gs_ICWP, gs_CE, gs_F.TF)
geneID<-c(geneID.GSEA.ROP.RIP.Aux.MT,geneID.GSEA.Pectin, geneID.GSEA.Elongation, geneID.GSEA.TF.Flower)
gs_TCWI_ICWP_CE_F.TF<-data.frame(gs_name,geneID)
names(gs_TCWI_ICWP_CE_F.TF)<-c("gs_name", "geneID")


#I do a first trial to check whether the comming script is working well
#using the first 43 genes in the list of genes rankes based on logfold
#change

#gs_top<-para.GSEA.Loa.v[1:43]
#gs_top<-names(gs_top)
#gs_top.enr<-rep("top.43.enr", 43)
#gs_top<-data.frame(gs_top.enr, gs_top)
#names(gs_top)<-c("gs_name", "geneID")

#3) Analysis

# Now that you have your msigdb collections ready, prepare your data
# grab the dataframe you made in step3 script
# Pull out just the columns corresponding to gene symbols and LogFC for at least one pairwise comparison for the enrichment analysis


# run GSEA using the 'GSEA' function from clusterProfiler

#myGSEA.prueba <- GSEA(para.GSEA.Loa.v, TERM2GENE=gs_top, verbose=FALSE, eps=0)
#myGSEA.prueba.df <- as_tibble(myGSEA.prueba@result)

#SCRIPT WORKS!

#Now I run GSEA with the four signatures I created in the previous
#steps

myGSEA.resL <- GSEA(para.GSEA.Loa.v, TERM2GENE=
                      gs_TCWI_ICWP_CE_F.TF, verbose=FALSE)


myGSEA.resC <- GSEA(para.GSEA.Cai.v, TERM2GENE=
                      gs_TCWI_ICWP_CE_F.TF, verbose=FALSE)


myGSEA.resFlor <- GSEA(para.GSEA.Flor.v, TERM2GENE=
                          gs_TCWI_ICWP_CE_F.TF, verbose=FALSE)


myGSEA.resBot <- GSEA(para.GSEA.Bot.v, TERM2GENE=
                         gs_TCWI_ICWP_CE_F.TF, verbose=FALSE)


#GSEA calculates a sum statistic for each signature. It also
#permutes rows (genes) and calculates a null distribution of the
#enrichment score of each signature. Based on that distribution
#and on the observed enrichment value it is possible to calculate
#a p-value of the enrichment score.


#myGSEA.df <- as_tibble(myGSEA.prueba@result)
myGSEA.resC.df <- as_tibble(myGSEA.resC@result)

# create enrichment plots using the enrichplot package

#pdf(file="GSEA Caiophora3.pdf")

gseaplot2(myGSEA.resC, 
          geneSetID = c(1,2), #can choose multiple signatures to overlay in this plot. Son las filas del objeto
          pvalue_table = FALSE, #can set this to FALSE for a cleaner plot
          title = "Enrichment of lobeyness genes in C. hibiscifolia flowers") #can also turn off this title

#dev.off()
```
#Quadratic regressions of cell wall lobeyness, cell area and cell elongation 

AGAINST POSITION ALONG THE PETAL MIDRIB

```{r}
library(graphics)

data=read.table("cell data.txt",header=T)

names(data)

data$Stage<-as.factor(data$Stage)
data$Sp<-as.factor(data$Sp)
data$Lobeyness<-1-data$Solidity
data$Elongation<-data$Long_vert/data$Long_hor


#the following lines are just to standardize the position of the
#SEM image along the petal midrib between 0 (basal) and 1 (apical)


#s1 = 5mm bud 
#s3 = mature flower

#l = L. heterophylla
#h = C.hibiscifolia

s1<-subset(data, Stage==1)
s1.l<-subset(s1, Sp=="loa")
s1.l$Foto.new <- (s1.l$Foto - min(s1.l$Foto)) / (max(s1.l$Foto)-min(s1.l$Foto))
s1.l$Foto.new <- 1- s1.l$Foto.new 
s1.l$Foto.cuad<-s1.l$Foto.new^2

s1.h<-subset(s1, Sp=="hib")
s1.h$Foto.new <- (s1.h$Foto - min(s1.h$Foto)) / (max(s1.h$Foto)-min(s1.h$Foto))
s1.h$Foto.new <- 1- s1.h$Foto.new
s1.h$Foto.cuad<-s1.h$Foto.new^2

s1<-rbind.data.frame(s1.l, s1.h)

s3<-subset(data, Stage==3)
s3.l<-subset(s3, Sp=="loa")
s3.l$Foto.new <- (s3.l$Foto - min(s3.l$Foto)) / (max(s3.l$Foto)-min(s3.l$Foto))
s3.l$Foto.new <- 1- s3.l$Foto.new
s3.l$Foto.cuad<-s3.l$Foto.new^2

s3.h<-subset(s3, Sp=="hib")
s3.h$Foto<-s3.h$Foto/max(s3.h$Foto)
s3.h$Foto.new <- (s3.h$Foto - min(s3.h$Foto)) / (max(s3.h$Foto)-min(s3.h$Foto))
s3.h$Foto.new <- 1- s3.h$Foto.new
s3.h$Foto.cuad<-s3.h$Foto.new^2

s3<-rbind.data.frame(s3.l, s3.h)

all<-rbind(s1, s3)


#Regressions----

#Cell lobeyness

all$Sp<-factor(all$Sp, levels=c("loa", "hib"))

Lobeyness.mod <-lm(Lobeyness ~ Foto.new*Sp + Foto.cuad*Sp + Foto.new*Stage +  Foto.cuad*Stage + Foto.new*Sp*Stage + Foto.cuad*Sp*Stage,data=all)
summary(Lobeyness.mod)

#log(Area)

LogArea.mod <-lm(log(Area) ~ Foto.new*Sp + Foto.cuad*Sp + Foto.new*Stage + Foto.cuad*Stage + Foto.new*Sp*Stage + Foto.cuad*Sp*Stage,data=all)
summary(LogArea.mod)

#Elongation

Elong.mod <-lm(Elongation ~ Foto.new*Sp + Foto.cuad*Sp + Foto.new*Stage + Foto.cuad*Stage + Foto.new*Sp*Stage + Foto.cuad*Sp*Stage,data=all)
summary(Elong.mod)

#Plots----

#pdf("Cell lobeyness cuadratic regr2.pdf", height = 3.3, width=9)

par(mfrow=c(1,3))

#development of L. heterophylla

all$StageSp<- paste(all$Stage, all$Sp)
all$StageSp<- factor(all$StageSp, levels =c("1 loa", "3 loa", "1 hib", "3 hib"))

palette(c("cyan", "blue", "orange", "red"))


plot(Lobeyness~Foto.new, col=StageSp, ylab= "CL", xlab="PAMR", xlim=c(0,1), ylim=c(0,0.5), data= all)
#het 1
curve(0.095249 + x*0.346359 + x^2*-0.242785, from=min(all$Foto.new), to=max(all$Foto.new), add = TRUE, col= "cyan")
#het3
curve(0.095249 + 0.018227 + x*(0.346359-0.013954) + x^2*(-0.242785+0.048367), from=min(all$Foto.new), to=max(all$Foto.new), add = TRUE, col="blue")
#hib 1
curve(0.095249 -0.074549  + x*(0.346359 -0.366550 ) + x^2*(-0.242785+0.277844), from=min(all$Foto.new), to=max(all$Foto.new), add = TRUE, col= "orange")
#hib3
curve(0.095249 + 0.018227 -0.074549  + 0.003484  + x*(0.346359-0.013954-0.366550+ 0.473564) + x^2*(-0.242785+0.048367+0.277844-0.400959), from=min(all$Foto.new), to=max(all$Foto.new), add = TRUE, col="red")


legend(0.0, 0.5, legend=c("L. heterophylla bud", "L. heterophylla flower",
                          "C. hibiscifolia bud", "C. hibiscifolia flower"),
       col=c("cyan", "blue", "orange", "red"), lty=1, cex=0.8)

plot(log(Area)~Foto.new, col=StageSp, ylab= "log(CA)", xlab="PAMR", xlim=c(0,1), ylim=c(min(log(Area)),max(log(Area))), data= all)
#het 1
curve(7.41242 + x*-2.58848 + x^2*1.62690, from=min(all$Foto.new), to=max(all$Foto.new), add = TRUE, col= "cyan")
#het3
curve(7.41242 + 0.49630 + x*(-2.58848+0.18261) + x^2*(1.62690+0.32581), from=min(all$Foto.new), to=max(all$Foto.new), add = TRUE, col="blue")
#hib 1
curve(7.41242 -1.23556  + x*(-2.58848+1.82739) + x^2*(1.62690-0.48491), from=min(all$Foto.new), to=max(all$Foto.new), add = TRUE, col= "orange")
#hib3
curve(7.41242 + 0.49630-1.23556 +0.44091 + x*(-2.58848+1.82739+0.18261+2.60732 ) + x^2*(1.62690+0.32581-0.48491-3.01997), from=min(all$Foto.new), to=max(all$Foto.new), add = TRUE, col="red")


plot(Elongation~Foto.new, col=StageSp, ylab= "CLWR", xlab="PAMR", xlim=c(0,1), ylim=c(min(Elongation),max(Elongation)), data= all)
#het 1
curve(5.3393 + x*-6.2680 + x^2* 2.4565 , from=min(all$Foto.new), to=max(all$Foto.new), add = TRUE, col= "cyan")
#het3
curve(5.3393  + 2.5412 + x*(-6.2680-9.0696) + x^2*(2.4565+6.6951), from=min(all$Foto.new), to=max(all$Foto.new), add = TRUE, col="blue")
#hib 1
curve(5.3393  -2.1636  + x*(-6.2680+2.4237) + x^2*(2.4565-0.3625), from=min(all$Foto.new), to=max(all$Foto.new), add = TRUE, col= "orange")
#hib3
curve(5.3393 -2.1636  + 2.5412 -3.7264  + x*(-6.2680 -9.0696 +2.4237 + 14.5251  ) 
      + x^2*(2.4565 +6.6951-0.3625-10.8733), from=min(all$Foto.new), to=max(all$Foto.new), add = TRUE, col="red")


#dev.off()
```





