File: GraphDescriptors.py

package info (click to toggle)
rdkit 202009.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 129,624 kB
  • sloc: cpp: 288,030; python: 75,571; java: 6,999; ansic: 5,481; sql: 1,968; yacc: 1,842; lex: 1,254; makefile: 572; javascript: 461; xml: 229; fortran: 183; sh: 134; cs: 93
file content (699 lines) | stat: -rw-r--r-- 20,296 bytes parent folder | download | duplicates (2)
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
# $Id$
#
# Copyright (C) 2003-2006 greg Landrum and Rational Discovery LLC
#
#   @@ All Rights Reserved @@
#  This file is part of the RDKit.
#  The contents are covered by the terms of the BSD license
#  which is included in the file license.txt, found at the root
#  of the RDKit source tree.
#
""" Calculation of topological/topochemical descriptors.



"""

from rdkit import Chem
from rdkit.Chem import Graphs
from rdkit.Chem import rdchem
from rdkit.Chem import rdMolDescriptors
import numpy
import math
from rdkit.ML.InfoTheory import entropy

ptable = Chem.GetPeriodicTable()

_log2val = math.log(2)


def _VertexDegrees(mat, onlyOnes=0):
  """  *Internal Use Only*

  this is just a row sum of the matrix... simple, neh?

  """
  if not onlyOnes:
    res = sum(mat)
  else:
    res = sum(numpy.equal(mat, 1))
  return res


def _NumAdjacencies(mol, dMat):
  """  *Internal Use Only*

  """
  res = mol.GetNumBonds()
  return res


def _GetCountDict(arr):
  """  *Internal Use Only*

  """
  res = {}
  for v in arr:
    res[v] = res.get(v, 0) + 1
  return res

# WARNING: this data should probably go somewhere else...
hallKierAlphas = {'Br': [None, None, 0.48],
                  'C': [-0.22, -0.13, 0.0],
                  'Cl': [None, None, 0.29],
                  'F': [None, None, -0.07],
                  'H': [0.0, 0.0, 0.0],
                  'I': [None, None, 0.73],
                  'N': [-0.29, -0.2, -0.04],
                  'O': [None, -0.2, -0.04],
                  'P': [None, 0.3, 0.43],
                  'S': [None, 0.22, 0.35]}

def _pyHallKierAlpha(m):
  """ calculate the Hall-Kier alpha value for a molecule

   From equations (58) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  """
  alphaSum = 0.0
  rC = ptable.GetRb0(6)
  for atom in m.GetAtoms():
    atNum = atom.GetAtomicNum()
    if not atNum:
      continue
    symb = atom.GetSymbol()
    alphaV = hallKierAlphas.get(symb, None)
    if alphaV is not None:
      hyb = atom.GetHybridization() - 2
      if (hyb < len(alphaV)):
        alpha = alphaV[hyb]
        if alpha is None:
          alpha = alphaV[-1]
      else:
        alpha = alphaV[-1]
    else:
      rA = ptable.GetRb0(atNum)
      alpha = rA / rC - 1
    # print(atom.GetIdx(), atom.GetSymbol(), alpha)
    alphaSum += alpha
  return alphaSum
# HallKierAlpha.version="1.0.2"


def Ipc(mol, avg=0, dMat=None, forceDMat=0):
  """This returns the information content of the coefficients of the characteristic
    polynomial of the adjacency matrix of a hydrogen-suppressed graph of a molecule.

    'avg = 1' returns the information content divided by the total population.

    From D. Bonchev & N. Trinajstic, J. Chem. Phys. vol 67, 4517-4533 (1977)

  """
  if forceDMat or dMat is None:
    if forceDMat:
      dMat = Chem.GetDistanceMatrix(mol, 0)
      mol._adjMat = dMat
    else:
      try:
        dMat = mol._adjMat
      except AttributeError:
        dMat = Chem.GetDistanceMatrix(mol, 0)
        mol._adjMat = dMat

  adjMat = numpy.equal(dMat, 1)
  cPoly = abs(Graphs.CharacteristicPolynomial(mol, adjMat))
  if avg:
    return entropy.InfoEntropy(cPoly)
  else:
    return sum(cPoly) * entropy.InfoEntropy(cPoly)


Ipc.version = "1.0.0"


def _pyKappa1(mol):
  """ Hall-Kier Kappa1 value

   From equations (58) and (59) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  """
  P1 = mol.GetNumBonds(1)
  A = mol.GetNumHeavyAtoms()
  alpha = HallKierAlpha(mol)
  denom = P1 + alpha
  if denom:
    kappa = (A + alpha) * (A + alpha - 1)**2 / denom**2
  else:
    kappa = 0.0
  return kappa
# Kappa1.version="1.0.0"


def _pyKappa2(mol):
  """  Hall-Kier Kappa2 value

   From equations (58) and (60) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  """
  P2 = len(Chem.FindAllPathsOfLengthN(mol, 2))
  A = mol.GetNumHeavyAtoms()
  alpha = HallKierAlpha(mol)
  denom = (P2 + alpha)**2
  if denom:
    kappa = (A + alpha - 1) * (A + alpha - 2)**2 / denom
  else:
    kappa = 0
  return kappa
# Kappa2.version="1.0.0"


def _pyKappa3(mol):
  """  Hall-Kier Kappa3 value

   From equations (58), (61) and (62) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  """
  P3 = len(Chem.FindAllPathsOfLengthN(mol, 3))
  A = mol.GetNumHeavyAtoms()
  alpha = HallKierAlpha(mol)
  denom = (P3 + alpha)**2
  if denom:
    if A % 2 == 1:
      kappa = (A + alpha - 1) * (A + alpha - 3)**2 / denom
    else:
      kappa = (A + alpha - 2) * (A + alpha - 3)**2 / denom
  else:
    kappa = 0
  return kappa
# Kappa3.version="1.0.0"

HallKierAlpha = lambda x: rdMolDescriptors.CalcHallKierAlpha(x)
HallKierAlpha.version = rdMolDescriptors._CalcHallKierAlpha_version
Kappa1 = lambda x: rdMolDescriptors.CalcKappa1(x)
Kappa1.version = rdMolDescriptors._CalcKappa1_version
Kappa2 = lambda x: rdMolDescriptors.CalcKappa2(x)
Kappa2.version = rdMolDescriptors._CalcKappa2_version
Kappa3 = lambda x: rdMolDescriptors.CalcKappa3(x)
Kappa3.version = rdMolDescriptors._CalcKappa3_version


def Chi0(mol):
  """ From equations (1),(9) and (10) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  """
  deltas = [x.GetDegree() for x in mol.GetAtoms()]
  while 0 in deltas:
    deltas.remove(0)
  deltas = numpy.array(deltas, 'd')
  res = sum(numpy.sqrt(1. / deltas))
  return res


Chi0.version = "1.0.0"


def Chi1(mol):
  """ From equations (1),(11) and (12) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  """
  c1s = [x.GetBeginAtom().GetDegree() * x.GetEndAtom().GetDegree() for x in mol.GetBonds()]
  while 0 in c1s:
    c1s.remove(0)
  c1s = numpy.array(c1s, 'd')
  res = sum(numpy.sqrt(1. / c1s))
  return res


Chi1.version = "1.0.0"


def _nVal(atom):
  return ptable.GetNOuterElecs(atom.GetAtomicNum()) - atom.GetTotalNumHs()


def _hkDeltas(mol, skipHs=1):
  global ptable
  res = []
  if hasattr(mol, '_hkDeltas') and mol._hkDeltas is not None:
    return mol._hkDeltas
  for atom in mol.GetAtoms():
    n = atom.GetAtomicNum()
    if n > 1:
      nV = ptable.GetNOuterElecs(n)
      nHs = atom.GetTotalNumHs()
      if n <= 10:
        # first row
        res.append(float(nV - nHs))
      else:
        # second row and up
        res.append(float(nV - nHs) / float(n - nV - 1))
    elif n == 1:
      if not skipHs:
        res.append(0.0)
    else:
      res.append(0.0)
  mol._hkDeltas = res
  return res


def _pyChi0v(mol):
  """  From equations (5),(9) and (10) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  """
  deltas = _hkDeltas(mol)
  while 0 in deltas:
    deltas.remove(0)
  mol._hkDeltas = None
  res = sum(numpy.sqrt(1. / numpy.array(deltas)))
  return res


def _pyChi1v(mol):
  """  From equations (5),(11) and (12) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  """
  deltas = numpy.array(_hkDeltas(mol, skipHs=0))
  res = 0.0
  for bond in mol.GetBonds():
    v = deltas[bond.GetBeginAtomIdx()] * deltas[bond.GetEndAtomIdx()]
    if v != 0.0:
      res += numpy.sqrt(1. / v)
  return res


def _pyChiNv_(mol, order=2):
  """  From equations (5),(15) and (16) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  **NOTE**: because the current path finding code does, by design,
  detect rings as paths (e.g. in C1CC1 there is *1* atom path of
  length 3), values of ChiNv with N >= 3 may give results that differ
  from those provided by the old code in molecules that have rings of
  size 3.

  """
  deltas = numpy.array(
    [(1. / numpy.sqrt(hkd) if hkd != 0.0 else 0.0) for hkd in _hkDeltas(mol, skipHs=0)])
  accum = 0.0
  for path in Chem.FindAllPathsOfLengthN(mol, order + 1, useBonds=0):
    accum += numpy.prod(deltas[numpy.array(path)])
  return accum


def _pyChi2v(mol):
  """ From equations (5),(15) and (16) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  """
  return _pyChiNv_(mol, 2)


def _pyChi3v(mol):
  """ From equations (5),(15) and (16) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  """
  return _pyChiNv_(mol, 3)


def _pyChi4v(mol):
  """ From equations (5),(15) and (16) of Rev. Comp. Chem. vol 2, 367-422, (1991)

  **NOTE**: because the current path finding code does, by design,
  detect rings as paths (e.g. in C1CC1 there is *1* atom path of
  length 3), values of Chi4v may give results that differ from those
  provided by the old code in molecules that have 3 rings.

  """
  return _pyChiNv_(mol, 4)


def _pyChi0n(mol):
  """  Similar to Hall Kier Chi0v, but uses nVal instead of valence
  This makes a big difference after we get out of the first row.

  """
  deltas = [_nVal(x) for x in mol.GetAtoms()]
  while deltas.count(0):
    deltas.remove(0)
  deltas = numpy.array(deltas, 'd')
  res = sum(numpy.sqrt(1. / deltas))
  return res


def _pyChi1n(mol):
  """  Similar to Hall Kier Chi1v, but uses nVal instead of valence

  """
  delts = numpy.array([_nVal(x) for x in mol.GetAtoms()], 'd')
  res = 0.0
  for bond in mol.GetBonds():
    v = delts[bond.GetBeginAtomIdx()] * delts[bond.GetEndAtomIdx()]
    if v != 0.0:
      res += numpy.sqrt(1. / v)
  return res


def _pyChiNn_(mol, order=2):
  """  Similar to Hall Kier ChiNv, but uses nVal instead of valence
  This makes a big difference after we get out of the first row.

  **NOTE**: because the current path finding code does, by design,
  detect rings as paths (e.g. in C1CC1 there is *1* atom path of
  length 3), values of ChiNn with N >= 3 may give results that differ
  from those provided by the old code in molecules that have rings of
  size 3.

  """
  nval = [_nVal(x) for x in mol.GetAtoms()]
  deltas = numpy.array([(1. / numpy.sqrt(x) if x else 0.0) for x in nval])
  accum = 0.0
  for path in Chem.FindAllPathsOfLengthN(mol, order + 1, useBonds=0):
    accum += numpy.prod(deltas[numpy.array(path)])
  return accum


def _pyChi2n(mol):
  """  Similar to Hall Kier Chi2v, but uses nVal instead of valence
  This makes a big difference after we get out of the first row.

  """
  return _pyChiNn_(mol, 2)


def _pyChi3n(mol):
  """  Similar to Hall Kier Chi3v, but uses nVal instead of valence
  This makes a big difference after we get out of the first row.

  """
  return _pyChiNn_(mol, 3)


def _pyChi4n(mol):
  """  Similar to Hall Kier Chi4v, but uses nVal instead of valence
  This makes a big difference after we get out of the first row.


  **NOTE**: because the current path finding code does, by design,
  detect rings as paths (e.g. in C1CC1 there is *1* atom path of
  length 3), values of Chi4n may give results that differ from those
  provided by the old code in molecules that have 3 rings.

  """
  return _pyChiNn_(mol, 4)


Chi0v = lambda x: rdMolDescriptors.CalcChi0v(x)
Chi0v.version = rdMolDescriptors._CalcChi0v_version
Chi1v = lambda x: rdMolDescriptors.CalcChi1v(x)
Chi1v.version = rdMolDescriptors._CalcChi1v_version
Chi2v = lambda x: rdMolDescriptors.CalcChi2v(x)
Chi2v.version = rdMolDescriptors._CalcChi2v_version
Chi3v = lambda x: rdMolDescriptors.CalcChi3v(x)
Chi3v.version = rdMolDescriptors._CalcChi3v_version
Chi4v = lambda x: rdMolDescriptors.CalcChi4v(x)
Chi4v.version = rdMolDescriptors._CalcChi4v_version
ChiNv_ = lambda x, y: rdMolDescriptors.CalcChiNv(x, y)
ChiNv_.version = rdMolDescriptors._CalcChiNv_version

Chi0n = lambda x: rdMolDescriptors.CalcChi0n(x)
Chi0n.version = rdMolDescriptors._CalcChi0n_version
Chi1n = lambda x: rdMolDescriptors.CalcChi1n(x)
Chi1n.version = rdMolDescriptors._CalcChi1n_version
Chi2n = lambda x: rdMolDescriptors.CalcChi2n(x)
Chi2n.version = rdMolDescriptors._CalcChi2n_version
Chi3n = lambda x: rdMolDescriptors.CalcChi3n(x)
Chi3n.version = rdMolDescriptors._CalcChi3n_version
Chi4n = lambda x: rdMolDescriptors.CalcChi4n(x)
Chi4n.version = rdMolDescriptors._CalcChi4n_version
ChiNn_ = lambda x, y: rdMolDescriptors.CalcChiNn(x, y)
ChiNn_.version = rdMolDescriptors._CalcChiNn_version


def BalabanJ(mol, dMat=None, forceDMat=0):
  """ Calculate Balaban's J value for a molecule

  **Arguments**

    - mol: a molecule

    - dMat: (optional) a distance/adjacency matrix for the molecule, if this
      is not provide, one will be calculated

    - forceDMat: (optional) if this is set, the distance/adjacency matrix
      will be recalculated regardless of whether or not _dMat_ is provided
      or the molecule already has one

  **Returns**

    - a float containing the J value

  We follow the notation of Balaban's paper:
    Chem. Phys. Lett. vol 89, 399-404, (1982)

  """
  # if no dMat is passed in, calculate one ourselves
  if forceDMat or dMat is None:
    if forceDMat:
      # FIX: should we be using atom weights here or not?
      dMat = Chem.GetDistanceMatrix(mol, useBO=1, useAtomWts=0, force=1)
      mol._balabanMat = dMat
      adjMat = Chem.GetAdjacencyMatrix(mol, useBO=0, emptyVal=0, force=0, prefix="NoBO")
      mol._adjMat = adjMat
    else:
      try:
        # first check if the molecule already has one
        dMat = mol._balabanMat
      except AttributeError:
        # nope, gotta calculate one
        dMat = Chem.GetDistanceMatrix(mol, useBO=1, useAtomWts=0, force=0, prefix="Balaban")
        # now store it
        mol._balabanMat = dMat
      try:
        adjMat = mol._adjMat
      except AttributeError:
        adjMat = Chem.GetAdjacencyMatrix(mol, useBO=0, emptyVal=0, force=0, prefix="NoBO")
        mol._adjMat = adjMat
  else:
    adjMat = Chem.GetAdjacencyMatrix(mol, useBO=0, emptyVal=0, force=0, prefix="NoBO")

  s = _VertexDegrees(dMat)
  q = _NumAdjacencies(mol, dMat)
  n = mol.GetNumAtoms()
  mu = q - n + 1

  sum_ = 0.
  nS = len(s)
  for i in range(nS):
    si = s[i]
    for j in range(i, nS):
      if adjMat[i, j] == 1:
        sum_ += 1. / numpy.sqrt(si * s[j])

  if mu + 1 != 0:
    J = float(q) / float(mu + 1) * sum_
  else:
    J = 0

  return J


BalabanJ.version = "1.0.0"

# ------------------------------------------------------------------------
#
# Start block of BertzCT stuff.
#


def _AssignSymmetryClasses(mol, vdList, bdMat, forceBDMat, numAtoms, cutoff):
  """
     Used by BertzCT

     vdList: the number of neighbors each atom has
     bdMat: "balaban" distance matrix

  """
  if forceBDMat:
    bdMat = Chem.GetDistanceMatrix(mol, useBO=1, useAtomWts=0, force=1, prefix="Balaban")
    mol._balabanMat = bdMat

  keysSeen = []
  symList = [0] * numAtoms
  for i in range(numAtoms):
    tmpList = bdMat[i].tolist()
    tmpList.sort()
    theKey = tuple(['%.4f' % x for x in tmpList[:cutoff]])
    try:
      idx = keysSeen.index(theKey)
    except ValueError:
      idx = len(keysSeen)
      keysSeen.append(theKey)
    symList[i] = idx + 1
  return tuple(symList)


def _LookUpBondOrder(atom1Id, atom2Id, bondDic):
  """
     Used by BertzCT
  """
  if atom1Id < atom2Id:
    theKey = (atom1Id, atom2Id)
  else:
    theKey = (atom2Id, atom1Id)
  tmp = bondDic[theKey]
  if tmp == Chem.BondType.AROMATIC:
    tmp = 1.5
  else:
    tmp = float(tmp)
  # tmp = int(tmp)
  return tmp


def _CalculateEntropies(connectionDict, atomTypeDict, numAtoms):
  """
     Used by BertzCT
  """
  connectionList = list(connectionDict.values())
  totConnections = sum(connectionList)
  connectionIE = totConnections * (
    entropy.InfoEntropy(numpy.array(connectionList)) + math.log(totConnections) / _log2val)
  atomTypeList = list(atomTypeDict.values())
  atomTypeIE = numAtoms * entropy.InfoEntropy(numpy.array(atomTypeList))
  return atomTypeIE + connectionIE


def _CreateBondDictEtc(mol, numAtoms):
  """ _Internal Use Only_
     Used by BertzCT

  """
  bondDict = {}
  nList = [None] * numAtoms
  vdList = [0] * numAtoms
  for aBond in mol.GetBonds():
    atom1 = aBond.GetBeginAtomIdx()
    atom2 = aBond.GetEndAtomIdx()
    if atom1 > atom2:
      atom2, atom1 = atom1, atom2
    if not aBond.GetIsAromatic():
      bondDict[(atom1, atom2)] = aBond.GetBondType()
    else:
      # mark Kekulized systems as aromatic
      bondDict[(atom1, atom2)] = Chem.BondType.AROMATIC
    if nList[atom1] is None:
      nList[atom1] = [atom2]
    elif atom2 not in nList[atom1]:
      nList[atom1].append(atom2)
    if nList[atom2] is None:
      nList[atom2] = [atom1]
    elif atom1 not in nList[atom2]:
      nList[atom2].append(atom1)

  for i, element in enumerate(nList):
    try:
      element.sort()
      vdList[i] = len(element)
    except Exception:
      vdList[i] = 0
  return bondDict, nList, vdList


def BertzCT(mol, cutoff=100, dMat=None, forceDMat=1):
  """ A topological index meant to quantify "complexity" of molecules.

     Consists of a sum of two terms, one representing the complexity
     of the bonding, the other representing the complexity of the
     distribution of heteroatoms.

     From S. H. Bertz, J. Am. Chem. Soc., vol 103, 3599-3601 (1981)

     "cutoff" is an integer value used to limit the computational
     expense.  A cutoff value tells the program to consider vertices
     topologically identical if their distance vectors (sets of
     distances to all other vertices) are equal out to the "cutoff"th
     nearest-neighbor.

     **NOTE**  The original implementation had the following comment:
         > this implementation treats aromatic rings as the
         > corresponding Kekule structure with alternating bonds,
         > for purposes of counting "connections".
       Upon further thought, this is the WRONG thing to do.  It
        results in the possibility of a molecule giving two different
        CT values depending on the kekulization.  For example, in the
        old implementation, these two SMILES:
           CC2=CN=C1C3=C(C(C)=C(C=N3)C)C=CC1=C2C
           CC3=CN=C2C1=NC=C(C)C(C)=C1C=CC2=C3C
        which correspond to differentk kekule forms, yield different
        values.
       The new implementation uses consistent (aromatic) bond orders
        for aromatic bonds.

       THIS MEANS THAT THIS IMPLEMENTATION IS NOT BACKWARDS COMPATIBLE.

       Any molecule containing aromatic rings will yield different
       values with this implementation.  The new behavior is the correct
       one, so we're going to live with the breakage.

     **NOTE** this barfs if the molecule contains a second (or
       nth) fragment that is one atom.

  """
  atomTypeDict = {}
  connectionDict = {}
  numAtoms = mol.GetNumAtoms()
  if forceDMat or dMat is None:
    if forceDMat:
      # nope, gotta calculate one
      dMat = Chem.GetDistanceMatrix(mol, useBO=0, useAtomWts=0, force=1)
      mol._adjMat = dMat
    else:
      try:
        dMat = mol._adjMat
      except AttributeError:
        dMat = Chem.GetDistanceMatrix(mol, useBO=0, useAtomWts=0, force=1)
        mol._adjMat = dMat

  if numAtoms < 2:
    return 0

  bondDict, neighborList, vdList = _CreateBondDictEtc(mol, numAtoms)
  symmetryClasses = _AssignSymmetryClasses(mol, vdList, dMat, forceDMat, numAtoms, cutoff)
  # print('Symmm Classes:',symmetryClasses)
  for atomIdx in range(numAtoms):
    hingeAtomNumber = mol.GetAtomWithIdx(atomIdx).GetAtomicNum()
    atomTypeDict[hingeAtomNumber] = atomTypeDict.get(hingeAtomNumber, 0) + 1

    hingeAtomClass = symmetryClasses[atomIdx]
    numNeighbors = vdList[atomIdx]
    for i in range(numNeighbors):
      neighbor_iIdx = neighborList[atomIdx][i]
      NiClass = symmetryClasses[neighbor_iIdx]
      bond_i_order = _LookUpBondOrder(atomIdx, neighbor_iIdx, bondDict)
      # print('\t',atomIdx,i,hingeAtomClass,NiClass,bond_i_order)
      if (bond_i_order > 1) and (neighbor_iIdx > atomIdx):
        numConnections = bond_i_order * (bond_i_order - 1) / 2
        connectionKey = (min(hingeAtomClass, NiClass), max(hingeAtomClass, NiClass))
        connectionDict[connectionKey] = connectionDict.get(connectionKey, 0) + numConnections

      for j in range(i + 1, numNeighbors):
        neighbor_jIdx = neighborList[atomIdx][j]
        NjClass = symmetryClasses[neighbor_jIdx]
        bond_j_order = _LookUpBondOrder(atomIdx, neighbor_jIdx, bondDict)
        numConnections = bond_i_order * bond_j_order
        connectionKey = (min(NiClass, NjClass), hingeAtomClass, max(NiClass, NjClass))
        connectionDict[connectionKey] = connectionDict.get(connectionKey, 0) + numConnections

  if not connectionDict:
    connectionDict = {'a': 1}

  return _CalculateEntropies(connectionDict, atomTypeDict, numAtoms)


BertzCT.version = "2.0.0"
# Recent Revisions:
#  1.0.0 -> 2.0.0:
#    - force distance matrix updates properly (Fixed as part of Issue 125)
#    - handle single-atom fragments (Issue 136)

#
# End block of BertzCT stuff.
#
# ------------------------------------------------------------------------