---
title: "LowMACA: Low frequency Mutation Analysis via Consensus Alignment"
author: "Giorgio Melloni, Stefano de Pretis"
abstract: >
  LowMACA evaluates the functional impact of somatic mutations in cancer by statistically 
  assessing the number of alterations that accumulates on the same residue of a protein family.
vignette: >
  %\VignetteIndexEntry{Bioconductor style for HTML documents}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
output: 
  BiocStyle::html_document:
    self_contained: false
    toc_float: true
---

# Introduction
The `r Rpackage("LowMACA")` package is a simple suite of tools to investigate and 
analyse the profile of the somatic mutations provided by the cBioPortal 
(via the `r Rpackage("cgdsr")`). `r Rpackage("LowMACA")` evaluates the functional impact of somatic 
mutations by statistically assessing the number of alterations that accumulates on the same residue 
(or residue that are conserved in Pfam domains). 
For example, the known driver mutations G12,G13 and Q61 in KRAS can be found on the 
corresponding residues of other proteins in the RAS family (PF00071) like NRAS and HRAS, 
but also in less frequently mutated genes like RRAS and RRAS2. 
The corresponding residues are identified via multiple sequence alignment. 
Thanks to this approach the user can identify 
new driver mutations that occur at low frequency at single protein level but emerge 
at Pfam level. In addition, the impact of known driver mutations can be transferred to other proteins that share 
a high degree of sequence similarity (like in the RAS family example). 

You can conduct an hypothesis driven exploratory analysis using our package 
simply providing a set of genes and/or pfam domains of your interest. 
The user is able to choose the kind of tumor and the type of mutations 
(like missense, nonsense, frameshift etc.). 
The data are directly downloaded from the largest cancer sequencing projects 
and aggregated by LowMACA to evaluate the possible functional impact of somatic mutations by spotting 
the most conserved variations in the cohort of cancer samples. 
By connecting several proteins that share sequence similarity via consensus alignment, 
this package is able to statistically assessing the occurrence of mutations on the same residue and ultimately see:

- where mutations fall and what are the involved domains
- what is the frequency of the aberrations and what is the more represented tumor type
- if and where the mutations tend to clusterize 
- what is the degree of conservation of the mutated residues
- if there are new driver genes and in particular, driver mutations


# System Requirements
LowMACA relies on two external resources to work properly.

- Clustal Omega, our trusted aligner (http://www.clustal.org/omega/)
- Ghostscript, a postscript interpreter needed to draw logo plots (http://www.ghostscript.com/)

Clustal Omega is a fast aligner that can be downloaded from the link above. 
For both Unix and Windows users, remember to have "clustalo" in your PATH variable. 
In case you cannot set "clustalo" in the PATH,
you can always set the clustalo command from inside R, after creating a LowMACA object:

```{r , echo=TRUE , eval=FALSE , results="hide" , message=FALSE , warning=FALSE}
#Given a LowMACA object 'lm'
lm <- newLowMACA(genes=c("TP53" , "TP63" , "TP73"))
lmParams(lm)$clustal_cmd <- "/your/path/to/clustalo"
```

If you cannot install clustalomega, we provide a wrapper around EBI web service 
(http://www.ebi.ac.uk/Tools/webservices/services/msa/clustalo\_soap). You just need 
to set your email as explained in section setup, but you have a limit of 2000 
input sequences and perl must be installed with the modules LWP and XML::Simple.

Ghostscript is an interpreter of postscript language and a pdf reader that is used by the R library grImport.

- For Linux users, simply download the program from http://ghostscript.com/download/gsdnld.html and compile it
- For MacOS users there is a dmg installer at http://pages.uoregon.edu/koch/
- For Windows users, download the program from http://ghostscript.com/download/gsdnld.html and then you have 3 options:
	- Put C:/Program Files/gs/gs9.05/bin in your PATH once for all (Adjust the path to match your gs installation number)
	- Run the command Sys.setenv(R\_GSCMD = '"C:/Program Files/gs/gs9.05/bin/gswin32c.exe"') at every new session of R
	- Put the command showed above in your .Renviron file

More details can be found here:
http://pgfe.umassmed.edu/BioconductorGallery/docs/motifStack/motifStack.html

`r Rpackage("LowMACA")` needs an internet connection to:

- retrieve mutation data from cBioPortal, 
- draw the Protter-style plot (http://wlab.ethz.ch/protter/start/) and 
- use the web service of clustalomega (http://www.ebi.ac.uk/Tools/webservices/services/msa/clustalo\_soap)

# Create a LowMACA object

First of all, we have to define our target genes or pfam domains that we wish to analyse. 

## Find the target family of proteins or pfam

```{r , echo=TRUE , eval=TRUE,results="hide" , message=FALSE , warning=FALSE}
library(LowMACA)
#User Input
Genes <- c("ADNP","ALX1","ALX4","ARGFX","CDX4","CRX"
        ,"CUX1","CUX2","DBX2","DLX5","DMBX1","DRGX"
        ,"DUXA","ESX1","EVX2","HDX","HLX","HNF1A"
    	  ,"HOXA1","HOXA2","HOXA3","HOXA5","HOXB1","HOXB3"
			  ,"HOXD3","ISL1","ISX","LHX8")
Pfam <- "PF00046"
```

```{r , echo=TRUE}
#Construct the object
lm <- newLowMACA(genes=Genes, pfam=Pfam)
str(lm , max.level=3)
```

Now we have created a `r Rpackage("LowMACA")` object. In this case, we want to analyse 
the homeodomain fold pfam (PF00046), considering 28 genes that belong to this clan. 
If we don't specify the pfam parameter, `r Rpackage("LowMACA")` proceeds to analyse 
the entire proteins passed by the genes parameter (we map only canonical proteins, one per gene).
Conversely, if we don't specify the genes parameter, `r Rpackage("LowMACA")` 
looks for all the proteins that contain the specified pfam and analyses just the portion of the protein assigned to the domain.

## Change default parameters

A LowMACA object is composed by four slots. The first slot is *arguments* 
and is filled at the very creation of the object. It contains information as Uniprot 
name for the proteins associated to the genes, the amino acid sequences, 
start and end of the selected domains and the default parameters that can be changed to start the analysis.

```{r, echo=TRUE}
#See default parameters
lmParams(lm)
#Change some parameters
#Accept sequences even with no mutations
lmParams(lm)$min_mutation_number <- 0
#Changing selected tumor types
#Check the available tumor types in cBioPortal
available_tumor_types <- showTumorType()
head(available_tumor_types)
#Select melanoma, stomach adenocarcinoma, uterine cancer, lung adenocarcinoma, 
#lung squamous cell carcinoma, colon rectum adenocarcinoma and breast cancer
lmParams(lm)$tumor_type <- c("skcm" , "stad" , "ucec" , "luad" 
	, "lusc" , "coadread" , "brca")
```


# Setup

## Align sequences

```{r , fourthchunk, echo=TRUE , eval=TRUE}
lm <- alignSequences(lm)
```

This method is basically self explained. It aligns the sequences in the object. 
If you have not installed clustalomega yet, you can use the web service of clustalomega 
that we wrapped in our R package. 
The limit is set to 2000 sequences and it is slower than a local installation. 
Remember to put your own email in the mail command 
to activate this option since is required by the EBI server.

```{r , fourthchunkBis, echo=TRUE , eval=TRUE  , message=FALSE , warning=FALSE}
lm <- alignSequences(lm , mail="lowmaca@gmail.com")
```

```{r , fifthchunck, echo=TRUE, eval=TRUE}
#Access to the slot alignment
myAlignment <- lmAlignment(lm)
str(myAlignment , max.level=2 , vec.len=2)
```

- ALIGNMENT: mapping from original position to the position in the consensus
- SCORE: some score of distance between the sequences
- CLUSTAL: an object of class *AAMultipleAlignment* as provided by the Biostrings R package
- df: Consensus sequence and conservation Trident Score at every position

## Get Mutations and Map Mutations

```{r , sixthchunk, echo=TRUE , eval=TRUE}
lm <- getMutations(lm)
lm <- mapMutations(lm)
```

These commands produce a change in the slot mutation and provide the results from R cgdsr package.

```{r , seventhchunk2, echo=TRUE,eval=TRUE}
#Access to the slot mutations
myMutations <- lmMutations(lm)
str(myMutations , vec.len=3 , max.level=1)
```


- data: provide the mutations selected from the cBioPortal divided by gene and patient/tumor type
- freq: a table containing the absolute number of mutated patients by gene and tumor type (this is useful to explore the mutational landscape of your genes in the different tumor types)
- aligned: a matrix of m rows, proteins or pfam, and n columns, consensus positions derived from the mapping of the mutations from the original positions to the new consensus 

If we want to check what are the most represented genes in terms of number of mutations divided by tumor type, we can simply run:

```{r , seventhchunk, echo=TRUE,eval=TRUE}
myMutationFreqs <- myMutations$freq
#Showing the first genes
myMutationFreqs[ , 1:10]
```

This can be useful for a stratified analysis in the future.

## Whole setup

To simplify this setup process, you can use directly the command setup to launch alignSequences, getMutations and mapMutations at once

```{r , eighthchunk, echo=TRUE , eval=FALSE , message=FALSE , warning=FALSE}
#Local Installation of clustalo
lm <- setup(lm)
#Web Service
lm <- setup(lm , mail="lowmaca@gmail.com")
```

## Custom Data

If you have your own data and you don't need to rely on the cgdsr package, 
you can use getMutations or setup method with the parameter repos, like this:

```{r , ninthchunk_pre , echo=TRUE , eval=TRUE}
#Reuse the downloaded data as a toy example
myOwnData <- myMutations$data
#How myOwnData should look like for the argument repos
str(myMutations$data , vec.len=1)
#Read the mutation data repository instead of using cgdsr package
#Following the process step by step
lm <- getMutations(lm , repos=myOwnData)
#Setup in one shot
lm <- setup(lm , repos=myOwnData)
```

# Statistics

In this step we calculate the general statistics for the entire consensus profile

```{r , tenthchunk, echo=TRUE , eval=TRUE}
lm <- entropy(lm)
#Global Score
myEntropy <- lmEntropy(lm)
str(myEntropy)
#Per position score
head(myAlignment$df)
```

With the method entropy, we calculate the entropy score and a p-value against 
the null hypothesis that the mutations are distributed randomly across our consensus protein.
In addition, a test is performed for every position of the consensus and the 
output is reported in the slot *alignment*. The position 4 has a conservation 
score of 0.88 (highly conserved) and the corrected p-value is significant (q-value below 0.01).
There are signs of positive selection for the position 4. To retrieve the original mutations 
that generated that cluster, we can use the function lfm

```{r , eleventhchunk, echo=TRUE}
significant_muts <- lfm(lm)
#Display original mutations that formed significant clusters (column Multiple_Aln_pos)
head(significant_muts)
#What are the genes mutated in position 4 in the consensus?
genes_mutated_in_pos4 <- significant_muts[ significant_muts$Multiple_Aln_pos==4 , 'Gene_Symbol']
```

```{r , eleventh_2chunck , echo=TRUE}
sort(table(genes_mutated_in_pos4))
```

The position 4 accounts for mutations in 13 different genes. The most represented one is ISX (ISX\_HUMAN, Intestine-specific homeobox protein).

# Plot

## Consensus Bar Plot

```{r , echo=TRUE, eval=TRUE, results="hide"}
bpAll(lm)
```

This barplot shows all the mutations reported on the consensus sequence divided by protein/pfam domain

## LowMACA comprehensive Plot

```{r ,  echo=TRUE, eval=TRUE, results="hide"}
lmPlot(lm)
```

This four layer plot encompasses:

- The bar plot visualize before
- The distribution of mutations against the null hypothesis 
		(blue line) with orange bars representing a p-value below 0.05 for
		 that position and a red star for q-value below 0.05
- The Trident score distribution
- The logo plot representing the consensus sequence

## Protter plot

```{r , protterChunk, echo=TRUE, eval=TRUE, message=FALSE, warning=FALSE}
#This plot is saved as a png image on a temporary file
tmp <- tempfile(pattern = "homeobox_protter" , fileext = ".png")
protter(lm , filename=tmp)
```

```{r, out.width = "400px" , echo=FALSE , eval=TRUE}
knitr::include_graphics(tmp)
```

A request to the Protter server is sent and a png file is downloaded with the possible sequence structure 
of the protein and the significant positions colored in orange and red

# Data driven workflow

An alternative use of `r Rpackage("LowMACA")` consists in analysing all the Pfams and single sequences 
encompassed by a specific set of mutations. For example, it is possible to analyse mutations 
derived from a cohort of patients to see which Pfams and set of mutations are enriched, following the LowMACA statistics. 
The function *allPfamAnalysis* takes as input a *data.frame* or the name of a file which contains the set of mutations, 
analyse all the Pfams that are represented and reports all the significant mutations as output. 
Moreover, the function *allPfamAnalysis* analyses individually all the mutated genes and reports 
the significant mutations found by this analysis as part of the output.

```{r , allPfamAnalysis, eval=TRUE}
#Load Homeobox example
data(lmObj)
#Extract the data inside the object as a toy example
myData <- lmMutations(lmObj)$data
#Run allPfamAnalysis on every mutations
significant_muts <- allPfamAnalysis(repos=myData)
#Show the result of alignment based analysis
head(significant_muts$AlignedSequence)
#Show all the genes that harbor significant mutations
unique(significant_muts$AlignedSequence$Gene_Symbol)
#Show the result of the Single Gene based analysis
head(significant_muts$SingleSequence)
#Show all the genes that harbor significant mutations
unique(significant_muts$SingleSequence$Gene_Symbol)
```

The parameter *allLowMACAObjects* can be used to specify the name of the file where 
all the Pfam analyses will be stored (by default this information is not stored, 
because the resulting file can be huge, according to the size of the input dataset). 
In this case, all the analysed Pfams are stored as `r Rpackage("LowMACA")` objects 
and they can be loaded and analysed with the usual `r Rpackage("LowMACA")` workflow.

# Summary

Copy and paste on your R console and perform the entire analysis by yourself. You need Ghostscript to see all the plots.

```{r , summary, eval=FALSE , echo=TRUE}
library(LowMACA)
Genes <- c("ADNP","ALX1","ALX4","ARGFX","CDX4","CRX"
			,"CUX1","CUX2","DBX2","DLX5","DMBX1","DRGX"
			,"DUXA","ESX1","EVX2","HDX","HLX","HNF1A"
			,"HOXA1","HOXA2","HOXA3","HOXA5","HOXB1","HOXB3"
			,"HOXD3","ISL1","ISX","LHX8")
Pfam <- "PF00046"
lm <- newLowMACA(genes=Genes , pfam=Pfam)
lmParams(lm)$tumor_type <- c("skcm" , "stad" , "ucec" , "luad" 
	, "lusc" , "coadread" , "brca")
lmParams(lm)$min_mutation_number <- 0
lm <- setup(lm , mail="lowmaca@gmail.com")
lm <- entropy(lm)
lfm(lm)
bpAll(lm)
lmPlot(lm)
protter(lm)
```

# Session Information

```{r , info,echo=TRUE}
sessionInfo()
```