RDCode: MolWriters.h Source File

# MolWriters.h

Go to the documentation of this file.

    00001 //
    00002 //  Copyright (C) 2002-2006 Rational Discovery LLC
    00003 //
    00004 //   @@ All Rights Reserved  @@
    00005 //
    00006
    00007 #ifndef _RD_MOLWRITERS_H_
    00008 #define _RD_MOLWRITERS_H_
    00009
    00010 #include <RDGeneral/types.h>
    00011
    00012 #include <string>
    00013 #include <iostream>
    00014 #include <GraphMol/ROMol.h>
    00015
    00016 namespace RDKit {
    00017
    00018   static int defaultConfId=-1;
    00019   class MolWriter {
    00020   public:
    00021     virtual ~MolWriter() {}
    00022     virtual void write(ROMol &mol,int confId=defaultConfId) = 0;
    00023     virtual void flush() = 0;
    00024     virtual void setProps(const STR_VECT &propNames)=0;
    00025     virtual unsigned int numMols() const =0;
    00026   };
    00027
    00028   //! The SmilesWriter is for writing molecules and properties to
    00029   //! delimited text files.
    00030   class SmilesWriter : public MolWriter {
    00031     /******************************************************************************
    00032      * A Smiles Table writer - this is how it is used
    00033      *  - create a SmilesWriter with a output file name (or a ostream), a delimiter,
    00034      *     and a list of properties that need to be written out
    00035      *  - then a call is made to the write function for each molecule that needs to
    00036      *     be written out
    00037      ******************************************************************************/
    00038   public:
    00039     /*!
    00040       \param fileName       : filename to write to
    00041       \param delimiter      : delimiter to use in the text file
    00042       \param nameHeader     : used to label the name column in the output. If this
    00043                               is provided as the empty string, no names will be written.
    00044       \param includeHeader  : toggles inclusion of a header line in the output
    00045       \param isomericSmiles : toggles generation of isomeric SMILES
    00046       \param kekuleSmiles   : toggles the generation of kekule SMILES
    00047
    00048      */
    00049     SmilesWriter(std::string fileName,
    00050                  std::string delimiter=" ",
    00051                  std::string nameHeader="Name",
    00052                  bool includeHeader=true,
    00053                  bool isomericSmiles=false,
    00054                  bool kekuleSmiles=false);
    00055     //! \overload
    00056     SmilesWriter(std::ostream *outStream,
    00057                  std::string delimiter=" ",
    00058                  std::string nameHeader="Name",
    00059                  bool includeHeader=true,
    00060                  bool takeOwnership=false,
    00061                  bool isomericSmiles=false,
    00062                  bool kekuleSmiles=false);
    00063
    00064     ~SmilesWriter();
    00065
    00066     //! \brief set a vector of property names that are need to be
    00067     //! written out for each molecule
    00068     void setProps(const STR_VECT &propNames);
    00069
    00070     //! \brief write a new molecule to the file
    00071     void write(ROMol &mol,int confId=defaultConfId);
    00072
    00073     //! \brief flush the ostream
    00074     void flush() {
    00075       PRECONDITION(dp_ostream,"no output stream");
    00076       dp_ostream->flush();
    00077     };
    00078
    00079     //! \brief get the number of molecules written so far
    00080     unsigned int numMols() const { return d_molid;} ;
    00081
    00082   private:
    00083     // local initialization
    00084     void init(std::string delimiter,std::string nameHeader,
    00085               bool includeHeader,
    00086               bool isomericSmiles,
    00087               bool kekuleSmiles);
    00088
    00089
    00090     // dumps a header line to the output stream
    00091     void dumpHeader() const;
    00092
    00093
    00094     std::ostream *dp_ostream;
    00095     bool df_owner;
    00096     bool df_includeHeader; // whether or not to include a title line
    00097     unsigned int d_molid; // the number of the molecules we wrote so far
    00098     std::string d_delim; // delimiter string between various records
    00099     std::string d_nameHeader; // header for the name column in the output file
    00100     STR_VECT d_props; // list of property name that need to be written out
    00101     bool df_isomericSmiles; // whether or not to do isomeric smiles
    00102     bool df_kekuleSmiles; // whether or not to do kekule smiles
    00103   };
    00104
    00105
    00106   //! The SDWriter is for writing molecules and properties to
    00107   //! SD files
    00108   class SDWriter : public MolWriter {
    00109     /**************************************************************************************
    00110      * A SD file ( or stream) writer - this is how it is used
    00111      *  - create a SDMolWriter with a output file name (or a ostream),
    00112      *     and a list of properties that need to be written out
    00113      *  - then a call is made to the write function for each molecule that needs to be written out
    00114      **********************************************************************************************/
    00115   public:
    00116     SDWriter(std::string fileName);
    00117     SDWriter(std::ostream *outStream,bool takeOwnership=false);
    00118
    00119     ~SDWriter();
    00120
    00121     //! \brief set a vector of property names that are need to be
    00122     //! written out for each molecule
    00123     void setProps(const STR_VECT &propNames);
    00124
    00125     //! \brief write a new molecule to the file
    00126     void write(ROMol &mol, int confId=defaultConfId);
    00127
    00128     //! \brief flush the ostream
    00129     void flush() {
    00130       PRECONDITION(dp_ostream,"no output stream");
    00131       dp_ostream->flush();
    00132     } ;
    00133
    00134     //! \brief get the number of molecules written so far
    00135     unsigned int numMols() const { return d_molid; };
    00136
    00137   private:
    00138     void writeProperty(const ROMol &mol, std::string name);
    00139
    00140     std::ostream *dp_ostream;
    00141     bool d_owner;
    00142     unsigned int d_molid; // the number of the molecules we wrote so far
    00143     STR_VECT d_props; // list of property name that need to be written out
    00144   };
    00145
    00146   //! The TDTWriter is for writing molecules and properties to
    00147   //! TDT files
    00148   class TDTWriter : public MolWriter {
    00149     /**************************************************************************************
    00150      * A TDT file ( or stream) writer - this is how it is used
    00151      *  - create a TDTWriter with a output file name (or a ostream),
    00152      *     and a list of properties that need to be written out
    00153      *  - then a call is made to the write function for each molecule that needs to be written out
    00154      **********************************************************************************************/
    00155   public:
    00156     TDTWriter(std::string fileName);
    00157     TDTWriter(std::ostream *outStream,bool takeOwnership=false);
    00158
    00159     ~TDTWriter();
    00160
    00161     //! \brief set a vector of property names that are need to be
    00162     //! written out for each molecule
    00163     void setProps(const STR_VECT &propNames);
    00164
    00165     //! \brief write a new molecule to the file
    00166     void write(ROMol &mol, int confId=defaultConfId);
    00167
    00168     //! \brief flush the ostream
    00169     void flush() {
    00170       PRECONDITION(dp_ostream,"no output stream");
    00171       dp_ostream->flush();
    00172     };
    00173
    00174     //! \brief get the number of molecules written so far
    00175     unsigned int numMols() const { return d_molid; };
    00176
    00177     void setWrite2D(bool state=true) { df_write2D=state; };
    00178     bool getWrite2D() const { return df_write2D; };
    00179
    00180     void setWriteNames(bool state=true) { df_writeNames=state; };
    00181     bool getWriteNames() const { return df_writeNames; };
    00182
    00183     void setNumDigits(unsigned int numDigits) { d_numDigits=numDigits; };
    00184     unsigned int getNumDigits() const { return d_numDigits;};
    00185
    00186   private:
    00187     void writeProperty(const ROMol &mol, std::string name);
    00188
    00189     std::ostream *dp_ostream;
    00190     bool d_owner;
    00191     unsigned int d_molid; // the number of molecules we wrote so far
    00192     STR_VECT d_props; // list of property name that need to be written out
    00193     bool df_write2D; // write 2D coordinates instead of 3D
    00194     bool df_writeNames; // write a name record for each molecule
    00195     unsigned int d_numDigits; // number of digits to use in our output of coordinates;
    00196   };
    00197
    00198 }
    00199
    00200 #endif
    00201