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