/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * SubgraphDissectionAction.cpp * * Created on: May 9, 2010 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "Helpers/MemDebug.hpp" #include "Descriptors/AtomIdDescriptor.hpp" #include "Descriptors/MoleculeDescriptor.hpp" #include "atom.hpp" #include "bond.hpp" #include "bondgraph.hpp" #include "config.hpp" #include "Helpers/Log.hpp" #include "Helpers/Verbose.hpp" #include "molecule.hpp" #include "stackclass.hpp" #include "World.hpp" #include #include typedef std::map< moleculeId_t, std::vector > MolAtomList; using namespace std; #include "Actions/FragmentationAction/SubgraphDissectionAction.hpp" // and construct the stuff #include "SubgraphDissectionAction.def" #include "Action_impl_pre.hpp" /** =========== define the function ====================== */ Action::state_ptr FragmentationSubgraphDissectionAction::performCall() { // obtain information getParametersfromValueStorage(); DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl); // first create undo state MolAtomList moleculelist; vector allmolecules = World::getInstance().getAllMolecules(); for (vector::const_iterator moliter = allmolecules.begin(); moliter != allmolecules.end(); ++moliter) { std::vector atomlist; atomlist.resize((*moliter)->size()); for (molecule::const_iterator atomiter = (*moliter)->begin(); atomiter != (*moliter)->end(); ++atomiter) { atomlist.push_back((*atomiter)->getId()); } moleculelist.insert( std::pair< moleculeId_t, std::vector > ((*moliter)->getId(), atomlist)); } FragmentationSubgraphDissectionState *UndoState = new FragmentationSubgraphDissectionState(moleculelist, params); MoleculeListClass *molecules = World::getInstance().getMolecules(); config * const configuration = World::getInstance().getConfig(); // 0a. remove all present molecules for (vector::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) { molecules->erase(*MolRunner); World::getInstance().destroyMolecule(*MolRunner); } // 0b. remove all bonds and construct a molecule with all atoms molecule *mol = World::getInstance().createMolecule(); { vector allatoms = World::getInstance().getAllAtoms(); for(vector::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) { for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin()) delete(*BondRunner); mol->AddAtom(*AtomRunner); } } // 1. create the bond structure of the single molecule if (configuration->BG != NULL) { if (!configuration->BG->ConstructBondGraph(mol)) { World::getInstance().destroyMolecule(mol); DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl); return Action::failure; } } else { DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl); return Action::failure; } // 2. scan for connected subgraphs MoleculeLeafClass *Subgraphs = NULL; // list of subgraphs from DFS analysis class StackClass *BackEdgeStack = NULL; Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack); delete(BackEdgeStack); if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) { //World::getInstance().destroyMolecule(mol); DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl); return Action::failure; } int FragmentCounter = Subgraphs->next->Count(); // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms { // 3a. copy the bonds atom **ListOfAtoms = NULL; Subgraphs->next->FillBondStructureFromReference(mol, ListOfAtoms, true); // we want to keep the created ListOfLocalAtoms // 3b. correct fathers (AddCopyAtom sets father to original atom, which has been destroyed). vector allatoms = World::getInstance().getAllAtoms(); for(vector::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) { (*AtomRunner)->father = *AtomRunner; } // 3c. destroy the original molecule for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin()) World::getInstance().destroyAtom(*AtomRunner); World::getInstance().destroyMolecule(mol); } // 4. free Leafs MoleculeLeafClass *MolecularWalker = Subgraphs; while (MolecularWalker->next != NULL) { MolecularWalker->Leaf = NULL; MolecularWalker = MolecularWalker->next; delete(MolecularWalker->previous); } MolecularWalker->Leaf = NULL; delete(MolecularWalker); DoLog(1) && (Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl); return Action::state_ptr(UndoState); } Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) { FragmentationSubgraphDissectionState *state = assert_cast(_state.get()); { // remove all present molecules MoleculeListClass *molecules = World::getInstance().getMolecules(); vector allmolecules = World::getInstance().getAllMolecules(); for (vector::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) { molecules->erase(*MolRunner); World::getInstance().destroyMolecule(*MolRunner); } } { // construct the old state molecule *mol = NULL; for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) { mol = World::getInstance().createMolecule(); if (mol->getId() != (*iter).first) World::getInstance().changeMoleculeId(mol->getId(), (*iter).first); for (std::vector::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter) mol->AddAtom(World::getInstance().getAtom(AtomById(*atomiter))); } } return Action::state_ptr(_state); } Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){ return performCall(); } bool FragmentationSubgraphDissectionAction::canUndo() { return true; } bool FragmentationSubgraphDissectionAction::shouldUndo() { return true; } const string FragmentationSubgraphDissectionAction::getName() { return NAME; } /** =========== end of function ====================== */