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