-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathmolecule.py
More file actions
3006 lines (2631 loc) · 120 KB
/
molecule.py
File metadata and controls
3006 lines (2631 loc) · 120 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
###############################################################################
# #
# RMG - Reaction Mechanism Generator #
# #
# Copyright (c) 2002-2023 Prof. William H. Green (whgreen@mit.edu), #
# Prof. Richard H. West (r.west@neu.edu) and the RMG Team (rmg_dev@mit.edu) #
# #
# Permission is hereby granted, free of charge, to any person obtaining a #
# copy of this software and associated documentation files (the 'Software'), #
# to deal in the Software without restriction, including without limitation #
# the rights to use, copy, modify, merge, publish, distribute, sublicense, #
# and/or sell copies of the Software, and to permit persons to whom the #
# Software is furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in #
# all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
# DEALINGS IN THE SOFTWARE. #
# #
###############################################################################
"""
This module provides classes and methods for working with molecules and
molecular configurations. A molecule is represented internally using a graph
data type, where atoms correspond to vertices and bonds correspond to edges.
Both :class:`Atom` and :class:`Bond` objects store semantic information that
describe the corresponding atom or bond.
"""
import itertools
import logging
import os
from collections import OrderedDict, defaultdict
from copy import deepcopy
from urllib.parse import quote
from operator import attrgetter
import cython
import numpy as np
import rmgpy.constants as constants
import rmgpy.molecule.converter as converter
import rmgpy.molecule.element as elements
import rmgpy.molecule.group as gr
import rmgpy.molecule.resonance as resonance
import rmgpy.molecule.translator as translator
from rmgpy.exceptions import DependencyError
from rmgpy.molecule.adjlist import Saturator
from rmgpy.molecule.atomtype import AtomType, ATOMTYPES, get_atomtype, AtomTypeError
from rmgpy.molecule.element import bdes
from rmgpy.molecule.graph import Vertex, Edge, Graph, get_vertex_connectivity_value
from rmgpy.molecule.kekulize import kekulize
from rmgpy.molecule.pathfinder import find_shortest_path
from rmgpy.molecule.fragment import CuttingLabel
################################################################################
# helper function for sorting
def _skip_first(in_tuple):
return in_tuple[1:]
bond_orders = {'S': 1, 'D': 2, 'T': 3, 'B': 1.5}
globals().update({
'bond_orders': bond_orders,
})
class Atom(Vertex):
"""
An atom. The attributes are:
==================== =================== ====================================
Attribute Type Description
==================== =================== ====================================
`atomtype` :class:`AtomType` The :ref:`atom type <atom-types>`
`element` :class:`Element` The chemical element the atom represents
`radical_electrons` ``short`` The number of radical electrons
`charge` ``short`` The formal charge of the atom
`label` ``str`` A string label that can be used to tag individual atoms
`coords` ``numpy array`` The (x,y,z) coordinates in Angstrom
`lone_pairs` ``short`` The number of lone electron pairs
`id` ``int`` Number assignment for atom tracking purposes
`bonds` ``dict`` Dictionary of bond objects with keys being neighboring atoms
`props` ``dict`` Dictionary for storing additional atom properties
`mass` ``int`` atomic mass of element (read only)
`number` ``int`` atomic number of element (read only)
`symbol` ``str`` atomic symbol of element (read only)
`site` ``str`` type of adsorption site
`morphology` ``str`` morphology of the adsorption site
==================== =================== ====================================
Additionally, the ``mass``, ``number``, and ``symbol`` attributes of the
atom's element can be read (but not written) directly from the atom object,
e.g. ``atom.symbol`` instead of ``atom.element.symbol``.
"""
def __init__(self, element=None, radical_electrons=0, charge=0, label='', lone_pairs=-100, site='', morphology='',
coords=np.array([]), id=-1, props=None):
Vertex.__init__(self)
if isinstance(element, str):
self.element = elements.__dict__[element]
else:
self.element = element
self.radical_electrons = radical_electrons
self.charge = charge
self.label = label
self.atomtype = None
self.lone_pairs = lone_pairs
self.site = site
self.morphology = morphology
self.coords = coords
self.id = id
self.props = props or {}
def __str__(self):
"""
Return a human-readable string representation of the object.
"""
return '{0}{1}{2}'.format(
str(self.element),
'.' * self.radical_electrons,
'+' * self.charge if self.charge > 0 else '-' * -self.charge,
)
def __repr__(self):
"""
Return a representation that can be used to reconstruct the object.
"""
return "<Atom '{0}'>".format(str(self))
def __reduce__(self):
"""
A helper function used when pickling an object.
"""
d = {
'edges': self.edges,
'connectivity1': self.connectivity1,
'connectivity2': self.connectivity2,
'connectivity3': self.connectivity3,
'sorting_label': self.sorting_label,
'atomtype': self.atomtype.label if self.atomtype else None,
'lone_pairs': self.lone_pairs,
'site': self.site,
'morphology': self.morphology,
}
if self.element.isotope == -1:
element2pickle = self.element.symbol
else:
element2pickle = self.element
return (Atom, (element2pickle, self.radical_electrons, self.charge, self.label), d)
def __setstate__(self, d):
"""
A helper function used when unpickling an object.
"""
self.edges = d['edges']
self.connectivity1 = d['connectivity1']
self.connectivity2 = d['connectivity2']
self.connectivity3 = d['connectivity3']
self.sorting_label = d['sorting_label']
self.atomtype = ATOMTYPES[d['atomtype']] if d['atomtype'] else None
self.lone_pairs = d['lone_pairs']
self.site = d['site']
self.morphology = d['morphology']
def __hash__(self):
"""
Define a custom hash method to allow Atom objects to be used in dictionaries and sets.
"""
return hash(('Atom', self.symbol))
def __eq__(self, other):
"""Method to test equality of two Atom objects."""
return self is other
def __lt__(self, other):
"""Define less than comparison. For comparing against other Atom objects (e.g. when sorting)."""
if issubclass(type(other), Vertex):
return self.sorting_key < other.sorting_key
else:
raise NotImplementedError('Cannot perform less than comparison between Atom and '
'{0}.'.format(type(other).__name__))
def __gt__(self, other):
"""Define greater than comparison. For comparing against other Atom objects (e.g. when sorting)."""
if issubclass(type(other), Vertex):
return self.sorting_key > other.sorting_key
else:
raise NotImplementedError('Cannot perform greater than comparison between Atom and '
'{0}.'.format(type(other).__name__))
@property
def mass(self):
return self.element.mass
@property
def number(self):
return self.element.number
@property
def symbol(self):
return self.element.symbol
@property
def bonds(self):
return self.edges
@property
def sorting_key(self):
"""Returns a sorting key for comparing Atom objects. Read-only"""
return self.number, -get_vertex_connectivity_value(self), self.radical_electrons, self.lone_pairs, self.charge
def equivalent(self, other, strict=True):
"""
Return ``True`` if `other` is indistinguishable from this atom, or
``False`` otherwise. If `other` is an :class:`Atom` object, then all
attributes except `label` and 'ID' must match exactly. If `other` is an
:class:`GroupAtom` object, then the atom must match any of the
combinations in the atom pattern. If ``strict`` is ``False``, then only
the element is compared and electrons are ignored.
"""
cython.declare(atom=Atom, ap=gr.GroupAtom)
if isinstance(other, Atom):
atom = other
if strict:
return (self.element is atom.element
and self.radical_electrons == atom.radical_electrons
and self.lone_pairs == atom.lone_pairs
and self.charge == atom.charge
and self.atomtype is atom.atomtype
and self.site == atom.site
and self.morphology == atom.morphology)
else:
return self.element is atom.element
elif isinstance(other, gr.GroupAtom):
cython.declare(a=AtomType, radical=cython.short, lp=cython.short, charge=cython.short)
if not strict:
raise NotImplementedError('There is currently no implementation of '
'the strict argument for Group objects.')
ap = other
for a in ap.atomtype:
if self.atomtype.equivalent(a): break
else:
return False
if ap.radical_electrons:
for radical in ap.radical_electrons:
if self.radical_electrons == radical: break
else:
return False
if ap.lone_pairs:
for lp in ap.lone_pairs:
if self.lone_pairs == lp: break
else:
return False
if ap.charge:
for charge in ap.charge:
if self.charge == charge: break
else:
return False
if ap.site:
for site in ap.site:
if self.site == site: break
else:
return False
if ap.morphology:
for morphology in ap.morphology:
if self.morphology == morphology: break
else:
return False
if 'inRing' in self.props and 'inRing' in ap.props:
if self.props['inRing'] != ap.props['inRing']:
return False
return True
def is_specific_case_of(self, other):
"""
Return ``True`` if `self` is a specific case of `other`, or ``False``
otherwise. If `other` is an :class:`Atom` object, then this is the same
as the :meth:`equivalent()` method. If `other` is an
:class:`GroupAtom` object, then the atom must match or be more
specific than any of the combinations in the atom pattern.
"""
if isinstance(other, Atom):
return self.equivalent(other)
elif isinstance(other, gr.GroupAtom):
cython.declare(atom=gr.GroupAtom, a=AtomType, radical=cython.short, lp=cython.short, charge=cython.short)
atom = other
if self.atomtype is None:
return False
for a in atom.atomtype:
if self.atomtype.is_specific_case_of(a):
break
else:
return False
if atom.radical_electrons:
for radical in atom.radical_electrons:
if self.radical_electrons == radical:
break
else:
return False
if atom.lone_pairs:
for lp in atom.lone_pairs:
if self.lone_pairs == lp:
break
else:
return False
if atom.charge:
for charge in atom.charge:
if self.charge == charge:
break
else:
return False
if atom.site:
for site in atom.site:
if self.site == site: break
else:
return False
if atom.morphology:
for morphology in atom.morphology:
if self.morphology == morphology: break
else:
return False
if 'inRing' in self.props and 'inRing' in atom.props:
if self.props['inRing'] != atom.props['inRing']:
return False
elif 'inRing' not in self.props and 'inRing' in atom.props:
return False
return True
def copy(self):
"""
Generate a deep copy of the current atom. Modifying the
attributes of the copy will not affect the original.
"""
cython.declare(a=Atom)
# a = Atom(self.element, self.radical_electrons, self.spin_multiplicity, self.charge, self.label)
a = Atom.__new__(Atom)
a.edges = {}
a.reset_connectivity_values()
a.element = self.element
a.radical_electrons = self.radical_electrons
a.charge = self.charge
a.label = self.label
a.atomtype = self.atomtype
a.lone_pairs = self.lone_pairs
a.site = self.site
a.morphology = self.morphology
a.coords = self.coords[:]
a.id = self.id
a.props = deepcopy(self.props)
return a
def is_electron(self):
"""
Return ``True`` if the atom represents an electron or ``False`` if
not.
"""
return self.element.number == -1
def is_proton(self):
"""
Return ``True`` if the atom represents a proton or ``False`` if
not.
"""
if self.element.number == 1 and self.charge == 1:
return True
return False
def is_hydrogen(self):
"""
Return ``True`` if the atom represents a hydrogen atom or ``False`` if
not.
"""
return self.element.number == 1
def is_non_hydrogen(self):
"""
Return ``True`` if the atom does not represent a hydrogen atom or
``False`` if it does.
"""
return self.element.number != 1
def is_halogen(self):
"""
Return ``True`` if the atom represents a halogen atom (F, Cl, Br, I)
``False`` if it does.
"""
return self.element.number in [9, 17, 35, 53]
def is_carbon(self):
"""
Return ``True`` if the atom represents a carbon atom or ``False`` if
not.
"""
return self.element.number == 6
def is_lithium(self):
"""
Return ``True`` if the atom represents a hydrogen atom or ``False`` if
not.
"""
return self.element.number == 3
def is_nitrogen(self):
"""
Return ``True`` if the atom represents a nitrogen atom or ``False`` if
not.
"""
return self.element.number == 7
def is_oxygen(self):
"""
Return ``True`` if the atom represents an oxygen atom or ``False`` if
not.
"""
return self.element.number == 8
def is_fluorine(self):
"""
Return ``True`` if the atom represents a fluorine atom or ``False`` if
not.
"""
return self.element.number == 9
def is_surface_site(self):
"""
Return ``True`` if the atom represents a surface site or ``False`` if not.
"""
return self.symbol == 'X'
def is_bonded_to_surface(self):
"""
Return ``True`` if the atom is bonded to a surface atom `X`
``False`` if it is not
"""
cython.declare(bonded_atom=Atom)
for bonded_atom in self.bonds.keys():
if bonded_atom.is_surface_site():
return True
return False
def is_bonded_to_halogen(self):
"""
Return ``True`` if the atom is bonded to at least one halogen (F, Cl, Br, or I)
``False`` if it is not
"""
cython.declare(bonded_atom=Atom)
for bonded_atom in self.bonds.keys():
if bonded_atom.is_halogen():
return True
return False
def is_silicon(self):
"""
Return ``True`` if the atom represents a silicon atom or ``False`` if
not.
"""
return self.element.number == 14
def is_phosphorus(self):
"""
Return ``True`` if the atom represents a phosphorus atom or ``False`` if
not.
"""
return self.element.number == 15
def is_sulfur(self):
"""
Return ``True`` if the atom represents a sulfur atom or ``False`` if
not.
"""
return self.element.number == 16
def is_chlorine(self):
"""
Return ``True`` if the atom represents a chlorine atom or ``False`` if
not.
"""
return self.element.number == 17
def is_bromine(self):
"""
Return ``True`` if the atom represents a bromine atom or ``False`` if
not.
"""
return self.element.number == 35
def is_iodine(self):
"""
Return ``True`` if the atom represents an iodine atom or ``False`` if
not.
"""
return self.element.number == 53
def is_nos(self):
"""
Return ``True`` if the atom represent either nitrogen, sulfur, or oxygen
``False`` if it does not.
"""
return self.element.number in [7, 8, 16]
def increment_radical(self):
"""
Update the atom pattern as a result of applying a GAIN_RADICAL action,
where `radical` specifies the number of radical electrons to add.
"""
# Set the new radical electron count
self.radical_electrons += 1
if self.radical_electrons <= 0:
raise gr.ActionError('Unable to update Atom due to GAIN_RADICAL action: '
'Invalid radical electron set "{0}".'.format(self.radical_electrons))
def decrement_radical(self):
"""
Update the atom pattern as a result of applying a LOSE_RADICAL action,
where `radical` specifies the number of radical electrons to remove.
"""
cython.declare(radical_electrons=cython.short)
# Set the new radical electron count
radical_electrons = self.radical_electrons = self.radical_electrons - 1
if radical_electrons < 0:
raise gr.ActionError('Unable to update Atom due to LOSE_RADICAL action: '
'Invalid radical electron set "{0}".'.format(self.radical_electrons))
def increment_charge(self):
"""
Update the atom pattern as a result of applying a GAIN_CHARGE action
"""
self.charge += 1
def decrement_charge(self):
"""
Update the atom pattern as a result of applying a LOSE_CHARGE action
"""
self.charge -= 1
def set_lone_pairs(self, lone_pairs):
"""
Set the number of lone electron pairs.
"""
# Set the number of electron pairs
self.lone_pairs = lone_pairs
if self.lone_pairs < 0:
raise gr.ActionError('Unable to update Atom due to set_lone_pairs: '
'Invalid lone electron pairs set "{0}".'.format(self.set_lone_pairs))
self.update_charge()
def increment_lone_pairs(self):
"""
Update the lone electron pairs pattern as a result of applying a GAIN_PAIR action.
"""
# Set the new lone electron pairs count
self.lone_pairs += 1
if self.lone_pairs <= 0:
raise gr.ActionError('Unable to update Atom due to GAIN_PAIR action: '
'Invalid lone electron pairs set "{0}".'.format(self.lone_pairs))
self.update_charge()
def decrement_lone_pairs(self):
"""
Update the lone electron pairs pattern as a result of applying a LOSE_PAIR action.
"""
# Set the new lone electron pairs count
self.lone_pairs -= 1
if self.lone_pairs < 0:
raise gr.ActionError('Unable to update Atom due to LOSE_PAIR action: '
'Invalid lone electron pairs set "{0}".'.format(self.lone_pairs))
self.update_charge()
def update_charge(self):
"""
Update self.charge, according to the valence, and the
number and types of bonds, radicals, and lone pairs.
"""
if self.is_surface_site():
self.charge = 0
return
if self.is_electron():
self.charge = -1
return
valence_electron = elements.PeriodicSystem.valence_electrons[self.symbol]
order = self.get_total_bond_order()
self.charge = valence_electron - order - self.radical_electrons - 2 * self.lone_pairs
def apply_action(self, action):
"""
Update the atom pattern as a result of applying `action`, a tuple
containing the name of the reaction recipe action along with any
required parameters. The available actions can be found
:ref:`here <reaction-recipe-actions>`.
"""
# Invalidate current atom type
self.atomtype = None
act = action[0].upper()
# Modify attributes if necessary
if act in ['CHANGE_BOND', 'FORM_BOND', 'BREAK_BOND']:
# Nothing else to do here
pass
elif act == 'GAIN_RADICAL':
for i in range(action[2]): self.increment_radical()
elif act == 'LOSE_RADICAL':
for i in range(abs(action[2])): self.decrement_radical()
elif act == 'GAIN_CHARGE':
for i in range(action[2]): self.increment_charge()
elif act == 'LOSE_CHARGE':
for i in range(abs(action[2])): self.decrement_charge()
elif action[0].upper() == 'GAIN_PAIR':
for i in range(action[2]): self.increment_lone_pairs()
elif action[0].upper() == 'LOSE_PAIR':
for i in range(abs(action[2])): self.decrement_lone_pairs()
else:
raise gr.ActionError('Unable to update Atom: Invalid action {0}".'.format(action))
def get_total_bond_order(self):
"""
This helper function is to help calculate total bond orders for an
input atom.
Some special consideration for the order `B` bond. For atoms having
three `B` bonds, the order for each is 4/3.0, while for atoms having other
than three `B` bonds, the order for each is 3/2.0
"""
num_b_bond = 0
order = 0
for bond in self.bonds.values():
if bond.is_benzene():
num_b_bond += 1
else:
order += bond.order
if num_b_bond == 3:
order += num_b_bond * 4 / 3.0
else:
order += num_b_bond * 3 / 2.0
return order
################################################################################
class Bond(Edge):
"""
A chemical bond. The attributes are:
=================== =================== ====================================
Attribute Type Description
=================== =================== ====================================
`order` ``float`` The :ref:`bond type <bond-types>`
`atom1` ``Atom`` An Atom object connecting to the bond
`atom2` ``Atom`` An Atom object connecting to the bond
=================== =================== ====================================
"""
def __init__(self, atom1, atom2, order=1):
Edge.__init__(self, atom1, atom2)
if isinstance(order, str):
self.set_order_str(order)
else:
self.order = order
def __str__(self):
"""
Return a human-readable string representation of the object.
"""
return self.get_order_str()
def __repr__(self):
"""
Return a representation that can be used to reconstruct the object.
"""
return '<Bond "{0}">'.format(self.order)
def __reduce__(self):
"""
A helper function used when pickling an object.
"""
return (Bond, (self.vertex1, self.vertex2, self.order))
def __hash__(self):
"""
Define a custom hash method to allow Bond objects to be used in dictionaries and sets.
"""
return hash(('Bond', self.order,
self.atom1.symbol if self.atom1 is not None else '',
self.atom2.symbol if self.atom2 is not None else ''))
def __eq__(self, other):
"""Method to test equality of two Bond objects."""
return self is other
def __lt__(self, other):
"""Define less than comparison. For comparing against other Bond objects (e.g. when sorting)."""
if isinstance(other, Bond):
return self.sorting_key < other.sorting_key
else:
raise NotImplementedError('Cannot perform less than comparison between Bond and '
'{0}.'.format(type(other).__name__))
def __gt__(self, other):
"""Define greater than comparison. For comparing against other Bond objects (e.g. when sorting)."""
if isinstance(other, Bond):
return self.sorting_key > other.sorting_key
else:
raise NotImplementedError('Cannot perform greater than comparison between Bond and '
'{0}.'.format(type(other).__name__))
@property
def atom1(self):
return self.vertex1
@property
def atom2(self):
return self.vertex2
@property
def sorting_key(self):
"""Returns a sorting key for comparing Bond objects. Read-only"""
return (self.order,
self.atom1.number if self.atom1 is not None else 0,
self.atom2.number if self.atom2 is not None else 0)
def get_bde(self):
"""
estimate the bond dissociation energy in J/mol of the bond based on the order of the bond
and the atoms involved in the bond
"""
try:
return bdes[(self.atom1.element.symbol, self.atom2.element.symbol, self.order)]
except KeyError:
raise KeyError('Bond Dissociation energy not known for combination: '
'({0},{1},{2})'.format(self.atom1.element.symbol, self.atom2.element.symbol, self.order))
def equivalent(self, other):
"""
Return ``True`` if `other` is indistinguishable from this bond, or
``False`` otherwise. `other` can be either a :class:`Bond` or a
:class:`GroupBond` object.
"""
cython.declare(bond=Bond, bp=gr.GroupBond)
if isinstance(other, Bond):
bond = other
return self.is_order(bond.get_order_num())
elif isinstance(other, gr.GroupBond):
bp = other
return any([self.is_order(otherOrder) for otherOrder in bp.get_order_num()])
def is_specific_case_of(self, other):
"""
Return ``True`` if `self` is a specific case of `other`, or ``False``
otherwise. `other` can be either a :class:`Bond` or a
:class:`GroupBond` object.
"""
# There are no generic bond types, so is_specific_case_of is the same as equivalent
return self.equivalent(other)
def get_order_str(self):
"""
returns a string representing the bond order
"""
if self.is_single():
return 'S'
elif self.is_benzene():
return 'B'
elif self.is_double():
return 'D'
elif self.is_triple():
return 'T'
elif self.is_quadruple():
return 'Q'
elif self.is_van_der_waals():
return 'vdW'
elif self.is_hydrogen_bond():
return 'H'
elif self.is_reaction_bond():
return 'R'
else:
raise ValueError("Bond order {} does not have string representation.".format(self.order))
def set_order_str(self, new_order):
"""
set the bond order using a valid bond-order character
"""
if new_order == 'S':
self.order = 1
elif new_order == 'D':
self.order = 2
elif new_order == 'T':
self.order = 3
elif new_order == 'B':
self.order = 1.5
elif new_order == 'Q':
self.order = 4
elif new_order == 'vdW':
self.order = 0
elif new_order == 'H':
self.order = 0.1
elif new_order == 'R':
self.order = 0.05
else:
# try to see if an float disguised as a string was input by mistake
try:
self.order = float(new_order)
except ValueError:
raise TypeError('Bond order {} is not hardcoded into this method'.format(new_order))
def get_order_num(self):
"""
returns the bond order as a number
"""
return self.order
def set_order_num(self, new_order):
"""
change the bond order with a number
"""
self.order = new_order
def copy(self):
"""
Generate a deep copy of the current bond. Modifying the
attributes of the copy will not affect the original.
"""
# return Bond(self.vertex1, self.vertex2, self.order)
cython.declare(b=Bond)
b = Bond.__new__(Bond)
b.vertex1 = self.vertex1
b.vertex2 = self.vertex2
b.order = self.order
return b
def is_van_der_waals(self):
"""
Return ``True`` if the bond represents a van der Waals bond or
``False`` if not.
"""
return self.is_order(0)
def is_order(self, other_order):
"""
Return ``True`` if the bond is of order other_order or ``False`` if
not. This compares floats that takes into account floating point error
NOTE: we can replace the absolute value relation with math.isclose when
we swtich to python 3.5+
"""
return abs(self.order - other_order) <= 1e-4
def is_single(self):
"""
Return ``True`` if the bond represents a single bond or ``False`` if
not.
"""
return self.is_order(1)
def is_double(self):
"""
Return ``True`` if the bond represents a double bond or ``False`` if
not.
"""
return self.is_order(2)
def is_triple(self):
"""
Return ``True`` if the bond represents a triple bond or ``False`` if
not.
"""
return self.is_order(3)
def is_quadruple(self):
"""
Return ``True`` if the bond represents a quadruple bond or ``False`` if
not.
"""
return self.is_order(4)
def is_double_or_triple(self):
"""
Return ``True`` if the bond represents a double or triple bond or ``False``
if not.
"""
return self.is_order(2) or self.is_order(3)
def is_benzene(self):
"""
Return ``True`` if the bond represents a benzene bond or ``False`` if
not.
"""
return self.is_order(1.5)
def is_hydrogen_bond(self):
"""
Return ``True`` if the bond represents a hydrogen bond or ``False`` if
not.
"""
return self.is_order(0.1)
def is_reaction_bond(self):
"""
Return ``True`` if the bond represents a reaction bond or ``False`` if
not.
"""
return self.is_order(0.05)
def increment_order(self):
"""
Update the bond as a result of applying a CHANGE_BOND action to
increase the order by one.
"""
if self.order <= 3.0001:
self.order += 1
else:
raise gr.ActionError('Unable to increment Bond due to CHANGE_BOND action: '
'Bond order "{0}" is greater than 3.'.format(self.order))
def decrement_order(self):
"""
Update the bond as a result of applying a CHANGE_BOND action to
decrease the order by one.
"""
if self.order >= 0.9999:
self.order -= 1
else:
raise gr.ActionError('Unable to decrease Bond due to CHANGE_BOND action: '
'bond order "{0}" is less than 1.'.format(self.order))
def _change_bond(self, order):
"""
Update the bond as a result of applying a CHANGE_BOND action,
where `order` specifies whether the bond is incremented or decremented
in bond order, and can be any real number.
"""
self.order += order
if self.order < -0.0001 or self.order > 4.0001:
raise gr.ActionError('Unable to update Bond due to CHANGE_BOND action: '
'Invalid resulting order "{0}".'.format(self.order))
def apply_action(self, action):
"""
Update the bond as a result of applying `action`, a tuple
containing the name of the reaction recipe action along with any
required parameters. The available actions can be found
:ref:`here <reaction-recipe-actions>`.
"""
if action[0].upper() == 'CHANGE_BOND':
if isinstance(action[2], str):
self.set_order_str(action[2])
else:
try: # try to see if addable
self._change_bond(action[2])
except TypeError:
raise gr.ActionError('Unable to update Bond due to CHANGE_BOND action: '
'Invalid order "{0}".'.format(action[2]))
else:
raise gr.ActionError('Unable to update GroupBond: Invalid action {0}.'.format(action))
def get_bond_string(self):
"""
Represent the bond object as a string (eg. 'C#N'). The returned string is independent of the atom ordering, with
the atom labels in alphabetical order (i.e. 'C-H' is possible but not 'H-C')
:return: str
"""
bond_symbol_mapping = {0.05: '~', 0.1: '~', 1: '-', 1.5: ':', 2: '=', 3: '#'}
atom_labels = [self.atom1.symbol, self.atom2.symbol]
atom_labels.sort()
try:
bond_symbol = bond_symbol_mapping[self.get_order_num()]
except KeyError:
# Direct lookup didn't work, but before giving up try
# with the is_order() method which allows a little latitude
# for floating point errors.
for order, symbol in bond_symbol_mapping.items():
if self.is_order(order):
bond_symbol = symbol
break
else: # didn't break
bond_symbol = '<bond order {0}>'.format(self.get_order_num())
return '{0}{1}{2}'.format(atom_labels[0], bond_symbol, atom_labels[1])
#################################################################################
class Molecule(Graph):
"""
A representation of a molecular structure using a graph data type, extending