ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/openbabel/oopseformat.cpp
(Generate patch)

Comparing trunk/OOPSE-2.0/src/openbabel/oopseformat.cpp (file contents):
Revision 2440 by tim, Wed Nov 16 19:42:11 2005 UTC vs.
Revision 2469 by tim, Fri Dec 2 15:38:03 2005 UTC

# Line 13 | Line 13 | GNU General Public License for more details.
13   GNU General Public License for more details.
14   ***********************************************************************/
15  
16 < #include <set>
17 <
18 < #include "mol.hpp"
19 < #include "obconversion.hpp"
20 < #include "obmolecformat.hpp"
21 <
22 < #ifdef HAVE_SSTREAM
23 < #include <sstream>
24 < #else
25 < #include <strstream>
26 < #endif
27 <
28 < using namespace std;
16 > #include "oopseformat.hpp"
17 > #include <fstream>
18   namespace OpenBabel
19   {
20  
32 class OOPSEFormat : public OBMoleculeFormat
33 {
34 public:
35  //Register this format type ID
36  OOPSEFormat()
37  {
38    OBConversion::RegisterFormat("in", this, "chemical/x-in");
39  }
40
41  virtual const char* Description() //required
42  {
43    return
44      "oopse cartesian coordinates format\n";
45  };
46
47  virtual const char* SpecificationURL()
48  { return "";}; //optional
49
50  virtual const char* GetMIMEType()
51  { return "chemical/x-in"; };
52
53   virtual unsigned int Flags() { return WRITEONEONLY;}
54
55  //*** This section identical for most OBMol conversions ***
56  ////////////////////////////////////////////////////
57  /// The "API" interface functions
58  //virtual bool ReadMolecule(OBBase* pOb, OBConversion* pConv);
59  virtual bool WriteMolecule(OBBase* pOb, OBConversion* pConv);
60
61  private:
62      bool AreSameFragments(OBMol& mol, vector<int>& frag1, vector<int>& frag2);
63      void findAngles(OBMol& mol);
64      OBMol* createMolFromFragment(OBMol& mol, vector<int>& fragment);
65      void WriteMDFile(vector<OBMol*> mols, vector<int> numMols);
66      void WriteINFile(OBMol& mol, ostream& ofs, vector<int>& indices);
67 };
68
69
70 //Make an instance of the format class
71 OOPSEFormat theOOPSEFormat;
72
73 ////////////////////////////////////////////////////////////////
74
21   bool OOPSEFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
22   {
23      OBMol* pmol = dynamic_cast<OBMol*>(pOb);
# Line 120 | Line 66 | bool OOPSEFormat::WriteMolecule(OBBase* pOb, OBConvers
66          numMols.push_back((*i).size());
67      }
68  
69 <    WriteMDFile(mdMols, numMols);    
69 >    string OutputFileName = pConv->GetInFilename();
70 >    unsigned int pos = OutputFileName.rfind(".");
71 >    if(pos==string::npos)
72 >        OutputFileName += ".md";
73 >    else
74 >        OutputFileName = OutputFileName.substr(0, pos) + ".md";      
75 >    ofstream ofs(OutputFileName.c_str());
76 >    if(!ofs)
77 >    {
78 >         cerr << "Cannot write to " << OutputFileName <<endl;
79 >         return false;
80 >    }
81 >                
82 >    WriteMDFile(mdMols, numMols, ofs);    
83  
84      for(vector<OBMol*>::iterator  i = mdMols.begin(); i != mdMols.end(); ++i)
85      {
# Line 142 | Line 101 | bool OOPSEFormat::AreSameFragments(OBMol& mol, vector<
101          return false;
102      }
103  
104 <    //exact graph matching is a NP complete problem,
104 >    //exact graph matching is a NP complete problem
105 >    /** @todo using sparse matrix to store the connectivities*/
106      for (unsigned int i =0 ; i < frag1.size(); ++i)
107      {
108 <        if (strcmp(mol.GetAtom(frag1[i])->GetType(), mol.GetAtom(frag2[i])->GetType()) )
108 >        OBAtom* atom1 = mol.GetAtom(frag1[i]);
109 >        OBAtom* atom2 = mol.GetAtom(frag2[i]);
110 >        
111 >        if (atom1->GetAtomicNum() != atom2->GetAtomicNum())
112          {
113              return false;
114          }
# Line 218 | Line 181 | void OOPSEFormat::WriteMDFile(vector<OBMol*> mols, vec
181      newMol->FindTorsions();
182      return newMol;
183   }
184 < void OOPSEFormat::WriteMDFile(vector<OBMol*> mols, vector<int> numMols)
184 > void OOPSEFormat::WriteMDFile(vector<OBMol*> mols, vector<int> numMols, ostream& os)
185   {
186      std::string identLevel1("\t");
187      std::string identLevel2("\t\t");
188      std::string molPrefix("MolName");
226    ostream& ofs = std::cout;
189      const int BUFFLEN = 1024;
190      char buffer[BUFFLEN];
191      
# Line 231 | Line 193 | void OOPSEFormat::WriteMDFile(vector<OBMol*> mols, vec
193      {
194          OBMol* pmol = mols[i];
195          map<OBAtom*, int> atomMap;
196 <        ofs << "molecule {\n";
196 >        os << "molecule {\n";
197          sprintf(buffer, "%d", i);
198 <        ofs << identLevel1 << "name = " << "\"" << molPrefix << buffer << "\"" << ";\n";
198 >        os << identLevel1 << "name = " << "\"" << molPrefix << buffer << "\"" << ";\n";
199  
200          
201          //atom
240        ofs << identLevel1 << "nAtoms = " << pmol->NumAtoms() << ";\n";
202          int ai = 0;
203          FOR_ATOMS_OF_MOL(atom, *pmol ) {
204 <            ofs << identLevel1 << "atom[" << ai << "] {\n";
205 <            ofs << identLevel2 << "type = " << "\"" << atom->GetType() << "\"" << ";\n";
206 <            ofs << identLevel2 << "position(" << atom->GetX() << ", " << atom->GetY() << ", " << atom->GetZ() << ");\n";
246 <            ofs << identLevel1 << "}\n";
204 >            os << identLevel1 << "atom[" << ai << "] {\n";
205 >            os << identLevel2 << "type = " << "\"" << etab.GetSymbol(atom->GetAtomicNum()) << "\"" << ";\n";
206 >            os << identLevel1 << "}\n";
207              atomMap[&(*atom)] = ai++;
208          }        
209 +        os << "\n";
210  
211          //bond
251        ofs << identLevel1 << "nBonds = " << pmol->NumBonds() << ";\n";
252        int bi = 0;
212          FOR_BONDS_OF_MOL(bond, *pmol ) {
213 <            ofs << identLevel1 << "bond[" << bi++ << "] {\n";
214 <            ofs << identLevel2 << "member(" << atomMap[bond->GetBeginAtom()] <<  ", " << atomMap[bond->GetEndAtom()] << ");\n";
215 <            ofs << identLevel1 << "}\n";
213 >            os << identLevel1 << "bond {\n";
214 >            os << identLevel2 << "members(" << atomMap[bond->GetBeginAtom()] <<  ", " << atomMap[bond->GetEndAtom()] << ");\n";
215 >            os << identLevel1 << "}\n";
216          }  
217          /*
218          //bend
# Line 261 | Line 220 | void OOPSEFormat::WriteMDFile(vector<OBMol*> mols, vec
220          OBAngleData* pAngleData = dynamic_cast<OBAngleData*>(pGenericData);
221          vector<OBAngle> angles = pAngleData->GetData();
222  
223 <        ofs << identLevel1 << "nBends = " << angles.size() << ";\n";        
223 >        os << identLevel1 << "nBends = " << angles.size() << ";\n";        
224          int bendIndex = 0;
225          for (vector<OBAngle>::iterator ti = angles.begin(); ti != angles.end(); ++ti)
226          {
227              triple<OBAtom*, OBAtom*, OBAtom*> bendAtoms = ti->getAtoms();
228 <            ofs << identLevel1 << "bend[" << bendIndex++ << "] {\n";
229 <            ofs << identLevel2 << "member(" << atomMap[bendAtoms.first] <<  ", " << atomMap[bendAtoms.second] << atomMap[bendAtoms.third] <<");\n";
230 <            ofs << identLevel1 << "}\n";            
228 >            os << identLevel1 << "bend[" << bendIndex++ << "] {\n";
229 >            os << identLevel2 << "member(" << atomMap[bendAtoms.first] <<  ", " << atomMap[bendAtoms.second] << atomMap[bendAtoms.third] <<");\n";
230 >            os << identLevel1 << "}\n";            
231          }
232          
233          //torsion
# Line 282 | Line 241 | void OOPSEFormat::WriteMDFile(vector<OBMol*> mols, vec
241              torsionArray.insert(torsionArray.end(), tmpTorsions.begin(), tmpTorsions.end());            
242          }
243  
244 <        ofs << identLevel1 << "nTorsions = " << torsionArray.size() << ";\n";
244 >        os << identLevel1 << "nTorsions = " << torsionArray.size() << ";\n";
245          int torsionIndex = 0;
246          for (vector<quad<OBAtom*,OBAtom*,OBAtom*,OBAtom*> >::iterator ti = torsionArray.begin(); ti != torsionArray.end(); ++ti)
247          {
248 <            ofs << identLevel1 << "torsion[" << torsionIndex++ << "] {\n";
249 <            ofs << identLevel2 << "member(" << atomMap[ti->first] <<  ", " << atomMap[ti->second] <<", " << atomMap[ti->third] <<", " << atomMap[ti->forth] << ");\n";
250 <            ofs << identLevel1 << "}\n";          
248 >            os << identLevel1 << "torsion[" << torsionIndex++ << "] {\n";
249 >            os << identLevel2 << "member(" << atomMap[ti->first] <<  ", " << atomMap[ti->second] <<", " << atomMap[ti->third] <<", " << atomMap[ti->forth] << ");\n";
250 >            os << identLevel1 << "}\n";          
251          }
252          */
253 <        ofs << "}\n";
253 >        os << "}\n";
254 >        os << "\n";
255 >
256      }
257  
258 <    ofs << "nComponents = " << mols.size() << ";\n";
258 >    os << "\n";
259 >    os << "nComponents = " << mols.size() << ";\n";
260      
261      for(unsigned int i =0; i < mols.size(); ++i)
262      {
263 <        ofs << "component{\n";
263 >        os << "component{\n";
264          sprintf(buffer, "%d", i);
265 <        ofs << "type = " << molPrefix << buffer << ";\n";
266 <        ofs << "nMol = " << numMols[i]<< ";\n";
267 <        ofs << "}\n";
265 >        os << "type = " << molPrefix << buffer << ";\n";
266 >        os << "nMol = " << numMols[i]<< ";\n";
267 >        os << "}\n";
268      }
269   }
270   void OOPSEFormat::WriteINFile(OBMol& mol, ostream& ofs, vector<int>& indices)
# Line 322 | Line 284 | void OOPSEFormat::WriteINFile(OBMol& mol, ostream& ofs
284      for(vector<int>::iterator i = indices.begin();i != indices.end(); ++i)
285      {
286          atom = mol.GetAtom(*i);
287 <        sprintf(buffer,"%15s%15.5f%15.5f%15.5f%15.5f%15.5f%15.5f%15.5f%15.5f%15.5f%15.5f%15.5f%15.5f%15.5f",
288 <                atom->GetType(),
287 >        sprintf(buffer,"%-10s%-15.5f%-15.5f%-15.5f%-15.5f%-15.5f%-15.5f%-15.5f%-15.5f%-15.5f%-15.5f%-15.5f%-15.5f%-15.5f",
288 >                etab.GetSymbol(atom->GetAtomicNum()),
289                  atom->GetX(), atom->GetY(), atom->GetZ(),
290                  0.0, 0.0, 0.0,
291                  0.0, 0.0, 0.0, 0.0,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines