| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2011 University of Bonn. All rights reserved. | 
|---|
| 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| 6 | * | 
|---|
| 7 | * | 
|---|
| 8 | *   This file is part of MoleCuilder. | 
|---|
| 9 | * | 
|---|
| 10 | *    MoleCuilder 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, either version 2 of the License, or | 
|---|
| 13 | *    (at your option) any later version. | 
|---|
| 14 | * | 
|---|
| 15 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 16 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 17 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 18 | *    GNU General Public License for more details. | 
|---|
| 19 | * | 
|---|
| 20 | *    You should have received a copy of the GNU General Public License | 
|---|
| 21 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | /* | 
|---|
| 25 | * ExportGraph.cpp | 
|---|
| 26 | * | 
|---|
| 27 | *  Created on: 08.03.2012 | 
|---|
| 28 | *      Author: heber | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 | // include config.h | 
|---|
| 32 | #ifdef HAVE_CONFIG_H | 
|---|
| 33 | #include <config.h> | 
|---|
| 34 | #endif | 
|---|
| 35 |  | 
|---|
| 36 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #include "ExportGraph.hpp" | 
|---|
| 39 |  | 
|---|
| 40 | #include "CodePatterns/Info.hpp" | 
|---|
| 41 | #include "CodePatterns/Log.hpp" | 
|---|
| 42 |  | 
|---|
| 43 | #include "Bond/bond.hpp" | 
|---|
| 44 | #include "Element/element.hpp" | 
|---|
| 45 | #include "Fragmentation/Graph.hpp" | 
|---|
| 46 | #include "Fragmentation/KeySet.hpp" | 
|---|
| 47 | #include "Fragmentation/SortIndex.hpp" | 
|---|
| 48 | #include "Graph/ListOfLocalAtoms.hpp" | 
|---|
| 49 | #include "molecule.hpp" | 
|---|
| 50 | #include "MoleculeListClass.hpp" | 
|---|
| 51 | #include "World.hpp" | 
|---|
| 52 |  | 
|---|
| 53 | /** Constructor for class ExportGraph. | 
|---|
| 54 | * | 
|---|
| 55 | * @param _graph | 
|---|
| 56 | */ | 
|---|
| 57 | ExportGraph::ExportGraph( | 
|---|
| 58 | const Graph &_graph, | 
|---|
| 59 | const enum HydrogenTreatment _treatment, | 
|---|
| 60 | const enum HydrogenSaturation _saturation, | 
|---|
| 61 | const SaturatedFragment::GlobalSaturationPositions_t &_globalsaturationpositions) : | 
|---|
| 62 | TotalGraph(_graph), | 
|---|
| 63 | BondFragments(World::getPointer()), | 
|---|
| 64 | treatment(_treatment), | 
|---|
| 65 | saturation(_saturation), | 
|---|
| 66 | globalsaturationpositions(_globalsaturationpositions), | 
|---|
| 67 | CurrentKeySet(TotalGraph.begin()) | 
|---|
| 68 | { | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | /** Destructor for class ExportGraph. | 
|---|
| 72 | * | 
|---|
| 73 | */ | 
|---|
| 74 | ExportGraph::~ExportGraph() | 
|---|
| 75 | { | 
|---|
| 76 | // remove all create molecules again from the World including their atoms | 
|---|
| 77 | for (MoleculeList::iterator iter = BondFragments.ListOfMolecules.begin(); | 
|---|
| 78 | !BondFragments.ListOfMolecules.empty(); | 
|---|
| 79 | iter = BondFragments.ListOfMolecules.begin()) { | 
|---|
| 80 | // remove copied atoms and molecule again | 
|---|
| 81 | molecule *mol = *iter; | 
|---|
| 82 | mol->removeAtomsinMolecule(); | 
|---|
| 83 | World::getInstance().destroyMolecule(mol); | 
|---|
| 84 | BondFragments.ListOfMolecules.erase(iter); | 
|---|
| 85 | } | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | void ExportGraph::reset() | 
|---|
| 89 | { | 
|---|
| 90 | CurrentKeySet = TotalGraph.begin(); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | ExportGraph::SaturatedFragment_ptr ExportGraph::getNextFragment() | 
|---|
| 94 | { | 
|---|
| 95 | // if a fragment is still leased, return zero ptr. | 
|---|
| 96 | if (!KeySetsInUse.empty()) { | 
|---|
| 97 | ELOG(1, "Leasing KeySet while old one is not returned."); | 
|---|
| 98 | return SaturatedFragment_ptr(); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | // else return current fragment or indicate end | 
|---|
| 102 | if (CurrentKeySet != TotalGraph.end()) { | 
|---|
| 103 | const KeySet &set = (CurrentKeySet++)->first; | 
|---|
| 104 | return leaseFragment(set); | 
|---|
| 105 | } else { | 
|---|
| 106 | return leaseFragment(EmptySet); | 
|---|
| 107 | } | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | ExportGraph::SaturatedFragment_ptr ExportGraph::leaseFragment(const KeySet &_set) | 
|---|
| 111 | { | 
|---|
| 112 | // create the saturation which adds itself to KeySetsInUse | 
|---|
| 113 | SaturatedFragment_ptr _ptr( | 
|---|
| 114 | new SaturatedFragment( | 
|---|
| 115 | _set, | 
|---|
| 116 | KeySetsInUse, | 
|---|
| 117 | hydrogens, | 
|---|
| 118 | treatment, | 
|---|
| 119 | saturation, | 
|---|
| 120 | globalsaturationpositions) | 
|---|
| 121 | ); | 
|---|
| 122 | // and return | 
|---|
| 123 | return _ptr; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | void ExportGraph::releaseFragment(SaturatedFragment_ptr &_ptr) | 
|---|
| 127 | { | 
|---|
| 128 | ASSERT(_ptr != NULL, | 
|---|
| 129 | "ExportGraph::releaseFragment() - pointer is NULL."); | 
|---|
| 130 | SaturatedFragment::KeySetsInUse_t::iterator iter = KeySetsInUse.find(_ptr->getKeySet()); | 
|---|
| 131 | if (iter == KeySetsInUse.end()) { | 
|---|
| 132 | ASSERT(0, | 
|---|
| 133 | "ExportGraph::releaseFragment() - returning unknown set " | 
|---|
| 134 | +toString(_ptr->getKeySet())+"."); | 
|---|
| 135 | return; | 
|---|
| 136 | } else { | 
|---|
| 137 | // release instance which removes itself in KeySetsInUse | 
|---|
| 138 | _ptr.reset(); | 
|---|
| 139 | } | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | ///** Internal helper to create from each keyset a molecule | 
|---|
| 143 | // * | 
|---|
| 144 | // */ | 
|---|
| 145 | //void ExportGraph::prepareMolecule() | 
|---|
| 146 | //{ | 
|---|
| 147 | //  size_t count = 0; | 
|---|
| 148 | //  for(Graph::const_iterator runner = TotalGraph.begin(); runner != TotalGraph.end(); runner++) { | 
|---|
| 149 | //    KeySet test = (*runner).first; | 
|---|
| 150 | //    LOG(2, "DEBUG: Fragment No." << (*runner).second.first << " with TEFactor " | 
|---|
| 151 | //        << (*runner).second.second << "."); | 
|---|
| 152 | //    BondFragments.insert(StoreFragmentFromKeySet(test, World::getInstance().getConfig())); | 
|---|
| 153 | //    ++count; | 
|---|
| 154 | //  } | 
|---|
| 155 | //  LOG(1, "INFO: " << count << "/" << BondFragments.ListOfMolecules.size() | 
|---|
| 156 | //      << " fragments generated from the keysets."); | 
|---|
| 157 | //} | 
|---|
| 158 | // | 
|---|
| 159 | ///** Stores a fragment from \a KeySet into \a molecule. | 
|---|
| 160 | // * First creates the minimal set of atoms from the KeySet, then creates the bond structure from the complete | 
|---|
| 161 | // * molecule and adds missing hydrogen where bonds were cut. | 
|---|
| 162 | // * \param &Leaflet pointer to KeySet structure | 
|---|
| 163 | // * \param IsAngstroem whether we have Ansgtroem or bohrradius | 
|---|
| 164 | // * \return pointer to constructed molecule | 
|---|
| 165 | // */ | 
|---|
| 166 | //molecule * ExportGraph::StoreFragmentFromKeySet(KeySet &Leaflet, bool IsAngstroem) | 
|---|
| 167 | //{ | 
|---|
| 168 | //  Info info(__func__); | 
|---|
| 169 | //  ListOfLocalAtoms_t SonList; | 
|---|
| 170 | //  molecule *Leaf = World::getInstance().createMolecule(); | 
|---|
| 171 | // | 
|---|
| 172 | //  StoreFragmentFromKeySet_Init(Leaf, Leaflet, SonList); | 
|---|
| 173 | //  // create the bonds between all: Make it an induced subgraph and add hydrogen | 
|---|
| 174 | ////  LOG(2, "Creating bonds from father graph (i.e. induced subgraph creation)."); | 
|---|
| 175 | //  CreateInducedSubgraphOfFragment(Leaf, SonList, IsAngstroem); | 
|---|
| 176 | // | 
|---|
| 177 | //  //Leaflet->Leaf->ScanForPeriodicCorrection(out); | 
|---|
| 178 | //  return Leaf; | 
|---|
| 179 | //} | 
|---|
| 180 | // | 
|---|
| 181 | ///** Initializes some value for putting fragment of \a *mol into \a *Leaf. | 
|---|
| 182 | // * \param *Leaf fragment molecule | 
|---|
| 183 | // * \param &Leaflet pointer to KeySet structure | 
|---|
| 184 | // * \param SonList calloc'd list which atom of \a *Leaf is a son of which atom in \a *mol | 
|---|
| 185 | // * \return number of atoms in fragment | 
|---|
| 186 | // */ | 
|---|
| 187 | //int ExportGraph::StoreFragmentFromKeySet_Init(molecule *Leaf, KeySet &Leaflet, ListOfLocalAtoms_t &SonList) | 
|---|
| 188 | //{ | 
|---|
| 189 | //  atom *FatherOfRunner = NULL; | 
|---|
| 190 | // | 
|---|
| 191 | //  // first create the minimal set of atoms from the KeySet | 
|---|
| 192 | //  World &world = World::getInstance(); | 
|---|
| 193 | //  int size = 0; | 
|---|
| 194 | //  for(KeySet::const_iterator runner = Leaflet.begin(); runner != Leaflet.end(); runner++) { | 
|---|
| 195 | //    FatherOfRunner = world.getAtom(AtomById(*runner));  // find the id | 
|---|
| 196 | //    SonList.insert( std::make_pair(FatherOfRunner->getNr(), Leaf->AddCopyAtom(FatherOfRunner) ) ); | 
|---|
| 197 | //    size++; | 
|---|
| 198 | //  } | 
|---|
| 199 | //  return size; | 
|---|
| 200 | //} | 
|---|
| 201 | // | 
|---|
| 202 | ///** Creates an induced subgraph out of a fragmental key set, adding bonds and hydrogens (if treated specially). | 
|---|
| 203 | // * \param *Leaf fragment molecule | 
|---|
| 204 | // * \param IsAngstroem whether we have Ansgtroem or bohrradius | 
|---|
| 205 | // * \param SonList list which atom of \a *Leaf is another atom's son | 
|---|
| 206 | // */ | 
|---|
| 207 | //void ExportGraph::CreateInducedSubgraphOfFragment(molecule *Leaf, ListOfLocalAtoms_t &SonList, bool IsAngstroem) | 
|---|
| 208 | //{ | 
|---|
| 209 | //  bool LonelyFlag = false; | 
|---|
| 210 | //  atom *OtherFather = NULL; | 
|---|
| 211 | //  atom *FatherOfRunner = NULL; | 
|---|
| 212 | // | 
|---|
| 213 | //  // we increment the iter just before skipping the hydrogen | 
|---|
| 214 | //  // as we use AddBond, we cannot have a const_iterator here | 
|---|
| 215 | //  for (molecule::iterator iter = Leaf->begin(); iter != Leaf->end();) { | 
|---|
| 216 | //    LonelyFlag = true; | 
|---|
| 217 | //    FatherOfRunner = (*iter)->father; | 
|---|
| 218 | //    ASSERT(FatherOfRunner,"Atom without father found"); | 
|---|
| 219 | //    if (SonList.find(FatherOfRunner->getNr()) != SonList.end())  {  // check if this, our father, is present in list | 
|---|
| 220 | //      // create all bonds | 
|---|
| 221 | //      const BondList& ListOfBonds = FatherOfRunner->getListOfBonds(); | 
|---|
| 222 | //      for (BondList::const_iterator BondRunner = ListOfBonds.begin(); | 
|---|
| 223 | //          BondRunner != ListOfBonds.end(); | 
|---|
| 224 | //          ++BondRunner) { | 
|---|
| 225 | //        OtherFather = (*BondRunner)->GetOtherAtom(FatherOfRunner); | 
|---|
| 226 | //        if (SonList.find(OtherFather->getNr()) != SonList.end()) { | 
|---|
| 227 | ////          LOG(2, "INFO: Father " << *FatherOfRunner << " of son " << *SonList[FatherOfRunner->getNr()] | 
|---|
| 228 | ////              << " is bound to " << *OtherFather << ", whose son is " | 
|---|
| 229 | ////              << *SonList[OtherFather->getNr()] << "."); | 
|---|
| 230 | //          if (OtherFather->getNr() > FatherOfRunner->getNr()) { // add bond (Nr check is for adding only one of both variants: ab, ba) | 
|---|
| 231 | //            std::stringstream output; | 
|---|
| 232 | ////            output << "ACCEPT: Adding Bond: " | 
|---|
| 233 | //            output << Leaf->AddBond((*iter), SonList[OtherFather->getNr()], (*BondRunner)->getDegree()); | 
|---|
| 234 | ////            LOG(3, output.str()); | 
|---|
| 235 | //            //NumBonds[(*iter)->getNr()]++; | 
|---|
| 236 | //          } else { | 
|---|
| 237 | ////            LOG(3, "REJECY: Not adding bond, labels in wrong order."); | 
|---|
| 238 | //          } | 
|---|
| 239 | //          LonelyFlag = false; | 
|---|
| 240 | //        } else { | 
|---|
| 241 | ////          LOG(2, "INFO: Father " << *FatherOfRunner << " of son " << *SonList[FatherOfRunner->getNr()] | 
|---|
| 242 | ////              << " is bound to " << *OtherFather << ", who has no son in this fragment molecule."); | 
|---|
| 243 | //          if (saturation == DoSaturate) { | 
|---|
| 244 | ////          LOG(3, "ACCEPT: Adding Hydrogen to " << (*iter)->Name << " and a bond in between."); | 
|---|
| 245 | //            if (!Leaf->AddHydrogenReplacementAtom((*BondRunner), (*iter), FatherOfRunner, OtherFather, IsAngstroem)) | 
|---|
| 246 | //              exit(1); | 
|---|
| 247 | //          } else if ((treatment == ExcludeHydrogen) && (OtherFather->getElementNo() == (atomicNumber_t)1)) { | 
|---|
| 248 | //            // just copy the atom if it's a hydrogen | 
|---|
| 249 | //            atom * const OtherWalker = Leaf->AddCopyAtom(OtherFather); | 
|---|
| 250 | //            Leaf->AddBond((*iter), OtherWalker, (*BondRunner)->getDegree()); | 
|---|
| 251 | //          } | 
|---|
| 252 | //          //NumBonds[(*iter)->getNr()] += Binder->getDegree(); | 
|---|
| 253 | //        } | 
|---|
| 254 | //      } | 
|---|
| 255 | //    } else { | 
|---|
| 256 | //      ELOG(1, "Son " << (*iter)->getName() << " has father " << FatherOfRunner->getName() << " but its entry in SonList is " << SonList[FatherOfRunner->getNr()] << "!"); | 
|---|
| 257 | //    } | 
|---|
| 258 | //    if ((LonelyFlag) && (Leaf->getAtomCount() > 1)) { | 
|---|
| 259 | //      LOG(0, **iter << "has got bonds only to hydrogens!"); | 
|---|
| 260 | //    } | 
|---|
| 261 | //    ++iter; | 
|---|
| 262 | //    if (saturation == DoSaturate) { | 
|---|
| 263 | //      while ((iter != Leaf->end()) && ((*iter)->getType()->getAtomicNumber() == 1)){ // skip added hydrogen | 
|---|
| 264 | //        iter++; | 
|---|
| 265 | //      } | 
|---|
| 266 | //    } | 
|---|
| 267 | //  } | 
|---|
| 268 | //} | 
|---|