Completed
Push — master ( ea74a1...6ddd48 )
by Rich
04:03
created

Bond.index()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
crap 1
1
#! /usr/bin/env python
2
#
3
# Copyright (C) 2015-2016 Rich Lewis <[email protected]>
4
# License: 3-clause BSD
5
6 1
"""
7
## skchem.core.bond
8
9
Defining chemical bonds in scikit-chem.
10
"""
11
12 1
from itertools import chain, combinations
13
14 1
import pandas as pd
0 ignored issues
show
Configuration introduced by
The import pandas could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
15 1
import rdkit.Chem
0 ignored issues
show
Configuration introduced by
The import rdkit.Chem could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
16 1
import numpy as np
0 ignored issues
show
Configuration introduced by
The import numpy could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
17
18 1
from .atom import Atom
19 1
from .base import ChemicalObject, PropertyView, ChemicalObjectView
20
21
22 1
class Bond(rdkit.Chem.rdchem.Bond, ChemicalObject):
23
24
    """
25
    Class representing a chemical bond in scikit-chem.
26
27
    """
28
29 1
    @property
30
    def index(self):
31
32
        """ int: the index of the bond in the atom. """
33
34 1
        return self.GetIdx()
0 ignored issues
show
Bug introduced by
The Instance of Bond does not seem to have a member named GetIdx.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
35
36 1
    @property
37
    def atoms(self):
38
39
        """ tuple[Atom]: list of atoms involved in the bond. """
40
41 1
        return (Atom.from_super(self.GetBeginAtom()),
0 ignored issues
show
Bug introduced by
The Instance of Bond does not seem to have a member named GetBeginAtom.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
42
                Atom.from_super(self.GetEndAtom()))
0 ignored issues
show
Bug introduced by
The Instance of Bond does not seem to have a member named GetEndAtom.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
43
44 1
    @property
45
    def atom_idxs(self):
46
47
        """ tuple[int]: list of atom indexes involved in the bond. """
48
49 1
        return (self.GetBeginAtomIdx(), self.GetEndAtomIdx())
0 ignored issues
show
Bug introduced by
The Instance of Bond does not seem to have a member named GetBeginAtomIdx.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
Bug introduced by
The Instance of Bond does not seem to have a member named GetEndAtomIdx.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
50
51 1
    @property
52
    def props(self):
53
54
        """ PropertyView: rdkit properties of the atom. """
55
56 1
        if not hasattr(self, '_props'):
57 1
            self._props = PropertyView(self)
0 ignored issues
show
Coding Style introduced by
The attribute _props was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
58 1
        return PropertyView(self)
59
60 1
    @property
61
    def order(self):
62
63
        """ int: the order of the bond. """
64
65 1
        return self.GetBondTypeAsDouble()
0 ignored issues
show
Bug introduced by
The Instance of Bond does not seem to have a member named GetBondTypeAsDouble.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
66
67 1
    @property
68
    def is_aromatic(self):
69
70
        """ bool: whether the bond is aromatic. """
71
72 1
        return self.GetIsAromatic()
0 ignored issues
show
Bug introduced by
The Instance of Bond does not seem to have a member named GetIsAromatic.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
73
74 1
    @property
75
    def is_conjugated(self):
76
77
        """ bool: whether the bond is conjugated. """
78
79 1
        return self.GetIsConjugated()
0 ignored issues
show
Bug introduced by
The Instance of Bond does not seem to have a member named GetIsConjugated.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
80
81 1
    @property
82
    def owner(self):
83
84
        """ skchem.Mol: the molecule this bond is a part of. """
85
86 1
        from .mol import Mol
87 1
        return Mol.from_super(self.GetOwningMol())
0 ignored issues
show
Bug introduced by
The Instance of Bond does not seem to have a member named GetOwningMol.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
88
89 1
    @property
90
    def stereo_symbol(self):
91
92
        """ str: the stereo label of the bond ('Z', 'E', 'ANY', 'NONE') """
93
94 1
        return self.GetStereo().name.lstrip('STEREO')
0 ignored issues
show
Bug introduced by
The Instance of Bond does not seem to have a member named GetStereo.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
95
96 1
    @property
97
    def is_in_ring(self):
98
99
        """ bool: whether the bond is in a ring. """
100
101 1
        return self.IsInRing()
0 ignored issues
show
Bug introduced by
The Instance of Bond does not seem to have a member named IsInRing.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
102
103 1
    def draw(self):
104
105
        """ str: Draw the bond in ascii. """
106
107 1
        return '{}{}{}'.format(self.atoms[0].symbol,
108
                               '-' if self.order == 1 else self.GetSmarts(),
0 ignored issues
show
Bug introduced by
The Instance of Bond does not seem to have a member named GetSmarts.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
109
                               self.atoms[1].symbol)
110
111 1
    def to_dict(self):
112
113
        """ dict: Convert to a dictionary representation. """
114
115 1
        return {"b": self.GetBeginAtomIdx(),
0 ignored issues
show
Bug introduced by
The Instance of Bond does not seem to have a member named GetBeginAtomIdx.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
116
                "e": self.GetEndAtomIdx(),
0 ignored issues
show
Bug introduced by
The Instance of Bond does not seem to have a member named GetEndAtomIdx.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
117
                "o": self.order}
118
119 1
    def __repr__(self):
120 1
        return '<{klass} type="{bond}" at {address}>'.format(
121
            klass=self.__class__.__name__,
122
            bond=self.draw(),
123
            address=hex(id(self)))
124
125 1
    def __str__(self):
126 1
        return self.draw()
127
128
129 1
class BondView(ChemicalObjectView):
130
131
    """ Bond interface wrapper """
132 1
    def __getitem__(self, index):
133 1
        res = super(BondView, self).__getitem__(index)
134 1
        if res is None:
135 1
            if abs(index) >= len(self):
136 1
                raise IndexError('Index {} out of range for molecule with '
137
                                 '{} bonds.'.format(index, len(self)))
138
139
            # index is negative, so adding gives desired indexing from back
140 1
            if index < 0:
141 1
                index += len(self)
142
143 1
            return Bond.from_super(self.owner.GetBondWithIdx(index))
144
145
        else:
146 1
            return res
147
148 1
    def __len__(self):
149 1
        return self.owner.GetNumBonds()
150
151 1
    @property
152
    def atom_idxs(self):
153
154
        """ The atom indices for the bonds in the view. """
155
156 1
        return np.array([atom.atom_idxs for atom in self])
157
158 1
    @property
159
    def order(self):
160
161
        """ np.array<int> the bond orders of the bonds in the view. """
162
163 1
        return np.array([bond.order for bond in self])
164
165 1
    @property
166
    def is_aromatic(self):
167
168
        """ np.array<bool> whether each of the bonds in the view are
169
        aromatic. """
170
171 1
        return np.array([bond.is_aromatic for bond in self])
172
173 1
    @property
174
    def is_conjugated(self):
175
176
        """ np.array<bool> whether each of the bonds in the view are c
177
        onjugated. """
178
179 1
        return np.array([bond.is_conjugated for bond in self])
180
181 1
    @property
182
    def is_in_ring(self):
183
184
        """ np.array<bool> whether each of the bonds in the view are in a
185
        ring. """
186
187 1
        return np.array([bond.is_in_ring for bond in self])
188
189 1
    @property
190
    def stereo_symbol(self):
191
192
        """ np.array<str> the stereo symbol of the bonds in the view. """
193
194 1
        return np.array([bond.stereo_symbol for bond in self])
195
196 1
    @property
197
    def index(self):
198
199
        """ pd.Index: an index of the bonds in the `BondView`. """
200
201 1
        return pd.RangeIndex(len(self), name='bond_idx')
202
203 1
    def adjacency_matrix(self):
204
205
        """ np.array[int]: the bond adjacency matrix. """
206
207 1
        res = np.zeros((len(self), len(self)))
208 1
        ixs = zip(*chain(*(combinations((bond.index for bond in atom.bonds), 2)
209
                           for atom in self.owner.atoms)))
210 1
        res[tuple(ixs)] = 1
211 1
        res += res.T
212 1
        return res.astype(int)
213
214
__all__ = ['Atom', 'AtomView']
0 ignored issues
show
Bug introduced by
Undefined variable name 'AtomView' in __all__
Loading history...
215