library(knitr)
opts_knit$set(root.dir = normalizePath(".."))
opts_chunk$set(cache = TRUE)
options(knitr.duplicate.label = "allow")

library(biomaRt)
library(yaml)
library(rlang)
library(dplyr)

devtools::load_all()
get_data <- function(recipe) {
    message(recipe$dataset)
    mart <- useMart(recipe$biomart, recipe$dataset, recipe$host)
    attr <- unlist(recipe$attributes, use.names = FALSE)
    getBM(attr, mart = mart)
}

tidy_data <- function(df, recipe) {
    df %>%
        as_tibble %>%
        distinct %>%
        rename(!!!syms(recipe$attributes)) %>%
        arrange(!!sym("ensgene"))
}

save_data <- function(..., name) {
    path <- file.path("data", paste0(name, ".rda"))
    objs <- setNames(list(...), name)
    save(list = name, file = path, envir = list2env(objs), compress = "xz")
}
recipe.files <- file.path("data-raw", "recipes") %>%
    dir(full.names = TRUE)
names(recipe.files) <- sub(".yml", "", basename(recipe.files))
recipes <- lapply(recipe.files, yaml.load_file)
# download gene tables
genetables <- lapply(recipes, get_data)

# tidy gene tables
genetables <- Map(tidy_data, genetables, recipes)

# export data
Map(save_data, genetables, name = names(genetables))

# document
Map(document_annotable, recipes, names(recipes), type = "gene")
names(recipes) <- paste(names(recipes), "tx2gene", sep = "_")

recipes <- lapply(recipes, function(x) {
    x$attributes <- c(
        enstxp = "ensembl_transcript_id",
        ensgene = "ensembl_gene_id")
    x
})

# download tx2gene
tx2gene <- lapply(recipes, get_data)

# tidy tx2gene
tx2gene <- Map(tidy_data, tx2gene, recipes)

# export data
Map(save_data, tx2gene, name = names(tx2gene))

# document
Map(document_annotable, recipes, names(recipes), type = "tx2gene")
ensembl_version <- listMarts() %>%
    filter(biomart == "ENSEMBL_MART_ENSEMBL") %>%
    pull(version)
save_data(ensembl_version, name = "ensembl_version")
rmarkdown::render("README.Rmd")


iliefadz/differentialpathways documentation built on May 28, 2019, 8:44 a.m.