| 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 | #ifdef HAVE_JOBMARKET
|
|---|
| 44 | #include "Fragmentation/Exporters/ExportGraph_ToJobs.hpp"
|
|---|
| 45 | #endif
|
|---|
| 46 | #include "Fragmentation/Fragmentation.hpp"
|
|---|
| 47 | #include "Fragmentation/Graph.hpp"
|
|---|
| 48 | #include "Fragmentation/HydrogenSaturation_enum.hpp"
|
|---|
| 49 | #include "Graph/AdjacencyList.hpp"
|
|---|
| 50 | #include "Graph/BondGraph.hpp"
|
|---|
| 51 | #include "Graph/DepthFirstSearchAnalysis.hpp"
|
|---|
| 52 | #include "Helpers/defs.hpp"
|
|---|
| 53 | #include "molecule.hpp"
|
|---|
| 54 | #include "World.hpp"
|
|---|
| 55 |
|
|---|
| 56 | #include <boost/shared_ptr.hpp>
|
|---|
| 57 | #include <boost/filesystem.hpp>
|
|---|
| 58 | #include <algorithm>
|
|---|
| 59 | #include <iostream>
|
|---|
| 60 | #include <map>
|
|---|
| 61 | #include <string>
|
|---|
| 62 | #include <vector>
|
|---|
| 63 |
|
|---|
| 64 | #include "Actions/FragmentationAction/FragmentationAction.hpp"
|
|---|
| 65 |
|
|---|
| 66 | using namespace MoleCuilder;
|
|---|
| 67 |
|
|---|
| 68 | // and construct the stuff
|
|---|
| 69 | #include "FragmentationAction.def"
|
|---|
| 70 | #include "Action_impl_pre.hpp"
|
|---|
| 71 | /** =========== define the function ====================== */
|
|---|
| 72 | Action::state_ptr FragmentationFragmentationAction::performCall() {
|
|---|
| 73 | clock_t start,end;
|
|---|
| 74 | int ExitFlag = -1;
|
|---|
| 75 | World &world = World::getInstance();
|
|---|
| 76 |
|
|---|
| 77 | // inform about used parameters
|
|---|
| 78 | LOG(0, "STATUS: Fragmenting molecular system with current connection matrix maximum bond distance "
|
|---|
| 79 | << params.distance.get() << " up to "
|
|---|
| 80 | << params.order.get() << " order. ");
|
|---|
| 81 | if (params.types.get().size() != 0)
|
|---|
| 82 | LOG(0, "STATUS: Fragment files begin with "
|
|---|
| 83 | << params.prefix.get() << " and are stored as: "
|
|---|
| 84 | << params.types.get() << "." << std::endl);
|
|---|
| 85 |
|
|---|
| 86 | // check for selected atoms
|
|---|
| 87 | if (world.beginAtomSelection() == world.endAtomSelection()) {
|
|---|
| 88 | ELOG(1, "There are not atoms selected for fragmentation.");
|
|---|
| 89 | return Action::failure;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | // go through all atoms, note down their molecules and group them
|
|---|
| 93 | typedef std::multimap<molecule *, atom *> clusters_t;
|
|---|
| 94 | typedef std::vector<atomId_t> atomids_t;
|
|---|
| 95 | atomids_t atomids;
|
|---|
| 96 | clusters_t clusters;
|
|---|
| 97 | for (World::AtomSelectionConstIterator iter = world.beginAtomSelection();
|
|---|
| 98 | iter != world.endAtomSelection(); ++iter) {
|
|---|
| 99 | clusters.insert( std::make_pair(iter->second->getMolecule(), iter->second) );
|
|---|
| 100 | atomids.push_back(iter->second->getId());
|
|---|
| 101 | }
|
|---|
| 102 | {
|
|---|
| 103 | std::vector<molecule *> molecules;
|
|---|
| 104 | molecules.insert( molecules.end(), MapKeyIterator<clusters_t::const_iterator>(clusters.begin()),
|
|---|
| 105 | MapKeyIterator<clusters_t::const_iterator>(clusters.end()) );
|
|---|
| 106 | molecules.erase( std::unique(molecules.begin(), molecules.end()), molecules.end() );
|
|---|
| 107 | LOG(1, "INFO: There are " << molecules.size() << " molecules to consider.");
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | // parse in Adjacency file
|
|---|
| 111 | boost::shared_ptr<AdjacencyList> FileChecker;
|
|---|
| 112 | boost::filesystem::path filename(params.prefix.get() + std::string(ADJACENCYFILE));
|
|---|
| 113 | if (boost::filesystem::exists(filename) && boost::filesystem::is_regular_file(filename)) {
|
|---|
| 114 | std::ifstream File;
|
|---|
| 115 | File.open(filename.string().c_str(), ios::out);
|
|---|
| 116 | FileChecker.reset(new AdjacencyList(File));
|
|---|
| 117 | File.close();
|
|---|
| 118 | } else {
|
|---|
| 119 | LOG(1, "INFO: Could not open default adjacency file " << filename.string() << ".");
|
|---|
| 120 | FileChecker.reset(new AdjacencyList);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | // make sure bond degree is correct
|
|---|
| 124 | {
|
|---|
| 125 | BondGraph *BG = World::getInstance().getBondGraph();
|
|---|
| 126 | World::AtomComposite Set = World::getInstance().getAllAtoms(AtomsBySelection());
|
|---|
| 127 | BG->CorrectBondDegree(Set);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | // we require the current bond graph
|
|---|
| 131 | DepthFirstSearchAnalysis DFS;
|
|---|
| 132 |
|
|---|
| 133 | // we parse in the keysets from last time if present
|
|---|
| 134 | Graph StoredGraph;
|
|---|
| 135 | StoredGraph.ParseKeySetFile(params.prefix.get());
|
|---|
| 136 |
|
|---|
| 137 | start = clock();
|
|---|
| 138 | // go through all keys (i.e. all molecules)
|
|---|
| 139 | clusters_t::const_iterator advanceiter;
|
|---|
| 140 | Graph TotalGraph;
|
|---|
| 141 | int keysetcounter = 0;
|
|---|
| 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() << ".");
|
|---|
| 157 | const enum HydrogenTreatment treatment = params.HowtoTreatHydrogen.get() ? ExcludeHydrogen : IncludeHydrogen;
|
|---|
| 158 | Fragmentation Fragmenter(mol, *FileChecker, treatment);
|
|---|
| 159 |
|
|---|
| 160 | // perform fragmentation
|
|---|
| 161 | LOG(0, std::endl << " ========== Fragmentation of molecule " << mol->getName() << " ========================= ");
|
|---|
| 162 | {
|
|---|
| 163 | Graph StoredLocalGraph(StoredGraph.getLocalGraph(mol));
|
|---|
| 164 | const int tempFlag = Fragmenter.FragmentMolecule(mols_atomids, params.order.get(), params.prefix.get(), DFS, StoredLocalGraph);
|
|---|
| 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
|
|---|
| 169 | }
|
|---|
| 170 | if (TotalGraph.empty()) {
|
|---|
| 171 | TotalGraph = Fragmenter.getGraph();
|
|---|
| 172 | keysetcounter = TotalGraph.size();
|
|---|
| 173 | } else
|
|---|
| 174 | TotalGraph.InsertGraph(Fragmenter.getGraph(), keysetcounter);
|
|---|
| 175 |
|
|---|
| 176 | }
|
|---|
| 177 | LOG(0, "STATUS: There are " << TotalGraph.size() << " fragments.");
|
|---|
| 178 |
|
|---|
| 179 | // store keysets to file
|
|---|
| 180 | {
|
|---|
| 181 | TotalGraph.StoreKeySetFile(params.prefix.get());
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | {
|
|---|
| 185 | const enum HydrogenSaturation saturation = params.DoSaturation.get() ? DoSaturate : DontSaturate;
|
|---|
| 186 | const enum HydrogenTreatment treatment = params.HowtoTreatHydrogen.get() ? ExcludeHydrogen : IncludeHydrogen;
|
|---|
| 187 | if (params.types.get().size() != 0) {
|
|---|
| 188 | // store molecule's fragment to file
|
|---|
| 189 | ExportGraph_ToFiles exporter(TotalGraph, treatment, saturation);
|
|---|
| 190 | exporter.setPrefix(params.prefix.get());
|
|---|
| 191 | exporter.setOutputTypes(params.types.get());
|
|---|
| 192 | exporter();
|
|---|
| 193 | } else {
|
|---|
| 194 | #ifdef HAVE_JOBMARKET
|
|---|
| 195 | // store molecule's fragment in FragmentJobQueue
|
|---|
| 196 | ExportGraph_ToJobs exporter(TotalGraph, treatment, saturation);
|
|---|
| 197 | exporter.setLevel(params.level.get());
|
|---|
| 198 | exporter();
|
|---|
| 199 | #else
|
|---|
| 200 | ELOG(1, "No output file types specified and JobMarket support is not compiled in.");
|
|---|
| 201 | return Action::failure;
|
|---|
| 202 | #endif
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | // store Adjacency to file
|
|---|
| 207 | {
|
|---|
| 208 | std::string filename = params.prefix.get() + ADJACENCYFILE;
|
|---|
| 209 | std::ofstream AdjacencyFile;
|
|---|
| 210 | AdjacencyFile.open(filename.c_str(), ios::out);
|
|---|
| 211 | AdjacencyList adjacency(atomids);
|
|---|
| 212 | adjacency.StoreToFile(AdjacencyFile);
|
|---|
| 213 | AdjacencyFile.close();
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | World::getInstance().setExitFlag(ExitFlag);
|
|---|
| 217 | end = clock();
|
|---|
| 218 | LOG(0, "STATUS: Clocks for this operation: " << (end-start) << ", time: " << ((double)(end-start)/CLOCKS_PER_SEC) << "s.");
|
|---|
| 219 |
|
|---|
| 220 | return Action::success;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | Action::state_ptr FragmentationFragmentationAction::performUndo(Action::state_ptr _state) {
|
|---|
| 224 | return Action::success;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | Action::state_ptr FragmentationFragmentationAction::performRedo(Action::state_ptr _state){
|
|---|
| 228 | return Action::success;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | bool FragmentationFragmentationAction::canUndo() {
|
|---|
| 232 | return true;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | bool FragmentationFragmentationAction::shouldUndo() {
|
|---|
| 236 | return true;
|
|---|
| 237 | }
|
|---|
| 238 | /** =========== end of function ====================== */
|
|---|