---
title: "Introduction to ampir"
author: "Legana Fingerhut"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to ampir}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

# Background

The **ampir** (short for **a**nti**m**icrobial **p**eptide prediction **i**n **r** ) package was designed to be a fast and user-friendly method to predict antimicrobial peptides (AMPs) from any given size protein dataset. **ampir** uses a *supervised statistical machine learning* approach to predict AMPs. It incorporates two support vector machine classification models, "precursor" and "mature" that have been trained on publicly available antimicrobial peptide data. The default model, "precursor" is best suited for full length proteins and the "mature" model is best suited for small mature proteins (<60 amino acids). **ampir** also accepts custom (user trained) models based on the [caret](https://github.com/topepo/caret) package. Please see the **ampir** *"How to train your model"* vignette for details.

## Usage

Standard input to **ampir** is a `data.frame` with sequence names in the first column and protein sequences in the second column. 

```{r}
library(ampir)
```

Read in a FASTA formatted file as a `data.frame` with `read_faa()` 

```{r, warning=FALSE, message=FALSE}
my_protein_df <- read_faa(system.file("extdata/little_test.fasta", package = "ampir"))
```

```{r, echo=FALSE}
display_df <- my_protein_df
display_df$seq_aa <- paste(substring(display_df$seq_aa,1,45),"...",sep="")
knitr::kable(display_df)
```

Calculate the probability that each protein is an antimicrobial peptide with `predict_amps()` using the default "precursor" model. 

*Note that amino acid sequences that are shorter than 10 amino acids long and/or contain anything other than the standard 20 amino acids are not evaluated and will contain an `NA` as their `prob_AMP` value.*

```{r}
my_prediction <- predict_amps(my_protein_df, model = "precursor")
```

```{r, echo=FALSE}
my_prediction$seq_aa <- paste(substring(my_prediction$seq_aa,1,45),"...",sep="")
knitr::kable(my_prediction, digits = 3)
```

Predicted proteins with a specified predicted probability value could then be extracted and written to a FASTA file:

```{r}
my_predicted_amps <- my_protein_df[my_prediction$prob_AMP > 0.8,]
```

```{r, echo=FALSE}
my_predicted_amps$seq_aa <- paste(substring(my_predicted_amps$seq_aa,1,45),"...",sep="")
knitr::kable(my_predicted_amps)
```

Write the `data.frame` with sequence names in the first column and protein sequences in the second column to a FASTA formatted file with `df_to_faa()`

```{r}
df_to_faa(my_predicted_amps, tempfile("my_predicted_amps.fasta", tempdir()))
```





