| [77b350] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the COPYING file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 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 |  * HomologyGraph_getFromKeyset.cpp
 | 
|---|
 | 26 |  *
 | 
|---|
 | 27 |  *  Created on: Sep 25, 2012
 | 
|---|
 | 28 |  *      Author: heber
 | 
|---|
 | 29 |  */
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | // include config.h
 | 
|---|
 | 33 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 34 | #include <config.h>
 | 
|---|
 | 35 | #endif
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | #include "Fragmentation/Homology/HomologyGraph.hpp"
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 | #include "Atom/atom.hpp"
 | 
|---|
 | 44 | #include "Bond/bond.hpp"
 | 
|---|
 | 45 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
 | 46 | #include "Fragmentation/KeySet.hpp"
 | 
|---|
| [372c912] | 47 | #include "Fragmentation/Summation/IndexSet.hpp"
 | 
|---|
| [77b350] | 48 | #include "World.hpp"
 | 
|---|
 | 49 | 
 | 
|---|
| [67db80] | 50 | // we have placed these functions into an extra module to allow compiling the
 | 
|---|
 | 51 | // unit tests which do no require them against dummy units which do not pull
 | 
|---|
 | 52 | // in all the cludder of World, atom, molecule, and so on ...
 | 
|---|
 | 53 | 
 | 
|---|
| [647148] | 54 | template <typename S, typename U>
 | 
|---|
 | 55 | void addNodeToMap(S &_map,  std::pair< U, size_t> _pair) {
 | 
|---|
 | 56 |   std::pair<typename S::iterator,bool> inserter =
 | 
|---|
 | 57 |       _map.insert( _pair );
 | 
|---|
 | 58 |   if (!inserter.second)
 | 
|---|
 | 59 |     inserter.first->second += _pair.second;
 | 
|---|
 | 60 | }
 | 
|---|
 | 61 | 
 | 
|---|
| [372c912] | 62 | template <typename T>
 | 
|---|
 | 63 | const HomologyGraph::nodes_t getNodesFromSet(const std::set<T> &keyset)
 | 
|---|
 | 64 | {
 | 
|---|
 | 65 |   HomologyGraph::nodes_t nodes;
 | 
|---|
 | 66 |   for (typename std::set<T>::const_iterator iter = keyset.begin();
 | 
|---|
 | 67 |       iter != keyset.end(); ++iter) {
 | 
|---|
| [28e203] | 68 | //    LOG(2, "DEBUG: Current global id is " << *iter << ".");
 | 
|---|
| [f01769] | 69 |     const atom * const Walker = const_cast<const World &>(World::getInstance()).
 | 
|---|
 | 70 |         getAtom(AtomById(*iter));
 | 
|---|
| [372c912] | 71 |     if (Walker != NULL) {
 | 
|---|
| [28e203] | 72 |       size_t NoBonds = 0;
 | 
|---|
| [372c912] | 73 |       const BondList& ListOfBonds = Walker->getListOfBonds();
 | 
|---|
| [28e203] | 74 |       for (BondList::const_iterator bonditer = ListOfBonds.begin();
 | 
|---|
 | 75 |           bonditer != ListOfBonds.end(); ++bonditer) {
 | 
|---|
| [f01769] | 76 |         const atom * const OtherWalker = (*bonditer)->GetOtherAtom(Walker);
 | 
|---|
| [28e203] | 77 |         if (keyset.count(OtherWalker->getId()))
 | 
|---|
 | 78 |           ++NoBonds;
 | 
|---|
| [647148] | 79 |         else {
 | 
|---|
 | 80 |           // add as many bonds as saturation hydrogens
 | 
|---|
 | 81 |           NoBonds += (*bonditer)->getDegree();
 | 
|---|
 | 82 |           // add a saturation node for each, too
 | 
|---|
 | 83 | //          LOG(1, "DEBUG: Adding saturation node " << *Walker << ".");
 | 
|---|
 | 84 |           addNodeToMap(nodes, std::make_pair(FragmentNode(1, 1), (size_t)1) );
 | 
|---|
 | 85 |         }
 | 
|---|
| [28e203] | 86 |       }
 | 
|---|
| [647148] | 87 | //      LOG(1, "DEBUG: Adding node " << *Walker << " with element "
 | 
|---|
| [28e203] | 88 | //          << Walker->getElementNo() << " and " << NoBonds << " bonds.");
 | 
|---|
| [647148] | 89 |       addNodeToMap(nodes, std::make_pair(FragmentNode(Walker->getElementNo(), NoBonds), (size_t)1) );
 | 
|---|
| [372c912] | 90 |     } else {
 | 
|---|
| [28e203] | 91 |       ELOG(3, "Skipping id " << *iter << ", is not associated with any atom.");
 | 
|---|
| [77b350] | 92 |     }
 | 
|---|
 | 93 |   }
 | 
|---|
| [372c912] | 94 |   return nodes;
 | 
|---|
 | 95 | }
 | 
|---|
| [77b350] | 96 | 
 | 
|---|
| [372c912] | 97 | template <typename T>
 | 
|---|
 | 98 | const HomologyGraph::edges_t getEdgesFromSet(const std::set<T> &keyset)
 | 
|---|
 | 99 | {
 | 
|---|
 | 100 |   HomologyGraph::edges_t edges;
 | 
|---|
 | 101 |   for (typename std::set<T>::const_iterator iter = keyset.begin();
 | 
|---|
 | 102 |       iter != keyset.end(); ++iter) {
 | 
|---|
| [28e203] | 103 | //    LOG(2, "DEBUG: Current global id is " << *iter << ".");
 | 
|---|
| [f01769] | 104 |     const atom * const Walker = const_cast<const World &>(World::getInstance()).
 | 
|---|
 | 105 |         getAtom(AtomById(*iter));
 | 
|---|
| [372c912] | 106 |     if (Walker != NULL) {
 | 
|---|
 | 107 |       const BondList& ListOfBonds = Walker->getListOfBonds();
 | 
|---|
 | 108 |       for (BondList::const_iterator bonditer = ListOfBonds.begin();
 | 
|---|
 | 109 |           bonditer != ListOfBonds.end(); ++bonditer) {
 | 
|---|
| [f01769] | 110 |         const atom * const OtherWalker = (*bonditer)->GetOtherAtom(Walker);
 | 
|---|
| [647148] | 111 | //        LOG(1, "DEBUG: Neighbor is " << OtherWalker->getId() << ".");
 | 
|---|
 | 112 |         if (keyset.count(OtherWalker->getId())) {
 | 
|---|
 | 113 |           if (Walker->getId() < OtherWalker->getId()) {
 | 
|---|
 | 114 | //            LOG(1, "DEBUG: Adding edge " << Walker->getId() << " and " << OtherWalker->getId() << ".");
 | 
|---|
 | 115 |             addNodeToMap(edges, std::make_pair(FragmentEdge( Walker->getElementNo(), OtherWalker->getElementNo()), (size_t)1) );
 | 
|---|
 | 116 |           }
 | 
|---|
 | 117 |         } else { // also add edge for each saturation hydrogen
 | 
|---|
 | 118 |           const size_t bonddegree = (*bonditer)->getDegree();
 | 
|---|
 | 119 | //          LOG(1, "DEBUG: Adding " << bonddegree << " saturation edge(s) " << Walker->getId() << " and " << OtherWalker->getId() << ".");
 | 
|---|
 | 120 |           addNodeToMap(edges, std::make_pair(FragmentEdge( Walker->getElementNo(), 1), bonddegree) );
 | 
|---|
| [28e203] | 121 |         }
 | 
|---|
| [77b350] | 122 |       }
 | 
|---|
| [372c912] | 123 |     } else {
 | 
|---|
| [28e203] | 124 |       ELOG(3, "Skipping id " << *iter << ", is not associated with any atom.");
 | 
|---|
| [77b350] | 125 |     }
 | 
|---|
| [372c912] | 126 |   }
 | 
|---|
 | 127 |   return edges;
 | 
|---|
 | 128 | }
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 | namespace detail {
 | 
|---|
 | 131 |   const HomologyGraph::nodes_t getNodesFromKeySet(const KeySet &keyset) {
 | 
|---|
 | 132 |     return getNodesFromSet<int>(keyset);
 | 
|---|
 | 133 |   }
 | 
|---|
 | 134 |   const HomologyGraph::nodes_t getNodesFromIndexSet(const IndexSet &keyset) {
 | 
|---|
 | 135 |     return getNodesFromSet<size_t>(keyset);
 | 
|---|
 | 136 |   }
 | 
|---|
 | 137 |   const HomologyGraph::edges_t getEdgesFromKeySet(const KeySet &keyset) {
 | 
|---|
 | 138 |     return getEdgesFromSet<int>(keyset);
 | 
|---|
 | 139 |   }
 | 
|---|
 | 140 |   const HomologyGraph::edges_t getEdgesFromIndexSet(const IndexSet &keyset) {
 | 
|---|
 | 141 |     return getEdgesFromSet<size_t>(keyset);
 | 
|---|
| [77b350] | 142 |   }
 | 
|---|
 | 143 | };  /* namespace detail */
 | 
|---|
 | 144 | 
 | 
|---|