Creating molecule object in python fails

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:

import os, sys
import psi4
from rdkit import Chem

mol = Chem.MolFromMol2File(’./MAP.mol2’,removeHs=False)

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 += β€˜"""’

psi4mol = psi4.geometry(xyz)

But this fails with an error message:

Traceback (most recent call last):
File β€œ./”, line 17, in
psi4mol = psi4.geometry(xyz)
File β€œ/home/mmetz/anaconda3/envs/my-rdkit-env/lib/python3.6/site-packages/psi4/driver/”, line 305, in geometry
molecule = core.Molecule.create_molecule_from_string(geom)
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/ on line: 1205
The most recent 5 function calls were:

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,

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,

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 . Approx docs here.

1 Like

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