Skip to content

Instantly share code, notes, and snippets.

@Vessy
Created November 8, 2013 14:42
Show Gist options
  • Save Vessy/7371961 to your computer and use it in GitHub Desktop.
Save Vessy/7371961 to your computer and use it in GitHub Desktop.
3D networks with Jmol
library("igraph")
rm(list = ls())
##########################################################################
# Read data
dataSet <- read.table("lesmis.txt", header = FALSE, sep = "\t")
# Create a graph. Use simplify to ensure that there are no duplicated edges or self loops
gD <- simplify(graph.data.frame(dataSet, directed=FALSE))
# Calculate degree for all nodes
degAll <- degree(gD, v = V(gD), mode = "all")
coord3D <- layout.fruchterman.reingold(gD, dim = 3)
nType <- 1
write.table("@<TRIPOS>MOLECULE", file = "3D_lesmis.mol2", row.names = FALSE, col.names = FALSE, quote = FALSE)
write.table("Les Miserables", file = "3D_lesmis.mol2", row.names = FALSE, col.names = FALSE, quote = FALSE, append = TRUE)
write.table(data.frame(vcount(gD), ecount(gD), nType), file = "3D_lesmis.mol2", sep = "\t", row.names = FALSE, col.names = FALSE, quote = FALSE, append = TRUE)
write.table("SMALL", file = "3D_lesmis.mol2", row.names = FALSE, col.names = FALSE, quote = FALSE, append = TRUE)
write.table("NO_CHARGES\n", file = "3D_lesmis.mol2", row.names = FALSE, col.names = FALSE, quote = FALSE, append = TRUE)
write.table("@<TRIPOS>ATOM", file = "3D_lesmis.mol2", row.names = FALSE, col.names = FALSE, quote = FALSE, append = TRUE)
isType <- 1
for (i in 1:vcount(gD))
{
if (degAll[i] == 1)
isIn <- "He"
if (degAll[i] == 2)
isIn <- "Na"
if ((degAll[i] > 2) & (degAll[i] <= 5))
isIn <- "O"
if ((degAll[i] > 5) & (degAll[i] <= 10))
isIn <- "Au"
if ((degAll[i] > 10) & (degAll[i] <= 15))
isIn <- "P"
if (degAll[i] > 15)
isIn <- "Cl"
hlpL <- data.frame(i, V(gD)[i]$name, coord3D[i, 1], coord3D[i, 2], coord3D[i, 3], isIn, isType, 0.0 )
write.table(hlpL, file = "3D_lesmis.mol2", sep = "\t", row.names = FALSE, col.names = FALSE, quote = FALSE, append = TRUE)
}
write.table("@<TRIPOS>BOND", file = "3D_lesmis.mol2", row.names = FALSE, col.names = FALSE, quote = FALSE, append = TRUE)
isType <- 1
for (i in 1:ecount(gD))
{
hlpL <- data.frame(i, get.edge(gD,i)[1], get.edge(gD,i)[2], isType)
write.table(hlpL, file = "3D_lesmis.mol2", sep = "\t", row.names = FALSE, col.names = FALSE, quote = FALSE, append = TRUE)
}
################################
# Visualization adjustments using jmol scripts
#
# _He; spacefill 0.2; wireframe 10; color bonds blue; font label 10;
# select _Na; spacefill 0.4; wireframe 12; color bonds purple; font label 12;
# select _O; spacefill 0.6; wireframe 14; color bonds red; font label 14;
# select _Au; spacefill 0.8; wireframe 16; color bonds yellow; font label 16;
# select _P; spacefill 1.0; wireframe 18; color bonds orange; font label 18;
# select _Cl; spacefill 1.5; wireframe 20; color bonds green; font label 20;
#
# select _He; label hide;
# select _Na; label hide;
# select _O; label hide
################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment