Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Actions/FragmentationAction/SubgraphDissectionAction.cpp

    r952f38 rbf3817  
    66 */
    77
     8// include config.h
     9#ifdef HAVE_CONFIG_H
     10#include <config.h>
     11#endif
     12
    813#include "Helpers/MemDebug.hpp"
    914
    1015#include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
    1116#include "Actions/ActionRegistry.hpp"
     17#include "Descriptors/MoleculeDescriptor.hpp"
     18
    1219#include "atom.hpp"
     20#include "bond.hpp"
     21#include "bondgraph.hpp"
    1322#include "config.hpp"
    1423#include "Helpers/Log.hpp"
    1524#include "molecule.hpp"
    16 #include "Descriptors/MoleculeDescriptor.hpp"
    1725#include "stackclass.hpp"
    1826#include "World.hpp"
     
    5361  // @TODO rather do the dissection afterwards
    5462  MoleculeListClass *molecules = World::getInstance().getMolecules();
    55   molecules->DissectMoleculeIntoConnectedSubgraphs(World::getInstance().getPeriode(), World::getInstance().getConfig());
     63  config * const configuration = World::getInstance().getConfig();
     64
     65  // 0a. remove all present molecules
     66  vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
     67  for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
     68    molecules->erase(*MolRunner);
     69    World::getInstance().destroyMolecule(*MolRunner);
     70  }
     71
     72  // 0b. remove all bonds and construct a molecule with all atoms
     73  molecule *mol = World::getInstance().createMolecule();
     74  vector <atom *> allatoms = World::getInstance().getAllAtoms();
     75  for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
     76    for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
     77      delete(*BondRunner);
     78    mol->AddAtom(*AtomRunner);
     79  }
     80
     81  // 1. create the bond structure of the single molecule
     82  if (configuration->BG != NULL) {
     83    if (!configuration->BG->ConstructBondGraph(mol)) {
     84      World::getInstance().destroyMolecule(mol);
     85      DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl);
     86      return Action::failure;
     87    }
     88  } else {
     89    DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl);
     90    return Action::failure;
     91  }
     92
     93  // 2. scan for connected subgraphs
     94  MoleculeLeafClass *Subgraphs = NULL;      // list of subgraphs from DFS analysis
     95  class StackClass<bond *> *BackEdgeStack = NULL;
     96  Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack);
     97  delete(BackEdgeStack);
     98  if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
     99    World::getInstance().destroyMolecule(mol);
     100    DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
     101    return Action::failure;
     102  }
     103
     104  // 3. dissect (the following construct is needed to have the atoms not in the order of the DFS, but in
     105  // the original one as parsed in)
     106  // TODO: Optimize this, when molecules just contain pointer list of global atoms!
     107
     108  // 4a. create array of molecules to fill
     109  const int MolCount = Subgraphs->next->Count();
     110  char number[MAXSTRINGSIZE];
     111  molecule **moleculelist = new molecule *[MolCount];
     112  MoleculeLeafClass *MolecularWalker = Subgraphs;
     113  for (int i=0;i<MolCount;i++) {
     114    MolecularWalker = MolecularWalker->next;
     115    moleculelist[i] = World::getInstance().createMolecule();
     116    moleculelist[i]->ActiveFlag = true;
     117    strncpy(moleculelist[i]->name, mol->name, MAXSTRINGSIZE);
     118    if (MolCount > 1) {
     119      sprintf(number, "-%d", i+1);
     120      strncat(moleculelist[i]->name, number, MAXSTRINGSIZE - strlen(mol->name) - 1);
     121    }
     122    DoLog(1) && (Log() << Verbose(1) << "MolName is " << moleculelist[i]->name << ", id is " << moleculelist[i]->getId() << endl);
     123    for (molecule::iterator iter = MolecularWalker->Leaf->begin(); iter != MolecularWalker->Leaf->end(); ++iter) {
     124      DoLog(1) && (Log() << Verbose(1) << **iter << endl);
     125    }
     126    molecules->insert(moleculelist[i]);
     127  }
     128
     129  // 4b. create and fill map of which atom is associated to which connected molecule (note, counting starts at 1)
     130  int FragmentCounter = 0;
     131  map<int, atom *> AtomToFragmentMap;
     132  MolecularWalker = Subgraphs;
     133  while (MolecularWalker->next != NULL) {
     134    MolecularWalker = MolecularWalker->next;
     135    for (molecule::iterator iter = MolecularWalker->Leaf->begin(); !MolecularWalker->Leaf->empty(); iter = MolecularWalker->Leaf->begin()) {
     136      atom * Walker = *iter;
     137      DoLog(1) && (Log() << Verbose(1) << "Re-linking " << Walker << "..." << endl);
     138      MolecularWalker->Leaf->erase(iter);
     139      moleculelist[FragmentCounter]->AddAtom(Walker);    // counting starts at 1
     140    }
     141    FragmentCounter++;
     142  }
     143  // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms.
     144  // 4d. destroy the original molecule
     145  for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
     146    World::getInstance().destroyAtom(*AtomRunner);
     147  World::getInstance().destroyMolecule(mol);
     148
     149  // 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
     150  // TODO: check whether this is really not needed anymore
     151  // 4e. free Leafs
     152  MolecularWalker = Subgraphs;
     153  while (MolecularWalker->next != NULL) {
     154    MolecularWalker = MolecularWalker->next;
     155    delete(MolecularWalker->previous);
     156  }
     157  delete(MolecularWalker);
     158  delete[](moleculelist);
     159  DoLog(1) && (Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl);
     160
    56161  return Action::success;
    57162}
Note: See TracChangeset for help on using the changeset viewer.