Skip to content

Instantly share code, notes, and snippets.

@katwre
Created March 3, 2016 22:39
Show Gist options
  • Save katwre/d2a3f070da63e8abcb53 to your computer and use it in GitHub Desktop.
Save katwre/d2a3f070da63e8abcb53 to your computer and use it in GitHub Desktop.
UCSC table to GRanges object (only chrom, start and end)
uniform_tfbs = data.frame(cell.line=c("GM12878" ,"H1-hESC" ,"K562", "HeLa-S3", "HepG2", "HUVEC", "A549", "IMR-90", "MCF-7"),
schema=c("wgEncodeAwgTfbsUtaGm12878CtcfUniPk", "wgEncodeAwgTfbsUtaH1hescCtcfUniPk", "wgEncodeAwgTfbsUtaK562CtcfUniPk", "wgEncodeAwgTfbsUtaHelas3CtcfUniPk",
"wgEncodeAwgTfbsUtaHepg2CtcfUniPk", "wgEncodeAwgTfbsUtaHuvecCtcfUniPk", "wgEncodeAwgTfbsHaibA549Ctcfsc5916Pcr1xDex100nmUniPk",
"wgEncodeAwgTfbsSydhImr90CtcfbIggrabUniPk", "wgEncodeAwgTfbsUtaMcf7CtcfSerumstimUniPk"),
stringsAsFactors = FALSE)
#' UCSC table to GRanges object (only chrom, start and end)
#'
#' @param table UCSC table
#' @param assembly genome assembly
UCSCtable2Granges = function(table, assembly="hg19"){
require(RMySQL)
require(GenomicRanges)
con <- dbConnect(MySQL(),user="genome",db=assembly,host="genome-mysql.cse.ucsc.edu")
query = dbSendQuery(con, paste0("SELECT chrom, chromStart, chromEnd FROM ", assembly, ".", table));
res = fetch(query, n=-1); # n = -1 to retrieve all pending records.
dbClearResult(query)
dbDisconnect(con)
makeGRangesFromDataFrame(res,
seqnames.field="chrom", start.field="chromStart", end.field="chromEnd")
}
uniform_tfbs_schemas_gr = mclapply(uniform_tfbs$schema,
function(table){
print(table)
UCSCtable2Granges(table, "hg19")
},
mc.cores=9)
names(uniform_tfbs_schemas_gr) = uniform_tfbs$cell.line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment