1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
6 | *
|
---|
7 | *
|
---|
8 | * This file is part of MoleCuilder.
|
---|
9 | *
|
---|
10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation, either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*
|
---|
25 | * FragmentationAction.cpp
|
---|
26 | *
|
---|
27 | * Created on: May 9, 2010
|
---|
28 | * Author: heber
|
---|
29 | */
|
---|
30 |
|
---|
31 | // include config.h
|
---|
32 | #ifdef HAVE_CONFIG_H
|
---|
33 | #include <config.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include "CodePatterns/MemDebug.hpp"
|
---|
37 |
|
---|
38 | #include "Atom/atom.hpp"
|
---|
39 | #include "CodePatterns/IteratorAdaptors.hpp"
|
---|
40 | #include "CodePatterns/Log.hpp"
|
---|
41 | #include "Descriptors/AtomSelectionDescriptor.hpp"
|
---|
42 | #include "Fragmentation/Exporters/ExportGraph_ToFiles.hpp"
|
---|
43 | #include "Fragmentation/Exporters/ExportGraph_ToJobs.hpp"
|
---|
44 | #include "Fragmentation/Exporters/SaturatedFragment.hpp"
|
---|
45 | #include "Fragmentation/Fragmentation.hpp"
|
---|
46 | #include "Fragmentation/Graph.hpp"
|
---|
47 | #include "Fragmentation/HydrogenSaturation_enum.hpp"
|
---|
48 | #include "Fragmentation/Interfragmenter.hpp"
|
---|
49 | #include "Fragmentation/KeySetsContainer.hpp"
|
---|
50 | #include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp"
|
---|
51 | #include "Graph/AdjacencyList.hpp"
|
---|
52 | #include "Graph/BondGraph.hpp"
|
---|
53 | #include "Graph/CyclicStructureAnalysis.hpp"
|
---|
54 | #include "Graph/DepthFirstSearchAnalysis.hpp"
|
---|
55 | #include "Helpers/defs.hpp"
|
---|
56 | #include "molecule.hpp"
|
---|
57 | #include "World.hpp"
|
---|
58 |
|
---|
59 | #include <boost/shared_ptr.hpp>
|
---|
60 | #include <boost/filesystem.hpp>
|
---|
61 | #include <algorithm>
|
---|
62 | #include <iostream>
|
---|
63 | #include <map>
|
---|
64 | #include <string>
|
---|
65 | #include <vector>
|
---|
66 |
|
---|
67 | #include "Actions/FragmentationAction/FragmentationAction.hpp"
|
---|
68 |
|
---|
69 | using namespace MoleCuilder;
|
---|
70 |
|
---|
71 | // and construct the stuff
|
---|
72 | #include "FragmentationAction.def"
|
---|
73 | #include "Action_impl_pre.hpp"
|
---|
74 | /** =========== define the function ====================== */
|
---|
75 | ActionState::ptr FragmentationFragmentationAction::performCall() {
|
---|
76 | clock_t start,end;
|
---|
77 | int ExitFlag = -1;
|
---|
78 | World &world = World::getInstance();
|
---|
79 |
|
---|
80 | // inform about used parameters
|
---|
81 | LOG(0, "STATUS: Fragmenting molecular system with current connection matrix up to "
|
---|
82 | << params.order.get() << " order. ");
|
---|
83 | if (params.types.get().size() != 0)
|
---|
84 | LOG(0, "STATUS: Fragment files begin with "
|
---|
85 | << params.prefix.get() << " and are stored as: "
|
---|
86 | << params.types.get() << "." << std::endl);
|
---|
87 |
|
---|
88 | // check for selected atoms
|
---|
89 | if (world.beginAtomSelection() == world.endAtomSelection()) {
|
---|
90 | STATUS("There are no atoms selected for fragmentation.");
|
---|
91 | return Action::failure;
|
---|
92 | }
|
---|
93 |
|
---|
94 | // go through all atoms, note down their molecules and group them
|
---|
95 | typedef std::multimap<molecule *, atom *> clusters_t;
|
---|
96 | typedef std::vector<atomId_t> atomids_t;
|
---|
97 | atomids_t atomids;
|
---|
98 | clusters_t clusters;
|
---|
99 | for (World::AtomSelectionConstIterator iter = world.beginAtomSelection();
|
---|
100 | iter != world.endAtomSelection(); ++iter) {
|
---|
101 | clusters.insert( std::make_pair(iter->second->getMolecule(), iter->second) );
|
---|
102 | atomids.push_back(iter->second->getId());
|
---|
103 | }
|
---|
104 | {
|
---|
105 | std::vector<molecule *> molecules;
|
---|
106 | molecules.insert( molecules.end(), MapKeyIterator<clusters_t::const_iterator>(clusters.begin()),
|
---|
107 | MapKeyIterator<clusters_t::const_iterator>(clusters.end()) );
|
---|
108 | molecules.erase( std::unique(molecules.begin(), molecules.end()), molecules.end() );
|
---|
109 | LOG(1, "INFO: There are " << molecules.size() << " molecules to consider.");
|
---|
110 | }
|
---|
111 |
|
---|
112 | // parse in Adjacency file
|
---|
113 | boost::shared_ptr<AdjacencyList> FileChecker;
|
---|
114 | boost::filesystem::path filename(params.prefix.get() + std::string(ADJACENCYFILE));
|
---|
115 | if (boost::filesystem::exists(filename) && boost::filesystem::is_regular_file(filename)) {
|
---|
116 | std::ifstream File;
|
---|
117 | File.open(filename.string().c_str(), ios::out);
|
---|
118 | FileChecker.reset(new AdjacencyList(File));
|
---|
119 | File.close();
|
---|
120 | } else {
|
---|
121 | LOG(1, "INFO: Could not open default adjacency file " << filename.string() << ".");
|
---|
122 | FileChecker.reset(new AdjacencyList);
|
---|
123 | }
|
---|
124 |
|
---|
125 | // make sure bond degree is correct
|
---|
126 | {
|
---|
127 | BondGraph *BG = World::getInstance().getBondGraph();
|
---|
128 | World::AtomComposite Set = World::getInstance().getAllAtoms(AtomsBySelection());
|
---|
129 | BG->CorrectBondDegree(Set);
|
---|
130 | }
|
---|
131 |
|
---|
132 | // we parse in the keysets from last time if present
|
---|
133 | Graph StoredGraph;
|
---|
134 | StoredGraph.ParseKeySetFile(params.prefix.get());
|
---|
135 |
|
---|
136 | start = clock();
|
---|
137 | // go through all keys (i.e. all molecules)
|
---|
138 | clusters_t::const_iterator advanceiter;
|
---|
139 | Graph TotalGraph;
|
---|
140 | int keysetcounter = 0;
|
---|
141 | for (clusters_t::const_iterator iter = clusters.begin();
|
---|
142 | iter != clusters.end();
|
---|
143 | iter = advanceiter) {
|
---|
144 | // get iterator to past last atom in this molecule
|
---|
145 | molecule * mol = iter->first;
|
---|
146 | advanceiter = clusters.upper_bound(mol);
|
---|
147 |
|
---|
148 | // copy molecule's atoms' ids as parameters to Fragmentation's AtomMask
|
---|
149 | std::vector<atomId_t> mols_atomids;
|
---|
150 | std::transform(iter, advanceiter, std::back_inserter(mols_atomids),
|
---|
151 | boost::bind( &atom::getNr,
|
---|
152 | boost::bind( &clusters_t::value_type::second, _1 ))
|
---|
153 | );
|
---|
154 | LOG(2, "INFO: Fragmenting in molecule " << mol->getName() << " in " << clusters.count(mol)
|
---|
155 | << " atoms, out of " << mol->getAtomCount() << ".");
|
---|
156 | const enum HydrogenTreatment treatment = params.HowtoTreatHydrogen.get() ? ExcludeHydrogen : IncludeHydrogen;
|
---|
157 | Fragmentation Fragmenter(mol, *FileChecker, treatment);
|
---|
158 |
|
---|
159 | // perform fragmentation
|
---|
160 | LOG(0, std::endl << " ========== Fragmentation of molecule " << mol->getName() << " ========================= ");
|
---|
161 | {
|
---|
162 | Graph StoredLocalGraph(StoredGraph.getLocalGraph(mol));
|
---|
163 | const int tempFlag = Fragmenter.FragmentMolecule(mols_atomids, params.order.get(), params.prefix.get(), StoredLocalGraph);
|
---|
164 | if ((ExitFlag == 2) && (tempFlag != 2))
|
---|
165 | ExitFlag = tempFlag; // if there is one molecule that needs further fragmentation, it overrides others
|
---|
166 | if (ExitFlag == -1)
|
---|
167 | ExitFlag = tempFlag; // if we are the first, we set the standard
|
---|
168 | }
|
---|
169 | if (TotalGraph.empty()) {
|
---|
170 | TotalGraph = Fragmenter.getGraph();
|
---|
171 | keysetcounter = TotalGraph.size();
|
---|
172 | } else
|
---|
173 | TotalGraph.InsertGraph(Fragmenter.getGraph(), keysetcounter);
|
---|
174 |
|
---|
175 | }
|
---|
176 | // add full cycles if desired
|
---|
177 | if (params.DoCyclesFull.get()) {
|
---|
178 | // get the BackEdgeStack from somewhere
|
---|
179 | DepthFirstSearchAnalysis DFS;
|
---|
180 | DFS();
|
---|
181 | std::deque<bond::ptr> BackEdgeStack = DFS.getBackEdgeStack();
|
---|
182 | // then we analyse the cycles and get them
|
---|
183 | CyclicStructureAnalysis CycleAnalysis(params.HowtoTreatHydrogen.get() ? ExcludeHydrogen : IncludeHydrogen);
|
---|
184 | CycleAnalysis(&BackEdgeStack);
|
---|
185 | CyclicStructureAnalysis::cycles_t cycles = CycleAnalysis.getAllCycles();
|
---|
186 | // sort them according to KeySet::operator<()
|
---|
187 | std::sort(cycles.begin(), cycles.end());
|
---|
188 | // store all found cycles to file
|
---|
189 | {
|
---|
190 | boost::filesystem::path filename(params.prefix.get() + std::string(CYCLEKEYSETFILE));
|
---|
191 | std::ofstream File;
|
---|
192 | LOG(1, "INFO: Storing cycle keysets to " << filename.string() << ".");
|
---|
193 | File.open(filename.string().c_str(), ios::out);
|
---|
194 | for (CyclicStructureAnalysis::cycles_t::const_iterator iter = cycles.begin();
|
---|
195 | iter != cycles.end(); ++iter) {
|
---|
196 | for (CyclicStructureAnalysis::cycle_t::const_iterator cycleiter = (*iter).begin();
|
---|
197 | cycleiter != (*iter).end(); ++cycleiter) {
|
---|
198 | File << *cycleiter << "\t";
|
---|
199 | }
|
---|
200 | File << "\n";
|
---|
201 | }
|
---|
202 | File.close();
|
---|
203 | }
|
---|
204 | // ... and to result container
|
---|
205 | {
|
---|
206 | KeySetsContainer cyclekeys;
|
---|
207 | for (CyclicStructureAnalysis::cycles_t::const_iterator iter = cycles.begin();
|
---|
208 | iter != cycles.end(); ++iter) {
|
---|
209 | const CyclicStructureAnalysis::cycle_t &cycle = *iter;
|
---|
210 | const size_t order = cycle.size();
|
---|
211 | KeySetsContainer::IntVector temp_cycle(cycle.begin(), cycle.end());
|
---|
212 | cyclekeys.insert(temp_cycle, order);
|
---|
213 | }
|
---|
214 | FragmentationResultContainer::getInstance().addCycles(cyclekeys);
|
---|
215 | }
|
---|
216 | // Create graph and insert into TotalGraph
|
---|
217 | LOG(0, "STATUS: Adding " << cycles.size() << " cycles.");
|
---|
218 | {
|
---|
219 | Graph CycleGraph;
|
---|
220 | for (CyclicStructureAnalysis::cycles_t::const_iterator iter = cycles.begin();
|
---|
221 | iter != cycles.end(); ++iter) {
|
---|
222 | const CyclicStructureAnalysis::cycle_t ¤tcycle = *iter;
|
---|
223 | LOG(2, "INFO: Inserting cycle " << currentcycle << ".");
|
---|
224 | #ifndef NDEBUG
|
---|
225 | std::pair< Graph::iterator, bool > inserter =
|
---|
226 | #endif
|
---|
227 | CycleGraph.insert( std::make_pair(currentcycle, NumberValuePair(1,1.)) );
|
---|
228 | ASSERT( inserter.second,
|
---|
229 | "FragmentationFragmentationAction::performCall() - keyset "
|
---|
230 | +toString(currentcycle)+" inserted twice into CycleGraph.");
|
---|
231 | }
|
---|
232 | TotalGraph.InsertGraph(CycleGraph, keysetcounter);
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | LOG(0, "STATUS: There are " << TotalGraph.size() << " fragments.");
|
---|
237 |
|
---|
238 | {
|
---|
239 | // remove OrderAtSite file
|
---|
240 | std::string line;
|
---|
241 | std::ofstream file;
|
---|
242 | line = params.prefix.get() + ORDERATSITEFILE;
|
---|
243 | file.open(line.c_str(), std::ofstream::out | std::ofstream::trunc);
|
---|
244 | file << "";
|
---|
245 | file.close();
|
---|
246 | }
|
---|
247 |
|
---|
248 | // now add interfragments
|
---|
249 | if (params.InterOrder.get() != 0) {
|
---|
250 | LOG(0, "STATUS: Putting fragments together up to order "
|
---|
251 | << params.InterOrder.get() << " and distance of "
|
---|
252 | << params.distance.get() << ".");
|
---|
253 | Interfragmenter fragmenter(TotalGraph);
|
---|
254 | const enum HydrogenTreatment treatment = params.HowtoTreatHydrogen.get() ? ExcludeHydrogen : IncludeHydrogen;
|
---|
255 | fragmenter(params.InterOrder.get(), params.distance.get(), treatment);
|
---|
256 | LOG(0, "STATUS: There are now " << TotalGraph.size() << " fragments after interfragmenting.");
|
---|
257 | }
|
---|
258 |
|
---|
259 | // store keysets to file
|
---|
260 | {
|
---|
261 | TotalGraph.StoreKeySetFile(params.prefix.get());
|
---|
262 | }
|
---|
263 |
|
---|
264 | // create global saturation positions map
|
---|
265 | SaturatedFragment::GlobalSaturationPositions_t globalsaturationpositions;
|
---|
266 |
|
---|
267 | {
|
---|
268 | const enum HydrogenSaturation saturation = params.DoSaturation.get() ? DoSaturate : DontSaturate;
|
---|
269 | const enum HydrogenTreatment treatment = params.HowtoTreatHydrogen.get() ? ExcludeHydrogen : IncludeHydrogen;
|
---|
270 | if (params.types.get().size() != 0) {
|
---|
271 | // store molecule's fragment to file
|
---|
272 | ExportGraph_ToFiles exporter(TotalGraph, treatment, saturation, globalsaturationpositions);
|
---|
273 | exporter.setPrefix(params.prefix.get());
|
---|
274 | exporter.setOutputTypes(params.types.get());
|
---|
275 | exporter();
|
---|
276 | } else {
|
---|
277 | // store molecule's fragment in FragmentJobQueue
|
---|
278 | ExportGraph_ToJobs exporter(TotalGraph, treatment, saturation, globalsaturationpositions);
|
---|
279 | exporter.setLevel(params.level.get());
|
---|
280 | exporter();
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | // store Adjacency to file
|
---|
285 | {
|
---|
286 | std::string filename = params.prefix.get() + ADJACENCYFILE;
|
---|
287 | std::ofstream AdjacencyFile;
|
---|
288 | AdjacencyFile.open(filename.c_str(), ios::out);
|
---|
289 | AdjacencyList adjacency(atomids);
|
---|
290 | adjacency.StoreToFile(AdjacencyFile);
|
---|
291 | AdjacencyFile.close();
|
---|
292 | }
|
---|
293 |
|
---|
294 | World::getInstance().setExitFlag(ExitFlag);
|
---|
295 | end = clock();
|
---|
296 | LOG(0, "STATUS: Clocks for this operation: " << (end-start) << ", time: " << ((double)(end-start)/CLOCKS_PER_SEC) << "s.");
|
---|
297 |
|
---|
298 | return Action::success;
|
---|
299 | }
|
---|
300 |
|
---|
301 | ActionState::ptr FragmentationFragmentationAction::performUndo(ActionState::ptr _state) {
|
---|
302 | return Action::success;
|
---|
303 | }
|
---|
304 |
|
---|
305 | ActionState::ptr FragmentationFragmentationAction::performRedo(ActionState::ptr _state){
|
---|
306 | return Action::success;
|
---|
307 | }
|
---|
308 |
|
---|
309 | bool FragmentationFragmentationAction::canUndo() {
|
---|
310 | return true;
|
---|
311 | }
|
---|
312 |
|
---|
313 | bool FragmentationFragmentationAction::shouldUndo() {
|
---|
314 | return true;
|
---|
315 | }
|
---|
316 | /** =========== end of function ====================== */
|
---|