---
title: "Exercice 2"
output:
  html_document:
    df_print: paged
    toc: yes
    toc_float: yes
    highlight: tango
    theme: sandstone
    rows.print: 6
---


<script>
function myFunction(ID) {
    var x = document.getElementById(ID);
    if (x.style.display === "none") {
        x.style.display = "block";
    } else {
        x.style.display = "none";
    }
}
</script>


# Load data

- Load the 2 *.Rdata* files that you can find in the data/ directory. It contains gene expression data and metadata about experimental design and 3 mouse strains used (B6, D2, AK)
- Using *ls()* you can find the variables saved in these files
- Which type of variables are they ?

<button onclick="myFunction(ID='myDIV2')" style="background-color: red"><span style='color:white'>Show answer !</span></button>

<div id="myDIV2" style="display:none">

First we load the data:

```{r}
setwd("data")
load("Ex2.expression.Rdata")
load("Ex2.metadata.Rdata")
```

We then list all variables present in our environment:

```{r}
ls()
```

We see which type of variable are *expression* and *metadata*

```{r}
class(expression)
class(metadata)
```

Both of them are data.frame


</div>

# Inspect data

- Have a look at the data
- Describe them
- Can you see some problems using the *summary()* function ?

<button onclick="myFunction(ID='myDIV3')" style="background-color: red"><span style='color:white'>Show answer !</span></button>

<div id="myDIV3" style="display:none">


We can easly see that the order of sample ID in the metadata (rows) and in the expression data (columns) are not the same order. It can be problematic if we write some statistical test in R. 

```{r,rows.print=6,cols.print=6}
metadata
expression
```


Using summary, we don't see obvious problems. Strain, Condition and Tissue are in charactet, we should transform them into factors to maybe detect some typos

```{r}
summary(metadata)
```


Having a quick look at expression data, we see that GSM239937 and  GSM239879 are in character type while they should be numeric like the other

```{r}
summary(expression,digits = 2,quantile.type=2)
```


</div>

# Reformat

- Reformat the metadata and expression data that are needed
- Do you see new problems with the *summary()* function ?


<button onclick="myFunction(ID='myDIV4')" style="background-color: red"><span style='color:white'>Show answer !</span></button>

<div id="myDIV4" style="display:none">

We can transform some information in the metadata into factors:

```{r}
metadata$Strain<-as.factor(metadata$Strain)
metadata$Condition<-as.factor(metadata$Condition)
metadata$Tissu<-as.factor(metadata$Tissu)
```

Using a summary, we see better potential problems:

```{r}
summary(metadata)
```

Some samples are unknown mouse Strain, they will be removed later and we see a typo. One mouse was annotated "d2" instead of "D2". 

We have to rename it:

```{r}
metadata$Strain[which(metadata$Strain ==  "d2")]<-"D2"
```

For the experession data, we can see the 2 problematic recordings:

```{r}
expression$GSM239892
```

```{r}
expression$GSM239935
```

We see that the "NA" caused data to be saved as character, we can simply change them into numerical value:

```{r}
expression$GSM239892<-as.numeric(expression$GSM239892)
expression$GSM239935<-as.numeric(expression$GSM239935)
```

We can save our data into a matrix, if all data are numerical it should be transformed into a numerical matrix.

```{r}
expression<-as.matrix(expression)
```

All values are numerical now

```{r}
typeof(expression)
```


</div>

# Filter metadata

- Only keep the following strains: B6, D2 in your **metadata**

<button onclick="myFunction(ID='myDIV6')" style="background-color: red"><span style='color:white'>Show answer !</span></button>

<div id="myDIV6" style="display:none">

With the %in% operator, we ask which strain are B6 and D2 and only keep row which are TRUE

```{r}
idx<-metadata$Strain %in% c("B6","D2")
idx
metadata<-metadata[idx,]
```

</div>


# Reorder

- Filter accordingly the **expression** data.
- Metadata Samples ID and expression data samples should be in the same order


<button onclick="myFunction(ID='myDIV7')" style="background-color: red"><span style='color:white'>Show answer !</span></button>

<div id="myDIV7" style="display:none">

Directly reordering expression does not work, because 1 sample is missing

```{r,eval = F}
expression<-expression[,metadata$ID]
```

Keep only shared samples

```{r}
metadata<-metadata[metadata$ID %in% colnames(expression),]
expression<-expression[,metadata$ID]
```

We can do a control that they are in the same order:

```{r}
table(colnames(expression) == metadata$ID)
```


</div>

# Filter

- Filter low expressed genes with a mean less than 5

<button onclick="myFunction(ID='myDIV8')" style="background-color: red"><span style='color:white'>Show answer !</span></button>

<div id="myDIV8" style="display:none">

Some row contain NA value, we have to use the option na.rm=T to avoid them

```{r}
expression<-expression[rowMeans(expression,na.rm=T)>5,]
dim(expression)
```

</div>

# NA remove

- Remove all genes with empty gene expression value

<button onclick="myFunction(ID='myDIV9')" style="background-color: red"><span style='color:white'>Show answer !</span></button>

<div id="myDIV9" style="display:none">

With the function *is.na()* we can detect which value are NA. the option *arr.ind =T* will return the row and column with na.

```{r}
which(is.na(expression),arr.ind=T)
```

The gene on row 1 contains an empty value, we remove it.

```{r}
expression<-expression[-1,]
```

</div>

# Plot

- Plot the mean expression overall gene using *plot()*

<button onclick="myFunction(ID='myDIV10')" style="background-color: red"><span style='color:white'>Show answer !</span></button>

<div id="myDIV10" style="display:none">

```{r}
plot(rowMeans(expression))
```

</div>

# Save data

- Save metadata and expression data using the following command:

```{r}
save(metadata,expression,file="data/Ex2.Results.Rdata")
```

