1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010 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.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 | atom *Father = NULL;
|
---|
113 |
|
---|
114 | if (RootStack != NULL) {
|
---|
115 | // find first root candidates
|
---|
116 | if (&(RootStack[FragmentCounter]) != NULL) {
|
---|
117 | RootStack[FragmentCounter].clear();
|
---|
118 | for(molecule::const_iterator iter = Leaf->begin(); iter != Leaf->end(); ++iter) {
|
---|
119 | Father = (*iter)->GetTrueFather();
|
---|
120 | if (AtomMask[Father->getNr()]) // apply mask
|
---|
121 | if ((saturation == DontSaturate) || ((*iter)->getType()->getAtomicNumber() != 1)) // skip hydrogen
|
---|
122 | RootStack[FragmentCounter].push_front((*iter)->getNr());
|
---|
123 | }
|
---|
124 | if (next != NULL)
|
---|
125 | next->FillRootStackForSubgraphs(RootStack, AtomMask, ++FragmentCounter, saturation);
|
---|
126 | } else {
|
---|
127 | LOG(1, "Rootstack[" << FragmentCounter << "] is NULL.");
|
---|
128 | return false;
|
---|
129 | }
|
---|
130 | FragmentCounter--;
|
---|
131 | return true;
|
---|
132 | } else {
|
---|
133 | LOG(1, "Rootstack is NULL.");
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 | };
|
---|
137 |
|
---|
138 | /** The indices per keyset are compared to the respective father's Atom::Nr in each subgraph and thus put into \a **&FragmentList.
|
---|
139 | * \param *out output stream fro debugging
|
---|
140 | * \param *reference reference molecule with the bond structure to be copied
|
---|
141 | * \param *KeySetList list with all keysets
|
---|
142 | * \param ***ListOfLocalAtoms Lookup table for each subgraph and index of each atom in global molecule, may be NULL on start, then it is filled
|
---|
143 | * \param **&FragmentList list to be allocated and returned
|
---|
144 | * \param &FragmentCounter counts the fragments as we move along the list
|
---|
145 | * \param FreeList true - ***ListOfLocalAtoms is free'd before return, false - it is not
|
---|
146 | * \retuen true - success, false - failure
|
---|
147 | */
|
---|
148 | bool MoleculeLeafClass::AssignKeySetsToFragment(molecule *reference, Graph *KeySetList, atom ***&ListOfLocalAtoms, Graph **&FragmentList, int &FragmentCounter, bool FreeList)
|
---|
149 | {
|
---|
150 | bool status = true;
|
---|
151 | int KeySetCounter = 0;
|
---|
152 |
|
---|
153 | LOG(1, "Begin of AssignKeySetsToFragment.");
|
---|
154 | // fill ListOfLocalAtoms if NULL was given
|
---|
155 | if (!Leaf->FillListOfLocalAtoms(ListOfLocalAtoms[FragmentCounter], reference->getAtomCount())) {
|
---|
156 | LOG(1, "Filling of ListOfLocalAtoms failed.");
|
---|
157 | return false;
|
---|
158 | }
|
---|
159 |
|
---|
160 | // allocate fragment list
|
---|
161 | if (FragmentList == NULL) {
|
---|
162 | KeySetCounter = Count();
|
---|
163 | FragmentList = new Graph*[KeySetCounter];
|
---|
164 | for (int i=0;i<KeySetCounter;i++)
|
---|
165 | FragmentList[i] = NULL;
|
---|
166 | KeySetCounter = 0;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if ((KeySetList != NULL) && (KeySetList->size() != 0)) { // if there are some scanned keysets at all
|
---|
170 | // assign scanned keysets
|
---|
171 | if (FragmentList[FragmentCounter] == NULL)
|
---|
172 | FragmentList[FragmentCounter] = new Graph;
|
---|
173 | KeySet *TempSet = new KeySet;
|
---|
174 | for (Graph::iterator runner = KeySetList->begin(); runner != KeySetList->end(); runner++) { // key sets contain global numbers!
|
---|
175 | 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
|
---|
176 | // translate keyset to local numbers
|
---|
177 | for (KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++)
|
---|
178 | TempSet->insert(ListOfLocalAtoms[FragmentCounter][reference->FindAtom(*sprinter)->getNr()]->getNr());
|
---|
179 | // insert into FragmentList
|
---|
180 | FragmentList[FragmentCounter]->insert(GraphPair(*TempSet, pair<int, double> (KeySetCounter++, (*runner).second.second)));
|
---|
181 | }
|
---|
182 | TempSet->clear();
|
---|
183 | }
|
---|
184 | delete (TempSet);
|
---|
185 | if (KeySetCounter == 0) {// if there are no keysets, delete the list
|
---|
186 | LOG(1, "KeySetCounter is zero, deleting FragmentList.");
|
---|
187 | delete (FragmentList[FragmentCounter]);
|
---|
188 | } else
|
---|
189 | LOG(1, KeySetCounter << " keysets were assigned to subgraph " << FragmentCounter << ".");
|
---|
190 | FragmentCounter++;
|
---|
191 | if (next != NULL)
|
---|
192 | next->AssignKeySetsToFragment(reference, KeySetList, ListOfLocalAtoms, FragmentList, FragmentCounter, FreeList);
|
---|
193 | FragmentCounter--;
|
---|
194 | } else
|
---|
195 | LOG(1, "KeySetList is NULL or empty.");
|
---|
196 |
|
---|
197 | if ((FreeList) && (ListOfLocalAtoms != NULL)) {
|
---|
198 | // free the index lookup list
|
---|
199 | delete[](ListOfLocalAtoms[FragmentCounter]);
|
---|
200 | }
|
---|
201 | LOG(1, "End of AssignKeySetsToFragment.");
|
---|
202 | return status;
|
---|
203 | };
|
---|
204 |
|
---|
205 | /** Translate list into global numbers (i.e. ones that are valid in "this" molecule, not in MolecularWalker->Leaf)
|
---|
206 | * \param *out output stream for debugging
|
---|
207 | * \param **FragmentList Graph with local numbers per fragment
|
---|
208 | * \param &FragmentCounter counts the fragments as we move along the list
|
---|
209 | * \param &TotalNumberOfKeySets global key set counter
|
---|
210 | * \param &TotalGraph Graph to be filled with global numbers
|
---|
211 | */
|
---|
212 | void MoleculeLeafClass::TranslateIndicesToGlobalIDs(Graph **FragmentList, int &FragmentCounter, int &TotalNumberOfKeySets, Graph &TotalGraph)
|
---|
213 | {
|
---|
214 | LOG(1, "Begin of TranslateIndicesToGlobalIDs.");
|
---|
215 | KeySet *TempSet = new KeySet;
|
---|
216 | if (FragmentList[FragmentCounter] != NULL) {
|
---|
217 | for (Graph::iterator runner = FragmentList[FragmentCounter]->begin(); runner != FragmentList[FragmentCounter]->end(); runner++) {
|
---|
218 | for (KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++)
|
---|
219 | TempSet->insert((Leaf->FindAtom(*sprinter))->GetTrueFather()->getNr());
|
---|
220 | TotalGraph.insert(GraphPair(*TempSet, pair<int, double> (TotalNumberOfKeySets++, (*runner).second.second)));
|
---|
221 | TempSet->clear();
|
---|
222 | }
|
---|
223 | delete (TempSet);
|
---|
224 | } else {
|
---|
225 | LOG(1, "FragmentList is NULL.");
|
---|
226 | }
|
---|
227 | if (next != NULL)
|
---|
228 | next->TranslateIndicesToGlobalIDs(FragmentList, ++FragmentCounter, TotalNumberOfKeySets, TotalGraph);
|
---|
229 | FragmentCounter--;
|
---|
230 | LOG(1, "End of TranslateIndicesToGlobalIDs.");
|
---|
231 | };
|
---|
232 |
|
---|
233 | /** Simply counts the number of items in the list, from given MoleculeLeafClass.
|
---|
234 | * \return number of items
|
---|
235 | */
|
---|
236 | int MoleculeLeafClass::Count() const
|
---|
237 | {
|
---|
238 | if (next != NULL)
|
---|
239 | return next->Count() + 1;
|
---|
240 | else
|
---|
241 | return 1;
|
---|
242 | };
|
---|
243 |
|
---|