---
title: "dagLogo Vignette"
author: "Jianhong Ou, Lihua Julie Zhu"
date: "`r doc_date()`"
package: "`r pkg_ver('dagLogo')`"
bibliography: bibliography.bib
abstract: >
  A sequence logo has been widely used as a graphical representation of an alignment of multiple amino acid or nucleic acid sequences. dagLogo can  visualize significant conserved amio acid sequence patterns in groups,  such as charge, chemistry, hydrophobicity and etc.
vignette: >
  %\VignetteIndexEntry{dagLogo Vignette}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
output:
  BiocStyle::html_document
---

```{r echo=FALSE, results='hide', warning=FALSE}
library(dagLogo)
```

# introduction
A sequence logo has been widely used as a graphical representation of an alignment of multiple amino acid or nucleic acid sequences.
There is a package seqlogo[@Oliver2006] implemented in R to draw DNA sequence logos. 
And another package motifStack[@Jianhong2012] was developed for drawing sequence logos for Amino Acid, DNA and RNA sequences. motifStack also has the capability for graphical representation of multiple motifs.

IceLogo[@Colaert2009] is a tool developed in java to visualize significant conserved sequence patterns in
an alignment of multiple peptide sequence against background sequences. Compare to 
webLogo[@Crooks2004], which relying on information theory, iceLogo builds on probability theory. It is
reported that iceLogo has a more dynamic nature and is correcter and completer in the analysis of
conserved sequence patterns.

However iceLogo can only compare conserved sequences to reference sequences peptide by peptide. 
As we know, some conserved sequence patterns are not conserved by peptides but by groups such as
charge, chemistry, hydrophobicity and etc.

Here we developed a R package:dagLogo based on iceLogo to visualize significant conserved sequence patterns in groups.

#Prepare environment
You will need ghostscript: the full path to the executable can be set by the environment variable R\_GSCMD. 
If this is unset, a GhostScript executable will be searched by name on your path. For example, on a Unix, linux or Mac "gs" is used for searching, 
and on Windows the setting of the environment variable GSC is used, otherwise commands "gswi64c.exe" then "gswin32c.exe" are tried.

Example on Windows: assume that the gswin32c.exe is installed at C:\\ Program Files\\ gs\\ gs9.06\\ bin, then open R and try:
```{r setenv, eval=FALSE, echo=TRUE}
Sys.setenv(R_GSCMD=file.path("C:", "Program Files", "gs", 
                             "gs9.06", "bin", "gswin32c.exe"))
```

# Examples of using dagLogo
## Sample 1, start from given peptides positions
## Step 1, fetch sequences
You should have interesting peptides position info and the identifiers for fetching
sequences via biomaRt.
```{r fetchSequences}
library(dagLogo)
library(biomaRt)
try({##just in case biomaRt server does not response
    mart <- useMart("ensembl", "dmelanogaster_gene_ensembl")
    dat <- read.csv(system.file("extdata", "dagLogoTestData.csv", 
                                package="dagLogo"))
    dat <- dat[1:5,] ##subset to speed sample
    dat
    seq <- fetchSequence(as.character(dat$entrez_geneid), 
                         anchorPos=as.character(dat$NCBI_site), 
                         mart=mart, 
                         upstreamOffset=7, 
                         downstreamOffset=7)
    head(seq@peptides)
})
```

Sometimes you don't have the exactly postions. You have only the interesting peptides and the identifiers.

```{r fetchSequences2}
try({
    seq <- fetchSequence(as.character(dat$entrez_geneid), 
                         anchorAA="*",
                         anchorPos=as.character(dat$peptide), 
                         mart=mart, 
                         upstreamOffset=7, 
                         downstreamOffset=7)
    head(seq@peptides)
})
```

In above sample, anchorAA is represented by asterisk. In following sample,
anchorAA is represented by lower case of amino acid.
```{r fetchSequences3}
if(interactive()){
    dat <- read.csv(system.file("extdata", "peptides4dagLogo.csv",
                package="dagLogo"))

    tail(dat)
    mart <- useMart("ensembl", "hsapiens_gene_ensembl")
    seq <- fetchSequence(toupper(as.character(dat$symbol)), 
                         type="hgnc_symbol",
                         anchorAA="s",
                         anchorPos=as.character(dat$peptides), 
                         mart=mart, 
                         upstreamOffset=7, 
                         downstreamOffset=7)
    head(seq@peptides)
}
```

Sometimes you may already have the aligned peptides sequences in hand. You will use 
formatSequence function to prepare an object of dagPeptides for further testing. To use 
formatSequence, you need prepare the proteome by prepareProteome function.
```{r formatSequence}
dat <- unlist(read.delim(system.file("extdata", "grB.txt", package="dagLogo"), 
                         header=F, as.is=TRUE))
head(dat)
##prepare proteome from a fasta file
proteome <- prepareProteome(fasta=system.file("extdata", 
                                              "HUMAN.fasta",
                                              package="dagLogo"))
##prepare object of dagPeptides
seq <- formatSequence(seq=dat, proteome=proteome, 
                      upstreamOffset=14, downstreamOffset=15)
```

## Step 2, build background model
Once you have an object of dagPeptides in hand, you can start to build background model
for DAG test. The background could be random subsequence of whole proteome or your inputs.
If the background was built from whole proteome or proteome without your inputs, 
an object of Proteome is required. 

To prepare a proteome, there are two methods, from a fasta file or from UniProt webservice.
Last example shows how to prepare proteome from a fasta file. Here we show how to prepare
proteome via UniProt webservice.

```{r prepareProteome0}
if(interactive()){
    library(UniProt.ws)
    UniProt.ws <- UniProt.ws(taxId=9606)
    proteome <- prepareProteome(UniProt.ws=UniProt.ws)
}
```

Then the proteome can be used for background model building. 

```{r prepareProteome}
bg <- buildBackgroundModel(seq, bg="wholeGenome", proteome=proteome)
```

## Step 3, do test
Test can be done without any change of the symbol pattern or with changes of grouped peptides
by such as charge, chemistry, hydrophobicity and etc.

```{r testDAU}
t0 <- testDAU(seq, bg)
t1 <- testDAU(seq, bg, group="classic")
t2 <- testDAU(seq, bg, group="charge")
t3 <- testDAU(seq, bg, group="chemistry")
t4 <- testDAU(seq, bg, group="hydrophobicity")
```

## Step 4, graphical representation results
We can use heatmap or logo to show the results.
```{r dagHeatmap,fig.cap="DAG heatmap",fig.width=6,fig.height=6}
dagHeatmap(t0) ##Plot a heatmap to show the results
```

```{r dagLogo0,fig.cap="ungrouped results",fig.width=6,fig.height=4}
dagLogo(t0) ##Plot a logo to show the ungrouped results
```

```{r dagLogo1,fig.cap="classic grouped",fig.width=6,fig.height=4}
##Plot a logo to show the classic grouped results
dagLogo(t1, namehash=nameHash(t1@group), legend=TRUE)
```

```{r dagLogo2,fig.cap="charge grouped",fig.width=6,fig.height=4}
##Plot a logo to show the charge grouped results
dagLogo(t2, namehash=nameHash(t2@group), legend=TRUE)
```

```{r dagLogo3,fig.cap="chemistry grouped",fig.width=6,fig.height=4}
##Plot a logo to show the chemistry grouped results
dagLogo(t3, namehash=nameHash(t3@group), legend=TRUE)
```

```{r dagLogo4,fig.cap="hydrophobicity grouped",fig.width=6,fig.height=4}
##Plot a logo to show the hydrophobicity grouped results
dagLogo(t4, namehash=nameHash(t4@group), legend=TRUE)
```

# using dagLogo to analysis Catobolite Activator Protein
CAP (Catabolite Activator Protein, also known as CRP for cAMP Receptor Protein) 
is a transcription promoter that binds at more than 100 sites within the E. coli genome. 

The motif of the DNA-binding helix-turn-helix motif of the CAP family is drawn by 
motifStack as following figure.

```{r CAPmotif,fig.cap="Catobolite Activator Protein Motif",fig.width=6,fig.height=4}
library(motifStack)
protein<-read.table(file.path(find.package("motifStack"),"extdata","cap.txt"))
protein<-t(protein[,1:20])
motif<-pcm2pfm(protein)
motif<-new("pfm", mat=motif, name="CAP", 
            color=colorset(alphabet="AA",colorScheme="chemistry"))
##The DNA-binding helix-turn-helix motif of the CAP family ploted by motifStack
plot(motif)
```

If we use dagLogo to plot the motif, it will be shown as following figure.
Residues 7-13 form the first helix, 14-17 the turn and 18-26 the DNA recognition helix. 
The glycine at position 15 appears to be critical in forming the turn. 
```{r CAPdagLogo,fig.cap="Catobolite Activator Protein Motif",fig.width=6,fig.height=4}
library(Biostrings)
cap <- as.character(readAAStringSet(system.file("extdata", 
                                                "cap.fasta", 
                                                package="dagLogo")))
data(ecoli.proteome)
seq <- formatSequence(seq=cap, proteome=ecoli.proteome)
bg <- buildBackgroundModel(seq, bg="wholeGenome", 
                           proteome=ecoli.proteome, 
                           permutationSize=10L)
##The DNA-binding helix-turn-helix motif of the CAP family ploted by dagLogo
t0 <- testDAU(seq, bg)
dagLogo(t0)
```

If the peptides are grouped by chemistry and then plot, it will be shown as 
following figure. Positions 10, 14, 16, 21 and 25 are partially or 
completely buried and therefore tend to be populated by hydrophobic amino acids, 
which are very clear if we group the peptides by chemistry. 
```{r CAPgroup,fig.cap="Catobolite Activator Protein Motif",fig.width=6,fig.height=4}
## The DNA-binding helix-turn-helix motif of the CAP family grouped by chemistry
t1 <- testDAU(seq, bg, group="chemistry")
dagLogo(t1, namehash=nameHash(t1@group), legend=TRUE)
```

# Session Info
```{r sessionInfo}
sessionInfo()
```
