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