Nothing
#####################
#####CONVERSIONS#####
#####################
#' Get compound names from KEGG codes
#'
#' Returns compound names associated with KEGG compound identifiers.
#'
#' @param kegg_codes Character vector of KEGG compound identifiers.
#' @return A named \code{character} vector in which the values are KEGG compound
#' identifiers and the names are the corresponding compound names. Each element
#' of the returned vector represents one KEGG compound code matched to its
#' primary compound name.
#' @examples
#' \dontrun{
#' get_cpd_names(c("C00031", "C00022"))
#' }
#' @export
get_cpd_names=function(kegg_codes){
if(!requireNamespace("KEGGREST", quietly = TRUE)){
stop("Package KEGGREST needed for this function to work. Please install it: BiocManager::install('KEGGREST').",
call. = FALSE)
}
n=c()
n_kegg=c()
cpds_to_get=c()
for (i in 1:length(kegg_codes)){
kegg=kegg_codes[i]
tab_kegg=strsplit(kegg, ":")[[1]][2]
for(i in 1:length(codes$KEGG)){
kegg_ref=codes$KEGG[i]
if (tab_kegg %in% unlist(strsplit(kegg_ref, "; ")[[1]])){
n=c(n,codes$NAME[i])
n_kegg=c(n_kegg, kegg)
}
else{
cpds_to_get=c(cpds_to_get, kegg)
}
}
}
if(!is.null(cpds_to_get)){
all_cpds=c(KEGGREST::keggList("compound"), KEGGREST::keggList("glycan"))
cpds_names_kegg=all_cpds[cpds_to_get]
all_n_kegg=strsplit(cpds_names_kegg, "; ")
for (i in 1:length(all_n_kegg)){
kegg_name=all_n_kegg[[i]][1]
kegg_code=names(all_n_kegg)[i]
n=c(n, kegg_name)
n_kegg=c(n_kegg, kegg_code)
}
}
names(n_kegg)=n
return(n_kegg)
}
#' Get kegg codes from hmdb codes:
#'
#' @param hmdb_codes Character vector of HMDB identifiers.
#' @return A named \code{character} vector with the KEGG compound identifiers
#' corresponding to the input HMDB codes. The vector values are KEGG compound
#' codes prefixed with \code{"cpd:"}, and the element names are the matched
#' compound names.
#' @examples
#' \dontrun{
#' convert_hmdb_to_kegg(c("HMDB0000122"))
#' }
#' @export
convert_hmdb_to_kegg=function(hmdb_codes){
hmdbs=toupper(hmdb_codes)
names_cpds=c()
keggs=c()
for (hmdb in hmdbs){
for(i in 1:length(codes$HMDB)){
hmdb_ref=codes$HMDB[i]
if (hmdb %in% unlist(strsplit(hmdb_ref, "; ")[[1]])){
keggs_for_hmdb = strsplit(codes$KEGG[i], "; ")[[1]]
if(length(keggs_for_hmdb)!=0){
cpd=paste("cpd:", keggs_for_hmdb, sep="")
keggs=c(keggs, cpd)
names_cpds=c(names_cpds, rep(codes$NAME[codes$HMDB==hmdb], length(cpd)))
}
}
}
}
names(keggs)=names_cpds
return(keggs)
}
#' Get kegg codes from chebi codes:
#'
#' Auto-exported function: convert_chebi_to_kegg
#'
#' @param chebi_codes Character vector of ChEBI identifiers.
#'
#' @return A named \code{character} vector with the KEGG compound identifiers
#' corresponding to the input ChEBI codes. The vector values are KEGG compound
#' codes prefixed with \code{"cpd:"}, and the element names are the matched
#' compound names.
#'
#' @examples
#' \dontrun{
#' convert_chebi_to_kegg(c("CHEBI:15377"))
#' }
#'
#' @keywords internal
#' @export
convert_chebi_to_kegg=function(chebi_codes){
chebis=toupper(chebi_codes)
names_cpds=c()
keggs=c()
for (chebi in chebis){
for(i in 1:length(codes$HMDB)){
chebi_ref=codes$CHEBI[i]
if (chebi %in% unlist(strsplit(chebi_ref, "; ")[[1]])){
keggs_for_chebi = strsplit(codes$KEGG[i], "; ")[[1]]
if(length(keggs_for_chebi)!=0){
cpd=paste("cpd:", keggs_for_chebi, sep="")
keggs=c(keggs, cpd)
names_cpds=c(names_cpds, rep(codes$NAME[codes$CHEBI==chebi], length(cpd)))
}
}
}
}
names(keggs)=names_cpds
return(keggs)
}
#' Get kegg codes from spcmnm codes:
#'
#' @param spcmnm_codes Character vector of SPCMNM identifiers.
#' @return A named \code{character} vector with the KEGG compound identifiers
#' corresponding to the input SPCMNM codes. The vector values are KEGG compound
#' codes prefixed with \code{"cpd:"}, and the element names are the matched
#' compound names.
#'
#' @examples
#' \dontrun{
#' convert_multiple_spcmnm_to_kegg(c("SPCM00001"))
#' }
#'
#' @export
convert_multiple_spcmnm_to_kegg=function(spcmnm_codes){
spcmnms=toupper(spcmnm_codes)
names_cpds=c()
keggs=c()
for (spcmnm in spcmnms){
if(!spcmnm%in%codes$SPCMNM) stop("The following SPCMNM code is not available: ", spcmnm, "\n")
keggs_for_spcmnm=unlist(strsplit(codes$KEGG[codes$SPCMNM==spcmnm], "; ")[[1]])
if(length(keggs_for_spcmnm)!=0){
cpd=paste("cpd:", keggs_for_spcmnm, sep="")
keggs=c(keggs, cpd)
names_cpds=c(names_cpds, rep(codes$NAME[codes$SPCMNM==spcmnm], length(cpd)))
}
}
names(keggs)=names_cpds
return(keggs)
}
###########################
#####ORGANISMS - PATHS#####
###########################
#' Get code, t number, full name and phylogeny of all organisms in KEGG:
#'
#' @return A \code{data.frame} with the KEGG T number, organism code, species
#' names, and phylogeny for all organisms available in KEGG. Each row
#' represents one organism and the columns are \code{Tnumber},
#' \code{organismCode}, \code{speciesNames}, and \code{phylogeny}.
#'
#' @examples
#' \dontrun{
#' head(get_OrganismsCodes())
#' }
#'
#' @export
get_OrganismsCodes=function(){
if(!requireNamespace("KEGGREST", quietly = TRUE)){
stop("Package KEGGREST needed for this function to work. Please install it: BiocManager::install('KEGGREST').",
call. = FALSE)
}
organisms=KEGGREST::keggList("organism")
organisms.Inf=data.frame(Tnumber=organisms[,1], organismCode=organisms[,2],
speciesNames=organisms[,3], phylogeny=organisms[,4], stringsAsFactors=FALSE)
return(organisms.Inf)
}
#' Get vector with paths numbers that occur in the given organism, named with the full path name:
#'
#' @param org_code TODO.
#' @return A named \code{character} vector with the pathway identifiers
#' available for the specified organism, where each value is an organism-specific
#' pathway code and each name is the corresponding KEGG pathway name.
#'
#' @examples
#' \dontrun{
#' head(get_metabPaths_org("hsa"))
#' }
#'
#' @export
get_metabPaths_org=function(org_code){
if(!requireNamespace("KEGGREST", quietly = TRUE)){
stop("Package KEGGREST needed for this function to work. Please install it: BiocManager::install('KEGGREST').",
call. = FALSE)
}
if (!org_code%in%get_OrganismsCodes()[["organismCode"]]) stop("Invalid organism code.")
paths=unique(KEGGREST::keggLink("pathway", org_code))
paths_n=substr(paths, nchar(paths)-5+1, nchar(paths))
maps_take_out=c("00190", "00195", "00196", "01100", "01110", "01120", "01130", "01200", "01210", "01212", "01230", "01220", "00511", "00514",
"00533", "01010", "01060", "01062", "01063", "01064", "01065", "01066", "01070", "01052", "01054", "00121")
idx_take_out=as.vector(na.exclude(match(maps_take_out, paths_n)))
idx_take_out=c(idx_take_out, as.vector(na.exclude(match(paths_n[as.numeric(paths_n)>1230], paths_n))))
paths_n=paths_n[-idx_take_out]
pathways_all=KEGGREST::keggList("pathway")
paths_names=pathways_all[match(paste("path:map", paths_n, sep=""), names(pathways_all))]
paths_n=paste(org_code, paths_n, sep="")
names(paths_n)=paths_names
return(paths_n)
}
#######################################
#####ORGANISMS - PATHS - COMPOUNDS#####
#######################################
#' Get only the paths of the organism that contain given compounds:
#'
#' @param organism_code TODO.
#' @param compounds TODO.
#' @param full.result TODO.
#' @return A \code{data.frame} with the pathways containing the input compounds.
#' Each row represents one matched pathway. When \code{full.result = TRUE}, the
#' returned data frame includes the columns \code{pathways}, \code{ratio},
#' \code{compounds}, and \code{compounds_names}; otherwise it contains only
#' \code{pathways} and \code{ratio}. Row names correspond to pathway names.
#'
#' @examples
#' \dontrun{
#' cpds <- c(glucose = "cpd:C00031", pyruvate = "cpd:C00022")
#' head(get_paths_with_cpds_org("hsa", cpds, full.result = FALSE))
#' }
#'
#' @export
get_paths_with_cpds_org=function(organism_code, compounds, full.result=TRUE){
if(!requireNamespace("KEGGgraph", quietly = TRUE)){
stop("Package KEGGgraph needed for this function to work. Please install it: BiocManager::install('KEGGgraph').",
call. = FALSE)
}
org_paths=get_metabPaths_org(organism_code)
paths_with_cpds=c()
names_paths=c()
compounds_in_paths=c()
compounds_in_paths_names=c()
ratio=c()
for (i in 1:length(org_paths)){
path=org_paths[i]
path_name=names(org_paths)[i]
path_r=get_MetabolitePath(path)
nods=KEGGgraph::nodes(convert_keggpathway_2_reactiongraph(path_r))
cpds_idx=as.vector(na.exclude(match(nods, compounds)))
if (length(cpds_idx)>0){
paths_with_cpds=c(paths_with_cpds, path)
names_paths=c(names_paths, path_name)
ratio=c(ratio, length(cpds_idx)/length(nods))
if (full.result){
compounds_in_paths=c(compounds_in_paths, paste(compounds[unique(cpds_idx)], collapse="; "))
compounds_in_paths_names=c(compounds_in_paths_names, paste(names(compounds)[unique(cpds_idx)], collapse="; "))
}
}
}
order_idx=order(ratio)
if(full.result) return(data.frame(pathways=paths_with_cpds, ratio=ratio, compounds=compounds_in_paths, compounds_names=compounds_in_paths_names, row.names=names_paths, stringsAsFactors=FALSE)[order_idx,])
return(data.frame(pathways=paths_with_cpds, ratio=ratio, row.names=names_paths, stringsAsFactors=FALSE)[order_idx,])
}
########################
#####CREATE PATHWAY#####
########################
#' Returns an object of KEGGPathway of the pathway especified in pathcode
#'
#' @param pathcode TODO.
#' @return A \code{KEGGPathway} object corresponding to the pathway specified by
#' \code{pathcode}. This object contains the parsed KEGG pathway structure and
#' can be used as input to downstream graph conversion and visualization
#' functions.
#'
#' @examples
#' \dontrun{
#' path <- get_MetabolitePath("hsa00010")
#' class(path)
#' }
#'
#' @export
get_MetabolitePath=function(pathcode){
pathObj=KEGGREST::keggGet(pathcode, option="kgml")
parsedPath=KEGGgraph::parseKGML(pathObj)
return(parsedPath)
}
#' Convert KEGGPathway object to graph object
#'
#' @param pathObj TODO.
#' @return A graph object representing the reactions in the input
#' \code{KEGGPathway} object. The returned object is suitable for graph-based
#' inspection or for use in downstream visualization functions.
#'
#' @examples
#' \dontrun{
#' path <- get_MetabolitePath("hsa00010")
#' graph <- convert_keggpathway_2_reactiongraph(path)
#' class(graph)
#' }
#'
#' @export
convert_keggpathway_2_reactiongraph=function(pathObj){
reactionGraphObj=KEGGgraph::KEGGpathway2reactionGraph(pathObj)
return(reactionGraphObj)
}
#' Creates the pathway, with reactions included in the nodes
#'
#' @param path TODO.
#' @param path.name TODO.
#' @param identified_cpds TODO.
#' @param nodeNames TODO.
#' @param nodeTooltip TODO.
#' @param map.zoom TODO.
#' @param map.layout TODO.
#' @param map.width TODO.
#' @param map.height TODO.
#' @return A \code{cyjShiny} widget representing the pathway with reactions
#' included in the nodes. The widget contains the pathway graph ready for
#' interactive visualization, with identified compounds highlighted when they
#' are present in the pathway.
#'
#' @examples
#' \dontrun{
#' path <- get_MetabolitePath("hsa00010")
#' create_pathway_with_reactions(
#' path = path,
#' path.name = "hsa00010",
#' identified_cpds = c("cpd:C00031", "cpd:C00022")
#' )
#' }
#'
#' @export
create_pathway_with_reactions <- function(path, path.name, identified_cpds,
nodeNames = "kegg", nodeTooltip = FALSE,
map.zoom = FALSE, map.layout = "preset",
map.width = NULL, map.height = NULL) {
source <- c()
target <- c()
edgeSourceShape <- c()
edgeTargetShape <- c()
colorLine <- c()
nodes <- c()
shape <- c()
height <- c()
width <- c()
color <- c()
reaction_map <- KEGGgraph::getReactions(path)
for (reaction in reaction_map) {
n <- KEGGgraph::getName(reaction)
names <- strsplit(n, " ")[[1]]
for (name in names) {
t <- KEGGgraph::getType(reaction)
substrate <- c()
for (l in strsplit(KEGGgraph::getSubstrate(reaction), " ")) substrate <- c(substrate, l)
n_substrate <- length(substrate)
product <- c()
for (l in strsplit(KEGGgraph::getProduct(reaction), " ")) product <- c(product, l)
n_product <- length(product)
n_compounds <- n_substrate + n_product
n_edges <- n_compounds + 1
source <- c(source, substrate)
target <- c(target, rep(name, n_substrate))
source <- c(source, rep(name, n_product))
target <- c(target, product)
if (t == "irreversible") {
colorLine <- c(colorLine, rep("#bf000c", n_compounds))
color <- c(color, rep("#888888", n_compounds), "#bf000c")
} else {
colorLine <- c(colorLine, rep("#145b02", n_compounds))
color <- c(color, rep("#888888", n_compounds), "#145b02")
}
nodes <- c(nodes, substrate, product, name)
shape <- c(shape, rep("ellipse", n_compounds), "roundrectangle")
height <- c(height, rep(80, n_compounds), 30)
width <- c(width, rep(80, n_compounds), 80)
}
}
map_names <- c()
maps <- c()
path_p <- substr(path.name, nchar(path.name) - 5 + 1, nchar(path.name))
maps_con <- maps_con[grep(paste(".*", path_p, ".*", sep = ""), maps_con$in_map), ]
for (node in path@nodes) {
if (KEGGgraph::getType(node) == "map" &&
!(KEGGgraph::getName(node)[1] %in% maps) &&
length(grep("^TITLE:", node@graphics@name)) == 0) {
map_name <- KEGGgraph::getName(node)
map_names <- c(map_names, node@graphics@name)
maps <- c(maps, map_name)
path_n <- substr(map_name, nchar(map_name) - 4, nchar(map_name))
cpds <- maps_con$node[maps_con$map == path_n]
cpds_ant <- c()
if (sum(cpds %in% nodes) != 0) {
nodes <- c(nodes, map_name)
color <- c(color, "#D2691E")
shape <- c(shape, "rectangle")
height <- c(height, 80)
width <- c(width, 80)
for (cpd in cpds) {
if (cpd %in% nodes && !(cpd %in% cpds_ant)) {
type <- maps_con$type[maps_con$map == path_n & maps_con$node == cpd]
cpds_ant <- c(cpds_ant, cpd)
if (type %in% c("reversible", "in")) {
nodes <- c(nodes, cpd)
color <- c(color, "#888888")
shape <- c(shape, "ellipse")
height <- c(height, 80)
width <- c(width, 80)
source <- c(source, cpd)
target <- c(target, map_name)
if (type == "reversible") colorLine <- c(colorLine, "#145b02")
else colorLine <- c(colorLine, "#bf000c")
} else {
nodes <- c(nodes, cpd)
color <- c(color, "#888888")
shape <- c(shape, "ellipse")
height <- c(height, 80)
width <- c(width, 80)
source <- c(source, map_name)
target <- c(target, cpd)
colorLine <- c(colorLine, "#bf000c")
}
}
}
}
}
}
names(map_names) <- maps
if (nodeNames == "kegg") {
name <- c()
new_name <- strsplit(nodes, ":")
for (n in new_name) {
name <- c(name, n[2])
}
} else if (nodeNames == "names") {
name <- nodes
cpds <- grep("^cpd:", name, value = TRUE)
cpds <- c(cpds, grep("^gl:", name, value = TRUE))
not_cpds <- grep("^cpd:", name, value = TRUE, invert = TRUE)
named_cpds <- get_cpd_names(cpds)
for (cpd in named_cpds) name[name == cpd] <- names(named_cpds)[named_cpds == cpd]
for (code in not_cpds) {
if (length(grep("^path:", code, value = TRUE))) name[name == code] <- map_names[code]
name[name == code] <- strsplit(code, ":")[[1]][2]
}
}
if (map.layout == "preset") {
node_name <- c()
x <- c()
y <- c()
x.data <- c()
y.data <- c()
for (node in path@nodes) {
x <- c(x, node@graphics@x)
y <- c(y, node@graphics@y)
if (is.na(node@reaction)) node_name <- c(node_name, paste(node@name, collapse = " "))
else if (KEGGgraph::getType(node) == "map") node_name <- c(node_name, paste(node@name, collapse = " "))
else node_name <- c(node_name, node@reaction)
}
position.df <- data.frame(x = x, y = y, node_name = node_name)
position.df$x <- position.df$x * 2
position.df$y <- position.df$y * 2
for (node in nodes) {
x <- position.df$x[position.df$node_name == node][1]
y <- position.df$y[position.df$node_name == node][1]
if (sum(is.na(c(x, y))) > 0) {
target_x <- position.df$x[position.df$node_name == target[match(node, source)]][1]
target_y <- position.df$y[position.df$node_name == target[match(node, source)]][1]
source_x <- position.df$x[position.df$node_name == source[match(node, target)]][1]
source_y <- position.df$y[position.df$node_name == source[match(node, target)]][1]
x <- (target_x + source_x) / 2
y <- (target_y + source_y) / 2
}
x.data <- c(x.data, x)
y.data <- c(y.data, y)
}
nodeData <- data.frame(
id = nodes, name = name, shape = shape, height = height, width = width,
color = color, nodeLabelColor = rep("#000000", length(nodes)),
x = x.data, y = y.data, stringsAsFactors = FALSE
)
edgeData <- data.frame(
source = source, target = target, interaction = rep("interacts", length(source)),
color = colorLine, label = rep("", length(source)),
labelColor = rep("#888888", length(source)), stringsAsFactors = FALSE
)
} else {
nodeData <- data.frame(
id = nodes, name = name, shape = shape, height = height, width = width,
color = color, nodeLabelColor = rep("#000000", length(nodes)),
stringsAsFactors = FALSE
)
edgeData <- data.frame(
source = source, target = target, interaction = rep("interacts", length(source)),
color = colorLine, label = rep("", length(source)),
labelColor = rep("#888888", length(source)), stringsAsFactors = FALSE
)
}
cpds_to_color <- c()
for (cpd in identified_cpds) {
if (cpd %in% nodes) cpds_to_color <- c(cpds_to_color, cpd)
}
for (cpd in cpds_to_color) {
nodeData$color[nodeData$id == cpd] <- "#0061ff"
}
graph_json <- cyjShiny::dataFramesToJSON(edgeData, nodeData)
cyjShiny::cyjShiny(
graph = graph_json,
layoutName = map.layout,
width = map.width,
height = map.height
)
}
#' Creates the pathway wanted. If any of the given compounds is present in the pathway, it is coloured differently.
#'
#' @param compounds TODO.
#' @param pathway TODO.
#' @param nodeNames TODO.
#' @param nodeTooltip TODO.
#' @param map.zoom TODO.
#' @param map.layout TODO.
#' @param map.width TODO.
#' @param map.height TODO.
#' @param verbose Logical indicating whether progress messages should be shown.
#' @return A \code{cyjShiny} widget representing the selected pathway with the
#' input compounds highlighted when present. The returned widget can be printed
#' or embedded in an interactive R session to inspect the pathway graph.
#'
#' @examples
#' \dontrun{
#' pathway_analysis(
#' compounds = c(glucose = "cpd:C00031", pyruvate = "cpd:C00022"),
#' pathway = "hsa00010",
#' verbose = FALSE
#' )
#' }
#'
#' @export
pathway_analysis <- function(compounds, pathway,
nodeNames = "kegg", nodeTooltip = FALSE,
map.zoom = FALSE, map.layout = "preset",
map.width = NULL, map.height = NULL,
verbose = TRUE) {
if (!requireNamespace("KEGGgraph", quietly = TRUE) &&
!requireNamespace("KEGGREST", quietly = TRUE) &&
!requireNamespace("cyjShiny", quietly = TRUE)) {
stop(
paste0(
"Packages 'KEGGgraph', 'KEGGREST', and 'cyjShiny' are needed for this function to work. ",
"Please install 'KEGGgraph' and 'KEGGREST' with ",
"BiocManager::install(c('KEGGgraph', 'KEGGREST')) and install 'cyjShiny' with ",
"install.packages('cyjShiny') or ",
"remotes::install_github('cytoscape/cyjShiny', ref = 'master', build_vignettes = TRUE)."
),
call. = FALSE
)
} else if (!requireNamespace("KEGGgraph", quietly = TRUE) &&
!requireNamespace("KEGGREST", quietly = TRUE)) {
stop(
"Packages 'KEGGgraph' and 'KEGGREST' are needed for this function to work. Please install them with BiocManager::install(c('KEGGgraph', 'KEGGREST')).",
call. = FALSE
)
} else if (!requireNamespace("KEGGgraph", quietly = TRUE) &&
!requireNamespace("cyjShiny", quietly = TRUE)) {
stop(
paste0(
"Packages 'KEGGgraph' and 'cyjShiny' are needed for this function to work. ",
"Please install 'KEGGgraph' with BiocManager::install('KEGGgraph') and install 'cyjShiny' with ",
"install.packages('cyjShiny') or ",
"remotes::install_github('cytoscape/cyjShiny', ref = 'master', build_vignettes = TRUE)."
),
call. = FALSE
)
} else if (!requireNamespace("KEGGREST", quietly = TRUE) &&
!requireNamespace("cyjShiny", quietly = TRUE)) {
stop(
paste0(
"Packages 'KEGGREST' and 'cyjShiny' are needed for this function to work. ",
"Please install 'KEGGREST' with BiocManager::install('KEGGREST') and install 'cyjShiny' with ",
"install.packages('cyjShiny') or ",
"remotes::install_github('cytoscape/cyjShiny', ref = 'master', build_vignettes = TRUE)."
),
call. = FALSE
)
} else if (!requireNamespace("KEGGgraph", quietly = TRUE)) {
stop(
"Package 'KEGGgraph' is needed for this function to work. Please install it with BiocManager::install('KEGGgraph').",
call. = FALSE
)
} else if (!requireNamespace("KEGGREST", quietly = TRUE)) {
stop(
"Package 'KEGGREST' is needed for this function to work. Please install it with BiocManager::install('KEGGREST').",
call. = FALSE
)
} else if (!requireNamespace("cyjShiny", quietly = TRUE)) {
stop(
paste0(
"Package 'cyjShiny' is needed for this function to work. ",
"Please install it with install.packages('cyjShiny') or ",
"remotes::install_github('cytoscape/cyjShiny', ref = 'master', build_vignettes = TRUE)."
),
call. = FALSE
)
}
if (verbose) message("Getting pathway map")
pathMap <- get_MetabolitePath(pathway)
reactionObj <- convert_keggpathway_2_reactiongraph(pathMap)
if (verbose) message("Creating pathway")
create_pathway_with_reactions(
pathMap, pathway, compounds,
nodeNames, nodeTooltip,
map.zoom, map.layout, map.width, map.height
)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.