[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 5 | *
|
---|
| 6 | *
|
---|
| 7 | * This file is part of MoleCuilder.
|
---|
| 8 | *
|
---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 10 | * it under the terms of the GNU General Public License as published by
|
---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 12 | * (at your option) any later version.
|
---|
| 13 | *
|
---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | * GNU General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * You should have received a copy of the GNU General Public License
|
---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
[bcf653] | 21 | */
|
---|
| 22 |
|
---|
[97ebf8] | 23 | /*
|
---|
| 24 | * FragmentationAction.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: May 9, 2010
|
---|
| 27 | * Author: heber
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[bf3817] | 30 | // include config.h
|
---|
| 31 | #ifdef HAVE_CONFIG_H
|
---|
| 32 | #include <config.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
[ad011c] | 35 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 36 |
|
---|
[6f0841] | 37 | #include "Atom/atom.hpp"
|
---|
[ad011c] | 38 | #include "CodePatterns/Log.hpp"
|
---|
[ca8bea] | 39 | #include "Fragmentation/Exporters/ExportGraph_ToFiles.hpp"
|
---|
[246e13] | 40 | #include "Fragmentation/Fragmentation.hpp"
|
---|
[b4f72c] | 41 | #include "Fragmentation/Graph.hpp"
|
---|
[07a47e] | 42 | #include "Fragmentation/HydrogenSaturation_enum.hpp"
|
---|
[9511c7] | 43 | #include "Graph/CheckAgainstAdjacencyFile.hpp"
|
---|
[49c059] | 44 | #include "Graph/DepthFirstSearchAnalysis.hpp"
|
---|
[9511c7] | 45 | #include "Helpers/defs.hpp"
|
---|
[97ebf8] | 46 | #include "molecule.hpp"
|
---|
| 47 | #include "World.hpp"
|
---|
| 48 |
|
---|
| 49 | #include <iostream>
|
---|
[2a0eb0] | 50 | #include <map>
|
---|
[97ebf8] | 51 | #include <string>
|
---|
[2a0eb0] | 52 | #include <vector>
|
---|
[97ebf8] | 53 |
|
---|
[1fd675] | 54 | #include "Actions/FragmentationAction/FragmentationAction.hpp"
|
---|
[70d9b9] | 55 |
|
---|
[ce7fdc] | 56 | using namespace MoleCuilder;
|
---|
| 57 |
|
---|
[1fd675] | 58 | // and construct the stuff
|
---|
| 59 | #include "FragmentationAction.def"
|
---|
| 60 | #include "Action_impl_pre.hpp"
|
---|
| 61 | /** =========== define the function ====================== */
|
---|
[70d9b9] | 62 | Action::state_ptr FragmentationFragmentationAction::performCall() {
|
---|
[e4b5de] | 63 | clock_t start,end;
|
---|
[2a0eb0] | 64 | int ExitFlag = -1;
|
---|
| 65 | World &world = World::getInstance();
|
---|
[e4b5de] | 66 |
|
---|
[2a0eb0] | 67 | // inform about used parameters
|
---|
[99b0dc] | 68 | LOG(0, "STATUS: Fragmenting molecular system with current connection matrix maximum bond distance "
|
---|
[f10b0c] | 69 | << params.distance.get() << " up to "
|
---|
| 70 | << params.order.get() << " order. Fragment files begin with "
|
---|
| 71 | << params.prefix.get() << " and are stored as: "
|
---|
| 72 | << params.types.get() << "." << std::endl);
|
---|
[99b0dc] | 73 |
|
---|
[2a0eb0] | 74 | // check for selected atoms
|
---|
| 75 | if (world.beginAtomSelection() == world.endAtomSelection()) {
|
---|
| 76 | ELOG(1, "There are not atoms selected for fragmentation.");
|
---|
| 77 | return Action::failure;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | // go through all atoms, note down their molecules and group them
|
---|
| 81 | typedef std::multimap<molecule *, atom *> clusters_t;
|
---|
| 82 | clusters_t clusters;
|
---|
| 83 | for (World::AtomSelectionConstIterator iter = world.beginAtomSelection();
|
---|
| 84 | iter != world.endAtomSelection(); ++iter) {
|
---|
| 85 | clusters.insert( std::make_pair(iter->second->getMolecule(), iter->second) );
|
---|
| 86 | }
|
---|
| 87 | LOG(1, "INFO: There are " << clusters.size() << " molecules to consider.");
|
---|
| 88 |
|
---|
[b4f72c] | 89 | // get whether to saturate or not
|
---|
| 90 | const enum HydrogenSaturation saturation = params.DoSaturation.get() ? DoSaturate : DontSaturate;
|
---|
| 91 |
|
---|
[9511c7] | 92 | // parse in Adjacency file
|
---|
| 93 | std::ifstream File;
|
---|
| 94 | std::string filename;
|
---|
| 95 | filename = params.prefix.get() + ADJACENCYFILE;
|
---|
| 96 | File.open(filename.c_str(), ios::out);
|
---|
| 97 | CheckAgainstAdjacencyFile FileChecker(File);
|
---|
| 98 |
|
---|
[49c059] | 99 | DepthFirstSearchAnalysis DFS;
|
---|
[2a0eb0] | 100 | start = clock();
|
---|
| 101 | // go through all keys (i.e. all molecules)
|
---|
| 102 | clusters_t::const_iterator advanceiter;
|
---|
[b4f72c] | 103 | Graph TotalGraph;
|
---|
| 104 | int keysetcounter = 0;
|
---|
[2a0eb0] | 105 | for (clusters_t::const_iterator iter = clusters.begin();
|
---|
| 106 | iter != clusters.end();
|
---|
| 107 | iter = advanceiter) {
|
---|
| 108 | // get iterator to past last atom in this molecule
|
---|
| 109 | molecule * mol = iter->first;
|
---|
| 110 | advanceiter = clusters.upper_bound(mol);
|
---|
| 111 |
|
---|
| 112 | // copy molecule's atoms' ids as parameters to Fragmentation's AtomMask
|
---|
| 113 | std::vector<atomId_t> mols_atomids;
|
---|
| 114 | std::transform(iter, advanceiter, std::back_inserter(mols_atomids),
|
---|
| 115 | boost::bind( &atom::getNr,
|
---|
| 116 | boost::bind( &clusters_t::value_type::second, _1 ))
|
---|
| 117 | );
|
---|
| 118 | LOG(2, "INFO: Fragmenting in molecule " << mol->getName() << " in " << clusters.count(mol)
|
---|
| 119 | << " atoms, out of " << mol->getAtomCount() << ".");
|
---|
[ca8bea] | 120 | const enum HydrogenSaturation saturation = params.DoSaturation.get() ? DoSaturate : DontSaturate;
|
---|
| 121 | Fragmentation Fragmenter(mol, FileChecker, saturation);
|
---|
[2a0eb0] | 122 |
|
---|
| 123 | // perform fragmentation
|
---|
| 124 | LOG(0, std::endl << " ========== Fragmentation of molecule " << mol->getName() << " ========================= ");
|
---|
| 125 | {
|
---|
| 126 | const int tempFlag = Fragmenter.FragmentMolecule(mols_atomids, params.order.get(), params.prefix.get(), DFS);
|
---|
| 127 | if ((ExitFlag == 2) && (tempFlag != 2))
|
---|
| 128 | ExitFlag = tempFlag; // if there is one molecule that needs further fragmentation, it overrides others
|
---|
| 129 | if (ExitFlag == -1)
|
---|
| 130 | ExitFlag = tempFlag; // if we are the first, we set the standard
|
---|
[e4b5de] | 131 | }
|
---|
[b4f72c] | 132 | TotalGraph.InsertGraph(Fragmenter.getGraph(), keysetcounter);
|
---|
[ca8bea] | 133 |
|
---|
[97ebf8] | 134 | }
|
---|
[b4f72c] | 135 | LOG(0, "STATUS: There are " << keysetcounter << " fragments.");
|
---|
| 136 | // store molecule's fragment to file
|
---|
| 137 | ExportGraph_ToFiles exporter(TotalGraph, saturation);
|
---|
| 138 | exporter.setPrefix(params.prefix.get());
|
---|
| 139 | exporter.setOutputTypes(params.types.get());
|
---|
| 140 | exporter();
|
---|
[2a0eb0] | 141 | World::getInstance().setExitFlag(ExitFlag);
|
---|
| 142 | end = clock();
|
---|
| 143 | LOG(0, "STATUS: Clocks for this operation: " << (end-start) << ", time: " << ((double)(end-start)/CLOCKS_PER_SEC) << "s.");
|
---|
| 144 |
|
---|
[ca8bea] | 145 |
|
---|
[70d9b9] | 146 | return Action::success;
|
---|
[97ebf8] | 147 | }
|
---|
| 148 |
|
---|
| 149 | Action::state_ptr FragmentationFragmentationAction::performUndo(Action::state_ptr _state) {
|
---|
[70d9b9] | 150 | return Action::success;
|
---|
[97ebf8] | 151 | }
|
---|
| 152 |
|
---|
| 153 | Action::state_ptr FragmentationFragmentationAction::performRedo(Action::state_ptr _state){
|
---|
[70d9b9] | 154 | return Action::success;
|
---|
[97ebf8] | 155 | }
|
---|
| 156 |
|
---|
| 157 | bool FragmentationFragmentationAction::canUndo() {
|
---|
[70d9b9] | 158 | return true;
|
---|
[97ebf8] | 159 | }
|
---|
| 160 |
|
---|
| 161 | bool FragmentationFragmentationAction::shouldUndo() {
|
---|
[70d9b9] | 162 | return true;
|
---|
[97ebf8] | 163 | }
|
---|
[1fd675] | 164 | /** =========== end of function ====================== */
|
---|