---
title: "CHAP 4 - additional exercises"
author: "Valentine"
output: pdf_document
---

## EXO 1

Focusing on the region from base 10 million to 11 million along chromosome 2L, calculate the GC content across a sliding window of 5000 nucleotides (you can use the letterFrequencyInSlidingView functionn) and visualise it (see an example below). On that plot, show the mean GC content and highlight the regions that have a GC content > 0.5 and < 0.35.  

```{r message=FALSE}
library("BSgenome.Dmelanogaster.UCSC.dm2")
```

```{r}
chr2L_10to11M <- GRanges("chr2L", IRanges(start=10e+06,end=11e+06))
chr2L_10to11M_seq <- getSeq(Dmelanogaster,chr2L_10to11M)
```

```{r}
gc_content <- letterFrequencyInSlidingView(chr2L_10to11M_seq[[1]],5000,letters="GC", as.prob=TRUE)   
head(gc_content)
```

```{r}
plot(gc_content)
abline(h = mean(gc_content), col = "green")
abline(v = which(gc_content > 0.5), col = "red")
abline(v = which(gc_content < 0.35), col = "blue")
```

OU en partant de l'exercice 4.6  
```{r}
chr2L_range <- GRanges("chr2L", IRanges(1, seqlengths(Dmelanogaster)["chr2L"]))
chr2L_dna <- getSeq(Dmelanogaster, chr2L_range)
x <- subseq(chr2L_dna[[1]], 10*1e6, 11*1e6)
gc <- letterFrequencyInSlidingView(x, 5000, letters = "GC", as.prob = TRUE)
plot(gc)
```


## EXO 2

Visit the UniProt web page at https://www.uniprot.org/ and download the manually reviewed complete human proteome
(containing about 20500 proteins). Do to so, choose Proteomes on the main page, then search for Homo sapiens, select it
among the list of hits, choose the reviewed Swiss-Prot release, and download all entries in compressed fasta format.  
**Le fichier up.fasta.gz peut également être téléchargé àpdu google drive**  

Load the file into R. Beware though that these are protein sequences, composed of amino acids. Read the ?readDNAStringSet manual page to find a more appropriate function. How many sequences are there? What's the average. max and min length of the proteins?  

```{r message=FALSE}
library(Biostrings)
human_proteome <- readAAStringSet("up.fasta.gz")
```

```{r}
length(human_proteome)
mean(nchar(human_proteome))
min(nchar(human_proteome))
max(nchar(human_proteome))
```

Load the up_selected data from the rWSBIM1322 data (version 0.1.1 or later) and use it to create a subset of the object created above. To to so, you will need to match the protein identifiers from that vector and the names of the AAStringSet above.  

```{r message=FALSE}
library(rWSBIM1322)
data("up_selected")
```

```{r}
head(up_selected)
head(names(human_proteome))
```
Les identifiants dans up_selected correspondent à la partie entre || des noms dans human_proteome  
Modifier les noms de human_proteome (garder partie entre ||, supprimer le reste) pour avoir noms de la forme de ceux de up_selected :  
```{r}
n <- names(human_proteome)
nn <- sub("sp\\|","", n)
nnn <- sub("\\|.+$","", nn)
head(nnn)
```
sub("x","y") : substituer x par y  
| signifie ou dans R. Pour traiter comme tel, précéder de \\  
sub("sp\\|","", nm) : remplacer sp| par rien = suppprimer ce qu’il y avant l’identifiant d’intérêt  
sub(("\\|.+$","", nm2) : supprimer ce qu’il y a après l’identifiant d’intérêt   
.+$ : n’importe quel caractère répété un certain nombre de fois jusqu’à la fin de la chaine de caractères  

```{r}
sel <- nnn %in% up_selected
table(sel)
human_proteome_sel <- human_proteome[sel]
```

Among the proteins of interest (that where also found in the original data), how many match the following patterns: DDVF, DEVF EDVF or EEVF? Do you consider this to be a high number?  

The identification of patterns in sequences (not specifically biological sequences) is called pattern matching, which defines a whole syntax to define patterns. In particular, it is possible to define letter D or E as [DE]. To find the pattern of interest, use the str_detect function from the stringr package.  

```{r}
grep("DDVF", human_proteome_sel) 
grep("DEVF", human_proteome_sel)        
grep("EDVF", human_proteome_sel)
grep("EEVF", human_proteome_sel)
grep("[DE][DE]VF", human_proteome_sel)
```
