obmolecformat.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 obmolecformat.h - Subclass of OBFormat for conversion of OBMol.
00003 
00004 Copyright (C) 2005 Chris Morley
00005 
00006 This file is part of the Open Babel project.
00007 For more information, see <http://openbabel.sourceforge.net/>
00008 
00009 This program is free software; you can redistribute it and/or modify
00010 it under the terms of the GNU General Public License as published by
00011 the Free Software Foundation version 2 of the License.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 ***********************************************************************/
00018 
00019 #ifndef OB_MOLECULEFORMAT_H
00020 #define OB_MOLECULEFORMAT_H
00021 
00022 #include "mol.h"
00023 #include "obconversion.h"
00024 
00025 namespace OpenBabel {
00026 
00034 class OBMoleculeFormat : public OBFormat
00035 {
00036 public:
00037 
00038         OBMoleculeFormat()
00039         {
00040                 OBConversion::RegisterOptionParam("b", this, 0, OBConversion::INOPTIONS);
00041                 OBConversion::RegisterOptionParam("s", this, 0, OBConversion::INOPTIONS);
00042                 //The follow are OBMol options, which should not be in OBConversion.
00043                 //But here isn't entirely appropriate either, since could have
00044                 //OBMol formats loaded but none of them derived from this class.
00045                 //However, this possibility is remote.
00046                 OBConversion::RegisterOptionParam("s", NULL, 1,OBConversion::GENOPTIONS);
00047                 OBConversion::RegisterOptionParam("v", NULL, 1,OBConversion::GENOPTIONS);
00048                 OBConversion::RegisterOptionParam("h", NULL, 0,OBConversion::GENOPTIONS);
00049                 OBConversion::RegisterOptionParam("d", NULL, 0,OBConversion::GENOPTIONS);
00050                 OBConversion::RegisterOptionParam("b", NULL, 0,OBConversion::GENOPTIONS);
00051                 OBConversion::RegisterOptionParam("c", NULL, 0,OBConversion::GENOPTIONS);
00052                 OBConversion::RegisterOptionParam("p", NULL, 0,OBConversion::GENOPTIONS); 
00053                 OBConversion::RegisterOptionParam("t", NULL, 0,OBConversion::GENOPTIONS);
00054                 OBConversion::RegisterOptionParam("j", NULL, 0,OBConversion::GENOPTIONS);
00055         };
00056 
00058         virtual bool ReadChemObject(OBConversion* pConv)
00059         {
00060           std::istream &ifs = *pConv->GetInStream();
00061           if (ifs.peek() == EOF || !ifs.good())
00062             return false;
00063 
00064                 static OBMol* pmol;
00065 
00066                     std::string auditMsg = "OpenBabel::Read molecule ";
00067                     std::string description(Description());
00068                     auditMsg += description.substr(0,description.find('\n'));
00069                     obErrorLog.ThrowError(__FUNCTION__,
00070                                           auditMsg,
00071                                           obAuditMsg);
00072 
00073                 //With j option, reuse pmol except for the first mol
00074                 if(!pConv->IsOption("j",OBConversion::GENOPTIONS) || pConv->IsFirstInput())
00075                         pmol = new OBMol;
00076                 
00077                 bool ret=ReadMolecule(pmol,pConv);
00078                 if(ret && pmol->NumAtoms() > 0) //Do transformation and return molecule
00079                         pConv->AddChemObject(pmol->DoTransformations(pConv->GetOptions(OBConversion::GENOPTIONS)));
00080                 else
00081                         pConv->AddChemObject(NULL);
00082 
00083                 return ret;
00084         };
00085         
00086         virtual bool WriteChemObject(OBConversion* pConv)
00087         {
00088                 //Retrieve the target OBMol
00089                 OBBase* pOb = pConv->GetChemObject();
00090                 OBMol* pmol = dynamic_cast<OBMol*> (pOb);
00091                 bool ret=false;
00092                 if(pmol)
00093                 {       
00094                         if(pmol->NumAtoms()==0)
00095                         {
00096                                 std::string auditMsg = "OpenBabel::Molecule ";
00097                                 auditMsg += pmol->GetTitle();
00098                                 auditMsg += " has 0 atoms";
00099                                 obErrorLog.ThrowError(__FUNCTION__,
00100                                                 auditMsg,
00101                                                 obInfo);
00102                         }
00103                         ret=true;
00104 
00105                         std::string auditMsg = "OpenBabel::Write molecule ";
00106                         std::string description(Description());
00107                         auditMsg += description.substr(0,description.find('\n'));
00108                         obErrorLog.ThrowError(__FUNCTION__,
00109                                               auditMsg,
00110                                               obAuditMsg);
00111 
00112                         if(!pConv->IsOption("j",OBConversion::GENOPTIONS) || pConv->IsLast()) //With j option, output only at end
00113                         {
00114                                 ret=WriteMolecule(pmol,pConv);
00115                                 delete pOb;
00116                         }
00117                 }
00118                 return ret;
00119         };
00120 
00121         const std::type_info& GetType()
00122         {
00123                 return typeid(OBMol*);
00124         };
00125 
00126 };
00127 
00128 }
00129 #endif //OB_MOLECULEFORMAT_H
00130