RDCode: MolWriters.h Source File

- Main Page
- Namespaces
- Classes
- Files
- Directories

# 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   class SmilesWriter : public MolWriter {
    00029     /******************************************************************************
    00030      * A Smiles Table writer - this is how it is used
    00031      *  - create a SDMolWriter with a output file name (or a ostream), a delimiter,
    00032      *     and a list of properties that need to be written out
    00033      *  - then a call is made to the write function for each molecule that needs to
    00034      *     be written out
    00035      ******************************************************************************/
    00036   public:
    00037     SmilesWriter(std::string fileName,
    00038                  std::string delimiter=" ",
    00039                  std::string nameHeader="Name",
    00040                  bool includeHeader=true);
    00041     SmilesWriter(std::ostream *outStream,
    00042                  std::string delimiter=" ",
    00043                  std::string nameHeader="Name",
    00044                  bool includeHeader=true,
    00045                  bool takeOwnership=false);
    00046
    00047     ~SmilesWriter();
    00048
    00049     // FIX; unfornately we cannot handle this through he constructor for now
    00050     // because of the conversion issues betweem python list of strings and
    00051     // STR_VECT - this is currently handled through a wrapper function and I do not
    00052     // know how to wrap a constructor.
    00053     // set a vector of property names that are need to be
    00054     // written out for each molecule
    00055     void setProps(const STR_VECT &propNames);
    00056
    00057     // write a new molecule to the file
    00058     // NOTE: the default value for the confId parameter is set in the
    00059     //   baseClass, do *not* redefine it.
    00060     void write(ROMol &mol,int confId=defaultConfId);
    00061
    00062     // flush the ostream
    00063     void flush() {
    00064       PRECONDITION(dp_ostream,"no output stream");
    00065       dp_ostream->flush();
    00066     };
    00067
    00068     // get the number of molecules written so far
    00069     unsigned int numMols() const { return d_molid;} ;
    00070
    00071   private:
    00072     // local initialization
    00073     void init(std::string delimiter,std::string nameHeader,
    00074                bool includeHeader);
    00075
    00076
    00077     // dumps a header line to the output stream
    00078     void dumpHeader() const;
    00079
    00080
    00081     std::ostream *dp_ostream;
    00082     bool d_owner;
    00083     bool d_includeHeader; // whether or not to include a title line
    00084     unsigned int d_molid; // the number of the molecules we wrote so far
    00085     std::string d_delim; // delimiter string between various records
    00086     std::string d_nameHeader; // header for the name column in the output file
    00087     STR_VECT d_props; // list of property name that need to be written out
    00088   };
    00089
    00090
    00091   class SDWriter : public MolWriter {
    00092     /**************************************************************************************
    00093      * A SD file ( or stream) writer - this is how it is used
    00094      *  - create a SDMolWriter with a output file name (or a ostream),
    00095      *     and a list of properties that need to be written out
    00096      *  - then a call is made to the write function for each molecule that needs to be written out
    00097      **********************************************************************************************/
    00098   public:
    00099     SDWriter(std::string fileName);
    00100     SDWriter(std::ostream *outStream,bool takeOwnership=false);
    00101
    00102     ~SDWriter();
    00103
    00104     // FIX; unfornately we cannot handle this through he constructor for now
    00105     // because of the conversion issues betweem python list of strings and
    00106     // STR_VECT - this is currently handled through a wrapper function and I do not
    00107     // know how to wrap a constructor.
    00108     // set a vector of property names that are need to be
    00109     // written out for each molecule
    00110     void setProps(const STR_VECT &propNames);
    00111
    00112     // write a new molecule to the file
    00113     // NOTE: the default value for the confId parameter is set in the
    00114     //   baseClass, do *not* redefine it.
    00115     void write(ROMol &mol, int confId=defaultConfId);
    00116
    00117     // flush the ostream
    00118     void flush() {
    00119       PRECONDITION(dp_ostream,"no output stream");
    00120       dp_ostream->flush();
    00121     } ;
    00122
    00123     // get the number of molecules written so far
    00124     unsigned int numMols() const { return d_molid; };
    00125
    00126   private:
    00127     void writeProperty(const ROMol &mol, std::string name);
    00128
    00129     std::ostream *dp_ostream;
    00130     bool d_owner;
    00131     unsigned int d_molid; // the number of the molecules we wrote so far
    00132     STR_VECT d_props; // list of property name that need to be written out
    00133   };
    00134
    00135   class TDTWriter : public MolWriter {
    00136     /**************************************************************************************
    00137      * A TDT file ( or stream) writer - this is how it is used
    00138      *  - create a TDTWriter with a output file name (or a ostream),
    00139      *     and a list of properties that need to be written out
    00140      *  - then a call is made to the write function for each molecule that needs to be written out
    00141      **********************************************************************************************/
    00142   public:
    00143     TDTWriter(std::string fileName);
    00144     TDTWriter(std::ostream *outStream,bool takeOwnership=false);
    00145
    00146     ~TDTWriter();
    00147
    00148     // FIX; unfornately we cannot handle this through he constructor for now
    00149     // because of the conversion issues betweem python list of strings and
    00150     // STR_VECT - this is currently handled through a wrapper function and I do not
    00151     // know how to wrap a constructor.
    00152     // set a vector of property names that are need to be
    00153     // written out for each molecule
    00154     void setProps(const STR_VECT &propNames);
    00155
    00156     // write a new molecule to the file
    00157     // NOTE: the default value for the confId parameter is set in the
    00158     //   baseClass, do *not* redefine it.
    00159     void write(ROMol &mol, int confId=defaultConfId);
    00160
    00161     // flush the ostream
    00162     void flush() {
    00163       PRECONDITION(dp_ostream,"no output stream");
    00164       dp_ostream->flush();
    00165     };
    00166
    00167     // get the number of molecules written so far
    00168     unsigned int numMols() const { return d_molid; };
    00169
    00170     void setWrite2D(bool state=true) { df_write2D=state; };
    00171     bool getWrite2D() const { return df_write2D; };
    00172
    00173     void setWriteNames(bool state=true) { df_writeNames=state; };
    00174     bool getWriteNames() const { return df_writeNames; };
    00175
    00176     void setNumDigits(unsigned int numDigits) { d_numDigits=numDigits; };
    00177     unsigned int getNumDigits() const { return d_numDigits;};
    00178
    00179   private:
    00180     void writeProperty(const ROMol &mol, std::string name);
    00181
    00182     std::ostream *dp_ostream;
    00183     bool d_owner;
    00184     unsigned int d_molid; // the number of molecules we wrote so far
    00185     STR_VECT d_props; // list of property name that need to be written out
    00186     bool df_write2D; // write 2D coordinates instead of 3D
    00187     bool df_writeNames; // write a name record for each molecule
    00188     unsigned int d_numDigits; // number of digits to use in our output of coordinates;
    00189   };
    00190
    00191 }
    00192
    00193 #endif
    00194