| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010-2012 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 | * BreadthFirstSearchAdd.cpp
|
|---|
| 25 | *
|
|---|
| 26 | * Created on: Feb 16, 2011
|
|---|
| 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 "BreadthFirstSearchAdd.hpp"
|
|---|
| 38 |
|
|---|
| 39 | #include <sstream>
|
|---|
| 40 |
|
|---|
| 41 | #include "Atom/atom.hpp"
|
|---|
| 42 | #include "Bond/bond.hpp"
|
|---|
| 43 | #include "CodePatterns/Assert.hpp"
|
|---|
| 44 | #include "CodePatterns/Info.hpp"
|
|---|
| 45 | #include "CodePatterns/Log.hpp"
|
|---|
| 46 | #include "CodePatterns/Verbose.hpp"
|
|---|
| 47 | #include "molecule.hpp"
|
|---|
| 48 | #include "World.hpp"
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | BreadthFirstSearchAdd::BreadthFirstSearchAdd(atom *&_Root, int _BondOrder, bool _IsAngstroem, const enum HydrogenSaturation _saturation) :
|
|---|
| 52 | BondOrder(_BondOrder),
|
|---|
| 53 | Root(_Root),
|
|---|
| 54 | saturation(_saturation),
|
|---|
| 55 | IsAngstroem(_IsAngstroem)
|
|---|
| 56 | {
|
|---|
| 57 | BFSStack.push_front(Root);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | BreadthFirstSearchAdd::~BreadthFirstSearchAdd()
|
|---|
| 62 | {}
|
|---|
| 63 |
|
|---|
| 64 | void BreadthFirstSearchAdd::Init(atom *&_Root, int _BondOrder)
|
|---|
| 65 | {
|
|---|
| 66 | BondOrder = _BondOrder;
|
|---|
| 67 | Root = _Root;
|
|---|
| 68 | BFSStack.clear();
|
|---|
| 69 | BFSStack.push_front(Root);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | void BreadthFirstSearchAdd::InitNode(atomId_t atom_id)
|
|---|
| 73 | {
|
|---|
| 74 | PredecessorList[atom_id] = NULL;
|
|---|
| 75 | ShortestPathList[atom_id] = -1;
|
|---|
| 76 | if (AddedAtomList.find(atom_id) != AddedAtomList.end()) // mark already present atoms (i.e. Root and maybe others) as visited
|
|---|
| 77 | ColorList[atom_id] = GraphEdge::lightgray;
|
|---|
| 78 | else
|
|---|
| 79 | ColorList[atom_id] = GraphEdge::white;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 | void BreadthFirstSearchAdd::UnvisitedNode(molecule *Mol, atom *&Walker, atom *&OtherAtom, bond::ptr &Binder, bond::ptr &Bond)
|
|---|
| 84 | {
|
|---|
| 85 | if (Binder != Bond) // let other atom GraphEdge::white if it's via Root bond. In case it's cyclic it has to be reached again (yet Root is from OtherAtom already GraphEdge::black, thus no problem)
|
|---|
| 86 | ColorList[OtherAtom->getNr()] = GraphEdge::lightgray;
|
|---|
| 87 | PredecessorList[OtherAtom->getNr()] = Walker; // Walker is the predecessor
|
|---|
| 88 | ShortestPathList[OtherAtom->getNr()] = ShortestPathList[Walker->getNr()] + 1;
|
|---|
| 89 | LOG(2, "Coloring OtherAtom " << OtherAtom->getName() << " " << GraphEdge::getColorName(ColorList[OtherAtom->getNr()]) << ", its predecessor is " << Walker->getName() << " and its Shortest Path is " << ShortestPathList[OtherAtom->getNr()] << " egde(s) long.");
|
|---|
| 90 | if ((((ShortestPathList[OtherAtom->getNr()] < BondOrder) && (Binder != Bond)))) { // Check for maximum distance
|
|---|
| 91 | std::stringstream output;
|
|---|
| 92 | if (AddedAtomList[OtherAtom->getNr()] == NULL) { // add if it's not been so far
|
|---|
| 93 | AddedAtomList[OtherAtom->getNr()] = Mol->AddCopyAtom(OtherAtom);
|
|---|
| 94 | output << "Added OtherAtom " << OtherAtom->getName();
|
|---|
| 95 | AddedBondList[Binder] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder);
|
|---|
| 96 | output << " and bond " << *(AddedBondList[Binder]) << ", ";
|
|---|
| 97 | } else { // this code should actually never come into play (all GraphEdge::white atoms are not yet present in BondMolecule, that's why they are GraphEdge::white in the first place)
|
|---|
| 98 | output << "Not adding OtherAtom " << OtherAtom->getName();
|
|---|
| 99 | if (AddedBondList[Binder] == NULL) {
|
|---|
| 100 | AddedBondList[Binder] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder);
|
|---|
| 101 | output << ", added Bond " << *(AddedBondList[Binder]);
|
|---|
| 102 | } else
|
|---|
| 103 | output << ", not added Bond ";
|
|---|
| 104 | }
|
|---|
| 105 | output << ", putting OtherAtom into queue.";
|
|---|
| 106 | LOG(0, output.str());
|
|---|
| 107 | BFSStack.push_front(OtherAtom);
|
|---|
| 108 | } else { // out of bond order, then replace
|
|---|
| 109 | if ((AddedAtomList[OtherAtom->getNr()] == NULL) && (Binder->Cyclic))
|
|---|
| 110 | ColorList[OtherAtom->getNr()] = GraphEdge::white; // unmark if it has not been queued/added, to make it available via its other bonds (cyclic)
|
|---|
| 111 | {
|
|---|
| 112 | std::stringstream output;
|
|---|
| 113 | if (Binder == Bond)
|
|---|
| 114 | output << "Not Queueing, is the Root bond";
|
|---|
| 115 | else if (ShortestPathList[OtherAtom->getNr()] >= BondOrder)
|
|---|
| 116 | output << "Not Queueing, is out of Bond Count of " << BondOrder;
|
|---|
| 117 | if (!Binder->Cyclic)
|
|---|
| 118 | output << ", is not part of a cyclic bond, saturating bond with Hydrogen.";
|
|---|
| 119 | LOG(3, output.str());
|
|---|
| 120 | }
|
|---|
| 121 | if (AddedBondList[Binder] == NULL) {
|
|---|
| 122 | if ((AddedAtomList[OtherAtom->getNr()] != NULL)) { // .. whether we add or saturate
|
|---|
| 123 | AddedBondList[Binder] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder);
|
|---|
| 124 | } else {
|
|---|
| 125 | if (saturation == DoSaturate)
|
|---|
| 126 | if (!Mol->AddHydrogenReplacementAtom(Binder, AddedAtomList[Walker->getNr()], Walker, OtherAtom, IsAngstroem))
|
|---|
| 127 | exit(1);
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 | void BreadthFirstSearchAdd::VisitedNode(molecule *Mol, atom *&Walker, atom *&OtherAtom, bond::ptr &Binder, bond::ptr &Bond)
|
|---|
| 135 | {
|
|---|
| 136 | LOG(3, "Not Adding, has already been visited.");
|
|---|
| 137 | // This has to be a cyclic bond, check whether it's present ...
|
|---|
| 138 | if (AddedBondList[Binder] == NULL) {
|
|---|
| 139 | if ((Binder != Bond) && (Binder->Cyclic) && (((ShortestPathList[Walker->getNr()] + 1) < BondOrder))) {
|
|---|
| 140 | AddedBondList[Binder] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder);
|
|---|
| 141 | } else { // if it's root bond it has to broken (otherwise we would not create the fragments)
|
|---|
| 142 | if (saturation == DoSaturate)
|
|---|
| 143 | if(!Mol->AddHydrogenReplacementAtom(Binder, AddedAtomList[Walker->getNr()], Walker, OtherAtom, IsAngstroem))
|
|---|
| 144 | exit(1);
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 | void BreadthFirstSearchAdd::operator()(molecule *Mol, atom *_Root, bond::ptr Bond, int _BondOrder)
|
|---|
| 151 | {
|
|---|
| 152 | Info FunctionInfo("BreadthFirstSearchAdd");
|
|---|
| 153 | atom *Walker = NULL, *OtherAtom = NULL;
|
|---|
| 154 | bond::ptr Binder;
|
|---|
| 155 |
|
|---|
| 156 | // add Root if not done yet
|
|---|
| 157 | if (AddedAtomList[_Root->getNr()] == NULL) // add Root if not yet present
|
|---|
| 158 | AddedAtomList[_Root->getNr()] = Mol->AddCopyAtom(_Root);
|
|---|
| 159 |
|
|---|
| 160 | Init(_Root, _BondOrder);
|
|---|
| 161 |
|
|---|
| 162 | // and go on ... Queue always contains all GraphEdge::lightgray vertices
|
|---|
| 163 | while (!BFSStack.empty()) {
|
|---|
| 164 | // we have to pop the oldest atom from stack. This keeps the atoms on the stack always of the same ShortestPath distance.
|
|---|
| 165 | // e.g. if current atom is 2, push to end of stack are of length 3, but first all of length 2 would be popped. They again
|
|---|
| 166 | // append length of 3 (their neighbours). Thus on stack we have always atoms of a certain length n at bottom of stack and
|
|---|
| 167 | // followed by n+1 till top of stack.
|
|---|
| 168 | Walker = BFSStack.front(); // pop oldest added
|
|---|
| 169 | BFSStack.pop_front();
|
|---|
| 170 | const BondList& ListOfBonds = Walker->getListOfBonds();
|
|---|
| 171 | LOG(1, "Current Walker is: " << Walker->getName() << ", and has " << ListOfBonds.size() << " bonds.");
|
|---|
| 172 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
|---|
| 173 | Runner != ListOfBonds.end();
|
|---|
| 174 | ++Runner) {
|
|---|
| 175 | if ((*Runner) != NULL) { // don't look at bond equal NULL
|
|---|
| 176 | Binder = (*Runner);
|
|---|
| 177 | OtherAtom = (*Runner)->GetOtherAtom(Walker);
|
|---|
| 178 | LOG(2, "Current OtherAtom is: " << OtherAtom->getName() << " for bond " << *(*Runner) << ".");
|
|---|
| 179 | if (ColorList[OtherAtom->getNr()] == GraphEdge::white) {
|
|---|
| 180 | UnvisitedNode(Mol, Walker, OtherAtom, Binder, Bond);
|
|---|
| 181 | } else {
|
|---|
| 182 | VisitedNode(Mol, Walker, OtherAtom, Binder, Bond);
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 | ColorList[Walker->getNr()] = GraphEdge::black;
|
|---|
| 187 | LOG(1, "Coloring Walker " << Walker->getName() << " GraphEdge::black.");
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|