예제 #1
0
    def testFunctionAddStructNewModel(self):
        pdbID1 = '1P30'  # A
        pdbID2 = '1CJD'  # A, B,C
        outFile = "/tmp/model.cif"
        aSH1 = emconv.AtomicStructHandler()
        aSH2 = emconv.AtomicStructHandler()
        #
        fileName1 = aSH1.readFromPDBDatabase(pdbID1, type='mmCif', dir='/tmp')
        fileName2 = aSH2.readFromPDBDatabase(pdbID2, type='mmCif', dir='/tmp')
        aSH1.read(fileName1)
        aSH2.read(fileName2)
        atomsNum1 = len([atom.id for atom in aSH1.getStructure().get_atoms()])
        atomsNum2 = len([atom.id for atom in aSH2.getStructure().get_atoms()])
        #
        aSH1.addStruct(fileName2, outPDBfileName=outFile, useModel=True)
        #
        # aSH1.addStruct(fileName2, outPDBfileName=outFile, useModel=False)
        chains = [chain.id for chain in aSH1.getStructure().get_chains()]
        # compare unordered lists of chains
        goal = ['A', 'A', 'B', 'C']
        self.assertTrue(
            Counter(chains) == Counter(goal), "{} != {}".format(chains, goal))
        atomsNumT = len([atom.id for atom in aSH1.getStructure().get_atoms()])
        self.assertEqual(atomsNum1 + atomsNum2, atomsNumT)

        os.unlink(fileName1)
        os.unlink(fileName2)
        os.unlink(outFile)
예제 #2
0
    def testFunctionAddStructNoNewModelAddTwice(self):
        """ add two atomic structures with overlaping chain ids, last atomic
        structure is added two times"""
        pdbID1 = '1P30'  # A,
        pdbID2 = '1CJD'  # A, B, C
        outFile = "/tmp/nomodel.cif"  # A, A002, B, C, A003, B002, C002
        aSH1 = emconv.AtomicStructHandler()
        aSH2 = emconv.AtomicStructHandler()
        #
        fileName1 = aSH1.readFromPDBDatabase(pdbID1, type='mmCif', dir='/tmp')
        fileName2 = aSH2.readFromPDBDatabase(pdbID2, type='mmCif', dir='/tmp')
        aSH1.read(fileName1)
        aSH2.read(fileName2)
        atomsNum1 = len([atom.id for atom in aSH1.getStructure().get_atoms()])
        atomsNum2 = len([atom.id for atom in aSH2.getStructure().get_atoms()])
        #
        aSH1.addStruct(fileName2, outPDBfileName=outFile, useModel=False)
        aSH1.addStruct(fileName2, outPDBfileName=outFile, useModel=False)
        chains = [chain.id for chain in aSH1.getStructure().get_chains()]
        # compare unordered lists of chains
        goal = ['A', 'A002', 'B', 'C', 'A003', 'B002', 'C002']
        self.assertTrue(
            Counter(chains) == Counter(goal), "{} != {}".format(chains, goal))

        atomsNumT = len([atom.id for atom in aSH1.getStructure().get_atoms()])
        self.assertEqual(atomsNum1 + atomsNum2 + atomsNum2, atomsNumT)
        os.unlink(fileName1)
        os.unlink(fileName2)
        os.unlink(outFile)
예제 #3
0
    def testPDBToCIF(self):
        aSH1 = emconv.AtomicStructHandler(self.PDBFileName)
        CIFFileName2 = self.CIFFileName.replace(".cif", "_2.cif")
        aSH1.write(CIFFileName2)
        aSH2 = emconv.AtomicStructHandler(CIFFileName2)

        structure1 = aSH1.getStructure()
        structure2 = aSH2.getStructure()
        for atom1, atom2 in zip(structure1.get_atoms(),
                                structure2.get_atoms()):
            self.assertEqual(atom1.get_name(), atom2.get_name())
        os.unlink(CIFFileName2)
예제 #4
0
    def testWritePDB(self):
        aSH = emconv.AtomicStructHandler(self.PDBFileName)
        PDBFileName2 = self.PDBFileName.replace(".pdb", "_2.pdb")
        aSH._write(PDBFileName2)
        aSH2 = emconv.AtomicStructHandler(PDBFileName2)
        structure1 = aSH.getStructure()
        structure2 = aSH2.getStructure()

        for atom1, atom2 in zip(structure1.get_atoms(),
                                structure2.get_atoms()):
            self.assertEqual(atom1.get_name(), atom2.get_name())
        os.unlink(PDBFileName2)
예제 #5
0
 def testCenterOfMass(self):
     aSH = emconv.AtomicStructHandler()
     structure = aSH.read(self.CIFFileName)
     x, y, z = aSH.centerOfMass(structure)
     self.assertAlmostEqual(x, 8.1593891597, 2)
     self.assertAlmostEqual(y, 21.1833304818, 2)
     self.assertAlmostEqual(z, 20.0177924407, 2)
예제 #6
0
    def getModelsChainsStep(cls, protocol):
        """ Returns (1) list with the information
           {"model": %d, "chain": "%s", "residues": %d} (modelsLength)
           (2) list with residues, postion and chain (modelsFirstResidue)"""
        structureHandler = emconv.AtomicStructHandler()
        fileName = ""
        if hasattr(protocol, 'pdbId'):
            if protocol.pdbId.get() is not None:
                pdbID = protocol.pdbId.get()
                url = "https://www.rcsb.org/structure/"
                URL = url + ("%s" % pdbID)
                try:
                    response = requests.get(URL)
                except:
                    raise Exception("Cannot connect to PDB server")
                if (response.status_code >= 400) and (response.status_code <
                                                      500):
                    raise Exception("%s is a wrong PDB ID" % pdbID)
                fileName = structureHandler.readFromPDBDatabase(
                    os.path.basename(pdbID), dir="/tmp/")
            else:
                fileName = protocol.pdbFile.get()
        else:
            if protocol.pdbFileToBeRefined.get() is not None:
                fileName = os.path.abspath(
                    protocol.pdbFileToBeRefined.get().getFileName())

        structureHandler.read(fileName)
        structureHandler.getStructure()
        # listOfChains, listOfResidues = structureHandler.getModelsChains()
        return structureHandler.getModelsChains()
예제 #7
0
    def testReadFromPDBDatabase(self):
        PDBID = '6CUD'
        aSH = emconv.AtomicStructHandler()
        # EMD-7620
        fileName = aSH.readFromPDBDatabase(PDBID, type='pdb', dir='/tmp')
        self.assertTrue(os.path.exists(fileName))

        os.unlink(fileName)
예제 #8
0
 def pdbDownloadStep(self):
     """Download all pdb files in file_list and unzip them.
     """
     aSH = emconv.AtomicStructHandler()
     print("retrieving atomic structure with ID = %s" % self.pdbId.get())
     atomStructPath = aSH.readFromPDBDatabase(self.pdbId.get(),
                                              type='mmCif',
                                              dir=self._getExtraPath())
     self.createOutputStep(atomStructPath)
예제 #9
0
    def testRenameToChains(self):
        aSH = emconv.AtomicStructHandler(self.PDBFileName)
        structure = aSH.getStructure()

        model = structure[0]
        chain = model['B']
        chain.id = 'CC'
        aSH.renameChains(structure)
        for chain in structure.get_chains():
            self.assertEqual(chain.id, 'C')
예제 #10
0
 def testIntToChain(self):
     aSH = emconv.AtomicStructHandler(self.PDBFileName)
     solString = [
         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
         'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
         'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
         'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', "AA"
     ]
     for i in range(63):
         self.assertEqual(solString[i], aSH._intToChain(i))
예제 #11
0
 def pdbDownloadStep(self):
     """Download all pdb files in file_list and unzip them.
   """
     aSH = emconv.AtomicStructHandler()
     ASPaths = []
     for pdbId in self.pdbIds.get().split(','):
         print("retrieving atomic structure with ID = %s" % pdbId)
         ASPaths.append(
             aSH.readFromPDBDatabase(pdbId.strip(),
                                     type='mmCif',
                                     dir=self._getExtraPath()))
     self.createOutputStep(ASPaths)
예제 #12
0
 def testFunctionSelectChain(self):
     pdbID1 = '1P30'  # A,B,C
     outFile = "/tmp/model.cif"
     aSH1 = emconv.AtomicStructHandler()
     aSH2 = emconv.AtomicStructHandler()
     #
     fileName1 = aSH1.readFromPDBDatabase(pdbID1, type='mmCif', dir='/tmp')
     aSH1.read(fileName1)
     atomsNum1 = len([atom.id for atom in aSH1.getStructure().get_atoms()])
     #
     chainID = 'A'
     outFileName = "/tmp/output.mmcif"
     aSH1.extractChain(chainID=chainID,
                       modelID='0',
                       end=20,
                       filename=outFileName)
     chains = [chain.id for chain in aSH1.getStructure().get_chains()]
     # compare unordered lists of chains
     goal = chainID
     self.assertTrue(
         Counter(chains) == Counter(goal), "{} != {}".format(chains, goal))
     aSH2.read(outFileName)
     atomsNumT = len([atom.id for atom in aSH2.getStructure().get_atoms()])
     self.assertEqual(122, atomsNumT)
예제 #13
0
 def testTransformTranslation(self):
     aSHSource = emconv.AtomicStructHandler(self.PDBFileName)
     structure = aSHSource.getStructure()
     structure_copy = deepcopy(aSHSource.getStructure())
     shift = [100., 50., 25.]
     #        rotation_matrix = euler_matrix(deg2rad(45.), 0., 0., 'szyz')
     rotation_matrix = emconv.euler_matrix(0., 0., 0., 'szyz')
     translation = emconv.translation_matrix(shift)
     M = emconv.concatenate_matrices(rotation_matrix, translation)
     aSHSource.transform(M)
     for atom1, atom2 in zip(structure.get_atoms(),
                             structure_copy.get_atoms()):
         coord1 = atom1.get_coord()
         coord2 = [sum(x) for x in zip(atom2.get_coord(), shift)]
         for i in range(3):
             self.assertAlmostEqual(coord1[i], coord2[i], 2)
예제 #14
0
 def testTransformRotation(self):
     aSHSource = emconv.AtomicStructHandler(self.PDBFileName)
     structure = aSHSource.getStructure()
     structure_copy = deepcopy(structure)
     rot = np.deg2rad(10)
     theta = np.deg2rad(20.)
     psi = np.deg2rad(30.)
     rotation_matrix = emconv.euler_matrix(rot, theta, psi, 'szyz')
     translation = emconv.translation_matrix([0., 0., 0.])
     M = emconv.concatenate_matrices(rotation_matrix, translation)
     aSHSource.transform(M)
     m = M[:3, :3]
     for atom1, atom2 in zip(structure.get_atoms(),
                             structure_copy.get_atoms()):
         coord1 = atom1.get_coord()
         coord2 = m.dot(atom2.get_coord())
         for i in range(3):
             self.assertAlmostEqual(coord1[i], coord2[i], 2)
예제 #15
0
    def testReadCIF(self):
        aSH = emconv.AtomicStructHandler(self.CIFFileName)
        structure = aSH.getStructure()

        solList = ['N', 'CA', 'C', 'O', 'CB']
        counter = 0
        for atom in structure.get_atoms():
            self.assertEqual(solList[counter], atom.get_name())
            counter += 1

        solDict = {}
        solDict['_exptl.method'] = 'x-ray diffraction'
        solDict['_struct_keywords.pdbx_keywords'] = 'extracellular matrix'
        solDict['_struct.title'] = 'x-ray crystallographic determination ' \
                                   'of a collagen-like peptide with the ' \
                                   'repeating sequence (pro-pro-gly)'
        _dict = aSH.readLowLevel(self.CIFFileName)

        for k, v in solDict.items():
            self.assertEqual(_dict[k][0].strip().lower(), v.lower())
예제 #16
0
    def testReadPDB(self):
        aSH = emconv.AtomicStructHandler(self.PDBFileName)
        structure = aSH.getStructure()

        solDict = {}
        solDict['structure_method'] = 'x-ray diffraction'
        solDict['head'] = 'extracellular matrix'
        solDict['name'] = 'x-ray crystallographic determination ' \
                          'of a collagen-like peptide with the ' \
                          'repeating sequence (pro-pro-gly)'
        solDict['author'] = 'R.Z.Kramer,L.Vitagliano,J.Bella,R.Berisio,' \
                            'L.Mazzarella,B.Brodsky,A.Zagari,H.M.Berman'
        solDict['deposition_date'] = '1998-01-22'
        for k, v in solDict.items():
            self.assertEqual(structure.header[k].strip(), v)

        solList = ['N', 'CA', 'C', 'O', 'CB']
        counter = 0
        for atom in structure.get_atoms():
            self.assertEqual(solList[counter], atom.get_name())
            counter += 1
예제 #17
0
    def testTransformTranslationCoherence(self):
        """
        Question: If I transform the PDB and the 3D map with matrix T
        do they move in the same direction?
        I do not know how to make an automatic test to check this
        The following code perform all operations but check that
        the PDB and 3D map match. This should be check using
        your eye.

        change False to True (in the following if) to
        perform all or some of the checks
        """

        # retrieve "Structure of the human TRPC3
        # both 3Dmap and PDB

        doTest = False

        if not doTest:

            print(
                "This test is to be tested manually since it opens chimera afterwards"
            )
            print("For testing this, edit this file and set doTest = True")
            return

        PDBID = '6CUD'
        EMDBID = '7620'

        doAll = False

        if False or doAll:  # set to False if you aready have the 3dmap file
            url = 'ftp://ftp.ebi.ac.uk/pub/databases/emdb/structures/EMD-%s/map/emd_%s.map.gz' % \
                  (EMDBID, EMDBID)
            urlretrieve(url, 'emd_%s.map.gz' % EMDBID)
            os.system("gunzip emd_%s.map.gz" % EMDBID)  # file is gzipped
        if False or doAll:  # set to False if you aready have the PDB file
            aSH = emconv.AtomicStructHandler()
            pdbFileName = aSH.readFromPDBDatabase(PDBID,
                                                  type='pdb',
                                                  dir=os.getcwd())
        else:
            pdbFileName = 'pdb%s.ent' % PDBID.lower()

        # get 3D map sampling
        from pwem.convert.headers import Ccp4Header
        header = Ccp4Header("emd_%s.map" % EMDBID, readHeader=True)
        sampling, y, z = header.getSampling()

        def __runXmippProgram(program, args):
            """ Internal function to launch a Xmipp program. """
            from pwem import Domain
            xmipp3 = Domain.importFromPlugin('xmipp3', doRaise=True)
            xmipp3.runXmippProgram(program, args)

        def __getXmippEulerAngles(matrix):
            """ Internal fuction to convert scipion to xmipp angles"""
            from pwem import Domain
            geometryFromMatrix = Domain.importFromPlugin('xmipp3.convert',
                                                         'geometryFromMatrix',
                                                         doRaise=True)

            return geometryFromMatrix(matrix, False)

        def __applyTransform(suffix, pdbFileName, shift, angles, sampling):
            """ auxiliary function, transform PDB and 3dmap files"""
            # create a Scipion transformation matrix
            from numpy import deg2rad
            rotation_matrix = emconv.euler_matrix(deg2rad(angles[0]),
                                                  deg2rad(angles[1]),
                                                  deg2rad(angles[2]), 'szyz')
            translation = emconv.translation_matrix(shift)
            M = emconv.concatenate_matrices(rotation_matrix, translation)

            # apply it to the pdb file
            # if rotation move to center
            aSH = emconv.AtomicStructHandler(pdbFileName)
            if angles[0] != 0. or angles[1] != 0. or angles[2] != 0.:
                ih = ImageHandler()
                x, y, z, n = ih.getDimensions("emd_%s.map" % EMDBID)
                x /= 2.
                y /= 2.
                z /= 2.
                localShift = [-x, -y, -z]
                rotation_matrix = emconv.euler_matrix(0., 0., 0., 'szyz')
                translation = emconv.translation_matrix(localShift)
                localM = emconv.concatenate_matrices(rotation_matrix,
                                                     translation)
                aSH.transform(localM, sampling=sampling)

            aSH.transform(M, sampling=sampling)

            if angles[0] != 0. or angles[1] != 0. or angles[2] != 0.:
                localShift = [x, y, z]
                rotation_matrix = emconv.euler_matrix(0., 0., 0., 'szyz')
                translation = emconv.translation_matrix(localShift)
                localM = emconv.concatenate_matrices(rotation_matrix,
                                                     translation)
                aSH.transform(localM, sampling=sampling)

            aSH.write("%s_%s_transformed.ent" % (suffix, PDBID.lower()))

            # get equivalent xmipp transformation
            shift, angles = __getXmippEulerAngles(M)
            # shift 3map and set sampling
            __runXmippProgram(
                "xmipp_transform_geometry", '-i emd_%s.map '
                '-o %s_emd_%s_transform.map '
                '--interp linear '
                '--shift %f %f %f '
                '--rotate_volume euler %f %f %f ' %
                (EMDBID, suffix, EMDBID, shift[0], shift[1], shift[2],
                 angles[0], angles[1], angles[2]))
            header = Ccp4Header("%s_emd_%s_transform.map" % (suffix, EMDBID),
                                readHeader=True)
            header.setSampling(sampling)
            # put the sampling back, xmipp_transform_geometry erased it
            header.writeHeader()

            # view the results with chimera
            from pwem.viewers import Chimera
            args = "%s %s %s %s" % (
                pdbFileName, "emd_%s.map" % EMDBID, "%s_%s_transformed.ent" %
                (suffix, PDBID.lower()), "%s_emd_%s_transform.map" %
                (suffix, EMDBID))
            Chimera.runProgram(args)

        # shift atomic structure
        doAll = True
        if False or doAll:
            shift = [20., 0., 0.]
            angles = [0., 0., 0.]
            __applyTransform("Xshift", pdbFileName, shift, angles, sampling)

        # repeat test this time  rotation one angle
        # problem, xmipp rotates with respect the volume center
        # pdb with respect the origin of coordinates (much better convention)
        # in order to compare both I need to
        # move pdb to origin, rotate it, put it back in the possition
        if False or doAll:
            shift = [0., 0., 0.]
            angles = [30., 0., 0.]
            __applyTransform("Rot2D", pdbFileName, shift, angles, sampling)

        # repeat test this time  rotation in 3 angles
        # problem, xmipp rotates with respect the volume center
        # pdb with respect the origin of coordinates (much better convention)
        if False or doAll:
            shift = [0., 0., 0.]
            angles = [10., 20., 30.]
            __applyTransform("Rot3D", pdbFileName, shift, angles, sampling)

        # repeat test this time  rotation in 3 angles and shift
        # problem, xmipp rotates with respect the volume center
        # pdb with respect the origin of coordinates (much better convention)
        if False or doAll:
            shift = [5., 10., 15.]
            angles = [10., 20., 30.]
            __applyTransform("Rot3DShift", pdbFileName, shift, angles,
                             sampling)
예제 #18
0
        def __applyTransform(suffix, pdbFileName, shift, angles, sampling):
            """ auxiliary function, transform PDB and 3dmap files"""
            # create a Scipion transformation matrix
            from numpy import deg2rad
            rotation_matrix = emconv.euler_matrix(deg2rad(angles[0]),
                                                  deg2rad(angles[1]),
                                                  deg2rad(angles[2]), 'szyz')
            translation = emconv.translation_matrix(shift)
            M = emconv.concatenate_matrices(rotation_matrix, translation)

            # apply it to the pdb file
            # if rotation move to center
            aSH = emconv.AtomicStructHandler(pdbFileName)
            if angles[0] != 0. or angles[1] != 0. or angles[2] != 0.:
                ih = ImageHandler()
                x, y, z, n = ih.getDimensions("emd_%s.map" % EMDBID)
                x /= 2.
                y /= 2.
                z /= 2.
                localShift = [-x, -y, -z]
                rotation_matrix = emconv.euler_matrix(0., 0., 0., 'szyz')
                translation = emconv.translation_matrix(localShift)
                localM = emconv.concatenate_matrices(rotation_matrix,
                                                     translation)
                aSH.transform(localM, sampling=sampling)

            aSH.transform(M, sampling=sampling)

            if angles[0] != 0. or angles[1] != 0. or angles[2] != 0.:
                localShift = [x, y, z]
                rotation_matrix = emconv.euler_matrix(0., 0., 0., 'szyz')
                translation = emconv.translation_matrix(localShift)
                localM = emconv.concatenate_matrices(rotation_matrix,
                                                     translation)
                aSH.transform(localM, sampling=sampling)

            aSH.write("%s_%s_transformed.ent" % (suffix, PDBID.lower()))

            # get equivalent xmipp transformation
            shift, angles = __getXmippEulerAngles(M)
            # shift 3map and set sampling
            __runXmippProgram(
                "xmipp_transform_geometry", '-i emd_%s.map '
                '-o %s_emd_%s_transform.map '
                '--interp linear '
                '--shift %f %f %f '
                '--rotate_volume euler %f %f %f ' %
                (EMDBID, suffix, EMDBID, shift[0], shift[1], shift[2],
                 angles[0], angles[1], angles[2]))
            header = Ccp4Header("%s_emd_%s_transform.map" % (suffix, EMDBID),
                                readHeader=True)
            header.setSampling(sampling)
            # put the sampling back, xmipp_transform_geometry erased it
            header.writeHeader()

            # view the results with chimera
            from pwem.viewers import Chimera
            args = "%s %s %s %s" % (
                pdbFileName, "emd_%s.map" % EMDBID, "%s_%s_transformed.ent" %
                (suffix, PDBID.lower()), "%s_emd_%s_transform.map" %
                (suffix, EMDBID))
            Chimera.runProgram(args)