---
title: "*Phytophthora sojae* Pathotype Data Analysis"
author: "Austin McCoy, Zachary Noel"
date: "March 7th, 2019"
output:
    html_document: default
---
##### This analysis will provide distribution of susceptibilities, distribution of complexities with statistics, pathotype frequency distribution, and individual isolate pathotypes as well as diversity indices for pathotypes.These scripts are meant to be a substitute for the Hagis spreadsheet previously used for *Phytophthora sojae* pathotype analysis and provide the same necessary data as the Hagis sheet.

## Packages needed for analysis

TRUE indicates package was installed and loaded correctly.
```{r installing packages, warning=FALSE, message=FALSE, echo=FALSE}
# ipak will install and load the multiple r packages that are needed for this analysis
# Source: https://gist.github.com/stevenworthington/3178163
ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[,"Package"])]
if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
packages <- c("plyr", "ggplot2", "tidyverse", "plotrix", "stringr", "pander", "vegan", "devtools")
ipak(packages)
devtools::install_github("krlmlr/here")
library(here)
```

Reading in your data. Do not change "Pathotype.Data", just the name of the file, we will use the Practice data provided as an example. The function > here() will find the .csv file relative to the R project, so there is no need to set working directory or provide full file path. 

The input should be in .csv format with any NA values encoded as blanks

If NA values are encoded differently, replace the option na = "" to what your values are encoded as
```{r read data, message=FALSE}
Pathotype.Data <- read.csv(here("Practice data set.csv"), na = "")
```

This reads the file called functions_themes.R and runs it. This will enable you to use the functions and defines graphic themes. 

```{r Sourcing functions}
source(here("functions_themes.R"))
```


## Section 1: Distribution of Susceptibilities

Do not change Pathotype.Data in any of the functions. You need only change the three spaces after Pathotype.Data to your associated column headings and the fourth space, which is susceptibility cutoff percentage.

###Instructions

"Isolate" should be renamed to the column header for the column which identifies the isolates tested
"perc.susc" should be renamed to the column header for the column which identifies the percent susceptible plants for each gene
"Gene" should be renamed to the column header for the column which identifies the genes tested

These will need to be changed in all functions within this .Rmd file for the code to work.

The value in "Distribution_of_Susceptibilities(Pathotype.Data, "Isolate:, "perc.susc", "Gene", 60)" (in this case, 60), sets the cutoff for susceptible reactions. For example, currently all genes with 60% or more of the plants rated susceptible will return a "1" in The following scripts (meaning it is susceptible). You can change this to whatever percentage you require for your study.



The output will return a list with the first element equal to the graphic, and the second with the table. 
You can parse the list by putting a $ and showing if you want the Data or a graphic

```{r Running function}
Suceptibilities <- Distribution_of_Susceptibilities(Pathotype.Data, "Isolate", "perc.susc", "Gene", 60)
```

```{r Data output}
pander::pander(Suceptibilities$Data)
```

```{r Graphic output}
Suceptibilities$Graphic
```

## Section 2: Distribution of Complexities

You will need the change "Isolate", "perc.susc", and "Gene" again in this function to your correct column headers in your dataset.
Again, you can change your susceptible cutoff value here for your dataset
```{r Run function}
complexities <- Distribution_of_Complexities(Pathotype.Data, "Isolate", "perc.susc", "Gene", 60)
```

Output the frequency data
```{r Frequency data output}
pander::pander(complexities$FrequencyData)
```

Output the distribution data 
```{r Distribution data output}
pander::pander(complexities$DistributionData)
```

output the mean of the distribution 
```{r Mean output}
complexities$Mean
```

output the standard deviation of the output
```{r Standard Deviation output}
complexities$StandardDev
```

output the standard error of the output
```{r Standard Error output}
complexities$StandardErr
```

Output the frequency plot
```{r Frequency Plot output}
complexities$FrequencyPlot
```

Output the distribution plot
```{r Distribution Plot output}
complexities$DistributionPlot
```



## Section 3: Pathotype Frequency Distribution
You will need the change "Isolate", "perc.susc", and "Gene" again in this function to your correct column headers in your dataset.
Again, you can change your susceptible cutoff value here for your dataset.
```{r Pathotype Freq}
path.freq <- Pathotype.frequency.dist(Pathotype.Data, "Isolate", "perc.susc", "Gene", 60) 
```

You can parse the data in this chunk to show either the pathotype frequency for unique pathotypes, or to show each individual isolates pathotype that you tested.

frequency of unique pathotypes = $pathotypes_distribution

Individual pathotypes = $individual_pathotypes
```{r echo = FALSE, results='hold'}
pander::pander(path.freq$individual_pathotypes)
```

## Section 4. Diversity index for Pathotypes

Diversity indices used to investigate pathotype diversity within and between states are shown below. 

```{r}
diversity <- Diversity_index(Pathotype.Data, "Isolate", "perc.susc", "Gene", 60)
```


Pathotype diversity indices can be parsed as shown:

Simple diversity = $Simple

Shannon diversity = $Shannon

Simpson diversity = $Simpson

Gleason diversity = $Gleason

Evenness = $Evenness
```{r}
diversity$Evenness
```

### Recommendations are always appreciated!!
#### Chilvers Lab
#### Michigan State University
#### East Lansing, MI
