---
title: "Tutorial 2: MR analysis"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Tutorial 2: MR analysis}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)
```


```{r setup}
library(gwasglue2)
library(TwoSampleMR)
library(ieugwasr)
devtools::load_all("../") # this was added just for development
```


The usecase in this tutorial will be performing a basic MR analysis, using the ``TwoSampleMR`` package.


We will perform an MR analysis of body mass index (exposure - ieu-a-2) against coronary heart disease (outcome - ieu-a-7).

Obtain the data

```{r}
x <- ieugwasr::tophits("ieu-a-2")$rsid
d1 <- ieugwasr::associations(variants = x, id = "ieu-a-2")
d2 <- ieugwasr::associations(variants = x, id = "ieu-a-7")
```

Obtain the metadata and create a metadata object

```{r}
m1 <- ieugwasr::gwasinfo( "ieu-a-2")
m2 <- ieugwasr::gwasinfo( "ieu-a-7")
meta1 <-create_metadata(m1)
meta2 <-create_metadata(m2)

```
Construct the Summary Sets and set the tool and metadata (it uses ieugwasr::gwasinfo() with the source and id arguments)

```{r}
sumset1 <- create_summaryset(d1, metadata=meta1)
sumset2 <- create_summaryset(d2, metadata=meta2)

```

Set the MR labels for each of your summary sets

```{r}
sumset1 <-setAttributes(sumset1, mr_label = "exposure")
sumset2 <-setAttributes(sumset2, mr_label = "outcome")

```


Check the Metadata and MR labels

```{r}
getMetadata(sumset1)
getAttributes(sumset1)

getMetadata(sumset2)
getAttributes(sumset2)
```


Create the DataSet object and Convert it to TwoSampleMR format

```{r}

  #  create dataset and convert it to mr format
summary_sets <- list(sumset1,sumset2)
 
    # create dataset
    dataset <- create_dataset(summary_sets, harmonise = TRUE, tolerance = 0.08, action = 1)  %>%
    # Convert dataset to TwoSampleMR format
   convertForTwoSampleMR(.)

```



Perform the MR analysis
```{r}
mr_result <-  merge(getData(dataset,1),getData(dataset,2), by = c("SNP", "mr_keep"))  %>%
  TwoSampleMR::mr(., method_list="mr_ivw")

mr_result

```
