---
title: "Exercice 3"
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>


# Read metadata

- Go into the data/ directory and read in R the Toy_Metadata.txt file

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

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

<br>
Many comments are present in the file, they are all skipped by the default option *comment.char="#"* 


```{r}
metadata<-read.table("data/Toy_Metadata.txt",header=T)
```

```{r}
metadata
```

</div>

# Read ReadCounts data

- Control that all in metadata are present in data/Toy_STARcounts/ directory (using code !)

- Based on comments found in metadata, write a function that:
  - Take sample name as argument
  - Read STAR gene count file
  - skip first 4 lines
  - return counts from the 4th column
  
- Read all Sample file, to have at the end a matrix of count, matrix col.names should be Sample ID and matrix row.names should be Gene

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

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

<br>

First we control that all files in Toy_STARcounts directory can be found in metadata 


```{r}
# We list all file in data/Toy_STARcounts/
ReadCountsFiles<-list.files("data/Toy_STARcounts/",pattern = ".out.tab")
# We remove "ReadsPerGene.out.tab" from the name to only have sampleID
ReadCountsFiles<-gsub("ReadsPerGene.out.tab","",ReadCountsFiles)
# We control that all file in metadata are present:
metadata$SampleID %in% ReadCountsFiles
# We could test in we find a FALSE results:
any((metadata$SampleID %in% ReadCountsFiles)== FALSE)
```

Then we write our function the read data from a sample ID

```{r}
ReadCountFunction<-function(SampleID){
  # skip = 4 allows to skip 4 rows
  STARcounts<-read.table(paste("data/Toy_STARcounts/",SampleID,"ReadsPerGene.out.tab",sep=""),skip=4)
  # We take the column 4
  GeneCount<-STARcounts$V4
  # We name our vector of counts
  names(GeneCount)<-STARcounts$V1
  # We return our vector of counts
  return(GeneCount)
}
```

We then apply our function to all sampleID

```{r}
counts<-sapply(metadata$SampleID,ReadCountFunction)
```

We have indeed a matrix of counts

```{r}
class(counts)
head(counts)
```

</div>

# Read Log files

- Do the same as above for STAR Log file name Log.final.out (The file is a bit harder to read, you can use the option "fill" in the *read.table()* function)

- Plot the number of Uniquely mapped reads

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

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

<br>

```{r}
ReadLogFunction<-function(SampleID){
  # use fill to avoid error when data don't have 2 values
  STAR_Log_Data<-read.table(paste("data/Toy_STARcounts/Log/",SampleID,"Log.final.out",sep=""),sep="\t",header=F,fill=T)
  STAR_Log<-STAR_Log_Data$V2
  names(STAR_Log)<-STAR_Log_Data$V1
  # For better reading, we transform space and | character
  names(STAR_Log)<-gsub(" ","",names(STAR_Log))
  names(STAR_Log)<-gsub("\\|","",names(STAR_Log))
  return(STAR_Log)
}
```

Get Log for all files

```{r}
Logs<-sapply(metadata$SampleID,ReadLogFunction)
```

Plot data

```{r}
plot(log10(as.numeric(Logs["Uniquelymappedreadsnumber",])),ylab="log10 Uniquely Mapped Reads")
```


</div>

