Ejemplo n.º 1
0
    def __deserializeMmCif(self, locator, **kwargs):
        """ """
        try:
            containerList = []
            workPath = kwargs.get("workPath", None)
            enforceAscii = kwargs.get("enforceAscii", True)
            raiseExceptions = kwargs.get("raiseExceptions", True)
            useCharRefs = kwargs.get("useCharRefs", True)
            minSize = kwargs.get("minSize", 5)
            #
            if self.__fileU.isLocal(locator):
                if minSize >= 0 and not self.__hasMinSize(locator, minSize):
                    logger.warning("Minimum file size not satisfied for: %r",
                                   locator)
                myIo = IoAdapter(raiseExceptions=raiseExceptions,
                                 useCharRefs=useCharRefs)
                containerList = myIo.readFile(
                    locator, enforceAscii=enforceAscii,
                    outDirPath=workPath)  # type: ignore
            else:
                # myIo = IoAdapterPy(raiseExceptions=raiseExceptions, useCharRefs=useCharRefs)
                # containerList = myIo.readFile(locator, enforceAscii=enforceAscii, outDirPath=workPath)
                containerList = self.__deserializeMmCifRemote(
                    locator, useCharRefs, enforceAscii, workPath)

        except Exception as e:
            logger.error("Failing for %s with %s", locator, str(e))
        return containerList
Ejemplo n.º 2
0
 def __serializeMmCif(self, filePath, containerList, **kwargs):
     """ """
     try:
         ret = False
         workPath = kwargs.get("workPath", None)
         enforceAscii = kwargs.get("enforceAscii", True)
         raiseExceptions = kwargs.get("raiseExceptions", True)
         useCharRefs = kwargs.get("useCharRefs", True)
         #
         myIo = IoAdapter(raiseExceptions=raiseExceptions,
                          useCharRefs=useCharRefs)
         if filePath.endswith(".gz") and workPath:
             rfn = "".join(
                 random.choice(string.ascii_uppercase + string.digits)
                 for _ in range(10))
             tPath = os.path.join(workPath, rfn)
             ret = myIo.writeFile(tPath,
                                  containerList=containerList,
                                  enforceAscii=enforceAscii)
             ret = self.__fileU.compress(tPath,
                                         filePath,
                                         compressType="gzip")
         else:
             ret = myIo.writeFile(filePath,
                                  containerList=containerList,
                                  enforceAscii=enforceAscii)
     except Exception as e:
         logger.error("Failing for %s with %s", filePath, str(e))
     return ret
    def merge(srcpath,
              newcontentpath,
              outpath,
              mergelist=None,
              replacelist=None):
        """ Merges selected categories from newcontentpath (first block) into srcpath and outputs to outpath in
                       first data block.
            Categories in replacelist will replace category, if present in newcontentpath, otherwise leaves alone.
            Categories in mergelist must be single row. Attributes will be replaced/appended. Attributes not in
                       newcontentpath are ignored.

            newcontentpath will contain to combined file

            If the same category is in mergelist and replacelist, undefined behaviour

        """
        logger.debug("Starting merge %s %s %s" %
                     (srcpath, newcontentpath, outpath))
        try:
            io = IoAdapterCore()
            srcin = io.readFile(srcpath)
            srcblock = srcin[0]

            newcontent = io.readFile(newcontentpath)
            newcontentblock = newcontent[0]

            # The plan is to replace/modify the datablock - and then rewrite
            if replacelist:
                for cat in replacelist:
                    if cat in newcontentblock.getObjNameList():
                        obj = newcontentblock.getObj(cat)
                        srcblock.append(obj)

            if mergelist:
                for cat in mergelist:
                    if cat in newcontentblock.getObjNameList():
                        obj = newcontentblock.getObj(cat)
                        if srcblock.exists(cat):
                            # Category exists in file
                            srcobj = srcblock.getObj(cat)
                            for attr in obj.getAttributeList():
                                val = obj.getValue(attr, 0)
                                if not srcobj.hasAttribute(attr):
                                    srcobj.appendAttribute(attr)
                                srcobj.setValue(val, attr, 0)
                        else:
                            # New category
                            srcblock.append(obj)

            ret = io.writeFile(outpath, srcin)
            return ret

        except Exception as e:
            logger.exception("Failing with %s" % str(e))
            return False
Ejemplo n.º 4
0
 def __testFileReader(self, fp, enforceAscii=False, raiseExceptions=True):
     """Test case -  read PDBx file"""
     try:
         io = IoAdapter(raiseExceptions=raiseExceptions)
         containerList = io.readFile(fp,
                                     enforceAscii=enforceAscii,
                                     outDirPath=self.__pathOutputDir)
         logger.debug("Read %d data blocks", len(containerList))
         self.assertEqual(len(containerList), 1)
     except Exception as e:
         logger.error("Failing with %s", str(e))
         self.fail()
Ejemplo n.º 5
0
 def setChemCompPath(self, ccPath):
     try:
         myReader = IoAdapter(self.__verbose, self.__lfh)
         cL = myReader.readFile(ccPath)
         self.__ccId = cL[0].getName()
         self.__dcChemCompAtom = cL[0].getObj("chem_comp_atom")
         self.__dcChemCompBond = cL[0].getObj("chem_comp_bond")
         return self.__ccId
     except Exception as e:
         self.__lfh.write("OeBuildMol(setChemCompPath) Fails for %s %s\n" %
                          (ccPath, str(e)))
         traceback.print_exc(file=self.__lfh)
     return None
Ejemplo n.º 6
0
 def __testFileReaderWriter(self, ifp, ofp, **kwargs):
     """Test case -  read and then write PDBx file or dictionary"""
     try:
         io = IoAdapter(raiseExceptions=True, useCharRefs=True)
         containerList = io.readFile(ifp,
                                     enforceAscii=True,
                                     outDirPath=self.__pathOutputDir)
         logger.debug("Read %d data blocks", len(containerList))
         ok = io.writeFile(ofp, containerList=containerList, **kwargs)
         self.assertTrue(ok)
     except Exception as e:
         logger.error("Failing with %s", str(e))
         self.fail()
    def __write_mmcif(self, pathout, coef, entry_id):
        """Writes out the specific map coefficients

        """

        # Categories that will not be copied
        _striplist = [
            'audit', 'diffrn_radiation_wavelength', 'exptl_crystal',
            'reflns_scale'
        ]

        # refln attributes to keep
        _keepattr = ['index_h', 'index_k', 'index_l', 'fom']
        if coef == 'fo':
            _keepattr.extend(['pdbx_DELFWT', 'pdbx_DELPHWT'])
        else:
            _keepattr.extend(['pdbx_FWT', 'pdbx_PHWT'])

        # Datablockname
        blkname = "{}{}".format(entry_id, coef)
        new_cont = DataContainer(blkname)

        # Only care about first block
        blockin = self.__sf[0]

        for objname in blockin.getObjNameList():
            if objname in _striplist:
                continue

            myobj = blockin.getObj(objname)

            # Make a copy of the original - as likely will need to modify
            modobj = copy.deepcopy(myobj)
            if objname == 'entry':
                modobj.setValue(entry_id, 'id', 0)
            if objname in ['cell', 'symmetry']:
                modobj.setValue(entry_id, 'entry_id', 0)
            if objname == 'refln':
                # Remove all but what we want
                # Make a copy to ensure not messed with during operation
                for attr in list(modobj.getAttributeList()):
                    if attr not in _keepattr:
                        modobj.removeAttribute(attr)

            new_cont.append(modobj)

        # new_cont.printIt()
        io = IoAdapterCore()
        # Write out a single block
        ret = io.writeFile(pathout, [new_cont])
        return ret
Ejemplo n.º 8
0
    def testIoFileCase(self):
        """Test case sensitive IoAdapter writer"""

        if os.path.exists(self.__pathOutputFile2):
            os.unlink(self.__pathOutputFile2)

        curContainer = self.__generateData()

        myDataList = [curContainer]
        io = IoAdapter(raiseExceptions=True)
        ok = io.writeFile(self.__pathOutputFile2, containerList=myDataList)
        self.assertTrue(ok, "Writing data test")

        self.__testReaders(self.__pathOutputFile2)
    def read_mmcif_sf(self, pathin):
        """Reads PDBx/mmCIF structure factor file with map coefficients

           Return True on success, otherwise False
        """

        logger.debug("Starting read %s" % pathin)
        try:
            io = IoAdapterCore()
            self.__sf = io.readFile(pathin)
            return True
        except Exception as e:
            logger.exception("Failing with %s" % str(e))
            self.__sf = None
            return False
Ejemplo n.º 10
0
 def __testDictReader(self, fp, enforceAscii=False):
     """Test case -  read PDBx dictionary file"""
     try:
         io = IoAdapter(raiseExceptions=True)
         containerList = io.readFile(fp,
                                     enforceAscii=enforceAscii,
                                     outDirPath=self.__pathOutputDir)
         logger.info("Read %d data blocks", len(containerList))
         for container in containerList:
             container.printIt()
         #
         self.assertTrue(len(containerList) > self.__testBlockCount)
     except Exception as e:
         logger.error("Failing with %s", str(e))
         self.fail()
Ejemplo n.º 11
0
    def __getPDBId(self):
        """Returns the PDB accession code in model file or None"""
        if not self.__modelfile:
            return None
        io = IoAdapterCore()
        cont = io.readFile(self.__modelfile, selectList=['database_2'])
        if cont:
            block = cont[0]
            catObj = block.getObj('database_2')
            if catObj:
                vals = catObj.selectValuesWhere('database_code', 'PDB', 'database_id')
                if len(vals) > 0 and vals[0] and vals[0] and len(vals[0]) > 0 and vals[0] not in ['.', '?']:
                    return vals[0]

        return None
Ejemplo n.º 12
0
    def __getPDBId(self):
        """Returns the PDB accession code in model file or None"""
        if not self.__modelfile:
            return None
        io = IoAdapterCore()
        cont = io.readFile(self.__modelfile, selectList=["database_2"])
        if cont:
            block = cont[0]
            catObj = block.getObj("database_2")
            if catObj:
                vals = catObj.selectValuesWhere("database_code", "PDB",
                                                "database_id")
                if len(vals) > 0 and vals[0] and vals[0] and len(
                        vals[0]) > 0 and vals[0] not in [".", "?"]:
                    return vals[0]

        return None
Ejemplo n.º 13
0
 def __deserializeMmCif(self, filePath, **kwargs):
     """"""
     try:
         containerList = []
         workPath = kwargs.get("workPath", None)
         enforceAscii = kwargs.get("enforceAscii", True)
         raiseExceptions = kwargs.get("raiseExceptions", True)
         useCharRefs = kwargs.get("useCharRefs", True)
         #
         myIo = IoAdapter(raiseExceptions=raiseExceptions,
                          useCharRefs=useCharRefs)
         containerList = myIo.readFile(filePath,
                                       enforceAscii=enforceAscii,
                                       outDirPath=workPath)  # type: ignore
     except Exception as e:
         logger.error("Failing for %s with %s", filePath, str(e))
     return containerList
Ejemplo n.º 14
0
 def __testFileReaderExceptionHandler2(self, fp, enforceAscii=False):
     """Test case -  read selected categories from PDBx and handle exceptions"""
     ok = True
     try:
         io = IoAdapter(raiseExceptions=True)
         containerList = io.readFile(fp,
                                     enforceAscii=enforceAscii,
                                     outDirPath=self.__pathOutputDir)
         self.assertGreaterEqual(len(containerList), 1)
         #
     except PdbxSyntaxError as e:
         logger.debug("Expected syntax failure %s", str(e))
         self.assertTrue(ok)
     except PdbxError as e:
         logger.debug("Expected character encoding failure %s", str(e))
         self.assertTrue(ok)
     except Exception as e:
         logger.error("Unexpected exception %s", type(e).__name__)
         self.fail("Unexpected exception raised: " + str(e))
     else:
         self.fail("Expected exception not raised")
Ejemplo n.º 15
0
 def __testFileReaderWriterSelect(self,
                                  ifp,
                                  ofp,
                                  selectList=None,
                                  excludeFlag=False):
     """Test case -  read and then write PDBx file with selection."""
     try:
         io = IoAdapter(raiseExceptions=True, useCharRefs=True)
         containerList = io.readFile(ifp,
                                     enforceAscii=True,
                                     selectList=selectList,
                                     excludeFlag=excludeFlag,
                                     outDirPath=self.__pathOutputDir)
         logger.debug("Read %d data blocks", len(containerList))
         ok = io.writeFile(ofp,
                           containerList=containerList,
                           enforceAscii=True)
         self.assertTrue(ok)
     except Exception as e:
         logger.error("Failing with %s", str(e))
         self.fail()
Ejemplo n.º 16
0
    def __testReaders(self, fPath):
        """Tests python and IoAdapter readers and checks values"""
        # Python reader

        myContainerList = []
        with open(fPath, "r") as ifh:
            pRd = PdbxReader(ifh)
            pRd.read(myContainerList)

        self.__testValues(myContainerList)

        # C++ IoAdapter reader
        try:
            io = IoAdapter(raiseExceptions=True)
            containerList = io.readFile(fPath, outDirPath=self.__pathOutputDir)
            logger.debug("Read %d data blocks", len(containerList))
            self.assertEqual(len(containerList), 1)
        except Exception as e:
            logger.error("Failing with %s", str(e))
            self.fail()

        self.__testValues(containerList)
Ejemplo n.º 17
0
    def setUp(self):
        self.__lfh = sys.stderr
        self.__verbose = True
        # default database
        self.__databaseName = "prdv4"
        self.__birdCachePath = "/data/components/prd-v3"
        self.__birdFamilyCachePath = "/data/components/family-v3"
        self.__ccCachePath = "/data/components/ligand-dict-v3"
        #
        self.__ccFileList = ["BA1T.cif"]
        self.__ccPath = "./data"
        #
        self.__pdbxPath = "../rcsb/data"
        self.__pdbxFileList = ["1cbs.cif", "1o3q.cif", "1xbb.cif", "3of4.cif", "3oqp.cif", "3rer.cif", "3rij.cif", "5hoh.cif"]

        self.__ioObj = IoAdapterCore(verbose=self.__verbose, log=self.__lfh)
        #
        self.open()
Ejemplo n.º 18
0
 def __init__(self,
              schemaDefObj,
              ioObj=IoAdapterCore(),
              dbCon=None,
              workPath=".",
              cleanUp=False,
              warnings="default",
              verbose=True,
              log=sys.stderr):
     self.__lfh = log
     self.__verbose = verbose
     self.__debug = False
     self.__sD = schemaDefObj
     self.__ioObj = ioObj
     #
     self.__dbCon = dbCon
     self.__workingPath = workPath
     self.__cleanUp = cleanUp
     #
     self.__colSep = "&##&\t"
     self.__rowSep = "$##$\n"
     #
     self.__warningAction = warnings
     self.__overWrite = {}
Ejemplo n.º 19
0
 def setUp(self):
     self.__lfh = sys.stderr
     self.__verbose = False
     self.__ioObj = IoAdapterCore(verbose=self.__verbose, log=self.__lfh)
     self.__topCachePath = "/data/components/ligand-dict-v3"
     self.__dbCon = None
Ejemplo n.º 20
0
def process_entry(file_in, file_out):
    try:
        cif_file = gemmi.cif.read(file_in)  # pylint: disable=no-member
        data_block = cif_file[0]
    except Exception as e:
        logger.error("Failed to read cif file in Gemmi")
        logger.error(e)
        return 1

    logging.info("Finding Centre of Mass")
    com = get_center_of_mass(data_block)
    if not com:
        return 1

    try:
        io = IoAdapterCore()
        ccL = io.readFile(file_in)
    except Exception as e:
        logger.error("Failed to read cif file using IoAdapterCore %s", e)
        return 1

    if len(ccL) == 0:
        logger.error("No data parsed from file")
        return 1

    # First block only
    b0 = ccL[0]

    obj = b0.getObj("struct")
    # If category does not exist
    if obj is None:
        # Need entry.id
        eid = "XXXX"
        eobj = b0.getObj("entry")
        if eobj:
            if "id" in eobj.getAttributeList():
                eid = eobj.getValue("id", 0)
        obj = DataCategory("struct")
        obj.appendAttribute("entry_id")
        obj.setValue(eid, "entry_id", 0)
        ccL[0].append(obj)

    newdata = [["pdbx_center_of_mass_x", com.x],
               ["pdbx_center_of_mass_y", com.y],
               ["pdbx_center_of_mass_z", com.z]]
    for [it, val] in newdata:
        if it not in obj.getAttributeList():
            obj.appendAttribute(it)
        obj.setValue(str(val), it, 0)

    try:
        logging.info("Writing mmcif file: %s", file_out)
        ret = io.writeFile(file_out, ccL)
        if not ret:
            logger.info("Writing failed error %s", ret)
            return 1
    except Exception as e:
        logger.error("Failed to write ccif file in IoAdapater %s", e)
        return 1

    # existing_data = data_block.get_mmcif_category('_struct.')
    # new_data = {
    #     **existing_data,
    #     'pdbx_center_of_mass_x': [com.x],
    #     'pdbx_center_of_mass_y': [com.y],
    #     'pdbx_center_of_mass_z': [com.z]
    # }
    # logging.info("Writing mmcif file: %s", file_out)
    # try:
    #     data_block.set_mmcif_category('_struct.', new_data)
    #     cif_file.write_file(file_out)
    # except Exception as e:
    #     logger.error("Failed to write cif file in Gemmi")
    #     logger.error(e)
    #     return 1
    return 0