---
title: "Introduction to metabolighteR"
author:
  - name: Thomas Wilson
    affiliation: Department of Life Sciences, Aberystwyth University, UK
    email: tpw2@aber.ac.uk
output: BiocStyle::html_document
vignette: >
  %\VignetteIndexEntry{Introduction_to_metabolighteR}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

```{r, load, eval=TRUE, echo=FALSE}
suppressPackageStartupMessages(library(metabolighteR))
```


## Introduction

[MetaboLights](https://www.ebi.ac.uk/metabolights/) is a database for metabolomics experiments and associated information. The database allows users to deposit raw data, sample information, analysis protocols and metabolite annotation data.

metabolighteR provides easy access to publicly available MetaboLights studies *via* the MetaboLights RESTful API. Only API methods which retrieve data (`GET`) are supported by metabolighteR. 


## Installation

metabolighteR can be installed from CRAN or, for the latest development version, directly from GitHub using the `remotes` package.

```{r install_cran, echo=TRUE, eval=FALSE}
install.packages('metabolighteR')

remotes::install_github('aberHRML/metabolighteR')

library(metabolighteR)
```



## Querying the Repository

A list of all public study identification codes can be easily retrieved.

```{r, get_studies, echo=TRUE, eval=TRUE}
all_study_ids <- get_studies()

studies <- as.vector(all_study_ids$study)

head(studies)
```


Generate a summary table containing; Study ID, Study Title and Study Technology, for publicly available studies. 

```{r, get_study_info, echo=TRUE, eval=TRUE}
# For the first five studies

study_titles <- purrr::map(studies[1:5], get_study_title)
names(study_titles) <- studies[1:5]

study_titles <- tibble::as_tibble(study_titles) %>% tidyr::gather()
names(study_titles) <- c('STUDY', 'Title')

study_tech <- get_study_tech()

study_tech_filter <- study_tech %>% dplyr::filter(STUDY %in% studies[1:5])

StudyInfoTable <-
  dplyr::left_join(study_titles, study_tech_filter, by = 'STUDY')
```



```{r}
StudyInfoTable

```


## Download File Contents

A list of all available files can be generated using the `get_study_files` function.

```{r, file_list, echo=TRUE, eval=TRUE}

studyFileList <- get_study_files('MTBLS264')

studyFileList
```

The contents of these files can then be downloaded using the `download_file` function.

```{r, download_file, echo=TRUE, eval=TRUE}

fileContents_A <- download_study_file('MTBLS264', studyFileList$file[1])

head(fileContents_A)

fileContents_B <- download_study_file('MTBLS264', studyFileList$file[4])

head(fileContents_B)


```




















