[97ebf8] | 1 | /*
|
---|
| 2 | * SubgraphDissectionAction.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: May 9, 2010
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[bf3817] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
[112b09] | 13 | #include "Helpers/MemDebug.hpp"
|
---|
| 14 |
|
---|
[97ebf8] | 15 | #include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
|
---|
[0430e3] | 16 | #include "Actions/ActionRegistry.hpp"
|
---|
[d74077] | 17 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
| 18 |
|
---|
[97ebf8] | 19 | #include "atom.hpp"
|
---|
[d74077] | 20 | #include "bond.hpp"
|
---|
| 21 | #include "bondgraph.hpp"
|
---|
[97ebf8] | 22 | #include "config.hpp"
|
---|
[952f38] | 23 | #include "Helpers/Log.hpp"
|
---|
[97ebf8] | 24 | #include "molecule.hpp"
|
---|
| 25 | #include "stackclass.hpp"
|
---|
[2b7d1b] | 26 | #include "verbose.hpp"
|
---|
[97ebf8] | 27 | #include "World.hpp"
|
---|
| 28 |
|
---|
| 29 | #include <iostream>
|
---|
| 30 | #include <string>
|
---|
| 31 |
|
---|
| 32 | using namespace std;
|
---|
| 33 |
|
---|
| 34 | #include "UIElements/UIFactory.hpp"
|
---|
| 35 | #include "UIElements/Dialog.hpp"
|
---|
[861874] | 36 | #include "Actions/ValueStorage.hpp"
|
---|
[97ebf8] | 37 |
|
---|
[6866aa] | 38 | const char FragmentationSubgraphDissectionAction::NAME[] = "subgraph-dissect";
|
---|
[97ebf8] | 39 |
|
---|
| 40 | FragmentationSubgraphDissectionAction::FragmentationSubgraphDissectionAction() :
|
---|
| 41 | Action(NAME)
|
---|
| 42 | {}
|
---|
| 43 |
|
---|
| 44 | FragmentationSubgraphDissectionAction::~FragmentationSubgraphDissectionAction()
|
---|
| 45 | {}
|
---|
| 46 |
|
---|
[2b7d1b] | 47 | /** Dissects given \a *mol into connected subgraphs and inserts them as new molecules but with old atoms into \a this.
|
---|
| 48 | */
|
---|
[4ff081] | 49 | void FragmentationSubgraphDissection() {
|
---|
| 50 | ActionRegistry::getInstance().getActionByName(FragmentationSubgraphDissectionAction::NAME)->call(Action::NonInteractive);
|
---|
| 51 | };
|
---|
| 52 |
|
---|
[047878] | 53 | Dialog* FragmentationSubgraphDissectionAction::fillDialog(Dialog *dialog) {
|
---|
| 54 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
[97ebf8] | 55 |
|
---|
| 56 | dialog->queryEmpty(NAME, MapOfActions::getInstance().getDescription(NAME));
|
---|
| 57 |
|
---|
[1015fd] | 58 | return dialog;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | Action::state_ptr FragmentationSubgraphDissectionAction::performCall() {
|
---|
| 63 | DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl);
|
---|
[2b7d1b] | 64 |
|
---|
[1015fd] | 65 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
[d74077] | 66 | config * const configuration = World::getInstance().getConfig();
|
---|
| 67 |
|
---|
| 68 | // 0a. remove all present molecules
|
---|
| 69 | vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
|
---|
| 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 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
| 78 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
| 79 | for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
|
---|
| 80 | delete(*BondRunner);
|
---|
| 81 | mol->AddAtom(*AtomRunner);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | // 1. create the bond structure of the single molecule
|
---|
| 85 | if (configuration->BG != NULL) {
|
---|
| 86 | if (!configuration->BG->ConstructBondGraph(mol)) {
|
---|
| 87 | World::getInstance().destroyMolecule(mol);
|
---|
| 88 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl);
|
---|
| 89 | return Action::failure;
|
---|
| 90 | }
|
---|
| 91 | } else {
|
---|
| 92 | DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl);
|
---|
| 93 | return Action::failure;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | // 2. scan for connected subgraphs
|
---|
| 97 | MoleculeLeafClass *Subgraphs = NULL; // list of subgraphs from DFS analysis
|
---|
| 98 | class StackClass<bond *> *BackEdgeStack = NULL;
|
---|
| 99 | Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack);
|
---|
| 100 | delete(BackEdgeStack);
|
---|
| 101 | if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
|
---|
| 102 | World::getInstance().destroyMolecule(mol);
|
---|
| 103 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
|
---|
| 104 | return Action::failure;
|
---|
| 105 | }
|
---|
[2b7d1b] | 106 | <<<<<<< HEAD
|
---|
[d74077] | 107 |
|
---|
| 108 | // 3. dissect (the following construct is needed to have the atoms not in the order of the DFS, but in
|
---|
| 109 | // the original one as parsed in)
|
---|
| 110 | // TODO: Optimize this, when molecules just contain pointer list of global atoms!
|
---|
| 111 |
|
---|
| 112 | // 4a. create array of molecules to fill
|
---|
| 113 | const int MolCount = Subgraphs->next->Count();
|
---|
| 114 | char number[MAXSTRINGSIZE];
|
---|
| 115 | molecule **moleculelist = new molecule *[MolCount];
|
---|
| 116 | MoleculeLeafClass *MolecularWalker = Subgraphs;
|
---|
| 117 | for (int i=0;i<MolCount;i++) {
|
---|
| 118 | MolecularWalker = MolecularWalker->next;
|
---|
| 119 | moleculelist[i] = World::getInstance().createMolecule();
|
---|
| 120 | moleculelist[i]->ActiveFlag = true;
|
---|
| 121 | strncpy(moleculelist[i]->name, mol->name, MAXSTRINGSIZE);
|
---|
| 122 | if (MolCount > 1) {
|
---|
| 123 | sprintf(number, "-%d", i+1);
|
---|
| 124 | strncat(moleculelist[i]->name, number, MAXSTRINGSIZE - strlen(mol->name) - 1);
|
---|
| 125 | }
|
---|
| 126 | DoLog(1) && (Log() << Verbose(1) << "MolName is " << moleculelist[i]->name << ", id is " << moleculelist[i]->getId() << endl);
|
---|
| 127 | for (molecule::iterator iter = MolecularWalker->Leaf->begin(); iter != MolecularWalker->Leaf->end(); ++iter) {
|
---|
| 128 | DoLog(1) && (Log() << Verbose(1) << **iter << endl);
|
---|
| 129 | }
|
---|
| 130 | molecules->insert(moleculelist[i]);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | // 4b. create and fill map of which atom is associated to which connected molecule (note, counting starts at 1)
|
---|
| 134 | int FragmentCounter = 0;
|
---|
| 135 | map<int, atom *> AtomToFragmentMap;
|
---|
| 136 | MolecularWalker = Subgraphs;
|
---|
| 137 | while (MolecularWalker->next != NULL) {
|
---|
| 138 | MolecularWalker = MolecularWalker->next;
|
---|
| 139 | for (molecule::iterator iter = MolecularWalker->Leaf->begin(); !MolecularWalker->Leaf->empty(); iter = MolecularWalker->Leaf->begin()) {
|
---|
| 140 | atom * Walker = *iter;
|
---|
| 141 | DoLog(1) && (Log() << Verbose(1) << "Re-linking " << Walker << "..." << endl);
|
---|
| 142 | MolecularWalker->Leaf->erase(iter);
|
---|
| 143 | moleculelist[FragmentCounter]->AddAtom(Walker); // counting starts at 1
|
---|
| 144 | }
|
---|
| 145 | FragmentCounter++;
|
---|
| 146 | }
|
---|
| 147 | // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms.
|
---|
| 148 | // 4d. destroy the original molecule
|
---|
[2b7d1b] | 149 | =======
|
---|
| 150 | int FragmentCounter = Subgraphs->Count();
|
---|
| 151 |
|
---|
| 152 | // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms
|
---|
| 153 | // 3. destroy the original molecule
|
---|
| 154 | >>>>>>> Merged MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs() into FragmenationSubgraphDissectionAction.
|
---|
[d74077] | 155 | for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
|
---|
| 156 | World::getInstance().destroyAtom(*AtomRunner);
|
---|
| 157 | World::getInstance().destroyMolecule(mol);
|
---|
| 158 |
|
---|
[2b7d1b] | 159 | <<<<<<< HEAD
|
---|
[d74077] | 160 | // 4d. we don't need to redo bonds, as they are connected subgraphs and still maintain their ListOfBonds, but we have to remove them from first..last list
|
---|
| 161 | // TODO: check whether this is really not needed anymore
|
---|
| 162 | // 4e. free Leafs
|
---|
| 163 | MolecularWalker = Subgraphs;
|
---|
[2b7d1b] | 164 | =======
|
---|
| 165 | // 4. free Leafs
|
---|
| 166 | MoleculeLeafClass *MolecularWalker = Subgraphs;
|
---|
| 167 | >>>>>>> Merged MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs() into FragmenationSubgraphDissectionAction.
|
---|
[d74077] | 168 | while (MolecularWalker->next != NULL) {
|
---|
| 169 | MolecularWalker = MolecularWalker->next;
|
---|
| 170 | delete(MolecularWalker->previous);
|
---|
| 171 | }
|
---|
| 172 | delete(MolecularWalker);
|
---|
[2b7d1b] | 173 | <<<<<<< HEAD
|
---|
[d74077] | 174 | delete[](moleculelist);
|
---|
[2b7d1b] | 175 | =======
|
---|
| 176 | >>>>>>> Merged MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs() into FragmenationSubgraphDissectionAction.
|
---|
[d74077] | 177 | DoLog(1) && (Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl);
|
---|
| 178 |
|
---|
[1015fd] | 179 | return Action::success;
|
---|
[97ebf8] | 180 | }
|
---|
| 181 |
|
---|
| 182 | Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
|
---|
| 183 | // ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
|
---|
| 184 |
|
---|
| 185 | return Action::failure;
|
---|
| 186 | // string newName = state->mol->getName();
|
---|
| 187 | // state->mol->setName(state->lastName);
|
---|
| 188 | //
|
---|
| 189 | // return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){
|
---|
| 193 | return Action::failure;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | bool FragmentationSubgraphDissectionAction::canUndo() {
|
---|
| 197 | return false;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | bool FragmentationSubgraphDissectionAction::shouldUndo() {
|
---|
| 201 | return false;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | const string FragmentationSubgraphDissectionAction::getName() {
|
---|
| 205 | return NAME;
|
---|
| 206 | }
|
---|