Created
February 11, 2014 15:33
-
-
Save aaronwolen/8937163 to your computer and use it in GitHub Desktop.
Download the latest GENCODE annotations file and export as a GRanges object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Download the latest GENCODE annotations file and export as a GRanges object | |
library(RCurl) | |
library(rtracklayer) | |
# Variables --------------------------------------------------------------- | |
# 'human' or 'mouse' | |
organism <- "human" | |
# output directory | |
out.dir <- "~/Downloads" | |
# Functions --------------------------------------------------------------- | |
ls_url <- function(url) { | |
stopifnot(url.exists(url)) | |
out <- getURL(url, ftp.use.epsv = FALSE, dirlistonly = TRUE) | |
readLines(textConnection(out)) | |
} | |
# Determine latest release ------------------------------------------------ | |
ftp.url <- paste0("ftp://ftp.sanger.ac.uk/pub/gencode/Gencode_", organism) | |
ftp.dirs <- ls_url(ftp.url) | |
ftp.dirs <- Filter(function(x) grepl("release", x), ftp.dirs) | |
releases <-sapply(strsplit(ftp.dirs, "_"), function(x) x[2]) | |
ftp.dirs <- ftp.dirs[order(as.numeric(sub("\\D+", "", releases)), | |
sub("\\d+", "", releases))] | |
message("Latest release is ", tail(ftp.dirs, 1)) | |
# Download latest annotation.gtf file ------------------------------------- | |
ftp.url <- paste0(ftp.url, "/", tail(ftp.dirs, 1), "/") | |
ftp.files <- ls_url(ftp.url) | |
gtf.file <- Filter(function(x) grepl("\\d\\.annotation.gtf", x), ftp.files) | |
gtf.url <- paste0(ftp.url, gtf.file) | |
download.file(gtf.url, destfile = file.path(out.dir, gtf.file)) | |
# Export GRanges object --------------------------------------------------- | |
gencode <- import.gff(file.path(out.dir, gtf.file), version = "2") | |
save(gencode, file = file.path(out.dir, paste0(sub("gtf.gz", "", gtf.file), "rda"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment