Creating molecule object in python fails

Hello:
I have successfully tried the psithon examples from the website. So, my python installation, the psi4 installation is working.

My plan is to use rdkit and psi4 in the same script.
Assume I am reading with rdkit a molecule. Next I am extracting the x,y,z coordinates of the atoms and create a triple quoted variable. This variable I would like to use in psithon.
Here is my simple script:

Blockquote
import os, sys
import psi4
from rdkit import Chem

Blockquote
mol = Chem.MolFromMol2File(β€˜./MAP.mol2’,removeHs=False)

Blockquote
xyz = β€˜β€œβ€"’ + β€œ\n”
xyz += β€œ0 1 " + β€œ\n”
for counter in range(mol.GetNumAtoms()):
pos = mol.GetConformer().GetAtomPosition(counter)
xyz += str((”%s %12.8f %12.8f %12.8f\n" % (’ ’ + mol.GetAtomWithIdx(counter).GetSymbol(), pos.x, pos.y, pos.z)))
xyz += β€˜β€œβ€"’

Blockquote
print(xyz)
psi4mol = psi4.geometry(xyz)
psi4mol.update_geometry()

But this fails with an error message:

Blockquote
Traceback (most recent call last):
File β€œ./test_my1.py”, line 17, in
psi4mol = psi4.geometry(xyz)
File β€œ/home/mmetz/anaconda3/envs/my-rdkit-env/lib/python3.6/site-packages/psi4/driver/molutil.py”, line 305, in geometry
molecule = core.Molecule.create_molecule_from_string(geom)
RuntimeError:
Fatal Error: Illegal atom symbol in geometry specification: β€œβ€" on line
β€œβ€"
Error occurred in file: /scratch/psilocaluser/conda-builds/psi4-multiout_1532493090788/work/psi4/src/psi4/libmints/molecule.cc on line: 1205
The most recent 5 function calls were:

Blockquote
psi::Molecule::create_molecule_from_string(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)

I have tried to create the triple quoted variable in different ways such as using β€œ"""” but this did not work.
What worked is if I create the molecule in the same way as in the examples:
q4mol = ps4.geometry(β€œβ€" … β€œβ€")

So, to be clear, I am having problems creating the triple quoted variable in the fly.

Not sure what I am missing.
Best regards,
Markus

The triple quote isn’t part of the variable value; it’s just tells Python that the next lines are a multiline string, exactly like the single quote tells Python that what comes next is a string. When you add triple quotes to the string value, Psi sees those and is confused about what atom a quotation mark is supposed to be. Just get rid of them.

string1 = """H 0 0 0
H 0 0 0"""

string2 = "H 0 0 0" + "\n" + "H 0 0 0"

string3 = '"""' + "H 0 0 0" + "\n" + "H 0 0 0" + '"""'

print(string1 == string2) # True
print(string1 == string3) # False

Works like charm.
Thank you very much,
Markus

If you’d rather build your molecule by API (presumably one can access geometry and element lists through rdkit), you can use this constructor instead https://github.com/psi4/psi4/blob/master/tests/isapt1/input.dat#L52-L82 . Approx docs here.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.