---
title: "Getting started with wrProteo"
author: Wolfgang Raffelsberger
date: '`r Sys.Date()`'
output:
  knitr:::html_vignette:
    toc: true
    fig_caption: yes
  pdf_document:
    highlight: null
    number_sections: no
vignette: >
  %\VignetteIndexEntry{wrProteoVignette1}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---


## Introduction

This package contains a collection of various tools for Proteomics used at the [proteomics platform](https://www.igbmc.fr/en/plateformes-technologiques/translate-to-english-proteomique) of the [IGBMC](https://www.igbmc.fr).
To get started, we need to load the packages "[wrMisc](https://CRAN.R-project.org/package=wrMisc)" and 
this package ([wrProteo](https://CRAN.R-project.org/package=wrProteo)), both are available from CRAN.
The packages [wrGraph](https://CRAN.R-project.org/package=wrGraph) and [RColorBrewer](https://CRAN.R-project.org/package=RColorBrewer) get used internally by some of the functions from this package for (optional/improved) figures.
Furthermore, the Bioconductor package [limma](https://bioconductor.org/packages/release/bioc/html/limma.html) will be used internally for statistical testing.

If you are not familiar with [R](https://www.r-project.org) you may find many introductory documents on the official R-site
in [contributed documents](https://cran.r-project.org/other-docs.html) or under [Documentation/Manuals](https://cran.r-project.org/manuals.html).
Of course, numerous other documents/sites with tutorials exit.

The aim of package-vignettes is to provide additional information and show examples how the R-package concerned may be used, thus complementing the documentation given with _help()_ for each of the functions of the package. In terms of examples, frequent standard types of problems are preferred in a vignette. 
Nevertheless, most functions can be used in many other ways, for this you may have to check the various arguments via calling _help_ on the function of interest.
All R-code in this vigentte can be directly repeated by the user, all data used is provided with the package.

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

```{r install, echo=TRUE, eval=FALSE}
## if you need to install the packages 'wrMisc','wrProteo' and 'wrGraph' from CRAN :
install.packages("wrMisc")
install.packages("wrProteo")
## The package 'wrGraph' is not obligatory, but it allows making better graphs
install.packages("wrGraph")

## Installation of limma from Bioconductor
if(!requireNamespace("BiocManager", quietly=TRUE)) install.packages("BiocManager")
BiocManager::install("limma")
```

```{r setup, echo=FALSE, messages=FALSE, warnings=FALSE}
suppressPackageStartupMessages({
    library(wrMisc)
    library(wrProteo)
    library(wrGraph)
    library(knitr)
    library(rmarkdown) 
}) 
```

```{r setup2}
## Let's assume this is a fresh R-session
## Get started by loading the packages
library("knitr")
library("wrMisc")
library("wrProteo")
library("wrGraph")
# This is wrProteo version no :
packageVersion("wrProteo")
```

This way you can browse all vignettes available to [this package](https://CRAN.R-project.org/package=wrProteo) :
```{r Vigenttes1, echo=TRUE, eval=FALSE}
browseVignettes("wrProteo")
```
There you can find another vignette dedicated to the analysis of heterogenous spike-in experiments.


## Calculating Molecular Masses From Composition Formulas

Please note that molecular masses may be given in two flavours : Monoisotopic mass and average mass.
For details you may refer to [Wikipedia: monoisotopic mass](https://en.wikipedia.org/wiki/Monoisotopic_mass). 
Monoisotopic masses commonly are used in mass-spectrometry and will be used by default in [this package](https://CRAN.R-project.org/package=wrProteo).

Molecular (mono-isotopic) masses of the atomes integrated in this package were taken from [Unimod](http://www.unimod.org/masses.html). 
They can be easily updated, if in the future, (mono-isotopic) molecular masses will be determined with higher precision (ie more digits).


### Molecular masses based on (summed) chemical formulas

At this level (summed) atomic compositions are evaluated.
Here, the number of atoms has to be written _before_ the atom. Thus, '2C' means two atoms of carbon.
Empty or invalid entries will be by default returned as mass=0, a message will comment such issues. 
 
The mass of an electron can be assigned using 'e'.
```{r ChemFormMolMass1, echo=TRUE}
massDeFormula(c("12H12O", "HO", " 2H 1 Se, 6C 2N", "HSeCN", " ", "e"))

# Ignore empty/invalid entries
massDeFormula(c("12H12O", "HO", " 2H 1 Se, 6C 2N", "HSeCN"), rmEmpty=TRUE)

```

Using the argument _massTy_ one can switch from default _monoisotopic mass_ to _average mass_ :
```{r ChemFormMolMass2, echo=TRUE}
massDeFormula(c("12H12O", "HO", " 2H 1 Se, 6C 2N", "HSeCN"), massTy="aver")
```

### Molecular masses based on amino-acid sequence
The mass of these amino-acids can be used:
```{r AAseqMolMass, echo=TRUE}
AAmass()
```

Here the one-letter amino-acid code is used to descibre peptides or proteins.
```{r AAseqMolMass2, echo=TRUE}
## mass of peptide (or protein)
pep1 <- c(aa="AAAA",de="DEFDEF")
convAASeq2mass(pep1, seqN=FALSE)
```

&nbsp; 
&nbsp; 

***


# Working With Fasta(Files)

## Reading Fasta Files (from Uniprot)

This package contains a parser for Fasta-files allowing to separate different fields of meta-data like IDs, name and species of the respecive entries.
Here we will read a tiny example fasta-file (a collection of typical contaminants in proteomics) using `readFasta2()`.

```{r readFasta, echo=TRUE}
path1 <- system.file('extdata', package='wrProteo')
fiNa <-  "conta1.fasta.gz"
## basic reading of Fasta
fasta1 <- readFasta2(file.path(path1, fiNa))
str(fasta1)

## now let's read and further separate details in annotation-fields
fasta1b <- readFasta2(file.path(path1, fiNa), tableOut=TRUE)
dim(fasta1b)
```

Now we can check if some sequences entries appear twice. 
```{r treatFasta2, echo=TRUE}
dupEntry <- duplicated(fasta1)
table(dupEntry)
```

Let's remove the duplicated entry.
```{r treatFasta3, echo=TRUE}
fasta3 <- readFasta2(file.path(path1, fiNa), removeEntries="duplicated", tableOut=TRUE)
dim(fasta3)
```




## Writing Sequences As Fasta Files

Once we have modified a fasta we might want to save it again as fasta-formatted file.
This can be done using `writeFasta2()`.
This function has been tuned to write UniProt formatted headers (or as close as possible) with its default-settings.

```{r writeFasta1, echo=TRUE, eval=FALSE}
writeFasta2(fasta3, fileNa=file.path(tempdir(),"testWrite.fasta"))
```

.
&nbsp; 
&nbsp; 

***



# Analyzing Label-Free Quantitative Proteomics Data

### Label-free Quantitative Proteomics Introduction

Multiple algorithms and software implementations have been developed for quantitation label-free proteomics experiments (LFQ), 
in particular for extracted ion chromatograms (XIC). For more background information you may look at
[Wikipedia labell-free Proteomics](https://en.wikipedia.org/wiki/Label-free_quantification).

The tools presented here are designed for use with label-free XIC (ie LFQ) data. 
Several of the programs for extracting initial quantitations also allow getting spectral counting (PSM) data which can also get imported into R,
however their use is not further discussed in this vignette. 
In general it is preferable to use XIC for comparing peptde of protein quantities between different protein extracts/samples.


This package provides support for importing quantitation results from [Proteome Discoverer](https://www.thermofisher.com/order/catalog/product/OPTON-30812),
[MaxQuant](https://maxquant.org/), [Fragpipe](https://fragpipe.nesvilab.org), [Proline](https://www.profiproteomics.fr/proline/),
[MassChroQ](http://pappso.inrae.fr/bioinfo/masschroq/), [DIA-NN](https://github.com/vdemichev/DiaNN), [AlphaPept](https://github.com/MannLabs/alphapept), 
[Wombat-P](https://github.com/wombat-p) and [OpenMS](https://openms.de/).


All quantitation import functions offer special features for further separating annotation related information, like species, for later use.

In most common real-world cases people typically analyze data using only one quantitation algorithm/software.
Below in this vignette, we'll use only the quantitation data generated using MaxQuant (AlphaPept, DIA-NN, FragPipe, MassChroQ, OpenMS, ProteomeDiscoverer, Proline and Wombat-P are supported, too). 
The other vignette to [this package](https://CRAN.R-project.org/package=wrProteo) ("UPS-1 spike-in Experiments") shows in detail the import functions available for MaxQuant, ProteomeDiscoverer and Proline
and how further comparsions can be performed in bench-mark studies. 
All these import functions generate an equivalent output format, separating (selected) annotation data (\$annot) from normalized log2-quantitation data (\$quant)
and initial quantitation (\$raw).

Normalization (discussed [below](#Normalization) in more detail) is an important part of 'preparing' the data for subsequant analysis.
The import functions in this package allow performin an initial normalization step (with choice among multiple algorithims), too.
Further information about the proteins identifed can be considered during normalization: 
For example, it is possible to exclude contaminants like keratins which are frequently found among the higher abundant proteins which may potentially introduce bias at global normalization.

Technical replicates are very frequently produced in proteomics, they allow to assess the variability linked to repeated injection of the same material.
Biological replicates, however, make additional information accessible, allowing the interpretation of experiments in a more general way.



## Import From Dedicated Quantification Algorithms/Software    {#ImportQuantitation}

### MaxQuant: Import Protein Quantification Data    {#ReadMaxQuant}

[MaxQuant](https://maxquant.org/) is free software provided by the [Max-Planck-Institute](https://www.biochem.mpg.de/de), 
see also [Tyanova et al 2016](https://doi.org/10.1038/nprot.2016.136). 
Typically [MaxQuant](https://maxquant.org/) exports by default quantitation data on level of consensus-proteins as a folder called txt with a file always called 'proteinGroups.txt'. 
Data exported from [MaxQuant](https://maxquant.org/) can get imported (and normalized) using `readMaxQuantFile()`,
in a standard case one needs only to provide the path to the file 'proteinGroups.txt' which can be found the _combined/txt/_ folder produced by MaxQuant. 
gz-compressed files can be read, too (as in the example below the file 'proteinGroups.txt.gz').
The argument _specPref_ allows giving further details about expected (primary) species, it defaults to working with human proteins.
To get started, let's just set it to _NULL_ for ignoring.

```{r readMaxQuant1, fig.height=8, fig.width=9.5, fig.align="center", echo=TRUE} 
path1 <- system.file("extdata", package="wrProteo")
dataMQ <- readMaxQuantFile(path1, specPref=NULL, normalizeMeth="median")
## number of lines and columns of quantitation data
dim(dataMQ$quant)
```

##### Adding Meta-Data at Import (Example MaxQuant)    {#ReadMaxQuantWithMetaData}

Similarly we can also add directly information about principal species, contaminants, special groups of proteins and add sdrf annotation (if existing) directly when reading the data.
Setting customized tags according to species or other search-terms can be done using the argument _specPref_.
In the example below we define a main species (tags are made by comparing to the species information initially given by the fasta)
and we define a custom group of proteins by their Uniprot-Accessions (here the UPS1 spike-in).
Then, the content of argument _specPref_ will get searched in multiple types of annotation (if available from the initial Fasta).

By setting _suplAnnotFile=TRUE_ the import function will also look for files (by default produced by MaxQuant as 'summary.txt' and 'parameters.txt')
giving more information about experiment and samples and integrate this to the output.
(This time let's do not display the plot of distributions, it's the same plot as above, see argument _plotGraph_.)

```{r readMaxQuant2, fig.height=8, fig.width=9.5, fig.align="center", echo=TRUE}
## The grouping of replicates
grp9 <- rep(1:9, each=3)
head(grp9)

## special group of proteins (we want to differentiate/ highlight lateron)
specPrefMQ <- list(conta="CON_|LYSC_CHICK", mainSpecies="OS=Saccharomyces cerevisiae", 
  spike=getUPS1acc()$ac)

dataMQ <- readMaxQuantFile(path1, specPref=specPrefMQ, suplAnnotFile=TRUE, 
  groupPref=list(lowNumberOfGroups=FALSE), gr=grp9, plotGraph=FALSE)

## the quantifiation data is the same as before
dim(dataMQ$quant)
```

Now we can access special tags in the annotation part of the resulting object the results :

```{r readMaxQuant3,  echo=TRUE}
## count of tags based on argument specPref
table(dataMQ$annot[,"SpecType"])
```

This information can be used automatically lateron for assigning different symbols and/or colors when drawing Volcano-plots or PCA.


##### Adding Experimental Setup (Sdrf) to Meta-Data at Import (Example MaxQuant)

To further analyze the data from an experiment typically the user also need to know/declare different groups of samples (eg who is replicate of whom).
In the simplest case this can be done via the argument _gr_, as shown above.
By the way, if _gr_ is provided it gets priority over other automcatic mining results.

The import-functions from this package try to help you in multiple ways to find out more about the experimental details.
Most quantitation software (like MaxQuant and ProteomeDiscoverer) also produce files/documentation about experimental annotation specified by the user.
These files may be automatically read and mined via argument _suplAnnotFile=TRUE_ to gather information about groups of samples.

The project [Proteomics Sample Metadata Format](https://github.com/bigbio/proteomics-sample-metadata) aims to provide a framework
of providing a uniform format for documenting experimental meta-data ([sdrf](#ImportSdrf)).
If sfdr-annotation (see [Proteomics Sample Metadata Format](https://github.com/bigbio/proteomics-sample-metadata)) exists on [Pride](https://www.ebi.ac.uk/pride/), it can be imported, too.
The information on the experimental setup will be mined to automatically to design groups of samples (ie levels of covariant factors).
If sdrf has not been prepared, the user may also simply provide a data.frame formatted like sfdr from Pride.

Finally, if nothing of the above is available, the column-names from the quantitation columns will be minded to search hints about groups of replicates (in particular when using MaxQuant).

For a bit more complex example of using _readMaxQuantFile()_ or integrating other annotation information,
please look at the vignette "UPS1 spike-in Experiments" also available to [this package](https://CRAN.R-project.org/package=wrProteo).

The simplest way of adding sdrf annotation consists in addin the project ID from [Pride](https://www.ebi.ac.uk/pride/), as shown below.
The argument _groupPref_ allows defining further adjustments/choices.
The import-function will first check if this a local file, and if not try to download from Pride (if available) and further mine the information.


```{r readMaxQuant4,  echo=TRUE}
path1 <- system.file("extdata", package="wrProteo")
specPrefMQ <- list(conta="CON_|LYSC_CHICK", mainSpecies="OS=Saccharomyces cerevisiae", 
  spike=getUPS1acc()$ac)

dataMQ <- readMaxQuantFile(path1, specPref=specPrefMQ, sdrf="PXD001819", suplAnnotFile=TRUE,
  groupPref=list(lowNumberOfGroups=FALSE), plotGraph=FALSE)
```


##### Exporting Experimental Setup from MaxQuant to Draft-Sdrf   {#ExportDraftSdrf}

As mentioned, the [Proteomics Sample Metadata Format - sdrf](https://github.com/bigbio/proteomics-sample-metadata) is an effort for standardizing experimental meta-data.
Many of the typically documented ones may already have been entered when lauching [MaxQuant](https://maxquant.org/) and can be exported as a draft Sdrf-file.
All main columns for standard experiments are present in the file, though some columns will have to be completed by the user (by any text-editor) for submitting to Pride.

```{r exportSdrfDraftMaxQuant5,  echo=TRUE}
path1 <- system.file("extdata", package="wrProteo")
fiNaMQ <- "proteinGroups.txt.gz"
dataMQ2 <- readMaxQuantFile(path1, file=fiNaMQ, refLi="mainSpe", sdrf=FALSE, suplAnnotFile=TRUE)
## Here we'll write simply in the current temporary directory of this R-session
exportSdrfDraft(dataMQ2, file.path(tempdir(),"testSdrf.tsv"))
```



#### MaxQuant : Import Peptide Data   {#ReadMaxQuantPetides}

Similarly it is possible to read the file by default called _'peptides.txt'_ for the peptide-data.
In the example below we'll provide a custom file-name (to a tiny example non-representative for biological interpretation).
The data get imported to a similar structure like the protein-level data, quantitations on peptide level by default median-normalized, sample-setup from sdrf-files may be added, too.

```{r readMaxQuantPeptides,  echo=TRUE}
MQpepFi1 <- "peptides_tinyMQ.txt.gz"
path1 <- system.file("extdata", package="wrProteo")
specPref1 <- c(conta="conta|CON_|LYSC_CHICK", mainSpecies="YEAST", spec2="HUMAN")
dataMQpep <- readMaxQuantPeptides(path1, file=MQpepFi1, specPref=specPref1, tit="Tiny MaxQuant Peptides")
summary(dataMQpep$quant)
```

If the argument _suplAnnotFile_ is set to _TRUE_, the files 'summary.txt' and 'parameters.txt' (produced by MaxQuant by default) will be searched in the same directory.
If these files are available and seem to correspond to the quantiation date read in the main part of the function,
supplemental information about experimental setup will be mined and added to the resulting object.

.



### ProteomeDiscoverer : Import Protein Quantification   {#ReadProteomeDiscoverer}

[Proteome Discoverer](https://www.thermofisher.com/order/catalog/product/OPTON-30812) is commercial software from ThermoFisher (www.thermofisher.com),
see also [Orsburn, 2021](https://doi.org/10.3390/proteomes9010015).
Data exported from [Proteome Discoverer](https://www.thermofisher.com/order/catalog/product/OPTON-30812) can get imported (typically the *xx_Proteins.txt* file)
using `readProteomeDiscovererFile()`, for details please see the vignette "UPS-1 spike-in Experiments" also available with [this package](https://CRAN.R-project.org/package=wrProteo).
The example below is just a toy data-set, normally one can identify and quantify many more proteins.


```{r readProteomeDiscovererProt1,  echo=TRUE}
path1 <- system.file("extdata", package="wrProteo")
fiNa <- "tinyPD_allProteins.txt.gz"
dataPD <- readProteomeDiscovererFile(file=fiNa, path=path1, suplAnnotFile=FALSE, plotGraph=FALSE)
summary(dataPD$quant)
```

Please note, that quantitation data exported from ProteomeDiscoverer frequently have very generic column-names (increasing numbers).
When calling the import-function they can be replaced by more meaningful names either using the argument _sampNa_
(thus, much care should be taken on the order when preparing the vector _sampleNames_ !),
or from reading the default annotation in the file _'InputFiles.txt'_ (if exported) or, from sdrf-annotation (if available).
In this case, supplemental information about experimental setup will be mined and added to the resulting object.

As described with [MaxQuant](#ReadMaxQuant), additional meta-data as [sdrf](#ImportSdrf) can be imported in the same way.
For a more complex example of using _readProteomeDiscovererFile()_ please see the vignette _'UPS1 spike-in Experiments'_ of [this package](https://CRAN.R-project.org/package=wrProteo).


#### ProteomeDiscoverer : Import Peptide Data       {#ReadProteomeDiscovererPetides}

Similarly it is possible to read the peptide-data files exported by ProteomeDiscoverer using the function `readProtDiscovererPeptides()`.
The data get imported to a similar structure like the protein-level data, quantitations on peptide level by default median-normalized, sample-setup from sdrf-files may be added, too.



### DIA-NN: Import Protein Quantification Data      {#ReadDiaNN}

[DIA-NN](https://github.com/vdemichev/DiaNN) is free software provided by the by Demichev, Ralser and Lilley labs,
see also [Demichev et al, 2020](https://doi.org/10.1038/s41592-019-0638-x).
Typically [DIA-NN](https://github.com/vdemichev/DiaNN) allows exporting quantitation data on level of consensus-proteins as tsv-formatted files.
Such data can get imported (and normalized) using `readDiaNNFile()`.
The example below is just a toy data-set, normally one can identify and quantify many more proteins.

```{r readDiaNN1, fig.height=8, fig.width=9.5, fig.align="center", echo=TRUE}
diaNNFi1 <- "tinyDiaNN1.tsv.gz"
## This file contains much less identifications than one may usually obtain
path1 <- system.file("extdata", package="wrProteo")
## let's define the main species and allow tagging some contaminants
specPref1 <- c(conta="conta|CON_|LYSC_CHICK", mainSpecies="HUMAN")
dataNN <- readDiaNNFile(path1, file=diaNNFi1, specPref=specPref1, tit="Tiny DIA-NN Data", plotGraph=FALSE)
summary(dataNN$quant)
```


#### DIA-NN : Import Peptide Data                  {#ReadDiaNNPetides}

Similarly data from [DIA-NN](https://github.com/vdemichev/DiaNN) on peptide level can get imported (and normalized) using `readDiaNNPeptides()`.




### Proline : Import Protein Quantification Data         {#ReadProline}

[Proline](https://www.profiproteomics.fr/proline/) is free software provided by the Profi-consortium,
see also [Bouyssi&eacute; et al 2020](https://doi.org/10.1016/j.jprot.2015.11.011).
Data exported from [Proline](https://www.profiproteomics.fr/proline/) (xlsx, csv or tsv format) can get imported using `readProlineFile()`.
The example below is just a toy data-set, normally one can identify and quantify many more proteins.


```{r readProlineProt1,  echo=TRUE}
path1 <- system.file("extdata", package="wrProteo")
fiNa <- "exampleProlineABC.csv.gz"                  # gz compressed data can be read, too
dataPL <- readProlineFile(file=fiNa, path=path1, plotGraph=FALSE)
summary(dataPL$quant[,1:8])
```

As described with [MaxQuant](#ReadMaxQuant), additional meta-data as [sdrf](#ImportSdrf) can be imported in the same way.
For a more complex example of using _readProlineFile()_ please see the vignette _'UPS1 spike-in Experiments'_ from [this package](https://CRAN.R-project.org/package=wrProteo).



### Fragpipe : Import Protein Quantification Data       {#ReadFragpipe}

[Fragpipe](https://fragpipe.nesvilab.org) is a database search tool for peptide identification, open-source developed by the [Nesvizhskii lab](https://www.nesvilab.org),
see eg  [Kong et al 2017](https://doi.org/10.1038/nmeth.4256), [da Veiga Leprevost; et al 2020](https://doi.org/10.1038/s41592-020-0912-y) or other related publications.
Data exported from [Fragpipe](https://fragpipe.nesvilab.org)  (in tsv format) can get imported using `readFragpipeFile()`.
The example below is just a toy data-set, normally one can identify and quantify many more proteins.


```{r readFragpipe1,  echo=TRUE}
FPproFi1 <- "tinyFragpipe1.tsv.gz"
## let's define the main species and allow tagging some contaminants
specPref1 <- c(conta="conta|CON_|LYSC_CHICK", mainSpecies="MOUSE")
dataFP <- readFragpipeFile(path1, file=FPproFi1, specPref=specPref1, tit="Tiny Fragpipe Example", plotGraph=FALSE)
summary(dataFP$quant)
```

As described with [MaxQuant](#ReadMaxQuant), additional meta-data as [sdrf](#ImportSdrf) can be imported in the same way.



### MassChroQ : Import Protein Quantification Data

[MassChroQ](http://pappso.inrae.fr/bioinfo/masschroq/) is free open software provided by the [PAPPSO](http://pappso.inrae.fr),
see also [Valot et al 2011](https://doi.org/10.1002/pmic.201100120).
Inital quantifications are on peptide basis and should be normalized and summarized using the R-package MassChroqR, which is also publicly available at the [PAPPSO](http://pappso.inrae.fr/bioinfo/).
Quantifications at protein-level can be saved as matrix into an RData-file or written to tsv, csv or txt files for following import into the framework of this package
using `readMassChroQFile()`, for details please see the help-page to this function.
The example below is just a toy data-set, normally one can identify and quantify many more proteins.


```{r readMassChroq1,  echo=TRUE}
MCproFi1 <- "tinyMC.RData"
dataMC <- readMassChroQFile(path1, file=MCproFi1, tit="Tiny MassChroq Example", plotGraph=FALSE)
summary(dataMC$quant)
```

As described with [MaxQuant](#ReadMaxQuant), additional meta-data as [sdrf](#ImportSdrf) can be imported in the same way.


### AlphaPept : Import Protein Quantification Data       {#ReadAlphaPeptide}

[AlphaPept](https://github.com/MannLabs/alphapept) is a free open-source search tool for peptide identification created by the Mann-lab,
see eg [Strauss et al 2021](https://doi.org/10.1101/2021.07.23.453379).
Data exported from AlphaPept (in csv format) can get imported using `readAlphaPeptFile()`.
The example below is just a toy data-set, normally one can identify and quantify many more proteins.


```{r readAlphaPept1,  echo=TRUE}
APproFi1 <- "tinyAlpaPeptide.csv.gz"
## let's define the main species and allow tagging some contaminants
specPref1 <- c(conta="conta|CON_|LYSC_CHICK")
dataAP <- readAlphaPeptFile(path1, file=APproFi1, specPref=specPref1, tit="Tiny AlphaPept Example", plotGraph=FALSE)
summary(dataAP$quant)
```

As described with [MaxQuant](#ReadMaxQuant), additional meta-data as [sdrf](#ImportSdrf) can be imported in the same way.



### Ionbot : Import Peptide Identification And  Quantification Data       {#ReadIonbot}

[ionbot](https://ionbot.cloud/about) is a database search tool for protein and peptide identification, developed by the [ComPomics group](https://www.compomics.com/) headed by L. Martens,
see eg  [Kong et al 2017](https://doi.org/10.1101/2021.07.02.450686) or other related publications.
Peptide identification from [ionbot](https://ionbot.cloud/about) are typically complemeented by quantification using [FlashLFQ](https://doi.org/10.1021/acs.jproteome.7b00608). 
The resulting output in tsv format can get imported using `readIonbotPeptides()`.  
Since the output may not have complete protein annotation information, it is possible to complement annotations from the fasta-file (Uniprot-formatted) used for initial identifications (using the argument '_specPref_', not shown below).
The example below is just a tiny toy data-set, normally one can identify and quantify many more proteins.


```{r readIonbot1,  echo=TRUE}
path1 <- system.file("extdata", package="wrProteo")
fiIonbot <- "tinyIonbotFile1.tsv.gz"
datIobPep <- readIonbotPeptides(fiIonbot, path=path1)
summary(datIobPep$quant)
```

As described with [MaxQuant](#ReadMaxQuant), additional meta-data as [sdrf](#ImportSdrf) can be imported in the same way.



### Wombat-P : Import Protein Quantification Data       {#ReadWombatP}

[Wombat-P](https://github.com/wombat-p) is a free open-source search tool for peptide identification created by an Elixir-consortium,
see also [Bouyssie et al 2023](https://doi.org/10.1021/acs.jproteome.3c00636).
Data exported from Wombat-P (in csv format) can get imported using `readWombatNormFile()`.
The example below is just a toy data-set, normally one can identify and quantify many more proteins.


```{r readWombarP1,  echo=TRUE}
WBproFi1 <- "tinyWombCompo1.csv.gz"
## let's define the main species and allow tagging some contaminants
specPref1 <- c(conta="conta|CON_|LYSC_CHICK", mainSpecies="YEAST")
dataWB <- readWombatNormFile(path1, file=WBproFi1, specPref=specPref1, tit="Tiny Wombat-P Example", plotGraph=FALSE)
summary(dataWB$quant)
```

As described with [MaxQuant](#ReadMaxQuant), additional meta-data as [sdrf](#ImportSdrf) can be imported in the same way.



### OpenMS : Import Protein Quantification Data

[OpenMS](https://openms.de/) is free open software provided by the deNBI Center for integrative Bioinformatics,
see also [Rost et al 2016](https://doi.org/10.1038/nmeth.3959).
Peptide level data exported as csv get summarized from peptide to protein level and further normalized using `readOpenMSFile()`, for details please see the help-page to this function.



### Importing Sdrf Meta-Data                       {#ImportSdrf}

The project [Proteomics Sample Metadata Format](https://github.com/bigbio/proteomics-sample-metadata) aims to provide a framework
of providing a uniform format for documenting experimental meta-data (sdrf-format).

As mentioned at the section for reading [MaxQuant](#ReadMaxQuant), most import-functions
from [wrProteo](https://CRAN.R-project.org/package=wrProteo) can directly import (if available) the experimental setup from sdrf,
or from files produced using the various quantitation software (as shown with [MaxQuant](#ReadMaxQuant).

To do this separately, or if you need to read an alternative annotation file, you may use `readSampleMetaData()`.
If sdrf annotation is available on Pride/github this information can be read and directly integrated with software specific annotation using the import-functions shown above or as shown below.
Of course the user should always make sure the annotation really corresponds to current the experimental data !

When adding the quantitation-data using argument _abund_, the functions also checks if the number of samples fit and
tries to align the order of the meta-data to that of the quantitation data (based on the raw files), since they are not necessarily in the same order.

```{r readSampleMetaData2,  echo=TRUE}
MQsdrf001819Setup <- readSampleMetaData(quantMeth="MQ", sdrf="PXD001819", path=path1, suplAnnotFile="summary.txt.gz", abund=dataMQ$quant)
names(MQsdrf001819Setup)
```

However, the recommended and most convenient way is to add/import meta-data directly when importing quantitation-data (eg using _readMaxQuantFile()_, _readProteomeDiscovererFile()_, etc).



## Removing Samples From Proteomics Projects   {#RemoveSamplesFromProject}

Sometimes the raw data were quantified with some additional control-samples still inlcuded, and it makes sense to remove them before further analysis.
The function 'removeSampleInList()' provides help to do so. Please note, that it might be beneficial re-renormalize the data (based on the raw data).

```{r removeSamplesFromProject1,  echo=TRUE}
path1 <- system.file("extdata", package="wrProteo")
fiNa <- "tinyPD_allProteins.txt.gz"
dataPD <- readProteomeDiscovererFile(file=fiNa, path=path1, suplAnnotFile=FALSE, plotGraph=FALSE)

dataPD2 <- removeSampleInList(dataPD, 2)

summary(dataPD$quant)
summary(dataPD2$quant)
```



## Combining Proteomics Projects   {#FuseProteomicsProjects}

If needed, the function `fuseProteomicsProjects()` allows combining up to 3 separate data-sets previously imported using wrProteo.
The user should very carefully think how and why he wants to fuse multiple separately imported data-sets, which might have their own charteristics.
Note, the function presented here does not re-normalize the combined data, the user should investigate the data and decide on suitable strategies
for further [normalization](#Normalization).

Data from different software may not contain exactely the same proteins or peptides, only the common identifiers are retained by this approach.
The user should pay attention to which identifyer should be used and that these identifyer do not appear multiple times in the the same data-set. 
If, however, some IDs appear multiple times (ie as separate lines) in the same data-set, the corresponding numeric data will be summarized to a single line.
This may may have noticable effect on the following biological interpretation.

Thus, it is very important to know your data and to understand that lines appearing with the same identifyers should/may be fused/summarized without 
doing damage to the later biological interpretation ! The user may specify for each dataset the colum out of the protein/peptide-annotation to use
via the argument _columnNa_. 
Then, this content will be matched as identical match, so when combining data from different software special care shoud be taken !

```{r fuseProteomicsProjects1,  echo=TRUE}
path1 <- system.file("extdata", package="wrProteo")
dataMQ <- readMaxQuantFile(path1, specPref=NULL, normalizeMeth="median", plotGraph=FALSE)
dataMC <- readMassChroQFile(path1, file="tinyMC.RData", tit="Tiny MassChroq Example", plotGraph=FALSE)
dataFused <- fuseProteomicsProjects(dataMQ, dataMC)
str(dataFused$quant)
```


## Normalization   {#Normalization}

As mentioned, the aim of normalization is to remove bias in data not linked to the original (biological) question.
The import functions presented above do already by default run global median normalization.
When choosing a normalization procedure one should reflect what additional information may be available to guide normalization.
For example, it may be very useful to exclude classical protein contaminants since they typically do not reflect the original biolocial material.
In overall, it is important to inspect results from normalization, graphical display of histograms, boxplots or violin-plots to compare distributions.
Multiple options exist for normalizing data, please look at the documentation provided with the import-functions introduced above.
Please note, that enrichment experiments (like IP) can quickly pose major problems to the choice of normalization approaches.
The function `normalizeThis()` from the package [wrMisc](https://CRAN.R-project.org/package=wrMisc) is run internally.
It can be used to run additional normalization, if needed.

Different normalization procedures intervene with different 'aggressiveness', ie also with different capacity to change the initial data.
In general, it is suggested to start normalizing using 'milder' procedures, like global median and switch to more intervening methods if initial results seem not satisfactory.
Beware, heavy normalization procedures may also alter the main information you want to analyze.
Ie, some biologically true positive changes may start to fade or disappear when inappropriate normalization gets performed.
Please note, that normalization should be performed before [NA-imputation](#NA-imputation) to avoid introducing new bias in the group of imputed values.


## Imputation of NA-values  {#NA-imputation}

In proteomics the quantitation of very low abundances is very challenging.
Proteins which are absent or very low abundances typically appear in the results as 0 or NA.
Frequantly this may be linked to the fact that no peak is detected in a MS-1 chromatogram (for a given LC elution-time) while other samples had a strong peak
at the respective place which led to successful MS-2 identification.
Please note, that the match-between-runs option in the various softwar options allows to considerably reduce the number of NAs.
To simplify the treatment all 0 values are transformed to NA, anyway they would not allow log2 transformation either.

Before replacing NA-values it is important to verify that such values may be associated to absent or very low abundances.
To do so, we suggest to inspect groups of replicate-measurements using `matrixNAinspect()`.
In particular, with multiple technical replicates of the same sample it is supposed that any variability observed is not linked to the sample itself.
So for each NA that occurs in the data we suggest to
look what was reported for the same protein with the other (technical) replicates.
This brings us to the term of 'NA-neighbours' (quantifications for the same protein in replicates).
When drawing histograms of NA-neighbours one can visually inspect and verify that NA-neighbours are typically low abundance values,
however, but not necessarily the lowest values observed in the entire data-set.

```{r NA_MaxQuant, echo=TRUE}
## Let's inspect NA values as graphic
matrixNAinspect(dataMQ$quant, gr=grp9, tit="Histogram of Protein Abundances and NA-Neighbours")
```

So only if the hypothesis of NA-neighbours as typically low abundance values gets confirmed by visual inspection of the histograms,
one may safely proceed to replacing them by low random values.

If one uses a unique (very) low value for NA-replacements, this will quickly pose a problem
at the level of t-tests to look for proteins changing abundance between two or more groups of samples.
Therefore it is common practice to draw random values from a Normal distribution representing this lower end of abundance values.
Nevertheless, the choice of the parameters of this Normal distribution is very delicate.

This package proposes several related strategies/options for NA-imputation.
First, the classical imputation of NA-values using Normal distributed random data is presented.
The mean value for the Normal data can be taken from the median or mode of the NA-neighbour values,
since (in case of technical replicetes) NA-neighbours tell us what these values might have been and thus we model a distribution around.
Later in this vignette, a more elaborate version based on repeated implementations to obtain more robust results will be presented.

The function `matrixNAneighbourImpute()` proposed in this package offers automatic selection of these parameters, which have been tested in a number of different projects.
However, this choice should be checked by critically inspecting the histograms of 'NA-neighbours' (ie successful quantitation in other
replicate samples of the same protein) and the final resulting distribution. Initially all NA-neighbours are extracted.
It is also worth mentioning that in the majority of data-sets encountered, such NA-neighbours form skewed distributions.

The successful quantitation of instances with more than one NA-values per group may be considered even more representative, but of course less sucessfully quntified values remain.
Thus a primary choice is made: If the selection of (min) 2 NA-values per group has more than 300 values, this distribution will be used as base to model
the distribution for drawing random values. In this case, by default the 0.18 quantile of the 2 NA-neighbour distribution will be used as mean for
the new Normal distribution used for NA-replacements. If the number of 2 NA-neighbours is >= 300, (by default) the 0.1 quantile all NA-neighbour values will used.
Of course, the user has also the possibility to use custom choices for these parameters.

The final replacement is done on all NA values. This also includes proteins with are all NA in a given condition as well a instances of mixed successful quantitation and NA values.

```{r NArepl_MaxQuant, echo=TRUE}
## MaxQuant simple NA-imputation (single round)
dataMQimp <- matrixNAneighbourImpute(dataMQ$quant, gr=grp9, tit="Histogram of Imputed and Final Data")
```

However, imputing using normal distributed random data also brings the risk of occasional extreme values.
In the extreme case it may happen that a given protein is all NA in one group, and by chance the random values turn out be rather high.
Then, the final group mean of imputed values may be even higher than the mean of another group with successful quantitations.
Of course in this case it would be a bad interpretation to consider the protein in question upregulated in a sample where all values for this protein were NA.
To circumvent this problem there are 2 options : 1) one may use special filtering schemes to exclude such constellations from final results or 2)
one could repeat replacement of NA-values numerous times.

The function _testRobustToNAimputation()_ allows such repeated replacement of NA-values. For details, see also the following section.

For other packages dealing with missing values (NAs), please also look at the [missing data task-view](https://CRAN.R-project.org/view=MissingData) on CRAN.


## Filtering         {#Filtering}

The main aim of filtering in omic-data analysis is to remove proteins/genes/lines which are for obvious reasons not suitable for further analysis.
Invalid or low quality measures are not suitable for good results and may thus be removed.
Frequently additional information is used to justy the procedure of removing certain proteins/genes/lines.

One very common element in filtering is the observation that very low abundance measures are typically less precise than medium or high abundance values.
Thus, a protein/gene with all abundance measures at the very low end of the global scale may as well just seem to change abundance due to the elevated variance of low abundance measures.
However, most statitical tests are not very well prepared for elevated variance of low abundance measures.
In consequence, it is common to remove or disqualify such proteins/genes/lines which are at high risk of yielding false positive results.

In the context of proteomics the number of samples with NAs (non-quantified peptides/proteins) for a given protein/peptide represents also an interesting starting point.  If almost all values finally compared are a result of (random) imputation, any apparent change in abundanc of such proteins/peptides lay rather reflect rare stochastic events of NA-imputation.
Please note, that rather aggressive filtering may severly reduce the ability to identify on/off situations which very well may occur in most biological settings.

General filtering can be performed using `presenceFilt()` (from package [wrMisc](https://CRAN.R-project.org/package=wrMisc)).
Other filtering of proteins/peptides/lines based on the annotation (eg for hypothetical proteins etc) may done using _filterLiColDeList()_ (also from package [wrMisc](https://CRAN.R-project.org/package=wrMisc)).


Initial information for filtering is already collected by the import-functions (_readMaxQuantFile()_, readProteomeDiscovererFile()_, _readProlineFile()_, _readOpenMSFile()_ etc..).
Then information for filtering can be used by the function _combineMultFilterNAimput()_ which is integrated to _testRobustToNAimputation()_ (see section below) to conveniently include filtering-aspects.



## Statistical Testing        {#StatisticalTesting}

The [t-test](https://en.wikipedia.org/wiki/Student%27s_t-test) remains the main statistical test used, as in many other coases of omics, too.
Statistical testing in the context of proteomics data poses challenges similar to transcriptomics :
Many times the number of replicate-samples is fairly low and the inter-measurement variability quite high.
In some unfortunate cases proteins with rather constant quantities may appear as false positives when searching for proteins who's abundance changes between two groups of samples : If the apparent variability is by chance too low, the respective standard-deviations will be low and a plain t-test may give very enthusiastic p-values.
Besides stringent filtering (previous section of this vignette), the use of shrinkage when estimating the intra-group/replicate variance from the Bioconductor package [limma](https://bioconductor.org/packages/release/bioc/html/limma.html) turns out very helpful,
see also [Ritchie et al 2015](https://doi.org/10.1093/nar/gkv007).
In this package the function _eBayes()_ has been used and adopted to proteomics.

The function `testRobustToNAimputation()` allows running multiple cycles of NA-imputation and statistical testing with the aim of providing stable imputation and testing results.
It performs NA-imputation and statistical testing (after repeated imputation) between all groups of samples the same time (as it would be inefficient to separate these two tasks). The tests underneath apply shrinkage from the empirical Bayes procedure from the bioconductor package [limma](https://bioconductor.org/packages/release/bioc/html/limma.html).  In addition, various formats of multiple test correction can be directly added to the results : Benjamini-Hochberg FDR, local false discovery rate (lfdr, using the package [fdrtool](https://CRAN.R-project.org/package=fdrtool), see [Strimmer 2008](https://doi.org/10.1093/bioinformatics/btn209)), or modified testing by [ROTS](https://bioconductor.org/packages/release/bioc/html/ROTS.html), etc ...

The fact that a single round of NA-imputation may provoke false positives as well as false negatives, made it necessary to combine this (iterative) process of NA-imputation and subsequent testing in one single function.


```{r testRobustToNAimputation_MQ1, echo=TRUE}
path1 <- system.file("extdata", package="wrProteo")
dataMQ <- readMaxQuantFile(path1, specPref=NULL, normalizeMeth="median", plotGraph=FALSE)
grp9 <- sub("_.+","",colnames(dataMQ$quant))
## Impute NA-values repeatedly and run statistical testing after each round of imputations
testMQ <- testRobustToNAimputation(dataMQ, gr=grp9)

## Example of the data after repeated NA-imputation
head(testMQ$datImp[,1:6])

## Example of the BH-FDR testing results after repeated NA-imputation & testing
head(testMQ$BH[,1:6])
```


It is also possible to select only a few of all pariwise comparisons for testing

```{r testRobustToNAimputation_MQ2, echo=TRUE}
testMQ2 <- testRobustToNAimputation(dataMQ, gr=grp9,nLoop=9, useComparison=c("125amol-25000amol","500amol-25000amol","2500amol-25000amol"))

## Example of the data after repeated NA-imputation
head(testMQ2$datImp[,1:6])

## Example of the BH-FDR values after repeated NA-imputation & testing
head(testMQ2$BH)
```


## Data Exploration With Graphical Support       {#DataExploreGraphics}


### PCA                  {#PCA}

Brielfy, principal components analysis ([PCA](https://en.wikipedia.org/wiki/Principal_component_analysis)) searches to decompose the data along all the axises defined by all samples. Then, the axis-combinations with the highest degree of correlation are searched.
In principle one could also run PCA along the rows, ie the proteins, but their number is typically so high that the resultant plots get too crowded.

In the context of high throughput experiments, like proteomics, PCA allows to distinguish important information how the different samples are related (ie similar).
This covers of course the real differences between different biological conditions, but also additional bias introduced as (technical) artifacts.
Thus, such plots serve as well for quality control (in particular to identify outlyer-samples, eg due to degraded material) as well as for the biological interpretation.

Normally one could immediately check the normalized data by PCA before running statistical tests.
As stated in other places, PCA can't handle missing values (ie _NA_ ).
Thus, all proteins having one NA in just one sample won't be considered during PCA.
This would mask a significant number of proteins in numerous of proteomics experiments.
Thus, it may be preferable to run PCA after NA-imputation.
However, since in this package statistical testing was coupled to the repeated NA-imputation, it may be better to use the NA-imputations made for the statistical testing (in the section above).

Here we'll use the function `plotPCAw()` form the package [wrGraph](https://CRAN.R-project.org/package=wrGraph)

```{r PCA1MQ, fig.height=12, fig.width=9.5, fig.align="center", echo=TRUE}
# limit to UPS1
plotPCAw(testMQ$datImp, sampleGrp=grp9, tit="PCA on Protein Abundances (MaxQuant,NAs imputed)", rowTyName="proteins", useSymb2=0, silent=TRUE)
```

Please note, the vignette dedicated to spike-in experiments ("UPS-1 spike-in Experiments") presents a slightly different way of making PCA-plots for this specific type of experiment/data-set.


### MA-plot            {#MAplot}

MA-plots are mainly used for diagnostic purposes. Basically, an [MA-plot](https://en.wikipedia.org/wiki/MA_plot) displays the log-Fold-Change versus the average abundance.
We'll use the function `MAplotW()` from the package [wrGraph](https://CRAN.R-project.org/package=wrGraph).

```{r MAplot1, fig.height=6.5, fig.width=9.5, fig.align="center", echo=TRUE}
# By default this plots at the first of all pairwise questions
MAplotW(testMQ)
```

#####

Now for the second group of pair-wise comparisons, plus adding names of proteins passing threshold:
```{r MAplot2, fig.height=6.5, fig.width=9.5, fig.align="center", echo=TRUE}
res1 <- NULL
MAplotW(testMQ, useComp=2, namesNBest="passFC")
```


### Volcano-Plot        {#VolcanoPlot}

A [Volcano-plot](https://en.wikipedia.org/wiki/Volcano_plot_\(statistics\)) allows to compare the simple fold-change (FC) opposed to the outcome of a statistcal test.
Frequently we can obsereve, that a some proteins show very small FC but enthousiastic p-values and subsequently enthousiastic FDR-values.
However, generally such proteins with so small FC don't get considered as reliable results, therefore it is common practice to add an additional FC-threshold,
typically a 1.5 or 2 fold-change.


The number of proteins retained by pair-wise comparison :
```{r VolcanoPlot1MQ, fig.height=6.5, fig.width=9.5, fig.align="center", echo=TRUE}
## by default the first pairwise comparison is taken
## using the argument 'namesNBest' we can add names from the annotation
VolcanoPlotW(testMQ, useComp=2, namesNBest="passFDR")
```

Additional Note : Volcano-plots may also help identifying bias in the data, in particular, to the question if normalization gave satisfactory results.
Based on the hypothesis of no global change used for normalization, normally, one would expect about the same number of down-regulated as up-regulated proteins.

In fact, this experiment is somehow unusual since one set of samples got a strong increase in abundance for 48 UPS1 proteins while the other proteins remained constant.
Thus, on the global scale there may be a (small) imbalance of abundances and the global median will reflect this, which can create some bias.
So, in this special case it might be better to perform normalization only based on the yeast proteins (which are assumed as constant),
as it has been performed in the vignette 'UPS-1 spike-in Experiments', a vignette which is entirely dedicated to UPS1 spike-in experiments.


## Reporting Results

Tables with results can be either directed created using _VolcanoPlotW()_ or, as shown below, using the function `extractTestingResults()`.

For example, let's look at the first of the pair-wise comparisons (the Volcano-plot above shwed another pait-wise comparison):
The moderated t-test expressed as Benjamini-Hochberg FDR gave `r sum(testMQ$BH[,1] < 0.05,na.rm=TRUE)` proteins with
FDR < 0.05 for the comparison `r colnames(testMQ$BH)[1]`.
Since unfortunately many verly low fold-change instances are amongst the results, one should add an additional filter for too low FC values.
This is common practice in most omics analysis when mostly technical replicates are run and/or the number of replicates is rather low.


As  seen before, this is the code to prepare testing results ...
```{r prepareForResults_MQ, echo=TRUE, eval=FALSE}
path1 <- system.file("extdata", package="wrProteo")
dataMQ <- readMaxQuantFile(path1, specPref=NULL, normalizeMeth="median", plotGraph=FALSE)
grp9 <- sub("_.+","",colnames(dataMQ$quant))
testMQ <- testRobustToNAimputation(dataMQ, gr=grp9)
```


```{r results1, echo=TRUE}
res1 <- extractTestingResults(testMQ, compNo=1, thrsh=0.05, FCthrs=2)
```

After FC-filtering for 2-fold (ie change of protein abundance to double or half) `r nrow(res1)` proteins remain.

```{r results2, echo=TRUE}
knitr::kable(res1[,-1], caption="5%-FDR (BH) Significant results for 1st pairwise set", align="c")
```

Please note that the column-name 'BH' referrs to Benjamini-Hochberg FDR (several other options of multiple testing correction exist, too).
We can see that many UPS1 proteins are, as expected, among the best-ranking differential changes.
However, not all UPS1 proteins do show up in the results as expected, and furthermore, a number of yeast proteins
(however expected to remain constant !) were reported as differential, too.

The function _extractTestingResults()_ also allows to write the data shown above directly to a csv-file.

.


## Further Steps

In case of standard projects one typically would like to find out more about the biological context of the proteins retained at statistical analysis, their function and their interactors.
Such a list of significant proteins from a given project could be tested lateron for enrichment of [GO-functions](https://www.uniprot.org/help/gene_ontology) or for their inter-connectivity in PPI networks like [String](https://string-db.org).
There are multiple tools available on [Bioconductor](https://www.bioconductor.org) and [CRAN](https://cran.r-project.org) as well as outside of R to perform such analysis tasks.

In case of UPS1 spike-in experiments the subsequent analysis is different.
Suggestions for in depth-analysis of UPS1 spike-in are shown in the **vignette 'UPS-1 spike-in Experiments'** of [this package](https://CRAN.R-project.org/package=wrProteo).

.

***


# Protein Annotation

In most 'Omics' activities getting additional annotation may get sometimes a bit tricky.
In Proteomics most mass-spectrometry software will use the informaton provided in the Fasta-file as annotation (typically as provided from UniProt).
But this lacks for example chromosomal location information.
There are are many repositories with genome-, gene- and protein-annotation and most of them are linked, but sometimes the links get broken when
data-base updates are not done everywhere or are not followed by new re-matching.
The Fasta-files used initially for mass-spectrometry peak-identification
may be not completely up to date (sometimes gene- or protein-IDs do change or may even disappear) and thus will contribute to a certain percentage of entries hard to link.

Globally two families of strategies for adding annotation exist :

a) Strategies using online-based ressources for getting the most up-to-date information/annotation (like [biomaRt](https://bioconductor.org/packages/release/bioc/html/biomaRt.html)).
Despite the advantage of most up-to-date information there may be some downsides :
Interogating databases may require more time to run all queries via interet-connections and this strategy is vulnerable to broken links
(eg linked to the delay of updates between different types of databases that may need to get combined).
Furthermore, the results typically may change a bit when the queries get repeated (in particular when this contains hypothetical peptides, pseudogenes etc).
When combining multiple interconnected ressources it may be very tricky to document the precise version of all individual ressources used.

b) Strategies based on using (local copies of) defined versions of databases.
Frequently, these databases can get downloaded/installed locally and thus allow faster queries and guarantee of repeatability and comparability to other tools or studies.
A number of databases are available on [Bioconductor](https://www.bioconductor.org) formatted for R.
Besides, the tables from [UCSC](https://genome.ucsc.edu/cgi-bin/hgTables) are another option (which will be used here).
Note, that tracking version-numbers may be much easier using this approach based on defined versions of databases.
And finally results are 100% reproducible when the same versions much easier to document are used.


In the context of adding chromosomal annotation to a list of proteins here the following concept is developed :
Annotation-tables from [UCSC](https://genome.ucsc.edu/cgi-bin/hgTables) are available for a decent number of species and can be downloaded for conventient off-line search.
However, in the context of less common species we realized that the UniProt tables from UCSC had many times low yield in final matching.
For this reason we propose the slightly more complicated route that provided finally a much higher success-rate to find chromosomal locations for a list of UniProt IDs.
First one needs to acces/download from [UCSC](https://genome.ucsc.edu/cgi-bin/hgTables) the table corresponding to the species of question fields _'clade','genome','assembly'_).
For _'group'_  choose 'Genes and Gene Predictions' and for _'track'_ choose 'Ensembl Genes', as table choose 'ensGene'.
In addition, it is possible to select either the entire genome-annotation or user-specified regions.
In terms of 'output-format' one may choose 'GTF' (slightly more condensed, no headers) or 'all filds from selected table'.

The following strategy for adding genomic location data using this package is presented here :

Locate (& download) organism annotation from UCSC, read into R (_readUCSCtable()_ ) ->  from R export (non-redundant) 'enst'-IDs (still using _readUCSCtable()_ ),
get corresponding UniProt-IDs at UniProt site, save as file and import result into R (_readUniProtExport()_ ) -> (in R) combine with initial UCSC table (_readUniProtExport()_ ) .

The function `readUCSCtable()` is able to read such files downloaded from UCSC, compressed .gz files can be read, too (like in the example below).
In the example below we'll just look at chromosome 11 of the human genome - to keep this example small.

```{r readUCSC1, echo=TRUE}
path1 <- system.file("extdata", package="wrProteo")
gtfFi <- file.path(path1, "UCSC_hg38_chr11extr.gtf.gz")
UcscAnnot1 <- readUCSCtable(gtfFi)

# The Ensemble transcript identifyers and their chromosomal locations :
head(UcscAnnot1)
```

However, this annotation does not provide protein IDs. In order to obtain the corresponding protein IDs an additional step is required :
Here we will use the batch search/conversion tool from [UniProt](https://www.uniprot.org/id-mapping).
In order to do so, we can export directly from _readUCSCtable()_ a small text-file which can be fed into the UniProt batch-search tool.

```{r readUCSC2, echo=TRUE}
# Here we'll redo reading the UCSC table, plus immediatley write the file for UniProt conversion
#  (in this vignette we write to tempdir() to keep things tidy)
expFi <- file.path(tempdir(),"deUcscForUniProt2.txt")
UcscAnnot1 <- readUCSCtable(gtfFi, exportFileNa=expFi)
```

Now everything is ready to go to [UniProt](https://www.uniprot.org/id-mapping) for retrieving the corresponding UniProt-IDs.
Since we exported Ensemble transcript IDs (ENSTxxx), select converting from 'Ensembl Transcript' to 'UniProtKB'.
Then, when downloading the conversion results, choose tab-separated file format (compression is recommended), this may take several seconds (depending on the size).

It is suggested to rename the downloaded file so one can easily understand its content.
Note, that the function `readUniProtExport()` can also read .gz compressed files.
To continue this vignette we'll use a result which has been downloaded from [UniProt](https://www.uniprot.org/id-mapping) and renamed to 'deUniProt_hg38chr11extr.tab'.
One may also optionally define a specific genomic region of interest using the argument 'targRegion', here the entire chromosome 11 was chosen.

```{r readUniProt1, echo=TRUE}
deUniProtFi <- file.path(path1, "deUniProt_hg38chr11extr.tab")
deUniPr1 <- readUniProtExport(UniP=deUniProtFi, deUcsc=UcscAnnot1, targRegion="chr11:1-135,086,622")
str(deUniPr1)
```

The resulting data.frame (ie the column 'UniProtID') may be used to complement protein annotation after importing mass-spectrometry peak- and protein-identification results.
Obviously, using recent Fasta-files from UniProt for protein-identification will typically give better matching at the end.

You may note that sometimes Ensemble transcript IDs are written as 'enst00000410108' whereas at other places it may be written as 'ENST00000410108.5'.
The function _readUniProtExport()_ switches to a more flexible search mode stripping of version-numbers and reading all as lower-caps, if initial direct matching reveals less than 4 hits.

Finally, it should be added, that of course several other ways of retrieving annotation do exist, too.
For example, as mentioned above, [Bioconductor](https://www.bioconductor.org)) offers serval packages dedicated to gene- and protein-annotation.

.

***

# Appendix

## Acknowledgements

The author would like to acknowledge the support by the [IGBMC](https://www.igbmc.fr) (CNRS UMR 7104, Inserm U 1258, UdS), [CNRS](http://www.cnrs.fr/en), [Universit&eacute; de Strasbourg (UdS)](https://www.unistra.fr/fr) and [Inserm](https://www.inserm.fr).

All collegues from the [proteomics platform](https://www.igbmc.fr/en/plateformes-technologiques/translate-to-english-proteomique) at the [IGBMC](https://www.igbmc.fr) work very commited to provide high quality mass-spectrometry data (including some of those used here).
The author wishes to thank the [CRAN-staff](https://CRAN.R-project.org) for all their help with new entries and their efforts in maintaining this repository of R-packages.
Furthermore, many very fruitful discussions with colleages on national and international level have helped to improve the tools presented here.

Thank you for you interest. This package is constantly evolving, new featues/functions may get added to the next version.




## Session-Info

For completeness : The output from _sessionInfo()_ :

\small
```{r sessionInfo, echo=TRUE}
sessionInfo()
```
