| 1 | /********************************************************************** | 
| 2 | obmolecformat.h - Subclass of OBFormat for conversion of OBMol. | 
| 3 |  | 
| 4 | Copyright (C) 2005 Chris Morley | 
| 5 |  | 
| 6 | This file is part of the Open Babel project. | 
| 7 | For more information, see <http://openbabel.sourceforge.net/> | 
| 8 |  | 
| 9 | This program is free software; you can redistribute it and/or modify | 
| 10 | it under the terms of the GNU General Public License as published by | 
| 11 | the Free Software Foundation version 2 of the License. | 
| 12 |  | 
| 13 | This program is distributed in the hope that it will be useful, | 
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 16 | GNU General Public License for more details. | 
| 17 | ***********************************************************************/ | 
| 18 |  | 
| 19 | #ifndef OB_MOLECULEFORMAT_H | 
| 20 | #define OB_MOLECULEFORMAT_H | 
| 21 |  | 
| 22 | #include "mol.hpp" | 
| 23 | #include "obconversion.hpp" | 
| 24 |  | 
| 25 | namespace OpenBabel { | 
| 26 |  | 
| 27 | //! \brief An OBFormat convenience subclass for conversion to/from OBMol data | 
| 28 | //! | 
| 29 | //! An OBFormat which converts to and/or from OBMol can derive from this class | 
| 30 | //! to save duplicating the ReadChemObject() and/or WriteChemObject() methods. | 
| 31 | //! Derive directly from OBFormat if the object converted is not OBMol or | 
| 32 | //! if interaction with the framework is required during the execution | 
| 33 | //! of ReadMolecule() or WriteMolecule(), as for example in CMLFormat | 
| 34 | class OBMoleculeFormat : public OBFormat | 
| 35 | { | 
| 36 | public: | 
| 37 |  | 
| 38 | OBMoleculeFormat() | 
| 39 | { | 
| 40 | OBConversion::RegisterOptionParam("b", this, 0, OBConversion::INOPTIONS); | 
| 41 | OBConversion::RegisterOptionParam("s", this, 0, OBConversion::INOPTIONS); | 
| 42 | //The follow are OBMol options, which should not be in OBConversion. | 
| 43 | //But here isn't entirely appropriate either, since could have | 
| 44 | //OBMol formats loaded but none of them derived from this class. | 
| 45 | //However, this possibility is remote. | 
| 46 | OBConversion::RegisterOptionParam("s", NULL, 1,OBConversion::GENOPTIONS); | 
| 47 | OBConversion::RegisterOptionParam("v", NULL, 1,OBConversion::GENOPTIONS); | 
| 48 | OBConversion::RegisterOptionParam("h", NULL, 0,OBConversion::GENOPTIONS); | 
| 49 | OBConversion::RegisterOptionParam("d", NULL, 0,OBConversion::GENOPTIONS); | 
| 50 | OBConversion::RegisterOptionParam("b", NULL, 0,OBConversion::GENOPTIONS); | 
| 51 | OBConversion::RegisterOptionParam("c", NULL, 0,OBConversion::GENOPTIONS); | 
| 52 | OBConversion::RegisterOptionParam("p", NULL, 0,OBConversion::GENOPTIONS); | 
| 53 | OBConversion::RegisterOptionParam("t", NULL, 0,OBConversion::GENOPTIONS); | 
| 54 | OBConversion::RegisterOptionParam("j", NULL, 0,OBConversion::GENOPTIONS); | 
| 55 | }; | 
| 56 |  | 
| 57 | /// The "Convert" interface functions | 
| 58 | virtual bool ReadChemObject(OBConversion* pConv) | 
| 59 | { | 
| 60 | std::istream &ifs = *pConv->GetInStream(); | 
| 61 | if (ifs.peek() == EOF || !ifs.good()) | 
| 62 | return false; | 
| 63 |  | 
| 64 | static OBMol* pmol; | 
| 65 |  | 
| 66 | std::string auditMsg = "OpenBabel::Read molecule "; | 
| 67 | std::string description(Description()); | 
| 68 | auditMsg += description.substr(0,description.find('\n')); | 
| 69 | obErrorLog.ThrowError(__func__, | 
| 70 | auditMsg, | 
| 71 | obAuditMsg); | 
| 72 |  | 
| 73 | //With j option, reuse pmol except for the first mol | 
| 74 | if(!pConv->IsOption("j",OBConversion::GENOPTIONS) || pConv->IsFirstInput()) | 
| 75 | pmol = new OBMol; | 
| 76 |  | 
| 77 | bool ret=ReadMolecule(pmol,pConv); | 
| 78 | if(ret && pmol->NumAtoms() > 0) //Do transformation and return molecule | 
| 79 | pConv->AddChemObject(pmol->DoTransformations(pConv->GetOptions(OBConversion::GENOPTIONS))); | 
| 80 | else | 
| 81 | pConv->AddChemObject(NULL); | 
| 82 |  | 
| 83 | return ret; | 
| 84 | }; | 
| 85 |  | 
| 86 | virtual bool WriteChemObject(OBConversion* pConv) | 
| 87 | { | 
| 88 | //Retrieve the target OBMol | 
| 89 | OBBase* pOb = pConv->GetChemObject(); | 
| 90 | OBMol* pmol = dynamic_cast<OBMol*> (pOb); | 
| 91 | bool ret=false; | 
| 92 | if(pmol) | 
| 93 | { | 
| 94 | if(pmol->NumAtoms()==0) | 
| 95 | { | 
| 96 | std::string auditMsg = "OpenBabel::Molecule "; | 
| 97 | auditMsg += pmol->GetTitle(); | 
| 98 | auditMsg += " has 0 atoms"; | 
| 99 | obErrorLog.ThrowError(__func__, | 
| 100 | auditMsg, | 
| 101 | obInfo); | 
| 102 | } | 
| 103 | ret=true; | 
| 104 |  | 
| 105 | std::string auditMsg = "OpenBabel::Write molecule "; | 
| 106 | std::string description(Description()); | 
| 107 | auditMsg += description.substr(0,description.find('\n')); | 
| 108 | obErrorLog.ThrowError(__func__, | 
| 109 | auditMsg, | 
| 110 | obAuditMsg); | 
| 111 |  | 
| 112 | if(!pConv->IsOption("j",OBConversion::GENOPTIONS) || pConv->IsLast()) //With j option, output only at end | 
| 113 | { | 
| 114 | ret=WriteMolecule(pmol,pConv); | 
| 115 | delete pOb; | 
| 116 | } | 
| 117 | } | 
| 118 | return ret; | 
| 119 | }; | 
| 120 |  | 
| 121 | const std::type_info& GetType() | 
| 122 | { | 
| 123 | return typeid(OBMol*); | 
| 124 | }; | 
| 125 |  | 
| 126 | }; | 
| 127 |  | 
| 128 | } | 
| 129 | #endif //OB_MOLECULEFORMAT_H | 
| 130 |  | 
| 131 | //! \file obmolecformat.h | 
| 132 | //! \brief Subclass of OBFormat for conversion of OBMol. |