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()
Exemple #2
0
    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
Exemple #3
0
class DictionaryData(object):
    def __init__(self):
        basedir = os.path.join(os.path.dirname(__file__), "..", "dist")
        self.dictPath = os.path.join(basedir, "mmcif_pdbx_v5_next.dic")

    def readDictionary(self):
        myIo = IoAdapterPy()
        self.__containerList = myIo.readFile(inputFilePath=self.dictPath)
        self._dApi = DictionaryApi(containerList=self.__containerList,
                                   consolidate=True)
        assert len(self.getGroups()) > 0, "Failed to load %s" % self.dictPath

    def getGroups(self):
        groupList = self._dApi.getCategoryGroups()
        return groupList

    def getTypeRegex(self, cat, attrib):
        return self._dApi.getTypeRegex(cat, attrib)

    def getTypeRegexAlt(self, cat, attrib):
        return self._dApi.getTypeRegexAlt(cat, attrib)

    def getDataTypeList(self):
        """ Return list of tuples containing ('code','primitive_code','construct','detail' )
        """
        return self._dApi.getDataTypeList()

    def getEnumList(self, category, attribute, sortFlag=True):
        return self._dApi.getEnumList(category, attribute, sortFlag)

    def getEnumListAlt(self, category, attribute, sortFlag=True):
        return self._dApi.getEnumListAlt(category, attribute, sortFlag)

    def getEnumListAltWithDetail(self, category, attribute):
        return self._dApi.getEnumListAltWithDetail(category, attribute)

    def getAllCategory(self):
        """Returns list of all categories"""
        return self._dApi.getCategoryList()

    def getCategoryList(self):
        return self._dApi.getCategoryList()

    def getItemNameList(self, category):
        return self._dApi.getItemNameList(category)

    def wwPDBContext(self, catItem):
        """Returns true if category or item have local context"""

        category, item = catItem.split('.')
        if category[0] == "_":
            category = category[1:]

        #print "CHECKING ", category, item, self.__dictA.getCategoryContextList(category)
        if self._dApi.getCategoryContextList(category) == ['WWPDB_LOCAL']:
            return True
        if self._dApi.getContextList(category, item) == ['WWPDB_LOCAL']:
            return True

        return False

    def getCategoryItemEnum(self, itemName):
        """Returns any archive enum list"""

        categoryName = CifName.categoryPart(itemName)
        attributeName = CifName.attributePart(itemName)
        return self._dApi.getEnumList(categoryName, attributeName)

    def getCategoryPdbxItemEnum(self, itemName):
        """Returns any DepUI enum list"""

        categoryName = CifName.categoryPart(itemName)
        attributeName = CifName.attributePart(itemName)
        return self._dApi.getEnumListAlt(categoryName, attributeName)

    def getItemRelatedList(self, itemName):
        categoryName = CifName.categoryPart(itemName)
        attributeName = CifName.attributePart(itemName)

        return self._dApi.getItemRelatedList(categoryName, attributeName)
    def test_markup_category_group(self, in_tmpdir, test_files, out_file_name,
                                   groupSelectList):
        oFile = out_file_name
        rL = []

        myIo = IoAdapter()
        containerList = myIo.readFile(
            str(test_files / "mmcif_pdbx_v5_next.dic"))
        dApi = DictionaryApi(containerList=containerList, consolidate=True)
        #
        groupList = dApi.getCategoryGroups()
        print('groupList %s\n' % groupList)
        for groupName in groupList:
            if groupSelectList and groupName not in groupSelectList:
                continue

            #
            # Goup header details
            #
            rL.append("# Category Group %s" % groupName)
            rL.append("")
            rL.append("")
            rL.append("%s" % dApi.getCategoryGroupDescription(groupName))
            rL.append("")
            rL.append("---")
            rL.append("")
            catNameList = dApi.getCategoryGroupCategories(groupName=groupName)
            #
            cList = self.__sortIgnoreCase(catNameList)
            for catName in cList:
                print('Group %s category %s\n' % (groupName, catName))
                catDescription = dApi.getCategoryDescription(category=catName)
                catExTupList = dApi.getCategoryExampleList(category=catName)
                keyItemNameList = dApi.getCategoryKeyList(category=catName)
                keyAttNameList = [
                    CifName.attributePart(k) for k in keyItemNameList
                ]
                #
                # Category header details
                #
                rL.append("## Category %s" % catName)
                rL.append("")
                rL.append("")
                rL.append(" %s" % catDescription)
                rL.append("")
                rL.append("---")
                rL.append("")
                if catExTupList:
                    rL.extend(
                        self.__formatTupListInset(catExTupList, tab='     '))
                    #
                    # summary table
                    #
                rL.append("")
                rL.append("---")
                rL.append("")
                rL.append(
                    "| Attribute | Key | Required | Type | Units | Enumerated | Bounded |"
                )
                rL.append(
                    "| --------- | --- | -------- | ---- | ----- | ---------- | ------- |"
                )
                aList = self.__sortIgnoreCase(
                    dApi.getAttributeNameList(category=catName))
                for attName in aList:
                    isKey = attName in keyAttNameList
                    attDescription = dApi.getDescription(category=catName,
                                                         attribute=attName)
                    attUnits = dApi.getUnits(category=catName,
                                             attribute=attName)
                    attMandatory = dApi.getMandatoryCode(category=catName,
                                                         attribute=attName)
                    attTypeCode = dApi.getTypeCode(category=catName,
                                                   attribute=attName)

                    enumTupList = dApi.getEnumListWithDetail(category=catName,
                                                             attribute=attName)
                    if len(enumTupList) > 0:
                        isEnum = True
                    else:
                        isEnum = False
                    bL = dApi.getBoundaryList(category=catName,
                                              attribute=attName)
                    if len(bL) > 0:
                        isBounded = True
                    else:
                        isBounded = False
                    rL.append(
                        '| %s | %s | %s | %s | %s | %s | %s |' %
                        (attName, self.__trB(isKey), attMandatory, attTypeCode,
                         attUnits, self.__trB(isEnum), self.__trB(isBounded)))
                #
                rL.append("")
                rL.append("---")
                rL.append("")
                #
                for attName in aList:
                    isKey = attName in keyAttNameList
                    attMandatory = dApi.getMandatoryCode(category=catName,
                                                         attribute=attName)
                    #
                    tN = '_' + catName + '.' + attName
                    if isKey:
                        tN = tN + ' (key)'
                    elif attMandatory.upper() in ['YES', 'Y']:
                        tN = tN + ' (required)'
                    #
                    rL.append("#### %s\n" % tN)
                    rL.append("")
                    attDescription = dApi.getDescription(category=catName,
                                                         attribute=attName)
                    rL.append(" %s\n" % attDescription)
                    rL.append("")
                    attUnits = dApi.getUnits(category=catName,
                                             attribute=attName)
                    attTypeCode = dApi.getTypeCode(category=catName,
                                                   attribute=attName)

                    enumTupList = dApi.getEnumListWithDetail(category=catName,
                                                             attribute=attName)
                    if len(enumTupList) > 0:
                        rL.append("")
                        rL.append("---")
                        rL.append("")
                        rL.append("| Allowed Values | Detail |")
                        rL.append("| -------------- | ------ |")
                        for tup in enumTupList:
                            if tup[1] and len(tup[1]) > 0:
                                rL.append("| %s | %s |" % (tup[0], tup[1]))
                            else:
                                rL.append("| %s | %s |" % (tup[0], ' '))
                        rL.append("")

                    #
                    bL = dApi.getBoundaryList(category=catName,
                                              attribute=attName)
                    btL = self.__processbounds(bL)
                    if len(btL) > 0:
                        tup = btL[0]
                        rL.append("")
                        rL.append("---")
                        rL.append("")
                        rL.append("| %s | %s |" % (tup[0], tup[1]))
                        #
                        rL.append("| ------------- | ------ |")
                        for tup in btL[1:]:
                            rL.append("| %s | %s |" % (tup[0], tup[1]))
                        rL.append("")
                    rL.append("")
        with open(str(oFile), 'w') as ofh:
            ofh.write('\n'.join(rL))
    def testClassifyByGroup(self):
        """Test case -  organize dictionary items by classes: SAMPLE, MX, NMR, EM, STRUCTURE, and DB"""
        try:
            myIo = IoAdapter(raiseExceptions=True)
            self.__containerList = myIo.readFile(
                inputFilePath=self.__pathPdbxDictionary)
            dApi = DictionaryApi(containerList=self.__containerList,
                                 consolidate=True,
                                 verbose=self.__verbose)
            #
            itemList = []
            groupList = dApi.getCategoryGroups()
            categoryList = dApi.getCategoryList()
            for category in categoryList:
                itemList.extend(dApi.getItemNameList(category))
            itemList = sorted(set(itemList))

            logger.info("Total category length %d", len(categoryList))
            logger.info("Total definition length %d", len(itemList))

            logger.info("group length %s", len(groupList))
            logger.debug("groupList %r", groupList)
            #
            findUnlinked = False
            if findUnlinked:
                tSet = set(["pdbx_group", "inclusive_group"])
                for category in categoryList:
                    gList = dApi.getCategoryGroupList(category)
                    gSet = set(gList)
                    if gSet == tSet:
                        logger.info("unqualified %s", category)

                    # logger.info("%s -> %r", category, gList)
                    if not gList:
                        logger.info("--- No category group assignment for %s",
                                    category)
            #
            classD = {}
            # Add category group members -
            for groupName, className in self.__groupClassTupL:
                categoryL = dApi.getCategoryGroupCategories(
                    groupName, followChildren=True)
                for category in categoryL:
                    classD.setdefault(className, []).extend(
                        dApi.getItemNameList(category))
            #
            # Add unlinked categories
            #
            for category, className in self.__unlinkedCategoryClassTup:
                classD.setdefault(className,
                                  []).extend(dApi.getItemNameList(category))
            #
            sumItem = 0
            classItemD = {}
            for className, itemL in classD.items():
                numItem = len(set(itemL))
                sumItem += numItem
                logger.info("class %s items %d", className, len(set(itemL)))
                for item in itemL:
                    classItemD[item] = True
            #
            logger.info("Sum classified items is %d", sumItem)
            logger.info("classified items %d", len(classItemD))
            #
            logger.debug("classItemD.items() %r",
                         list(classItemD.items())[:10])

            missingGroupL = []

            jj = 0
            for item in itemList:
                if item not in classItemD:
                    jj += 1
                    category = CifName.categoryPart(item)
                    logger.info("%d item %r category %r", jj, item, category)
                    missingGroupL.extend(dApi.getCategoryGroupList(category))
            #
            logger.info("missing groups %r", sorted(set(missingGroupL)))

        except Exception as e:
            logger.exception("Failing with %s", str(e))
            self.fail()
Exemple #6
0
    def __testMarkupCategoryGroup(self, oFile, groupSelectList=None):
        """Test case -  extract the content to be rendered -"""
        startTime = time.time()
        logger.debug("\nStarting at %s\n",
                     time.strftime("%Y %m %d %H:%M:%S", time.localtime()))
        try:
            rL = []

            myIo = IoAdapter()
            containerList = myIo.readFile(self.__pathPdbxV50Dictionary)
            dApi = DictionaryApi(containerList=containerList,
                                 consolidate=True,
                                 verbose=self.__verbose)
            #
            groupList = dApi.getCategoryGroups()
            logger.debug("groupList %s\n", groupList)
            for groupName in groupList:
                if groupSelectList and groupName not in groupSelectList:
                    continue

                #
                # Goup header details
                #
                rL.append("# Category Group %s" % groupName)
                rL.append("")
                rL.append("")
                rL.append("%s" % dApi.getCategoryGroupDescription(groupName))
                rL.append("")
                rL.append("---")
                rL.append("")
                catNameList = dApi.getCategoryGroupCategories(
                    groupName=groupName)
                #
                cList = self.__sortIgnoreCase(catNameList)
                for catName in cList:
                    logger.debug("Group %s category %s\n", groupName, catName)
                    catDescription = dApi.getCategoryDescription(
                        category=catName)
                    catExTupList = dApi.getCategoryExampleList(
                        category=catName)
                    keyItemNameList = dApi.getCategoryKeyList(category=catName)
                    keyAttNameList = [
                        CifName.attributePart(k) for k in keyItemNameList
                    ]
                    #
                    # Category header details
                    #
                    rL.append("## Category %s" % catName)
                    rL.append("")
                    rL.append("")
                    rL.append(" %s" % catDescription)
                    rL.append("")
                    rL.append("---")
                    rL.append("")
                    if catExTupList:
                        rL.extend(
                            self.__formatTupListInset(catExTupList,
                                                      tab="     "))
                        #
                        # summary table
                        #
                    rL.append("")
                    rL.append("---")
                    rL.append("")
                    rL.append(
                        "| Attribute | Key | Required | Type | Units | Enumerated | Bounded |"
                    )
                    rL.append(
                        "| --------- | --- | -------- | ---- | ----- | ---------- | ------- |"
                    )
                    aList = self.__sortIgnoreCase(
                        dApi.getAttributeNameList(category=catName))
                    for attName in aList:
                        isKey = attName in keyAttNameList
                        attDescription = dApi.getDescription(category=catName,
                                                             attribute=attName)
                        attUnits = dApi.getUnits(category=catName,
                                                 attribute=attName)
                        attMandatory = dApi.getMandatoryCode(category=catName,
                                                             attribute=attName)
                        attTypeCode = dApi.getTypeCode(category=catName,
                                                       attribute=attName)

                        enumTupList = dApi.getEnumListWithDetail(
                            category=catName, attribute=attName)
                        if enumTupList:
                            isEnum = True
                        else:
                            isEnum = False
                        bL = dApi.getBoundaryList(category=catName,
                                                  attribute=attName)
                        if bL:
                            isBounded = True
                        else:
                            isBounded = False
                        rL.append("| %s | %s | %s | %s | %s | %s | %s |" %
                                  (attName, self.__trB(isKey), attMandatory,
                                   attTypeCode, attUnits, self.__trB(isEnum),
                                   self.__trB(isBounded)))
                    #
                    rL.append("")
                    rL.append("---")
                    rL.append("")
                    #
                    for attName in aList:
                        isKey = attName in keyAttNameList
                        attMandatory = dApi.getMandatoryCode(category=catName,
                                                             attribute=attName)
                        #
                        tN = "_" + catName + "." + attName
                        if isKey:
                            tN = tN + " (key)"
                        elif attMandatory.upper() in ["YES", "Y"]:
                            tN = tN + " (required)"
                        #
                        rL.append("#### %s\n" % tN)
                        rL.append("")
                        attDescription = dApi.getDescription(category=catName,
                                                             attribute=attName)
                        rL.append(" %s\n" % attDescription)
                        rL.append("")
                        attUnits = dApi.getUnits(category=catName,
                                                 attribute=attName)
                        attTypeCode = dApi.getTypeCode(category=catName,
                                                       attribute=attName)

                        enumTupList = dApi.getEnumListWithDetail(
                            category=catName, attribute=attName)
                        if enumTupList:
                            rL.append("")
                            rL.append("---")
                            rL.append("")
                            rL.append("| Allowed Values | Detail |")
                            rL.append("| -------------- | ------ |")
                            for tup in enumTupList:
                                if tup[1]:
                                    rL.append("| %s | %s |" % (tup[0], tup[1]))
                                else:
                                    rL.append("| %s | %s |" % (tup[0], " "))
                            rL.append("")

                        #
                        bL = dApi.getBoundaryList(category=catName,
                                                  attribute=attName)
                        btL = self.__processbounds(bL)
                        if btL:
                            tup = btL[0]
                            rL.append("")
                            rL.append("---")
                            rL.append("")
                            rL.append("| %s | %s |" % (tup[0], tup[1]))
                            #
                            rL.append("| ------------- | ------ |")
                            for tup in btL[1:]:
                                rL.append("| %s | %s |" % (tup[0], tup[1]))
                            rL.append("")
                        rL.append("")
            with open(oFile, "w") as ofh:
                ofh.write("\n".join(rL))

        except Exception as e:
            logger.exception("Failing with %s", str(e))
            self.fail()

        endTime = time.time()
        logger.debug("\nCompleted at %s (%.2f seconds)",
                     time.strftime("%Y %m %d %H:%M:%S", time.localtime()),
                     endTime - startTime)