1 /**********************************************************************
2 molreport.cpp - Report information about the molecule:
3      atom list, bond list, etc.
4 
5 Copyright (C) 2006 by Geoffrey R. Hutchison
6 
7 This file is part of the Open Babel project.
8 For more information, see <http://openbabel.org/>
9 
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation version 2 of the License.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 ***********************************************************************/
19 
20 #include <openbabel/babelconfig.h>
21 #include <openbabel/obmolecformat.h>
22 #include <openbabel/mol.h>
23 #include <openbabel/atom.h>
24 #include <openbabel/bond.h>
25 #include <openbabel/obiter.h>
26 #include <openbabel/elements.h>
27 
28 using namespace std;
29 namespace OpenBabel
30 {
31 
32   class MolReportFormat : public OBMoleculeFormat
33   {
34   public:
35     //Register this format type ID
MolReportFormat()36     MolReportFormat()
37     {
38       OBConversion::RegisterFormat("molreport",this);
39     }
40 
Description()41     virtual const char* Description() //required
42     {
43       return
44         "Open Babel molecule report\n"
45         "Generates a summary of the atoms and bonds in a molecule\n"
46         "Example output::\n\n"
47         " TITLE: Ethanol.mopout\n"
48 " FORMULA: C2H6O\n"
49 " MASS: 46.0684\n"
50 " ATOM:         1   C TYPE: C3     HYB:  3 CHARGE:  -0.2151\n"
51 " ATOM:         2   C TYPE: C3     HYB:  3 CHARGE:  -0.0192\n"
52 " ATOM:         3   O TYPE: O3     HYB:  3 CHARGE:  -0.3295\n"
53 " ATOM:         4   H TYPE: HC     HYB:  0 CHARGE:   0.0771\n"
54 " ATOM:         5   H TYPE: HC     HYB:  0 CHARGE:   0.0873\n"
55 " ATOM:         6   H TYPE: HC     HYB:  0 CHARGE:   0.0874\n"
56 " ATOM:         7   H TYPE: HC     HYB:  0 CHARGE:   0.0577\n"
57 " ATOM:         8   H TYPE: HC     HYB:  0 CHARGE:   0.0577\n"
58 " ATOM:         9   H TYPE: HC     HYB:  0 CHARGE:   0.1966\n"
59 " BOND:         0 START:         8 END:         2 ORDER:   1\n"
60 " BOND:         1 START:         6 END:         1 ORDER:   1\n"
61 " BOND:         2 START:         1 END:         2 ORDER:   1\n"
62 " BOND:         3 START:         1 END:         4 ORDER:   1\n"
63 " BOND:         4 START:         1 END:         5 ORDER:   1\n"
64 " BOND:         5 START:         2 END:         3 ORDER:   1\n"
65 " BOND:         6 START:         2 END:         7 ORDER:   1\n"
66 " BOND:         7 START:         3 END:         9 ORDER:   1\n\n"
67 
68 ".. seealso::\n\n"
69 
70 " :ref:`Open_Babel_report_format`\n\n"
71 
72 ;
73     };
74 
SpecificationURL()75     virtual const char* SpecificationURL()
76     {return "http://openbabel.org/wiki/MolReport";}; //optional
77 
78     //Flags() can return be any the following combined by | or be omitted if none apply
79     // NOTREADABLE  READONEONLY  NOTWRITABLE  WRITEONEONLY
Flags()80     virtual unsigned int Flags()
81     {
82       return NOTREADABLE;
83     };
84 
85     ////////////////////////////////////////////////////
86     /// The "API" interface functions
87     virtual bool WriteMolecule(OBBase* pOb, OBConversion* pConv);
88   };
89 
90   //Make an instance of the format class
91   MolReportFormat theMolReportFormat;
92 
93   ////////////////////////////////////////////////////////////////
94 
WriteMolecule(OBBase * pOb,OBConversion * pConv)95   bool MolReportFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
96   {
97     OBMol* pmol = dynamic_cast<OBMol*>(pOb);
98     if (pmol == nullptr)
99       return false;
100 
101     //Define some references so we can use the old parameter names
102     ostream &ofs = *pConv->GetOutStream();
103     OBMol &mol = *pmol;
104 
105     char buffer[BUFF_SIZE];
106     ofs << "TITLE: " << mol.GetTitle() << "\n";
107     ofs << "FORMULA: " << mol.GetFormula() << "\n";
108     ofs << "MASS: ";
109     snprintf(buffer, BUFF_SIZE, "%5.4f\n", mol.GetMolWt());
110     ofs << buffer;
111 
112     if (mol.GetTotalCharge() != 0)
113       {
114         ofs << "TOTAL CHARGE: ";
115         snprintf(buffer, BUFF_SIZE, "%d", mol.GetTotalCharge());
116         ofs << buffer << "\n";
117       }
118     if (mol.GetTotalSpinMultiplicity() != 1)
119       {
120         ofs << "TOTAL SPIN: ";
121         snprintf(buffer, BUFF_SIZE, "%d", mol.GetTotalSpinMultiplicity());
122         ofs << buffer << "\n";
123       }
124 
125     FOR_ATOMS_OF_MOL(atom, mol)
126       {
127         snprintf(buffer, BUFF_SIZE, "ATOM: %9d %3s TYPE: %-6s HYB: %2d CHARGE: %8.4f",
128                  atom->GetIdx(),
129                  OBElements::GetSymbol(atom->GetAtomicNum()),
130                  atom->GetType(),
131                  atom->GetHyb(),
132                  atom->GetPartialCharge());
133         ofs << buffer << "\n";
134       }
135 
136     FOR_BONDS_OF_MOL(bond, mol)
137       {
138         snprintf(buffer, BUFF_SIZE, "BOND: %9d START: %9d END: %9d ORDER: %3d",
139                  bond->GetIdx(),
140                  bond->GetBeginAtomIdx(),
141                  bond->GetEndAtomIdx(),
142                  bond->GetBondOrder());
143         ofs << buffer << "\n";
144       }
145 
146     return(true);
147   }
148 
149 } //namespace OpenBabel
150