| 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 "World.hpp"
 | 
|---|
| 51 | 
 | 
|---|
| 52 | /** Constructor for class ExportGraph.
 | 
|---|
| 53 |  *
 | 
|---|
| 54 |  * @param _graph
 | 
|---|
| 55 |  */
 | 
|---|
| 56 | ExportGraph::ExportGraph(
 | 
|---|
| 57 |   const Graph &_graph,
 | 
|---|
| 58 |   const enum HydrogenTreatment _treatment,
 | 
|---|
| 59 |   const enum HydrogenSaturation _saturation,
 | 
|---|
| 60 |   const SaturatedFragment::GlobalSaturationPositions_t &_globalsaturationpositions) :
 | 
|---|
| 61 |   TotalGraph(_graph),
 | 
|---|
| 62 |   treatment(_treatment),
 | 
|---|
| 63 |   saturation(_saturation),
 | 
|---|
| 64 |   globalsaturationpositions(_globalsaturationpositions),
 | 
|---|
| 65 |   CurrentKeySet(TotalGraph.begin())
 | 
|---|
| 66 | {
 | 
|---|
| 67 | }
 | 
|---|
| 68 | 
 | 
|---|
| 69 | /** Destructor for class ExportGraph.
 | 
|---|
| 70 |  *
 | 
|---|
| 71 |  */
 | 
|---|
| 72 | ExportGraph::~ExportGraph()
 | 
|---|
| 73 | {
 | 
|---|
| 74 |   // remove all create molecules again from the World including their atoms
 | 
|---|
| 75 |   for (std::vector<molecule *>::iterator iter = BondFragments.begin();
 | 
|---|
| 76 |       !BondFragments.empty();
 | 
|---|
| 77 |       iter = BondFragments.begin()) {
 | 
|---|
| 78 |     // remove copied atoms and molecule again
 | 
|---|
| 79 |     molecule *mol = *iter;
 | 
|---|
| 80 |     BondFragments.erase(iter);
 | 
|---|
| 81 |     removeAtomsinMolecule(mol);
 | 
|---|
| 82 |   }
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | void ExportGraph::reset()
 | 
|---|
| 86 | {
 | 
|---|
| 87 |   CurrentKeySet = TotalGraph.begin();
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | ExportGraph::SaturatedFragment_ptr ExportGraph::getNextFragment()
 | 
|---|
| 91 | {
 | 
|---|
| 92 |   // if a fragment is still leased, return zero ptr.
 | 
|---|
| 93 |   if (!KeySetsInUse.empty()) {
 | 
|---|
| 94 |     ELOG(1, "Leasing KeySet while old one is not returned.");
 | 
|---|
| 95 |     return SaturatedFragment_ptr();
 | 
|---|
| 96 |   }
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   // else return current fragment or indicate end
 | 
|---|
| 99 |   if (CurrentKeySet != TotalGraph.end()) {
 | 
|---|
| 100 |     const KeySet &set = (CurrentKeySet++)->first;
 | 
|---|
| 101 |     return leaseFragment(set);
 | 
|---|
| 102 |   } else {
 | 
|---|
| 103 |     return leaseFragment(EmptySet);
 | 
|---|
| 104 |   }
 | 
|---|
| 105 | }
 | 
|---|
| 106 | 
 | 
|---|
| 107 | ExportGraph::SaturatedFragment_ptr ExportGraph::leaseFragment(const KeySet &_set)
 | 
|---|
| 108 | {
 | 
|---|
| 109 |   // create the saturation which adds itself to KeySetsInUse
 | 
|---|
| 110 |   SaturatedFragment_ptr _ptr(
 | 
|---|
| 111 |       new SaturatedFragment(
 | 
|---|
| 112 |           _set,
 | 
|---|
| 113 |           KeySetsInUse,
 | 
|---|
| 114 |           hydrogens,
 | 
|---|
| 115 |           treatment,
 | 
|---|
| 116 |           saturation,
 | 
|---|
| 117 |           globalsaturationpositions)
 | 
|---|
| 118 |   );
 | 
|---|
| 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 | 
 | 
|---|
| 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: "
 | 
|---|
| 230 | //            output << Leaf->AddBond((*iter), SonList[OtherFather->getNr()], (*BondRunner)->getDegree());
 | 
|---|
| 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);
 | 
|---|
| 247 | //            Leaf->AddBond((*iter), OtherWalker, (*BondRunner)->getDegree());
 | 
|---|
| 248 | //          }
 | 
|---|
| 249 | //          //NumBonds[(*iter)->getNr()] += Binder->getDegree();
 | 
|---|
| 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 | //}
 | 
|---|