---
title: "tidypopgen"
output: rmarkdown::html_vignette
        #pdf_document
vignette: >
  %\VignetteIndexEntry{tidypopgen}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

This vignette gives a short introduction on how to use the `tidypopgen` package
to perform basic data cleaning and a PCA on a dataset of European lobsters
(*Homarus gammarus*) from Jenkins et al. (2019). The original data are available
at https://datadryad.org/dataset/doi:10.5061/dryad.2v1kr38 (released under
CC0), but for the purpose of
this vignette we will use a subset of the data stored as a .bed file, available
in the `inst/extdata/lobster` directory of the package. To install `tidypopgen`
from r-universe, use:

```{r eval = FALSE}
install.packages("tidypopgen",
  repos = c(
    "https://evolecolgroup.r-universe.dev",
    "https://cloud.r-project.org"
  )
)
```

Next, load the `tidypopgen` package and the `ggplot2` package for plotting.

```{r}
library(tidypopgen)
library(ggplot2)
```

# Creating a `gen_tibble`

In `tidypopgen`, we represent data as a `gen_tibble`, a subclass of `tibble`
containing the columns `id` and `genotypes` for each individual. Genotypes are
stored in a compressed format as a File-Backed Matrix that can be easily
accessed by functions in `tidypopgen`. The `genotypes` column contains a vector
of the row indices of each individual in the File-Backed Matrix, and when
printed, the `genotypes` column shows the first two genotypes for each
individual. For a visual representation of the `gen_tibble` object structure and
a full discussion of how to manipulate `gen_tibble` objects, see vignette 'The
grammar of population genetics'.

Additionally, if data are loaded from a .bed file with information in the `FID`
column, this is treated as population information and is automatically
added to the `gen_tibble` as column `population`. As with a normal tibble, this
information can be changed, updated, or removed from the `gen_tibble` if needed.
`tidypopgen` can also read data from `packedancestry` files (`.geno` files, in
the GENO or the TGENO layout of EIGENSOFT, as used by the AADR) and `vcf`.

Let's start by creating a `gen_tibble` from the `lobster.bed` file.

```{r}
lobsters <- gen_tibble(
  x = system.file("extdata/lobster/lobster.bed", package = "tidypopgen"),
  quiet = TRUE, backingfile = tempfile()
)
head(lobsters)
```

We can see the structure of the `gen_tibble` above, with the expected columns.
In this case, our .bed file contains population information corresponding to the
sampling site of each lobster in the dataset.

If we want to take a look at the genotypes of our lobsters, we can use the
`show_genotypes` function to return a matrix of genotypes, where rows are
individuals and columns are loci. This is a big table, so we will just look
at the first ten loci for the first 5 individuals:

```{r}
lobsters %>% show_genotypes(indiv_indices = 1:5, loci_indices = 1:10)
```

And, similarly, if we want to see information about the loci we can use the
`show_loci` function, which returns a tibble with information about each locus.
Again this is a big table, so we will use `head()` to only look at the first
few:

```{r}
head(lobsters %>% show_loci())
```

Now we have a `gen_tibble` to work with, we can start to clean the data.

# Quality control

Our dataset contains several lobster populations, so we group it by
population before quality control. The quality control functions then take
the populations into account: for example, `qc_report_loci` tests
Hardy-Weinberg equilibrium within each population (a test across populations
would flag many loci only because their allele frequencies differ between
populations).

```{r}
lobsters <- lobsters %>% group_by(population)
```

Let's start by checking the quality of the data for each individual lobster in
our dataset. We can use the `qc_report_indiv` function to generate a report
which contains information about missingness and heterozygosity for each
individual.

```{r}
indiv_qc_lobsters <- lobsters %>% qc_report_indiv()
```

We can take a look at this data using the `autoplot` function:

```{r, fig.alt = "Scatter plot of missingness proportion and observed heterozygosity for each individual"}
autoplot(indiv_qc_lobsters, type = "scatter")
```

We can see that most individuals have low missingness and heterozygosity, but
there are a few individuals missing >20% of their genotypes. We can remove
these individuals using the `filter` function, specifying to keep only
individuals with missingness under 20%. Note that `filter` can clash with
grouped data, so we need to ungroup (so that we can use a vector for all
individuals, rather than a vector for each population) before filtering, and
then re-group the data by population.


```{r}
lobsters <- lobsters %>%
  ungroup %>%
  filter(indiv_qc_lobsters$missingness < 0.2) %>%
  group_by(population)
```

Now lets check our loci. We can use the `qc_report_loci` function to generate a
report of the loci quality. This function will return another `qc_report`
object, which contains information about missingness, minor allele frequency,
and Hardy-Weinberg Equilibrium for each locus.

```{r}
loci_qc_lobsters <- lobsters %>% qc_report_loci()
```

As `lobsters` is grouped, Hardy-Weinberg equilibrium is tested within each
population (with a Bonferroni correction across populations), while the minor
allele frequency and the missingness of each locus are computed over all
lobsters. Let's take a look at minor allele frequency for all loci:

```{r, fig.alt = "Histogram of minor allele frequency"}
autoplot(loci_qc_lobsters, type = "maf")
```

And we can see that we don't have any monomorphic SNPs.

Now let's look at missingness.

```{r, fig.alt = "Histogram of the proportion of missing data"}
autoplot(loci_qc_lobsters, type = "missing")
```

Our data mostly have low missingness, but we can see that some loci have > 5%
missingness across individuals, and we want to remove these loci.

In tidypopgen, there are two functions to subset the loci in a `gen_tibble`
object: `select_loci` and `select_loci_if`. The function `select_loci` operates
on information about the loci (e.g filtering by chromosome or by rsID), while
`select_loci_if` operates on the genotypes at those loci (e.g filtering by minor
allele frequency or missingness). In this case, we want to remove loci with >5%
missingness, so we can use `select_loci_if` with the `loci_missingness`
function, operating on the `genotypes` column of our `gen_tibble`.

```{r}
lobsters <- lobsters %>%
  select_loci_if(loci_qc_lobsters$missingness < 0.05)
```

Note that `select_loci_if` ignores the groups: all populations share the same
loci, so the missingness of each locus is computed over all lobsters, as in
`qc_report_loci`. To select loci on statistics computed within each
population, see the vignette 'The grammar of population genetics'.

Through our filtering, we have removed a few individuals and loci. Most
functions, including the PCA below, work directly on the remaining data.

# PCA

There are a number of PCA algorithms in tidypopgen. Here, we will use
the default method of the `gt_pca` function, which does not need the remaining 
missing genotypes to be imputed. Imputation (with `gt_impute_simple`) is
required for some specific PCA methods. See the vignettes
'The grammar of population genetics' and 'Quality Control' for further details
on imputing a `gen_tibble`.

We can now run a PCA:

```{r}
partial_pca <- gt_pca(lobsters)
```

And we can create a simple plot using `autoplot`:

```{r, fig.alt = "Score plot of individuals across the first and second Principal Components"}
autoplot(partial_pca, type = "scores")
```

That was easy! The `autoplot` gives us a quick idea of the explained variance and
rough distribution of the samples, but we need to see the different populations
within our dataset. 

For a quick overview, we could add an aesthetic to our plot:

```{r, fig.alt = "Score plot of individuals across the first and second Principal Components, with individual samples coloured by population"}
autoplot(partial_pca, type = "scores") +
  aes(color = lobsters$population) +
  labs(color = "population")
```

However, if we want to fully customise our plot, we can wrangle the data
directly and use `ggplot2`.

# Plot with ggplot2

For a customised plot, we can extract the information on the scores of each
individual using the `augment` method for `gt_pca`.

```{r}
pcs <- augment(x = partial_pca, data = lobsters)
```

Then we can extract the eigenvalues for each principal component with the `tidy`
function, using the "eigenvalues" argument:

```{r}
eigenvalues <- tidy(partial_pca, "eigenvalues")

xlab <- paste("Axis 1 (", round(eigenvalues[1, 3], 1), " %)",
  sep = ""
)
ylab <- paste("Axis 2 (", round(eigenvalues[2, 3], 1), " %)",
  sep = ""
)
```

And finally plot:

```{r, fig.alt = "Score plot of individuals across the first and second Principal Components, with individual samples coloured by population"}
ggplot(data = pcs, aes(x = .fittedPC1, y = .fittedPC2)) +
  geom_hline(yintercept = 0) +
  geom_vline(xintercept = 0) +
  geom_point(aes(fill = population),
    shape = 21, size = 3, show.legend = FALSE
  ) +
  scale_fill_distruct() +
  labs(x = xlab, y = ylab) +
  ggtitle("Lobster PCA")
```

Or we could create a labelled version of our PCA by determining the centre
of each group to place the labels

```{r}
# Calculate centre for each population
centroid <- aggregate(cbind(.fittedPC1, .fittedPC2, .fittedPC3) ~ population,
  data = pcs, FUN = mean
)

# Add these coordinates to our augmented pca object
pcs <- left_join(pcs, centroid, by = "population", suffix = c("", ".cen"))
```

And then add labels to the plot:

```{r, fig.alt = "Score plot of individuals across the first and second Principal Components. Individual samples are coloured by population, the centroid of points for each group is labelled with the population name, and lines are drawn from the centroid to each point."}
ggplot(data = pcs, aes(x = .fittedPC1, y = .fittedPC2)) +
  geom_hline(yintercept = 0) +
  geom_vline(xintercept = 0) +
  geom_segment(aes(xend = .fittedPC1.cen, yend = .fittedPC2.cen),
    show.legend = FALSE
  ) +
  geom_point(aes(fill = population),
    shape = 21, size = 3, show.legend = FALSE
  ) +
  geom_label(
    data = centroid,
    aes(label = population, fill = population),
    size = 4, show.legend = FALSE
  ) +
  scale_fill_distruct() +
  labs(x = xlab, y = ylab) +
  ggtitle("Lobster PCA")
```

That's it! There are more functions to run different types of analyses (DAPC,
ADMIXTURE, f statistics with admixtools2,  etc.); each resulting object has
`autoplot`, `tidy`, and `augment`
to explore the results and integrate it into the information from the
`gen_tibble`.

## References

Jenkins T.L., Ellis C.D., Triantafyllidis A. and Stevens J.R. (2019) Single nucleotide polymorphisms reveal a genetic cline across the north-east Atlantic and enable powerful population assignment in the European lobster. *Evolutionary Applications* 12: 1881-1899. doi:10.1111/eva.12849
