OBGenericData Class Reference

Base class for generic data. More...

#include <openbabel/generic.h>

Inheritance diagram for OBGenericData:

Inheritance graph
[legend]

List of all members.

Public Member Functions

 OBGenericData (const std::string attr="undefined", const unsigned int type=OBGenericDataType::UndefinedData, const DataOrigin source=any)
virtual OBGenericDataClone (OBBase *) const
virtual ~OBGenericData ()
void SetAttribute (const std::string &v)
void SetOrigin (const DataOrigin s)
virtual const std::string & GetAttribute () const
unsigned int GetDataType () const
virtual const std::string & GetValue () const
virtual DataOrigin GetOrigin () const

Protected Attributes

std::string _attr
unsigned int _type
DataOrigin _source


Detailed Description

Base class for generic data.

OBGenericData is an abstract base class which defines an interface for storage, retrieval, and indexing of arbitrary generic data. Subclasses of OBGenericData can be used to store custom data on a per-atom, per-bond, per-molecule, or per-residue basis. Open Babel currently supports a small subset of chemical functionality as OBGenericData types, which will expand over time to support additional interconversion (e.g., spectroscopy, dynamics, surfaces...)

For more information on currently supported types, please see the developer wiki: http://openbabel.sourceforge.net/wiki/Generic_Data

For your own custom data, either define a custom subclass using an id from the OBGenericDataType::CustomData0 to OBGenericDataType::CustomData15 slots, or store your data as a string and use OBPairData for key/value access. The latter is highly recommended for various text descriptors e.g., in QSAR, atom or bond labels, or other textual data.

New in Open Babel, version 2.1 is the template-based OBPairTemplate, which can be used to store arbitrary data types. There are predefined types OBPairInteger and OBPairFloatingPoint for storing integers and floating-point values without converting to a string representation.

Also new is the "source" or "origin" of a data entry, enumerated by DataOrigin. This can be accessed by SetOrigin() and GetOrigin(), as well as via "filtering" methods in OBBase, allowing you to separate data read in from a file, added by a user, or assigned by Open Babel internally.

While the library and import routines will set DataOrigin correctly, you should try to annotate data added by your code. Typically this would either be userAdded or external. The former refers to something the user requested as an annotation, while the latter refers to annotations your code adds automatically.

Example code using OBGenericData:

      if (mol.HasData(OBGenericDataType::UnitCell))
      {
         uc = (OBUnitCell*)mol.GetData(OBGenericDataType::UnitCell);
         sprintf(buffer,
            "%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f",
            uc->GetA(), uc->GetB(), uc->GetC(),
            uc->GetAlpha() , uc->GetBeta(), uc->GetGamma());
         ofs << buffer << endl;
      }

      ...

      vector<OBGenericData*>::iterator k;
      vector<OBGenericData*> vdata = mol.GetData();
      for (k = vdata.begin();k != vdata.end();++k)
         if ((*k)->GetDataType() == OBGenericDataType::PairData)
         {
            ofs << ">  <" << (*k)->GetAttribute() << ">" << endl;
            ofs << ((OBPairData*)(*k))->GetValue() << endl << endl;
         }

Similar code also works for OBGenericData stored in an OBAtom or OBBond or OBResidue. These examples show use of DataOrigin outside of the Open Babel library.

      string atomLabel; // e.g., from the user adding annotation to an atom
      if (!atom.HasData("UserLabel")) // stored textual data as an OBPairData
      {
         OBPairData *label = new OBPairData;
         label->SetAttribute("UserLabel");
         label->SetValue(atomLabel);
         label->SetOrigin(userInput); // set by user, not by Open Babel

         atom.SetData(label);
      }

      ...

      if (bond.HasData("DisplayType")) // e.g. in a visualization tool
      {
         OBPairData *display = dynamic_cast<OBPairData *> bond.GetData("DisplayType");
         if (display->GetValue() == "wireframe")
         {
            ... // display a wireframe view
         }
      }

When designing a class derived from OBGenericData you must add a Clone() function. For classes used with OBMol this is used when an OBMol object is copied. If your class member variables contain pointers to atoms or bonds then it will be necessary to ensure that these are updated in Clone() to refer to the new molecule. Without these and similar pointers it is more likely that the very simple clone function

      virtual OBGenericData* Clone(OBBase* parent) const
         {return new MyNewClass(*this);}
and the compiler generated copy constructor would be sufficient.

It is recommended that, if possible, OBGenericData classes do not store atom and bond pointers. Using atom and bond indices instead would allow the simple version of Clone() above. See OBRotameterData::Clone for an example of a more complicated version. For classes which are not intended to support copying, Clone() can return NULL

      virtual OBGenericData* Clone(OBBase* parent) const 
         {return NULL;}
Clone() is a pure virtual function so that you need to decide what kind of function you need and include it explicitly.

Constructor & Destructor Documentation

OBGenericData ( const std::string  attr = "undefined",
const unsigned int  type = OBGenericDataType::UndefinedData,
const DataOrigin  source = any 
)

virtual ~OBGenericData (  )  [inline, virtual]


Member Function Documentation

virtual OBGenericData* Clone ( OBBase  )  const [inline, virtual]

void SetAttribute ( const std::string &  v  )  [inline]

void SetOrigin ( const DataOrigin  s  )  [inline]

virtual const std::string& GetAttribute (  )  const [inline, virtual]

Returns:
The attribute (key), which can be used to retrieve this data

Referenced by OBMoleculeFormat::MakeCombinedMolecule().

unsigned int GetDataType (  )  const [inline]

Returns:
the data type for this object as defined in OBGenericDataType

virtual const std::string& GetValue (  )  const [inline, virtual]

Base class returns a default value (the attribute type) but should never be called.

Reimplemented in OBCommentData, and OBPairData.

Referenced by OBDescriptor::FilterCompare(), and OBDescriptor::GetValues().

virtual DataOrigin GetOrigin (  )  const [inline, virtual]


Member Data Documentation

std::string _attr [protected]

attribute tag (e.g., "UnitCell", "Comment" or "Author")

Referenced by OBRotamerList::Clone(), and OBNasaThermoData::OBNasaThermoData().

unsigned int _type [protected]

attribute type -- declared for each subclass

Referenced by OBRotamerList::Clone(), and OBNasaThermoData::OBNasaThermoData().

DataOrigin _source [protected]


The documentation for this class was generated from the following files: