---
title: "Quick start example 1: Simulated data"
description: >
  Install sequoia, simulate data, run parentage assignment and full pedigree reconstruction.
author: "J Huisman"
date: "15 Oct 2020"
output:
  bookdown::html_document2:
    base_format: rmarkdown::html_vignette
    toc: true
    toc_depth: 3
config:
  toc: yes
  toc_depth: 3
bibliography: ["sequoia_refs.bib"]
biblio-style: "apalike"
link-citations: true
linkcolor: blue
vignette: >
  %\VignetteIndexEntry{Quick start example 1: Simulated data}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
---

# Quick start example 1: Simulated data

```{r setup-q, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval=TRUE, #cache=TRUE,
                      fig.height=4, fig.width=6, 
#                      fig.path='/figs',   DO NOT USE
                      fig.pos="ht!")
library(sequoia)
```

## Get started
*For a succinct version of this example where you can copy/paste all code in one go, see the main vignette*

In this example genotypes are simulated from a fictional pedigree, which together with the associated life history data is provided with the package. This pedigree consists of 5 discrete generations with interconnected half-sib clusters (Pedigree II in the paper[^1]).

[^1]: Huisman, Jisca. "Pedigree reconstruction from SNP data: parentage assignment, sibship clustering and beyond." Molecular ecology resources 17.5 (2017): 1009-1024.


```{r example1-a, eval=FALSE, purl=FALSE}
# Install the package. This is only the required the first time, or if you wish to update
# install.packages("sequoia")   

# Load the package. This is required at the start of every new R session.
library(sequoia) 

# Load the example pedigree and life history data
data(Ped_HSg5, LH_HSg5)

# Take a look at the data structure
tail(Ped_HSg5)
head(LH_HSg5)

# or, in Rstudio, view the full dataframe:
View(Ped_HSg5)
```


## Simulate genotypes
Simulate some genotype data to use for this try-out. 
```{r example1-d, eval=FALSE, purl=FALSE}
Geno <- SimGeno(Ped = Ped_HSg5, nSnp = 200)
```
Function `SimGeno()` can simulate data with a specific genotyping error rate, call rate and proportion of non-genotyped parents, see its helpfile (`?SimGeno`) for details. Here we use the defaults; 40\% of parents are presumed non-genotyped (their genotypes are removed from the dataset before `SimGeno()` returns its result). 

Alternatively, use the simulated genotype data included in the package to get identical results:
```{r}
Geno <- Geno_HSg5
```


## Parentage assignment
Run sequoia with the genotype data, lifehistory data, and otherwise default values. It is often advisable to first only run parentage assignment, and check if the results are sensible and/or if any parameters need adjusting. Full pedigree reconstruction, including sibship clustering etc., is much more time consuming. 

To only run a data check, duplicate check (always performed first) and parentage assignment, specify `Module = 'par'`:
```{r example1-e}
ParOUT <- sequoia(GenoM = Geno,
                  LifeHistData = LH_HSg5,
                  Module = 'par')
```

<!--- explain about ageprior messages & plots, and LL ---> 
You will see several plots appearing:  

- Histograms of the minor allele frequency and missingness, produced by `SnpStats()`, which is called by `CheckGeno()` to make sure there are no monomorphic SNPs or SNPs with extremely high missingness in the dataset  
- Heatmap of the ageprior before parentage assignment, showing that an age difference of 0 years is disallowed ($0$, black) for mothers (*M*) and fathers (*P*), but all other age differences are allowed ($1$, pale green) for all relationships. The initial maximum age of parents is set to the maximum age difference found in the lifehistory data.  
- Barplots of assignment rate (produced by `SummarySeq()`): nearly 60\% of individuals have a genotyped parent assigned, in line with the 40\% non-genotyped parents we simulated. 


In addition, you will see several messages, including about the initial and post-parentage total LL (log10-likelihood). This is the probability of observing the genotype data, given the (current) pedigree; initially it is assumed that all individuals are unrelated. This number is negative, and gets closer to zero when the pedigree explains the data better. 


The result is a list, with the following elements:
```{r example1-f}
names(ParOUT)
```
which are explained in detail in the helpfile (`?sequoia`) and the main vignette. 


you find the assigned parents in list element `PedigreePar`:
```{r example1-g}
tail(ParOUT$PedigreePar)
```

we can compare these to the true parents, in the original pedigree from which we simulated the genotype data: 
```{r example1-h}
chk_par <- PedCompare(Ped1 = Ped_HSg5, Ped2 = ParOUT$PedigreePar)
chk_par$Counts["GG",,]   
```
Here 'GG' stands for *G*enotyped offspring, *G*enotyped parent. 


## Pedigree reconstruction
Then, we can run full pedigree reconstruction. By default, this re-runs the parentage assignment.
```{r example1-i, eval=FALSE, purl=FALSE}
SeqOUT_HSg5 <- sequoia(GenoM = Geno, LifeHistData = LH_HSg5, Module = "ped")
```

If you don't want to re-run parentage assignment (e.g. because it took quite a bit of time), or if you have specified several non-default parameter values you want to use again, , you can provide the old output as input. 

Re-used will be:

- parameter settings (`$Specs`)
- genotyping error structure (`$ErrM`) 
- Lifehistory data (`$LifeHist`)
- Age prior, based on the age distribution of assigned parents (`$AgePriors`)

The last is generated by `MakeAgePrior()`, which has detected that all parent-offspring pairs have an age difference of $1$, and all siblings an age difference of $0$, i.e. that generations do not overlap. This information will be used during the full pedigree reconstruction.

```{r example1-i2, eval=TRUE}
ParOUT$AgePriors
```

So run `sequoia()` with the old output as input,
```{r example1-j, eval=FALSE, purl=FALSE}
SeqOUT_HSg5 <- sequoia(GenoM = Geno,
                  SeqList = ParOUT,
                  Module = "ped")
```


Full pedigree reconstruction will take at least a few minutes to run on this fairly simple dataset with one thousand individuals. It may take a few hours on larger or more complex datasets, and/or if there is much ambiguity about relationships due to a low number of SNPs, genotyping errors, and missing data. 

The output from this example pedigree is included with the package:
```{r example1-j2, eval=TRUE}
data(SeqOUT_HSg5)
```


You will get several messages:  

- From `CheckGeno()` to inform you that the genotype data is OK  
- To inform you which parts of `SeqList` (the output from the old `sequoia()` run) are being re-used  
- At the end of each iteration, to update you on the total log10-likelihood (LL), the time it took for that iteration, and the number of dams & sires assigned to real, genotyped (non-dummy) individuals. The total LL and number of assigned parents should plateau (but may 'wobble' a bit), and then the algorithm will finish.

Again you will see some plots:

- Histograms of the minor allele frequency and missingness  
- Heatmap of an extended version of the ageprior, including all grandparents and avuncular (aunt/uncle) relationships (`$AgePriorExtra`). These are derived from the ageprior for parents and siblings, which hasn't changed since just after parentage assignment  
- an update of the assignment rates plot, now including dummy parents.   

For additional plots and a few tables to inspect the pedigree, you can use `SummarySeq()`:
```{r example1-k}
summary_seq1 <- SummarySeq(SeqOUT_HSg5, Panels=c("sibships", "D.parents", "LLR"))
names(summary_seq1)
```



And again we can also compare the results to the true parents: 
```{r example1-l}
chk <- PedCompare(Ped1 = Ped_HSg5, Ped2 = SeqOUT_HSg5$Pedigree)
chk$Counts
```
See `?PedCompare` for a more interesting example with some mismatches. 

If you wish to count e.g. the number of full sibling pairs that are assigned as full siblings, paternal half siblings, maternal halfsiblings, or unrelated, or similar comparisons, use `ComparePairs()`. 



## Save results
Lastly, you often wish to save the results to file. You can do this as an `Rdata` file, which can contain any number of R objects. You can later retrieve these in R using `load`, so that you can later resume where you left of. The disadvantage is that you cannot open the `Rdata` files outside of R (as far as I am aware). Therefore, `sequoia` also includes a function to write all output to a set of plain text files in a specified folder. 
```{r example1-m, eval=FALSE, purl=FALSE}
save(SeqOUT_HSg5, Geno, file="Sequoia_output_date.RData")
writeSeq(SeqList = SeqOUT_HSg5, GenoM = Geno, folder = "Sequoia-OUT")
```


<!---
## Real data
For a template which can be adapted to analyse real genotype data, see [quick-start example 2](./quick_start_example2.html) in the main vignette. 
--->
