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