Ignore:
Timestamp:
Nov 4, 2009, 3:08:11 PM (16 years ago)
Author:
Frederik Heber <heber@…>
Children:
20895b
Parents:
e1900f
Message:

config::Load() refactored: Dissection into connected subgraphs -> MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/moleculelist.cpp

    re1900f rc1b76e  
    734734};
    735735
     736/** Dissects given \a *mol into connected subgraphs and inserts them as new molecules but with old atoms into \a this.
     737 * \param *out output stream for debugging
     738 * \param *mol molecule with atoms to dissect
     739 * \param *configuration config with BondGraph
     740 */
     741void MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs(ofstream * const out, molecule * const mol, config * const configuration)
     742{
     743  // 1. dissect the molecule into connected subgraphs
     744  configuration ->BG->ConstructBondGraph(out, mol);
     745
     746  // 2. scan for connected subgraphs
     747  MoleculeLeafClass *Subgraphs = NULL;      // list of subgraphs from DFS analysis
     748  class StackClass<bond *> *BackEdgeStack = NULL;
     749  Subgraphs = mol->DepthFirstSearchAnalysis(out, BackEdgeStack);
     750  delete(BackEdgeStack);
     751
     752  // 3. dissect (the following construct is needed to have the atoms not in the order of the DFS, but in
     753  // the original one as parsed in)
     754  // TODO: Optimize this, when molecules just contain pointer list of global atoms!
     755
     756  // 4a. create array of molecules to fill
     757  const int MolCount = Subgraphs->next->Count();
     758  molecule **molecules = Malloc<molecule *>(MolCount, "config::Load() - **molecules");
     759  for (int i=0;i<MolCount;i++) {
     760    molecules[i] = (molecule*) new molecule(mol->elemente);
     761    molecules[i]->ActiveFlag = true;
     762    insert(molecules[i]);
     763  }
     764
     765  // 4b. create and fill map of which atom is associated to which connected molecule (note, counting starts at 1)
     766  int FragmentCounter = 0;
     767  int *MolMap = Calloc<int>(mol->AtomCount, "config::Load() - *MolMap");
     768  MoleculeLeafClass *MolecularWalker = Subgraphs;
     769  atom *Walker = NULL;
     770  while (MolecularWalker->next != NULL) {
     771    MolecularWalker = MolecularWalker->next;
     772    Walker = MolecularWalker->Leaf->start;
     773    while (Walker->next != MolecularWalker->Leaf->end) {
     774      Walker = Walker->next;
     775      MolMap[Walker->GetTrueFather()->nr] = FragmentCounter+1;
     776    }
     777    FragmentCounter++;
     778  }
     779
     780  // 4c. relocate atoms to new molecules and remove from Leafs
     781  Walker = mol->start;
     782  while (mol->start->next != mol->end) {
     783    Walker = mol->start->next;
     784    if ((Walker->nr <0) || (Walker->nr >= mol->AtomCount)) {
     785      cerr << "Index of atom " << *Walker << " is invalid!" << endl;
     786      performCriticalExit();
     787    }
     788    FragmentCounter = MolMap[Walker->nr];
     789    if (FragmentCounter != 0) {
     790      cout << Verbose(3) << "Re-linking " << *Walker << "..." << endl;
     791      unlink(Walker);
     792      molecules[FragmentCounter-1]->AddAtom(Walker);    // counting starts at 1
     793    } else {
     794      cerr << "Atom " << *Walker << " not associated to molecule!" << endl;
     795      performCriticalExit();
     796    }
     797  }
     798  // 4d. we don't need to redo bonds, as they are connected subgraphs and still maintained their ListOfBonds
     799  // 4e. free Leafs
     800  MolecularWalker = Subgraphs;
     801  while (MolecularWalker->next != NULL) {
     802    MolecularWalker = MolecularWalker->next;
     803    delete(MolecularWalker->previous);
     804  }
     805  delete(MolecularWalker);
     806  Free(&MolMap);
     807  Free(&molecules);
     808  cout << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl;
     809};
    736810
    737811/******************************************* Class MoleculeLeafClass ************************************************/
Note: See TracChangeset for help on using the changeset viewer.