[d24ef58] | 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 |
|
---|
| 42 | #include "CodePatterns/Assert.hpp"
|
---|
| 43 |
|
---|
| 44 | #include "Atom/atom.hpp"
|
---|
[d83d64] | 45 | #include "Graph/Graph6Reader.hpp"
|
---|
[d24ef58] | 46 | #include "molecule.hpp"
|
---|
| 47 |
|
---|
| 48 | void BoostGraphCreator::createFromMolecule(
|
---|
| 49 | const molecule &_mol,
|
---|
| 50 | const predicate_t &_pred)
|
---|
| 51 | {
|
---|
| 52 | createFromRange<molecule::const_iterator>(_mol.begin(), _mol.end(), _mol.getAtomCount(), _pred);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | static bool predicateAnd(
|
---|
| 56 | const BoostGraphCreator::predicate_t &_pred1,
|
---|
| 57 | const BoostGraphCreator::predicate_t &_pred2,
|
---|
| 58 | const bond &_bond)
|
---|
| 59 | { return (_pred1(_bond) && _pred2(_bond)); }
|
---|
| 60 |
|
---|
| 61 | static atomId_t getAtomId(
|
---|
| 62 | const atom *_atom
|
---|
| 63 | )
|
---|
| 64 | { return _atom->getId(); }
|
---|
| 65 |
|
---|
| 66 | static bool inSetPredicate(
|
---|
| 67 | const std::vector<atomId_t> &_atomids,
|
---|
| 68 | const bond &_bond)
|
---|
| 69 | {
|
---|
| 70 | const atomId_t leftid = _bond.leftatom->getId();
|
---|
| 71 | const atomId_t rightid = _bond.rightatom->getId();
|
---|
| 72 | return std::binary_search(_atomids.begin(), _atomids.end(), leftid)
|
---|
| 73 | && std::binary_search(_atomids.begin(), _atomids.end(), rightid);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | void BoostGraphCreator::createFromAtoms(
|
---|
| 77 | const std::vector<atom *> &_atoms,
|
---|
| 78 | const predicate_t &_pred)
|
---|
| 79 | {
|
---|
| 80 | // sort makes binary_search possible
|
---|
| 81 | std::vector<atomId_t> atomids;
|
---|
| 82 | std::transform(_atoms.begin(), _atoms.end(),
|
---|
| 83 | std::back_inserter(atomids), getAtomId);
|
---|
| 84 | ASSERT( _atoms.size() == atomids.size(),
|
---|
[bccbe9] | 85 | "BoostGraphCreator::createFromAtom() - atomids "
|
---|
| 86 | +toString(atomids.size())+" and atoms "+toString(_atoms.size())
|
---|
| 87 | +" differ in size?");
|
---|
[d24ef58] | 88 | std::sort(atomids.begin(), atomids.end());
|
---|
| 89 | const predicate_t predicate = boost::bind(inSetPredicate, boost::ref(atomids), _1);
|
---|
| 90 | createFromRange<std::vector<atom *>::const_iterator>(
|
---|
| 91 | const_cast<const std::vector<atom *> &>(_atoms).begin(),
|
---|
| 92 | const_cast<const std::vector<atom *> &>(_atoms).end(),
|
---|
| 93 | _atoms.size(),
|
---|
| 94 | boost::bind(predicateAnd, _pred, predicate, _1));
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[d83d64] | 97 | void BoostGraphCreator::createFromGraph6String(
|
---|
| 98 | const std::string &_graph_string)
|
---|
| 99 | {
|
---|
| 100 | Graph6Reader reader;
|
---|
| 101 | {
|
---|
| 102 | std::stringstream inputstream(_graph_string);
|
---|
| 103 | reader(inputstream);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | graph = UndirectedGraph();
|
---|
| 107 |
|
---|
| 108 | // add nodes
|
---|
| 109 | for(int numbers = 0; numbers < reader.get_num_nodes(); ++numbers) {
|
---|
| 110 | const atomId_t atomid = numbers;
|
---|
| 111 | Vertex v = boost::add_vertex(atomid, graph);
|
---|
| 112 | const atomId_t vertexname = boost::get(boost::get(boost::vertex_name, graph), v);
|
---|
| 113 | const nodeId_t vertexindex = boost::get(boost::get(boost::vertex_index, graph), v);
|
---|
| 114 | LOG(2, "DEBUG: Adding node " << vertexindex << " associated to atom #" << vertexname);
|
---|
| 115 | ASSERT( vertexname == atomid,
|
---|
| 116 | "BoostGraphCreator::createFromRange() - atomid "+toString(atomid)
|
---|
| 117 | +" is not name of vertex "+toString(vertexname)+".");
|
---|
| 118 | atomids_nodeids.insert( std::make_pair(vertexname, vertexindex) );
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | // add edges
|
---|
| 122 | const Graph6Reader::edges_t &edges = reader.get_edges();
|
---|
| 123 | for(Graph6Reader::edges_t::const_iterator iter = edges.begin();
|
---|
| 124 | iter != edges.end(); ++iter) {
|
---|
| 125 | // graph6 contains only upper triangle of adjacency matrix, hence add only once
|
---|
| 126 | const nodeId_t leftnodeid = iter->first;
|
---|
| 127 | const nodeId_t rightnodeid = iter->second;
|
---|
| 128 | boost::add_edge(leftnodeid, rightnodeid, graph);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[bccbe9] | 132 | BoostGraphCreator::nodeId_t BoostGraphCreator::getNodeId(
|
---|
| 133 | const atomId_t &_atomid) const
|
---|
| 134 | {
|
---|
| 135 | atomids_nodeids_t::const_iterator iter =
|
---|
| 136 | atomids_nodeids.find(_atomid);
|
---|
| 137 | return (iter == atomids_nodeids.end()) ? (nodeId_t)-1 : iter->second;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[e0b960] | 140 | BoostGraphCreator::Edge BoostGraphCreator::findEdge(const atomId_t &_firstid, const atomId_t &_secondid)
|
---|
| 141 | {
|
---|
| 142 | edge_iter i, end;
|
---|
| 143 | const name_map_t name_map = boost::get(boost::vertex_name, graph);
|
---|
| 144 | for (boost::tie(i, end) = boost::edges(graph); i != end; ++i) {
|
---|
| 145 | const BoostGraphCreator::Edge &e = *i;
|
---|
| 146 | const Vertex u = source(e, graph);
|
---|
| 147 | const Vertex v = target(e, graph);
|
---|
| 148 | const atomId_t atomid_u = boost::get(name_map, u);
|
---|
| 149 | const atomId_t atomid_v = boost::get(name_map, v);
|
---|
| 150 | if (atomid_u == _firstid) {
|
---|
| 151 | if (atomid_v == _secondid)
|
---|
| 152 | break;
|
---|
| 153 | } else if (atomid_u == _secondid) {
|
---|
| 154 | if (atomid_v == _firstid)
|
---|
| 155 | break;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | if (i != end) {
|
---|
| 159 | BoostGraphCreator::Edge e = *i;
|
---|
| 160 | return e;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | return BoostGraphCreator::Edge();
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | bool BoostGraphCreator::removeEdge(const atomId_t &_firstid, const atomId_t &_secondid)
|
---|
| 167 | {
|
---|
| 168 | // look through all edges
|
---|
| 169 | BoostGraphCreator::Edge e = findEdge(_firstid, _secondid);
|
---|
| 170 |
|
---|
| 171 | // edge was found
|
---|
| 172 | if (e != BoostGraphCreator::Edge()) {
|
---|
| 173 | const Vertex u = source(e, graph);
|
---|
| 174 | const Vertex v = target(e, graph);
|
---|
| 175 | if (DoLog(3)) {
|
---|
| 176 | const name_map_t name_map = boost::get(boost::vertex_name, graph);
|
---|
| 177 | LOG(3, "DEBUG: Found edge " << boost::get(name_map, u) << " <-> "
|
---|
| 178 | << boost::get(name_map, v) << ", removing.");
|
---|
| 179 | }
|
---|
| 180 | boost::remove_edge(e, graph);
|
---|
| 181 |
|
---|
| 182 | return true;
|
---|
| 183 | }
|
---|
| 184 | return false;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | bool BoostGraphCreator::addEdge(const atomId_t &_firstid, const atomId_t &_secondid)
|
---|
| 188 | {
|
---|
| 189 | // look through all edges
|
---|
| 190 | BoostGraphCreator::Edge e = findEdge(_firstid, _secondid);
|
---|
| 191 |
|
---|
| 192 | if (e != BoostGraphCreator::Edge()) {
|
---|
| 193 | return false;
|
---|
| 194 | } else {
|
---|
| 195 | const nodeId_t leftnodeid = getNodeId(_firstid);
|
---|
| 196 | const nodeId_t rightnodeid = getNodeId(_secondid);
|
---|
| 197 |
|
---|
| 198 | boost::add_edge(leftnodeid, rightnodeid, graph);
|
---|
| 199 |
|
---|
| 200 | return true;
|
---|
| 201 | }
|
---|
| 202 | }
|
---|