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 "World.hpp"
|
---|
33 |
|
---|
34 | #include <iostream>
|
---|
35 | #include <string>
|
---|
36 |
|
---|
37 | typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
|
---|
38 | typedef std::map< atomId_t, atomId_t > AtomAtomList;
|
---|
39 |
|
---|
40 | using namespace std;
|
---|
41 |
|
---|
42 | #include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
|
---|
43 |
|
---|
44 | // and construct the stuff
|
---|
45 | #include "SubgraphDissectionAction.def"
|
---|
46 | #include "Action_impl_pre.hpp"
|
---|
47 | /** =========== define the function ====================== */
|
---|
48 | Action::state_ptr FragmentationSubgraphDissectionAction::performCall() {
|
---|
49 | // obtain information
|
---|
50 | getParametersfromValueStorage();
|
---|
51 |
|
---|
52 | DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl);
|
---|
53 |
|
---|
54 | // first create stuff for 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 | }
|
---|
65 | FragmentationSubgraphDissectionState *UndoState = new FragmentationSubgraphDissectionState(moleculelist, params);
|
---|
66 |
|
---|
67 | // 0a. remove all present molecules
|
---|
68 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
69 | for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
|
---|
70 | molecules->erase(*MolRunner);
|
---|
71 | World::getInstance().destroyMolecule(*MolRunner);
|
---|
72 | }
|
---|
73 |
|
---|
74 | // 0b. remove all bonds and construct a molecule with all atoms
|
---|
75 | molecule *mol = World::getInstance().createMolecule();
|
---|
76 | int BondCount = 0;
|
---|
77 | {
|
---|
78 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
79 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
80 | BondCount += (*AtomRunner)->ListOfBonds.size();
|
---|
81 | // for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
|
---|
82 | // delete(*BondRunner);
|
---|
83 | mol->AddAtom(*AtomRunner);
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | // 1. create the bond structure of the single molecule
|
---|
88 | if (BondCount == 0) {
|
---|
89 | config * const configuration = World::getInstance().getConfig();
|
---|
90 | if ((configuration->BG != NULL)) {
|
---|
91 | if (!configuration->BG->ConstructBondGraph(mol)) {
|
---|
92 | World::getInstance().destroyMolecule(mol);
|
---|
93 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl);
|
---|
94 | return Action::failure;
|
---|
95 | }
|
---|
96 | } else {
|
---|
97 | DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl);
|
---|
98 | return Action::failure;
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | // 2. scan for connected subgraphs
|
---|
103 | MoleculeLeafClass *Subgraphs = NULL; // list of subgraphs from DFS analysis
|
---|
104 | std::deque<bond *> *BackEdgeStack = NULL;
|
---|
105 | Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack);
|
---|
106 | delete(BackEdgeStack);
|
---|
107 | if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
|
---|
108 | //World::getInstance().destroyMolecule(mol);
|
---|
109 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
|
---|
110 | return Action::failure;
|
---|
111 | }
|
---|
112 |
|
---|
113 | //int FragmentCounter = Subgraphs->next->Count();
|
---|
114 |
|
---|
115 | // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms
|
---|
116 | {
|
---|
117 | {
|
---|
118 | atom **ListOfAtoms = NULL;
|
---|
119 | // 3a. re-create bond structure and insert molecules into general MoleculeListClass
|
---|
120 | MoleculeLeafClass *MoleculeWalker = Subgraphs->next;
|
---|
121 | while (MoleculeWalker->next != NULL) {
|
---|
122 | ListOfAtoms = NULL;
|
---|
123 | MoleculeWalker->FillBondStructureFromReference(mol, ListOfAtoms, true); // we want to keep the created ListOfLocalAtoms
|
---|
124 | molecules->insert(MoleculeWalker->Leaf);
|
---|
125 | MoleculeWalker = MoleculeWalker->next;
|
---|
126 | }
|
---|
127 | molecules->insert(MoleculeWalker->Leaf);
|
---|
128 | ListOfAtoms = NULL;
|
---|
129 | MoleculeWalker->FillBondStructureFromReference(mol, ListOfAtoms, true); // we want to keep the created ListOfLocalAtoms
|
---|
130 | }
|
---|
131 |
|
---|
132 | // 3b. store map from new to old ids for 3d
|
---|
133 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
134 | AtomAtomList newtooldlist;
|
---|
135 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner)
|
---|
136 | if ((*AtomRunner)->father != (*AtomRunner))
|
---|
137 | newtooldlist.insert( std::pair<atomId_t, atomId_t> ((*AtomRunner)->getId(),(*AtomRunner)->father->getId()) );
|
---|
138 |
|
---|
139 | {
|
---|
140 | // 3c. destroy the original molecule
|
---|
141 | for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
|
---|
142 | World::getInstance().destroyAtom(*AtomRunner);
|
---|
143 | World::getInstance().destroyMolecule(mol);
|
---|
144 | }
|
---|
145 |
|
---|
146 | {
|
---|
147 | // 3d. convert to old Ids and correct fathers (AddCopyAtom sets father to original atom, which has been destroyed).
|
---|
148 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
149 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
150 | World::getInstance().changeAtomId((*AtomRunner)->getId(), newtooldlist[(*AtomRunner)->getId()]);
|
---|
151 | (*AtomRunner)->father = *AtomRunner;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | // 4. free Leafs
|
---|
157 | MoleculeLeafClass *MolecularWalker = Subgraphs;
|
---|
158 | while (MolecularWalker->next != NULL) {
|
---|
159 | MolecularWalker->Leaf = NULL;
|
---|
160 | MolecularWalker = MolecularWalker->next;
|
---|
161 | delete(MolecularWalker->previous);
|
---|
162 | }
|
---|
163 | MolecularWalker->Leaf = NULL;
|
---|
164 | delete(MolecularWalker);
|
---|
165 | DoLog(1) && (Log() << Verbose(1) << "I scanned " << molecules->ListOfMolecules.size() << " molecules." << endl);
|
---|
166 |
|
---|
167 | return Action::state_ptr(UndoState);
|
---|
168 | }
|
---|
169 |
|
---|
170 | Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
|
---|
171 | FragmentationSubgraphDissectionState *state = assert_cast<FragmentationSubgraphDissectionState*>(_state.get());
|
---|
172 |
|
---|
173 | {
|
---|
174 | // remove all present molecules
|
---|
175 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
176 | vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
|
---|
177 | for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
|
---|
178 | molecules->erase(*MolRunner);
|
---|
179 | World::getInstance().destroyMolecule(*MolRunner);
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | {
|
---|
184 | // construct the old state
|
---|
185 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
186 | molecule *mol = NULL;
|
---|
187 | for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) {
|
---|
188 | mol = World::getInstance().createMolecule();
|
---|
189 | if (mol->getId() != (*iter).first)
|
---|
190 | World::getInstance().changeMoleculeId(mol->getId(), (*iter).first);
|
---|
191 | for (std::vector<atomId_t>::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter) {
|
---|
192 | atom *Walker = World::getInstance().getAtom(AtomById(*atomiter));
|
---|
193 | mol->AddAtom(Walker);
|
---|
194 | }
|
---|
195 | molecules->insert(mol);
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | return Action::state_ptr(_state);
|
---|
200 | }
|
---|
201 |
|
---|
202 | Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){
|
---|
203 | return performCall();
|
---|
204 | }
|
---|
205 |
|
---|
206 | bool FragmentationSubgraphDissectionAction::canUndo() {
|
---|
207 | return true;
|
---|
208 | }
|
---|
209 |
|
---|
210 | bool FragmentationSubgraphDissectionAction::shouldUndo() {
|
---|
211 | return true;
|
---|
212 | }
|
---|
213 | /** =========== end of function ====================== */
|
---|