[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;
|
---|
| 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 |
|
---|
[1015fd] | 52 | DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl);
|
---|
[2b7d1b] | 53 |
|
---|
[faa1c9] | 54 | // first create undo state
|
---|
| 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 |
|
---|
[1015fd] | 67 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
[d74077] | 68 | config * const configuration = World::getInstance().getConfig();
|
---|
| 69 |
|
---|
| 70 | // 0a. remove all present molecules
|
---|
| 71 | for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
|
---|
| 72 | molecules->erase(*MolRunner);
|
---|
| 73 | World::getInstance().destroyMolecule(*MolRunner);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | // 0b. remove all bonds and construct a molecule with all atoms
|
---|
| 77 | molecule *mol = World::getInstance().createMolecule();
|
---|
[faa1c9] | 78 | {
|
---|
| 79 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
| 80 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
| 81 | for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
|
---|
| 82 | delete(*BondRunner);
|
---|
| 83 | mol->AddAtom(*AtomRunner);
|
---|
| 84 | }
|
---|
[d74077] | 85 | }
|
---|
| 86 |
|
---|
| 87 | // 1. create the bond structure of the single molecule
|
---|
| 88 | if (configuration->BG != NULL) {
|
---|
| 89 | if (!configuration->BG->ConstructBondGraph(mol)) {
|
---|
| 90 | World::getInstance().destroyMolecule(mol);
|
---|
| 91 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl);
|
---|
| 92 | return Action::failure;
|
---|
| 93 | }
|
---|
| 94 | } else {
|
---|
| 95 | DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl);
|
---|
| 96 | return Action::failure;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | // 2. scan for connected subgraphs
|
---|
| 100 | MoleculeLeafClass *Subgraphs = NULL; // list of subgraphs from DFS analysis
|
---|
| 101 | class StackClass<bond *> *BackEdgeStack = NULL;
|
---|
| 102 | Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack);
|
---|
| 103 | delete(BackEdgeStack);
|
---|
| 104 | if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
|
---|
[faa1c9] | 105 | //World::getInstance().destroyMolecule(mol);
|
---|
[d74077] | 106 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
|
---|
| 107 | return Action::failure;
|
---|
| 108 | }
|
---|
[faa1c9] | 109 | int FragmentCounter = Subgraphs->next->Count();
|
---|
[d74077] | 110 |
|
---|
[faa1c9] | 111 | // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms
|
---|
| 112 | {
|
---|
[fbcd5c] | 113 | // 3a. copy the bonds
|
---|
| 114 | atom **ListOfAtoms = NULL;
|
---|
| 115 | Subgraphs->next->FillBondStructureFromReference(mol, ListOfAtoms, true); // we want to keep the created ListOfLocalAtoms
|
---|
[faa1c9] | 116 | // 3b. correct fathers (AddCopyAtom sets father to original atom, which has been destroyed).
|
---|
| 117 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
| 118 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
| 119 | (*AtomRunner)->father = *AtomRunner;
|
---|
[d74077] | 120 | }
|
---|
[fbcd5c] | 121 | // 3c. destroy the original molecule
|
---|
| 122 | for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
|
---|
| 123 | World::getInstance().destroyAtom(*AtomRunner);
|
---|
| 124 | World::getInstance().destroyMolecule(mol);
|
---|
[d74077] | 125 | }
|
---|
[2b7d1b] | 126 |
|
---|
| 127 | // 4. free Leafs
|
---|
| 128 | MoleculeLeafClass *MolecularWalker = Subgraphs;
|
---|
[d74077] | 129 | while (MolecularWalker->next != NULL) {
|
---|
[faa1c9] | 130 | MolecularWalker->Leaf = NULL;
|
---|
[d74077] | 131 | MolecularWalker = MolecularWalker->next;
|
---|
| 132 | delete(MolecularWalker->previous);
|
---|
| 133 | }
|
---|
[faa1c9] | 134 | MolecularWalker->Leaf = NULL;
|
---|
[d74077] | 135 | delete(MolecularWalker);
|
---|
| 136 | DoLog(1) && (Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl);
|
---|
| 137 |
|
---|
[faa1c9] | 138 | return Action::state_ptr(UndoState);
|
---|
[97ebf8] | 139 | }
|
---|
| 140 |
|
---|
| 141 | Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
|
---|
[faa1c9] | 142 | FragmentationSubgraphDissectionState *state = assert_cast<FragmentationSubgraphDissectionState*>(_state.get());
|
---|
| 143 |
|
---|
| 144 | {
|
---|
| 145 | // remove all present molecules
|
---|
| 146 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
| 147 | vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
|
---|
| 148 | for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
|
---|
| 149 | molecules->erase(*MolRunner);
|
---|
| 150 | World::getInstance().destroyMolecule(*MolRunner);
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | {
|
---|
| 155 | // construct the old state
|
---|
| 156 | molecule *mol = NULL;
|
---|
| 157 | for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) {
|
---|
| 158 | mol = World::getInstance().createMolecule();
|
---|
| 159 | if (mol->getId() != (*iter).first)
|
---|
| 160 | World::getInstance().changeMoleculeId(mol->getId(), (*iter).first);
|
---|
| 161 | for (std::vector<atomId_t>::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter)
|
---|
| 162 | mol->AddAtom(World::getInstance().getAtom(AtomById(*atomiter)));
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
[97ebf8] | 165 |
|
---|
[faa1c9] | 166 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 167 | }
|
---|
| 168 |
|
---|
| 169 | Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){
|
---|
[faa1c9] | 170 | return performCall();
|
---|
[97ebf8] | 171 | }
|
---|
| 172 |
|
---|
| 173 | bool FragmentationSubgraphDissectionAction::canUndo() {
|
---|
[faa1c9] | 174 | return true;
|
---|
[97ebf8] | 175 | }
|
---|
| 176 |
|
---|
| 177 | bool FragmentationSubgraphDissectionAction::shouldUndo() {
|
---|
[faa1c9] | 178 | return true;
|
---|
[97ebf8] | 179 | }
|
---|
| 180 |
|
---|
| 181 | const string FragmentationSubgraphDissectionAction::getName() {
|
---|
| 182 | return NAME;
|
---|
| 183 | }
|
---|
[1fd675] | 184 | /** =========== end of function ====================== */
|
---|