RCSB PDB Protein Data Bank A Member of the wwPDB
An Information Portal to Biological Macromolecular Structures
PDB Home | Contact Us

PDBML2CIF
Usage Example: V2-0-07

----------------PDBML2CIF Library Usage Examples-------------

1. Reading the content of a PDBML file and accessing its data

2. Reading the content of a PDBML file and writing out a CIF file


#include <string>

#include <util/PlatformUtils.hpp>
#include <util/TransService.hpp>
#include <sax2/XMLReaderFactory.hpp>

#include "CifFile.h"
#include "CifLoadReorganizer.h"

#include "misc_util.h"
#include "PdbMlParserHandler.h"


using namespace std;
XERCES_CPP_NAMESPACE_USE


    // The (absolute or relative) name of the PDBML file should be
    // stored in xmlFileName
    string xmlFileName;

    // The (absolute or relative) name of the CIF file should be
    // stored in cifFileName
    string cifFileName;

    // The name of block
    string blockName;

    // The name of table
    string tableName;


    /*
    ** Example 1: How to read the content of a PDML file.
    */

    // Initialize the XML parser platform
    try
    {
        XMLPlatformUtils::Initialize();
    }
    catch (const XMLException& toCatch)
    {
        // Initialization exception ocurred. Report the error and exit.
        string err("Error in PdbMlParser [main() method]: Error during"\
          " SAX2 initialization! :\n");
        err += toString(toCatch.getMessage());
        err += ".\n\n";

        Error(err);

        return(-1);
    }

    // Create the XML reader
    SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();

    // Initialize number of errors
    int errorCount = 0;

 
    // Create CIF file object.
    // Note that this is only an in-memory object, which will not be
    // serialized to its binary file equivalent. Therefore, the second
    // argument ("virtual"), which would normally indicate the name of the
    // serialized binary file, is not used at all.
    CifFile cifFile(VIRTUAL_MODE, "virtual");

    try
    {
        // Have the XML reader store data in the created CIF file object
        PdbMlParserHandler tHandler(cifFile);

        // Set content and error handlers in XML reader
        parser->setContentHandler(&tHandler);
        parser->setErrorHandler(&tHandler);

        // Parse the PDBML file
        parser->parse(xmlFileName.c_str());

        errorCount = parser->getErrorCount();
    }
    catch (const XMLException& toCatch)
    {
        // PDBML parsing exception. Report error and terminate
        string err("Error in PdbMlParser [main() method]: A SAX2 error"\
          " occurred:\n");
        err += toString(toCatch.getMessage());
        err += ".\n\n";

        Error(err);

        XMLPlatformUtils::Terminate();
        return(-1);
    }

    // XML parsing is successfull. Delete the parser and de-initialize the
    // XML parser platform.
    delete parser;
    XMLPlatformUtils::Terminate();

    // Accessing the data in the CIF file object
    // Example of printing out a table in a block.

    Block& block = cifFile.GetBlock(blockName);

    ISTable* isTableP = block.GetTablePtr(tableName);

    cout << (*isTableP) << endl;


    /*
    ** Example 2: How to convert PDBML to CIF
    */

    // Writing out a CIF file
    // Note that this is now conversion from PDBML format to CIF format
    cifFile.Write(cifFileName);



 

 

© RCSB PDB