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