예제 #1
0
    def copy(self, newname=None):
        """copy makes a new Protein instance with 'newname' and 
        other protein level parameters from self. Next,self.allAtoms is copied
        atom by atom. First: '_fit_atom_into_tree', which uses the same
        logic as pdbParser, builds up new instances of residues and chains
        as necessary.  Then: _copy_atom_attr copies the remaining
        String, Int, Float, None, List and Tuple attributes into new atom
        instances. The new molecule is returned by copy. 
        NB: subsequently the two copies can be visualized: 
        copy2=mv.Mols[0].copy()
        mv.addMolecule(copy2)
        mv.GUI.VIEWER.TransformRootOnly( yesno=0)
        mv.GUI.VIEWER.currentObject=copy2.geomContainer.geoms['master']
        then mouse movements would move only copy2, the new object """

        if not newname: newname = self.name + "_copy"
        newmol=Protein(name=newname, parent=self.parent,
            elementType=self.elementType, childrenName=self.childrenName,
            setClass=self.setClass, childrenSetClass=self.childrenSetClass,
            top=self.top)
        newmol.curChain=Chain()
        newmol.curRes=Residue()
        newmol.allAtoms= AtomSet()
        newmol.parser = self.parser
        for at in self.allAtoms:
            self._fit_atom_into_tree(newmol, at)
        newmol.buildBondsByDistance()
        return newmol
def buildPdb(map_dict,
             npts,
             name='DlgBuilt',
             ctr=0,
             outputfile='results.pdb',
             scale=1.0):
    if debug: print "in buildPdb: tolerance=", tolerance
    name = 'DlgBuilt'
    mol = Protein(name=name)
    mol.curChain = Chain()
    mol.chains = ChainSet([mol.curChain])
    mol.curRes = Residue()
    mol.curChain.adopt(mol.curRes)
    mol.allAtoms = AtomSet()
    mol.curRes.atoms = mol.allAtoms
    nzpts = nypts = nxpts = npts
    #nxpts, nypts, nzpts = npts
    ctr = 0
    for ADtype, m in map_dict.items():
        if debug:
            print "PROCESSING ", ADtype, " array:", max(m.ravel()), ':', min(
                m.ravel())
        vals = []
        tctr = 0  #for number of each type
        for z in range(nzpts):
            for y in range(nypts):
                for x in range(nxpts):
                    val = scale * abs(m[x, y, z])
                    vals.append(val)
                    #if abs(val)>.005:
                    if val > tolerance * scale:
                        ctr += 1
                        name = ADtype + str(ctr)
                        #version3:
                        #info_lo = (xcen - numxcells*spacing,
                        #    ycen - numycells*spacing,
                        #    zcen - numzcells *spacing)
                        #using lower back pt of cube, i think
                        #xcoord = (x-info_lo[0])/spacing
                        #ycoord = (y-info_lo[1])/spacing
                        #zcoord = (z-info_lo[2])/spacing
                        #version2:
                        xcoord = (x - numxcells) * spacing + xcen
                        ycoord = (y - numycells) * spacing + ycen
                        zcoord = (z - numzcells) * spacing + zcen
                        coords = (xcoord, ycoord, zcoord)
                        tctr += 1
                        #    #print "addAtom: name=",name,"ADtype=", ADtype," val=", val, "coords=", coords,"ctr=", ctr
                        addAtom(mol, name, ADtype, val, coords, ctr)
        print "added ", tctr, '<-', ADtype, " atoms"
        if debug:
            print ADtype, ':', tctr, ' ', ctr
    print "total atoms=", ctr
    writer = PdbWriter()
    writer.write(outputfile, mol.allAtoms, records=['ATOM'])
def buildPdb(map_dict, npts, name='DlgBuilt', ctr=0, outputfile='results.pdb', 
                    scale=1.0):
    if debug: print "in buildPdb: tolerance=", tolerance
    name = 'DlgBuilt'
    mol = Protein(name=name)
    mol.curChain = Chain()
    mol.chains = ChainSet([mol.curChain])
    mol.curRes = Residue()
    mol.curChain.adopt(mol.curRes)
    mol.allAtoms = AtomSet()
    mol.curRes.atoms = mol.allAtoms
    nzpts=nypts=nxpts = npts
    #nxpts, nypts, nzpts = npts
    ctr = 0
    for ADtype, m in map_dict.items():
        if debug: 
            print "PROCESSING ", ADtype, " array:", max(m.ravel()), ':', min(m.ravel())
        vals = []
        tctr = 0  #for number of each type
        for z in range(nzpts):
            for y in range(nypts):
                for x in range(nxpts):
                    val = scale * abs(m[x,y,z])
                    vals.append(val)
                    #if abs(val)>.005:
                    if val>tolerance*scale:
                        ctr += 1
                        name = ADtype + str(ctr)
                        #version3:
                        #info_lo = (xcen - numxcells*spacing,
                        #    ycen - numycells*spacing, 
                        #    zcen - numzcells *spacing)
                        #using lower back pt of cube, i think
                        #xcoord = (x-info_lo[0])/spacing
                        #ycoord = (y-info_lo[1])/spacing
                        #zcoord = (z-info_lo[2])/spacing
                        #version2:
                        xcoord = (x-numxcells)*spacing + xcen
                        ycoord = (y-numycells)*spacing + ycen
                        zcoord = (z-numzcells)*spacing + zcen
                        coords = (xcoord,ycoord,zcoord)
                        tctr += 1
                    #    #print "addAtom: name=",name,"ADtype=", ADtype," val=", val, "coords=", coords,"ctr=", ctr
                        addAtom(mol, name, ADtype, val, coords, ctr)
        print "added ",tctr, '<-', ADtype, " atoms"
        if debug:
            print ADtype, ':', tctr , ' ', ctr
    print "total atoms=", ctr
    writer = PdbWriter()
    writer.write(outputfile, mol.allAtoms, records=['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]
        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)
        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
        newat.segID = at.segID
        newat.hetatm = at.hetatm
        newat.normalname = at.normalname
        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
예제 #5
0
    def parse( self, objClass=Protein ):
        if self.allLines is None and self.filename:
            self.readFile()
            if self.allLines is None or len(self.allLines)==0:
                return

        mol = Protein()
        self.mol = mol
        molList = mol.setClass()
        molList.append( mol )
        current_residue_number = None
        current_chain = None
        current_residue = None
        number_of_atoms = int(self.allLines[1][:5])

        self.configureProgressBar( init=1, mode='increment', 
                                  authtext='parse atoms', max=number_of_atoms )
        
        
        current_chain = Chain( id='GRO',)
        #FIX this: The existence of allAtoms attribute (and the fact that it is an empty set rather than all atoms in the chain) causes getNodesByMolecule() to return wrong values
        if hasattr(current_chain, "allAtoms"):
            del(current_chain.allAtoms)
        #current_chain = Chain( id='GRO',parent = mol)
        mol.adopt( current_chain, setChildrenTop=1 )
         
        for index in range( 2,number_of_atoms+2 ):              
            residue_number = int(self.allLines[index][:5])
            if residue_number!=current_residue_number:# 
                #current_chain should adopt the current residue if there is one
                #create new residue
                res_type = self.allLines[index][5:10]
                residue_type = res_type.split(' ')[0]
                
                current_residue = Residue( type=residue_type, number=residue_number )
                current_residue_number = residue_number
                if current_residue is not None:    #REMEMBER TO ADOPT THE LAST ONE!!!
                     
                    current_chain.adopt( current_residue, setChildrenTop=1 )
                            
            n = self.allLines[index][10:15]
            name = n.split(' ')[-1]
            element = name 
            
            if element in babel_elements.keys():
                element = element

            else:
                 
                if residue_type == "System" or residue_type == "SOL":                 
                    #if element[1] == 'W':
                    #          element = 'H'
                        #   group is treated as one particle
                    #else:
                    element = element[0]

                elif element[:2] == 'Me':
                    element = 'C'
                else:
                    element = element[0]
                
            #if len(element)>1:
            #    if type(element[1]) == types.StringType:
            #        
            #        if element[1] == element[1].lower():
            #            element =element
            #        else:
            #            element = element[0]
            #            
            #    else:    
            #        element = element[0]
                
            atom = Atom( name, current_residue, element, top=mol )
            c =  self.allLines[index][15:20]
            cx = self.allLines[index][20:28] 
            cy = self.allLines[index][28:36]
            cz = self.allLines[index][36:44]
            
            x = float(cx)*10
            y = float(cy)*10
            z = float(cz)*10
            atom._coords = [[x, y, z]]
             
            atom._charges = []
            atom.segID =  mol.name   
            atom.normalname = name
            atom.number = int(self.allLines[index][15:20])
            atom.elementType = name[0]
            mol.atmNum[atom.number] = atom
            atom.altname = None    
            atom.hetatm = 0
        mol.name = os.path.split(os.path.splitext(self.filename)[0])[-1]
        mol.allAtoms = mol.chains.residues.atoms
        mol.parser = self
        mol.levels = [Protein, Chain, Residue, Atom]
        name = ''
        for n in molList.name:
            name = n + ','
        name = name[:-1]
        molList.setStringRepr( name )
        strRpr = name + ':::'
        molList.allAtoms.setStringRepr( strRpr )
        for m in molList:
            mname = m.name
            strRpr = mname + ':::'
            m.allAtoms.setStringRepr( strRpr )
            strRpr = mname + ':'
            m.chains.setStringRepr( strRpr )
            for c in m.chains:
                cname = c.id
                strRpr = mname + ':' + cname + ':'
                c.residues.setStringRepr( strRpr )
                for r in c.residues:
                    rname = r.name
                    strRpr = mname + ':' + cname + ':' + rname + ':'
                    r.atoms.setStringRepr( strRpr )        
        return molList
예제 #6
0
    def parse(self, objClass=Protein):
        """Parses mmCIF dictionary (self.mmCIF_dict) into MolKit object"""
        if self.allLines is None and self.filename:
            self.readFile()
            if self.allLines is None or len(self.allLines)==0:
                return
            self.mmCIF2Dict()
        type_symbol = None
        B_iso_or_equiv = None
        mmCIF_dict = self.mmCIF_dict
        fileName, fileExtension = os.path.splitext(self.filename)
        molName = os.path.basename(fileName)
        if mmCIF_dict.has_key('_entry.id'):
            molName = mmCIF_dict['_entry.id']
        if mmCIF_dict.has_key('_atom_site.id'):
            #The description of the data names can be found in the following link
            #http://mmcif.pdb.org/dictionaries/mmcif_pdbx.dic/Items   
            ids = mmCIF_dict['_atom_site.id'] #1 number
            group_PDB = mmCIF_dict['_atom_site.group_PDB']          #2 atom/hetatm
            
            atom_id = mmCIF_dict['_atom_site.label_atom_id']  #3 name

            comp_id = mmCIF_dict['_atom_site.label_comp_id']  #4 residue type
            label_asym_id = mmCIF_dict['_atom_site.label_asym_id']  #5 chain 
            #Note: chain ID from mmCIF file might be different from PDB file
            seq_id = mmCIF_dict['_atom_site.label_seq_id']    #6 residue number
            x_coords = mmCIF_dict['_atom_site.Cartn_x']             #7 xcoord
            y_coords = mmCIF_dict['_atom_site.Cartn_y']             #8 ycoord
            z_coords = mmCIF_dict['_atom_site.Cartn_z']             #9 zcoord
            occupancy = mmCIF_dict['_atom_site.occupancy']          #10    
            B_iso_or_equiv = mmCIF_dict['_atom_site.B_iso_or_equiv']#11
            type_symbol = mmCIF_dict['_atom_site.type_symbol']
            
                
        elif mmCIF_dict.has_key('_atom_site_label'):
            #ftp://ftp.iucr.org/pub/cif_core.dic
            atom_id = mmCIF_dict['_atom_site_label']
            len_atoms = len(atom_id)
            ids = range(len_atoms)
            
            group_PDB = len_atoms*['HETATM']
            comp_id = len_atoms*["CIF"]
            label_asym_id = len_atoms*['1']
            seq_id = len_atoms*[1]
            
            from mglutil.math.crystal import Crystal
            a = mmCIF_dict['_cell.length_a'] = float(mmCIF_dict['_cell_length_a'].split('(')[0])
            b = mmCIF_dict['_cell.length_b'] = float(mmCIF_dict['_cell_length_b'].split('(')[0])
            c = mmCIF_dict['_cell.length_c'] = float(mmCIF_dict['_cell_length_c'].split('(')[0])
            alpha = mmCIF_dict['_cell.angle_alpha'] = float(mmCIF_dict['_cell_angle_alpha'].split('(')[0])
            beta = mmCIF_dict['_cell.angle_beta'] = float(mmCIF_dict['_cell_angle_beta'].split('(')[0])
            gamma = mmCIF_dict['_cell.angle_gamma'] = float(mmCIF_dict['_cell_angle_gamma'].split('(')[0])
            cryst = Crystal((a, b, c), (alpha, beta, gamma))
            x = []
            for item in mmCIF_dict['_atom_site_fract_x']:
                x.append(float(item.split('(')[0]))
            y = []
            for item in mmCIF_dict['_atom_site_fract_y']:
                y.append(float(item.split('(')[0]))
            z = []
            for item in mmCIF_dict['_atom_site_fract_z']:
                z.append(float(item.split('(')[0]))
                
            x_coords = []
            y_coords = []
            z_coords = []
            B_iso_or_equiv = []
            for i in ids:
                trans = cryst.toCartesian([x[i], y[i], z[i]])
                
                x_coords.append(trans[0]) 
                y_coords.append(trans[1])
                z_coords.append(trans[2])
                if mmCIF_dict.has_key('_atom_site_U_iso_or_equiv'):
                    B_iso_or_equiv.append(mmCIF_dict['_atom_site_U_iso_or_equiv'][i].split('(')[0])
            if mmCIF_dict.has_key('_atom_site_type_symbol'):
                type_symbol = mmCIF_dict['_atom_site_type_symbol']
            if mmCIF_dict.has_key('_atom_site_occupancy'):
                occupancy = mmCIF_dict['_atom_site_occupancy']
            if mmCIF_dict.has_key('_chemical_name_common'):   
                molName = mmCIF_dict['_chemical_name_common']
            elif mmCIF_dict.has_key('_chemical_name_mineral'):
                molName = mmCIF_dict['_chemical_name_mineral']
                                
            if mmCIF_dict.has_key('_symmetry_space_group_name_H-M'):   
                mmCIF_dict['_symmetry.space_group_name_H-M'] = mmCIF_dict['_symmetry_space_group_name_H-M']
        else:
            print 'No _atom_site.id or _atom_site_label record is available in %s' % self.filename
            return  None  
        
        mol = Protein()
        self.mol = mol
        self.mol.allAtoms = AtomSet([])
        molList = mol.setClass()
        molList.append( mol )
        current_chain_id = None
        current_residue_number = None
        current_chain = None
        current_residue = None
        
        number_of_atoms = len(ids)

        self.configureProgressBar(init=1, mode='increment', 
                                  authtext='parse atoms', max=number_of_atoms)
        for index in range(number_of_atoms):              
            #make a new atom for the current index
            chain_id = label_asym_id[index]
            if chain_id != current_chain_id:         #make a new chain
                #molecule should adopt the current chain if there is one
                current_chain = Chain(id=chain_id)
                # FIXME: current_chain should not have allAtoms attribute
                delattr(current_chain, "allAtoms")
                current_chain_id = chain_id
                
                if current_chain is not None:    #REMEMBER TO ADOPT THE LAST ONE!!!
                    mol.adopt(current_chain, setChildrenTop=1)                    
            residue_number = seq_id[index]   

            if residue_number != current_residue_number or chain_id != label_asym_id[index-1]:         #make a new chain:
                #current_chain should adopt the current residue if there is one
                #create new residue
                residue_type = comp_id[index]
                current_residue = Residue(type=residue_type, number=residue_number)
                current_residue_number = residue_number
                if current_residue is not None:    #REMEMBER TO ADOPT THE LAST ONE!!!
                    current_chain.adopt(current_residue, setChildrenTop=1)
                
            
            name = atom_id[index]
            if type_symbol:
                element = type_symbol[index]
            else:
                element = None
            atom = Atom( name, current_residue, element, top=mol )
            atom._coords = [[float(x_coords[index]), float(y_coords[index]), float(z_coords[index])]]
            atom._charges = {}
            atom.segID =  mol.name   
            atom.normalname = name
            atom.number = int(ids[index])
            mol.atmNum[atom.number] = atom
            atom.occupancy = float(occupancy[index])
            if B_iso_or_equiv:
                atom.temperatureFactor = float(B_iso_or_equiv[index])
            atom.altname = None    
            atom.hetatm = 0
            if group_PDB[index]=='HETATM':
                atom.hetatm = 1
            self.updateProgressBar()
                           
        self.parse_MMCIF_CELL()
        try:
            self.parse_MMCIF_HYDBND()       
        except:
             print >>sys.stderr,"Parsing Hydrogen Bond Record Failed in",self.filename
               
        mol.name = molName
        mol.allAtoms = mol.chains.residues.atoms
        
        mol.parser = self
        mol.levels = [Protein, Chain, Residue, Atom]
        name = ''
        for n in molList.name:
            name = n + ','
        name = name[:-1]
        molList.setStringRepr(name)
        strRpr = name + ':::'
        molList.allAtoms.setStringRepr(strRpr)
        for m in molList:
            mname = m.name
            strRpr = mname + ':::'
            m.allAtoms.setStringRepr(strRpr)
            strRpr = mname + ':'
            m.chains.setStringRepr(strRpr)
            for c in m.chains:
                cname = c.id
                strRpr = mname + ':' + cname + ':'
                c.residues.setStringRepr(strRpr)
                for r in c.residues:
                    rname = r.name
                    strRpr = mname + ':' + cname + ':' + rname + ':'
                    r.atoms.setStringRepr(strRpr)                            
        self.buildBonds()
        return molList
예제 #7
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
예제 #8
0
    def parse(self, objClass=Protein):
        """Parses mmCIF dictionary (self.mmCIF_dict) into MolKit object"""
        if self.allLines is None and self.filename:
            self.readFile()
            if self.allLines is None or len(self.allLines) == 0:
                return
            self.mmCIF2Dict()
        type_symbol = None
        B_iso_or_equiv = None
        mmCIF_dict = self.mmCIF_dict
        fileName, fileExtension = os.path.splitext(self.filename)
        molName = os.path.basename(fileName)
        if mmCIF_dict.has_key('_entry.id'):
            molName = mmCIF_dict['_entry.id']
        if mmCIF_dict.has_key('_atom_site.id'):
            #The description of the data names can be found in the following link
            #http://mmcif.pdb.org/dictionaries/mmcif_pdbx.dic/Items
            ids = mmCIF_dict['_atom_site.id']  #1 number
            group_PDB = mmCIF_dict['_atom_site.group_PDB']  #2 atom/hetatm

            atom_id = mmCIF_dict['_atom_site.label_atom_id']  #3 name

            comp_id = mmCIF_dict['_atom_site.label_comp_id']  #4 residue type
            label_asym_id = mmCIF_dict['_atom_site.label_asym_id']  #5 chain
            #Note: chain ID from mmCIF file might be different from PDB file
            seq_id = mmCIF_dict['_atom_site.label_seq_id']  #6 residue number
            x_coords = mmCIF_dict['_atom_site.Cartn_x']  #7 xcoord
            y_coords = mmCIF_dict['_atom_site.Cartn_y']  #8 ycoord
            z_coords = mmCIF_dict['_atom_site.Cartn_z']  #9 zcoord
            occupancy = mmCIF_dict['_atom_site.occupancy']  #10
            B_iso_or_equiv = mmCIF_dict['_atom_site.B_iso_or_equiv']  #11
            type_symbol = mmCIF_dict['_atom_site.type_symbol']

        elif mmCIF_dict.has_key('_atom_site_label'):
            #ftp://ftp.iucr.org/pub/cif_core.dic
            atom_id = mmCIF_dict['_atom_site_label']
            len_atoms = len(atom_id)
            ids = range(len_atoms)

            group_PDB = len_atoms * ['HETATM']
            comp_id = len_atoms * ["CIF"]
            label_asym_id = len_atoms * ['1']
            seq_id = len_atoms * [1]

            from mglutil.math.crystal import Crystal
            a = mmCIF_dict['_cell.length_a'] = float(
                mmCIF_dict['_cell_length_a'].split('(')[0])
            b = mmCIF_dict['_cell.length_b'] = float(
                mmCIF_dict['_cell_length_b'].split('(')[0])
            c = mmCIF_dict['_cell.length_c'] = float(
                mmCIF_dict['_cell_length_c'].split('(')[0])
            alpha = mmCIF_dict['_cell.angle_alpha'] = float(
                mmCIF_dict['_cell_angle_alpha'].split('(')[0])
            beta = mmCIF_dict['_cell.angle_beta'] = float(
                mmCIF_dict['_cell_angle_beta'].split('(')[0])
            gamma = mmCIF_dict['_cell.angle_gamma'] = float(
                mmCIF_dict['_cell_angle_gamma'].split('(')[0])
            cryst = Crystal((a, b, c), (alpha, beta, gamma))
            x = []
            for item in mmCIF_dict['_atom_site_fract_x']:
                x.append(float(item.split('(')[0]))
            y = []
            for item in mmCIF_dict['_atom_site_fract_y']:
                y.append(float(item.split('(')[0]))
            z = []
            for item in mmCIF_dict['_atom_site_fract_z']:
                z.append(float(item.split('(')[0]))

            x_coords = []
            y_coords = []
            z_coords = []
            B_iso_or_equiv = []
            for i in ids:
                trans = cryst.toCartesian([x[i], y[i], z[i]])

                x_coords.append(trans[0])
                y_coords.append(trans[1])
                z_coords.append(trans[2])
                if mmCIF_dict.has_key('_atom_site_U_iso_or_equiv'):
                    B_iso_or_equiv.append(
                        mmCIF_dict['_atom_site_U_iso_or_equiv'][i].split(
                            '(')[0])
            if mmCIF_dict.has_key('_atom_site_type_symbol'):
                type_symbol = mmCIF_dict['_atom_site_type_symbol']
            if mmCIF_dict.has_key('_atom_site_occupancy'):
                occupancy = mmCIF_dict['_atom_site_occupancy']
            if mmCIF_dict.has_key('_chemical_name_common'):
                molName = mmCIF_dict['_chemical_name_common']
            elif mmCIF_dict.has_key('_chemical_name_mineral'):
                molName = mmCIF_dict['_chemical_name_mineral']

            if mmCIF_dict.has_key('_symmetry_space_group_name_H-M'):
                mmCIF_dict['_symmetry.space_group_name_H-M'] = mmCIF_dict[
                    '_symmetry_space_group_name_H-M']
        else:
            print 'No _atom_site.id or _atom_site_label record is available in %s' % self.filename
            return None

        mol = Protein()
        self.mol = mol
        self.mol.allAtoms = AtomSet([])
        molList = mol.setClass()
        molList.append(mol)
        current_chain_id = None
        current_residue_number = None
        current_chain = None
        current_residue = None

        number_of_atoms = len(ids)

        self.configureProgressBar(init=1,
                                  mode='increment',
                                  authtext='parse atoms',
                                  max=number_of_atoms)
        for index in range(number_of_atoms):
            #make a new atom for the current index
            chain_id = label_asym_id[index]
            if chain_id != current_chain_id:  #make a new chain
                #molecule should adopt the current chain if there is one
                current_chain = Chain(id=chain_id)
                # FIXME: current_chain should not have allAtoms attribute
                delattr(current_chain, "allAtoms")
                current_chain_id = chain_id

                if current_chain is not None:  #REMEMBER TO ADOPT THE LAST ONE!!!
                    mol.adopt(current_chain, setChildrenTop=1)
            residue_number = seq_id[index]

            if residue_number != current_residue_number or chain_id != label_asym_id[
                    index - 1]:  #make a new chain:
                #current_chain should adopt the current residue if there is one
                #create new residue
                residue_type = comp_id[index]
                current_residue = Residue(type=residue_type,
                                          number=residue_number)
                current_residue_number = residue_number
                if current_residue is not None:  #REMEMBER TO ADOPT THE LAST ONE!!!
                    current_chain.adopt(current_residue, setChildrenTop=1)

            name = atom_id[index]
            if type_symbol:
                element = type_symbol[index]
            else:
                element = None
            atom = Atom(name, current_residue, element, top=mol)
            atom._coords = [[
                float(x_coords[index]),
                float(y_coords[index]),
                float(z_coords[index])
            ]]
            atom._charges = {}
            atom.segID = mol.name
            atom.normalname = name
            atom.number = int(ids[index])
            mol.atmNum[atom.number] = atom
            atom.occupancy = float(occupancy[index])
            if B_iso_or_equiv:
                atom.temperatureFactor = float(B_iso_or_equiv[index])
            atom.altname = None
            atom.hetatm = 0
            if group_PDB[index] == 'HETATM':
                atom.hetatm = 1
            self.updateProgressBar()

        self.parse_MMCIF_CELL()
        try:
            self.parse_MMCIF_HYDBND()
        except:
            print >> sys.stderr, "Parsing Hydrogen Bond Record Failed in", self.filename

        mol.name = molName
        mol.allAtoms = mol.chains.residues.atoms

        mol.parser = self
        mol.levels = [Protein, Chain, Residue, Atom]
        name = ''
        for n in molList.name:
            name = n + ','
        name = name[:-1]
        molList.setStringRepr(name)
        strRpr = name + ':::'
        molList.allAtoms.setStringRepr(strRpr)
        for m in molList:
            mname = m.name
            strRpr = mname + ':::'
            m.allAtoms.setStringRepr(strRpr)
            strRpr = mname + ':'
            m.chains.setStringRepr(strRpr)
            for c in m.chains:
                cname = c.id
                strRpr = mname + ':' + cname + ':'
                c.residues.setStringRepr(strRpr)
                for r in c.residues:
                    rname = r.name
                    strRpr = mname + ':' + cname + ':' + rname + ':'
                    r.atoms.setStringRepr(strRpr)
        self.buildBonds()
        return molList
예제 #9
0
    def parse(self, objClass=Protein):
        if self.allLines is None and self.filename:
            self.readFile()
            if self.allLines is None or len(self.allLines) == 0:
                return

        mol = Protein()
        self.mol = mol
        molList = mol.setClass()
        molList.append(mol)
        current_residue_number = None
        current_chain = None
        current_residue = None
        number_of_atoms = int(self.allLines[1][:5])

        self.configureProgressBar(init=1,
                                  mode='increment',
                                  authtext='parse atoms',
                                  max=number_of_atoms)

        current_chain = Chain(id='GRO', )
        # FIX this: The existence of allAtoms attribute (and the fact that it is an empty set rather than all atoms in
        # the chain) causes getNodesByMolecule() to return wrong values
        if hasattr(current_chain, "allAtoms"):
            del current_chain.allAtoms
        # current_chain = Chain( id='GRO',parent = mol)
        mol.adopt(current_chain, setChildrenTop=1)

        for index in range(2, number_of_atoms + 2):
            residue_number = int(self.allLines[index][:5])
            if residue_number != current_residue_number:  #
                # current_chain should adopt the current residue if there is one
                # create new residue
                res_type = self.allLines[index][5:10]
                residue_type = res_type.split(' ')[0]

                current_residue = Residue(type=residue_type,
                                          number=residue_number)
                current_residue_number = residue_number
                if current_residue is not None:  # REMEMBER TO ADOPT THE LAST ONE!!!

                    current_chain.adopt(current_residue, setChildrenTop=1)

            n = self.allLines[index][10:15]
            name = n.split(' ')[-1]
            element = name

            if element in list(babel_elements.keys()):
                element = element

            else:

                if residue_type == "System" or residue_type == "SOL":
                    # if element[1] == 'W':
                    #          element = 'H'
                    #   group is treated as one particle
                    # else:
                    element = element[0]

                elif element[:2] == 'Me':
                    element = 'C'
                else:
                    element = element[0]

            # if len(element)>1:
            #    if type(element[1]) == types.StringType:
            #
            #        if element[1] == element[1].lower():
            #            element =element
            #        else:
            #            element = element[0]
            #
            #    else:
            #        element = element[0]

            atom = Atom(name, current_residue, element, top=mol)
            c = self.allLines[index][15:20]
            cx = self.allLines[index][20:28]
            cy = self.allLines[index][28:36]
            cz = self.allLines[index][36:44]

            x = float(cx) * 10
            y = float(cy) * 10
            z = float(cz) * 10
            atom._coords = [[x, y, z]]

            atom._charges = []
            atom.segID = mol.name
            atom.normalname = name
            atom.number = int(self.allLines[index][15:20])
            atom.elementType = name[0]
            mol.atmNum[atom.number] = atom
            atom.altname = None
            atom.hetatm = 0
        mol.name = os.path.split(os.path.splitext(self.filename)[0])[-1]
        mol.allAtoms = mol.chains.residues.atoms
        mol.parser = self
        mol.levels = [Protein, Chain, Residue, Atom]
        name = ''
        for n in molList.name:
            name = n + ','
        name = name[:-1]
        molList.setStringRepr(name)
        strRpr = name + ':::'
        molList.allAtoms.setStringRepr(strRpr)
        for m in molList:
            mname = m.name
            strRpr = mname + ':::'
            m.allAtoms.setStringRepr(strRpr)
            strRpr = mname + ':'
            m.chains.setStringRepr(strRpr)
            for c in m.chains:
                cname = c.id
                strRpr = mname + ':' + cname + ':'
                c.residues.setStringRepr(strRpr)
                for r in c.residues:
                    rname = r.name
                    strRpr = mname + ':' + cname + ':' + rname + ':'
                    r.atoms.setStringRepr(strRpr)
        return molList