| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2017 Frederik Heber. All rights reserved.
 | 
|---|
| 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/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * BoostGraphCreator.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: May 17, 2017
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 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 "BoostGraphCreator.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include <algorithm>
 | 
|---|
| 41 | #include <iterator>
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 44 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include "Atom/atom.hpp"
 | 
|---|
| 47 | #include "Bond/bond.hpp"
 | 
|---|
| 48 | #include "molecule.hpp"
 | 
|---|
| 49 | 
 | 
|---|
| 50 | void BoostGraphCreator::createFromMolecule(
 | 
|---|
| 51 |     const molecule &_mol,
 | 
|---|
| 52 |     const predicate_t &_pred)
 | 
|---|
| 53 | {
 | 
|---|
| 54 |   createFromRange<molecule::const_iterator>(_mol.begin(), _mol.end(), _mol.getAtomCount(), _pred);
 | 
|---|
| 55 | }
 | 
|---|
| 56 | 
 | 
|---|
| 57 | static bool predicateAnd(
 | 
|---|
| 58 |     const BoostGraphCreator::predicate_t &_pred1,
 | 
|---|
| 59 |     const BoostGraphCreator::predicate_t &_pred2,
 | 
|---|
| 60 |     const bond &_bond)
 | 
|---|
| 61 | { return (_pred1(_bond) && _pred2(_bond)); }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | static atomId_t getAtomId(
 | 
|---|
| 64 |     const atom *_atom
 | 
|---|
| 65 |     )
 | 
|---|
| 66 | { return _atom->getId(); }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | static bool inSetPredicate(
 | 
|---|
| 69 |   const std::vector<atomId_t> &_atomids,
 | 
|---|
| 70 |   const bond &_bond)
 | 
|---|
| 71 | {
 | 
|---|
| 72 |   const atomId_t leftid = _bond.leftatom->getId();
 | 
|---|
| 73 |   const atomId_t rightid = _bond.rightatom->getId();
 | 
|---|
| 74 |   return std::binary_search(_atomids.begin(), _atomids.end(), leftid)
 | 
|---|
| 75 |       && std::binary_search(_atomids.begin(), _atomids.end(), rightid);
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | void BoostGraphCreator::createFromAtoms(
 | 
|---|
| 79 |     const std::vector<atom *> &_atoms,
 | 
|---|
| 80 |     const predicate_t &_pred)
 | 
|---|
| 81 | {
 | 
|---|
| 82 |   // sort makes binary_search possible
 | 
|---|
| 83 |   std::vector<atomId_t> atomids;
 | 
|---|
| 84 |   std::transform(_atoms.begin(), _atoms.end(),
 | 
|---|
| 85 |       std::back_inserter(atomids), getAtomId);
 | 
|---|
| 86 |   ASSERT( _atoms.size() == atomids.size(),
 | 
|---|
| 87 |       "BoostGraphCreator::createFromAtom() - atomids "
 | 
|---|
| 88 |       +toString(atomids.size())+" and atoms "+toString(_atoms.size())
 | 
|---|
| 89 |       +" differ in size?");
 | 
|---|
| 90 |   std::sort(atomids.begin(), atomids.end());
 | 
|---|
| 91 |   const predicate_t predicate = boost::bind(inSetPredicate, boost::ref(atomids), _1);
 | 
|---|
| 92 |   createFromRange<std::vector<atom *>::const_iterator>(
 | 
|---|
| 93 |       const_cast<const std::vector<atom *> &>(_atoms).begin(),
 | 
|---|
| 94 |       const_cast<const std::vector<atom *> &>(_atoms).end(),
 | 
|---|
| 95 |       _atoms.size(),
 | 
|---|
| 96 |       boost::bind(predicateAnd, _pred, predicate, _1));
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | BoostGraphCreator::nodeId_t BoostGraphCreator::getNodeId(
 | 
|---|
| 100 |     const atomId_t &_atomid) const
 | 
|---|
| 101 | {
 | 
|---|
| 102 |   atomids_nodeids_t::const_iterator iter =
 | 
|---|
| 103 |       atomids_nodeids.find(_atomid);
 | 
|---|
| 104 |   return (iter == atomids_nodeids.end()) ? (nodeId_t)-1 : iter->second;
 | 
|---|
| 105 | }
 | 
|---|
| 106 | 
 | 
|---|
| 107 | template <typename iterator>
 | 
|---|
| 108 | void BoostGraphCreator::createFromRange(
 | 
|---|
| 109 |     const iterator &_begin,
 | 
|---|
| 110 |     const iterator &_end,
 | 
|---|
| 111 |     const size_t &_no_nodes,
 | 
|---|
| 112 |     const predicate_t &_pred
 | 
|---|
| 113 |     )
 | 
|---|
| 114 | {
 | 
|---|
| 115 |   graph = UndirectedGraph();
 | 
|---|
| 116 | 
 | 
|---|
| 117 |   // add vertices
 | 
|---|
| 118 |   for(iterator iter = _begin; iter != _end; ++iter) {
 | 
|---|
| 119 |     const atomId_t atomid = (*iter)->getId();
 | 
|---|
| 120 |     Vertex v = boost::add_vertex(atomid, graph);
 | 
|---|
| 121 |     const atomId_t vertexname = boost::get(boost::get(boost::vertex_name, graph), v);
 | 
|---|
| 122 |     const nodeId_t vertexindex = boost::get(boost::get(boost::vertex_index, graph), v);
 | 
|---|
| 123 |     LOG(2, "DEBUG: Adding node " << vertexindex << " associated to atom #" << vertexname);
 | 
|---|
| 124 |     ASSERT( vertexname == atomid,
 | 
|---|
| 125 |         "BoostGraphCreator::createFromRange() - atomid "+toString(atomid)
 | 
|---|
| 126 |         +" is not name of vertex "+toString(vertexname)+".");
 | 
|---|
| 127 |     atomids_nodeids.insert( std::make_pair(vertexname, vertexindex) );
 | 
|---|
| 128 |   }
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   // add edges
 | 
|---|
| 131 |   for(iterator iter = _begin; iter != _end; ++iter) {
 | 
|---|
| 132 |     LOG(2, "DEBUG: Looking at atom " << (*iter)->getId());
 | 
|---|
| 133 |     const BondList& ListOfBonds = (*iter)->getListOfBonds();
 | 
|---|
| 134 |     for(BondList::const_iterator bonditer = ListOfBonds.begin();
 | 
|---|
| 135 |         bonditer != ListOfBonds.end(); ++bonditer) {
 | 
|---|
| 136 |       LOG(2, "DEBUG: Looking at bond " << *(*bonditer));
 | 
|---|
| 137 |       const atomId_t leftid = (*bonditer)->leftatom->getId();
 | 
|---|
| 138 |       const nodeId_t leftnodeid = getNodeId(leftid);
 | 
|---|
| 139 |       const atomId_t rightid = (*bonditer)->rightatom->getId();
 | 
|---|
| 140 |       const nodeId_t rightnodeid = getNodeId(rightid);
 | 
|---|
| 141 |       // only pick each bond once and evaluate predicate
 | 
|---|
| 142 |       if ((leftid == (*iter)->getId())
 | 
|---|
| 143 |           && (_pred(**bonditer))) {
 | 
|---|
| 144 |         LOG(3, "DEBUG: ADDING edge " << leftnodeid << " <-> " << rightnodeid);
 | 
|---|
| 145 |         boost::add_edge(leftnodeid, rightnodeid, graph);
 | 
|---|
| 146 |       } else {
 | 
|---|
| 147 |         LOG(3, "DEBUG: Discarding edge " << leftnodeid << " <-> " << rightnodeid);
 | 
|---|
| 148 |       }
 | 
|---|
| 149 |     }
 | 
|---|
| 150 |   }
 | 
|---|
| 151 |   LOG(2, "DEBUG: We have " << getNumVertices() << " nodes and " << getNumEdges()
 | 
|---|
| 152 |       << " edges in the molecule graph.");
 | 
|---|
| 153 | }
 | 
|---|
| 154 | 
 | 
|---|
| 155 | 
 | 
|---|