Dear Noel, Thank you for the answer. I was able to actually remove all hydrogens, merge the two molecules and reintroduce all the hydrogens. Nevertheless, what I am getting is not methylbenzene (C7H8) but just the same two molecules as before. I believe I also created a new bond but I am pretty sure it is not correct!
Here’s my code: >> mol1 = next(pb.readfile('xyz', 'benzene.xyz')) #loading molecules >> from file >> mol2 = next(pb.readfile('xyz', 'methane.xyz')) >> mol1.removeh() >> #removing loading molecules from file >> mol2.removeh() >> m1 = mol1.OBMol >> m2 = mol2.OBMol >> m1 += m2 >> #merging the two molecules >> m1.AddBond(1,2,1) ## how >> do I give the right indices here? >> new_mol = pb.Molecule(m1) >> new_mol.addh() >> print(len(new_mol.atoms)) 17 #this should rather be 15 Moreover I would like to check whether the bond I have inserted is correct. But I am not able to print something that is useful to understand >> print(m1.GetBond(1)) <openbabel.OBBond; proxy of <…>> Thank you Marco From: Noel O'Boyle <baoille...@gmail.com> Sent: Thursday, April 9, 2020 2:53 PM To: Marco Di Gennaro (TME) <marco.di.genn...@external.toyota-europe.com> Cc: openbabel-discuss@lists.sourceforge.net Subject: Re: [Open Babel] read molecules from xyz files and merge them CAUTION:This e-mail originated outside our organisation. Do not click links or open attachments unless you recognise the sender and know the content is safe. Use the Python keyword "next" on an iterator to get the next item: mol1 = next(pybel.readfile(....) You can add an OBMol to an existing one: m1 += m2 I'd recommend writing it out as SMILES at this point, just to check the structure: print(pybel.Molecule(m1).write("smi")) If the hydrogens are explicit in the XYZ, then you can call DeleteAtom() on the hydrogens to delete them. To create a bond, use AddBond(). See the Doxygen documentation. - Noel On Wed, 8 Apr 2020 at 17:31, Marco Di Gennaro (TME) <marco.di.genn...@external.toyota-europe.com<mailto:marco.di.genn...@external.toyota-europe.com>> wrote: Hello everyone, I have two molecules (benzene and methane) in two separate xyz files. I would like to create a methylbenzene molecule out of these and relax it with a UFF. I am new to pybel (and babel in general). I would like to: * open the two files * remove one hydrogen from the benzene and one from the methane * create a bond among the two carbons * perform UFF relaxation. So far I have tried several things, mostly trying to follow this discussion (which is a bit outdated actually): http://forums.openbabel.org/OBGenericData-and-OBPairData-td4651441.html#a4651899<https://eur01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fforums.openbabel.org%2FOBGenericData-and-OBPairData-td4651441.html%23a4651899&data=02%7C01%7CMarco.Di.Gennaro%40external.toyota-europe.com%7C10b274805dc7452f8f4f08d7dc84fc3e%7C52b742d13dc247acbf03609c83d9df9f%7C0%7C0%7C637220336034857892&sdata=QAp9Sv6sV%2F3pVgSQKZw95CvY6PBMAjcS%2BwkcHCzGV4k%3D&reserved=0> I could not get much out of it for the moment: * I could read the files with readfile(), but why do I get an iterator? * Is OBBuilder() what I am looking for? How to use it properly? * Should I rather use OBPairData() ? * I know I can use -d to remove (all) hydrogens in babel. How do I do it for specific atoms in pybel? Below you can find the little code I could write at the moment. I am attaching the two xyz files. Any help would be greatly appreciated. Thank you. Marco import pybel as pb mol1 = [ m for m in pb.readfile('xyz', 'benzene.xyz<https://eur01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fbenzene.xyz%2F&data=02%7C01%7CMarco.Di.Gennaro%40external.toyota-europe.com%7C10b274805dc7452f8f4f08d7dc84fc3e%7C52b742d13dc247acbf03609c83d9df9f%7C0%7C0%7C637220336034867879&sdata=Q9g%2BvSeWh8EX8pQJsQv%2BruRJyEGryZQNoh9DhGs2dnc%3D&reserved=0>')][0] mol2 = [ m for m in pb.readfile('xyz', 'methane.xyz<https://eur01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmethane.xyz%2F&data=02%7C01%7CMarco.Di.Gennaro%40external.toyota-europe.com%7C10b274805dc7452f8f4f08d7dc84fc3e%7C52b742d13dc247acbf03609c83d9df9f%7C0%7C0%7C637220336034867879&sdata=xN1CRrauPYnV53lgBO2jZBEg9qnYi4c4l7UTTFJO160%3D&reserved=0>')][0] mol1 + mol2 # This gives a TypeError: unsupported operand type(s) for +: ‘Molecule’ and ‘Molecule’ m1 = mol1.OBMol m2 = mol2.OBMol m3 = m1 + m2 # This gives a TypeError: unsupported operand type(s) for +: ‘OBMol’ and ‘OBMol’ == Marco Di Gennaro, Ph.D. Toyota Motor Europe This e-mail may contain confidential information. If you are not an addressee or otherwise authorised to receive this message, you should not use, copy, disclose or take any action based on this e-mail. If you have received this e-mail in error, please inform the sender promptly and delete this message and any attachments immediately. _______________________________________________ OpenBabel-discuss mailing list OpenBabel-discuss@lists.sourceforge.net<mailto:OpenBabel-discuss@lists.sourceforge.net> https://lists.sourceforge.net/lists/listinfo/openbabel-discuss<https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.sourceforge.net%2Flists%2Flistinfo%2Fopenbabel-discuss&data=02%7C01%7CMarco.Di.Gennaro%40external.toyota-europe.com%7C10b274805dc7452f8f4f08d7dc84fc3e%7C52b742d13dc247acbf03609c83d9df9f%7C0%7C0%7C637220336034877880&sdata=ZbE1GQYDPSySF9Iv9iQ2ovxh%2B6ImMlLv34R3VBn6k1E%3D&reserved=0> This e-mail may contain confidential information. If you are not an addressee or otherwise authorised to receive this message, you should not use, copy, disclose or take any action based on this e-mail. If you have received this e-mail in error, please inform the sender promptly and delete this message and any attachments immediately.
_______________________________________________ OpenBabel-discuss mailing list OpenBabel-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbabel-discuss