---
title: "Chapitre 9 exercices supplémentaires"
author: "Armelle de le Court"
date: "23/05/2021"
output: pdf_document
---
```{r,message = FALSE}
library("SummarizedExperiment")
library("rWSBIM1322")
data(cptac_se)
class(cptac_se)
```

### Extract the quantitative information for the peptides AIGVLPQLIIDR, NLDAAPTLR and YGLNHVVSLIENKK for samples 6A_7 and 6B_8. Subsetting works as we have seen for data.frames in chapter 3.
```{r}
colnames(cptac_se)
cptac_se3 <- cptac_se[c("AIGVLPQLIIDR", "NLDAAPTLR","YGLNHVVSLIENKK"),c(1,5)]
cptac_se3
assay(cptac_se3)
```

### Look and interpret the experimental design stored in the sample metadata of this experiment. To help you out, you can also read its documentation.
```{r}
metadatac <- colData(cptac_se)
metadatac
```

### What is the average expression of LSAAQAELAYAETGAHDK in the groups 6A and 6B?
```{r}
mean(assay(cptac_se)["LSAAQAELAYAETGAHDK",1:3])
mean(assay(cptac_se)["LSAAQAELAYAETGAHDK",4:6])
```

### Calculate the average expression of all peptides belonging to protein P02753ups|RETBP_HUMAN_UPS for each sample. You can indentify which peptides to use by looking for that protein in the object’s rowData slot.
```{r}
rowData(cptac_se)
avg <- rowData(cptac_se)$Proteins== "P02753ups|RETBP_HUMAN_UPS"
min(which(avg == TRUE))
which(avg == TRUE)

pp <- cptac_se[c(2742,4048)]
pp
app <- assay(pp)
mean(app)
```

## Exercices with KEM

### Import the data from two tab-separated files into R. The full paths to the two files can be accessed with kem.tsv(). Read ?kem for details on the content of the two files. In brief, the kem_counts.tsv file contains RNA-Seq expression counts for 13 genes and 18 samples and kem_annot.tsv contains annotation about each sample. Read the data into two tibbles names kem and annot respectively and familiarise yourself with the content of the two new tables.
```{r,message = FALSE}
library(tidyverse)
library(dplyr)

kem <- read_tsv("/usr/local/lib/R/site-library/rWSBIM1207/extdata/kem_counts.tsv")
annot <- read_tsv("/usr/local/lib/R/site-library/rWSBIM1207/extdata/kem_annot.tsv")
annot
```

### Convert the counts data into a long table format and annotate each sample using the experimental design.
```{r}
kem_long <- kem%>% 
  pivot_longer(names_to = "sample_id", values_to="gene_expression",-ref)
```

### Identity the three transcript identifiers that have the highest expression count over all samples.
```{r}
# kem_long%>%
# top_n(3, counts)
```

### Visualise the distribution of the expression for the three transcripts selected above in cell types A and B under both treatments.
```{r}
full_join(kem_long, annot) %>%
  group_by(ref) %>% summarise(counts=sum(gene_expression)) %>%
  arrange(desc(counts))

full_join(kem_long, annot) %>%
  filter(ref %in% c("ENSG00000198804","ENSG00000198712","ENSG00000109971")) %>%
  ggplot(aes(x=treatment, y= gene_expression)) +
  geom_boxplot() +
  geom_jitter()+
  facet_grid(ref~cell_type)
```

### For all genes, calculate the mean intensities in each experimental group (as defined by the cell_type and treatment variables).
```{r}
full_join(kem_long, annot) %>%
  group_by(cell_type,treatment,ref) %>%
  summarise(m=mean(gene_expression))
```

### Focusing only on the three most expressed transcripts and cell type A, calculate the fold-change induced by the treatment. The fold-change is the ratio between the average expressions in two conditions.
```{r}
kem_long_annot <- full_join(kem_long, annot)

kem_long_annot2 <- kem_long_annot%>%
  select(ref, cell_type, sample_id, gene_expression, treatment) %>% 
  filter(ref %in% c("ENSG00000198804", "ENSG00000198712", "ENSG00000109971"),
         cell_type == "A")

kem_long_annot2

kem_long_annot2 %>% 
  na.omit() %>% 
  group_by(sample_id) %>% 
  pivot_wider(names_from = "treatment",
              values_from = "gene_expression",
              c(ref, cell_type, sample_id))
```


## EXERCICES AVEC SE.RDS

### download se.rds
```{r,message=FALSE}
se <- read_rds("data/se.rds")
se
```

### Explore the object’s colData and interprete it as the data’s experimental design.
```{r}
colData(se)
```

### Generate a figure similar to the one below for your data own data.(cfr dans le cours pour voir à quoi doit ressembler la figure)
```{r}
se_tibble <- as_tibble(cbind(assay(se),rowData(se))) %>% 
  pivot_longer(names_to = "samples",values_to="gene_expression", -c(gene,location)) 

se_tibble

full_join(as_tibble(colData(se)),se_tibble) %>% 
  ggplot(aes(x=group, y= gene_expression)) + geom_boxplot()+ facet_wrap(~gene)
```

### Calculate the average expression for each gene in each group.


