Skip to content

Commit

Permalink
cleaning up documentation and formatting everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
colliner committed Jan 5, 2024
1 parent 67ea0b8 commit 4e2bf7b
Show file tree
Hide file tree
Showing 16 changed files with 3,128 additions and 2,183 deletions.
3 changes: 3 additions & 0 deletions .style.yapf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[style]
based_on_style = google
ALLOW_SPLIT_BEFORE_DICT_VALUE = False
104 changes: 57 additions & 47 deletions examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,88 @@
from rdkit import Chem
import numpy as np


fn_smi='sample_inputs/example.smi'
fn_xyz='sample_inputs/example.xyz'
fn_mol='sample_inputs/example.mol'
fn_smi = 'sample_inputs/example.smi'
fn_xyz = 'sample_inputs/example.xyz'
fn_mol = 'sample_inputs/example.mol'


def test_xyzfile2cbh():
print('coordinates file(s) (.xyz) --> xyz2cbh --> cbh-n')
cbh=pycbh.xyz2cbh(fn_xyz, 2)
pycbh.cbh_print(cbh)
print('coordinates file(s) (.xyz) --> xyz2cbh --> cbh-n')
cbh = pycbh.xyz2cbh(fn_xyz, 2)
pycbh.cbh_print(cbh)


def test_xyzfile2cbh_batch():
print('specifying multiple files and multiple rungs')
cbh=pycbh.xyz2cbh([fn_xyz,fn_xyz,fn_xyz], [0,1,2])
pycbh.cbh_print(cbh)
print('specifying multiple files and multiple rungs')
cbh = pycbh.xyz2cbh([fn_xyz, fn_xyz, fn_xyz], [0, 1, 2])
pycbh.cbh_print(cbh)


def test_smistr2cbh():
print('SMILES string(s) --> smi2cbh --> cbh-n')
smi_str = 'O=Cc1ccc(O)c(OC)c1'
cbh=pycbh.smi2cbh(smi_str, 0)
pycbh.cbh_print(cbh)
print('SMILES string(s) --> smi2cbh --> cbh-n')
smi_str = 'O=Cc1ccc(O)c(OC)c1'
cbh = pycbh.smi2cbh(smi_str, 0)
pycbh.cbh_print(cbh)


def test_smistr2cbh_batch():
print('specifying multiple rungs of CBH')
smi_str = 'O=Cc1ccc(O)c(OC)c1'
cbh=pycbh.smi2cbh([smi_str], [0,1,2,3,4,5,6])
pycbh.cbh_print(cbh)
print('specifying multiple rungs of CBH')
smi_str = 'O=Cc1ccc(O)c(OC)c1'
cbh = pycbh.smi2cbh([smi_str], [0, 1, 2, 3, 4, 5, 6])
pycbh.cbh_print(cbh)


def test_molfile2cbh():
print('RDKit .mol object file(s) --> mol2cbh --> cbh-n')
cbh=pycbh.mol2cbh(fn_mol, 0)
pycbh.cbh_print(cbh)
print('RDKit .mol object file(s) --> mol2cbh --> cbh-n')
cbh = pycbh.mol2cbh(fn_mol, 0)
pycbh.cbh_print(cbh)


def test_mixedinp2cbh():
print('General conversion from files:')
print(' filename(s) (.smi | .xyz | .mol) --> files2cbh --> cbh-n')
mixed_fns = [ fn_smi, fn_xyz, fn_mol ]
rungs = [0,1,2]
cbh=pycbh.files2cbh(mixed_fns, rungs)
pycbh.cbh_print(cbh)
print('General conversion from files:')
print(' filename(s) (.smi | .xyz | .mol) --> files2cbh --> cbh-n')
mixed_fns = [fn_smi, fn_xyz, fn_mol]
rungs = [0, 1, 2]
cbh = pycbh.files2cbh(mixed_fns, rungs)
pycbh.cbh_print(cbh)


def test_cbhlookup():
print('cbh reaction then energy lookup')
smi_str = 'O=Cc1ccc(O)c(OC)c1'
cbh=pycbh.smi2cbh(smi_str, [0,1,2,3])
pycbh.cbh_print(cbh)
print('cbh reaction then energy lookup')
smi_str = 'O=Cc1ccc(O)c(OC)c1'
cbh = pycbh.smi2cbh(smi_str, [0, 1, 2, 3])
pycbh.cbh_print(cbh)

methods = [['g4(0k)', 'zpe'], 'pbe-d3', ['g4(0k)', 'pbe-d3', 'zpe']]

methods=[['g4(0k)','zpe'],'pbe-d3',['g4(0k)','pbe-d3','zpe']]
key_fn = "fragment_lookup/keys.txt"
energy_fn = "fragment_lookup/lookup_energy.txt"

key_fn="fragment_lookup/keys.txt"
energy_fn="fragment_lookup/lookup_energy.txt"
cbh_e = pycbh.cbh_store2energy(cbh,
key_fn=key_fn,
energy_fn=energy_fn,
levels_of_theory=methods)

cbh_e = pycbh.cbh_store2energy(cbh, key_fn=key_fn, energy_fn=energy_fn, levels_of_theory=methods)
print('\nMethods : {}'.format(' '.join(
[x if type(x) != list else '-'.join(x) for x in methods])))

print('\nMethods : {}'.format(' '.join([x if type(x)!=list else '-'.join(x) for x in methods])))
for key in sorted(cbh_e.keys()):
if key != 'methods':
print('{} {}'.format(key, ' '.join([str(x) for x in cbh_e[key]])))

for key in sorted(cbh_e.keys()):
if key != 'methods':
print('{} {}'.format(key,' '.join([str(x) for x in cbh_e[key]])))

print('-'*50)
print('-' * 50)
test_xyzfile2cbh()
input('-'*50)
input('-' * 50)
test_xyzfile2cbh_batch()
input('-'*50)
input('-' * 50)
test_smistr2cbh()
input('-'*50)
input('-' * 50)
test_smistr2cbh_batch()
input('-'*50)
input('-' * 50)
test_molfile2cbh()
input('-'*50)
input('-' * 50)
test_mixedinp2cbh()
input('-'*50)
input('-' * 50)
test_cbhlookup()
print('-'*50)
print('-' * 50)
80 changes: 43 additions & 37 deletions fragment_lookup/mol2gjf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,48 @@
import subprocess
import re


def mol2gjf(fns):
if type(fns) != list:
fns=[fns]
goodf, badf = list(), list()
for fn in fns:
try:
outf=fn.replace('mol','gjf')
chkf=fn.replace('mol','chk')
inlines = open(fn, 'r').read().split('\n')
charge=0
spin_m=1
for line in inlines:
if re.findall('^M CHG ',line):
charge = line.split(' ')[-1]
elif re.findall('^M RAD ',line):
spin_m = line.split(' ')[-1]
bashCommand = 'obabel -imol {} -oxyz --gen3d -xb'.format(fn)
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
output=output.decode("utf-8").split("\n")
with open('gjf_files/'+outf, 'w') as FILE:
FILE.write('%chk={}\n%mem=7GB\n%nproc=2\n'.format(chkf))
FILE.write('# g4\n\n') #opt freq=(scale=0.9854) b3lyp/6-31G(2df,p)\n\n')
FILE.write('pyCBH fragment file ({}) : {}\n\n'.format(fn,output[1]))
FILE.write('{} {}\n'.format(charge, spin_m))
FILE.write('\n'.join(output[2::])+'\n')
print('written to gjf_files/{}'.format(outf))
goodf.append(outf)
except:
print('ERROR: {} failed to write'.format(outf))
badf.append(outf)
pass
return [goodf, badf]
if type(fns) != list:
fns = [fns]
goodf, badf = list(), list()
for fn in fns:
try:
outf = fn.replace('mol', 'gjf')
chkf = fn.replace('mol', 'chk')
inlines = open(fn, 'r').read().split('\n')
charge = 0
spin_m = 1
for line in inlines:
if re.findall('^M CHG ', line):
charge = line.split(' ')[-1]
elif re.findall('^M RAD ', line):
spin_m = line.split(' ')[-1]
bashCommand = 'obabel -imol {} -oxyz --gen3d -xb'.format(fn)
process = subprocess.Popen(bashCommand.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, error = process.communicate()
output = output.decode("utf-8").split("\n")
with open('gjf_files/' + outf, 'w') as FILE:
FILE.write('%chk={}\n%mem=7GB\n%nproc=2\n'.format(chkf))
FILE.write('# g4\n\n'
) #opt freq=(scale=0.9854) b3lyp/6-31G(2df,p)\n\n')
FILE.write('pyCBH fragment file ({}) : {}\n\n'.format(
fn, output[1]))
FILE.write('{} {}\n'.format(charge, spin_m))
FILE.write('\n'.join(output[2::]) + '\n')
print('written to gjf_files/{}'.format(outf))
goodf.append(outf)
except:
print('ERROR: {} failed to write'.format(outf))
badf.append(outf)
pass
return [goodf, badf]


if __name__=='__main__':
if len(sys.argv[1::]) < 1:
sys.exit('USAGE: python3 mol2gjf.py FILENAME(s)')
fns=sys.argv[1::]
_ = mol2gjf(fns)
if __name__ == '__main__':
if len(sys.argv[1::]) < 1:
sys.exit('USAGE: python3 mol2gjf.py FILENAME(s)')
fns = sys.argv[1::]
_ = mol2gjf(fns)
46 changes: 26 additions & 20 deletions pycbh.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,32 @@

from pycbh import cbh_print

if __name__=='__main__':
if len(sys.argv[1::]) < 2:
sys.exit('Usage python3 pycbh.py CBH_rung FILENAME(s)')
try:
rung_ls=[int(sys.argv[1])]
except:
sys.exit('Usage: python3 pycbh.py CBH_rung FILENAME(s)\n CBH rung must be an integer')
fns = sys.argv[2:]
'''
if __name__ == '__main__':
if len(sys.argv[1::]) < 2:
sys.exit('Usage python3 pycbh.py CBH_rung FILENAME(s)')
try:
rung_ls = [int(sys.argv[1])]
except:
sys.exit(
'Usage: python3 pycbh.py CBH_rung FILENAME(s)\n CBH rung must be an integer'
)
fns = sys.argv[2:]
'''
General conversion from files:
filename(s) (.smi | .xyz | .mol) --> files2cbh --> cbh-n
'''
cbh=pycbh.files2cbh(fns, rung_ls)
cbh_print(cbh)
methods=[['g4(0k)','zpe'],'pbe-d3',['g4(0k)','pbe-d3','zpe']]
key_fn="fragment_lookup/keys.txt"
energy_fn="fragment_lookup/lookup_energy.txt"
cbh_e = pycbh.cbh_store2energy(cbh, key_fn=key_fn, energy_fn=energy_fn, levels_of_theory=methods)
print('\nMethods : {}'.format(' '.join([x if type(x)!=list else '-'.join(x) for x in methods])))
for key in sorted(cbh_e.keys()):
if key != 'methods':
print('{} {}'.format(key,' '.join([str(x) for x in cbh_e[key]])))
print()
cbh = pycbh.files2cbh(fns, rung_ls)
cbh_print(cbh)
methods = [['g4(0k)', 'zpe'], 'pbe-d3', ['g4(0k)', 'pbe-d3', 'zpe']]
key_fn = "fragment_lookup/keys.txt"
energy_fn = "fragment_lookup/lookup_energy.txt"
cbh_e = pycbh.cbh_store2energy(cbh,
key_fn=key_fn,
energy_fn=energy_fn,
levels_of_theory=methods)
print('\nMethods : {}'.format(' '.join(
[x if type(x) != list else '-'.join(x) for x in methods])))
for key in sorted(cbh_e.keys()):
if key != 'methods':
print('{} {}'.format(key, ' '.join([str(x) for x in cbh_e[key]])))
print()
Loading

0 comments on commit 4e2bf7b

Please sign in to comment.