def testExtensions(self): """Test case - condition extensions""" try: myIo = IoAdapter(raiseExceptions=True) self.__containerList = myIo.readFile(inputFilePath=self.__pathPdbxDictionary) self.__containerList.extend(myIo.readFile(inputFilePath=self.__pathPdbxDictionaryExtension)) dApi = DictionaryApi(containerList=self.__containerList, consolidate=True) tD = dApi.getItemValueConditionDict() logger.debug("tD \n%s", pprint.pformat(tD)) self.assertGreaterEqual(len(tD), 2) tD = dApi.getComparisonOperatorDict() logger.debug("tD \n%s", pprint.pformat(tD)) self.assertGreaterEqual(len(tD), 5) tL = dApi.getComparisonOperators() logger.debug("tL %r", tL) self.assertGreaterEqual(len(tL), 5) # tD = dApi.getItemLinkedConditions() logger.debug("tD \n%s", pprint.pformat(tD)) self.assertGreaterEqual(len(tD), 1) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def main(): parser = argparse.ArgumentParser() # # parser.add_argument("--op", default=None, required=True, help="Operation (build | get_version)") parser.add_argument("--input_dict_path", required=True, default=None, help="Path to dictionary generator file") parser.add_argument("--output_dict_path", default=None, help="Path to output dictionary text file") parser.add_argument( "--cleanup", default=False, action="store_true", help="Remove include instruction categories after processing") args = parser.parse_args() # try: op = args.op.lower() if args.op else None inputDictPath = args.input_dict_path outputDictPath = args.output_dict_path cleanup = args.cleanup except Exception as e: logger.exception("Argument processing problem %s", str(e)) parser.print_help(sys.stderr) exit(1) ## if op == "build" and inputDictPath and outputDictPath: dirPath = os.path.abspath(os.path.dirname(inputDictPath)) logger.info("Starting dictionary path %s", dirPath) myIo = IoAdapter(raiseExceptions=True) containerList = myIo.readFile(inputFilePath=inputDictPath) logger.info("Starting dictionary container list length (%d)", len(containerList)) dIncl = DictionaryInclude(dirPath=dirPath) inclL = dIncl.processIncludedContent(containerList, cleanup=cleanup) logger.info("Processed dictionary container length (%d)", len(inclL)) ok = myIo.writeFile(outputFilePath=outputDictPath, containerList=inclL) logger.info("Operation completed with status %r", ok) elif op == "get_version" and inputDictPath: logger.setLevel(logging.ERROR) myIo = IoAdapter(raiseExceptions=True) containerList = myIo.readFile(inputFilePath=inputDictPath) dIncl = DictionaryInclude() inclL = dIncl.processIncludedContent(containerList) baseContainer = inclL[0] if baseContainer.exists("dictionary"): cObj = baseContainer.getObj("dictionary") version = cObj.getValueOrDefault("version", 0, None) print(version)
def test_invoke_dictionary_methods(self, test_paths): myIo = IoAdapter() dictContainerList = myIo.readFile(inputFilePath=str(test_paths['pathPdbxDictFile'])) mU = MethodUtils(dictContainerList=dictContainerList) dataContainerList = myIo.readFile(inputFilePath=str(test_paths['pathPdbxDataFile'])) mU.setDataContainerList(dataContainerList=dataContainerList) mU.invokeMethods() print("Write data file after invoking methods") dataContainerList = mU.getDataContainerList() ok = myIo.writeFile(outputFilePath=str(test_paths['pathOutFile']), containerList=dataContainerList) assert ok
def test_get_dictionary_methods(self, test_paths): myIo = IoAdapter() dictContainerList = myIo.readFile(inputFilePath=str(test_paths['pathPdbxDictFile'])) mU = MethodUtils(dictContainerList=dictContainerList) mU.dumpMethods(fh=sys.stdout) mD = mU.getMethods() assert len(mD) == 5
def testDumpEnums(self): """Test case - to verify enum ordering - """ try: myIo = IoAdapter(raiseExceptions=True) self.__containerList = myIo.readFile( inputFilePath=self.__pathPdbxDictionary) dApi = DictionaryApi(containerList=self.__containerList, consolidate=True, verbose=self.__verbose) # eList = dApi.getEnumListAlt(category="pdbx_audit_support", attribute="country") logger.debug("Item %s Enum list sorted %r\n", "country", eList) eList = dApi.getEnumListAlt(category="pdbx_audit_support", attribute="country", sortFlag=False) logger.debug("Item %s Enum list unsorted %r\n", "country", eList) eList = dApi.getEnumListAltWithDetail( category="pdbx_audit_support", attribute="country") logger.debug("Item %s Enum with detail list %r\n", "country", eList) self.assertGreater(len(eList), 100) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def test_file_reader_writer(self, io_data, ifp_key, ofp_key, enforceAscii): io = IoAdapter(raiseExceptions=True, useCharRefs=enforceAscii) containerList = io.readFile(str(io_data[ifp_key])) print ("Read %d data blocks" % len(containerList)) ok = io.writeFile(str(io_data[ofp_key]), containerList=containerList, enforceAscii=enforceAscii) assert ok
def test_get_adjacent_categories(self, api_paths): myIo = IoAdapter(raiseExceptions=True) containerList = myIo.readFile( inputFilePath=str(api_paths['pathPdbxDictionary'])) dApi = DictionaryApi(containerList=containerList, consolidate=True) cList = dApi.getCategoryList() cI = {} for c in cList: chL = dApi.getChildCategories(c) pL = dApi.getParentCategories(c) for ch in chL: if (ch, c) not in cI: cI[(ch, c)] = 1 else: cI[(ch, c)] += 1 for p in pL: if (c, p) not in cI: cI[(c, p)] = 1 else: cI[(c, p)] += 1 linkL = [] for s, t in cI.keys(): d = {'source': s, 'target': t, 'type': 'link'} linkL.append(d) assert len(linkL) > 50
def testSerialize(self): try: for storeStringsAsBytes in [True, False]: tcL = [] ioPy = IoAdapter() containerList = ioPy.readFile(self.__pathTextCif) for container in containerList: cName = container.getName() tc = DataContainer(cName) for catName in container.getObjNameList(): dObj = container.getObj(catName) tObj = DataCategoryTyped(dObj, dictionaryApi=self.__dApi, copyInputData=True) tc.append(tObj) tcL.append(tc) # bcw = BinaryCifWriter(self.__dApi, storeStringsAsBytes=storeStringsAsBytes, applyTypes=False, useFloat64=True) bcw.serialize(self.__testBcifOutput, tcL) self.assertEqual(containerList[0], containerList[0]) self.assertEqual(tcL[0], tcL[0]) bcr = BinaryCifReader(storeStringsAsBytes=storeStringsAsBytes) cL = bcr.deserialize(self.__testBcifOutput) # ioPy = IoAdapter() ok = ioPy.writeFile(self.__testBcifTranslated, cL) self.assertTrue(ok) self.assertTrue(self.__same(tcL[0], cL[0])) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testConsolidateDictionary(self): """Test case - dump methods for dictionary metadata""" try: myIo = IoAdapter(raiseExceptions=True) self.__containerList = myIo.readFile(inputFilePath=self.__pathPdbxDictionary) dApi = DictionaryApi(containerList=self.__containerList, consolidate=True, expandItemLinked=False, verbose=self.__verbose) for itemName in [ "_entity.id", "_entity_poly_seq.num", "_atom_site.label_asym_id", "_struct_asym.id", "_chem_comp.id", "chem_comp_atom.comp_id", "chem_comp_bond.comp_id", ]: categoryName = CifName.categoryPart(itemName) attributeName = CifName.attributePart(itemName) logger.debug("Full parent list for %s : %s\n", itemName, dApi.getFullParentList(categoryName, attributeName)) logger.debug("Full child list for %s : %s\n", itemName, dApi.getFullChildList(categoryName, attributeName)) logger.debug("Ultimate parent for %s : %s\n", itemName, dApi.getUltimateParent(categoryName, attributeName)) logger.debug("Type code for %s : %s\n", itemName, dApi.getTypeCode(categoryName, attributeName)) self.assertIsNotNone(dApi.getTypeCode(categoryName, attributeName)) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testGetAdjacentCategories(self): """Test case -""" try: myIo = IoAdapter(raiseExceptions=True) self.__containerList = myIo.readFile(inputFilePath=self.__pathPdbxDictionary) dApi = DictionaryApi(containerList=self.__containerList, consolidate=True, verbose=self.__verbose) cList = dApi.getCategoryList() cI = {} for cV in cList: chL = dApi.getChildCategories(cV) pL = dApi.getParentCategories(cV) for ch in chL: if (ch, cV) not in cI: cI[(ch, cV)] = 1 else: cI[(ch, cV)] += 1 for pV in pL: if (cV, pV) not in cI: cI[(cV, pV)] = 1 else: cI[(cV, pV)] += 1 linkL = [] for tup in cI: dD = {"source": tup[0], "target": tup[1], "type": "link"} linkL.append(dD) if self.__verbose: print(json.dumps(linkL, sort_keys=True, indent=4, separators=(",", ": "))) self.assertGreater(len(linkL), 50) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testDumpDictionary(self): """Test case - dump methods for dictionary metadata """ try: myIo = IoAdapter(raiseExceptions=True) self.__containerList = myIo.readFile( inputFilePath=self.__pathPdbxDictionary) dApi = DictionaryApi(containerList=self.__containerList, consolidate=True, verbose=self.__verbose) # dApi.dumpCategoryIndex(fh=self.__lfh) # dApi.dumpEnumFeatures(fh=self.__lfh) # dApi.dumpFeatures(fh=self.__lfh) # dApi.dumpMethods(fh=self.__lfh) logger.debug( "+++++++++++++++++++++++++++++++++++++++++++++++++++++++\n") groupList = dApi.getCategoryGroups() logger.debug("groupList %s\n", groupList) for group in groupList: logger.debug("Group %s category list %s\n", group, dApi.getCategoryGroupCategories(groupName=group)) self.assertGreater(len(groupList), 10) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def __testFileReaderExceptionHandler2(self, fp, enforceAscii=False, readEncodingErrors="ignore"): """Test case - read selected categories from PDBx and handle exceptions""" ok = True try: io = IoAdapter(raiseExceptions=True, readEncodingErrors=readEncodingErrors) containerList = io.readFile(fp, enforceAscii=enforceAscii, outDirPath=self.__pathOutputDir) logger.debug("Containerlist length %d ", len(containerList)) # 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.exception("Unexpected exception %s ", type(e).__name__) self.fail("Unexpected exception raised: " + str(e)) else: self.fail("Expected exception not raised")
def testGetUcode(self): """Test case - Get all data items of type ucode """ print("\n") try: myIo = IoAdapter(raiseExceptions=True) self.__containerList = myIo.readFile( inputFilePath=self.__pathPdbxDictionary) dApi = DictionaryApi(containerList=self.__containerList, consolidate=True, verbose=self.__verbose) logger.debug( "+++++++++++++++++++++++++++++++++++++++++++++++++++++++\n") catNameList = dApi.getCategoryList() for catName in catNameList: itemNameList = dApi.getItemNameList(catName) for itemName in itemNameList: categoryName = CifName.categoryPart(itemName) attributeName = CifName.attributePart(itemName) code = dApi.getTypeCode(categoryName, attributeName) if (code == "ucode"): print("Table: ", categoryName, "\tColumn: ", attributeName, "\tType: ", code) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def test_consolidate_dictionary(self, api_paths): myIo = IoAdapter(raiseExceptions=True) containerList = myIo.readFile( inputFilePath=str(api_paths['pathPdbxDictionary'])) dApi = DictionaryApi(containerList=containerList, consolidate=True) for itemName in [ '_entity.id', '_entity_poly_seq.num', '_atom_site.label_asym_id', '_struct_asym.id', '_chem_comp.id', 'chem_comp_atom.comp_id', 'chem_comp_bond.comp_id' ]: categoryName = CifName.categoryPart(itemName) attributeName = CifName.attributePart(itemName) print("Full parent list for %s : %s\n" % (itemName, dApi.getFullParentList(categoryName, attributeName))) print( "Full child list for %s : %s\n" % (itemName, dApi.getFullChildList(categoryName, attributeName))) print("Ultimate parent for %s : %s\n" % (itemName, dApi.getUltimateParent(categoryName, attributeName))) print("Type code for %s : %s\n" % (itemName, dApi.getTypeCode(categoryName, attributeName))) assert dApi.getTypeCode(categoryName, attributeName) is not None
def testDownloadReader(self): """Test case - download/read text and compressed file""" try: io = IoAdapter(raiseExceptions=True) containerList = io.readFile(self.__pathGzipUrl, outDirPath=self.__pathOutputDir) logger.info("Read %d data blocks", len(containerList)) self.assertGreaterEqual(len(containerList[0].getObjNameList()), 60) io = IoAdapter(raiseExceptions=True) containerList = io.readFile(self.__pathTextUrl, outDirPath=self.__pathOutputDir) logger.info("Read %d data blocks", len(containerList)) self.assertGreaterEqual(len(containerList[0].getObjNameList()), 60) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testFileReader(self, io_data, fp_key, enforceAscii): io = IoAdapter(raiseExceptions=True) containerList = io.readFile(str(io_data[fp_key]), enforceAscii=enforceAscii, outDirPath=str(io_data['pathOutputDir'])) print ("Read %d data blocks" % len(containerList)) assert len(containerList) == 1
def test_file_reader_exception_handler_2(self, io_data, fp_key, expected_exc, enforceAscii): with pytest.raises(expected_exc): io = IoAdapter(raiseExceptions=True, readEncodingErrors='ignore') containerList = io.readFile(str(io_data[fp_key]), enforceAscii=enforceAscii, outDirPath=str(io_data['pathOutputDir'])) print ("Containerlist length %d " % len(containerList))
def testReadWriteStarFile(self): """Test case - star file read and write ...""" try: for fp in self.__pathStarFileList: myIo = IoAdapter(self.__verbose, self.__lfh) containerList = myIo.readFile(inputFilePath=fp) # # containerList is a flat list of containers in the order parsed. # # Create an index from the linear list data_ save_ sections and names -- # # There can multiple data blocks where each data section is followed # by save frames -- Names can be repeated and the application must # create an appropriate index of the data and save sections according # it own requirements. # # iD = {} iDN = {} dL = [] for container in containerList: if container.getType() == "data": dL.append(container) if container.getName() not in iD: curContainerName = container.getName() iD[curContainerName] = [] iDN[curContainerName] = [] else: logger.debug("Duplicate data block %s", container.getName()) else: iD[curContainerName].append(container) iDN[curContainerName].append(container.getName()) # # get the reference data out of the 2nd data block -- # if len(dL) > 1: c1 = dL[1] if "chemical_shift_reference_1" in iDN[c1.getName()]: idx = iDN[c1.getName()].index( "chemical_shift_reference_1") sf0 = iD[c1.getName()][idx] catObj = sf0.getObj("Chem_shift_ref") aL = catObj.getAttributeList() rowL = catObj.getRowList() logger.debug("Attribute list %s", aL) rowL = catObj.getRowList() for ii, row in enumerate(rowL): logger.debug(" %4d %r", ii, row) _, fnOut = os.path.split(fp) ofn = os.path.join(HERE, "test-output", fnOut + ".out") ok = myIo.writeFile(outputFilePath=ofn, containerList=containerList, useStopTokens=True) self.assertEqual(ok, True) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testDDLApi(self): """Test case - DDL API linkage counts""" try: myIo = IoAdapter(raiseExceptions=True) containerList = myIo.readFile( inputFilePath=self.__pathDdlGeneratedDictionary) dApi = DictionaryApi(containerList=containerList, consolidate=True, expandItemLinked=True) cL = dApi.getCategoryList() logger.info("Category length %r", len(cL)) self.assertGreaterEqual(len(cL), 63) cL = dApi.getFullChildList("category", "id") logger.info("Children of category.id (%d)", len(cL)) self.assertGreaterEqual(len(cL), 11) # cL = dApi.getFullDescendentList("category", "id") logger.info("Descendents of category.id (%d)", len(cL)) self.assertGreaterEqual(len(cL), 13) # cL = dApi.getFullChildList("item", "name") logger.info("Children of item.name (%d)", len(cL)) self.assertGreaterEqual(len(cL), 36) cL = dApi.getFullDescendentList("item", "name") logger.info("Descendents of item.name (%d)", len(cL)) self.assertGreaterEqual(len(cL), 38) # val = dApi.getDictionaryVersion() self.assertEqual(val, "2.2.2") val = dApi.getDictionaryTitle() self.assertEqual(val, "mmcif_ddl.dic") val = dApi.getDictionaryUpdate(order="reverse") self.assertEqual(val, "2020-06-05") val = dApi.getDictionaryRevisionCount() self.assertGreaterEqual(val, 78) valL = dApi.getDictionaryHistory(order="reverse") self.assertGreaterEqual(len(valL), 78) # val = dApi.getDictionaryComponentCount() self.assertGreaterEqual(val, 6) # valL = dApi.getDictionaryComponentDetails() self.assertGreaterEqual(len(valL), 6) valL = dApi.getDictionaryComponents() self.assertGreaterEqual(len(valL), 6) for dictionaryComponentId in dApi.getDictionaryComponents(): valL = dApi.getDictionaryComponentHistory( dictionaryComponentId, order="reverse") self.assertGreaterEqual(len(valL), 2) # except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testReadDictionary(self): """Test case - read logical structure of dictionary""" try: myIo = IoAdapterPy(self.__verbose, self.__lfh) containerList = myIo.readFile(inputFilePath=self.__pathPdbxDictionary) logger.debug("container list is %s", ([c.getName() for c in containerList])) self.assertGreaterEqual(len(containerList), 400) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testReadWriteDictionary(self): """Test case - read and dump logical structure of dictionary""" try: myIo = IoAdapterPy(self.__verbose, self.__lfh) containerList = myIo.readFile(inputFilePath=self.__pathPdbxDictionary) ok = myIo.writeFile(outputFilePath=os.path.join(HERE, "test-output", "test-dict-out.dic"), containerList=containerList) self.assertTrue(ok) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def test_file_reader_writer_select(self, io_data, ifp_key, ofp_key, selectList, excludeFlag): io = IoAdapter(raiseExceptions=False, useCharRefs=True) containerList = io.readFile(str(io_data[ifp_key]), enforceAscii=True, selectList=selectList, excludeFlag=excludeFlag, outDirPath=str(io_data['pathOutputDir'])) print ("Read %d data blocks" % len(containerList)) ok = io.writeFile(str(io_data[ofp_key]), containerList=containerList, enforceAscii=True) assert ok
def __deserializeMmCifRemote(self, locator, useCharRefs, enforceAscii, workPath): containerList = [] try: myIo = IoAdapterPy(raiseExceptions=True, useCharRefs=useCharRefs) containerList = myIo.readFile(locator, enforceAscii=enforceAscii, outDirPath=workPath) except Exception as e: raise e return containerList
def test_dump_index(self, api_paths): myIo = IoAdapter(raiseExceptions=True) containerList = myIo.readFile( inputFilePath=str(api_paths['pathPdbxDictionary'])) dApi = DictionaryApi(containerList=containerList, consolidate=True) print("Index = %r\n" % dApi.getItemNameList('pdbx_nmr_spectral_dim')) print("Index = %r\n" % dApi.getAttributeNameList('pdbx_nmr_spectral_dim')) catIndex = dApi.getCategoryIndex() print("Index = %r\n" % catIndex['pdbx_nmr_spectral_dim']) assert catIndex['pdbx_nmr_spectral_dim'] is not None
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.debug("Read %d data blocks", len(containerList)) self.assertTrue(len(containerList) > self.__testBlockCount) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def test_read_star_file(self, in_tmpdir, star_files_list): for fp in star_files_list: myIo = IoAdapter() containerList = myIo.readFile(inputFilePath=str(fp)) print("container list is %r\n" % ([(c.getName(), c.getType()) for c in containerList])) for c in containerList: c.setType('data') ofn = Path(fp.stem + '.cif') ok = myIo.writeFile(outputFilePath=str(ofn), containerList=containerList[1:]) assert ok
def testGetDictionaryMethods(self): """Test case - dump methods for dictionary metadata""" try: myIo = IoAdapter(self.__verbose, self.__lfh) dictContainerList = myIo.readFile(inputFilePath=self.__pathPdbxDictFile) mU = MethodUtils(dictContainerList=dictContainerList, verbose=self.__verbose) mU.dumpMethods(fh=self.__lfh) # mD = mU.getMethods() self.assertEqual(len(mD), 5) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def __testFileReaderWriter(self, ifp, ofp, **kwargs): """Test case - read and then write PDBx file or dictionary""" try: enforceAscii = kwargs.get("enforceAscii", True) useCharRefs = True if enforceAscii else False io = IoAdapter(raiseExceptions=True, useCharRefs=useCharRefs) containerList = io.readFile(ifp) logger.debug("Read %d data blocks", len(containerList)) ok = io.writeFile(ofp, containerList=containerList, **kwargs) self.assertTrue(ok) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def test_dump_dictionary(self, api_paths): myIo = IoAdapter(raiseExceptions=True) containerList = myIo.readFile( inputFilePath=str(api_paths['pathPdbxDictionary'])) dApi = DictionaryApi(containerList=containerList, consolidate=True) print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++\n') groupList = dApi.getCategoryGroups() print('groupList %s\n' % groupList) for group in groupList: print('Group %s category list %s\n' % (group, dApi.getCategoryGroupCategories(groupName=group))) assert len(groupList) > 10
def test_read_write_star_file(self, in_tmpdir, star_files_list): for fp in star_files_list: myIo = IoAdapter() containerList = myIo.readFile(inputFilePath=str(fp)) # # containerList is a flat list of containers in the order parsed. # # Create an index from the linear list data_ save_ sections and names -- # # There can multiple data blocks where each data section is followed # by save frames -- Names can be repeated and the application must # create an appropriate index of the data and save sections according # it own requirements. # # iD = {} iDN = {} dL = [] for container in containerList: if container.getType() == "data": dL.append(container) if container.getName() not in iD: curContainerName = container.getName() iD[curContainerName] = [] iDN[curContainerName] = [] else: print("Duplicate data block %s\n" % container.getName()) else: iD[curContainerName].append(container) iDN[curContainerName].append(container.getName()) # # get the reference data out of the 2nd data block -- # if len(dL) > 1: c1 = dL[1] if 'chemical_shift_reference_1' in iDN[c1.getName()]: idx = iDN[c1.getName()].index('chemical_shift_reference_1') sf0 = iD[c1.getName()][idx] catObj = sf0.getObj('Chem_shift_ref') aL = catObj.getAttributeList() rowL = catObj.getRowList() print("Attribute list %s\n" % aL) rowL = catObj.getRowList() for ii, row in enumerate(rowL): print(" %4d %r\n" % (ii, row)) ofn = Path(fp.stem + '.out') ok = myIo.writeFile(outputFilePath=str(ofn), containerList=containerList, useStopTokens=True) assert ok