Esempio n. 1
0
 def __getCategoryKeysWithReplacement(self, categoryName):
     if categoryName in self.__keyReplaceCategoryD:
         keyItems = [
             CifName.itemName(categoryName, atName)
             for atName in self.__keyReplaceCategoryD[categoryName]
         ]
     else:
         keyItems = self.__dApi.getCategoryKeyList(categoryName)
     return sorted(keyItems)
Esempio n. 2
0
 def __substituteItemPrefix(self, itemName, curPrefix, newPrefix):
     atName = CifName.attributePart(itemName)
     atName = atName.replace(
         curPrefix, newPrefix,
         1) if atName and atName.startswith(curPrefix) else atName
     catName = CifName.categoryPart(itemName)
     catName = catName.replace(
         curPrefix, newPrefix,
         1) if atName and catName.startswith(curPrefix) else catName
     return CifName.itemName(catName, atName)
Esempio n. 3
0
    def _makeKeyItem(self, catName, attName, keyItemList, iDef):
        itemName = CifName.itemName(catName, attName)

        #
        dc = DataCategory("definition")
        dc.appendAttribute("id")
        dc.appendAttribute("scope")
        dc.appendAttribute("class")
        dc.appendAttribute("update")
        dc.append([itemName, "Item", "Single", "2013-08-22"])
        iDef.append(dc)
        #
        dc = DataCategory("description")
        dc.appendAttribute("text")
        dc.append(['synthentic componsite key'])
        iDef.append(dc)
        #
        dc = DataCategory("name")
        dc.appendAttribute("category_id")
        dc.appendAttribute("object_id")
        dc.append([catName, attName])
        iDef.append(dc)
        tcontainer = 'Set'
        purpose = 'Composite'
        source = 'Derived'
        contents = 'Name'
        dimension = '[%d]' % len(keyItemList)
        #

        dc = DataCategory("type")
        dc.appendAttribute("purpose")
        dc.appendAttribute("source")
        dc.appendAttribute("contents")
        dc.appendAttribute("container")
        dc.appendAttribute("dimension")
        dc.append([purpose, source, contents, tcontainer, dimension])
        iDef.append(dc)

        dc = DataCategory("method")
        dc.appendAttribute("purpose")
        dc.appendAttribute("expression")

        tmpl = '''

                      With row as %s

                           %s = [%s]

        '''
        mText = tmpl % (catName, itemName, ','.join(keyItemList))
        dc.append(['Evaluation', mText])
        iDef.append(dc)
Esempio n. 4
0
    def __makeKeyItem(self, catName, attName, keyItemList, iDef):
        itemName = CifName.itemName(catName, attName)

        #
        dc = DataCategory("definition")
        dc.appendAttribute("id")
        dc.appendAttribute("scope")
        dc.appendAttribute("class")
        dc.appendAttribute("update")
        dc.append([itemName, "Item", "Single", "2013-08-22"])
        iDef.append(dc)
        #
        dc = DataCategory("description")
        dc.appendAttribute("text")
        dc.append(["synthentic componsite key"])
        iDef.append(dc)
        #
        dc = DataCategory("name")
        dc.appendAttribute("category_id")
        dc.appendAttribute("object_id")
        dc.append([catName, attName])
        iDef.append(dc)
        tcontainer = "Set"
        purpose = "Composite"
        source = "Derived"
        contents = "Name"
        dimension = "[%d]" % len(keyItemList)
        #

        dc = DataCategory("type")
        dc.appendAttribute("purpose")
        dc.appendAttribute("source")
        dc.appendAttribute("contents")
        dc.appendAttribute("container")
        dc.appendAttribute("dimension")
        dc.append([purpose, source, contents, tcontainer, dimension])
        iDef.append(dc)

        dc = DataCategory("method")
        dc.appendAttribute("purpose")
        dc.appendAttribute("expression")

        tmpl = """

                      With row as %s

                           %s = [%s]

        """
        mText = tmpl % (catName, itemName, ",".join(keyItemList))
        dc.append(["Evaluation", mText])
        iDef.append(dc)
Esempio n. 5
0
    def __buildCategoryDefinition(self, name, description, keyAttributeNames,
                                  examples, contexts):
        """Construct an attribute definition from input attribute dictionary
            containing metadata extracted from the XML schema, and from the
            input schema name mapping dictionary.

        Args:
            name (str): category name
            description (str): category description
            keyAttributeNames (list): key attribute names
            examples (list): category examples
            contexts (list): category contexts

        Returns:
            Definition container (object):

        """
        defC = DefinitionContainer(name)
        #
        dc = DataCategory(
            "category",
            attributeNameList=["id", "description", "mandatory_code"])
        dc.append([name, description, "no"])
        defC.append(dc)
        #
        dc = DataCategory("category_key", attributeNameList=["name"])
        for keyAttributeName in keyAttributeNames:
            keyItemName = CifName.itemName(name, keyAttributeName)
            dc.append([keyItemName])
        defC.append(dc)

        dc = DataCategory("category_group", attributeNameList=["id"])
        dc.append(["inclusive_group"])
        dc.append(["validation_report_group"])
        defC.append(dc)
        # pdbx_category_context
        dc = DataCategory("pdbx_category_context",
                          attributeNameList=["category_id", "type"])
        for cType in contexts:
            dc.append([name, cType])
        defC.append(dc)
        #
        dc = DataCategory("category_examples",
                          attributeNameList=["detail", "case"])
        for example in examples:
            dc.append([".", example])
        defC.append(dc)

        return defC
Esempio n. 6
0
    def test_gen_ddlm(self, in_tmpdir, test_files):
        myIo = IoAdapterPy()
        containerList = myIo.readFile(
            inputFilePath=str(test_files / 'mmcif_pdbx_v5_next.dic'))
        dApi = DictionaryApi(containerList=containerList, consolidate=True)
        parentD = dApi.getParentDictionary()
        #
        oCList = []
        dDef = DataContainer('mmcif_pdbx_ddlm_auto')
        dc = DataCategory("dictionary")
        dc.appendAttribute("title")
        dc.appendAttribute("class")
        dc.appendAttribute("version")
        dc.appendAttribute("date")
        dc.appendAttribute("ddl_conformance")
        dc.appendAttribute("text")
        dc.append([
            'mmcif_pdbx_ddlm_auto', 'Instance', 'latest', '2018-03-09',
            'ddlm best effort',
            'Software converted PDBx dictionary using DDLm semantics'
        ])
        dDef.append(dc)
        oCList.append(dDef)

        catIdx = dApi.getCategoryIndex()
        for catName in sorted(catIdx.keys()):
            attNameList = catIdx[catName]
            # created definition container -
            cDef = DefinitionContainer(catName)
            oCList.append(cDef)
            #
            dc = DataCategory("definition")
            dc.appendAttribute("id")
            dc.appendAttribute("scope")
            dc.appendAttribute("class")
            dc.appendAttribute("update")
            dc.append([catName, "Category", "Loop", "2018-03-09"])
            cDef.append(dc)
            val = dApi.getCategoryDescription(category=catName)
            dc = DataCategory("description")
            dc.appendAttribute("text")
            dc.append([val])
            cDef.append(dc)
            #
            dc = DataCategory("name")
            dc.appendAttribute("category_id")
            dc.appendAttribute("object_id")

            valList = dApi.getCategoryGroupList(category=catName)
            pcg = catName
            for val in valList:
                if val != 'inclusive_group':
                    pcg = val
                    break
            dc.append([catName, pcg])
            cDef.append(dc)

            valList = dApi.getCategoryKeyList(category=catName)
            if len(valList) < 1:
                print("Missing caegory key for category %s\n" % catName)
            else:
                dc = DataCategory("category")
                dc.appendAttribute("key_id")
                kItemName = CifName.itemName(catName, "synthetic_key")
                dc.append([kItemName])
                cDef.append(dc)

                iDef = DefinitionContainer(kItemName)
                self._makeKeyItem(catName, "synthetic_key", valList, iDef)
                oCList.append(iDef)

            for attName in attNameList:
                itemName = CifName.itemName(catName, attName)
                iDef = DefinitionContainer(itemName)

                oCList.append(iDef)

                #
                dc = DataCategory("definition")
                dc.appendAttribute("id")
                dc.appendAttribute("scope")
                dc.appendAttribute("class")
                dc.appendAttribute("update")
                dc.append([itemName, "Item", "Single", "2013-08-22"])
                iDef.append(dc)
                #
                val = dApi.getDescription(category=catName, attribute=attName)
                dc = DataCategory("description")
                dc.appendAttribute("text")
                dc.append([val])
                iDef.append(dc)
                #
                dc = DataCategory("name")
                dc.appendAttribute("category_id")
                dc.appendAttribute("object_id")
                #
                if itemName in parentD:
                    dc.appendAttribute("linked_item_id")
                    dc.append([catName, attName, parentD[itemName][0]])
                else:
                    dc.append([catName, attName])
                iDef.append(dc)
                #
                #
                aliasList = dApi.getItemAliasList(category=catName,
                                                  attribute=attName)
                if len(aliasList) > 0:
                    dc = DataCategory("alias")
                    dc.appendAttribute("definition_id")
                    for alias in aliasList:
                        dc.append([alias[0]])
                    iDef.append(dc)

                enList = dApi.getEnumListAltWithDetail(category=catName,
                                                       attribute=attName)

                tC = dApi.getTypeCode(category=catName, attribute=attName)
                tcontainer = 'Single'
                purpose = 'Describe'
                source = 'Recorded'
                contents = 'Text'
                #
                if tC is None:
                    self.__lfh.write("Missing data type attribute %s\n" %
                                     attName)
                elif tC in [
                        'code', 'atcode', 'name', 'idname', 'symop', 'fax',
                        'phone', 'email', 'code30', 'ec-type'
                ]:
                    purpose = 'Encode'
                    contents = 'Text'
                    source = 'Assigned'
                elif tC in ['ucode']:
                    purpose = 'Encode'
                    contents = 'Code'
                    source = 'Assigned'
                elif tC in ['line', 'uline', 'text']:
                    purpose = 'Describe'
                    source = 'Recorded'
                    contents = 'Text'
                elif tC in ['int']:
                    purpose = 'Number'
                    source = 'Recorded'
                    contents = 'Integer'
                elif tC in ['int-range']:
                    purpose = 'Number'
                    source = 'Recorded'
                    contents = 'Range'
                elif tC in ['float']:
                    purpose = 'Measurand'
                    source = 'Recorded'
                    contents = 'Real'
                elif tC in ['float-range']:
                    purpose = 'Measurand'
                    source = 'Recorded'
                    contents = 'Range'
                elif tC.startswith('yyyy'):
                    source = 'Assigned'
                    contents = 'Date'
                    purpose = 'Describe'

                if len(enList) > 0:
                    purpose = 'State'

                dc = DataCategory("type")
                dc.appendAttribute("purpose")
                dc.appendAttribute("source")
                dc.appendAttribute("contents")
                dc.appendAttribute("container")
                dc.append([purpose, source, contents, tcontainer])
                iDef.append(dc)
                #
                if (len(enList) > 0):
                    dc = DataCategory("enumeration_set")
                    dc.appendAttribute("state")
                    dc.appendAttribute("detail")
                    for en in enList:
                        dc.append([en[0], en[1]])
                    iDef.append(dc)

                dfv = dApi.getDefaultValue(category=catName, attribute=attName)
                bvList = dApi.getBoundaryList(category=catName,
                                              attribute=attName)
                if (((dfv is not None) and (dfv not in ['?', '.']))
                        or len(bvList) > 0):
                    row = []
                    dc = DataCategory("enumeration")
                    if dfv is not None:
                        dc.appendAttribute("default")
                        row.append(dfv)
                    if len(bvList) > 0:
                        dc.appendAttribute("range")
                        mminVp = -1000000
                        mmaxVp = 10000000
                        mminV = mmaxVp
                        mmaxV = mminVp
                        for bv in bvList:
                            minV = float(bv[0]) if bv[0] != '.' else mminVp
                            maxV = float(bv[1]) if bv[1] != '.' else mmaxVp
                            mminV = min(mminV, minV)
                            mmaxV = max(mmaxV, maxV)
                        if mminV == mminVp:
                            mminV = ''
                        if mmaxV == mmaxVp:
                            mmaxV = ''
                        row.append(str(mminV) + ":" + str(mmaxV))

                    dc.append(row)
                    iDef.append(dc)

        myIo.writeFile(outputFilePath="mmcif_pdbx_ddlm_auto.dic",
                       containerList=oCList)
Esempio n. 7
0
    def testGenDDLm(self):
        """Generating alternative DDLm metadata format. (starting point)"""
        try:
            myIo = IoAdapterPy(self.__verbose, self.__lfh)
            self.__containerList = myIo.readFile(
                inputFilePath=self.__pathPdbxDictionary)
            dApi = DictionaryApi(containerList=self.__containerList,
                                 consolidate=True,
                                 verbose=self.__verbose)
            parentD = dApi.getParentDictionary()
            #
            oCList = []
            dDef = DataContainer("mmcif_pdbx_ddlm_auto")
            dc = DataCategory("dictionary")
            dc.appendAttribute("title")
            dc.appendAttribute("class")
            dc.appendAttribute("version")
            dc.appendAttribute("date")
            dc.appendAttribute("ddl_conformance")
            dc.appendAttribute("text")
            dc.append([
                "mmcif_pdbx_ddlm_auto", "Instance", "latest", "2018-03-09",
                "ddlm best effort",
                "Software converted PDBx dictionary using DDLm semantics"
            ])
            dDef.append(dc)
            oCList.append(dDef)

            catIdx = dApi.getCategoryIndex()
            for catName in sorted(catIdx.keys()):
                attNameList = catIdx[catName]
                # created definition container -
                cDef = DefinitionContainer(catName)
                oCList.append(cDef)
                #
                dc = DataCategory("definition")
                dc.appendAttribute("id")
                dc.appendAttribute("scope")
                dc.appendAttribute("class")
                dc.appendAttribute("update")
                dc.append([catName, "Category", "Loop", "2018-03-09"])
                cDef.append(dc)
                val = dApi.getCategoryDescription(category=catName)
                dc = DataCategory("description")
                dc.appendAttribute("text")
                dc.append([val])
                cDef.append(dc)
                #
                dc = DataCategory("name")
                dc.appendAttribute("category_id")
                dc.appendAttribute("object_id")

                valList = dApi.getCategoryGroupList(category=catName)
                pcg = catName
                for val in valList:
                    if val != "inclusive_group":
                        pcg = val
                        break
                dc.append([catName, pcg])
                cDef.append(dc)

                valList = dApi.getCategoryKeyList(category=catName)
                if not valList:
                    self.__lfh.write("Missing caegory key for category %s\n" %
                                     catName)
                else:
                    dc = DataCategory("category")
                    dc.appendAttribute("key_id")
                    kItemName = CifName.itemName(catName, "synthetic_key")
                    dc.append([kItemName])
                    cDef.append(dc)

                    iDef = DefinitionContainer(kItemName)
                    self.__makeKeyItem(catName, "synthetic_key", valList, iDef)
                    oCList.append(iDef)

                for attName in attNameList:
                    itemName = CifName.itemName(catName, attName)
                    iDef = DefinitionContainer(itemName)

                    oCList.append(iDef)

                    #
                    dc = DataCategory("definition")
                    dc.appendAttribute("id")
                    dc.appendAttribute("scope")
                    dc.appendAttribute("class")
                    dc.appendAttribute("update")
                    dc.append([itemName, "Item", "Single", "2013-08-22"])
                    iDef.append(dc)
                    #
                    val = dApi.getDescription(category=catName,
                                              attribute=attName)
                    dc = DataCategory("description")
                    dc.appendAttribute("text")
                    dc.append([val])
                    iDef.append(dc)
                    #
                    dc = DataCategory("name")
                    dc.appendAttribute("category_id")
                    dc.appendAttribute("object_id")
                    #
                    if itemName in parentD:
                        dc.appendAttribute("linked_item_id")
                        dc.append([catName, attName, parentD[itemName][0]])
                    else:
                        dc.append([catName, attName])
                    iDef.append(dc)
                    #
                    #
                    aliasList = dApi.getItemAliasList(category=catName,
                                                      attribute=attName)
                    if aliasList:
                        dc = DataCategory("alias")
                        dc.appendAttribute("definition_id")
                        for alias in aliasList:
                            dc.append([alias[0]])
                        iDef.append(dc)

                    enList = dApi.getEnumListAltWithDetail(category=catName,
                                                           attribute=attName)

                    tC = dApi.getTypeCode(category=catName, attribute=attName)
                    tcontainer = "Single"
                    purpose = "Describe"
                    source = "Recorded"
                    contents = "Text"
                    #
                    if tC is None:
                        self.__lfh.write("Missing data type attribute %s\n" %
                                         attName)
                    elif tC in [
                            "code", "atcode", "name", "idname", "symop", "fax",
                            "phone", "email", "code30", "ec-type"
                    ]:
                        purpose = "Encode"
                        contents = "Text"
                        source = "Assigned"
                    elif tC in ["ucode"]:
                        purpose = "Encode"
                        contents = "Code"
                        source = "Assigned"
                    elif tC in ["line", "uline", "text"]:
                        purpose = "Describe"
                        source = "Recorded"
                        contents = "Text"
                    elif tC in ["int"]:
                        purpose = "Number"
                        source = "Recorded"
                        contents = "Integer"
                    elif tC in ["int-range"]:
                        purpose = "Number"
                        source = "Recorded"
                        contents = "Range"
                    elif tC in ["float"]:
                        purpose = "Measurand"
                        source = "Recorded"
                        contents = "Real"
                    elif tC in ["float-range"]:
                        purpose = "Measurand"
                        source = "Recorded"
                        contents = "Range"
                    elif tC.startswith("yyyy"):
                        source = "Assigned"
                        contents = "Date"
                        purpose = "Describe"

                    if enList:
                        purpose = "State"

                    dc = DataCategory("type")
                    dc.appendAttribute("purpose")
                    dc.appendAttribute("source")
                    dc.appendAttribute("contents")
                    dc.appendAttribute("container")
                    dc.append([purpose, source, contents, tcontainer])
                    iDef.append(dc)
                    #
                    if enList:
                        dc = DataCategory("enumeration_set")
                        dc.appendAttribute("state")
                        dc.appendAttribute("detail")
                        for en in enList:
                            dc.append([en[0], en[1]])
                        iDef.append(dc)

                    dfv = dApi.getDefaultValue(category=catName,
                                               attribute=attName)
                    bvList = dApi.getBoundaryList(category=catName,
                                                  attribute=attName)
                    if ((dfv is not None) and
                        (dfv not in ["?", "."])) or bvList:
                        row = []
                        dc = DataCategory("enumeration")
                        if dfv is not None:
                            dc.appendAttribute("default")
                            row.append(dfv)
                        if bvList:
                            dc.appendAttribute("range")
                            mminVp = -1000000
                            mmaxVp = 10000000
                            mminV = mmaxVp
                            mmaxV = mminVp
                            for bv in bvList:
                                minV = float(bv[0]) if bv[0] != "." else mminVp
                                maxV = float(bv[1]) if bv[1] != "." else mmaxVp
                                mminV = min(mminV, minV)
                                mmaxV = max(mmaxV, maxV)
                            if mminV == mminVp:
                                mminV = ""
                            if mmaxV == mmaxVp:
                                mmaxV = ""
                            row.append(str(mminV) + ":" + str(mmaxV))

                        dc.append(row)
                        iDef.append(dc)

            myIo.writeFile(outputFilePath=os.path.join(
                HERE, "test-output", "mmcif_pdbx_ddlm_auto.dic"),
                           containerList=oCList)

        except Exception as e:
            logger.exception("Failing with %s", str(e))
            self.fail()
Esempio n. 8
0
    def __getAttributeFeatures(self, catName, iterableD, embeddedIterableD,
                               itemTransformD, methodD):
        """
        Args:
            catName (string): Category name
            iterableD (tuple, optional): iterable dictionary type codes
            iQueryStrings (list, optional): search strings applied to item descriptions to identify iterable candidates
            itemTransformD (dict): dictionary of data transform filters  itd[(catName,atName)] = [f1,f2,...]

        Returns:
            dict: attribute features


             cL = self.getCategoryContextList(catName)
        """
        aD = {}

        #
        # keyAtNames = [CifName.attributePart(kyItem) for kyItem in self.__dApi.getCategoryKeyList(catName)]
        keyAtNames = [
            CifName.attributePart(kyItem)
            for kyItem in self.__getCategoryKeysWithReplacement(catName)
        ]
        for atName in self.__categorySchema[catName]:
            itemName = CifName.itemName(catName, atName)
            fD = {
                "CATEGORY_NAME": catName,
                "ATTRIBUTE_NAME": atName,
                "TYPE_CODE": None,
                "TYPE_CODE_ALT": None,
                "IS_MANDATORY": False,
                "CHILD_ITEMS": [],
                "CHILDREN": [],
                "ROOT_PARENT_ITEM": None,
                "ROOT_PARENT": None,
                "PARENT": None,
                "DESCRIPTION": None,
                "DESCRIPTION_ANNOTATED": [],
                "IS_KEY": False,
                "ITERABLE_DELIMITER": None,
                "EMBEDDED_ITERABLE_DELIMITER": None,
                "FILTER_TYPES": [],
                "IS_CHAR_TYPE": False,
                "METHODS": [],
                "CONTENT_CLASSES": [],
                "UNITS": None,
                "ENUMS": None,
                "ENUMS_ANNOTATED": None,
                "SEARCH_CONTEXTS": None,
            }
            fD["TYPE_CODE"] = self.__dApi.getTypeCode(catName, atName)
            fD["TYPE_CODE_ALT"] = self.__dApi.getTypeCodeAlt(catName, atName)
            fD["IS_MANDATORY"] = True if str(
                self.__dApi.getMandatoryCode(
                    catName, atName)).lower() in ["y", "yes"] else False
            fD["DESCRIPTION"] = textwrap.dedent(
                self.__dApi.getDescription(catName, atName)).lstrip().rstrip()
            #
            fD["DESCRIPTION_ANNOTATED"] = [{
                "text": fD["DESCRIPTION"],
                "context": "dictionary"
            }]
            tS = self.__dApi.getDescriptionPdbx(catName, atName)
            if tS:
                fD["DESCRIPTION_ANNOTATED"].append({
                    "text":
                    textwrap.dedent(tS).lstrip().rstrip(),
                    "context":
                    "deposition"
                })
            #
            fD["UNITS"] = self.__dApi.getUnits(catName, atName)

            #
            fD["CHILD_ITEMS"] = self.__dApi.getFullChildList(catName, atName)
            fD["CHILDREN"] = self.__itemNameToDictList(
                self.__dApi.getFullChildList(catName, atName))
            #
            pItemName = self.__dApi.getUltimateParent(catName, atName)
            pName = pItemName if pItemName != itemName else None
            fD["ROOT_PARENT_ITEM"] = pName

            fD["ROOT_PARENT"] = self.__itemNameToDictList(
                [pName])[0] if pName else None
            #
            pL = self.__dApi.getFullParentList(catName,
                                               atName,
                                               stripSelfParent=True)
            if pL:
                rL = self.__itemNameToDictList(pL)
                fD["PARENT"] = rL[0] if rL else None
                if len(rL) > 1:
                    logger.warning(
                        "Unexpected multiple parent definition for %s %s : %r",
                        catName, atName, rL)
            #
            # logger.debug("catName %s atName %s : parent %r root_parent %r", catName, atName, fD['PARENT'], fD['ROOT_PARENT'])
            #
            fD["IS_KEY"] = atName in keyAtNames
            pType = self.__dApi.getTypePrimitive(catName, atName)
            fD["IS_CHAR_TYPE"] = str(pType).lower() in ["char", "uchar"]
            #
            fD["ITERABLE_DELIMITER"] = iterableD[(catName, atName)] if (
                catName, atName) in iterableD else None
            fD["EMBEDDED_ITERABLE_DELIMITER"] = embeddedIterableD[(
                catName, atName)] if (catName,
                                      atName) in embeddedIterableD else None
            #
            fD["FILTER_TYPES"] = itemTransformD[(catName, "__all__")] if (
                catName, "__all__") in itemTransformD else []
            fD["FILTER_TYPES"] = itemTransformD[(catName, atName)] if (
                catName, atName) in itemTransformD else fD["FILTER_TYPES"]
            #
            fD["METHODS"] = methodD[(catName,
                                     atName)] if (catName,
                                                  atName) in methodD else []
            fD["CONTENT_CLASSES"] = self.__getContentClasses(catName, atName)
            if (catName, atName) in self.__intEnumD:
                fD["ENUMS"] = sorted(
                    self.__assignEnumTypes(
                        self.__dApi.getEnumListPdbx(catName, atName), pType))
                logger.debug("Using internal enums for %s %s %d", catName,
                             atName, len(fD["ENUMS"]))
                enumTupList = self.__dApi.getEnumListAltWithFullDetails(
                    catName, atName)
            else:
                fD["ENUMS"] = sorted(
                    self.__assignEnumTypes(
                        self.__dApi.getEnumList(catName, atName), pType))
                enumTupList = self.__dApi.getEnumListWithFullDetails(
                    catName, atName)
            #
            if self.__hasEnumDetails(enumTupList):
                #
                fD["ENUMS_ANNOTATED"] = []
                for eTup in self.__assignEnumTupTypes(enumTupList, pType):
                    teD = {"value": eTup[0]}
                    if eTup[1]:
                        teD["detail"] = eTup[1]
                    if eTup[2]:
                        teD["name"] = eTup[2]
                    if eTup[3]:
                        teD["units"] = eTup[3]
                    fD["ENUMS_ANNOTATED"].append(teD)
            # -----
            fD["EXAMPLES"] = self.__assignExampleTupTypes(
                catName, atName,
                self.__dApi.getExampleListPdbx(catName, atName), pType)
            fD["EXAMPLES"].extend(
                self.__assignExampleTupTypes(
                    catName, atName,
                    self.__dApi.getExampleList(catName, atName), pType))
            # -----
            scL = []
            for scTup in self.__dApi.getItemSubCategoryList(catName, atName):
                if scTup[1] is not None:
                    qD = {"id": scTup[0], "label": scTup[1]}
                else:
                    qD = {"id": scTup[0]}
                scL.append(qD)
            fD["SUB_CATEGORIES"] = scL
            if len(scL) > 1:
                logger.debug("Multiple subcategories for %r %r %r", catName,
                             atName, scL)
            #
            # bList = self.__dApi.getBoundaryListAlt(catName, atName, fallBack=True)
            bdList = self.__dApi.getBoundaryList(catName, atName)
            if bdList:
                minD = {}
                maxD = {}
                for (minV, maxV) in bdList:
                    if minV == maxV:
                        continue
                    if minV not in [".", "?"]:
                        minD[minV] = False
                    if maxV not in [".", "?"]:
                        maxD[maxV] = False
                for (minV, maxV) in bdList:
                    if minV == maxV and minV in minD:
                        minD[minV] = True
                    if minV == maxV and maxV in maxD:
                        maxD[maxV] = True
                for ky in minD:
                    if "." in ky:
                        kyV = float(ky)
                    else:
                        kyV = int(ky)
                    if minD[ky]:
                        fD["MIN_VALUE"] = kyV
                    else:
                        fD["MIN_VALUE_EXCLUSIVE"] = kyV
                for ky in maxD:
                    if "." in ky:
                        kyV = float(ky)
                    else:
                        kyV = int(ky)
                    if maxD[ky]:
                        fD["MAX_VALUE"] = kyV
                    else:
                        fD["MAX_VALUE_EXCLUSIVE"] = kyV
            #
            aD[atName] = fD
        #
        return aD
Esempio n. 9
0
    def __buildAttributeDefinition(self, atD, mD):
        """ Construct an attribute definition from input attribute dictionary
            containing metadata extracted from the XML schema, and from the
            input schema name mapping dictionary.
        Args:
            atD (dict): attribute metadata dictionary
            dictionaryMap (dict): mapping details for categories and attributes

        Returns:
                Attribute definition (object)

        """
        #
        atName = atD["name"]
        catName = atD["category"]

        #
        mapAtName = mD["at"] if "at" in mD else atName
        mapCatName = mD["cat"] if "cat" in mD else catName
        pCat = mD["pCat"] if "pCat" in mD else None
        pAt = mD["pAt"] if "pAt" in mD else None
        pType = mD["pType"] if "pType" in mD else None
        #
        itemName = CifName.itemName(mapCatName, mapAtName)
        #
        aliasAtName = atD["aliasName"] if "aliasName" in atD else None
        aliasCatName = atD[
            "aliasCategoryName"] if "aliasCategoryName" in atD else None
        #
        atDescription = atD["description"] if "description" in atD else None
        atDescription = self.__filterDescription(atDescription,
                                                 self.__catMappingD)
        atDescription = self.__filterDescription(atDescription,
                                                 self.__atMappingD)
        #
        mCode = "yes" if atD["mandatory"] == "mandatory" else "no"
        mCode = "yes" if mapAtName == "entry_id" else mCode
        #
        if atD["type"] not in self.__typeMap:
            logger.info("Unmapped type %r", atD["type"])
        #
        atType = self.__typeMap[
            atD["type"]] if atD["type"] in self.__typeMap else "UNKNOWN"
        #
        if atType == "text" and atDescription.find("comma separate") >= 0:
            atType = "alphanum-csv"
        if atType == "text" and "_date" in mapAtName and mapAtName != "report_creation_date":
            atType = "yyyy-mm-dd"
        #
        atType = pType if pType else atType

        if atType == "UNKNOWN":
            logger.info("Missing type mapping for %s %s %s", catName, atName,
                        atD["type"])
        #
        defA = DefinitionContainer(itemName)
        #
        dc = DataCategory("item_description",
                          attributeNameList=["description"])
        dc.append([atDescription])
        defA.append(dc)

        dc = DataCategory(
            "item",
            attributeNameList=["name", "category_id", "mandatory_code"])
        dc.append([itemName, mapCatName, mCode])
        defA.append(dc)
        #
        dc = DataCategory("item_type", attributeNameList=["code"])
        dc.append([atType])
        defA.append(dc)

        dc = DataCategory(
            "item_aliases",
            attributeNameList=["alias_name", "dictionary", "version"])
        dc.append([
            aliasCatName + "." + aliasAtName, self.__schemaPath,
            self.__schemaVersion
        ])
        defA.append(dc)
        #
        # Note - expect boundaries in pairs and 'inclusive' of endpoints
        #
        if "minIncl" in atD and "maxIncl" in atD and atD["minIncl"] and atD[
                "maxIncl"]:
            minB = atD["minIncl"]
            maxB = atD["maxIncl"]
            dc = DataCategory("item_range",
                              attributeNameList=["minimum", "maximum"])
            dc.append([minB, minB])
            dc.append([minB, maxB])
            dc.append([maxB, maxB])
            defA.append(dc)
        else:
            if atType == "float" and ((mapAtName.find("percent_") >= 0) or
                                      (mapAtName.find("percentile_") >= 0)):
                dc = DataCategory("item_range",
                                  attributeNameList=["minimum", "maximum"])
                minB = "0.0"
                maxB = "100.0"
                dc.append([minB, minB])
                dc.append([minB, maxB])
                dc.append([maxB, maxB])
                defA.append(dc)
        #
        if "enum" in atD and isinstance(atD["enum"], list) and atD["enum"]:
            dc = DataCategory("item_enumeration",
                              attributeNameList=["value", "detail"])
            for enumVal in atD["enum"]:
                dc.append([enumVal, "."])
            defA.append(dc)

        # -  add parent link relationships -
        if pCat and pAt:
            dc = DataCategory("item_linked",
                              attributeNameList=["child_name", "parent_name"])
            parentItemName = CifName.itemName(pCat, pAt)
            dc.append([itemName, parentItemName])
            defA.append(dc)
        #
        #
        return defA