| 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 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * SubgraphDissectionAction.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: May 9, 2010
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| 23 | #include "Descriptors/MoleculeDescriptor.hpp"
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "atom.hpp"
 | 
|---|
| 26 | #include "bond.hpp"
 | 
|---|
| 27 | #include "bondgraph.hpp"
 | 
|---|
| 28 | #include "config.hpp"
 | 
|---|
| 29 | #include "Helpers/Log.hpp"
 | 
|---|
| 30 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 31 | #include "molecule.hpp"
 | 
|---|
| 32 | #include "stackclass.hpp"
 | 
|---|
| 33 | #include "World.hpp"
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include <iostream>
 | 
|---|
| 36 | #include <string>
 | 
|---|
| 37 | 
 | 
|---|
| 38 | typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
 | 
|---|
| 39 | typedef std::map< atomId_t, atomId_t > AtomAtomList;
 | 
|---|
| 40 | 
 | 
|---|
| 41 | using namespace std;
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
 | 
|---|
| 44 | 
 | 
|---|
| 45 | // and construct the stuff
 | 
|---|
| 46 | #include "SubgraphDissectionAction.def"
 | 
|---|
| 47 | #include "Action_impl_pre.hpp"
 | 
|---|
| 48 | /** =========== define the function ====================== */
 | 
|---|
| 49 | Action::state_ptr FragmentationSubgraphDissectionAction::performCall() {
 | 
|---|
| 50 |   // obtain information
 | 
|---|
| 51 |   getParametersfromValueStorage();
 | 
|---|
| 52 | 
 | 
|---|
| 53 |   DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl);
 | 
|---|
| 54 | 
 | 
|---|
| 55 |   // first create stuff for undo state
 | 
|---|
| 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 |   }
 | 
|---|
| 66 |   FragmentationSubgraphDissectionState *UndoState = new FragmentationSubgraphDissectionState(moleculelist, params);
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   // 0a. remove all present molecules
 | 
|---|
| 69 |   MoleculeListClass *molecules = World::getInstance().getMolecules();
 | 
|---|
| 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();
 | 
|---|
| 77 |   int BondCount = 0;
 | 
|---|
| 78 |   {
 | 
|---|
| 79 |     vector <atom *> allatoms = World::getInstance().getAllAtoms();
 | 
|---|
| 80 |     for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
 | 
|---|
| 81 |       BondCount += (*AtomRunner)->ListOfBonds.size();
 | 
|---|
| 82 | //      for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
 | 
|---|
| 83 | //        delete(*BondRunner);
 | 
|---|
| 84 |       mol->AddAtom(*AtomRunner);
 | 
|---|
| 85 |     }
 | 
|---|
| 86 |   }
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   // 1. create the bond structure of the single molecule
 | 
|---|
| 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);
 | 
|---|
| 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)) {
 | 
|---|
| 109 |     //World::getInstance().destroyMolecule(mol);
 | 
|---|
| 110 |     DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
 | 
|---|
| 111 |     return Action::failure;
 | 
|---|
| 112 |   }
 | 
|---|
| 113 | 
 | 
|---|
| 114 |   //int FragmentCounter = Subgraphs->next->Count();
 | 
|---|
| 115 | 
 | 
|---|
| 116 |   // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms
 | 
|---|
| 117 |   {
 | 
|---|
| 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
 | 
|---|
| 134 |     vector <atom *> allatoms = World::getInstance().getAllAtoms();
 | 
|---|
| 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 |       }
 | 
|---|
| 154 |     }
 | 
|---|
| 155 |   }
 | 
|---|
| 156 | 
 | 
|---|
| 157 |   // 4. free Leafs
 | 
|---|
| 158 |   MoleculeLeafClass *MolecularWalker = Subgraphs;
 | 
|---|
| 159 |   while (MolecularWalker->next != NULL) {
 | 
|---|
| 160 |     MolecularWalker->Leaf = NULL;
 | 
|---|
| 161 |     MolecularWalker = MolecularWalker->next;
 | 
|---|
| 162 |     delete(MolecularWalker->previous);
 | 
|---|
| 163 |   }
 | 
|---|
| 164 |   MolecularWalker->Leaf = NULL;
 | 
|---|
| 165 |   delete(MolecularWalker);
 | 
|---|
| 166 |   DoLog(1) && (Log() << Verbose(1) << "I scanned " << molecules->ListOfMolecules.size() << " molecules." << endl);
 | 
|---|
| 167 | 
 | 
|---|
| 168 |   return Action::state_ptr(UndoState);
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 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
 | 
|---|
| 186 |     MoleculeListClass *molecules = World::getInstance().getMolecules();
 | 
|---|
| 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);
 | 
|---|
| 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);
 | 
|---|
| 197 |     }
 | 
|---|
| 198 |   }
 | 
|---|
| 199 | 
 | 
|---|
| 200 |   return Action::state_ptr(_state);
 | 
|---|
| 201 | }
 | 
|---|
| 202 | 
 | 
|---|
| 203 | Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 204 |   return performCall();
 | 
|---|
| 205 | }
 | 
|---|
| 206 | 
 | 
|---|
| 207 | bool FragmentationSubgraphDissectionAction::canUndo() {
 | 
|---|
| 208 |   return true;
 | 
|---|
| 209 | }
 | 
|---|
| 210 | 
 | 
|---|
| 211 | bool FragmentationSubgraphDissectionAction::shouldUndo() {
 | 
|---|
| 212 |   return true;
 | 
|---|
| 213 | }
 | 
|---|
| 214 | 
 | 
|---|
| 215 | const string FragmentationSubgraphDissectionAction::getName() {
 | 
|---|
| 216 |   return NAME;
 | 
|---|
| 217 | }
 | 
|---|
| 218 | /** =========== end of function ====================== */
 | 
|---|