Ignore:
File:
1 edited

Legend:

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

    r952f38 rd74077  
    1010#include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
    1111#include "Actions/ActionRegistry.hpp"
     12#include "Descriptors/MoleculeDescriptor.hpp"
     13
    1214#include "atom.hpp"
     15#include "bond.hpp"
     16#include "bondgraph.hpp"
    1317#include "config.hpp"
    14 #include "Helpers/Log.hpp"
     18#include "log.hpp"
    1519#include "molecule.hpp"
    16 #include "Descriptors/MoleculeDescriptor.hpp"
    1720#include "stackclass.hpp"
    1821#include "World.hpp"
     
    2528#include "UIElements/UIFactory.hpp"
    2629#include "UIElements/Dialog.hpp"
    27 #include "Actions/ValueStorage.hpp"
     30#include "UIElements/ValueStorage.hpp"
    2831
    2932const char FragmentationSubgraphDissectionAction::NAME[] = "subgraph-dissect";
     
    4043};
    4144
    42 Dialog* FragmentationSubgraphDissectionAction::fillDialog(Dialog *dialog) {
    43   ASSERT(dialog,"No Dialog given when filling action dialog");
     45Dialog* FragmentationSubgraphDissectionAction::createDialog() {
     46  Dialog *dialog = UIFactory::getInstance().makeDialog();
    4447
    4548  dialog->queryEmpty(NAME, MapOfActions::getInstance().getDescription(NAME));
     
    5356  // @TODO rather do the dissection afterwards
    5457  MoleculeListClass *molecules = World::getInstance().getMolecules();
    55   molecules->DissectMoleculeIntoConnectedSubgraphs(World::getInstance().getPeriode(), World::getInstance().getConfig());
     58  config * const configuration = World::getInstance().getConfig();
     59
     60  // 0a. remove all present molecules
     61  vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
     62  for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
     63    molecules->erase(*MolRunner);
     64    World::getInstance().destroyMolecule(*MolRunner);
     65  }
     66
     67  // 0b. remove all bonds and construct a molecule with all atoms
     68  molecule *mol = World::getInstance().createMolecule();
     69  vector <atom *> allatoms = World::getInstance().getAllAtoms();
     70  for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
     71    for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
     72      delete(*BondRunner);
     73    mol->AddAtom(*AtomRunner);
     74  }
     75
     76  // 1. create the bond structure of the single molecule
     77  if (configuration->BG != NULL) {
     78    if (!configuration->BG->ConstructBondGraph(mol)) {
     79      World::getInstance().destroyMolecule(mol);
     80      DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl);
     81      return Action::failure;
     82    }
     83  } else {
     84    DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl);
     85    return Action::failure;
     86  }
     87
     88  // 2. scan for connected subgraphs
     89  MoleculeLeafClass *Subgraphs = NULL;      // list of subgraphs from DFS analysis
     90  class StackClass<bond *> *BackEdgeStack = NULL;
     91  Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack);
     92  delete(BackEdgeStack);
     93  if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
     94    World::getInstance().destroyMolecule(mol);
     95    DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
     96    return Action::failure;
     97  }
     98
     99  // 3. dissect (the following construct is needed to have the atoms not in the order of the DFS, but in
     100  // the original one as parsed in)
     101  // TODO: Optimize this, when molecules just contain pointer list of global atoms!
     102
     103  // 4a. create array of molecules to fill
     104  const int MolCount = Subgraphs->next->Count();
     105  char number[MAXSTRINGSIZE];
     106  molecule **moleculelist = new molecule *[MolCount];
     107  MoleculeLeafClass *MolecularWalker = Subgraphs;
     108  for (int i=0;i<MolCount;i++) {
     109    MolecularWalker = MolecularWalker->next;
     110    moleculelist[i] = World::getInstance().createMolecule();
     111    moleculelist[i]->ActiveFlag = true;
     112    strncpy(moleculelist[i]->name, mol->name, MAXSTRINGSIZE);
     113    if (MolCount > 1) {
     114      sprintf(number, "-%d", i+1);
     115      strncat(moleculelist[i]->name, number, MAXSTRINGSIZE - strlen(mol->name) - 1);
     116    }
     117    DoLog(1) && (Log() << Verbose(1) << "MolName is " << moleculelist[i]->name << ", id is " << moleculelist[i]->getId() << endl);
     118    for (molecule::iterator iter = MolecularWalker->Leaf->begin(); iter != MolecularWalker->Leaf->end(); ++iter) {
     119      DoLog(1) && (Log() << Verbose(1) << **iter << endl);
     120    }
     121    molecules->insert(moleculelist[i]);
     122  }
     123
     124  // 4b. create and fill map of which atom is associated to which connected molecule (note, counting starts at 1)
     125  int FragmentCounter = 0;
     126  map<int, atom *> AtomToFragmentMap;
     127  MolecularWalker = Subgraphs;
     128  while (MolecularWalker->next != NULL) {
     129    MolecularWalker = MolecularWalker->next;
     130    for (molecule::iterator iter = MolecularWalker->Leaf->begin(); !MolecularWalker->Leaf->empty(); iter = MolecularWalker->Leaf->begin()) {
     131      atom * Walker = *iter;
     132      DoLog(1) && (Log() << Verbose(1) << "Re-linking " << Walker << "..." << endl);
     133      MolecularWalker->Leaf->erase(iter);
     134      moleculelist[FragmentCounter]->AddAtom(Walker);    // counting starts at 1
     135    }
     136    FragmentCounter++;
     137  }
     138  // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms.
     139  // 4d. destroy the original molecule
     140  for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
     141    World::getInstance().destroyAtom(*AtomRunner);
     142  World::getInstance().destroyMolecule(mol);
     143
     144  // 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
     145  // TODO: check whether this is really not needed anymore
     146  // 4e. free Leafs
     147  MolecularWalker = Subgraphs;
     148  while (MolecularWalker->next != NULL) {
     149    MolecularWalker = MolecularWalker->next;
     150    delete(MolecularWalker->previous);
     151  }
     152  delete(MolecularWalker);
     153  delete[](moleculelist);
     154  DoLog(1) && (Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl);
     155
    56156  return Action::success;
    57157}
Note: See TracChangeset for help on using the changeset viewer.