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