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