[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 |
|
---|
[112b09] | 20 | #include "Helpers/MemDebug.hpp"
|
---|
| 21 |
|
---|
[faa1c9] | 22 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[d74077] | 23 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
| 24 |
|
---|
[97ebf8] | 25 | #include "atom.hpp"
|
---|
[d74077] | 26 | #include "bond.hpp"
|
---|
| 27 | #include "bondgraph.hpp"
|
---|
[97ebf8] | 28 | #include "config.hpp"
|
---|
[952f38] | 29 | #include "Helpers/Log.hpp"
|
---|
[faa1c9] | 30 | #include "Helpers/Verbose.hpp"
|
---|
[97ebf8] | 31 | #include "molecule.hpp"
|
---|
| 32 | #include "stackclass.hpp"
|
---|
| 33 | #include "World.hpp"
|
---|
| 34 |
|
---|
| 35 | #include <iostream>
|
---|
| 36 | #include <string>
|
---|
| 37 |
|
---|
[faa1c9] | 38 | typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
|
---|
[7d8342] | 39 | typedef std::map< atomId_t, atomId_t > AtomAtomList;
|
---|
[faa1c9] | 40 |
|
---|
[1fd675] | 41 | using namespace std;
|
---|
[1015fd] | 42 |
|
---|
[1fd675] | 43 | #include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
|
---|
[1015fd] | 44 |
|
---|
[1fd675] | 45 | // and construct the stuff
|
---|
| 46 | #include "SubgraphDissectionAction.def"
|
---|
| 47 | #include "Action_impl_pre.hpp"
|
---|
| 48 | /** =========== define the function ====================== */
|
---|
[1015fd] | 49 | Action::state_ptr FragmentationSubgraphDissectionAction::performCall() {
|
---|
[1fd675] | 50 | // obtain information
|
---|
| 51 | getParametersfromValueStorage();
|
---|
| 52 |
|
---|
[1015fd] | 53 | DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl);
|
---|
[2b7d1b] | 54 |
|
---|
[7d8342] | 55 | // first create stuff for undo state
|
---|
[faa1c9] | 56 | MolAtomList moleculelist;
|
---|
| 57 | vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
|
---|
| 58 | for (vector<molecule *>::const_iterator moliter = allmolecules.begin(); moliter != allmolecules.end(); ++moliter) {
|
---|
| 59 | std::vector<atomId_t> atomlist;
|
---|
| 60 | atomlist.resize((*moliter)->size());
|
---|
| 61 | for (molecule::const_iterator atomiter = (*moliter)->begin(); atomiter != (*moliter)->end(); ++atomiter) {
|
---|
| 62 | atomlist.push_back((*atomiter)->getId());
|
---|
| 63 | }
|
---|
| 64 | moleculelist.insert( std::pair< moleculeId_t, std::vector<atomId_t> > ((*moliter)->getId(), atomlist));
|
---|
| 65 | }
|
---|
[1fd675] | 66 | FragmentationSubgraphDissectionState *UndoState = new FragmentationSubgraphDissectionState(moleculelist, params);
|
---|
[faa1c9] | 67 |
|
---|
[d74077] | 68 | // 0a. remove 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();
|
---|
[7d8342] | 77 | int BondCount = 0;
|
---|
[faa1c9] | 78 | {
|
---|
| 79 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
| 80 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
[7d8342] | 81 | BondCount += (*AtomRunner)->ListOfBonds.size();
|
---|
| 82 | // for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
|
---|
| 83 | // delete(*BondRunner);
|
---|
[faa1c9] | 84 | mol->AddAtom(*AtomRunner);
|
---|
| 85 | }
|
---|
[d74077] | 86 | }
|
---|
| 87 |
|
---|
| 88 | // 1. create the bond structure of the single molecule
|
---|
[7d8342] | 89 | if (BondCount == 0) {
|
---|
| 90 | config * const configuration = World::getInstance().getConfig();
|
---|
| 91 | if ((configuration->BG != NULL)) {
|
---|
| 92 | if (!configuration->BG->ConstructBondGraph(mol)) {
|
---|
| 93 | World::getInstance().destroyMolecule(mol);
|
---|
| 94 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl);
|
---|
| 95 | return Action::failure;
|
---|
| 96 | }
|
---|
| 97 | } else {
|
---|
| 98 | DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl);
|
---|
[d74077] | 99 | return Action::failure;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | // 2. scan for connected subgraphs
|
---|
| 104 | MoleculeLeafClass *Subgraphs = NULL; // list of subgraphs from DFS analysis
|
---|
| 105 | class StackClass<bond *> *BackEdgeStack = NULL;
|
---|
| 106 | Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack);
|
---|
| 107 | delete(BackEdgeStack);
|
---|
| 108 | if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
|
---|
[faa1c9] | 109 | //World::getInstance().destroyMolecule(mol);
|
---|
[d74077] | 110 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
|
---|
| 111 | return Action::failure;
|
---|
| 112 | }
|
---|
[7d8342] | 113 |
|
---|
| 114 | //int FragmentCounter = Subgraphs->next->Count();
|
---|
[d74077] | 115 |
|
---|
[faa1c9] | 116 | // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms
|
---|
| 117 | {
|
---|
[7d8342] | 118 | {
|
---|
| 119 | atom **ListOfAtoms = NULL;
|
---|
| 120 | // 3a. re-create bond structure and insert molecules into general MoleculeListClass
|
---|
| 121 | MoleculeLeafClass *MoleculeWalker = Subgraphs->next;
|
---|
| 122 | while (MoleculeWalker->next != NULL) {
|
---|
| 123 | ListOfAtoms = NULL;
|
---|
| 124 | MoleculeWalker->FillBondStructureFromReference(mol, ListOfAtoms, true); // we want to keep the created ListOfLocalAtoms
|
---|
| 125 | molecules->insert(MoleculeWalker->Leaf);
|
---|
| 126 | MoleculeWalker = MoleculeWalker->next;
|
---|
| 127 | }
|
---|
| 128 | molecules->insert(MoleculeWalker->Leaf);
|
---|
| 129 | ListOfAtoms = NULL;
|
---|
| 130 | MoleculeWalker->FillBondStructureFromReference(mol, ListOfAtoms, true); // we want to keep the created ListOfLocalAtoms
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | // 3b. store map from new to old ids for 3d
|
---|
[faa1c9] | 134 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
[7d8342] | 135 | AtomAtomList newtooldlist;
|
---|
| 136 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner)
|
---|
| 137 | if ((*AtomRunner)->father != (*AtomRunner))
|
---|
| 138 | newtooldlist.insert( std::pair<atomId_t, atomId_t> ((*AtomRunner)->getId(),(*AtomRunner)->father->getId()) );
|
---|
| 139 |
|
---|
| 140 | {
|
---|
| 141 | // 3c. destroy the original molecule
|
---|
| 142 | for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
|
---|
| 143 | World::getInstance().destroyAtom(*AtomRunner);
|
---|
| 144 | World::getInstance().destroyMolecule(mol);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | {
|
---|
| 148 | // 3d. convert to old Ids and correct fathers (AddCopyAtom sets father to original atom, which has been destroyed).
|
---|
| 149 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
| 150 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
| 151 | World::getInstance().changeAtomId((*AtomRunner)->getId(), newtooldlist[(*AtomRunner)->getId()]);
|
---|
| 152 | (*AtomRunner)->father = *AtomRunner;
|
---|
| 153 | }
|
---|
[d74077] | 154 | }
|
---|
| 155 | }
|
---|
[2b7d1b] | 156 |
|
---|
| 157 | // 4. free Leafs
|
---|
| 158 | MoleculeLeafClass *MolecularWalker = Subgraphs;
|
---|
[d74077] | 159 | while (MolecularWalker->next != NULL) {
|
---|
[faa1c9] | 160 | MolecularWalker->Leaf = NULL;
|
---|
[d74077] | 161 | MolecularWalker = MolecularWalker->next;
|
---|
| 162 | delete(MolecularWalker->previous);
|
---|
| 163 | }
|
---|
[faa1c9] | 164 | MolecularWalker->Leaf = NULL;
|
---|
[d74077] | 165 | delete(MolecularWalker);
|
---|
[7d8342] | 166 | DoLog(1) && (Log() << Verbose(1) << "I scanned " << molecules->ListOfMolecules.size() << " molecules." << endl);
|
---|
[d74077] | 167 |
|
---|
[faa1c9] | 168 | return Action::state_ptr(UndoState);
|
---|
[97ebf8] | 169 | }
|
---|
| 170 |
|
---|
| 171 | Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
|
---|
[faa1c9] | 172 | FragmentationSubgraphDissectionState *state = assert_cast<FragmentationSubgraphDissectionState*>(_state.get());
|
---|
| 173 |
|
---|
| 174 | {
|
---|
| 175 | // remove all present molecules
|
---|
| 176 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
| 177 | vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
|
---|
| 178 | for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
|
---|
| 179 | molecules->erase(*MolRunner);
|
---|
| 180 | World::getInstance().destroyMolecule(*MolRunner);
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | {
|
---|
| 185 | // construct the old state
|
---|
[7d8342] | 186 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
[faa1c9] | 187 | molecule *mol = NULL;
|
---|
| 188 | for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) {
|
---|
| 189 | mol = World::getInstance().createMolecule();
|
---|
| 190 | if (mol->getId() != (*iter).first)
|
---|
| 191 | World::getInstance().changeMoleculeId(mol->getId(), (*iter).first);
|
---|
[7d8342] | 192 | for (std::vector<atomId_t>::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter) {
|
---|
| 193 | atom *Walker = World::getInstance().getAtom(AtomById(*atomiter));
|
---|
| 194 | mol->AddAtom(Walker);
|
---|
| 195 | }
|
---|
| 196 | molecules->insert(mol);
|
---|
[faa1c9] | 197 | }
|
---|
| 198 | }
|
---|
[97ebf8] | 199 |
|
---|
[faa1c9] | 200 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 201 | }
|
---|
| 202 |
|
---|
| 203 | Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){
|
---|
[faa1c9] | 204 | return performCall();
|
---|
[97ebf8] | 205 | }
|
---|
| 206 |
|
---|
| 207 | bool FragmentationSubgraphDissectionAction::canUndo() {
|
---|
[faa1c9] | 208 | return true;
|
---|
[97ebf8] | 209 | }
|
---|
| 210 |
|
---|
| 211 | bool FragmentationSubgraphDissectionAction::shouldUndo() {
|
---|
[faa1c9] | 212 | return true;
|
---|
[97ebf8] | 213 | }
|
---|
| 214 |
|
---|
| 215 | const string FragmentationSubgraphDissectionAction::getName() {
|
---|
| 216 | return NAME;
|
---|
| 217 | }
|
---|
[1fd675] | 218 | /** =========== end of function ====================== */
|
---|