---
title: "3.1 - Data input - Creating MAE object"
output: 
  html_document:
    self_contained: true
    number_sections: no
    theme: flatly
    highlight: tango
    mathjax: null
    toc: true
    toc_float: true
    toc_depth: 2
    css: style.css
  
bibliography: bibliography.bib    
vignette: >
  %\VignetteIndexEntry{"3.1 - Data input - Creating MAE object"}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

<br>

# Illustration of ELMER analysis steps

The example data set (`GeneExp`,`Meth`) is a subset of chromosome 1 data from TCGA LUSC and it is available with the ELMER package.

```{r, echo = FALSE,hide=TRUE, message=FALSE, warning=FALSE}
library(ELMER)
library(DT)
library(dplyr)
dir.create("result",showWarnings = FALSE)
library(BiocStyle)
```


ELMER analysis have 5 main steps which are shown in the next sections individually. 
And later the function `TCGA.pipe`, which is a pipeline combining all 5 steps and producing all results and figures, is presented. 

# Preparing data input

## Selection of probes within biofeatures

This step is to select HM450K/EPIC probes, which locate far from TSS (at least 2Kb away) 
These probes are called distal  probes.

Be default, this comprehensive list of  TSS annotated by ENSEMBL database,
which is programatically accessed using `r BiocStyle::Biocpkg("biomaRt")` to get its last version,
will be used to select distal probes. But user can use their 
own TSS annotation or add features such as H3K27ac ChIP-seq in a certain cell line, to select probes overlapping thoses features regions.


```{r, message=FALSE}
# get distal probes that are 2kb away from TSS on chromosome 1
distal.probes <- get.feature.probe(
  genome = "hg19", 
  met.platform = "450K", 
  rm.chr = paste0("chr",c(2:22,"X","Y"))
)
```

## Creation of a MAE object
```{r,eval=TRUE, message=FALSE}
library(MultiAssayExperiment)
library(ELMER.data)
data(LUSC_RNA_refined,package = "ELMER.data")
data(LUSC_meth_refined,package = "ELMER.data")
GeneExp[1:5,1:5]
Meth[1:5,1:5]
mae <- createMAE(
  exp = GeneExp, 
  met = Meth,
  save = TRUE,
  linearize.exp = TRUE,
  save.filename = "mae.rda",
  filter.probes = distal.probes,
  met.platform = "450K",
  genome = "hg19",
  TCGA = TRUE
)
as.data.frame(colData(mae)[1:5,])  %>% datatable(options = list(scrollX = TRUE))
as.data.frame(sampleMap(mae)[1:5,])  %>% datatable(options = list(scrollX = TRUE))
as.data.frame(assay(getMet(mae)[1:5,1:5]))  %>% datatable(options = list(scrollX = TRUE))
as.data.frame(assay(getMet(mae)[1:5,1:5])) %>% datatable(options = list(scrollX = TRUE))
```

## Using non-TCGA data

In case you are using non-TCGA data there are two matrices to be inputed, colData with the samples metadata and sampleMap, mapping for each column of the gene expression and DNA methylation matrices to samples. An simple example is below if the columns of the matrices have the same name.

```{r,eval=FALSE, message=FALSE}
library(ELMER)
# example input
met <- matrix(rep(0,15),ncol = 5)
colnames(met) <- c(
  "Sample1",
  "Sample2", 
  "Sample3",
  "Sample4",
  "Sample5"
)
rownames(met) <- c("cg26928153","cg16269199","cg13869341")

exp <- matrix(rep(0,15),ncol = 5)
colnames(exp) <- c(
  "Sample1",
  "Sample2", 
  "Sample3",
  "Sample4",
  "Sample5"
)
rownames(exp) <- c("ENSG00000073282","ENSG00000078900","ENSG00000141510")


assay <- c(
  rep("DNA methylation", ncol(met)),
  rep("Gene expression", ncol(exp))
)
primary <- c(colnames(met),colnames(exp))
colname <- c(colnames(met),colnames(exp))
sampleMap <- data.frame(assay,primary,colname)

distal.probes <- get.feature.probe(
  genome = "hg19", 
  met.platform = "EPIC"
)

colData <- data.frame(sample = colnames(met))
rownames(colData) <- colnames(met)

mae <- createMAE(
  exp = exp, 
  met = met,
  save = TRUE,
  filter.probes = distal.probes,
  colData = colData,
  sampleMap = sampleMap,
  linearize.exp = TRUE,
  save.filename = "mae.rda",
  met.platform = "EPIC",
  genome = "hg19",
  TCGA = FALSE
)
```


# Bibliography