Changeset 1c0b0b


Ignore:
Timestamp:
Oct 23, 2022, 9:08:55 AM (3 years ago)
Author:
Frederik Heber <frederik.heber@…>
Branches:
Candidate_v1.7.0, stable
Children:
894040
Parents:
0daef6
git-author:
Frederik Heber <frederik.heber@…> (10/19/22 19:38:02)
git-committer:
Frederik Heber <frederik.heber@…> (10/23/22 09:08:55)
Message:

Graph6Writer::write_elementlist uses BFS from boundary atom.

  • the elementlist is not stable as the set of atoms is arbitrary to any kind of permutation. However, the underlying bond graph is not, even though it may also have some symmetries.
  • Therefore, we use a BFS from a non-hydrogen atom on the boundary.
  • FIX: BoosGraphCreator gets vector of const atom*.
Location:
src/Graph
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/Graph/BoostGraphCreator.cpp

    r0daef6 r1c0b0b  
    7575
    7676void BoostGraphCreator::createFromAtoms(
    77     const std::vector<atom *> &_atoms,
     77    const std::vector<const atom *> &_atoms,
    7878    const predicate_t &_pred)
    7979{
     
    8888  std::sort(atomids.begin(), atomids.end());
    8989  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(),
     90  createFromRange<std::vector<const atom *>::const_iterator>(
     91      const_cast<const std::vector<const atom *> &>(_atoms).begin(),
     92      const_cast<const std::vector<const atom *> &>(_atoms).end(),
    9393      _atoms.size(),
    9494      boost::bind(predicateAnd, _pred, predicate, _1));
  • src/Graph/BoostGraphCreator.hpp

    r0daef6 r1c0b0b  
    8787   */
    8888  void createFromAtoms(
    89       const std::vector<atom *> &_atoms,
     89      const std::vector<const atom *> &_atoms,
    9090      const predicate_t &_pred);
    9191
  • src/Graph/Graph6Writer.cpp

    r0daef6 r1c0b0b  
    4444
    4545#include "Atom/atom.hpp"
     46#include "Descriptors/AtomIdDescriptor.hpp"
    4647#include "Element/element.hpp"
     48#include "Graph/BoostGraphCreator.hpp"
     49#include "Graph/BreadthFirstSearchGatherer.hpp"
     50#include "World.hpp"
    4751
    4852//#include "CodePatterns/MemDebug.hpp"
     
    116120  ASSERT( value==0,
    117121      "Graph6Writer::write_graph6() - byte is not null, i.e. chars left to write?");
    118   ASSERT( bytes_written == (int)ceil(n*(n-1)/12.0f),
     122  ASSERT( bytes_written == (unsigned int)ceil(n*(n-1)/12.0f),
    119123      "Graph6Writer::write_graph6() - unexpected number of bytes written");
    120124}
    121125
     126/**
     127 * Picks a non-hydrogen from all hydrogen atoms in the current set
     128 * of atoms.
     129 *
     130 * Returns -1 if none could be found.
     131 */
     132atomId_t Graph6Writer::getBoundaryNonHydrogen() const {
     133  atomId_t start_atom_id = -1;
     134  int bond_degree = 16;
     135  for(std::vector<const atom *>::const_iterator iter = _atoms.begin();
     136      iter != _atoms.end(); ++iter) {
     137    const atom *walker = *iter;
     138    if (walker->getElement().getSymbol() == "H") {
     139      const BondList& bond_list = walker->getListOfBonds();
     140      assert(bond_list.size() == 1);
     141      const atom *other = bond_list.front().get()->GetOtherAtom(walker);
     142      const int number_of_bonds = other->getListOfBonds().size();
     143      if ((other->getElement().getSymbol() != "H") && (bond_degree > number_of_bonds)) {
     144        start_atom_id = other->getId();
     145        bond_degree = number_of_bonds;
     146      }
     147    }
     148  }
     149  return start_atom_id;
     150}
     151
     152bool OnlyNonHydrogens(const bond &_bond) {
     153  return _bond.HydrogenBond == 0;
     154}
     155
    122156void Graph6Writer::write_elementlist(std::ostream& out) {
    123   for (std::vector<const atom *>::const_iterator iter = _atoms.begin();
    124       iter != _atoms.end(); ++iter) {
    125     if (iter != _atoms.begin())
     157  /** Execute a Breadth-First Search discovery from one terminal atom (e.g.,
     158   * pick random hydrogen and then it's bond-neighbor if it is non-hydrogen).
     159   * Then return the element list in that ordering.
     160   *
     161   * The graph6 string does not account for the inherent graph symmetries
     162   * (e.g., BW having 123<->321 but not 123<->132 symmetry).
     163   */
     164  // pick bond neighbor of a hydrogen atom
     165  atomId_t start_atom_id = getBoundaryNonHydrogen();
     166  if (start_atom_id == (unsigned int)-1) {
     167    // fall back to first atom in list
     168    start_atom_id = _atoms.front()->getId();
     169  }
     170
     171  // do an unlimited BFS and get set of nodes, ordered by discovery level
     172  BoostGraphCreator graphCreator;
     173  graphCreator.createFromAtoms(_atoms, OnlyNonHydrogens);
     174  BreadthFirstSearchGatherer gatherer(graphCreator);
     175  const std::vector<atomId_t> gathered_atoms = gatherer(start_atom_id);
     176
     177  // print all nodes
     178  const World& world = World::getConstInstance();
     179  for (std::vector<atomId_t>::const_iterator iter = gathered_atoms.begin();
     180      iter != gathered_atoms.end(); ++iter) {
     181    if (iter != gathered_atoms.begin())
    126182      out << ' ';
    127     out << (*iter)->getElement().getSymbol();
     183    const atom* walker = world.getAtom(AtomById(*iter));
     184    assert(walker != NULL);
     185    out << walker->getElement().getSymbol();
    128186  }
    129187}
  • src/Graph/Graph6Writer.hpp

    r0daef6 r1c0b0b  
    1717#include <iosfwd>
    1818#include <vector>
     19
     20#include "types.hpp"
    1921
    2022class atom;
     
    4143
    4244   void write_elementlist(std::ostream& out);
     45
     46private:
     47   atomId_t getBoundaryNonHydrogen() const;
    4348};
    4449
  • src/Graph/unittests/BoostGraphCreatorUnitTest.cpp

    r0daef6 r1c0b0b  
    164164void BoostGraphCreatorTest::createFromAtomsTest()
    165165{
    166   std::vector<atom *> atoms;
     166  std::vector<const atom *> atoms;
    167167  std::copy(TestMolecule->begin(), TestMolecule->end(), std::back_inserter(atoms));
    168168  BGCreator->createFromAtoms(atoms, BreadthFirstSearchGatherer::AlwaysTruePredicate);
Note: See TracChangeset for help on using the changeset viewer.