예제 #1
0
def makeMoleculeFromAtoms(molname, atomSet):
    """
    create a new molecule from a list of atoms

    mol <- makeMoleculeFromAtoms(molname, atomSet)
"""
    from MolKit.molecule import Atom, AtomSet
    from MolKit.protein import Protein, Chain, Residue

    # create the top object
    mol = Protein(name=molname)

    # find out all residues
    residues = atomSet.parent.uniq()

    # find out all chains
    chains = residues.parent.uniq()

    # create all chains
    chainsd = {}
    for c in chains:
        newchain = Chain(c.id, mol, top=mol)
        chainsd[c] = newchain

    # create all residues
    resd = {}
    for res in residues:
        newres = Residue(res.name[:3],
                         res.name[3:],
                         res.icode,
                         chainsd[res.parent],
                         top=mol)
        resd[res] = newres
        newres.hasCA = 0
        newres.hasO = 0

    # create all the atoms
    newats = []
    for num, at in enumerate(atomSet):
        name = at.name
        res = resd[at.parent]
        name1 = name
        if hasattr(at, "altname") and at.altname != None:
            name = at.name.split("@")[0]
        if name == 'CA':
            res.hasCA = 1
        if name == 'O' or name == 'OXT' or (len(name) > 3
                                            and name[:3] == 'OCT'):
            res.hasO = 2

        newat = Atom(name, res, at.element, top=mol)
        if name != name1:
            newat.name = name1
            newat.altname = at.altname
        newats.append(newat)
        # set constructotr attributes
        newat._coords = []
        for coords in at._coords:
            newat._coords.append(coords[:])
        newat.conformation = at.conformation
        newat.chemElem = at.chemElem
        newat.atomicNumber = at.atomicNumber
        newat.bondOrderRadius = at.bondOrderRadius
        newat.covalentRadius = at.covalentRadius
        newat.vdwRadius = at.vdwRadius
        newat.maxBonds = at.maxBonds
        newat.organic = at.organic
        newat.colors = at.colors.copy()
        newat.opacities = at.opacities.copy()
        newat._charges = at._charges.copy()
        newat.chargeSet = at.chargeSet

        # set attributes from PDB parser
        try:  # pdbqs do not have this
            newat.segID = at.segID
        except AttributeError:
            pass
        newat.hetatm = at.hetatm
        try:  # pdbqs do not have this
            newat.normalname = at.normalname
        except AttributeError:
            pass
        newat.number = num  #at.number
        newat.occupancy = at.occupancy
        newat.temperatureFactor = at.temperatureFactor
        newat.altname = at.altname

        # attribute created by PQR parser
        if hasattr(at, 'pqrRadius'):
            newat.pqrRadius = at.pqrRadius

        # attribute created by F2D parser
        if hasattr(at, 'hbstatus'):
            newat.hbstatus = at.hbstatus

        # attribute created by PDBQ parser
        if hasattr(at, 'autodock_element'):
            newat.autodock_element = at.autodock_element

        # attribute created by PDBQT parser
        #if hasattr(at, ''):
        #    newat. = at.

        # attribute created by PDBQS parser
        if hasattr(at, 'AtVol'):
            newat.AtVol = at.AtVol
            newat.AtSolPar = at.AtSolPar

    mol.allAtoms = AtomSet(newats)
    return mol
예제 #2
0
    def parse_PDB_ATOM_record(self, rec):
        """Parse PDB ATOM records using the pdb columns specifications"""
        self.atomCounter = self.atomCounter + 1  # not sure about altLoc
        if self.specType == 'i':
            rec = rec.split()
        # Handle the alternate location using a flag.
        altLoc = self.get_Field_Value(rec, 'altLoc')
        if altLoc != ' ':
            self.altLoc = altLoc
        else:
            self.altLoc = ''  # changed from None to ''

        # check for chains break
        # self.modlflag = modlflag
        # chainID = rec[21]+ modlflag
        hascid = 1
        chainID = self.get_Field_Value(rec, 'chainID')
        if not chainID:
            hascid = 0
            chainID = str(self.chaincounter)  ## should be unk???
        if chainID != self.mol.curChain.id:
            # has to check if the chain exists already or not !!!
            if not self.mol.chains.id or chainID not in self.mol.chains.id or hascid == 0:
                self.chaincounter = self.chaincounter + 1
                if hascid == 0:
                    chainID = str(self.chaincounter)
                self.mol.curChain = Chain(chainID, self.mol, top=self.mol)
                self.residueCounter = 0
            else:
                self.mol.curChain = self.mol.chains.get(chainID)[0]

        # check for residue break
        resName = self.get_Field_Value(rec, 'resName')
        resSeq = self.get_Field_Value(rec, 'resSeq').strip()
        # WARNING reSeq is a STRING
        noresSeq = 0
        if not resSeq and resName == self.mol.curRes.type and resName != 'HOH':
            noresSeq = 1
            resSeq = self.mol.curRes.number
        if resSeq != self.mol.curRes.number or \
                resName != self.mol.curRes.type:
            # check if this residue already exists
            na = resName.strip() + resSeq.strip()
            res = self.mol.curChain.get(na)
            if res:
                self.mol.curRes = res[0]
            else:
                self.residueCounter = self.residueCounter + 1
                if resName == 'HOH':
                    self.HOHCounter = self.HOHCounter + 1
                if not resSeq:
                    if resName == 'HOH':
                        resSeq = self.HOHCounter
                    else:
                        resSeq = self.residueCounter
                ## FIXME icodes are ignored
                self.mol.curRes = Residue(resName,
                                          resSeq,
                                          '',
                                          self.mol.curChain,
                                          top=self.mol)
        icode = self.get_Field_Value(rec, 'iCode')
        if not icode:
            pass
        elif icode != ' ':
            self.mol.curRes.icode = icode

        # parse atom info

        # handle atom names (calcium, hydrogen) and find element type
        # check validity of chemical element column and charge column
        ## only works if 'name' is in the pdb format!  FIX!
        n = self.get_Field_Value(rec, 'name')
        el = self.get_Field_Value(rec, 'element')
        if n:
            name, element = self.getPDBAtomName(n, el)
            # if there is not resSeq spec, use first N to figure out new res
            if noresSeq and name == 'N':
                At = self.mol.curRes.get('N')
                if At:
                    self.residueCounter = self.residueCounter + 1
                    resSeq = self.residueCounter
                    self.mol.curRes = Residue(resName,
                                              resSeq,
                                              self.mol.curChain,
                                              top=self.mol)
            atom = Atom(name, self.mol.curRes, element, top=self.mol)
        else:
            element = el
            if element:
                atom = Atom(parent=self.mol.curRes,
                            chemicalElement=element,
                            top=self.mol)
            else:
                atom = Atom(parent=self.mol.curRes, top=self.mol)
        ##          elem = string.lower(element)          # moved to getPDBAtomName
        ##          if elem =='lp' or elem =='ld':
        ##              element = 'Xx'
        atom.charge = self.get_Field_Value(rec, 'charge')
        # should have atom.charge if no charge?
        # coords are required; where to set default or check?
        xcoord = self.get_Field_Value(rec, 'x')
        ycoord = self.get_Field_Value(rec, 'y')
        zcoord = self.get_Field_Value(rec, 'z')
        assert xcoord and ycoord and zcoord
        atom._coords = [[float(xcoord), float(ycoord), float(zcoord)]]
        atom.segID = self.get_Field_Value(rec, 'segID').strip()
        if rec[:4] == 'ATOM' or rec[0] == 'ATOM':
            atom.hetatm = 0
        else:
            atom.hetatm = 1
        # atom.alternate = []
        atom.element = element
        num = self.get_Field_Value(rec, 'serial')
        if num:
            atom.number = int(num)
        else:
            atom.number = self.atomCounter
        occupancy = self.get_Field_Value(rec, 'occupancy')
        if occupancy:
            atom.occupancy = float(occupancy)
        # must check that it is a number
        atom.conformation = 0
        tempFactor = self.get_Field_Value(rec, 'tempFactor')
        if tempFactor:
            atom.temperatureFactor = float(tempFactor)

        # add in user defined fields to atom attributes
        for field_name in list(self.recSpecs.UserFieldsDict.keys()):
            value = self.get_Field_Value(rec, field_name)
            type = self.recSpecs.get(field_name, 'var_type')
            if value:
                if type == 'int':
                    atom.__setattr__(field_name, int(value))
                elif type == 'float':
                    atom.__setattr__(field_name, float(value))
                else:
                    atom.__setattr__(field_name, value)
            else:
                atom.__setattr__(field_name, value)

        if self.altLoc:
            # check if the name of the atom is the same than the
            # name of the previous atom .
            name = name + '@' + self.altLoc
            atom.name = name
            if len(self.mol.curRes.atoms) > 1:
                # the new atom has been add to the current residue
                # You have to go to the one before.
                lastAtom = self.mol.curRes.atoms[-2]
                altname = lastAtom.name.split('@')[0]
                if name.split('@')[0] == altname:
                    # Add the new alternate atom to the LastAtom.alternate and
                    # add the lastAtom to the atom.alternate.
                    lastAtom.alternate.append(atom)
                    atom.alternate.append(lastAtom)
                    for l in lastAtom.alternate:
                        if atom.name != l.name:
                            atom.alternate.append(l)
                            l.alternate.append(atom)
        return atom
예제 #3
0
    def parse_PDB_ATOM_record(self, rec):
        """Parse PDB ATOM records using the pdb columns specifications"""
        self.atomCounter = self.atomCounter + 1  # not sure about altLoc
        if self.specType=='i': rec = string.split(rec)
        # Handle the alternate location using a flag.
        altLoc = self.get_Field_Value(rec, 'altLoc')
        if altLoc!= ' ': self.altLoc = altLoc
        else: self.altLoc = ''   # changed from None to ''

        # check for chains break
        #self.modlflag = modlflag
        #chainID = rec[21]+ modlflag
        hascid = 1
        chainID = self.get_Field_Value(rec, 'chainID')
        if not chainID:
            hascid = 0
            chainID = str(self.chaincounter)  ## should be unk???
        if chainID != self.mol.curChain.id :
            # has to check if the chain exists already or not !!!
            if not self.mol.chains.id or not chainID in self.mol.chains.id or \
               hascid==0:
                self.chaincounter = self.chaincounter + 1
                if hascid==0: chainID = str(self.chaincounter)
                self.mol.curChain = Chain(chainID, self.mol, top=self.mol)
                self.residueCounter = 0
            else:
                self.mol.curChain = self.mol.chains.get(chainID)[0]

        # check for residue break
        resName = self.get_Field_Value(rec, 'resName')
        resSeq = string.strip(self.get_Field_Value(rec, 'resSeq'))
        #WARNING reSeq is a STRING
        noresSeq = 0
        if not resSeq and resName==self.mol.curRes.type and resName!='HOH':
            noresSeq = 1
            resSeq = self.mol.curRes.number
        if resSeq != self.mol.curRes.number or \
           resName != self.mol.curRes.type:
            # check if this residue already exists
            na = string.strip(resName) + string.strip(resSeq)
            res = self.mol.curChain.get( na )
            if res:
                self.mol.curRes = res[0]
            else:
                self.residueCounter = self.residueCounter + 1
                if resName=='HOH': self.HOHCounter = self.HOHCounter + 1
                if not resSeq: 
                    if resName=='HOH': resSeq = self.HOHCounter
                    else: resSeq = self.residueCounter
                ## FIXME icodes are ignored
                self.mol.curRes = Residue(resName, resSeq, '',
                                          self.mol.curChain,
                                          top=self.mol)
        icode = self.get_Field_Value(rec, 'iCode')
        if not icode: pass
        elif icode != ' ': self.mol.curRes.icode = icode

        # parse atom info

        # handle atom names (calcium, hydrogen) and find element type
        # check validity of chemical element column and charge column
        ## only works if 'name' is in the pdb format!  FIX!
        n = self.get_Field_Value(rec, 'name')
        el = self.get_Field_Value(rec, 'element')
        if n:
            name, element = self.getPDBAtomName(n, el)
            # if there is not resSeq spec, use first N to figure out new res
            if noresSeq and name=='N':
                At = self.mol.curRes.get('N')
                if At:
                    self.residueCounter = self.residueCounter + 1
                    resSeq = self.residueCounter
                    self.mol.curRes = Residue(resName, resSeq,
                                              self.mol.curChain,
                                              top=self.mol)                    
            atom = Atom(name, self.mol.curRes, element, top=self.mol)
        else:
            element = el
            if element: atom = Atom(parent = self.mol.curRes,
                                    chemicalElement = element, top=self.mol)
            else: atom = Atom(parent = self.mol.curRes, top=self.mol)
##          elem = string.lower(element)          # moved to getPDBAtomName
##          if elem =='lp' or elem =='ld':
##              element = 'Xx'
        atom.charge = self.get_Field_Value(rec, 'charge')
        #should have atom.charge if no charge?
        # coords are required; where to set default or check?
        xcoord = self.get_Field_Value(rec, 'x')
        ycoord = self.get_Field_Value(rec, 'y')
        zcoord = self.get_Field_Value(rec, 'z')
        assert xcoord and ycoord and zcoord
        atom._coords = [ [ float(xcoord), float(ycoord), float(zcoord) ] ]
        atom.segID = string.strip(self.get_Field_Value(rec, 'segID'))
        if rec[:4]=='ATOM' or rec[0]=='ATOM': atom.hetatm = 0
        else: atom.hetatm = 1
        #atom.alternate = []
        atom.element = element
        num = self.get_Field_Value(rec, 'serial')
        if num: atom.number = int(num)
        else: atom.number = self.atomCounter
        occupancy = self.get_Field_Value(rec, 'occupancy')
        if occupancy: atom.occupancy = float(occupancy)
        # must check that it is a number
        atom.conformation = 0
        tempFactor = self.get_Field_Value(rec, 'tempFactor')
        if tempFactor: atom.temperatureFactor = float(tempFactor)

        # add in user defined fields to atom attributes
        for field_name in self.recSpecs.UserFieldsDict.keys():
            value = self.get_Field_Value(rec, field_name)
            type = self.recSpecs.get(field_name, 'var_type')
            if value:
                if type=='int': atom.__setattr__(field_name, int(value))
                elif type=='float': atom.__setattr__(field_name, float(value))
                else: atom.__setattr__(field_name, value)
            else: atom.__setattr__(field_name, value)
        
        if self.altLoc :
            # check if the name of the atom is the same than the
            #name of the previous atom .
            name = name + '@'+self.altLoc
            atom.name = name
            if len(self.mol.curRes.atoms)>1:
                # the new atom has been add to the current residue
                # You have to go to the one before.
                lastAtom = self.mol.curRes.atoms[-2]
                altname = string.split(lastAtom.name, '@')[0]
                if string.split(name, '@')[0] == altname:
                    # Add the new alternate atom to the LastAtom.alternate and
                    # add the lastAtom to the atom.alternate.
                    lastAtom.alternate.append(atom)
                    atom.alternate.append(lastAtom)
                    for l in lastAtom.alternate:
                        if atom.name != l.name:
                            atom.alternate.append(l)
                            l.alternate.append(atom)
        return atom
예제 #4
0
def makeMoleculeFromAtoms(molname, atomSet):
    """
    create a new molecule from a list of atoms

    mol <- makeMoleculeFromAtoms(molname, atomSet)
"""
    from MolKit.molecule import Atom, AtomSet
    from MolKit.protein import Protein, Chain, Residue


    # create the top object
    mol = Protein(name=molname)

    # find out all residues
    residues = atomSet.parent.uniq()

    # find out all chains
    chains = residues.parent.uniq()

    # create all chains
    chainsd = {}
    for c in chains:
        newchain = Chain(c.id, mol, top=mol)
        chainsd[c] = newchain

    # create all residues
    resd = {}
    for res in residues:
        newres = Residue(res.name[:3], res.name[3:], res.icode,
                         chainsd[res.parent], top=mol)
        resd[res] = newres
        newres.hasCA = 0
        newres.hasO = 0

    # create all the atoms
    newats = []
    for num, at in enumerate(atomSet):
        name = at.name
        res = resd[at.parent]
        name1 = name
        if hasattr(at, "altname") and at.altname != None:
            name = at.name.split("@")[0]
        if name == 'CA':
            res.hasCA = 1
        if name == 'O' or name == 'OXT' or (len(name)>3 and name[:3]=='OCT'):
            res.hasO = 2
        
        newat = Atom(name, res, at.element, top=mol)
        if name != name1:
            newat.name = name1
            newat.altname = at.altname
        newats.append(newat)
        # set constructotr attributes
        newat._coords = []
        for coords in at._coords:
            newat._coords.append(coords[:])
        newat.conformation = at.conformation
        newat.chemElem = at.chemElem
        newat.atomicNumber = at.atomicNumber
        newat.bondOrderRadius = at.bondOrderRadius
        newat.covalentRadius = at.covalentRadius
        newat.vdwRadius = at.vdwRadius
        newat.maxBonds = at.maxBonds
        newat.organic = at.organic
        newat.colors = at.colors.copy()
        newat.opacities = at.opacities.copy()
        newat._charges = at._charges.copy()
        newat.chargeSet = at.chargeSet

        # set attributes from PDB parser
        try: # pdbqs do not have this
            newat.segID = at.segID
        except AttributeError:
            pass
        newat.hetatm = at.hetatm
        try: # pdbqs do not have this
            newat.normalname = at.normalname
        except AttributeError:
            pass
        newat.number = num #at.number
        newat.occupancy = at.occupancy
        newat.temperatureFactor = at.temperatureFactor
        newat.altname = at.altname

        # attribute created by PQR parser
        if hasattr(at, 'pqrRadius'):
            newat.pqrRadius = at.pqrRadius

        # attribute created by F2D parser
        if hasattr(at, 'hbstatus'):
            newat.hbstatus = at.hbstatus

        # attribute created by PDBQ parser
        if hasattr(at, 'autodock_element'):
            newat.autodock_element = at.autodock_element

        # attribute created by PDBQT parser
        #if hasattr(at, ''):
        #    newat. = at.

        # attribute created by PDBQS parser
        if hasattr(at, 'AtVol'):
            newat.AtVol = at.AtVol
            newat.AtSolPar = at.AtSolPar

    mol.allAtoms = AtomSet(newats)
    return mol