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