-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFafoom-backup2sdf.py
52 lines (45 loc) · 1.57 KB
/
Fafoom-backup2sdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
"""
Extracting stored Geometries from backup_{population,blacklist}.dat
which are genereated by Adriana Supady's "Flexible algorithm
for optimization of molecules", fafoom, to a sdf file.
(c)2015 Philipp Traber
Pylint 1.3.1: Your code has been rated at 10.00/10.
"""
import sys
import os
import errno
import re
# this function is from http://stackoverflow.com/a/10840586
def silentremove(filename):
"""Removes {population,blacklist}.sdf from the folder."""
try:
os.remove(filename)
# this would be "except OSError, e:" before Python 2.6
except OSError as damn:
# errno.ENOENT = no such file or directory
if damn.errno != errno.ENOENT:
# re-raise exception if a different error occured
raise
def convert_backup(filename):
"""Converts backup_{population,blacklist}.dat into a sdf-file."""
filestring = open(filename, 'r').read()
geometries = re.findall(r"'(.*?)'", filestring, re.DOTALL)
if "blacklist" in filename:
sdfname = "blacklist.sdf"
silentremove(sdfname)
elif "population" in filename:
sdfname = "population.sdf"
silentremove(sdfname)
with open(sdfname, 'a') as sdffile:
iszero = 0
for geometry in geometries:
if "NEWLINE" in geometry:
if iszero == 1:
line = "\n".join(geometry.split("NEWLINE"))
sdffile.write(line+'$$$$\n')
iszero = 0
else:
iszero = 1
if __name__ == '__main__':
convert_backup(sys.argv[1])