O , 2012-07-31 17:12 +0400, Чугунов Антон rakstīja:
> Dear open babel users,
> I'm adding hydrogens in python script
> using pep.OBMol.AddHydrogens(False, True, 7) function (where pep is
> pybel molecule). But after saving this to PDB file I've got a problem:
> all hydrogens are written AFTER all heavy atoms, but I don't want
> this. I'd like to have common representation where all hydrogens
> follow the atom where they are connected to.
> Is there a simple way to do this?
> Thanks,
>  
> Chugunov Anton.

It is working exactly as intended. If you want to change the ordering of
atoms then either you have to modify files manually or use OB API and
program this yourself.

Incidentally the need for this specific reordering of atoms is not
uncommon in molecular modeling. I also had a need for it so I have a
Python script for this, see attachment (you need Python 3 and OB Python
bindings for it to work).


Reinis
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Version 0.01
Author: Reinis Danne <rei4...@gmail.com>
License: GPLv3

Move all hydrogens next to the atoms they are bonded to.
"""

import os
import sys
import pybel

import openbabel as ob
from optparse import OptionParser


# Parse command line options.
usage="[python3] %prog -i file1 -o file2"
description="Move all hydrogens next to the atoms they are bonded to."
version="\n%prog Version 0.01\n\nRequires Python 3.0 or newer."
optParser = OptionParser(usage=usage,
                         version=version,
                         description=description)

optParser.set_defaults(inFN=None, outFN=None)
optParser.add_option('-i', type='str',
                     dest='inFN',
                     help="Input file name [default: %default]")
optParser.add_option('-o', type='str',
                     dest='outFN',
                     help="Output file name [default: %default]")

options, args = optParser.parse_args()


# Check if required parameters are given
if options.inFN is None or options.outFN is None:
    print(optParser.usage)
    sys.exit(1)


# Get input file name and format
fname = options.inFN
fformat = os.path.splitext(fname)[1][1:]

# Read input file
pybmol = pybel.readfile(fformat, fname).__next__()
mol = pybmol.OBMol

# Walk trough all atoms of the molecule and get their indices in order
atoms = []
for atom in ob.OBMolAtomIter(mol):
    # Check if hydrogen
    if atom.IsHydrogen():
        continue
    # Append heavy atom index to list
    atoms.append(atom.GetIdx())
    # Iterate trough bonded neighbors of heavy atom and put hydrogens in list
    for nbr in ob.OBAtomAtomIter(atom):
        if nbr.IsHydrogen():
            atoms.append(nbr.GetIdx())

# Reorder atoms in molecule
mol.RenumberAtoms(atoms)

# Write out molecule
offormat = os.path.splitext(options.outFN)[1][1:]
pybmol.write(offormat, options.outFN, overwrite=True)
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
OpenBabel-discuss mailing list
OpenBabel-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbabel-discuss

Reply via email to