| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * MoleculeLeafClass.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Oct 20, 2011
 | 
|---|
| 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 "MoleculeLeafClass.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include "Atom/atom.hpp"
 | 
|---|
| 27 | #include "Element/element.hpp"
 | 
|---|
| 28 | #include "Fragmentation/Graph.hpp"
 | 
|---|
| 29 | #include "Fragmentation/KeySet.hpp"
 | 
|---|
| 30 | #include "molecule.hpp"
 | 
|---|
| 31 | 
 | 
|---|
| 32 | /** Constructor for MoleculeLeafClass root leaf.
 | 
|---|
| 33 |  * \param *Up Leaf on upper level
 | 
|---|
| 34 |  * \param *PreviousLeaf NULL - We are the first leaf on this level, otherwise points to previous in list
 | 
|---|
| 35 |  */
 | 
|---|
| 36 | //MoleculeLeafClass::MoleculeLeafClass(MoleculeLeafClass *Up = NULL, MoleculeLeafClass *Previous = NULL)
 | 
|---|
| 37 | MoleculeLeafClass::MoleculeLeafClass(MoleculeLeafClass *PreviousLeaf = NULL) :
 | 
|---|
| 38 |   Leaf(NULL),
 | 
|---|
| 39 |   previous(PreviousLeaf)
 | 
|---|
| 40 | {
 | 
|---|
| 41 |   //  if (Up != NULL)
 | 
|---|
| 42 |   //    if (Up->DownLeaf == NULL) // are we the first down leaf for the upper leaf?
 | 
|---|
| 43 |   //      Up->DownLeaf = this;
 | 
|---|
| 44 |   //  UpLeaf = Up;
 | 
|---|
| 45 |   //  DownLeaf = NULL;
 | 
|---|
| 46 |   if (previous != NULL) {
 | 
|---|
| 47 |     MoleculeLeafClass *Walker = previous->next;
 | 
|---|
| 48 |     previous->next = this;
 | 
|---|
| 49 |     next = Walker;
 | 
|---|
| 50 |   } else {
 | 
|---|
| 51 |     next = NULL;
 | 
|---|
| 52 |   }
 | 
|---|
| 53 | };
 | 
|---|
| 54 | 
 | 
|---|
| 55 | /** Destructor for MoleculeLeafClass.
 | 
|---|
| 56 |  */
 | 
|---|
| 57 | MoleculeLeafClass::~MoleculeLeafClass()
 | 
|---|
| 58 | {
 | 
|---|
| 59 |   //  if (DownLeaf != NULL) {// drop leaves further down
 | 
|---|
| 60 |   //    MoleculeLeafClass *Walker = DownLeaf;
 | 
|---|
| 61 |   //    MoleculeLeafClass *Next;
 | 
|---|
| 62 |   //    do {
 | 
|---|
| 63 |   //      Next = Walker->NextLeaf;
 | 
|---|
| 64 |   //      delete(Walker);
 | 
|---|
| 65 |   //      Walker = Next;
 | 
|---|
| 66 |   //    } while (Walker != NULL);
 | 
|---|
| 67 |   //    // Last Walker sets DownLeaf automatically to NULL
 | 
|---|
| 68 |   //  }
 | 
|---|
| 69 |   // remove the leaf itself
 | 
|---|
| 70 |   if (Leaf != NULL) {
 | 
|---|
| 71 |     Leaf->removeAtomsinMolecule();
 | 
|---|
| 72 |     World::getInstance().destroyMolecule(Leaf);
 | 
|---|
| 73 |     Leaf = NULL;
 | 
|---|
| 74 |   }
 | 
|---|
| 75 |   // remove this Leaf from level list
 | 
|---|
| 76 |   if (previous != NULL)
 | 
|---|
| 77 |     previous->next = next;
 | 
|---|
| 78 |   //  } else { // we are first in list (connects to UpLeaf->DownLeaf)
 | 
|---|
| 79 |   //    if ((NextLeaf != NULL) && (NextLeaf->UpLeaf == NULL))
 | 
|---|
| 80 |   //      NextLeaf->UpLeaf = UpLeaf;  // either null as we are top level or the upleaf of the first node
 | 
|---|
| 81 |   //    if (UpLeaf != NULL)
 | 
|---|
| 82 |   //      UpLeaf->DownLeaf = NextLeaf;  // either null as we are only leaf or NextLeaf if we are just the first
 | 
|---|
| 83 |   //  }
 | 
|---|
| 84 |   //  UpLeaf = NULL;
 | 
|---|
| 85 |   if (next != NULL) // are we last in list
 | 
|---|
| 86 |     next->previous = previous;
 | 
|---|
| 87 |   next = NULL;
 | 
|---|
| 88 |   previous = NULL;
 | 
|---|
| 89 | };
 | 
|---|
| 90 | 
 | 
|---|
| 91 | /** Adds \a molecule leaf to the tree.
 | 
|---|
| 92 |  * \param *ptr ptr to molecule to be added
 | 
|---|
| 93 |  * \param *Previous previous MoleculeLeafClass referencing level and which on the level
 | 
|---|
| 94 |  * \return true - success, false - something went wrong
 | 
|---|
| 95 |  */
 | 
|---|
| 96 | bool MoleculeLeafClass::AddLeaf(molecule *ptr, MoleculeLeafClass *Previous)
 | 
|---|
| 97 | {
 | 
|---|
| 98 |   return false;
 | 
|---|
| 99 | };
 | 
|---|
| 100 | 
 | 
|---|
| 101 | /** Fills the root stack for sites to be used as root in fragmentation depending on order or adaptivity criteria
 | 
|---|
| 102 |  * Again, as in \sa FillBondStructureFromReference steps recursively through each Leaf in this chain list of molecule's.
 | 
|---|
| 103 |  * \param *out output stream for debugging
 | 
|---|
| 104 |  * \param *&RootStack stack to be filled
 | 
|---|
| 105 |  * \param *AtomMask defines true/false per global Atom::Nr to mask in/out each nuclear site
 | 
|---|
| 106 |  * \param &FragmentCounter counts through the fragments in this MoleculeLeafClass
 | 
|---|
| 107 |  * \param saturation whether to treat hydrogen special or not
 | 
|---|
| 108 |  * \return true - stack is non-empty, fragmentation necessary, false - stack is empty, no more sites to update
 | 
|---|
| 109 |  */
 | 
|---|
| 110 | bool MoleculeLeafClass::FillRootStackForSubgraphs(KeyStack *&RootStack, bool *AtomMask, int &FragmentCounter, const enum HydrogenSaturation saturation)
 | 
|---|
| 111 | {
 | 
|---|
| 112 |   if (RootStack != NULL) {
 | 
|---|
| 113 |     // find first root candidates
 | 
|---|
| 114 |     if (&(RootStack[FragmentCounter]) != NULL) {
 | 
|---|
| 115 |       RootStack[FragmentCounter].clear();
 | 
|---|
| 116 |       for(molecule::const_iterator iter = Leaf->begin(); iter != Leaf->end(); ++iter) {
 | 
|---|
| 117 |         const atom * const Father = (*iter)->GetTrueFather();
 | 
|---|
| 118 |         if (AtomMask[Father->getNr()]) // apply mask
 | 
|---|
| 119 |           if ((saturation == DontSaturate) || ((*iter)->getType()->getAtomicNumber() != 1)) // skip hydrogen
 | 
|---|
| 120 |             RootStack[FragmentCounter].push_front((*iter)->getNr());
 | 
|---|
| 121 |       }
 | 
|---|
| 122 |       if (next != NULL)
 | 
|---|
| 123 |         next->FillRootStackForSubgraphs(RootStack, AtomMask, ++FragmentCounter, saturation);
 | 
|---|
| 124 |     } else {
 | 
|---|
| 125 |       LOG(1, "Rootstack[" << FragmentCounter << "] is NULL.");
 | 
|---|
| 126 |       return false;
 | 
|---|
| 127 |     }
 | 
|---|
| 128 |     FragmentCounter--;
 | 
|---|
| 129 |     return true;
 | 
|---|
| 130 |   } else {
 | 
|---|
| 131 |     LOG(1, "Rootstack is NULL.");
 | 
|---|
| 132 |     return false;
 | 
|---|
| 133 |   }
 | 
|---|
| 134 | };
 | 
|---|
| 135 | 
 | 
|---|
| 136 | /** The indices per keyset are compared to the respective father's Atom::Nr in each subgraph and thus put into \a **&FragmentList.
 | 
|---|
| 137 |  * \param *out output stream fro debugging
 | 
|---|
| 138 |  * \param *reference reference molecule with the bond structure to be copied
 | 
|---|
| 139 |  * \param *KeySetList list with all keysets
 | 
|---|
| 140 |  * \param ***ListOfLocalAtoms Lookup table for each subgraph and index of each atom in global molecule, may be NULL on start, then it is filled
 | 
|---|
| 141 |  * \param **&FragmentList list to be allocated and returned
 | 
|---|
| 142 |  * \param &FragmentCounter counts the fragments as we move along the list
 | 
|---|
| 143 |  * \param FreeList true - ***ListOfLocalAtoms is free'd before return, false - it is not
 | 
|---|
| 144 |  * \retuen true - success, false - failure
 | 
|---|
| 145 |  */
 | 
|---|
| 146 | bool MoleculeLeafClass::AssignKeySetsToFragment(molecule *reference, Graph *KeySetList, atom ***&ListOfLocalAtoms, Graph **&FragmentList, int &FragmentCounter, bool FreeList)
 | 
|---|
| 147 | {
 | 
|---|
| 148 |   bool status = true;
 | 
|---|
| 149 |   int KeySetCounter = 0;
 | 
|---|
| 150 | 
 | 
|---|
| 151 |   LOG(1, "Begin of AssignKeySetsToFragment.");
 | 
|---|
| 152 |   // fill ListOfLocalAtoms if NULL was given
 | 
|---|
| 153 |   if (!Leaf->FillListOfLocalAtoms(ListOfLocalAtoms[FragmentCounter], reference->getAtomCount())) {
 | 
|---|
| 154 |     LOG(1, "Filling of ListOfLocalAtoms failed.");
 | 
|---|
| 155 |     return false;
 | 
|---|
| 156 |   }
 | 
|---|
| 157 | 
 | 
|---|
| 158 |   // allocate fragment list
 | 
|---|
| 159 |   if (FragmentList == NULL) {
 | 
|---|
| 160 |     KeySetCounter = Count();
 | 
|---|
| 161 |     FragmentList = new Graph*[KeySetCounter];
 | 
|---|
| 162 |     for (int i=0;i<KeySetCounter;i++)
 | 
|---|
| 163 |       FragmentList[i] = NULL;
 | 
|---|
| 164 |     KeySetCounter = 0;
 | 
|---|
| 165 |   }
 | 
|---|
| 166 | 
 | 
|---|
| 167 |   if ((KeySetList != NULL) && (KeySetList->size() != 0)) { // if there are some scanned keysets at all
 | 
|---|
| 168 |     // assign scanned keysets
 | 
|---|
| 169 |     if (FragmentList[FragmentCounter] == NULL)
 | 
|---|
| 170 |       FragmentList[FragmentCounter] = new Graph;
 | 
|---|
| 171 |     KeySet *TempSet = new KeySet;
 | 
|---|
| 172 |     for (Graph::iterator runner = KeySetList->begin(); runner != KeySetList->end(); runner++) { // key sets contain global numbers!
 | 
|---|
| 173 |       if (ListOfLocalAtoms[FragmentCounter][reference->FindAtom(*((*runner).first.begin()))->getNr()] != NULL) {// as we may assume that that bond structure is unchanged, we only test the first key in each set
 | 
|---|
| 174 |         // translate keyset to local numbers
 | 
|---|
| 175 |         for (KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++)
 | 
|---|
| 176 |           TempSet->insert(ListOfLocalAtoms[FragmentCounter][reference->FindAtom(*sprinter)->getNr()]->getNr());
 | 
|---|
| 177 |         // insert into FragmentList
 | 
|---|
| 178 |         FragmentList[FragmentCounter]->insert(GraphPair(*TempSet, pair<int, double> (KeySetCounter++, (*runner).second.second)));
 | 
|---|
| 179 |       }
 | 
|---|
| 180 |       TempSet->clear();
 | 
|---|
| 181 |     }
 | 
|---|
| 182 |     delete (TempSet);
 | 
|---|
| 183 |     if (KeySetCounter == 0) {// if there are no keysets, delete the list
 | 
|---|
| 184 |       LOG(1, "KeySetCounter is zero, deleting FragmentList.");
 | 
|---|
| 185 |       delete (FragmentList[FragmentCounter]);
 | 
|---|
| 186 |     } else
 | 
|---|
| 187 |       LOG(1, KeySetCounter << " keysets were assigned to subgraph " << FragmentCounter << ".");
 | 
|---|
| 188 |     FragmentCounter++;
 | 
|---|
| 189 |     if (next != NULL)
 | 
|---|
| 190 |       next->AssignKeySetsToFragment(reference, KeySetList, ListOfLocalAtoms, FragmentList, FragmentCounter, FreeList);
 | 
|---|
| 191 |     FragmentCounter--;
 | 
|---|
| 192 |   } else
 | 
|---|
| 193 |     LOG(1, "KeySetList is NULL or empty.");
 | 
|---|
| 194 | 
 | 
|---|
| 195 |   if ((FreeList) && (ListOfLocalAtoms != NULL)) {
 | 
|---|
| 196 |     // free the index lookup list
 | 
|---|
| 197 |     delete[](ListOfLocalAtoms[FragmentCounter]);
 | 
|---|
| 198 |   }
 | 
|---|
| 199 |   LOG(1, "End of AssignKeySetsToFragment.");
 | 
|---|
| 200 |   return status;
 | 
|---|
| 201 | };
 | 
|---|
| 202 | 
 | 
|---|
| 203 | /** Translate list into global numbers (i.e. ones that are valid in "this" molecule, not in MolecularWalker->Leaf)
 | 
|---|
| 204 |  * \param *out output stream for debugging
 | 
|---|
| 205 |  * \param **FragmentList Graph with local numbers per fragment
 | 
|---|
| 206 |  * \param &FragmentCounter counts the fragments as we move along the list
 | 
|---|
| 207 |  * \param &TotalNumberOfKeySets global key set counter
 | 
|---|
| 208 |  * \param &TotalGraph Graph to be filled with global numbers
 | 
|---|
| 209 |  */
 | 
|---|
| 210 | void MoleculeLeafClass::TranslateIndicesToGlobalIDs(Graph **FragmentList, int &FragmentCounter, int &TotalNumberOfKeySets, Graph &TotalGraph)
 | 
|---|
| 211 | {
 | 
|---|
| 212 |   LOG(1, "Begin of TranslateIndicesToGlobalIDs.");
 | 
|---|
| 213 |   KeySet *TempSet = new KeySet;
 | 
|---|
| 214 |   if (FragmentList[FragmentCounter] != NULL) {
 | 
|---|
| 215 |     for (Graph::iterator runner = FragmentList[FragmentCounter]->begin(); runner != FragmentList[FragmentCounter]->end(); runner++) {
 | 
|---|
| 216 |       for (KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++)
 | 
|---|
| 217 |         TempSet->insert((Leaf->FindAtom(*sprinter))->GetTrueFather()->getNr());
 | 
|---|
| 218 |       TotalGraph.insert(GraphPair(*TempSet, pair<int, double> (TotalNumberOfKeySets++, (*runner).second.second)));
 | 
|---|
| 219 |       TempSet->clear();
 | 
|---|
| 220 |     }
 | 
|---|
| 221 |     delete (TempSet);
 | 
|---|
| 222 |   } else {
 | 
|---|
| 223 |     LOG(1, "FragmentList is NULL.");
 | 
|---|
| 224 |   }
 | 
|---|
| 225 |   if (next != NULL)
 | 
|---|
| 226 |     next->TranslateIndicesToGlobalIDs(FragmentList, ++FragmentCounter, TotalNumberOfKeySets, TotalGraph);
 | 
|---|
| 227 |   FragmentCounter--;
 | 
|---|
| 228 |   LOG(1, "End of TranslateIndicesToGlobalIDs.");
 | 
|---|
| 229 | };
 | 
|---|
| 230 | 
 | 
|---|
| 231 | /** Simply counts the number of items in the list, from given MoleculeLeafClass.
 | 
|---|
| 232 |  * \return number of items
 | 
|---|
| 233 |  */
 | 
|---|
| 234 | int MoleculeLeafClass::Count() const
 | 
|---|
| 235 | {
 | 
|---|
| 236 |   if (next != NULL)
 | 
|---|
| 237 |     return next->Count() + 1;
 | 
|---|
| 238 |   else
 | 
|---|
| 239 |     return 1;
 | 
|---|
| 240 | };
 | 
|---|
| 241 | 
 | 
|---|