[97ebf8] | 1 | /*
|
---|
| 2 | * SubgraphDissectionAction.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: May 9, 2010
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[bf3817] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
[112b09] | 13 | #include "Helpers/MemDebug.hpp"
|
---|
| 14 |
|
---|
[97ebf8] | 15 | #include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
|
---|
[0430e3] | 16 | #include "Actions/ActionRegistry.hpp"
|
---|
[faa1c9] | 17 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[d74077] | 18 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
| 19 |
|
---|
[97ebf8] | 20 | #include "atom.hpp"
|
---|
[d74077] | 21 | #include "bond.hpp"
|
---|
| 22 | #include "bondgraph.hpp"
|
---|
[97ebf8] | 23 | #include "config.hpp"
|
---|
[952f38] | 24 | #include "Helpers/Log.hpp"
|
---|
[faa1c9] | 25 | #include "Helpers/Verbose.hpp"
|
---|
[97ebf8] | 26 | #include "molecule.hpp"
|
---|
| 27 | #include "stackclass.hpp"
|
---|
| 28 | #include "World.hpp"
|
---|
| 29 |
|
---|
| 30 | #include <iostream>
|
---|
| 31 | #include <string>
|
---|
| 32 |
|
---|
| 33 | using namespace std;
|
---|
| 34 |
|
---|
| 35 | #include "UIElements/UIFactory.hpp"
|
---|
| 36 | #include "UIElements/Dialog.hpp"
|
---|
[861874] | 37 | #include "Actions/ValueStorage.hpp"
|
---|
[97ebf8] | 38 |
|
---|
[faa1c9] | 39 | // memento to remember the state when undoing
|
---|
| 40 |
|
---|
| 41 | typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
|
---|
| 42 |
|
---|
| 43 | class FragmentationSubgraphDissectionState : public ActionState {
|
---|
| 44 | public:
|
---|
| 45 | FragmentationSubgraphDissectionState(const MolAtomList &_moleculelist) :
|
---|
| 46 | moleculelist(_moleculelist)
|
---|
| 47 | {}
|
---|
| 48 | MolAtomList moleculelist;
|
---|
| 49 | };
|
---|
| 50 |
|
---|
[6866aa] | 51 | const char FragmentationSubgraphDissectionAction::NAME[] = "subgraph-dissect";
|
---|
[97ebf8] | 52 |
|
---|
| 53 | FragmentationSubgraphDissectionAction::FragmentationSubgraphDissectionAction() :
|
---|
| 54 | Action(NAME)
|
---|
| 55 | {}
|
---|
| 56 |
|
---|
| 57 | FragmentationSubgraphDissectionAction::~FragmentationSubgraphDissectionAction()
|
---|
| 58 | {}
|
---|
| 59 |
|
---|
[2b7d1b] | 60 | /** Dissects given \a *mol into connected subgraphs and inserts them as new molecules but with old atoms into \a this.
|
---|
| 61 | */
|
---|
[4ff081] | 62 | void FragmentationSubgraphDissection() {
|
---|
| 63 | ActionRegistry::getInstance().getActionByName(FragmentationSubgraphDissectionAction::NAME)->call(Action::NonInteractive);
|
---|
| 64 | };
|
---|
| 65 |
|
---|
[047878] | 66 | Dialog* FragmentationSubgraphDissectionAction::fillDialog(Dialog *dialog) {
|
---|
| 67 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
[97ebf8] | 68 |
|
---|
| 69 | dialog->queryEmpty(NAME, MapOfActions::getInstance().getDescription(NAME));
|
---|
| 70 |
|
---|
[1015fd] | 71 | return dialog;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | Action::state_ptr FragmentationSubgraphDissectionAction::performCall() {
|
---|
| 76 | DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl);
|
---|
[2b7d1b] | 77 |
|
---|
[faa1c9] | 78 | // first create undo state
|
---|
| 79 | MolAtomList moleculelist;
|
---|
| 80 | vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
|
---|
| 81 | for (vector<molecule *>::const_iterator moliter = allmolecules.begin(); moliter != allmolecules.end(); ++moliter) {
|
---|
| 82 | std::vector<atomId_t> atomlist;
|
---|
| 83 | atomlist.resize((*moliter)->size());
|
---|
| 84 | for (molecule::const_iterator atomiter = (*moliter)->begin(); atomiter != (*moliter)->end(); ++atomiter) {
|
---|
| 85 | atomlist.push_back((*atomiter)->getId());
|
---|
| 86 | }
|
---|
| 87 | moleculelist.insert( std::pair< moleculeId_t, std::vector<atomId_t> > ((*moliter)->getId(), atomlist));
|
---|
| 88 | }
|
---|
| 89 | FragmentationSubgraphDissectionState *UndoState = new FragmentationSubgraphDissectionState(moleculelist);
|
---|
| 90 |
|
---|
[1015fd] | 91 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
[d74077] | 92 | config * const configuration = World::getInstance().getConfig();
|
---|
| 93 |
|
---|
| 94 | // 0a. remove all present molecules
|
---|
| 95 | for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
|
---|
| 96 | molecules->erase(*MolRunner);
|
---|
| 97 | World::getInstance().destroyMolecule(*MolRunner);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | // 0b. remove all bonds and construct a molecule with all atoms
|
---|
| 101 | molecule *mol = World::getInstance().createMolecule();
|
---|
[faa1c9] | 102 | {
|
---|
| 103 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
| 104 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
| 105 | for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
|
---|
| 106 | delete(*BondRunner);
|
---|
| 107 | mol->AddAtom(*AtomRunner);
|
---|
| 108 | }
|
---|
[d74077] | 109 | }
|
---|
| 110 |
|
---|
| 111 | // 1. create the bond structure of the single molecule
|
---|
| 112 | if (configuration->BG != NULL) {
|
---|
| 113 | if (!configuration->BG->ConstructBondGraph(mol)) {
|
---|
| 114 | World::getInstance().destroyMolecule(mol);
|
---|
| 115 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl);
|
---|
| 116 | return Action::failure;
|
---|
| 117 | }
|
---|
| 118 | } else {
|
---|
| 119 | DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl);
|
---|
| 120 | return Action::failure;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | // 2. scan for connected subgraphs
|
---|
| 124 | MoleculeLeafClass *Subgraphs = NULL; // list of subgraphs from DFS analysis
|
---|
| 125 | class StackClass<bond *> *BackEdgeStack = NULL;
|
---|
| 126 | Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack);
|
---|
| 127 | delete(BackEdgeStack);
|
---|
| 128 | if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
|
---|
[faa1c9] | 129 | //World::getInstance().destroyMolecule(mol);
|
---|
[d74077] | 130 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
|
---|
| 131 | return Action::failure;
|
---|
| 132 | }
|
---|
[faa1c9] | 133 | int FragmentCounter = Subgraphs->next->Count();
|
---|
[d74077] | 134 |
|
---|
[faa1c9] | 135 | // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms
|
---|
| 136 | {
|
---|
| 137 | // 3a. destroy the original molecule
|
---|
| 138 | for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
|
---|
| 139 | World::getInstance().destroyAtom(*AtomRunner);
|
---|
| 140 | World::getInstance().destroyMolecule(mol);
|
---|
| 141 | // 3b. correct fathers (AddCopyAtom sets father to original atom, which has been destroyed).
|
---|
| 142 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
| 143 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
| 144 | (*AtomRunner)->father = *AtomRunner;
|
---|
[d74077] | 145 | }
|
---|
| 146 | }
|
---|
[2b7d1b] | 147 |
|
---|
| 148 | // 4. free Leafs
|
---|
| 149 | MoleculeLeafClass *MolecularWalker = Subgraphs;
|
---|
[d74077] | 150 | while (MolecularWalker->next != NULL) {
|
---|
[faa1c9] | 151 | MolecularWalker->Leaf = NULL;
|
---|
[d74077] | 152 | MolecularWalker = MolecularWalker->next;
|
---|
| 153 | delete(MolecularWalker->previous);
|
---|
| 154 | }
|
---|
[faa1c9] | 155 | MolecularWalker->Leaf = NULL;
|
---|
[d74077] | 156 | delete(MolecularWalker);
|
---|
| 157 | DoLog(1) && (Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl);
|
---|
| 158 |
|
---|
[faa1c9] | 159 | return Action::state_ptr(UndoState);
|
---|
[97ebf8] | 160 | }
|
---|
| 161 |
|
---|
| 162 | Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
|
---|
[faa1c9] | 163 | FragmentationSubgraphDissectionState *state = assert_cast<FragmentationSubgraphDissectionState*>(_state.get());
|
---|
| 164 |
|
---|
| 165 | {
|
---|
| 166 | // remove all present molecules
|
---|
| 167 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
| 168 | vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
|
---|
| 169 | for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
|
---|
| 170 | molecules->erase(*MolRunner);
|
---|
| 171 | World::getInstance().destroyMolecule(*MolRunner);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | {
|
---|
| 176 | // construct the old state
|
---|
| 177 | molecule *mol = NULL;
|
---|
| 178 | for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) {
|
---|
| 179 | mol = World::getInstance().createMolecule();
|
---|
| 180 | if (mol->getId() != (*iter).first)
|
---|
| 181 | World::getInstance().changeMoleculeId(mol->getId(), (*iter).first);
|
---|
| 182 | for (std::vector<atomId_t>::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter)
|
---|
| 183 | mol->AddAtom(World::getInstance().getAtom(AtomById(*atomiter)));
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
[97ebf8] | 186 |
|
---|
[faa1c9] | 187 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 188 | }
|
---|
| 189 |
|
---|
| 190 | Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){
|
---|
[faa1c9] | 191 | return performCall();
|
---|
[97ebf8] | 192 | }
|
---|
| 193 |
|
---|
| 194 | bool FragmentationSubgraphDissectionAction::canUndo() {
|
---|
[faa1c9] | 195 | return true;
|
---|
[97ebf8] | 196 | }
|
---|
| 197 |
|
---|
| 198 | bool FragmentationSubgraphDissectionAction::shouldUndo() {
|
---|
[faa1c9] | 199 | return true;
|
---|
[97ebf8] | 200 | }
|
---|
| 201 |
|
---|
| 202 | const string FragmentationSubgraphDissectionAction::getName() {
|
---|
| 203 | return NAME;
|
---|
| 204 | }
|
---|