Skip to content

Instantly share code, notes, and snippets.

@kozo2
Last active February 26, 2020 04:23
Show Gist options
  • Save kozo2/428fbb9ca74040de182bb926ecfa29ec to your computer and use it in GitHub Desktop.
Save kozo2/428fbb9ca74040de182bb926ecfa29ec to your computer and use it in GitHub Desktop.
library(RCy3)
library(dplyr)
library(KEGGREST)
installApp('KEGGscape')
installApp('Cy3D')
create3Dnetfile <- function(pathwayID1, pathwayID2, pathwayID3, zheight1, zheight2, zheight3, output, kinase_enzyme) {
writeLines(keggGet(pathwayID1, option = 'kgml'), paste(pathwayID1, '.xml', sep=''))
writeLines(keggGet(pathwayID2, option = 'kgml'), paste(pathwayID2, '.xml', sep=''))
writeLines(keggGet(pathwayID3, option = 'kgml'), paste(pathwayID3, '.xml', sep=''))
# layer1
print(paste("Importing", pathwayID1))
importNetworkFromFile(file = paste(pathwayID1, '.xml', sep=''))
Sys.sleep(5)
print(paste("Reading node table", pathwayID1))
nodetable1 = getTableColumns()
et1 = getTableColumns(table = "edge")
ei1 = getEdgeInfo(et1$SUID)
et1['source'] = unlist(lapply(ei1, function(x) x$source))
et1['target'] = unlist(lapply(ei1, function(x) x$target))
KEGG_NODE_Z = rep(zheight1, dim(nodetable1)[1])
nodetable1 = cbind(nodetable1, KEGG_NODE_Z)
# layer2
print(paste("Importing", pathwayID2))
importNetworkFromFile(file = paste(pathwayID2, '.xml', sep=''))
Sys.sleep(5)
print(paste("Reading node table", pathwayID2))
nodetable2 = getTableColumns()
et2 = getTableColumns(table = "edge")
ei2 = getEdgeInfo(et2$SUID)
et2['source'] = unlist(lapply(ei2, function(x) x$source))
et2['target'] = unlist(lapply(ei2, function(x) x$target))
KEGG_NODE_Z = rep(zheight2, dim(nodetable2)[1])
nodetable2 = cbind(nodetable2, KEGG_NODE_Z)
#layer3
print(paste("Importing", pathwayID3))
importNetworkFromFile(file = paste(pathwayID3, '.xml', sep=''))
Sys.sleep(5)
print(paste("Reading node table", pathwayID3))
nodetable3 = getTableColumns()
et3 = getTableColumns(table = "edge")
ei3 = getEdgeInfo(et3$SUID)
et3['source'] = unlist(lapply(ei3, function(x) x$source))
et3['target'] = unlist(lapply(ei3, function(x) x$target))
KEGG_NODE_Z = rep(zheight3, dim(nodetable3)[1])
nodetable3 = cbind(nodetable3, KEGG_NODE_Z)
# Combining
print("Combining networks...")
nodetable3d = bind_rows(nodetable1, nodetable2, nodetable3)
nodetable3d %>% rename(id=SUID) -> nodes
nodes['id'] = as.character(nodes$id)
edges = bind_rows(et1, et2, et3)
edges['source'] = as.character(edges$source)
edges['target'] = as.character(edges$target)
k2e = read.table(kinase_enzyme)
for(i in 1:nrow(k2e)) {
kerow <- k2e[i,]
source = as.character(kerow$V1)
target = as.character(kerow$V2)
for(j in 1:nrow(nodes)) {
row4source = nodes[j,]
if (source %in% row4source$KEGG_ID) {
#print("source")
#print(noderow$id)
s = row4source$id
for (k in 1:nrow(nodes)) {
row4target = nodes[k,]
if (target %in% row4target$KEGG_ID){
t = row4target$id
#print("target")
edges %>% add_row(source = s, target = t) -> edges
#print(c(s,t))
}
}
}
}
}
createNetworkFromDataFrames(nodes, edges)
download.file("https://raw.githubusercontent.com/ecell/transomics2cytoscape/master/transomics.xml", "transomics.xml")
importVisualStyles(filename = "transomics.xml")
setVisualStyle("transomics")
print("Set visual style to 'transomics'")
exportNetwork(output,'cyjs')
print(paste('Wrote ', output, '.cyjs in the current working directory.', sep=''))
print("Please import it from Cytoscape Desktop and select 'Cy3D' from 'Network View Renderer' dropdown list.")
print("Then you should see the multi layered 3D network space.")
#print(nodes$KEGG_ID[[263]][3])
#return(c(nodes, edges))
}
## example
#create3Dnetfile('rno00010', 'rno00010', 'rno04910', 1, 200, 400, 'output', 'kinase_enzyme.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment