Skip to content

Instantly share code, notes, and snippets.

# 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)
# 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]]
# 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,
# 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"]],
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
# 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"))
# 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))
# function to get a parameter value from EXIF code,
# x = a vector of character strings representing the EXIF codes of a file
# parameter = character string representing the parameter
# for which you want to extract the value
# the literal character string is returned for the requested parameter
get_EXIF_value <- function(x, parameter)
{
line <- x[grepl(paste("EXIF_", parameter, "=", sep = ""), x)]
if (!is.null(line) && length(line) > 0)
{
# required packages
require(rgdal)
require(OpenStreetMap)
# select files for which geotags need to
# be extracted and store filenames in data.frame
file_info <- data.frame(file.name = choose.files(filters = Filters["jpeg",]))
#reconstruct fourier transformed image from filtered spectrum:
re_construct <- cos(phase)*magni_filter
im_construct <- sin(phase)*magni_filter
mat_fft_construct <- matrix(complex(real = re_construct, imaginary = im_construct), nrow(mat_clown), ncol(mat_clown))
#Apply inversed Fourier transform to obtain the filtered image:
mat_clown_constr <- Re(fft(mat_fft_construct, inv = T))*checkerboard/prod(dim(mat_clown))
#normalize the resulting matrix between 0 and 1
mat_clown_constr <- (mat_clown_constr - min(mat_clown_constr)) /