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