def getHBAtoms(mol):
    """
    Function to identify donor and acceptor atoms for hydrogen bonds

    d2, d3, a2, a3 <- getHBAtoms(mol)

    The function will first build bonds then assign atom hybridization
    and bond orders. Next it will use a HydrogenBondBuilder to identify and'
    report donor and acceptor atoms in sets corresponding to Sp2 and Sp3
    hybridization.
    A .hbstatus is added to all atoms. The value can be:
      NE: NEutral
      D2: Sp2 donor 
      D3: Sp3 donor 
      A2: Sp2 acceptor
      A3: Sp3 acceptor
      B2: Sp2 acceptor and donor
      B3: Sp3 acceptor and donor
    """
    # add bonds
    mol.buildBondsByDistance()
    atoms = mol.allAtoms

    # assign .babel_type
    from PyBabel.atomTypes import AtomHybridization
    atyper = AtomHybridization()
    atyper.assignHybridization(atoms)

    # assign bond order
    from PyBabel.bo import BondOrder
    bond_orderer = BondOrder()
    bond_orderer.assignBondOrder(atoms, atoms.bonds[0])

    # get donors
    from MolKit.hydrogenBondBuilder import HydrogenBondBuilder
    hbbuilder = HydrogenBondBuilder()
    donorTypes = hbbuilder.paramDict['donorTypes']
    donorsp2Ats, donorsp3Ats = hbbuilder.getHBDonors(atoms, donorTypes)

    # get acceptors
    acceptorTypes = hbbuilder.paramDict['acceptorTypes']
    acceptorsp2Ats, acceptorsp3Ats = hbbuilder.getHBAcceptors(atoms, acceptorTypes)
    # create hbstatus attribute
    atoms.hbstatus = 'NE'
    for a in donorsp2Ats:
        a.hbstatus = 'D2'
    for a in donorsp3Ats:
        a.hbstatus = 'D3'
    for a in acceptorsp2Ats:
        a.hbstatus = 'A2'
    for a in acceptorsp3Ats:
        a.hbstatus = 'A3'

    # handle atoms that are both donors and acceptor
    for a in donorsp2Ats.inter(acceptorsp2Ats):
        a.hbstatus = 'B2'
    for a in donorsp3Ats.inter(acceptorsp3Ats):
        a.hbstatus = 'B3'

    return donorsp2Ats, donorsp3Ats, acceptorsp2Ats, acceptorsp3Ats
 def check_babel_types(self, ats):
     num_ats = len(ats)
     num_babel_type = len(ats.get(lambda x: hasattr(x, 'babel_type')))
     num_bnd_type = len(ats.get(lambda x: hasattr(x, 'bnd_type')))
     if (num_babel_type!=num_ats) or (num_bnd_type!=num_ats):
         babel = AtomHybridization()
         bond_orderer = BondOrder()
         tops = ats.top.uniq()
         for mol in tops: 
             babel.assignHybridization(mol.allAtoms)
             bond_orderer.assignBondOrder(mol.allAtoms, mol.allAtoms.bonds[0])
             mol.allAtoms._bndtyped = 1
 def check_babel_types(self, ats):
     try:
         ats.babel_type
         ats._bndtyped
     except AttributeError:
         babel = AtomHybridization()
         bond_orderer = BondOrder()
         tops = ats.top.uniq()
         for mol in tops:
             babel.assignHybridization(mol.allAtoms)
             bond_orderer.assignBondOrder(mol.allAtoms, mol.allAtoms.bonds[0])
             mol.allAtoms._bndtyped = 1
Example #4
0
 def check_babel_types(self, ats):
     num_ats = len(ats)
     num_babel_type = len(ats.get(lambda x: hasattr(x, 'babel_type')))
     num_bnd_type = len(ats.get(lambda x: hasattr(x, 'bnd_type')))
     if (num_babel_type != num_ats) or (num_bnd_type != num_ats):
         babel = AtomHybridization()
         bond_orderer = BondOrder()
         tops = ats.top.uniq()
         for mol in tops:
             babel.assignHybridization(mol.allAtoms)
             bond_orderer.assignBondOrder(mol.allAtoms,
                                          mol.allAtoms.bonds[0])
             mol.allAtoms._bndtyped = 1
 def select(self, bnds, bondOrder=1):
     ats = self.getAtoms(bnds)
     mols = ats.top.uniq()
     atype = AtomHybridization()
     for m in mols:
         if not m.chains[0].hasBonds:
             m.buildBondsByDistance()
         allAts = m.chains.residues.atoms
         atype.assignHybridization(allAts)
         rf = RingFinder()
         rf.findRings2(allAts, allAts.bonds[0])
         bo = BondOrder()
         bo.assignBondOrder(allAts, allAts.bonds[0], rf)
     return BondSet(bnds.get(lambda x, ord=bondOrder: x.bondOrder==ord))
 def select(self, bnds, bondOrder=1):
     ats = self.getAtoms(bnds)
     mols = ats.top.uniq()
     atype = AtomHybridization()
     for m in mols:
         if not m.chains[0].hasBonds:
             m.buildBondsByDistance()
         allAts = m.chains.residues.atoms
         atype.assignHybridization(allAts)
         rf = RingFinder()
         rf.findRings2(allAts, allAts.bonds[0])
         bo = BondOrder()
         bo.assignBondOrder(allAts, allAts.bonds[0], rf)
     return BondSet(bnds.get(lambda x, ord=bondOrder: x.bondOrder == ord))
 def select(self, bnds):
     ats = self.getAtoms(bnds)
     rf = RingFinder()
     #nb maxSize of ring is 20 by default
     rf.findRings2(ats, bnds, maxSize=len(ats))
     ats = self.getAtoms(rf.allRingBonds)
     allAts = ats.top.uniq().allAtoms
     atype = AtomHybridization()
     atype.assignHybridization(allAts)
     bo = BondOrder()
     bo.assignBondOrder(allAts, allAts.bonds[0], rf)
     arom = Aromatic(rf)
     arom.find_aromatic_atoms(ats)
     aromatic_bnds = ats.bonds[0].get(lambda x: x.bondOrder=='aromatic')
     aromatic_ats = self.getAtoms(aromatic_bnds)
     aromatic_ats.aromatic = 1
     return aromatic_bnds
 def select(self, bnds):
     ats = self.getAtoms(bnds)
     rf = RingFinder()
     #nb maxSize of ring is 20 by default
     rf.findRings2(ats, bnds, maxSize=len(ats))
     ats = self.getAtoms(rf.allRingBonds)
     allAts = ats.top.uniq().allAtoms
     atype = AtomHybridization()
     atype.assignHybridization(allAts)
     bo = BondOrder()
     bo.assignBondOrder(allAts, allAts.bonds[0], rf)
     arom = Aromatic(rf)
     arom.find_aromatic_atoms(ats)
     aromatic_bnds = ats.bonds[0].get(lambda x: x.bondOrder == 'aromatic')
     aromatic_ats = self.getAtoms(aromatic_bnds)
     aromatic_ats.aromatic = 1
     return aromatic_bnds
Example #9
0
def getHBAtoms(mol):
    """
    Function to identify donor and acceptor atoms for hydrogen bonds

    d2, d3, a2, a3 <- getHBAtoms(mol)

    The function will first build bonds then assign atom hybridization
    and bond orders. Next it will use a HydrogenBondBuilder to identify and'
    report donor and acceptor atoms in sets corresponding to Sp2 and Sp3
    hybridization.
    A .hbstatus is added to all atoms. The value can be:
      NE: NEutral
      D2: Sp2 donor 
      D3: Sp3 donor 
      A2: Sp2 acceptor
      A3: Sp3 acceptor
      B2: Sp2 acceptor and donor
      B3: Sp3 acceptor and donor
    """
    # add bonds
    mol.buildBondsByDistance()
    atoms = mol.allAtoms

    # assign .babel_type
    from PyBabel.atomTypes import AtomHybridization
    atyper = AtomHybridization()
    atyper.assignHybridization(atoms)

    # assign bond order
    from PyBabel.bo import BondOrder
    bond_orderer = BondOrder()
    bond_orderer.assignBondOrder(atoms, atoms.bonds[0])

    # get donors

    hbbuilder = HydrogenBondBuilder()
    donorTypes = hbbuilder.paramDict['donorTypes']
    donorsp2Ats, donorsp3Ats = hbbuilder.getHBDonors(atoms, donorTypes)

    # get acceptors
    acceptorTypes = hbbuilder.paramDict['acceptorTypes']
    acceptorsp2Ats, acceptorsp3Ats = hbbuilder.getHBAcceptors(
        atoms, acceptorTypes)
    # create hbstatus attribute
    atoms.hbstatus = 'NE'
    for a in donorsp2Ats:
        a.hbstatus = 'D2'
    for a in donorsp3Ats:
        a.hbstatus = 'D3'
    for a in acceptorsp2Ats:
        a.hbstatus = 'A2'
    for a in acceptorsp3Ats:
        a.hbstatus = 'A3'

    # handle atoms that are both donors and acceptor
    for a in donorsp2Ats.inter(acceptorsp2Ats):
        a.hbstatus = 'B2'
    for a in donorsp3Ats.inter(acceptorsp3Ats):
        a.hbstatus = 'B3'

    return donorsp2Ats, donorsp3Ats, acceptorsp2Ats, acceptorsp3Ats
Example #10
0
    def addHydrogens(self, mol):
        #check for bonds
        if len(mol.allAtoms.bonds[0]) == 0:
            mol.buildBondsByDistance()
        bonds = mol.allAtoms.bonds[0]
        #could have preset babel_types
        #so check if allAtoms are already typed
        try:
            t = mol.allAtoms.babel_type
        except:
            #if all are not pretyped, type them
            babel = AtomHybridization()
            babel.assignHybridization(mol.allAtoms)

        if self.method == 'withBondOrder':
            mol.rings = RingFinder()
            mol.rings.findRings2(mol.allAtoms, mol.allAtoms.bonds[0])
            mol.rings.bondRings = {}
            for ind in xrange(len(mol.rings.rings)):
                r = mol.rings.rings[ind]
                for b in r['bonds']:
                    if not mol.rings.bondRings.has_key(b):
                        mol.rings.bondRings[b] = [
                            ind,
                        ]
                    else:
                        mol.rings.bondRings[b].append(ind)
            bo = BondOrder()
            bo.assignBondOrder(mol.allAtoms, bonds, mol.rings)
            mol.allAtoms._bndtyped = 1
            # do aromatic here
            arom = Aromatic(mol.rings)
            arom.find_aromatic_atoms(mol.allAtoms)

        hat = AddHydrogens().addHydrogens(mol.allAtoms, method=self.method)
        bondedAtomDict = {}  # key is heavy atom
        for a in hat:
            if bondedAtomDict.has_key(a[1]):
                bondedAtomDict[a[1]].append(a)
            else:
                bondedAtomDict[a[1]] = [a]

        # now create Atom object for hydrogens
        # and add the to the residues's atom list
        molNewHs = AtomSet([])  # list of created H atoms for this molecule
        heavyAtoms = AtomSet([])  # list of atoms that need new radii

        for heavyAtom, HatmsDscr in bondedAtomDict.items():
            #don't add hydrogens to carbons: polar Only!!!
            if self.htype != 'all' and heavyAtom.element == 'C':
                continue
            res = heavyAtom.parent
            # find where to insert H atom
            childIndex = res.children.index(heavyAtom) + 1

            # loop over H atoms description to be added
            # start at the end to number correctly
            l = len(HatmsDscr)
            for i in range(l - 1, -1, -1):
                a = HatmsDscr[i]
                # build H atom's name
                if len(heavyAtom.name) == 1:
                    name = 'H' + heavyAtom.name
                else:
                    name = 'H' + heavyAtom.name[1:]

                # if more than 1 H atom, add H atom index
                # for instance HD11, HD12, Hd13 (index is 1,2,3)
                if l > 1:
                    name = name + str(i + 1)

                # create the H atom object
                atom = Atom(name,
                            res,
                            top=heavyAtom.top,
                            chemicalElement='H',
                            childIndex=childIndex,
                            assignUniqIndex=0)

                # set atoms attributes
                atom._coords = [a[0]]
                if hasattr(a[1], 'segID'): atom.segID = a[1].segID
                atom.hetatm = 0
                atom.alternate = []
                #atom.element = 'H'
                atom.occupancy = 1.0
                atom.conformation = 0
                atom.temperatureFactor = 0.0
                atom.babel_atomic_number = a[2]
                atom.babel_type = a[3]
                atom.babel_organic = 1
                atom.radius = 1.2

                # create the Bond object bonding Hatom to heavyAtom
                bond = Bond(a[1], atom, bondOrder=1)

                # add the created atom the the list
                molNewHs.append(atom)
                # in case this new hydrogen atom ever ends up in pmv
                # HAVE TO CREATE THESE ENTRIES
                # create the color entries for all geoemtries
                # available for the heavyAtom
                for key, value in heavyAtom.colors.items():
                    atom.colors[key] = (0.0, 1.0, 1.0)
                    atom.opacities[key] = 1.0

        mol.allAtoms = mol.chains.residues.atoms
        if self.renumber:
            mol.allAtoms.number = range(1, len(mol.allAtoms) + 1)
        return len(molNewHs)
    def addHydrogens(self, mol):
        #check for bonds
        if len(mol.allAtoms.bonds[0])==0:
            mol.buildBondsByDistance()
        bonds = mol.allAtoms.bonds[0]
        #could have preset babel_types
        #so check if allAtoms are already typed
        try:
            t = mol.allAtoms.babel_type
        except:
            #if all are not pretyped, type them
            babel = AtomHybridization()
            babel.assignHybridization(mol.allAtoms)

        if self.method=='withBondOrder':
            mol.rings = RingFinder()
            mol.rings.findRings2(mol.allAtoms, mol.allAtoms.bonds[0])
            mol.rings.bondRings = {}
            for ind in xrange(len(mol.rings.rings)):
                r = mol.rings.rings[ind]
                for b in r['bonds']:
                    if not mol.rings.bondRings.has_key(b):
                        mol.rings.bondRings[b] = [ind,]
                    else:
                        mol.rings.bondRings[b].append(ind)
            bo = BondOrder()
            bo.assignBondOrder(mol.allAtoms, bonds, mol.rings)
            mol.allAtoms._bndtyped = 1
            # do aromatic here
            arom = Aromatic(mol.rings)
            arom.find_aromatic_atoms(mol.allAtoms)
            
        hat = AddHydrogens().addHydrogens(mol.allAtoms, method=self.method)
        bondedAtomDict = {}  # key is heavy atom
        for a in hat:
            if bondedAtomDict.has_key(a[1]):
                bondedAtomDict[a[1]].append(a)
            else:
                bondedAtomDict[a[1]] = [a]

        # now create Atom object for hydrogens
        # and add the to the residues's atom list
        molNewHs = AtomSet([]) # list of created H atoms for this molecule
        heavyAtoms = AtomSet([]) # list of atoms that need new radii
        
        for heavyAtom, HatmsDscr in bondedAtomDict.items():
            #don't add hydrogens to carbons: polar Only!!!
            if self.htype!='all' and heavyAtom.element=='C': 
                continue
            res = heavyAtom.parent
            # find where to insert H atom
            childIndex = res.children.index(heavyAtom)+1

            # loop over H atoms description to be added
            # start at the end to number correctly
            l = len(HatmsDscr)
            for i in range(l-1,-1,-1):
                a = HatmsDscr[i]
                # build H atom's name
                if len(heavyAtom.name)==1:
                    name = 'H' + heavyAtom.name
                else:
                    name = 'H' + heavyAtom.name[1:]

                # if more than 1 H atom, add H atom index
                # for instance HD11, HD12, Hd13 (index is 1,2,3)
                if l > 1:
                    name = name + str(i+1)

                # create the H atom object
                atom = Atom(name, res, top=heavyAtom.top,
                            chemicalElement='H',
                            childIndex=childIndex, assignUniqIndex=0)

                # set atoms attributes
                atom._coords = [ a[0] ]
                if hasattr(a[1], 'segID'): atom.segID = a[1].segID
                atom.hetatm = 0
                atom.alternate = []
                #atom.element = 'H'
                atom.occupancy = 1.0
                atom.conformation = 0
                atom.temperatureFactor = 0.0
                atom.babel_atomic_number = a[2]
                atom.babel_type = a[3]
                atom.babel_organic = 1
                atom.radius = 1.2
                
                # create the Bond object bonding Hatom to heavyAtom
                bond = Bond( a[1], atom, bondOrder=1)

                # add the created atom the the list
                molNewHs.append(atom)
                # in case this new hydrogen atom ever ends up in pmv 
                # HAVE TO CREATE THESE ENTRIES 
                # create the color entries for all geoemtries
                # available for the heavyAtom
                for key, value in heavyAtom.colors.items():
                    atom.colors[key]=(0.0, 1.0, 1.0)
                    atom.opacities[key]=1.0

        mol.allAtoms = mol.chains.residues.atoms
        if self.renumber:
            mol.allAtoms.number = range(1, len(mol.allAtoms)+1)
        return len(molNewHs)