Esempio n. 1
0
def instanceMatricesFromGroup(molecule):
    returnMatrices = [numpy.eye(4, 4)]
    crystal = Crystal(molecule.cellLength, molecule.cellAngles)
    matrices = spaceGroups[molecule.spaceGroup]
    for matrix in matrices:
        tmpMatix = numpy.eye(4, 4)
        tmpMatix[:3, :3] = matrix[0]
        tmpMatix[:3, 3] = crystal.toCartesian(matrix[1])
        returnMatrices.append(tmpMatix)
    molecule.crystal = crystal
    return returnMatrices
def instanceMatricesFromGroup(molecule):
    returnMatrices = [numpy.eye(4,4)]
    crystal = Crystal(molecule.cellLength, molecule.cellAngles)
    matrices = spaceGroups[molecule.spaceGroup]
    for matrix in matrices:
        tmpMatix = numpy.eye(4,4) 
        tmpMatix[:3, :3] = matrix[0]
        tmpMatix[:3, 3] = crystal.toCartesian(matrix[1])
        returnMatrices.append(tmpMatix)
    molecule.crystal = crystal
    return returnMatrices
Esempio n. 3
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
Esempio n. 4
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
Esempio n. 5
0
    def AddGrid3D(self, grid):
        assert isinstance(grid, Grid3DUC)

        if not self.volrenInitialized:
            if not self.viewer:  # we need an OpenGL context before
                print "self.viewer: ", self.viewer
                return  # we can initialize the renderer
            self.InitVolumeRenderer()
            for c in self.viewer.cameras:
                c.addButtonDownCB(self.coarseRendering)
                c.addButtonUpCB(self.fineRendering)

        orig = grid.origin[:]
        step = grid.stepSize

        nx, ny, nz = grid.data.shape
        gridSize = [nx * step[0], ny * step[1], nz * step[2]]
        gridSize2 = [gridSize[0] * 0.5, gridSize[1] * 0.5, gridSize[2] * 0.5]
        maxgrid = [
            orig[0] + gridSize[0], orig[1] + gridSize[1], orig[2] + gridSize[2]
        ]
        transl = [
            orig[0] + gridSize2[0], orig[1] + gridSize2[1],
            orig[2] + gridSize2[2]
        ]

        # save scale and tranlation into Matrix which is not affected
        # by reset
        if grid.crystal:
            # compute cartesian voxel sizes
            x, y, z = grid.getStepSizeReal()

            #build crystal object for length with padding
            from mglutil.math.crystal import Crystal
            if hasattr(grid, 'dataDims'):
                # compute ratios of padding along the 3 dimensions
                dx = grid.dimensions[0] - grid.dataDims[0] - 1
                dy = grid.dimensions[1] - grid.dataDims[1] - 1
                dz = grid.dimensions[2] - grid.dataDims[2] - 1
                ry = float(dx) / dy
                rz = float(dx) / dz
            else:
                ry = rz = 1.0

            dims = (grid.dimensions[0] - 1, (grid.dimensions[1] - 1) * ry,
                    (grid.dimensions[2] - 1) * rz)
            cryst = Crystal((dims[0] * x, dims[1] * y, dims[2] * z),
                            grid.crystal.angles)

            matrix = Numeric.identity(4, 'f')
            matrix[:3, :3] = cryst.ftoc.astype('f')
            matrix.shape = (16, )

            # cleanup=False because rotation can contain shear which should
            # be kept
            self.MatrixRot, MatrixTransl, self.MatrixScale = self.Decompose4x4(
                matrix, cleanup=False)

            # set utvolgeom's Matrix components
            self.MatrixScale = (self.MatrixScale[0], self.MatrixScale[1] / ry,
                                self.MatrixScale[2] / rz)

            origin = grid.crystal.toCartesian(grid.origin)
            self.MatrixTransl = (origin[0] + MatrixTransl[0] +
                                 dims[0] * 0.5 * x, origin[1] +
                                 MatrixTransl[1] + dims[1] * 0.5 * y / ry,
                                 origin[2] + MatrixTransl[2] +
                                 dims[2] * 0.5 * z / rz)
            #o = grid.origin
            #t1 = cryst.toFractional(MatrixTransl)
            #t2 = (0.5, 0.5, 0.5)
            #trans = ( o[0]+t1[0]+t2[0], o[1]+t1[1]+t2[1],o[2]+t1[2]+t2[2])
            #self.MatrixTransl = cryst.toCartesian(trans)
            #print o
            #print t1
            #print t2
            #print trans
            #print self.MatrixTransl

            #self.MatrixTransl = (0.,0.,0.)
            #self.MatrixScale = (1.,1.,1.)

            RotInv = Numeric.transpose(Numeric.reshape(self.MatrixRot, (4, 4)))
            self.MatrixRotInv = Numeric.reshape(RotInv, (16, ))
            #
            self.MatrixRot = self.MatrixRot.astype('f')
            self.MatrixRotInv = self.MatrixRot.astype('f')

        else:
            self.setMatrixComponents(trans=transl)
            self.setMatrixComponents(scale=gridSize, trans=transl)

        if self.firstLoaded:
            if self.viewer:
                rootObject = self.viewer.rootObject
                self.minBB = [-0.5, -0.5, -0.5]
                self.maxBB = [0.5, 0.5, 0.5]
                #print "all objects:", self.viewer.rootObject.AllObjects()
                self.viewer.NormalizeCurrentObject()
                self.firstLoaded = 0

# scale and translate volume
##self.SetScale( gridSize)
##trans = Numeric.array( gridSize2, 'f')
##self.SetTranslation( trans )

#mat = self.GetMatrix(self)
#self.SetMatrix(mat)
#self.ResetTransformation()

        arr = Numeric.ascontiguousarray(Numeric.transpose(grid.data),
                                        grid.data.dtype.char)
        upload = self.volume.uploadColorMappedData
        status = upload(arr.ravel(), nx, ny, nz)
        if status != 1:
            raise RuntimeError(
                "uploadColorMappedData() in AddVolume failed. Status %d" %
                status)

        self.dataArr = Numeric.reshape(arr, (nz, ny, nx))
        self.volumeSize = (nx, ny, nz)
        if self.byte_map == None:
            self.grayRamp()

# update cropping box
        self.crop.updateData()
        self.cropBox.setVolSize((nx, ny, nz))
        self.cropBox.xmin = 0
        self.cropBox.xmax = nx
        self.cropBox.ymin = 0
        self.cropBox.ymax = ny
        self.cropBox.zmin = 0
        self.cropBox.zmax = nz
        self.cropBox.update()
        for c in self.onaddVolume_list:
            c.OnAddVolumeToViewer()