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