Source code for moldesign.molecules.bonds

# Copyright 2016 Autodesk Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

from . import toplevel

[docs]class Bond(object): """ A bond between two atoms. Args: a1 (Atom): First atom a2 (Atom): Second atom (the order of atoms doesn't matter) order (int): Order of the bond Notes: Comparisons and hashes involving bonds will return True if the atoms involved in the bonds are the same. Bond orders are not compared. These objects are used to represent and pass bond data only - they are not used for storage. Attributes: a1 (Atom): First atom in the bond; assigned so that ``self.a1.index < self.a2.index`` a2 (Atom): Second atom in the bond; assigned so that ``self.a2.index > self.a1.index`` order (int): bond order (can be ``None``); not used in comparisons """ def __init__(self, a1, a2, order=None): if a1.index > a2.index: a1, a2 = a2, a1 self.a1 = a1 self.a2 = a2 if order is None: try: self.order = self.a1.bond_graph[a2] except KeyError: self.order = None else: self.order = order def __eq__(self, other): return (self.a1 is other.a1) and (self.a2 is other.a2) def __hash__(self): """Has this object using the atoms involved in its bond""" return hash((self.a1, self.a2))
[docs] def to_json(self): return dict(a1=self.a1.index, a2=self.a2.index, order=self.order)
[docs] def partner(self, atom): """ Return this atom's *partner* in the bond -- i.e., the other atom in the bond Args: atom (mdt.Atom): return the atom that this one is bonded to Returns: mdt.Atom: the passed atom's partner Raises: ValueError: if the passed atom is not part of this bond """ if atom is self.a1: return self.a2 elif atom is self.a2: return self.a1 else: raise ValueError('%s is not part of this bond' % atom)
@property def name(self): """ str: name of the bond """ return '{} (#{a1.index}) - {} (#{a2.index}) (order: {order})'.format( a1=self.a1, a2=self.a2, order=self.order) @property def ff(self): """mdt.forcefield.BondTerm: the force-field term for this bond (or ``None`` if no forcefield is present) """ try: ff = self.a1.molecule.energy_model.get_forcefield() except (NotImplementedError, AttributeError): return None return ff.bond_term[self] def __repr__(self): try: return '<Bond: %s>'%str(self) except: print '<Bond @ %s (error in __repr__)>' % id(self) def __str__(self): return