---
title: "Chapitre 7 : joining tables"
author: "Armelle le Court"
date: "20/05/2021"
output: pdf_document
---

# 1 : COMBINING TABLES 

## load the package
```{r,message= FALSE}
library("rWSBIM1207")
library("dplyr")
```
## join the two tables
```{r}
data(jdf)
jdf1 #with acesssion nbr + organelle + entry
jdf2 #with description of the gene + accession nbr + species the prot comes from

#to join those two tables we need "dplyr", it will be joined thanks to uniprot 
full_join(jdf1,jdf2)
```
## if different names
```{r}
#in case none of the names match, we can set them manually using the by argument
#jdf1 and jdf3 have, for example, different names for uniprot 
names(jdf3) #here it is "UniProt", and jdf1 is "uniprot"
full_join(jdf1, jdf3, by = c("uniprot" = "UniProt"))
```
## QUESTION : Using the full_join function demonstrated above, join tables jdf4 and jdf5. What has happened for observations P26039 and P02468?
```{r}
names(jdf4)
names(jdf5)
#they both have same name for "uniprot"
full_join(jdf4,jdf5)
#P02468 and P02468 are only present in jdf4 and jdf5 respectively and their respective values for the variables of the table have been encoded as missing.
```

# 2 : DIFFERENT TYPES OF JOINS

### An inner join keeps observations that are present in both tables.A left join keeps observations that are present in the left (first) table, dropping those that are only present in the other.A right join keeps observations that are present in the right (second) table, dropping those that are only present in the other.A full join keeps all observations.

## QUESTIONS

```{r}
#Join tables jdf4 and jdf5, keeping only observations in jdf4.
left_join(jdf4,jdf5)
# Join tables jdf4 and jdf5, keeping only observations in jdf5.
right_join(jdf4,jdf5)
#Join tables jdf4 and jdf5, keeping only observations in jdf5.
full_join(jdf4,jdf5)
```

# 3 : MULTIPLES MATCHES

### jdf6 -> the accession number Q99PL5 is repeated twice

```{r}
jdf6
#If we now want to join jdf6 and jdf2, the variables of the latter will be duplicated.
full_join(jdf6,jdf2)
```
### In the case above, repeating is useful, as it completes jdf6 with correct information from jdf2. One needs however to be careful when duplicated keys exist in both tables. Below, we create an inner join between jdf6 and jdf7, both having duplicated Q99PL5 entries.
```{r}
inner_join(jdf6,jdf7)
```

# 4 : MATCHING ACCROSS MULTIPLES KEYS 

### we see that the duplicated UniProt accession numbers in the jdf6 and jdf7 tables refer to different isoforms of the same RRBP1 gene. To uniquely identify isoforms, we need to consider two keys, namely the UniProt accession number (named uniprot in both tables) as well as the isoform number, called isoform and isoform_num respectively.

```{r}
inner_join(jdf6, jdf7, by = c("uniprot" = "uniprot", "isoform" = "isoform_num"))
```
## QUESTION 
### Can you think of another way to merge tables jdf6 and jdf7 using the two keys?
```{r}
i <- grep("isoform", names(jdf7))
names(jdf7)[i] <- "isoform"
inner_join(jdf6, jdf7)
```

# 5 ROW AND COLUMN BINDING

## first we need to create the dataframes d1 and d2
```{r}
x <- c(1,2,3)
y <- c(1,2,3)
d1 <- data.frame(x,y)

a <- c(4,5)
b <- c(4,5)
d2 <- data.frame(a,b)
```
## the number of columns dont match so there is a error if we use try(rbind(d1,d2))
### now we know we have to match those names before rbinding
```{r}
names(d2) <- names(d1)
rbind(d1, d2)
```
### If we want to bind to dataframes along their columns
```{r}
# cbind(d2, d3) 
```
### rbind dataframes with identical column names but different variables, which end up all being coerced into characters
```{r}
d4 <- data.frame(x = letters[1:2], y = letters[1:2])
str(rbind(d1, d4))
```

# 6 : ADDITIONAL EXERCISES 

## 1 
```{r}
#?
```

## 2

### 2.1. : load packages 
```{r,message = FALSE}
#1st, we have to load the packages and datas
library(rWSBIM1207)
data("clinical2")
data("expression")
```

### 2.2. : Inspect the expression data. How many samples are recorded?
```{r}
str(expression)

expression %>%
  count(sampleID)
```

### 2.3. : Join the expression and clinical2 tables by the patient reference, using the left_join and the right_join functions. Why are the results different?
```{r}
names(clinical2)
names(expression)
head(clinical2)
head(expression)
#we have to join by the patientID and patient names
full_join(clinical2,expression, by = c("patientID" = "patient"))
```

### 2.4. : Join expression and clinical2 tables in order to create a table containing merged data exclusively for normal samples.
```{r}
expression_norm <- expression %>% 
  filter(type == "normal")

expression_norm
inner_join(clinical2, expression_norm, by = c("patientID" = "patient"))
```






