---
title: "Inference and Analysis of Synteny Networks"
author: 
  - name: Fabricio Almeida-Silva
    affiliation: 
    - VIB-UGent Center for Plant Systems Biology, Ghent, Belgium
    - Department of Plant Biotechnology and Bioinformatics, Ghent University, Ghent, Belgium
  - name: Tao Zhao
    affiliation:
    - State Key Laboratory of Crop Stress Biology for Arid Areas/Shaanxi Key Laboratory of Apple, College of Horticulture, Northwest A&F University, Yangling, China
  - name: Kristian K Ullrich
    affiliation:
    - Department of Evolutionary Biology, Max Planck Institute For Evolutionary Biology, Ploen, Germany
  - name: Yves Van de Peer
    affiliation: 
    - VIB-UGent Center for Plant Systems Biology, Ghent, Belgium
    - Department of Plant Biotechnology and Bioinformatics, Ghent University, Ghent, Belgium
    - College of Horticulture, Academy for Advanced Interdisciplinary Studies, Nanjing Agricultural University, Nanjing, China
    - Center for Microbial Ecology and Genomics, Department of Biochemistry, Genetics and Microbiology, University of Pretoria, Pretoria, South Africa
output: 
  BiocStyle::html_document:
    toc: true
    toc_float: true
    number_sections: yes
bibliography: vignette_bibliography.bib
vignette: >
  %\VignetteIndexEntry{Inference and Analysis of Synteny Networks}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}  ---
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>",
    crop = NULL ## Related to https://stat.ethz.ch/pipermail/bioc-devel/2020-April/016656.html
)
```

# Introduction

The analysis of synteny (i.e., conserved gene content and order in a genomic
segment across species) can help understand the trajectory of duplicated 
genes through evolution. In particular, synteny analyses are widely used to
investigate the evolution of genes derived from whole-genome duplication (WGD)
events, as they can reveal genomic rearrangements that happened following the
duplication of all chromosomes. However, synteny analysis are typically
performed in a pairwise manner, which can be difficult to explore and interpret
when comparing several species. To understand global patterns of synteny,
@zhao2017network proposed a network-based approach to analyze synteny. 
In synteny networks, genes in a given syntenic block are represented as nodes
connected by an edge. Synteny networks have been used to explore, among others,
global synteny patterns in mammalian and angiosperm genomes [@zhao2019network], 
the evolution of MADS-box transcription factors [@zhao2017phylogenomic], 
and infer a microsynteny-based phylogeny for angiosperms [@zhao2021whole].
`r BiocStyle::Biocpkg("syntenet")` is a package that 
can be used to infer synteny networks from
protein sequences and perform downstream network analyses that include:

- **Network clustering** using the Infomap algorithm;

- **Phylogenomic profiling**, which consists in identifying which species 
contain which clusters. This analysis can reveal highly conserved synteny 
clusters and taxon-specific ones (e.g., family- and order-specific clusters);

- **Microsynteny-based phylogeny reconstruction** with maximum likelihood, 
which can be achieved by inferring a phylogeny from a binary matrix of 
phylogenomic profiles with IQTREE2.


# Installation

`r BiocStyle::Biocpkg("syntenet")` can be installed from Bioconductor
with the following code:

```{r installation, eval=FALSE}
if(!requireNamespace('BiocManager', quietly = TRUE))
  install.packages('BiocManager')

BiocManager::install("syntenet")
```

```{r load_package, message=FALSE}
# Load package after installation
library(syntenet)
```

# Data description

For this vignette, we will use the proteomes and gene annotation of the algae 
species *Ostreococcus lucimarinus* and *Ostreococcus sp RCC809*, which were 
obtained from Pico-PLAZA 3.0 [@vandepoele2013pico]. 

```{r data}
# Protein sequences
data(proteomes)
head(proteomes)

# Annotation (ranges)
data(annotation)
head(annotation)
```

# Importing data to the R session

To detect synteny and infer synteny networks, 
`r BiocStyle::Biocpkg("syntenet")` requires two objects
as input:

- **seq:** A list of `AAStringSet` objects containing the translated sequences
of primary transcripts for each species.
- **annotation:** A `GRangesList` or `CompressedGRangesList` object containing
the coordinates for the genes in **seq**.


If you have whole-genome protein sequences in FASTA files, store all FASTA
files in the same directory and use the function `fasta2AAStringSetlist()` to
read all FASTA files into a list of `AAStringSet` objects.

Likewise, if you have gene annotation in GFF/GFF3/GTF files, 
store all files in the same directory and use the function `gff2GRangesList()`
to read all GFF/GFF3/GTF files into a `GRangesList object`.

For a demonstration, we will read example FASTA and GFF3 files stored in 
subdirectories named **sequences/** and **annotation/**, which are located 
in the `extdata/` directory of this package.

## From FASTA files to a list of `AAStringSet` objects

Here is how you can use `fasta2AAStringSetlist()` to read FASTA files 
in a directory as a list of `AAStringSet` objects.

```{r fasta2AAStringSetlist}
# Path to directory containing FASTA files
fasta_dir <- system.file("extdata", "sequences", package = "syntenet")
fasta_dir

dir(fasta_dir) # see the contents of the directory

# Read all FASTA files in `fasta_dir`
aastringsetlist <- fasta2AAStringSetlist(fasta_dir)
aastringsetlist
```

And that's it! Now you have a list of `AAStringSet` objects.

## From GFF/GTF files to a `GRangesList` object

Here is how you can use `gff2GRangesList()` to read GFF/GFF3/GTF files 
in a directory as a `GRangesList` object.

```{r gff2GRangesList}
# Path to directory containing FASTA files
gff_dir <- system.file("extdata", "annotation", package = "syntenet")
gff_dir

dir(gff_dir) # see the contents of the directory

# Read all FASTA files in `fasta_dir`
grangeslist <- gff2GRangesList(gff_dir)
grangeslist
```

And now you have a `GRangesList` object.

# Data preprocessing

The first part of the pipeline consists in processing the data to make it
match a standard structure. However, before processing the data for synteny 
detection, you must use the function `check_input()` to check if your data can
enter the pipeline. This function checks the input data for 3 
required conditions:

1. Names of **seq** list (i.e., `names(seq)`) match 
the names of **annotation** `GRangesList`/`CompressedGRangesList`
(i.e., `names(annotation)`)

2. For each species (list elements), the number of sequences
in **seq** is not greater than the number of genes 
in **annotation**. This is a way to ensure users do not input
the translated sequences for multiple isoforms of the same gene (generated
by alternative splicing). Ideally, the number of sequences in **seq** 
should be equal to the number of genes in **annotation**, but
this may not always stand true because of non-protein-coding genes.

3. For each species, sequence names (i.e., `names(seq[[x]])`,
equivalent to FASTA headers) match gene names in `annotation`.

Let's check if the example data sets satisfy these 3 criteria:

```{r check_input}
check_input(proteomes, annotation)
```

As you can see, the data passed the checks. Now, let's process them 
with the function `process_input()`.

This function processes the input sequences and annotation to:

1. Remove whitespace and anything after it in sequence names 
(i.e., `names(seq[[x]])`, which is equivalent to FASTA headers), if
there is any.

2. Remove period followed by number at the end of sequence names, which
typically indicates different isoforms of the same gene 
(e.g., Arabidopsis thaliana's transcript AT1G01010.1, which belongs to
gene AT1G01010).

3. Add a unique species identifier to sequence names. The species 
identifier consists of the first 3-5 strings of the element name.
For instance, if the first element of the **seq** list is named
"Athaliana", each sequence in it will have an identifier "Atha_" added
to the beginning of each gene name (e.g., Atha_AT1G01010).

4. If sequences have an asterisk (*) representing stop codon, remove it.

5. Add a unique species identifier (same as above) to 
gene and chromosome names of each element of the **annotation**
`GRangesList`/`CompressedGRangesList`.

6. Filter each element of the **annotation** 
`GRangesList`/`CompressedGRangesList` to keep only seqnames, 
ranges, and gene ID.


Let's process our input data:

```{r process_input}
pdata <- process_input(proteomes, annotation)

# Looking at the processed data
pdata$seq
pdata$annotation
```

# Synteny network inference

Now that we have our processed data, we can infer the synteny network.
To detect synteny, we need the tabular output from BLASTp [@altschul1997gapped]
or similar programs. To get that, you can use the function `run_diamond()`, 
which runs DIAMOND [@buchfink2021sensitive] from the R session and
automatically parses its output to a list of data frames [^1].

[^1]: **Alternative:** if you want to use a different program for similarity
searches, you can run it on the command line, save the output in tabular
format, and read the tabular output as a data frame (e.g., with the base
R function `read.table`). 


Let's demonstrate how `run_diamond()` works. 
Needless to say, you need to have DIAMOND installed in your machine 
and in your PATH to run this function. To check if you have DIAMOND installed,
use the function `diamond_is_installed()` [^2].

[^2]: **Note:** in the code chunk below, the if statement is not required.
We just added it to make sure that the function `run_diamond()` is only
executed if DIAMOND is installed, to avoid problems when building this
vignette in machines that do not have DIAMOND installed. If you want to
reproduce the code in this vignette and do not have DIAMOND installed,
you can use the example output of `run_diamond()` stored in the *blast_list*
object (loaded with `data(blast_list)`).

```{r run_diamond}
data(blast_list)
if(diamond_is_installed()) {
    blast_list <- run_diamond(seq = pdata$seq)
}
```

The output of `run_diamond()` is a list of data frames containing the tabular
output of all-vs-all DIAMOND searches. Let's take a look at it.

```{r blast_inspect}
# List names
names(blast_list)

# Inspect first data frame
head(blast_list$Olucimarinus_Olucimarinus)
```

Now, we can use this list of DIAMOND data frames to detect synteny. Here,
we reimplemented the popular MCScanX algorithm [@wang2012mcscanx], originally
written in C++, using the `r BiocStyle::CRANpkg("Rcpp")` [@eddelbuettel2011rcpp] 
framework for R and C++ integration. This means that
`r BiocStyle::Biocpkg("syntenet")` comes with a native 
version of the MCScanX algorithm, so you can run MCScanX in R without
having to install it yourself. Amazing, huh?

To detect synteny and infer the synteny network, use the 
function `infer_syntenet()`. The output is a network represented as a
so-called **edge list** (i.e., a 2-column data frame with node 1 and node 2
in columns 1 and 2, respectively). 

```{r infer_syntenet}
# Infer synteny network
net <- infer_syntenet(blast_list, pdata$annotation)

# Look at the output
head(net)
```

In a synteny network, each row of the edge list represents an anchor pair. 
In the edge list above, for example, 
the genes `r net[1,1]` and `r net[1,2]` are in the same syntenic block.

# Phylogenomic profiling

After inferring the synteny network, the first thing you would want to do is
cluster your network and identify which phylogenetic groups are contained
in each cluster. This is what we call **phylogenomic profiling**. This way,
you can identify clade-specific clusters, and highly conserved clusters,
for instance. Here, we will use an example network of BUSCO genes for 
25 eudicot species, which was obtained from @zhao2019network. 

To obtain the phylogenomic profiles, you first need to cluster your network.
This can be done with `cluster_network()`. [^3]

[^3]: **Friendly tip:** `r BiocStyle::Biocpkg("syntenet")` uses the *Infomap* 
algorithm to cluster networks, which has been shown to have the best performance 
[@zhao2019network]. However, you can use any other network clustering
method implemented in the *cluster_* family of functions from the 
`r BiocStyle::CRANpkg("igraph")` package by passing the function directly
to the *clust_function* parameter (see `?cluster_network` for details).
Importantly, the Infomap algorithm (default clustering method) 
assigns each gene to a single cluster. 
However, for some cases (e.g., detection of tandem arrays), 
you may want to use an algorithm that allows community overlap 
(i.e., a gene can be part of more than one cluster). 
If this is your case, we recommend the *clique percolation* algorithm, 
which is implemented in the R package
`r BiocStyle::CRANpkg("CliquePercolation")` [@lange2021cliquepercolation].

```{r cluster_network}
# Load example data and take a look at it
data(network)
head(network)

# Cluster network
clusters <- cluster_network(network)
head(clusters)
```

Now that each gene has been assigned to a cluster, we can identify the
phylogenomic profiles of each cluster. This function returns 
a matrix of phylogenomic profiles, with clusters in rows 
and species in columns.

```{r phylogenomic_profile}
# Phylogenomic profiling
profiles <- phylogenomic_profile(clusters)

# Exploring the output
head(profiles)
```

As a plot is worth a thousand words (or numbers), you can use the function
`plot_profiles()` to visualize the phylogenomic profiles as a heatmap with 
species in rows and synteny network clusters in columns. The heatmap
generated by this function is highly customizable by users. Some
important remarks are:

1. You can add a legend for species metadata (e.g., taxonomic information) 
by passing a 2-column data frame to the parameter *species_annotation*.

2. Columns (network clusters) are grouped with Ward's clustering on a matrix
of distances. The method to compute the distance matrix can be defined by users
in parameters *dist_function* and *dist_params*. By default, it uses
the function `stats::dist()` with parameter `method = "euclidean"`. Likewise,
the function to cluster the distance matrix and additional parameters
can be specified in *clust_function* and *clust_params*. By default,
it uses `stats::hclust` with parameter `method = "ward.D"`.

3. The order in which species are displayed can be defined by users in 
parameter *cluster_species*. We strongly recommend passing a vector of 
species order that matches the species tree, so that patterns can be
explored in a phylogenetic context. Importantly, if the character vector is
named, vector names will be used as species names in the plot. This a nice
way to replace species abbreviations with their full names.


Here, to briefly demonstrate how to play with the parameters we just mentioned
in the 3 remarks above, we will:

- Create a vector with the order in which we want species to be displayed,
with longer species names in vector names.

- Create a metadata data frame containing the family of each species.

- Use the function `dsvdis()` from the `r BiocStyle::CRANpkg("labdsv")` package
to calculate Ruzicka distances when clustering columns.

```{r plot_profiles}
# 1) Create a named vector of custom species order to plot
species_order <- setNames(
    # vector elements
    c(
        "vra", "van", "pvu", "gma", "cca", "tpr", "mtr", "adu", "lja",
        "Lang", "car", "pmu", "ppe", "pbr", "mdo", "roc", "fve",
        "Mnot", "Zjuj", "jcu", "mes", "rco", "lus", "ptr"
    ),
    # vector names
    c(
        "V. radiata", "V. angularis", "P. vulgaris", "G. max", "C. cajan",
        "T. pratense", "M. truncatula", "A. duranensis", "L. japonicus",
        "L. angustifolius", "C. arietinum", "P. mume", "P. persica",
        "P. bretschneideri", "M. domestica", "R. occidentalis", 
        "F. vesca", "M. notabilis", "Z. jujuba",
        "J. curcas", "M. esculenta", "R. communis", 
        "L. usitatissimum", "P. trichocarpa"
    )
)
species_order

# 2) Create a metadata data frame containing the family of each species
species_annotation <- data.frame(
    Species = species_order,
    Family = c(
        rep("Fabaceae", 11), rep("Rosaceae", 6),
        "Moraceae", "Ramnaceae", rep("Euphorbiaceae", 3), 
        "Linaceae", "Salicaceae"
    )
)
head(species_annotation)


# 3) Plot phylogenomic profiles, but using Ruzicka distances
plot_profiles(
    profiles, 
    species_annotation, 
    cluster_species = species_order, 
    dist_function = labdsv::dsvdis,
    dist_params = list(index = "ruzicka")
)
```

The heatmap is a nice way to observe patterns. For instance, you can see some
Rosaceae-specific clusters, Fabaceae-specific clusters, and highly conserved
ones as well.

If you want to explore in more details the group-specific clusters,
you can use the function `find_GS_clusters()`. For that, you only need to
input the profiles matrix and a data frame of species annotation (i.e.,
species groups).

```{r find_GS_clusters}
# Find group-specific clusters
gs_clusters <- find_GS_clusters(profiles, species_annotation)

head(gs_clusters)

# How many family-specific clusters are there?
nrow(gs_clusters)
```

As you can see, there are `r nrow(gs_clusters)` family-specific clusters
in the network. Let's plot a heatmap of group-specific clusters only.

```{r heatmap_filtered}
# Filter profiles matrix to only include group-specific clusters
idx <- rownames(profiles) %in% gs_clusters$Cluster
p_gs <- profiles[idx, ]

# Plot heatmap
plot_profiles(
    p_gs, species_annotation, 
    cluster_species = species_order, 
    cluster_columns = TRUE
)
```

Pretty cool, huh? You can also visualize clusters as a network plot with
the function `plot_network()`. For example, let's visualize the 
group-specific clusters.

```{r plot_network}
# 1) Visualize a network of first 5 GS-clusters
id <- gs_clusters$Cluster[1:5]
plot_network(network, clusters, cluster_id = id)

# 2) Coloring nodes by family
genes <- unique(c(network$node1, network$node2))
gene_df <- data.frame(
    Gene = genes,
    Species = unlist(lapply(strsplit(genes, "_"), head, 1))
)
gene_df <- merge(gene_df, species_annotation)[, c("Gene", "Family")]
head(gene_df)

plot_network(network, clusters, cluster_id = id, color_by = gene_df)

# 3) Interactive visualization (zoom out and in to explore it)
plot_network(
    network, clusters, cluster_id = id, 
    interactive = TRUE, dim_interactive = c(500, 300)
)
```

# Microsynteny-based phylogeny reconstruction

Finally, you can use the information on presence/absence of clusters in each
species to reconstruct a microsynteny-based phylogeny.

To do that, you first need to binarize the profiles matrix (0s and 1s
representing absence and presence, respectively) and transpose it. This can
be done with `binarize_and_tranpose()`.

```{r binarize}
bt_mat <- binarize_and_transpose(profiles)

# Looking at the first 5 rows and 5 columns of the matrix
bt_mat[1:5, 1:5]
```

Now, you can use this transposed binary matrix as input to 
IQTREE2 [@minh2020iq] to infer a phylogeny. To do so, you can use the function
`infer_microsynteny_phylogeny()`, which allows you to run IQTREE2 from 
an R session [^4]. You need to have IQTREE2 installed in your machine and in 
your PATH to run this function. You can check if you have IQTREE2 installed 
with `iqtree_is_installed()`.

[^4]: **Alternative:** if you want to use a different program rather
than IQTREE2, you can use the function `profiles2phylip()` to write the 
transposed binary matrix to a PHYLIP file and run your favorite program
on the command line. However, when inferring a phylogeny from phylogenomic
profiles, you need to make sure that the program you are using supports 
substitution models for binary data. In IQTREE2, for instance, using
binary, morphological models requires passing parameters `-st MORPH`.


For the sake of demonstration, we will infer a phylogeny with 
`infer_microsynteny_phylogeny()` using the profiles for BUSCO genes for 
six legume species only. We will also remove non-variable sites (i.e.,
clusters that are present in all species or absent in all species).
However, we're only using this filtered data set for speed issues. 
For real-life applications, the best thing to do is to 
**include phylogenomic profiles for all genes** (not only BUSCO genes).


```{r infer_phylogeny}
# Leave only 6 legume species and P. mume as an outgroup for testing purposes
included <- c("gma", "pvu", "vra", "van", "cca", "pmu")
bt_mat <- bt_mat[rownames(bt_mat) %in% included, ]

# Remove non-variable sites
bt_mat <- bt_mat[, colSums(bt_mat) != length(included)]

if(iqtree_is_installed()) {
    phylo <- infer_microsynteny_phylogeny(bt_mat, outgroup = "pmu", 
                                          threads = 1)
}
```

The output of `infer_microsynteny_phylogeny()` is a character vector with paths
to the output files from IQTREE2. Usually, you are interested in the file
ending in *.treefile*. This is the species tree in Newick format, and it can
be visualized with your favorite tree viewer. We strongly recommend using
the `read.tree()` function from the Bioconductor package
`r BiocStyle::Biocpkg("treeio")` [@wang2020treeio] to
read the tree, and visualizing it with the `r BiocStyle::Biocpkg("ggtree")` 
Bioc package [@yu2017ggtree].
For example, let's visualize a microsynteny-based phylogeny for 123 angiosperm
species, whose phylogenomic profiles were obtained from @zhao2021whole.

```{r vis_phylogeny, message = FALSE, warning = FALSE, fig.height = 12, fig.width = 7}
data(angiosperm_phylogeny)

# Plotting the tree
library(ggtree)
ggtree(angiosperm_phylogeny) +
    geom_tiplab(size = 3) +
    xlim(0, 0.3)
```

# FAQ {.unnumbered}

## How do I execute an external dependency that is not in my PATH? {.unnumbered}

If you have DIAMOND and/or IQTREE2 installed, but in a directory that is not in 
your PATH, you can add this given directory to your PATH with the function
`Sys.setenv()`. 

For example, suppose your DIAMOND binary is in `/home/username/bioinfo_tools`.
To add this directory to your PATH, you would run:

```{r faq1}
# Add example directory /home/username/bioinfo_tools to PATH
Sys.setenv(
    PATH = paste(
        Sys.getenv("PATH"), "/home/username/bioinfo_tools", sep = ":"
    )
)
```

Note that your R PATH is not the same as your system's PATH. Thus, even if you
add the directory `/home/username/bioinfo_tools` to your system's path (e.g.,
by editing your ~/.bashrc file if you are on Linux), you would still need to
update your R PATH.

# Session information {.unnumbered}

This document was created under the following conditions:

```{r sessionInfo}
sessionInfo()
```

# References {.unnumbered}