# Exercice 3

# Read metadata

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

Many comments are present in the file, they are all skipped by the default option comment.char=“#”

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

    metadata

# 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

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

    # 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

    ## [1] TRUE TRUE TRUE TRUE TRUE TRUE

    # We could test in we find a FALSE results:
    any((metadata$SampleID %in% ReadCountsFiles)== FALSE)

    ## [1] FALSE

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

    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

    counts<-sapply(metadata$SampleID,ReadCountFunction)

We have indeed a matrix of counts

    class(counts)

    ## [1] "matrix" "array"

    head(counts)

    ##                    T6N2 T6N3 T6N4 T6S1 T6S3 T6S4
    ## ENSMUSG00000102693    0    0    0    0    0    0
    ## ENSMUSG00000064842    0    0    0    0    0    0
    ## ENSMUSG00000051951 1436 1389 2166 1434 1865 1514
    ## ENSMUSG00000102851    0    0    0    0    0    0
    ## ENSMUSG00000103377    0    5    1    2    0    0
    ## ENSMUSG00000104017    0    3    0    1    0    0

# 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

    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

    Logs<-sapply(metadata$SampleID,ReadLogFunction)

Plot data

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