Skip to content

Instantly share code, notes, and snippets.

@andersx
Last active January 26, 2017 14:30
Show Gist options
  • Select an option

  • Save andersx/e0a6ae7646a64a12167b17a098165579 to your computer and use it in GitHub Desktop.

Select an option

Save andersx/e0a6ae7646a64a12167b17a098165579 to your computer and use it in GitHub Desktop.
Wrapper for SYMMOL to convert and XYZ file to a symmetrized XYZ
#!/usr/bin/env python2
# MIT License
#
# Copyright (c) 2017 Anders Steen Christensen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import os
def read_xyz(xyz_file):
f = open(xyz_file)
lines = f.readlines()
f.close()
atoms = []
coordinates = []
for line in lines[2:]:
tokens = line.split()
if len(tokens) < 4:
break
atom = tokens[0]
x = float(tokens[1])
y = float(tokens[2])
z = float(tokens[3])
atoms.append(atom)
coordinates.append([x,y,z])
return atoms, coordinates
def write_symmol_input(atoms, coordinates):
output = """ 1 1 1 90 90 90
1 1 0.50000 0.50000"""
for atom, coord in zip(atoms, coordinates):
output += "\n%-2s 1%9.5f%9.5f%9.5f" %(atom, coord[0], coord[1], coord[2])
f = open("temp.symmol", "w")
f.write(output)
f.close()
def run_symmol():
os.system("/home/andersx/bin/symmol < temp.symmol > /dev/null")
def read_symmol():
f = open("symmol.out")
lines = f.readlines()
f.close()
read = False
atoms = []
coordinates = []
for line in lines:
if read == True:
tokens = line.split()
if len(tokens) < 8:
break
atom = tokens[0]
x = line[8:17]
y = line[17:26]
z = line[26:35]
atoms.append(atom)
coordinates.append([x,y,z])
if "SYMMETRIZED ORTHOGONAL COORDINATES" in line:
read = True
return atoms, coordinates
if __name__ == "__main__":
xyz_file = sys.argv[1]
atoms, coordinates = read_xyz(xyz_file)
write_symmol_input(atoms, coordinates)
run_symmol()
atoms, coordinates = read_symmol()
print len(atoms)
print
for atom, c in zip(atoms, coordinates):
print "%-2s %s %s %s" % (atom, c[0], c[1], c[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment