[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[97ebf8] | 8 | /*
|
---|
| 9 | * SubgraphDissectionAction.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: May 9, 2010
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 21 |
|
---|
[faa1c9] | 22 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[d74077] | 23 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
| 24 |
|
---|
[97ebf8] | 25 | #include "atom.hpp"
|
---|
[d74077] | 26 | #include "bond.hpp"
|
---|
[632508] | 27 | #include "Graph/BondGraph.hpp"
|
---|
[97ebf8] | 28 | #include "config.hpp"
|
---|
[ad011c] | 29 | #include "CodePatterns/Log.hpp"
|
---|
| 30 | #include "CodePatterns/Verbose.hpp"
|
---|
[97ebf8] | 31 | #include "molecule.hpp"
|
---|
| 32 | #include "World.hpp"
|
---|
| 33 |
|
---|
| 34 | #include <iostream>
|
---|
| 35 | #include <string>
|
---|
| 36 |
|
---|
[faa1c9] | 37 | typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
|
---|
[7d8342] | 38 | typedef std::map< atomId_t, atomId_t > AtomAtomList;
|
---|
[faa1c9] | 39 |
|
---|
[1fd675] | 40 | using namespace std;
|
---|
[1015fd] | 41 |
|
---|
[1fd675] | 42 | #include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
|
---|
[1015fd] | 43 |
|
---|
[1fd675] | 44 | // and construct the stuff
|
---|
| 45 | #include "SubgraphDissectionAction.def"
|
---|
| 46 | #include "Action_impl_pre.hpp"
|
---|
| 47 | /** =========== define the function ====================== */
|
---|
[1015fd] | 48 | Action::state_ptr FragmentationSubgraphDissectionAction::performCall() {
|
---|
[1fd675] | 49 | // obtain information
|
---|
| 50 | getParametersfromValueStorage();
|
---|
| 51 |
|
---|
[2b7d1b] | 52 |
|
---|
[7d8342] | 53 | // first create stuff for undo state
|
---|
[3738f0] | 54 | LOG(0, "STATUS: Creating undo state.");
|
---|
[faa1c9] | 55 | MolAtomList moleculelist;
|
---|
| 56 | vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
|
---|
| 57 | for (vector<molecule *>::const_iterator moliter = allmolecules.begin(); moliter != allmolecules.end(); ++moliter) {
|
---|
| 58 | std::vector<atomId_t> atomlist;
|
---|
| 59 | atomlist.resize((*moliter)->size());
|
---|
| 60 | for (molecule::const_iterator atomiter = (*moliter)->begin(); atomiter != (*moliter)->end(); ++atomiter) {
|
---|
| 61 | atomlist.push_back((*atomiter)->getId());
|
---|
| 62 | }
|
---|
| 63 | moleculelist.insert( std::pair< moleculeId_t, std::vector<atomId_t> > ((*moliter)->getId(), atomlist));
|
---|
| 64 | }
|
---|
[1fd675] | 65 | FragmentationSubgraphDissectionState *UndoState = new FragmentationSubgraphDissectionState(moleculelist, params);
|
---|
[faa1c9] | 66 |
|
---|
[d74077] | 67 | // 0a. remove all present molecules
|
---|
[3738f0] | 68 | LOG(0, "STATUS: Removing all present molecules.");
|
---|
[7d8342] | 69 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
[d74077] | 70 | for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
|
---|
| 71 | molecules->erase(*MolRunner);
|
---|
| 72 | World::getInstance().destroyMolecule(*MolRunner);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | // 0b. remove all bonds and construct a molecule with all atoms
|
---|
| 76 | molecule *mol = World::getInstance().createMolecule();
|
---|
[faa1c9] | 77 | {
|
---|
| 78 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
| 79 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
[458c31] | 80 | // const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
[9d83b6] | 81 | // for(BondList::iterator BondRunner = ListOfBonds.begin();
|
---|
| 82 | // !ListOfBonds.empty();
|
---|
| 83 | // BondRunner = ListOfBonds.begin()) {
|
---|
[7d8342] | 84 | // delete(*BondRunner);
|
---|
[9d83b6] | 85 | // }
|
---|
[faa1c9] | 86 | mol->AddAtom(*AtomRunner);
|
---|
| 87 | }
|
---|
[d74077] | 88 | }
|
---|
| 89 |
|
---|
| 90 | // 1. create the bond structure of the single molecule
|
---|
[3738f0] | 91 | LOG(0, "STATUS: (Re-)constructing adjacency.");
|
---|
[458c31] | 92 | if (mol->getBondCount() == 0) {
|
---|
[f71baf] | 93 | BondGraph *BG = World::getInstance().getBondGraph();
|
---|
[3738f0] | 94 | molecule::atomVector Set = mol->getAtomSet();
|
---|
| 95 | BG->CreateAdjacency(Set);
|
---|
| 96 | // if (mol->getBondCount() == 0) {
|
---|
| 97 | // World::getInstance().destroyMolecule(mol);
|
---|
| 98 | // ELOG(1, "There are no bonds.");
|
---|
| 99 | // return Action::failure;
|
---|
| 100 | // }
|
---|
[d74077] | 101 | }
|
---|
| 102 |
|
---|
| 103 | // 2. scan for connected subgraphs
|
---|
[3738f0] | 104 | LOG(0, "STATUS: Analysing adjacency graph.");
|
---|
[d74077] | 105 | MoleculeLeafClass *Subgraphs = NULL; // list of subgraphs from DFS analysis
|
---|
[a564be] | 106 | std::deque<bond *> *BackEdgeStack = NULL;
|
---|
[d74077] | 107 | Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack);
|
---|
| 108 | delete(BackEdgeStack);
|
---|
| 109 | if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
|
---|
[faa1c9] | 110 | //World::getInstance().destroyMolecule(mol);
|
---|
[d74077] | 111 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
|
---|
| 112 | return Action::failure;
|
---|
| 113 | }
|
---|
[7d8342] | 114 |
|
---|
| 115 | //int FragmentCounter = Subgraphs->next->Count();
|
---|
[d74077] | 116 |
|
---|
[faa1c9] | 117 | // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms
|
---|
[3738f0] | 118 | LOG(0, "STATUS: Creating molecules as connected subgraphs.");
|
---|
[faa1c9] | 119 | {
|
---|
[7d8342] | 120 | {
|
---|
| 121 | atom **ListOfAtoms = NULL;
|
---|
| 122 | // 3a. re-create bond structure and insert molecules into general MoleculeListClass
|
---|
| 123 | MoleculeLeafClass *MoleculeWalker = Subgraphs->next;
|
---|
| 124 | while (MoleculeWalker->next != NULL) {
|
---|
| 125 | ListOfAtoms = NULL;
|
---|
| 126 | MoleculeWalker->FillBondStructureFromReference(mol, ListOfAtoms, true); // we want to keep the created ListOfLocalAtoms
|
---|
| 127 | molecules->insert(MoleculeWalker->Leaf);
|
---|
| 128 | MoleculeWalker = MoleculeWalker->next;
|
---|
| 129 | }
|
---|
| 130 | molecules->insert(MoleculeWalker->Leaf);
|
---|
| 131 | ListOfAtoms = NULL;
|
---|
| 132 | MoleculeWalker->FillBondStructureFromReference(mol, ListOfAtoms, true); // we want to keep the created ListOfLocalAtoms
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | // 3b. store map from new to old ids for 3d
|
---|
[faa1c9] | 136 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
[7d8342] | 137 | AtomAtomList newtooldlist;
|
---|
| 138 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner)
|
---|
| 139 | if ((*AtomRunner)->father != (*AtomRunner))
|
---|
| 140 | newtooldlist.insert( std::pair<atomId_t, atomId_t> ((*AtomRunner)->getId(),(*AtomRunner)->father->getId()) );
|
---|
| 141 |
|
---|
| 142 | {
|
---|
| 143 | // 3c. destroy the original molecule
|
---|
| 144 | for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
|
---|
| 145 | World::getInstance().destroyAtom(*AtomRunner);
|
---|
| 146 | World::getInstance().destroyMolecule(mol);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | {
|
---|
| 150 | // 3d. convert to old Ids and correct fathers (AddCopyAtom sets father to original atom, which has been destroyed).
|
---|
| 151 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
| 152 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
| 153 | World::getInstance().changeAtomId((*AtomRunner)->getId(), newtooldlist[(*AtomRunner)->getId()]);
|
---|
| 154 | (*AtomRunner)->father = *AtomRunner;
|
---|
| 155 | }
|
---|
[d74077] | 156 | }
|
---|
| 157 | }
|
---|
[2b7d1b] | 158 |
|
---|
| 159 | // 4. free Leafs
|
---|
[3738f0] | 160 | LOG(0, "STATUS: Done.");
|
---|
[2b7d1b] | 161 | MoleculeLeafClass *MolecularWalker = Subgraphs;
|
---|
[d74077] | 162 | while (MolecularWalker->next != NULL) {
|
---|
[faa1c9] | 163 | MolecularWalker->Leaf = NULL;
|
---|
[d74077] | 164 | MolecularWalker = MolecularWalker->next;
|
---|
| 165 | delete(MolecularWalker->previous);
|
---|
| 166 | }
|
---|
[faa1c9] | 167 | MolecularWalker->Leaf = NULL;
|
---|
[d74077] | 168 | delete(MolecularWalker);
|
---|
[7d8342] | 169 | DoLog(1) && (Log() << Verbose(1) << "I scanned " << molecules->ListOfMolecules.size() << " molecules." << endl);
|
---|
[d74077] | 170 |
|
---|
[faa1c9] | 171 | return Action::state_ptr(UndoState);
|
---|
[97ebf8] | 172 | }
|
---|
| 173 |
|
---|
| 174 | Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
|
---|
[faa1c9] | 175 | FragmentationSubgraphDissectionState *state = assert_cast<FragmentationSubgraphDissectionState*>(_state.get());
|
---|
| 176 |
|
---|
| 177 | {
|
---|
| 178 | // remove all present molecules
|
---|
| 179 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
| 180 | vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
|
---|
| 181 | for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
|
---|
| 182 | molecules->erase(*MolRunner);
|
---|
| 183 | World::getInstance().destroyMolecule(*MolRunner);
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | {
|
---|
| 188 | // construct the old state
|
---|
[7d8342] | 189 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
[faa1c9] | 190 | molecule *mol = NULL;
|
---|
| 191 | for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) {
|
---|
| 192 | mol = World::getInstance().createMolecule();
|
---|
| 193 | if (mol->getId() != (*iter).first)
|
---|
| 194 | World::getInstance().changeMoleculeId(mol->getId(), (*iter).first);
|
---|
[7d8342] | 195 | for (std::vector<atomId_t>::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter) {
|
---|
| 196 | atom *Walker = World::getInstance().getAtom(AtomById(*atomiter));
|
---|
| 197 | mol->AddAtom(Walker);
|
---|
| 198 | }
|
---|
| 199 | molecules->insert(mol);
|
---|
[faa1c9] | 200 | }
|
---|
| 201 | }
|
---|
[97ebf8] | 202 |
|
---|
[faa1c9] | 203 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 204 | }
|
---|
| 205 |
|
---|
| 206 | Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){
|
---|
[faa1c9] | 207 | return performCall();
|
---|
[97ebf8] | 208 | }
|
---|
| 209 |
|
---|
| 210 | bool FragmentationSubgraphDissectionAction::canUndo() {
|
---|
[faa1c9] | 211 | return true;
|
---|
[97ebf8] | 212 | }
|
---|
| 213 |
|
---|
| 214 | bool FragmentationSubgraphDissectionAction::shouldUndo() {
|
---|
[faa1c9] | 215 | return true;
|
---|
[97ebf8] | 216 | }
|
---|
[1fd675] | 217 | /** =========== end of function ====================== */
|
---|