Loading...
Searching...
No Matches
OBConversion Class Reference

`#include <openbabel/obconversion.h>`

Inheritance diagram for OBConversion:
[legend]

## Classes

struct | StreamState

## Public Member Functions

Construction
| OBConversion (std::istream *is=nullptr, std::ostream *os=nullptr)
| OBConversion (std::string inFilename, std::string outFilename="")
| OBConversion (const OBConversion &o)
OBConversion & | operator= (const OBConversion &rhs)
virtual | ~OBConversion ()
Parameter get and set
These return a filtered stream for reading/writing (possible filters include compression, decompression, and newline transformation)
std::istream * | GetInStream () const
std::ostream * | GetOutStream () const
void | SetInStream (std::istream *pIn, bool takeOwnership=false)
void | SetOutStream (std::ostream *pOut, bool takeOwnership=false)
bool | SetInAndOutFormats (const char *inID, const char *outID, bool ingzip=false, bool outgzip=false)
bool | SetInAndOutFormats (OBFormat *pIn, OBFormat *pOut, bool ingzip=false, bool outgzip=false)
bool | SetInFormat (const char *inID, bool isgzip=false)
bool | SetInFormat (OBFormat *pIn, bool isgzip=false)
bool | SetOutFormat (const char *outID, bool isgzip=false)
bool | SetOutFormat (OBFormat *pOut, bool isgzip=false)
OBFormat * | GetInFormat () const
OBFormat * | GetOutFormat () const
bool | GetInGzipped () const
bool | GetOutGzipped () const
std::string | GetInFilename () const
std::string | GetOutFilename () const
std::streampos | GetInPos () const
size_t | GetInLen () const
const char * | GetTitle () const
OBConversion * | GetAuxConv () const
void | SetAuxConv (OBConversion *pConv)
Supported file format
std::vector< std::string > | GetSupportedInputFormat ()
std::vector< std::string > | GetSupportedOutputFormat ()
Conversion
int | Convert (std::istream *is, std::ostream *os)
int | Convert ()
int | FullConvert (std::vector< std::string > &FileList, std::string &OutputFileName, std::vector< std::string > &OutputFileList)
Conversion loop control
int | AddChemObject (OBBase *pOb)
OBBase * | GetChemObject ()
bool | IsLast ()
bool | IsFirstInput ()
void | SetFirstInput (bool b=true)
int | GetOutputIndex () const
void | SetOutputIndex (int indx)
void | SetMoreFilesToCome ()
void | SetOneObjectOnly (bool b=true)
void | SetLast (bool b)
bool | IsLastFile ()
int | GetCount () const

## Static Public Member Functions

Collection of formats
static int | RegisterFormat (const char *ID, OBFormat *pFormat, const char *MIME=nullptr)
static OBFormat * | FindFormat (const char *ID)
static OBFormat * | FindFormat (const std::string ID)
static OBFormat * | FormatFromExt (const char *filename)
static OBFormat * | FormatFromExt (const char *filename, bool &isgzip)
static OBFormat * | FormatFromExt (const std::string filename)
static OBFormat * | FormatFromExt (const std::string filename, bool &isgzip)
static OBFormat * | FormatFromMIME (const char *MIME)
static bool | GetNextFormat (Formatpos &itr, const char *&str, OBFormat *&pFormat)
Information
static const char * | Description ()

## Option handling

Three types of Option provide information and control instructions to the conversion process, INOPTIONS, OUTOPTIONS, GENOPTIONS, and are stored in each OBConversion object in separate maps. Each option has an id and an optional text string. They are set individually by AddOption() or (rarely) collectively in SetOptions(). Options cannot be altered but can be replaced with AddOption() and deleted with RemoveOption(), which, however, should be used in an op derived from OBOp (because of iterator invalidation).

If the "Convert" interface is used, the GENOPTIONS are acted upon in the OBBase::DoTransformations() functions (currently only OBMol has one). This happens after the object has been input but before it has been output. All the options are available to input and output formats, etc. via the IsOption() function, and the interpretation of any text string needs to be done subsequently.

In the commandline interface, options with single character ids are are indicated like -s, and those with multiple character ids like –gen3D. An option may have one or more parameters which appear, space separated, in the option's text string. With babel, unless the option is at the end of the command, it is necessary for the number of its parameters to be exactly that specified in RegisterOptionParam(). The default is 0, but if it is more, and babel is likely to be used, this function should be called in the constructor of a format or op. With obabel (or the GUI), it is not necessary to call RegisterOptionParam().

New GENOPTIONS can be defined (as plugins) using the class OBOp.

It is customary for a format or op to document any INOPTIONS or OUTPTIONS it uses in its Description() function. As well as providing documentation during use, this is also parsed by the GUI to construct its checkboxes,etc., so it is advisable to give new Descriptions the same form as existing ones.

Some conversion options, such as -f, -l, -m, are unlikely to be used in programming, but are listed in OBConversion::Description(). The built-in GENOPTIONS for OBMol objects are listed in OBMol::ClassDescription() which is in transform.cpp and also in this documentation under AddOption().

enum | Option_type { INOPTIONS , OUTOPTIONS , GENOPTIONS , ALL }
const char * | IsOption (const char *opt, Option_type opttyp=OUTOPTIONS)
const std::map< std::string, std::string > * | GetOptions (Option_type opttyp)
void | AddOption (const char *opt, Option_type opttyp=OUTOPTIONS, const char *txt=nullptr)
bool | RemoveOption (const char *opt, Option_type optype)
void | SetOptions (const char *options, Option_type opttyp)
void | CopyOptions (OBConversion *pSourceConv, Option_type typ=ALL)
static void | RegisterOptionParam (std::string name, OBFormat *pFormat, int numberParams=0, Option_type typ=OUTOPTIONS)
static int | GetOptionParams (std::string name, Option_type typ)

## Convenience functions

typedef std::map< std::string, int > | OPAMapType
typedef FilteringInputStream< LineEndingExtractor > | LEInStream
std::string | InFilename
std::string | OutFilename
std::istream * | pInput
std::vector< std::istream * > | ownedInStreams
std::ostream * | pOutput
std::vector< std::ostream * > | ownedOutStreams
OBFormat * | pInFormat
OBFormat * | pOutFormat
std::map< std::string, std::string > | OptionsArray [3]
int | Index
unsigned int | StartNumber
unsigned int | EndNumber
int | Count
bool | m_IsFirstInput
bool | m_IsLast
bool | MoreFilesToCome
bool | OneObjectOnly
bool | ReadyToInput
bool | SkippedMolecules
bool | inFormatGzip
bool | outFormatGzip
OBBase * | pOb1
std::streampos | wInpos
std::streampos | rInpos
size_t | wInlen
size_t | rInlen
OBConversion * | pAuxConv
std::vector< std::string > | SupportedInputFormat
std::vector< std::string > | SupportedOutputFormat
static OBFormat * | pDefaultFormat
bool | Write (OBBase *pOb, std::ostream *pout=nullptr)
std::string | WriteString (OBBase *pOb, bool trimWhitespace=false)
bool | WriteFile (OBBase *pOb, std::string filePath)
void | CloseOutFile ()
bool | Read (OBBase *pOb, std::istream *pin=nullptr)
bool | ReadString (OBBase *pOb, std::string input)
bool | ReadFile (OBBase *pOb, std::string filePath)
bool | OpenInAndOutFiles (std::string infilepath, std::string outfilepath)
void | ReportNumberConverted (int count, OBFormat *pFormat=nullptr)
int | NumInputObjects ()
static OBFormat * | GetDefaultFormat ()
static std::string | BatchFileName (std::string &BaseName, std::string &InFile)
static std::string | IncrementedFileName (std::string &BaseName, const int Count)
static bool | CheckForUnintendedBatch (const std::string &infile, const std::string &outfile)
static OPAMapType & | OptionParamArray (Option_type typ)
void | ClearInStreams ()
bool | SetStartAndEnd ()
bool | OpenAndSetFormat (bool SetFormat, std::ifstream *is, std::stringstream *ss=nullptr)

## Detailed Description

Class to convert from one format to another.

OBConversion maintains a list of the available formats, provides information on them, and controls the conversion process.

A conversion is carried out by the calling routine, usually in a user interface or an application program, making an instance of OBConversion. It is loaded with the in and out formats, any options and (usually) the default streams for input and output. Then either the Convert() function is called, which allows a single input file to be converted, or the extended functionality of FullConvert() is used. This allows multiple input and output files, allowing:

- aggregation - the contents of many input files converted and sent to one output file;
- splitting - the molecules from one input file sent to separate output files;
- batch conversion - each input file converted to an output file.

These procedures constitute the "Convert" interface. OBConversion and the user interface or application program do not need to be aware of any other part of OpenBabel - mol.h is not #included. This allows any chemical object derived from OBBase to be converted; the type of object is decided by the input format class. However,currently, almost all the conversions are for molecules of class OBMol. / OBConversion can also be used with an "API" interface called from programs which manipulate chemical objects. Input/output is done with the Read() and Write() functions which work with any chemical object, but need to have its type specified. (The ReadMolecule() and WriteMolecule() functions of the format classes can also be used directly.)

      Example code using OBConversion

      <b>To read in a molecule, manipulate it and write it out.</b>

      Set up an istream and an ostream, to and from files or elsewhere.
      (cin and cout are used in the example). Specify the file formats.

      @code
      OBConversion conv(&cin,&cout);
      if(conv.SetInAndOutFormats("SMI","MOL"))
      {
         OBMol mol;
         if(conv.Read(&mol))
            // ...manipulate molecule

         conv->Write(&mol);
      }
      @endcode

      A two stage construction is used to allow error handling
      if the format ID is not recognized. This is necessary now that the
      formats are dynamic and errors are not caught at compile time.
      OBConversion::Read() uses a pointer to OBBase, so that, in addition
      to OBMol, other kinds of objects, such as reactions, can also be handled
      if the format routines are written appropriately.

      <b>To make a molecule from a SMILES string.</b>
      @code
      std::string SmilesString;
      OBMol mol;
      stringstream ss(SmilesString)
      OBConversion conv(&ss);
      if(conv.SetInFormat("smi") && conv.Read(&mol))
         // ...
      @endcode

      An alternative way is more convenient if using bindings from another language:
      @code
      std::string SmilesString;
      OBMol mol;
      OBConversion conv;
      if(conv.SetInFormat("smi") && conv.ReadString(&mol, SmilesString))
         // ...
      @endcode

      <b>To do a file conversion without manipulating the molecule.</b>

      @code
      #include <openbabel/obconversion.h> //mol.h is not needed
      ...set up an istream is and an ostream os
      OBConversion conv(&is,&os);
      if(conv.SetInAndOutFormats("SMI","MOL"))
      {
         conv.AddOption("h",OBConversion::GENOPTIONS); //Optional; (h adds expicit hydrogens)
         conv.Convert();
      }
      @endcode

      <b>To read a multi-molecule file if using bindings from another language</b>

      The first molecule should be read using ReadFile, and subsequent molecules using Read,
      as follows:
      @code
      #include <openbabel/obconversion.h> //mol.h is not needed
      OBConversion conv;
      OBMol mol;
      bool success = conv.SetInFormat("sdf");
      if(success)
      {
         bool notatend = conv.ReadFile(&mol, "myfile.sdf");
         // Do something with mol
         while(notatend)
         {
             notatend = conv.Read(&mol);
             // Do something with mol
         }
      }
      @endcode

      <b>To add automatic format conversion to an existing program.</b>

      The existing program inputs from the file identified by the
      const char* filename into the istream is. The file is assumed to have
      a format ORIG, but other formats, identified by their file extensions,
      can now be used.

      @code
      ifstream ifs(filename); //Original code

      OBConversion conv;
      OBFormat* inFormat = conv.FormatFromExt(filename);
      OBFormat* outFormat = conv.GetFormat("ORIG");
      istream* pIn = &ifs;
      stringstream newstream;
      if(inFormat && outFormat)
      {
         conv.SetInAndOutFormats(inFormat,outFormat);
         conv.Convert(pIn,&newstream);
         pIn=&newstream;
      }
      //else error; new features not available; fallback to original functionality

      ...Carry on with original code using pIn
      @endcode

Examples
  obconformersearch_default.cpp, obconversion_readstring.cpp, and obforcefield_energy.cpp.

## Member Typedef Documentation

## OPAMapType

typedef std::map<std::string,int> OPAMapType

protected

## LEInStream

typedef FilteringInputStream< LineEndingExtractor > LEInStream

protected

## Member Enumeration Documentation

## Option_type

enum Option_type

Three types of options set on the the command line by -a? , -x? , or -?

Enumerator
INOPTIONS |
OUTOPTIONS |
GENOPTIONS |
ALL |

## Constructor & Destructor Documentation

## OBConversion() [1/3]

OBConversion | ( | std::istream * | is = nullptr,
|  | std::ostream * | os = nullptr )

Referenced by CopyOptions(), GetAuxConv(), XMLConversion::GetContentDouble(), XMLConversion::GetDerived(), OBConversion(), operator=(), OBConversion::StreamState::popInput(), OBConversion::StreamState::popOutput(), OBConversion::StreamState::pushInput(), OBConversion::StreamState::pushOutput(), SetAuxConv(), and XMLConversion::XMLConversion().

## OBConversion() [2/3]

OBConversion | ( | std::string | inFilename,
|  | std::string | outFilename = "" )

Convenience constructor. Sets up streams from specified files. If format can not be determined from filename, a stream is not opened.

## OBConversion() [3/3]

OBConversion | ( | const OBConversion & | o | ) |

Copy constructor. Stream ownership is not copied. Source remains responsible for the memory.

## ~OBConversion()

~OBConversion | ( |  | ) |

virtual

## Member Function Documentation

## operator=()

OBConversion & operator= | ( | const OBConversion & | rhs | ) |

Assignment. Stream ownership is not copied. Source remains responsible for the memory.

## RegisterFormat()

int RegisterFormat | ( | const char * | ID,
|  | OBFormat * | pFormat,
|  | const char * | MIME = nullptr )

static

Called once by each format class.

Class information on formats is collected by making an instance of the class derived from OBFormat(only one is usually required). RegisterFormat() is called from its constructor.

If the compiled format is stored separately, like in a DLL or shared library, the initialization code makes an instance of the imported OBFormat class.

## FindFormat() [1/2]

OBFormat * FindFormat | ( | const char * | ID | ) |

static

Searches registered formats.

Referenced by FormatFromExt(), SetInFormat(), and SetOutFormat().

## FindFormat() [2/2]

OBFormat * FindFormat | ( | const std::string | ID | ) |

static

Searches registered formats.

Since
  version 2.3

## FormatFromExt() [1/4]

OBFormat * FormatFromExt | ( | const char * | filename | ) |

static

Searches registered formats for an ID the same as the file extension.

Examples
  obconformersearch_default.cpp, and obforcefield_energy.cpp.

Referenced by FormatFromExt(), FormatFromExt(), FormatFromExt(), OpenAndSetFormat(), OpenInAndOutFiles(), ReadFile(), and WriteFile().

## FormatFromExt() [2/4]

OBFormat * FormatFromExt | ( | const char * | filename,
|  | bool & | isgzip )

static

## FormatFromExt() [3/4]

OBFormat * FormatFromExt | ( | const std::string | filename | ) |

static

Searches registered formats for an ID the same as the file extension.

Since
  version 2.3

## FormatFromExt() [4/4]

OBFormat * FormatFromExt | ( | const std::string | filename,
|  | bool & | isgzip )

static

## FormatFromMIME()

OBFormat * FormatFromMIME | ( | const char * | MIME | ) |

static

Searches registered formats for a MIME the same as the chemical MIME type passed.

## GetNextFormat()

bool GetNextFormat | ( | Formatpos & | itr,
|  | const char *& | str,
|  | OBFormat *& | pFormat )

static

Deprecated!.Repeatedly called to recover available Formats.

## Description()

const char * Description | ( |  | ) |

static

## GetInStream()

std::istream * GetInStream | ( |  | ) | const

inline

Referenced by OBMoleculeFormat::DeferMolOutput(), NumInputObjects(), and OBMoleculeFormat::ReadChemObjectImpl().

## GetOutStream()

std::ostream * GetOutStream | ( |  | ) | const

inline

## SetInStream()

void SetInStream | ( | std::istream * | pIn,
|  | bool | takeOwnership = false )

Set input stream. If takeOwnership is true, will deallocate when done. If isGzipped is true, will treat as a gzipped stream regardless of option settings,.

Set input stream, removing/deallocating previous stream if necessary. If takeOwnership is true, takes responsibility for freeing pIn

Referenced by Convert(), FullConvert(), OBConversion(), OpenInAndOutFiles(), operator=(), OBConversion::StreamState::popInput(), Read(), ReadFile(), ReadString(), and ~OBConversion().

## SetOutStream()

void SetOutStream | ( | std::ostream * | pOut,
|  | bool | takeOwnership = false )

Set output stream, removing/deallocating previous stream if necessary. If takeOwnership is true, takes responsibility for freeing pOut Be aware that if the output stream is gzipped format, then this outstream either needs to be replaced (e.g., SetOutStream(NULL)) or the OBConversion destroyed before the underlying outputstream is deallocated.

Referenced by CloseOutFile(), Convert(), FullConvert(), OBConversion(), OpenInAndOutFiles(), operator=(), OBConversion::StreamState::popOutput(), Write(), WriteFile(), WriteString(), and ~OBConversion().

## SetInAndOutFormats() [1/2]

bool SetInAndOutFormats | ( | const char * | inID,
|  | const char * | outID,
|  | bool | inzip = false,
|  | bool | outzip = false )

Sets the formats from their ids, e g CML.

Sets the formats from their ids, e g CML. If inID is NULL, the input format is left unchanged. Similarly for outID Returns true if both formats have been successfully set at sometime

## SetInAndOutFormats() [2/2]

bool SetInAndOutFormats | ( | OBFormat * | pIn,
|  | OBFormat * | pOut,
|  | bool | ingzip = false,
|  | bool | outgzip = false )

## SetInFormat() [1/2]

bool SetInFormat | ( | const char * | inID,
|  | bool | isgzip = false )

Sets the input format from an id e.g. CML.

Examples
  obconformersearch_default.cpp, obconversion_readstring.cpp, and obforcefield_energy.cpp.

Referenced by OpenBabel::CompileSmilesQuery(), OpenAndSetFormat(), OBMoleculeFormat::ReadNameIndex(), SetInAndOutFormats(), and SetInAndOutFormats().

## SetInFormat() [2/2]

bool SetInFormat | ( | OBFormat * | pIn,
|  | bool | isgzip = false )

## SetOutFormat() [1/2]

bool SetOutFormat | ( | const char * | outID,
|  | bool | isgzip = false )

Sets the output format from an id e.g. CML.

Examples
  obconformersearch_default.cpp.

Referenced by OBBuilder::Build(), UniqueTautomerFunctor::operator()(), SetInAndOutFormats(), and SetInAndOutFormats().

## SetOutFormat() [2/2]

bool SetOutFormat | ( | OBFormat * | pOut,
|  | bool | isgzip = false )

## GetInFormat()

OBFormat * GetInFormat | ( |  | ) | const

inline

Referenced by NumInputObjects().

## GetOutFormat()

OBFormat * GetOutFormat | ( |  | ) | const

inline

Referenced by OBMoleculeFormat::DoOutputOptions(), and OBMoleculeFormat::OutputDeferredMols().

## GetInGzipped()

bool GetInGzipped | ( |  | ) | const

inline

## GetOutGzipped()

bool GetOutGzipped | ( |  | ) | const

inline

## GetInFilename()

std::string GetInFilename | ( |  | ) | const

inline

## GetOutFilename()

std::string GetOutFilename | ( |  | ) | const

inline

## GetInPos()

std::streampos GetInPos | ( |  | ) | const

inline

Get the position in the input stream of the object being read.

## GetInLen()

size_t GetInLen | ( |  | ) | const

inline

Get the length in the input stream of the object being read.

## GetTitle()

const char * GetTitle | ( |  | ) | const

Returns
  a default title which is the filename

## GetAuxConv()

OBConversion * GetAuxConv | ( |  | ) | const

inline

Extension method: deleted in ~OBConversion().

## SetAuxConv()

void SetAuxConv | ( | OBConversion * | pConv | ) |

inline

## IsOption()

const char * IsOption | ( | const char * | opt,
|  | Option_type | opttyp = OUTOPTIONS )

Determine whether an option is set.

Returns
  NULL if option not and a pointer to the associated text if it is

Referenced by Convert(), OBMoleculeFormat::DoOutputOptions(), FullConvert(), NumInputObjects(), OBMoleculeFormat::ReadChemObjectImpl(), SetInStream(), SetOutStream(), SetStartAndEnd(), and OBMoleculeFormat::WriteChemObjectImpl().

## GetOptions()

const std::map< std::string, std::string > * GetOptions | ( | Option_type | opttyp | ) |

inline

Access the map with option name as key and any associated text as value.

Referenced by OBMoleculeFormat::OutputDeferredMols(), and OBMoleculeFormat::ReadChemObjectImpl().

## AddOption()

void AddOption | ( | const char * | opt,
|  | Option_type | opttyp = OUTOPTIONS,
|  | const char * | txt = nullptr )

Set an option of specified type, with optional text.

Referenced by OBMoleculeFormat::DeferMolOutput().

## RemoveOption()

bool RemoveOption | ( | const char * | opt,
|  | Option_type | optype )

## SetOptions()

void SetOptions | ( | const char * | options,
|  | Option_type | opttyp )

Set several single character options of specified type from string like ab"btext"c"ctext".

## RegisterOptionParam()

void RegisterOptionParam | ( | std::string | name,
|  | OBFormat * | pFormat,
|  | int | numberParams = 0,
|  | Option_type | typ = OUTOPTIONS )

static

For example -h takes 0 parameters; -f takes 1. Call in a format constructor.

Referenced by OBConversion(), OBConversion(), and OBMoleculeFormat::OBMoleculeFormat().

## GetOptionParams()

int GetOptionParams | ( | std::string | name,
|  | Option_type | typ )

static

Returns
  the number of parameters registered for the option, or 0 if not found

## CopyOptions()

void CopyOptions | ( | OBConversion * | pSourceConv,
|  | Option_type | typ = ALL )

Copies the options (by default of all types) from one OBConversion Object to another.

## GetSupportedInputFormat()

std::vector< std::string > GetSupportedInputFormat | ( |  | ) |

Returns the list of supported input format

## GetSupportedOutputFormat()

std::vector< std::string > GetSupportedOutputFormat | ( |  | ) |

Returns the list of supported output format

## Convert() [1/2]

int Convert | ( | std::istream * | is,
|  | std::ostream * | os )

Conversion for single input and output stream.

Convert molecules from is into os. If either is null, uses existing streams. If streams are specified, they do not replace any existing streams.

## Convert() [2/2]

int Convert | ( |  | ) |

Conversion with existing streams.

Actions the "convert" interface. Calls the OBFormat class's ReadMolecule() which

- makes a new chemical object of its chosen type (e.g. OBMol)
- reads an object from the input file
- subjects the chemical object to 'transformations' as specified by the Options
- calls AddChemObject to add it to a buffer. The previous object is first output via the output Format's WriteMolecule(). During the output process calling IsFirst() and GetIndex() (the number of objects including the current one already output. allows more control, for instance writing <cml> and </cml> tags for multiple molecule outputs only.

AddChemObject does not save the object passed to it if it is NULL (as a result of a DoTransformation()) or if the number of the object is outside the range defined by StartNumber and EndNumber.This means the start and end counts apply to all chemical objects found whether or not they are output.

If ReadMolecule returns false the input conversion loop is exited.

Referenced by Convert(), and FullConvert().

## FullConvert()

int FullConvert | ( | std::vector< std::string > & | FileList,
|  | std::string & | OutputFileName,
|  | std::vector< std::string > & | OutputFileList )

Conversion with multiple input/output files: makes input and output streams, and carries out normal, batch, aggregation, and splitting conversion.

Makes input and output streams, and carries out normal, batch, aggregation, and splitting conversion.

Normal Done if FileList contains a single file name and OutputFileName does not contain a *.

Aggregation Done if FileList has more than one file name and OutputFileName does not contain * . All the chemical objects are converted and sent to the single output file.

Splitting Done if FileList contains a single file name and OutputFileName contains a * . Each chemical object in the input file is converted and sent to a separate file whose name is OutputFileName with the replaced by 1, 2, 3, etc. OutputFileName must have at least one character other than the * before the extension. For example, if OutputFileName is NEW*.smi then the output files are NEW1.smi, NEW2.smi, etc.

Batch Conversion Done if FileList has more than one file name and contains a * . Each input file is converted to an output file whose name is OutputFileName with the * replaced by the inputfile name without its path and extension. So if the input files were inpath/First.cml, inpath/Second.cml and OutputFileName was NEW*.mol, the output files would be NEWFirst.mol, NEWSecond.mol.

If FileList is empty, the input stream that has already been set (usually in the constructor) is used. If OutputFileName is empty, the output stream already set is used.

On exit, OutputFileList contains the names of the output files.

Returns the number of Chemical objects converted.

## AddChemObject()

int AddChemObject | ( | OBBase * | pOb | ) |

Adds to internal array during input.

Called by ReadMolecule() to deliver an object it has read from an input stream. Used in two modes:

- When Count is negative it is left negative and the routine is just a store for an OBBase object. The negative value returned tells the calling routine that no more objects are required.
- When count is >=0, probably set by Convert(), it acts as a queue of 2: writing the currently stored value before accepting the supplied one. This delay allows output routines to respond differently when the written object is the last. Count is incremented with each call, even if pOb=NULL. Objects are not added to the queue if the count is outside the range StartNumber to EndNumber. There is no upper limit if EndNumber is zero. The return value is Count ((>0) or 0 if WriteChemObject returned false.

Referenced by OBMoleculeFormat::ReadChemObjectImpl().

## GetChemObject()

OBBase * GetChemObject | ( |  | ) |

Retrieve from internal array during output.

Retrieves an object stored by AddChemObject() during output.

Referenced by OBMoleculeFormat::WriteChemObjectImpl().

## IsLast()

bool IsLast | ( |  | ) |

True if no more objects to be output.

Referenced by OBMoleculeFormat::WriteChemObjectImpl().

## IsFirstInput()

bool IsFirstInput | ( |  | ) |

True if the first input object is being processed.

Referenced by OBMoleculeFormat::DeferMolOutput(), and OBMoleculeFormat::ReadChemObjectImpl().

## SetFirstInput()

void SetFirstInput | ( | bool | b = true | ) |

Setwhether or not is the first input.

Referenced by Convert(), and FullConvert().

## GetOutputIndex()

int GetOutputIndex | ( |  | ) | const

Retrieves number of ChemObjects that have been actually output.

Returns the number of objects which have been output or are currently being output. The outputindex is incremented when an object for output is fetched by GetChemObject(). So the function will return 1 if called from WriteMolecule() during output of the first object.

Referenced by OBMoleculeFormat::DoOutputOptions().

## SetOutputIndex()

void SetOutputIndex | ( | int | indx | ) |

Sets output index (maybe to control whether seen as first object).

Referenced by FullConvert(), OBMoleculeFormat::OutputDeferredMols(), and OBMoleculeFormat::WriteChemObjectImpl().

## SetMoreFilesToCome()

void SetMoreFilesToCome | ( |  | ) |

Used with multiple input files. Off by default.

Referenced by FullConvert().

## SetOneObjectOnly()

void SetOneObjectOnly | ( | bool | b = true | ) |

Used with multiple input files. Off by default.

Referenced by FullConvert(), OBMoleculeFormat::OutputDeferredMols(), SetLast(), and WriteString().

## SetLast()

void SetLast | ( | bool | b | ) |

inline

Synonym for SetOneObjectOnly().

## IsLastFile()

bool IsLastFile | ( |  | ) |

inline

True if no more files to be read.

## GetCount()

int GetCount | ( |  | ) | const

inline

Number of objects read and processed Incremented after options are processed, so 0 for first object. Returns -1 if Convert interface not used.

## GetDefaultFormat()

OBFormat * GetDefaultFormat | ( |  | ) |

inline static

The default format is set in a single OBFormat class (generally it is OBMol).

## Write()

bool Write | ( | OBBase * | pOb,
|  | std::ostream * | pout = nullptr )

Outputs an object of a class derived from OBBase.

Part of "API" interface. The output stream can be specified and the change is retained in the OBConversion instance

Writes the object pOb but does not delete it afterwards. The output stream is lastingly changed if pos is not NULL Returns true if successful.

Examples
  obconformersearch_default.cpp.

Referenced by WriteFile(), and WriteString().

## WriteString()

std::string WriteString | ( | OBBase * | pOb,
|  | bool | trimWhitespace = false )

Outputs an object of a class derived from OBBase as a string.

Part of "API" interface. The output stream is temporarily changed to the string and then restored This method is primarily intended for scripting languages without "stream" classes The optional "trimWhitespace" parameter allows trailing whitespace to be removed (e.g., in a SMILES string or InChI, etc.)

Writes the object pOb but does not delete it afterwards. The output stream not changed (since we cannot write to this string later) Returns true if successful.

Referenced by OBBuilder::Build(), and UniqueTautomerFunctor::operator()().

## WriteFile()

bool WriteFile | ( | OBBase * | pOb,
|  | std::string | filePath )

Outputs an object of a class derived from OBBase as a file (with the supplied path).

Part of "API" interface. The output stream is changed to the supplied file and the change is retained in the OBConversion instance. This method is primarily intended for scripting languages without "stream" classes

Writes the object pOb but does not delete it afterwards. The output stream is lastingly changed to point to the file Returns true if successful.

## CloseOutFile()

void CloseOutFile | ( |  | ) |

Manually closes and deletes the output stream The file is closed anyway when in the OBConversion destructor or when WriteFile is called again.

Since
  version 2.1

## Read()

bool Read | ( | OBBase * | pOb,
|  | std::istream * | pin = nullptr )

Reads an object of a class derived from OBBase into pOb.

Part of "API" interface. The input stream can be specified and the change is retained in the OBConversion instance

Returns
  false and pOb=NULL on error

Examples
  obconformersearch_default.cpp, and obforcefield_energy.cpp.

Referenced by ReadFile(), OBMoleculeFormat::ReadNameIndex(), and ReadString().

## ReadString()

bool ReadString | ( | OBBase * | pOb,
|  | std::string | input )

Reads an object of a class derived from OBBase into pOb from the supplied string.

Part of "API" interface. The input stream can be specified and the change is retained in the OBConversion instance

Returns
  NULL on error Part of "API" interface.
  false and pOb=NULL on error This method is primarily intended for scripting languages without "stream" classes Any existing input stream will be replaced by stringstream.

Examples
  obconversion_readstring.cpp.

Referenced by OpenBabel::CompileSmilesQuery().

## ReadFile()

bool ReadFile | ( | OBBase * | pOb,
|  | std::string | filePath )

Reads an object of a class derived from OBBase into pOb from the file specified.

Part of "API" interface. The output stream is changed to the supplied file and the change is retained in the OBConversion instance. For multi-molecule files, the remaining molecules can be read by repeatedly calling the Read() method.

Returns
  false and pOb=NULL on error This method is primarily intended for scripting languages without "stream" classes

## OpenInAndOutFiles()

bool OpenInAndOutFiles | ( | std::string | infilepath,
|  | std::string | outfilepath )

Part of the "Convert" interface. Open the files and update the streams in the OBConversion object. This method is primarily intended for scripting languages without "stream" classes and will usually followed by a call to Convert(). Will set format from file extension if format has not already been set. Files will be opened even if format cannot be determined, but not if file path is empty.

Returns
  false if unsuccessful.

Referenced by OBConversion().

## ReportNumberConverted()

void ReportNumberConverted | ( | int | count,
|  | OBFormat * | pFormat = nullptr )

Sends a message like "2 molecules converted" to clog The type of object is taken from the TargetClassDescription of the specified class (or the output format if not specified)and is appropriately singular or plural.

## NumInputObjects()

int NumInputObjects | ( |  | ) |

Returns
  the number of objects in the inputstream, or -1 if error or if SkipObjects for the input format is not implemented Adjusts for the value of -f and -l options (first and last objects).

## BatchFileName()

string BatchFileName | ( | std::string & | BaseName,
|  | std::string & | InFile )

static protected

Replaces * in BaseName by InFile without extension and path.

Referenced by FullConvert().

## IncrementedFileName()

string IncrementedFileName | ( | std::string & | BaseName,
|  | const int | Count )

static protected

Replaces * in BaseName by Count.

Referenced by FullConvert().

## CheckForUnintendedBatch()

bool CheckForUnintendedBatch | ( | const std::string & | infile,
|  | const std::string & | outfile )

static protected

Checks for misunderstandings when using the -m option.

Referenced by FullConvert().

## ClearInStreams()

void ClearInStreams | ( |  | ) |

protected

## SetStartAndEnd()

bool SetStartAndEnd | ( |  | ) |

protected

Referenced by Convert(), and Read().

## OptionParamArray()

OBConversion::OPAMapType & OptionParamArray | ( | Option_type | typ | ) |

static protected

Referenced by GetOptionParams(), and RegisterOptionParam().

## OpenAndSetFormat()

bool OpenAndSetFormat | ( | bool | SetFormat,
|  | std::ifstream * | is,
|  | std::stringstream * | ss = nullptr )

protected

Referenced by FullConvert().

## Member Data Documentation

## InFilename

std::string InFilename

protected

Referenced by FullConvert(), GetInFilename(), GetTitle(), OpenAndSetFormat(), OpenInAndOutFiles(), operator=(), and ReadFile().

## OutFilename

std::string OutFilename

protected

Referenced by FullConvert(), GetOutFilename(), and OpenInAndOutFiles().

## pInput

std::istream* pInput

protected

Referenced by AddChemObject(), Convert(), GetInStream(), OBConversion(), OBConversion(), operator=(), OBConversion::StreamState::popInput(), OBConversion::StreamState::pushInput(), Read(), and SetInStream().

## ownedInStreams

std::vector<std::istream *> ownedInStreams

protected

Referenced by OBConversion::StreamState::popInput(), OBConversion::StreamState::pushInput(), Read(), and SetInStream().

## pOutput

std::ostream* pOutput

protected

Referenced by FullConvert(), GetOutStream(), OBConversion(), OBConversion(), operator=(), OBConversion::StreamState::popOutput(), OBConversion::StreamState::pushOutput(), SetOutStream(), and Write().

## ownedOutStreams

std::vector<std::ostream *> ownedOutStreams

protected

Referenced by OBConversion::StreamState::popOutput(), OBConversion::StreamState::pushOutput(), and SetOutStream().

## pDefaultFormat

OBFormat* pDefaultFormat

static protected

## pInFormat

OBFormat* pInFormat

protected

Referenced by Convert(), Convert(), FullConvert(), GetInFormat(), OBConversion(), OBConversion(), OpenAndSetFormat(), OpenInAndOutFiles(), operator=(), Read(), ReadFile(), SetInFormat(), SetInFormat(), SetInStream(), and SetStartAndEnd().

## pOutFormat

OBFormat* pOutFormat

protected

Referenced by AddChemObject(), Convert(), GetOutFormat(), OBConversion(), OBConversion(), OpenInAndOutFiles(), operator=(), ReportNumberConverted(), SetOutFormat(), SetOutFormat(), Write(), WriteFile(), and WriteString().

## OptionsArray

std::map<std::string,std::string> OptionsArray[3]

protected

Referenced by AddOption(), CopyOptions(), GetOptions(), IsOption(), operator=(), RemoveOption(), and SetOptions().

## Index

int Index

protected

Referenced by AddChemObject(), Convert(), GetChemObject(), GetOutputIndex(), OBConversion(), OBConversion(), operator=(), SetOutputIndex(), Write(), WriteFile(), and WriteString().

## StartNumber

unsigned int StartNumber

protected

Referenced by AddChemObject(), Convert(), OBConversion(), OBConversion(), operator=(), and SetStartAndEnd().

## EndNumber

unsigned int EndNumber

protected

Referenced by AddChemObject(), Convert(), OBConversion(), OBConversion(), operator=(), Read(), and SetStartAndEnd().

## Count

int Count

protected

Referenced by AddChemObject(), Convert(), FullConvert(), GetCount(), IncrementedFileName(), OBConversion(), OBConversion(), operator=(), Read(), and SetStartAndEnd().

## m_IsFirstInput

bool m_IsFirstInput

protected

Referenced by IsFirstInput(), OBConversion(), OBConversion(), operator=(), and SetFirstInput().

## m_IsLast

bool m_IsLast

protected

Referenced by Convert(), IsLast(), OBConversion(), OBConversion(), operator=(), SetOneObjectOnly(), and WriteString().

## MoreFilesToCome

bool MoreFilesToCome

protected

Referenced by Convert(), IsLastFile(), OBConversion(), OBConversion(), operator=(), and SetMoreFilesToCome().

## OneObjectOnly

bool OneObjectOnly

protected

Referenced by Convert(), OBConversion(), OBConversion(), operator=(), SetOneObjectOnly(), and WriteString().

## ReadyToInput

bool ReadyToInput

protected

Referenced by AddChemObject(), Convert(), and operator=().

## SkippedMolecules

bool SkippedMolecules

protected

Referenced by OBConversion(), OBConversion(), operator=(), and Read().

## inFormatGzip

bool inFormatGzip

protected

skip molecules using -f and -l

Referenced by Convert(), FullConvert(), GetInGzipped(), OBConversion(), OBConversion(), OpenAndSetFormat(), OpenInAndOutFiles(), operator=(), Read(), ReadFile(), SetInFormat(), SetInFormat(), and SetInStream().

## outFormatGzip

bool outFormatGzip

protected

Referenced by GetOutGzipped(), OBConversion(), OBConversion(), OpenInAndOutFiles(), operator=(), SetOutFormat(), SetOutFormat(), SetOutStream(), and WriteFile().

## pOb1

OBBase* pOb1

protected

Referenced by AddChemObject(), Convert(), GetChemObject(), OBConversion(), OBConversion(), and operator=().

## wInpos

std::streampos wInpos

protected

position in the input stream of the object being written

Referenced by AddChemObject(), GetInPos(), OBConversion(), OBConversion(), and operator=().

## rInpos

std::streampos rInpos

protected

position in the input stream of the object being read

Referenced by AddChemObject(), Convert(), and operator=().

## wInlen

size_t wInlen

protected

length in the input stream of the object being written

Referenced by AddChemObject(), Convert(), GetInLen(), OBConversion(), OBConversion(), and operator=().

## rInlen

size_t rInlen

protected

length in the input stream of the object being read

Referenced by AddChemObject(), and operator=().

## pAuxConv

OBConversion* pAuxConv

protected

Way to extend OBConversion.

Referenced by GetAuxConv(), OBConversion(), OBConversion(), operator=(), SetAuxConv(), and ~OBConversion().

## SupportedInputFormat

std::vector<std::string> SupportedInputFormat

protected

list of supported input format

## SupportedOutputFormat

std::vector<std::string> SupportedOutputFormat

protected

list of supported output format

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

- obconversion.h
- obconversion.cpp