Creating molecule object in python fails


#1

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


#2

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

#3

Works like charm.
Thank you very much,
Markus


#4

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.