1 |
tim |
741 |
/********************************************************************** |
2 |
|
|
Copyright (C) 2000 by OpenEye Scientific Software, Inc. |
3 |
|
|
Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison |
4 |
|
|
Some portions Copyright (C) 2004 by Chris Morley |
5 |
|
|
|
6 |
|
|
This program is free software; you can redistribute it and/or modify |
7 |
|
|
it under the terms of the GNU General Public License as published by |
8 |
|
|
the Free Software Foundation version 2 of the License. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
***********************************************************************/ |
15 |
|
|
|
16 |
tim |
746 |
#include "oopseformat.hpp" |
17 |
tim |
756 |
#include <fstream> |
18 |
tim |
741 |
namespace OpenBabel |
19 |
|
|
{ |
20 |
|
|
|
21 |
|
|
bool OOPSEFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv) |
22 |
|
|
{ |
23 |
|
|
OBMol* pmol = dynamic_cast<OBMol*>(pOb); |
24 |
|
|
if(pmol==NULL) |
25 |
|
|
return false; |
26 |
|
|
|
27 |
|
|
vector<vector<int> > fragmentLists; |
28 |
|
|
pmol->ContigFragList(fragmentLists); |
29 |
|
|
OBBitVec unused; |
30 |
|
|
vector<bool> used(fragmentLists.size(), 0); |
31 |
|
|
vector<vector<int> > molecules; |
32 |
|
|
vector<int> indices; |
33 |
|
|
for(int i =0; i < used.size(); ++i) { |
34 |
|
|
if (used[i]) |
35 |
|
|
{ |
36 |
|
|
continue; |
37 |
|
|
} |
38 |
|
|
used[i] = true; |
39 |
|
|
vector<int> sameMolTypes; |
40 |
|
|
sameMolTypes.push_back(i); |
41 |
|
|
indices.insert(indices.end(), fragmentLists[i].begin(), fragmentLists[i].end()); |
42 |
|
|
for (int j = i + 1;j < used.size(); ++j) |
43 |
|
|
{ |
44 |
|
|
if (used[j]) |
45 |
|
|
{ |
46 |
|
|
continue; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
if (AreSameFragments(*pmol, fragmentLists[i], fragmentLists[j])) |
50 |
|
|
{ |
51 |
|
|
sameMolTypes.push_back(j); |
52 |
|
|
indices.insert(indices.end(), fragmentLists[j].begin(), fragmentLists[j].end()); |
53 |
|
|
used[j]=true; |
54 |
|
|
} |
55 |
|
|
} |
56 |
|
|
molecules.push_back(sameMolTypes); |
57 |
|
|
|
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
// |
61 |
|
|
vector<OBMol*> mdMols; |
62 |
|
|
vector<int> numMols; |
63 |
|
|
for(vector<vector<int> >::iterator i = molecules.begin(); i != molecules.end(); ++i) |
64 |
|
|
{ |
65 |
|
|
mdMols.push_back(createMolFromFragment(*pmol, fragmentLists[i->front()])); |
66 |
|
|
numMols.push_back((*i).size()); |
67 |
|
|
} |
68 |
|
|
|
69 |
tim |
756 |
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 |
tim |
741 |
|
84 |
|
|
for(vector<OBMol*>::iterator i = mdMols.begin(); i != mdMols.end(); ++i) |
85 |
|
|
{ |
86 |
|
|
delete *i; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
// |
90 |
|
|
WriteINFile(*pmol, *pConv->GetOutStream(), indices); |
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
return(true); |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
bool OOPSEFormat::AreSameFragments(OBMol& mol, vector<int>& frag1, vector<int>& frag2) |
98 |
|
|
{ |
99 |
|
|
if (frag1.size() != frag2.size()) |
100 |
|
|
{ |
101 |
|
|
return false; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
//exact graph matching is a NP complete problem, |
105 |
|
|
for (unsigned int i =0 ; i < frag1.size(); ++i) |
106 |
|
|
{ |
107 |
|
|
if (strcmp(mol.GetAtom(frag1[i])->GetType(), mol.GetAtom(frag2[i])->GetType()) ) |
108 |
|
|
{ |
109 |
|
|
return false; |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
return true; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
struct SameAngle |
116 |
|
|
{ |
117 |
|
|
bool operator()(const triple<OBAtom*,OBAtom*,OBAtom*> t1, const triple<OBAtom*,OBAtom*,OBAtom*> t2) const |
118 |
|
|
{ |
119 |
|
|
return (t1.second == t2.second) && ( (t1.first == t2.first && t1.third == t2.third) || (t1.first == t2.third && t1.third == t2.first)); |
120 |
|
|
} |
121 |
|
|
}; |
122 |
|
|
|
123 |
|
|
void OOPSEFormat::findAngles(OBMol& mol) |
124 |
|
|
{ |
125 |
|
|
/* |
126 |
|
|
//if already has data return |
127 |
|
|
if(mol.HasData(OBGenericDataType::AngleData)) |
128 |
|
|
return; |
129 |
|
|
|
130 |
|
|
vector<OBEdgeBase*>::iterator bi1,bi2; |
131 |
|
|
OBBond* bond; |
132 |
|
|
OBAtom *a,*b,*c; |
133 |
|
|
|
134 |
|
|
set<triple<OBAtom*,OBAtom*,OBAtom*>, SameAngle> uniqueAngles; |
135 |
|
|
//loop through all bonds generating torsions |
136 |
|
|
for(bond = mol.BeginBond(bi1);bond;bond = mol.NextBond(bi1)) |
137 |
|
|
{ |
138 |
|
|
b = bond->GetBeginAtom(); |
139 |
|
|
c = bond->GetEndAtom(); |
140 |
|
|
if(b->IsHydrogen()) |
141 |
|
|
continue; |
142 |
|
|
|
143 |
|
|
for(a = b->BeginNbrAtom(bi2);a;a = b->NextNbrAtom(bi2)) |
144 |
|
|
{ |
145 |
|
|
if(a == c) |
146 |
|
|
continue; |
147 |
|
|
|
148 |
|
|
uniqueAngles.insert(triple<OBAtom*,OBAtom*,OBAtom*>(a, b, c)); |
149 |
|
|
} |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
//get new data and attach it to molecule |
153 |
|
|
OBAngleData *angles = new OBAngleData; |
154 |
|
|
mol.SetData(angles); |
155 |
|
|
set<triple<OBAtom*,OBAtom*,OBAtom*>, SameAngle>::iterator i; |
156 |
|
|
|
157 |
|
|
for (i = uniqueAngles.begin(); i != uniqueAngles.end(); ++i) { |
158 |
|
|
OBAngle angle; |
159 |
|
|
angle.SetAtoms(i->first, i->second, i->second); |
160 |
|
|
angles->SetData(angle); |
161 |
|
|
} |
162 |
|
|
*/ |
163 |
|
|
} |
164 |
|
|
OBMol* OOPSEFormat::createMolFromFragment(OBMol& mol, vector<int>& fragment) |
165 |
|
|
{ |
166 |
|
|
OBMol* newMol = new OBMol(); |
167 |
|
|
newMol->ReserveAtoms(fragment.size()); |
168 |
|
|
newMol->BeginModify(); |
169 |
|
|
for(vector<int>::iterator i = fragment.begin(); i != fragment.end(); ++i) |
170 |
|
|
{ |
171 |
|
|
OBAtom* newAtom = newMol->NewAtom(); |
172 |
|
|
*newAtom = *mol.GetAtom(*i); |
173 |
|
|
} |
174 |
|
|
newMol->EndModify(); |
175 |
|
|
newMol->ConnectTheDots(); |
176 |
|
|
findAngles(*newMol); |
177 |
|
|
newMol->FindTorsions(); |
178 |
|
|
return newMol; |
179 |
|
|
} |
180 |
tim |
756 |
void OOPSEFormat::WriteMDFile(vector<OBMol*> mols, vector<int> numMols, ostream& os) |
181 |
tim |
741 |
{ |
182 |
|
|
std::string identLevel1("\t"); |
183 |
|
|
std::string identLevel2("\t\t"); |
184 |
|
|
std::string molPrefix("MolName"); |
185 |
|
|
const int BUFFLEN = 1024; |
186 |
|
|
char buffer[BUFFLEN]; |
187 |
|
|
|
188 |
|
|
for(unsigned int i = 0; i < mols.size(); ++i) |
189 |
|
|
{ |
190 |
|
|
OBMol* pmol = mols[i]; |
191 |
|
|
map<OBAtom*, int> atomMap; |
192 |
tim |
756 |
os << "molecule {\n"; |
193 |
tim |
741 |
sprintf(buffer, "%d", i); |
194 |
tim |
756 |
os << identLevel1 << "name = " << "\"" << molPrefix << buffer << "\"" << ";\n"; |
195 |
tim |
741 |
|
196 |
|
|
|
197 |
|
|
//atom |
198 |
|
|
int ai = 0; |
199 |
|
|
FOR_ATOMS_OF_MOL(atom, *pmol ) { |
200 |
tim |
756 |
os << identLevel1 << "atom[" << ai << "] {\n"; |
201 |
|
|
os << identLevel2 << "type = " << "\"" << atom->GetType() << "\"" << ";\n"; |
202 |
|
|
os << identLevel1 << "}\n"; |
203 |
tim |
741 |
atomMap[&(*atom)] = ai++; |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
//bond |
207 |
|
|
FOR_BONDS_OF_MOL(bond, *pmol ) { |
208 |
tim |
756 |
os << identLevel1 << "bond {\n"; |
209 |
|
|
os << identLevel2 << "members(" << atomMap[bond->GetBeginAtom()] << ", " << atomMap[bond->GetEndAtom()] << ");\n"; |
210 |
|
|
os << identLevel1 << "}\n"; |
211 |
tim |
741 |
} |
212 |
|
|
/* |
213 |
|
|
//bend |
214 |
|
|
OBGenericData* pGenericData = pmol->GetData(OBGenericDataType::AngleData); |
215 |
|
|
OBAngleData* pAngleData = dynamic_cast<OBAngleData*>(pGenericData); |
216 |
|
|
vector<OBAngle> angles = pAngleData->GetData(); |
217 |
|
|
|
218 |
tim |
756 |
os << identLevel1 << "nBends = " << angles.size() << ";\n"; |
219 |
tim |
741 |
int bendIndex = 0; |
220 |
|
|
for (vector<OBAngle>::iterator ti = angles.begin(); ti != angles.end(); ++ti) |
221 |
|
|
{ |
222 |
|
|
triple<OBAtom*, OBAtom*, OBAtom*> bendAtoms = ti->getAtoms(); |
223 |
tim |
756 |
os << identLevel1 << "bend[" << bendIndex++ << "] {\n"; |
224 |
|
|
os << identLevel2 << "member(" << atomMap[bendAtoms.first] << ", " << atomMap[bendAtoms.second] << atomMap[bendAtoms.third] <<");\n"; |
225 |
|
|
os << identLevel1 << "}\n"; |
226 |
tim |
741 |
} |
227 |
|
|
|
228 |
|
|
//torsion |
229 |
|
|
pGenericData = pmol->GetData(OBGenericDataType::TorsionData); |
230 |
|
|
OBTorsionData* pTorsionData = dynamic_cast<OBTorsionData*>(pGenericData); |
231 |
|
|
vector<OBTorsion> torsions = pTorsionData->GetData(); |
232 |
|
|
vector<quad<OBAtom*,OBAtom*,OBAtom*,OBAtom*> > torsionArray; |
233 |
|
|
for (vector<OBTorsion>::iterator ti = torsions.begin(); ti != torsions.end(); ++ti) |
234 |
|
|
{ |
235 |
|
|
vector<quad<OBAtom*,OBAtom*,OBAtom*,OBAtom*> > tmpTorsions = ti->getTorsions(); |
236 |
|
|
torsionArray.insert(torsionArray.end(), tmpTorsions.begin(), tmpTorsions.end()); |
237 |
|
|
} |
238 |
|
|
|
239 |
tim |
756 |
os << identLevel1 << "nTorsions = " << torsionArray.size() << ";\n"; |
240 |
tim |
741 |
int torsionIndex = 0; |
241 |
|
|
for (vector<quad<OBAtom*,OBAtom*,OBAtom*,OBAtom*> >::iterator ti = torsionArray.begin(); ti != torsionArray.end(); ++ti) |
242 |
|
|
{ |
243 |
tim |
756 |
os << identLevel1 << "torsion[" << torsionIndex++ << "] {\n"; |
244 |
|
|
os << identLevel2 << "member(" << atomMap[ti->first] << ", " << atomMap[ti->second] <<", " << atomMap[ti->third] <<", " << atomMap[ti->forth] << ");\n"; |
245 |
|
|
os << identLevel1 << "}\n"; |
246 |
tim |
741 |
} |
247 |
|
|
*/ |
248 |
tim |
756 |
os << "}\n"; |
249 |
tim |
741 |
} |
250 |
|
|
|
251 |
tim |
756 |
os << "nComponents = " << mols.size() << ";\n"; |
252 |
tim |
741 |
|
253 |
|
|
for(unsigned int i =0; i < mols.size(); ++i) |
254 |
|
|
{ |
255 |
tim |
756 |
os << "component{\n"; |
256 |
tim |
741 |
sprintf(buffer, "%d", i); |
257 |
tim |
756 |
os << "type = " << molPrefix << buffer << ";\n"; |
258 |
|
|
os << "nMol = " << numMols[i]<< ";\n"; |
259 |
|
|
os << "}\n"; |
260 |
tim |
741 |
} |
261 |
|
|
} |
262 |
|
|
void OOPSEFormat::WriteINFile(OBMol& mol, ostream& ofs, vector<int>& indices) |
263 |
|
|
{ |
264 |
|
|
unsigned int i; |
265 |
|
|
char buffer[BUFF_SIZE]; |
266 |
|
|
|
267 |
|
|
sprintf(buffer,"%d", mol.NumAtoms()); |
268 |
|
|
ofs << buffer << endl; |
269 |
|
|
//sprintf(buffer,"0;%f, 0, 0; 0, %15.7f, 0; 0, 0, %15.7f;", boxx, boxy, boxz); |
270 |
|
|
sprintf(buffer, "0;%f, 0, 0; 0, %15.7f, 0; 0, 0, %15.7f;", 100.0, 100.0, 100.0); |
271 |
|
|
ofs << buffer << endl; |
272 |
|
|
|
273 |
|
|
OBAtom *atom; |
274 |
|
|
string str,str1; |
275 |
|
|
|
276 |
|
|
for(vector<int>::iterator i = indices.begin();i != indices.end(); ++i) |
277 |
|
|
{ |
278 |
|
|
atom = mol.GetAtom(*i); |
279 |
|
|
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", |
280 |
|
|
atom->GetType(), |
281 |
|
|
atom->GetX(), atom->GetY(), atom->GetZ(), |
282 |
|
|
0.0, 0.0, 0.0, |
283 |
|
|
0.0, 0.0, 0.0, 0.0, |
284 |
|
|
0.0, 0.0, 0.0 |
285 |
|
|
); |
286 |
|
|
ofs << buffer << endl; |
287 |
|
|
} |
288 |
|
|
|
289 |
|
|
} |
290 |
|
|
|
291 |
|
|
} //namespace OpenBabel |
292 |
|
|
|