Пример #1
0
    def testSimpleInitialization(self):
        """Test case -  Simple initialization of a data category and data block
        """
        self.lfh.write(
            "\nStarting %s %s\n" %
            (self.__class__.__name__, sys._getframe().f_code.co_name))
        try:
            #
            fn = tempname(suffix='.cif')
            attributeNameList = [
                'aOne', 'aTwo', 'aThree', 'aFour', 'aFive', 'aSix', 'aSeven',
                'aEight', 'aNine', 'aTen'
            ]
            rowList = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                       [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                       [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                       [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                       [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                       [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                       [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                       [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                       [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                       [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
            nameCat = 'myCategory'
            #
            #
            curContainer = DataContainer("myblock")
            aCat = DataCategory(nameCat, attributeNameList, rowList)
            aCat.printIt()
            curContainer.append(aCat)
            curContainer.printIt()
            #
            myContainerList = []
            myContainerList.append(curContainer)
            ofh = open(fn, "w")
            pdbxW = PdbxWriter(ofh)
            pdbxW.write(myContainerList)
            ofh.close()

            myContainerList = []
            ifh = open(fn, "r")
            pRd = PdbxReader(ifh)
            pRd.read(myContainerList)
            ifh.close()
            for container in myContainerList:
                for objName in container.getObjNameList():
                    name, aList, rList = container.getObj(objName).get()
                    self.lfh.write("Recovered data category  %s\n" % name)
                    self.lfh.write("Attribute list           %r\n" %
                                   repr(aList))
                    self.lfh.write("Row list                 %r\n" %
                                   repr(rList))
        except:
            traceback.print_exc(file=self.lfh)
            self.fail()
Пример #2
0
    def __parser(self, tokenizer, containerList):
        """ Parser for PDBx data files and dictionaries.

            Input - tokenizer() reentrant method recognizing data item names (_category.attribute)
                    quoted strings (single, double and multi-line semi-colon delimited), and unquoted
                    strings.

                    containerList -  list-type container for data and definition objects parsed from
                                     from the input file.

            Return:
                    containerList - is appended with data and definition objects - 
        """
        # Working container - data or definition
        curContainer = None
        #
        # Working category container
        categoryIndex = {}
        curCategory = None
        #
        curRow = None
        state = None

        # Find the first reserved word and begin capturing data.
        #
        while True:
            curCatName, curAttName, curQuotedString, curWord = next(tokenizer)
            if curWord is None:
                continue
            reservedWord, state = self.__getState(curWord)
            if reservedWord is not None:
                break

        while True:
            #
            #  Set the current state  -
            #
            #  At this point in the processing cycle we are expecting a token containing
            #  either a '_category.attribute'  or a reserved word.
            #
            if curCatName is not None:
                state = "ST_KEY_VALUE_PAIR"
            elif curWord is not None:
                reservedWord, state = self.__getState(curWord)
            else:
                self.__syntaxError("Miscellaneous syntax error")
                return

                #
            # Process  _category.attribute  value assignments
            #
            if state == "ST_KEY_VALUE_PAIR":
                try:
                    curCategory = categoryIndex[curCatName]
                except KeyError:
                    # A new category is encountered - create a container and add a row
                    curCategory = categoryIndex[curCatName] = DataCategory(
                        curCatName)

                    try:
                        curContainer.append(curCategory)
                    except AttributeError:
                        self.__syntaxError(
                            "Category cannot be added to  data_ block")
                        return

                    curRow = []
                    curCategory.append(curRow)
                else:
                    # Recover the existing row from the category
                    try:
                        curRow = curCategory[0]
                    except IndexError:
                        self.__syntaxError(
                            "Internal index error accessing category data")
                        return

                # Check for duplicate attributes and add attribute to table.
                if curAttName in curCategory.getAttributeList():
                    self.__syntaxError(
                        "Duplicate attribute encountered in category")
                    return
                else:
                    curCategory.appendAttribute(curAttName)

                # Get the data for this attribute from the next token
                tCat, tAtt, curQuotedString, curWord = next(tokenizer)

                if tCat is not None or (curQuotedString is None
                                        and curWord is None):
                    self.__syntaxError("Missing data for item _%s.%s" %
                                       (curCatName, curAttName))

                if curWord is not None:
                    #
                    # Validation check token for misplaced reserved words  -
                    #
                    reservedWord, state = self.__getState(curWord)
                    if reservedWord is not None:
                        self.__syntaxError("Unexpected reserved word: %s" %
                                           (reservedWord))

                    curRow.append(curWord)

                elif curQuotedString is not None:
                    curRow.append(curQuotedString)

                else:
                    self.__syntaxError("Missing value in item-value pair")

                curCatName, curAttName, curQuotedString, curWord = next(
                    tokenizer)
                continue

            #
            # Process a loop_ declaration and associated data -
            #
            elif state == "ST_TABLE":

                # The category name in the next curCatName,curAttName pair
                #    defines the name of the category container.
                curCatName, curAttName, curQuotedString, curWord = next(
                    tokenizer)

                if curCatName is None or curAttName is None:
                    self.__syntaxError("Unexpected token in loop_ declaration")
                    return

                # Check for a previous category declaration.
                if curCatName in categoryIndex:
                    self.__syntaxError(
                        "Duplicate category declaration in loop_")
                    return

                curCategory = DataCategory(curCatName)

                try:
                    curContainer.append(curCategory)
                except AttributeError:
                    self.__syntaxError(
                        "loop_ declaration outside of data_ block or save_ frame"
                    )
                    return

                curCategory.appendAttribute(curAttName)

                # Read the rest of the loop_ declaration
                while True:
                    curCatName, curAttName, curQuotedString, curWord = next(
                        tokenizer)

                    if curCatName is None:
                        break

                    if curCatName != curCategory.getName():
                        self.__syntaxError(
                            "Changed category name in loop_ declaration")
                        return

                    curCategory.appendAttribute(curAttName)

                # If the next token is a 'word', check it for any reserved words -
                if curWord is not None:
                    reservedWord, state = self.__getState(curWord)
                    if reservedWord is not None:
                        if reservedWord == "stop":
                            return
                        else:
                            self.__syntaxError(
                                "Unexpected reserved word after loop declaration: %s"
                                % (reservedWord))

                # Read the table of data for this loop_ -
                while True:
                    curRow = []
                    curCategory.append(curRow)

                    for _ in curCategory.getAttributeList():
                        if curWord is not None:
                            curRow.append(curWord)
                        elif curQuotedString is not None:
                            curRow.append(curQuotedString)

                        curCatName, curAttName, curQuotedString, curWord = next(
                            tokenizer)

                    # loop_ data processing ends if -

                    # A new _category.attribute is encountered
                    if curCatName is not None:
                        break

                    # A reserved word is encountered
                    if curWord is not None:
                        reservedWord, state = self.__getState(curWord)
                        if reservedWord is not None:
                            break

                continue

            elif state == "ST_DEFINITION":
                # Ignore trailing unnamed saveframe delimiters e.g. 'save_'
                sName = self.__getContainerName(curWord)
                if (len(sName) > 0):
                    curContainer = DefinitionContainer(sName)
                    containerList.append(curContainer)
                    categoryIndex = {}
                    curCategory = None

                curCatName, curAttName, curQuotedString, curWord = next(
                    tokenizer)

            elif state == "ST_DATA_CONTAINER":
                #
                dName = self.__getContainerName(curWord)
                if len(dName) == 0:
                    dName = "unidentified"
                curContainer = DataContainer(dName)
                containerList.append(curContainer)
                categoryIndex = {}
                curCategory = None
                curCatName, curAttName, curQuotedString, curWord = next(
                    tokenizer)

            elif state == "ST_STOP":
                return
            elif state == "ST_GLOBAL":
                curContainer = DataContainer("blank-global")
                curContainer.setGlobal()
                containerList.append(curContainer)
                categoryIndex = {}
                curCategory = None
                curCatName, curAttName, curQuotedString, curWord = next(
                    tokenizer)

            elif state == "ST_UNKNOWN":
                self.__syntaxError("Unrecogized syntax element: " +
                                   str(curWord))
                return
Пример #3
0
    def testUpdateDataFile(self):
        """Test case -  write data file"""
        self.lfh.write(
            "\nStarting %s %s\n"
            % (self.__class__.__name__, sys._getframe().f_code.co_name)
        )
        try:
            # Create a initial data file --
            #
            tmpf = tempname(suffix=".cif")
            myDataList = []
            ofh = open(tmpf, "w")
            curContainer = DataContainer("myblock")
            aCat = DataCategory("pdbx_seqtool_mapping_ref")
            aCat.appendAttribute("ordinal")
            aCat.appendAttribute("entity_id")
            aCat.appendAttribute("auth_mon_id")
            aCat.appendAttribute("auth_mon_num")
            aCat.appendAttribute("pdb_chain_id")
            aCat.appendAttribute("ref_mon_id")
            aCat.appendAttribute("ref_mon_num")
            aCat.append((1, 2, 3, 4, 5, 6, 7))
            aCat.append((1, 2, 3, 4, 5, 6, 7))
            aCat.append((1, 2, 3, 4, 5, 6, 7))
            aCat.append((1, 2, 3, 4, 5, 6, 7))
            curContainer.append(aCat)
            myDataList.append(curContainer)
            pdbxW = PdbxWriter(ofh)
            pdbxW.write(myDataList)
            ofh.close()
            #
            # Read and update the data -
            #
            myDataList = []
            ifh = open(tmpf, "r")
            pRd = PdbxReader(ifh)
            pRd.read(myDataList)
            ifh.close()
            #
            myBlock = myDataList[0]
            myBlock.printIt()
            myCat = myBlock.getObj("pdbx_seqtool_mapping_ref")
            myCat.printIt()
            for iRow in range(0, myCat.getRowCount()):
                myCat.setValue("some value", "ref_mon_id", iRow)
                myCat.setValue(100, "ref_mon_num", iRow)
            ofh = open(tempname(suffix=".cif"), "w")
            pdbxW = PdbxWriter(ofh)
            pdbxW.write(myDataList)
            ofh.close()

        except:
            traceback.print_exc(file=sys.stderr)
            self.fail()
Пример #4
0
 def testWriteDataFile(self):
     """Test case -  write data file"""
     self.lfh.write(
         "\nStarting %s %s\n"
         % (self.__class__.__name__, sys._getframe().f_code.co_name)
     )
     try:
         #
         myDataList = []
         ofh = open(tempname(suffix=".cif"), "w")
         curContainer = DataContainer("myblock")
         aCat = DataCategory("pdbx_seqtool_mapping_ref")
         aCat.appendAttribute("ordinal")
         aCat.appendAttribute("entity_id")
         aCat.appendAttribute("auth_mon_id")
         aCat.appendAttribute("auth_mon_num")
         aCat.appendAttribute("pdb_chain_id")
         aCat.appendAttribute("ref_mon_id")
         aCat.appendAttribute("ref_mon_num")
         aCat.append((1, 2, 3, 4, 5, 6, 7))
         aCat.append((1, 2, 3, 4, 5, 6, 7))
         aCat.append((1, 2, 3, 4, 5, 6, 7))
         aCat.append((1, 2, 3, 4, 5, 6, 7))
         curContainer.append(aCat)
         myDataList.append(curContainer)
         pdbxW = PdbxWriter(ofh)
         pdbxW.write(myDataList)
         ofh.close()
     except:
         traceback.print_exc(file=sys.stderr)
         self.fail()
Пример #5
0
    def testUpdateDataFile(self):
        """Test case -  update data file 
        """
        self.lfh.write(
            "\nStarting %s %s\n" %
            (self.__class__.__name__, sys._getframe().f_code.co_name))
        try:
            # Create a initial data file --
            #
            myDataList = []

            curContainer = DataContainer("myblock")
            aCat = DataCategory("pdbx_seqtool_mapping_ref")
            aCat.appendAttribute("ordinal")
            aCat.appendAttribute("entity_id")
            aCat.appendAttribute("auth_mon_id")
            aCat.appendAttribute("auth_mon_num")
            aCat.appendAttribute("pdb_chain_id")
            aCat.appendAttribute("ref_mon_id")
            aCat.appendAttribute("ref_mon_num")
            aCat.append([9, 2, 3, 4, 5, 6, 7])
            aCat.append([10, 2, 3, 4, 5, 6, 7])
            aCat.append([11, 2, 3, 4, 5, 6, 7])
            aCat.append([12, 2, 3, 4, 5, 6, 7])

            #self.lfh.write("Assigned data category state-----------------\n")
            #aCat.dumpIt(fh=self.lfh)
            tmpf = tempname(suffix='.cif')

            curContainer.append(aCat)
            myDataList.append(curContainer)
            ofh = open(tmpf, "w")
            pdbxW = PdbxWriter(ofh)
            pdbxW.write(myDataList)
            ofh.close()
            #
            #
            # Read and update the data -
            #
            myDataList = []
            ifh = open(tmpf, "r")
            pRd = PdbxReader(ifh)
            pRd.read(myDataList)
            ifh.close()
            #
            myBlock = myDataList[0]
            myBlock.printIt()
            myCat = myBlock.getObj('pdbx_seqtool_mapping_ref')
            myCat.printIt()
            for iRow in range(0, myCat.getRowCount()):
                myCat.setValue('some value', 'ref_mon_id', iRow)
                myCat.setValue(100, 'ref_mon_num', iRow)
            ofh = open(tempname(suffix='.cif'), "w")
            pdbxW = PdbxWriter(ofh)
            pdbxW.write(myDataList)
            ofh.close()
            #

        except:
            traceback.print_exc(file=self.lfh)
            self.fail()