########################################
#### from Enrichment Browser ####
########################################
# from compileGRN.R
.compileGRNFromKEGG <- function(pwys, out.file=NULL)
{
kegg.rels <- unique(.getKEGGRels(pwys)[,1:3])
kegg.rels[,"TYPE"] <- ifelse(kegg.rels[,"TYPE"] == "-->", "+", "-")
return(kegg.rels)
write.table(kegg.rels, file=out.file,
row.names=FALSE, col.names=FALSE, quote=FALSE, sep="\t")
message(paste("GRN written to", out.file))
}
# rels %in% c("ECrel", "GErel", "PCrel", "PPrel")
.getKEGGRels <- function( pwys,
out.file=NULL,
types=c("-->", "--|"),
rels=c("GErel", "PPrel"))
{
if(is.character(pwys))
{
if(nchar(pwys) == 3) pwys <- downloadPathways(pwys)
else pwys <- .extractPwys(pwys)
}
org <- getPathwayInfo(pwys[[1]])@org
no.out <- FALSE
if(is.null(out.file))
{
no.out <- TRUE
out.dir <- configEBrowser("OUTDIR.DEFAULT")
if(!file.exists(out.dir)) dir.create(out.dir, recursive=TRUE)
out.file <- file.path(out.dir, paste(org, "rels.txt", sep="_"))
}
if(file.exists(out.file)) file.remove(out.file)
con <- file(out.file, open="at")
GRN.HEADER.COLS <- c("FROM", "TO", "TYPE", "REL", "PWY")
writeLines(paste(GRN.HEADER.COLS, collapse="\t"), con)
for(p in pwys)
{
nr <- getPathwayInfo(p)@number
for(e in edges(p))
{
# effect type: -->, --|, ...
# relation type: GErel, PPrel, ...
rt <- getType(e)
if(rt %in% rels)
{
et <- .getEdgeType(e)
if(et %in% types)
{
entries <- getEntryID(e)
ids1 <- .getNodeName(entries[1], nodes(p))
ids2 <- .getNodeName(entries[2], nodes(p))
for(i in ids1)
for(j in ids2)
writeLines(
paste(c(i,j,et,rt,nr), collapse="\t"),
con)
}
}
}
flush(con)
}
close(con)
cont <- as.matrix(read.delim(out.file))
cont <- gsub(" ", "", cont)
cont <- unique(cont)
if(no.out)
{
file.remove(out.file)
return(cont)
}
write.table(cont, file=out.file, sep="\t", row.names=FALSE, quote=FALSE)
message(paste(org, "KEGG relations written to", out.file))
}
.getEdgeType <- function(edge)
{
if(length(getSubtype(edge)) == 0) return(NA)
return(getSubtype(edge)$subtype@value)
}
.getNodeName <- function(entry, nodes)
{
n <- nodes[[entry]]
ids <- getName(n)
if((length(ids) == 1) && (ids == "undefined"))
{
ids <- NULL
ncomp <- getComponent(n)
ids <- unlist(lapply(ncomp, function(comp) getName(nodes[[comp]])))
}
ids <- sub("^[a-z]{3}:", "", ids)
return(ids)
}
# from getGenesets.R
# only preferred over '.dwnldKeggGS'
# when pathway kgmls have already been download
.extractKeggGS <- function(pwys, gmt.file=NULL)
{
# read in & parse pathways
if(is.character(pwys)) pwys <- .extractPwys(pwys)
# get pathway annotations
nn <- vapply(pwys, getName, character(1))
tt <- vapply(pwys, getTitle, character(1))
# extract genesets
gs <- lapply(pwys,
function(pwy)
{
genes <- .getGenesByPwy(pwy)
genes <- sub("^[a-z]{3}:", "", genes)
genes <- sort(genes)
return(genes)
})
names(gs) <- .makeGSNames(nn, tt)
if(!is.null(gmt.file)) writeGMT(gs, gmt.file=gmt.file)
return(gs)
}
## .extractPwys from zip archive and parse KGML files
.extractPwys <- function(pwy.zip)
{
pwy.dir <- dirname(pwy.zip)
unzip(pwy.zip, exdir=pwy.dir, junkpaths=TRUE)
pwy.files <- list.files(pwy.dir, pattern="*.xml", full.names=TRUE)
pwys <- sapply(pwy.files, parseKGML)
## clean up
for(f in pwy.files) file.remove(f)
return(pwys)
}
.getGenesByPwy <- function(pwy)
{
ts <- vapply(nodes(pwy), getType, character(1))
genes <- unique(unlist(lapply(nodes(pwy)[ts == "gene"], getName)))
return(genes)
}
# build first gmt column: the ID (format: <pwy.nr>_<pwy.title>)
.makeGSNames <- function(ids, titles)
{
ids <- sub("^path:", "", ids)
titles <- vapply(titles,
function(title) unlist(strsplit(title, " - "))[1],
character(1))
titles <- sub("^ +", "", titles)
titles <- sub(" +$", "", titles)
titles <- gsub(" ", "_", titles)
ids <- paste(ids, titles, sep="_")
return(ids)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.