Skip to content

Instantly share code, notes, and snippets.

# rearrang the data
GPStags <- t(matrix(unlist(GPStags), 2))
# store in the data.frame
file_info$lon <- GPStags[,1]
file_info$lat <- GPStags[,2]
# check if any files lack GPS longitudes
table(is.na(file_info$lon))
# Download nice-looking map-tiles for the relevant location
photo_map <- openmap(upperLeft = c(max(file_info$lat, na.rm = T) + 0.2, min(file_info$lon, na.rm = T) - 0.2),
lowerRight = c(min(file_info$lat, na.rm = T) - 0.2, max(file_info$lon, na.rm = T) + 0.2),
type = "stamen-watercolor")
# we need to turn our coordinates into an sp object in order to
# properly project the points onto the map.
photo_points <- SpatialPoints(na.omit(file_info[,c("lon", "lat")]),
CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))
require(rgl)
# Download a topographic image of Earth from NASA's website
# The file downloaded here is actually the lowest available
# resolution. Higher resolutions are available.
download.file("http://eoimages.gsfc.nasa.gov/images/imagerecords/73000/73751/world.topo.bathy.200407.3x5400x2700.jpg", "world.topo.jpg", mode = "wb")
# Use third party software (ImageMagick) to convert the jpeg
# into a png file (rgl can only work with png).
# I also reduce the size and number of colours to achieve an
# set up an rgl device with a black background
open3d(windowRect = c(100, 100, 250, 250))
bg3d("black")
# Remove all lights present and add a light to represent the sun
clear3d(type = "lights")
rgl.light(theta = 30, phi = 0, viewpoint.rel = T, ambient = "#FFFF66")
# Create a sphere on which the world topography is projected
persp3d(world_coords[["x"]], world_coords[["y"]], world_coords[["z"]],
# Rotate the model with the north pole up
rgl.viewpoint(zoom = .60, theta = 180, phi = 90)
# Tilt the rotational axis with 23.5 degrees
# with a slighlty better view of the Northern hemisphere
U <- par3d("userMatrix")
par3d(userMatrix = rotate3d(U, pi*23.5/180, -0.7,-0.3,0))
# Spin the model and save movie frames
movie3d(spin3d(axis = c(0, 0, 1), rpm = 15), duration = 4,
# required packages
require(rcdk)
require(rgl)
# download 3D structure of acetylcholine from Pubchem:
download.file("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/187/record/SDF/?record_type=3d&response_type=save&response_basename=Structure3D_CID_187",
"acetylcholine.sdf", mode = "wb")
# load the downloaded file:
molfile <- load.molecules("acetylcholine.sdf")[[1]]
# create a data.frame with atom properties
atom.props <- data.frame(
symbol = c("O", "N", "C", "H"),
vanderwaals = c(1.52, 1.55, 1.7, 1.2), #Van der Waals radii of atoms
colour = c("red", "blue", grey(0.1), "white")
)
# get atoms from the molecule
atoms <- get.atoms(molfile)
# simply plot the atoms as coloured spheres.
# multiply the Van der Waals radii with 0.75
# to reduce overlap of atoms.
mapply(function(x, y, z, vdw, col){
spheres3d(x, y, z, + vdw*0.75, col = col, add = T)
}, atom_dat$x, atom_dat$y, atom_dat$z,
atom_dat$vanderwaals, atom_dat$colour)
# Save as an interactive model on web page
writeWebGL()
#################################
# ball and stick model
#################################
# get bond info
bonds <- get.bonds(molfile)
bonds.atoms <- data.frame(do.call('rbind', lapply(bonds, function(x){
atoms <- get.atoms(x)
start <- get.point3d(atoms[[1]])
end <- get.point3d(atoms[[2]])
# required packages:
require(plot3D)
require(ProTrackR)
# Read the module 'elekfunk' from the Mod Archive:
con <- url("http://api.modarchive.org/downloads.php?moduleid=41528", "rb")
mod <- read.module(con)
close(con)
rm(con)