Zettelkasten

top

getGenedataByEnsemblId <- function(ensemblIds = NULL, file.location = ".", hg_version = 38) {
  file.name <- file.path(file.location, paste0("genes_info_hg", hg_version, ".csv"))
  if (!file.exists(file.name)) {
    if (!("mart" %in% ls())) {
      args <- list(
        biomart = "ENSEMBL_MART_ENSEMBL",
        dataset = "hsapiens_gene_ensembl"
      )

      if (hg_version != 38) {
        args <- append(args, list(GRCh = hg_version))
      }

      assign("mart", do.call(biomaRt::useEnsembl, args))
    }

    args <- list(
      attributes = c(
        "hgnc_symbol",
        "ensembl_gene_id",
        "ensembl_transcript_id",
        "chromosome_name",
        "start_position",
        "end_position",
        "strand",
        "transcription_start_site",
        "transcript_start",
        "transcript_end",
        "external_gene_name"
      ),
      mart = mart
    )

    if (!is.null(ensemblIds)) {
      args <- append(args,
        list(
          filters = "ensembl_gene_id",
          values = as.character(ensemblIds)
        )
      )
    }
    gene.list <- do.call(biomaRt::getBM, args)
    readr::write_csv(gene.list, path = file.name)
  }
  return(
    readr::read_csv(
      file.name,
      col_types = readr::cols()
    )
  )
}