File: Atropisomers.cpp

package info (click to toggle)
rdkit 202503.1-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 578; xml: 229; fortran: 183; sh: 105
file content (1202 lines) | stat: -rw-r--r-- 44,035 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
//
//  Copyright (C) 2004-2021 Tad hurst/CDD  and other RDKit contributors
//
//   @@ 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.
//
//
#include <list>
#include <RDGeneral/RDLog.h>
#include <GraphMol/Chirality.h>
#include <GraphMol/FileParsers/MolFileStereochem.h>
#include <GraphMol/Atropisomers.h>
#include <Geometry/point.h>
#include <boost/dynamic_bitset.hpp>
#include <algorithm>
#include <RDGeneral/Ranking.h>
#include <RDGeneral/FileParseException.h>
#include <RDGeneral/BoostStartInclude.h>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
#include <RDGeneral/BoostEndInclude.h>

constexpr double REALLY_SMALL_BOND_LEN = 0.0000001;

namespace RDKit {
namespace Atropisomers {

bool getAtropisomerAtomsAndBonds(const Bond *bond,
                                 AtropAtomAndBondVec atomsAndBondVects[2],
                                 const ROMol &mol) {
  PRECONDITION(bond, "no bond");
  atomsAndBondVects[0].first = bond->getBeginAtom();
  atomsAndBondVects[1].first = bond->getEndAtom();

  // get the one or two bonds on each end

  for (int bondAtomIndex = 0; bondAtomIndex < 2; ++bondAtomIndex) {
    for (const auto nbrBond :
         mol.atomBonds(atomsAndBondVects[bondAtomIndex].first)) {
      if (nbrBond == bond) {
        continue;  // a bond is NOT its own neighbor
      }
      atomsAndBondVects[bondAtomIndex].second.push_back(nbrBond);
    }
    if (atomsAndBondVects[bondAtomIndex].second.size() == 0) {
      return false;  // no neighbor bonds found
    }

    // make sure the bond with this lowest atom is is first

    if (atomsAndBondVects[bondAtomIndex].second.size() == 2 &&
        atomsAndBondVects[bondAtomIndex]
                .second[1]
                ->getOtherAtom(atomsAndBondVects[bondAtomIndex].first)
                ->getIdx() <
            atomsAndBondVects[bondAtomIndex]
                .second[0]
                ->getOtherAtom(atomsAndBondVects[bondAtomIndex].first)
                ->getIdx()) {
      std::swap(atomsAndBondVects[bondAtomIndex].second[0],
                atomsAndBondVects[bondAtomIndex].second[1]);
    }
  }

  return true;
}

bool getBondFrameOfReference(const Bond *bond, const Conformer *conf,
                             RDGeom::Point3D &xAxis, RDGeom::Point3D &yAxis,
                             RDGeom::Point3D &zAxis) {
  // create a frame of reference that has its X-axis along the atrop bond
  // for 2D confs, the yAxis is in the 2D plane and the zAxis is perpendicular
  // to that plane) for 3D confs  the yAxis and the zAxis are arbitrary.

  PRECONDITION(bond, "bad bond");

  xAxis = conf->getAtomPos(bond->getEndAtom()->getIdx()) -
          conf->getAtomPos(bond->getBeginAtom()->getIdx());
  if (xAxis.length() < REALLY_SMALL_BOND_LEN) {
    return false;  // bond len is xero
  }
  xAxis.normalize();
  if (!conf->is3D()) {
    yAxis = RDGeom::Point3D(-xAxis.y, xAxis.x, 0);
    yAxis.normalize();
    zAxis = RDGeom::Point3D(0.0, 0.0, 1.0);
    return true;
  }

  // here for 3D conf

  if (xAxis.x > REALLY_SMALL_BOND_LEN || xAxis.y > REALLY_SMALL_BOND_LEN) {
    zAxis = RDGeom::Point3D(-xAxis.y, xAxis.x,
                            0);  // temp z axis - used to find yAxis
  } else {
    zAxis = RDGeom::Point3D(xAxis.z, xAxis.z,
                            0);  // temp z axis - used to find yAxis
  }

  yAxis = zAxis.crossProduct(xAxis);
  zAxis = xAxis.crossProduct(yAxis);
  yAxis.normalize();
  zAxis.normalize();

  return true;
}

Bond::BondDir getBondDirForAtropisomerNoConf(Bond::BondStereo bondStereo,
                                             unsigned int whichEnd,
                                             unsigned int whichBond) {
  // the convention is that in the absence of coords, the coordiates are choosen
  // with the lowest numbered atom of the atrop bond down, and the other atom
  // straight up.
  // On each end, the lowest numbered connecting atom is on the left
  //
  //              a      b
  //               \   /
  //                 c
  //                 |
  //                 d
  //               /   \     aaa
  //              e      f
  //
  // where  c > d
  //        a < b
  //        e < f

  PRECONDITION(whichEnd <= 1, "whichEnd must be 0 or 1");
  PRECONDITION(whichBond <= 1, "whichBond must be 0 or 1");
  PRECONDITION(bondStereo == Bond::BondStereo::STEREOATROPCW ||
                   bondStereo == Bond::BondStereo::STEREOATROPCCW,
               "bondStereo must be BondAtropisomerCW or BondAtropisomerCCW");

  int flips = 0;
  if (bondStereo == Bond::BondStereo::STEREOATROPCW) {
    ++flips;
  }
  if (whichBond == 1) {
    ++flips;
  }
  if (whichEnd == 1) {
    ++flips;
  }

  return flips % 2 ? Bond::BEGINDASH : Bond::BEGINWEDGE;
}

Bond::BondDir getBondDirForAtropisomer2d(RDGeom::Point3D bondVecs[2],
                                         Bond::BondStereo bondStereo,
                                         unsigned int whichEnd,
                                         unsigned int whichBond) {
  PRECONDITION(whichEnd <= 1, "whichEnd must be 0 or 1");
  PRECONDITION(whichBond <= 1, "whichBond must be 0 or 1");
  PRECONDITION(bondStereo == Bond::BondStereo::STEREOATROPCW ||
                   bondStereo == Bond::BondStereo::STEREOATROPCCW,
               "bondStereo must be BondAtropisomerCW or BondAtropisomerCCW");

  int flips = 0;
  if (bondStereo == Bond::BondStereo::STEREOATROPCCW) {
    ++flips;
  }
  if (whichBond == 1) {
    ++flips;
  }
  if (whichEnd == 1) {
    ++flips;
  }
  if (bondVecs[1 - whichEnd].y < 0) {
    ++flips;  // if the OTHER end is negative for the low index bond vec, it
              // is a flip
  }

  return flips % 2 ? Bond::BEGINWEDGE : Bond::BEGINDASH;
}

Bond::BondDir getBondDirForAtropisomer3d(Bond *whichBond,
                                         const Conformer *conf) {
  // for 3D we mark it as wedge or hash depending on the z-value of the bond
  // vector
  //  IT really doesn't matter since we ignore these except as MARKERS for
  //  which bonds are atropisomer bonds
  if ((conf->getAtomPos(whichBond->getEndAtom()->getIdx()).z -
       conf->getAtomPos(whichBond->getBeginAtom()->getIdx()).z) >
      REALLY_SMALL_BOND_LEN) {
    return Bond::BondDir::BEGINWEDGE;
  } else {
    return Bond::BondDir::BEGINDASH;
  }
}

bool getAtropIsomerEndVect(const AtropAtomAndBondVec &atomAndBondVec,
                           const RDGeom::Point3D &yAxis,
                           const RDGeom::Point3D &zAxis, const Conformer *conf,
                           RDGeom::Point3D &bondVec) {
  PRECONDITION(
      atomAndBondVec.second.size() > 0 && atomAndBondVec.second.size() < 3,
      "bad bond size");
  PRECONDITION(atomAndBondVec.second[0], "bad first bond");
  PRECONDITION(atomAndBondVec.second.size() == 1 || atomAndBondVec.second[1],
               "bad second bond");

  bondVec = conf->getAtomPos(atomAndBondVec.second[0]
                                 ->getOtherAtom(atomAndBondVec.first)
                                 ->getIdx()) -
            conf->getAtomPos(
                atomAndBondVec.first->getIdx());  // in old frame of reference

  bondVec = RDGeom::Point3D(0.0, bondVec.dotProduct(yAxis),
                            bondVec.dotProduct(zAxis));  // in new frame

  // make sure the other atom is on the other side

  if (atomAndBondVec.second.size() == 2) {
    RDGeom::Point3D otherVec =
        conf->getAtomPos(atomAndBondVec.second[1]
                             ->getOtherAtom(atomAndBondVec.first)
                             ->getIdx()) -
        conf->getAtomPos(
            atomAndBondVec.first->getIdx());  // in old frame of reference
    otherVec = RDGeom::Point3D(0.0, otherVec.dotProduct(yAxis),
                               otherVec.dotProduct(zAxis));  // in new frame

    if (bondVec.length() < REALLY_SMALL_BOND_LEN) {
      bondVec = -otherVec;  // put it on the other side of otherVec
    } else if (bondVec.dotProduct(otherVec) > REALLY_SMALL_BOND_LEN) {
      // the product of dotproducts (y-values) should be
      // negative (or at least zero)
      BOOST_LOG(rdWarningLog)
          << "Both bonds on one end of an atropisomer are on the same side - atoms is : "
          << atomAndBondVec.first->getIdx() << std::endl;
      return false;
    }
  }
  if (bondVec.length() < REALLY_SMALL_BOND_LEN) {
    BOOST_LOG(rdWarningLog)
        << "Could not find a bond on one end of an atropisomer that is not co-linear - atoms are : "
        << atomAndBondVec.first->getIdx() << std::endl;
    return false;
  }

  bondVec.normalize();
  return true;
}

std::pair<bool, Bond::BondDir> getBondDir(
    const Bond *bond, const AtropAtomAndBondVec &atomAndBondVec) {
  // get the wedge dir for this end of the bond
  // if the first bond 1 has a bondDir, use it
  // if the second bond has a bond dir use the opposite of if
  // if both bonds have a dir, make sure they are different

  auto bond1Dir = atomAndBondVec.second[0]->getBondDir();
  if (bond1Dir != Bond::BEGINWEDGE && bond1Dir != Bond::BEGINDASH) {
    bond1Dir = Bond::NONE;  //  we dont care if it any thing else
  }
  auto bond2Dir = atomAndBondVec.second.size() == 2
                      ? atomAndBondVec.second[1]->getBondDir()
                      : Bond::NONE;
  if (bond2Dir != Bond::BEGINWEDGE && bond2Dir != Bond::BEGINDASH) {
    bond2Dir = Bond::NONE;
  }

  // if both are set to a direction, they must NOT be the same - one
  // must be a dash and the other a hash

  if (bond1Dir != Bond::NONE && bond2Dir != Bond::NONE &&
      bond1Dir == bond2Dir) {
    BOOST_LOG(rdWarningLog)
        << "The bonds on one end of an atropisomer are both UP or both DOWN - atoms are: "
        << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx() << std::endl;
    return {false, Bond::BondDir::NONE};
  }

  if (bond1Dir == Bond::BEGINWEDGE || bond2Dir == Bond::BEGINDASH) {
    return {true, Bond::BondDir::BEGINWEDGE};
  }
  if (bond1Dir == Bond::BEGINDASH || bond2Dir == Bond::BEGINWEDGE) {
    return {true, Bond::BondDir::BEGINDASH};
  }
  return {true, Bond::BondDir::NONE};
}

void DetectAtropisomerChiralityOneBond(Bond *bond, ROMol &mol,
                                       const Conformer *conf) {
  // the approach is this:
  // we will view the system along the line from the potential atropisomer
  // bond, from atom1 to atom 2 and we do a coordinate transformation to
  // the plane of reference where that vector, from a1 to a2, is the x-AXIS.
  // For 2D, the y axis is in the 2D plane, and the zaxis is perpendicaul to
  // the 2D plane For 3D, the Y and Z axes are taken arbitrarily to form
  // a right-handed system with the X-axis.
  //  atoms 1 and 2 each have one or two bonds out from the main potential
  //  atrop bond. for each end of the main bond, we find a vector to reprent
  //  the neighbor atom with the smallest index as its projection onto the
  //  x=0 plane.
  // (In 2d, this projection is on the y-AXIS for the end that does NOT have
  // a wedge/hash bond, and  on the z axis - out of the plane - for the end
  // that does have a wedge/hash). The chirality is recorded as the
  // direction we rotate from, atom 1's projection to atom2's proejection -
  // either clockwise or counter clockwise

  PRECONDITION(bond, "bad bond");

  // one vector for each end - each one - should end up with 1 or 2 entries
  AtropAtomAndBondVec atomAndBondVecs[2];
  if (!getAtropisomerAtomsAndBonds(bond, atomAndBondVecs, mol)) {
    return;  // not an atropisomer
  }

  // make sure we do not have wiggle bonds

  for (auto atomAndBondVec : atomAndBondVecs) {
    for (auto endBond : atomAndBondVec.second) {
      if (endBond->getBondDir() == Bond::UNKNOWN) {
        return;  // not an atropisomer
      }
    }
  }

  // the convention is that in the absence of coords, the coordiates are choosen
  // with the lowest numbered atom of the atrop bond down, and the other atom
  // straight up.
  // On each end, the lowest numbered connecting atom is on the left
  //
  //              a      b
  //               \   /
  //                 c
  //                 |
  //                 d
  //               /   \     aaa
  //              e      f
  //
  // where  c > d
  //        a < b
  //        e < f

  if (conf == nullptr) {
    std::pair<bool, Bond::BondDir> bond1DirResult;
    bond1DirResult = getBondDir(bond, atomAndBondVecs[0]);
    if (!bond1DirResult.first) {
      return;
    }
    std::pair<bool, Bond::BondDir> bond2DirResult;
    bond2DirResult = getBondDir(bond, atomAndBondVecs[1]);
    if (!bond2DirResult.first) {
      return;
    }
    if (bond1DirResult.second == bond2DirResult.second) {
      BOOST_LOG(rdWarningLog)
          << "inconsistent bond wedging for an atropisomer.  Atoms are: "
          << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx()
          << std::endl;
      return;
    }

    if (bond1DirResult.second == Bond::BEGINWEDGE ||
        bond2DirResult.second == Bond::BEGINDASH) {
      bond->setStereo(Bond::BondStereo::STEREOATROPCCW);
    } else if (bond1DirResult.second == Bond::BEGINDASH ||
               bond2DirResult.second == Bond::BEGINWEDGE) {
      bond->setStereo(Bond::BondStereo::STEREOATROPCW);
    }

    return;
  }

  // create a frame of reference that has its X-axis along the atrop bond

  RDGeom::Point3D xAxis, yAxis, zAxis;
  if (!getBondFrameOfReference(bond, conf, xAxis, yAxis, zAxis)) {
    // connot percieve atroisomer
    BOOST_LOG(rdWarningLog)
        << "Failed to get a frame of reference along an atropisomer bond - atoms are: "
        << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx() << std::endl;
    return;
  }
  RDGeom::Point3D bondVecs[2];  // one bond vector from each end of the
                                // potential atropisomer bond

  for (int bondAtomIndex = 0; bondAtomIndex < 2; ++bondAtomIndex) {
    // if the conf is 2D, we use the wedge bonds to set the coords for the
    // projected vector onto the xAxis perpendicular plane (looking down
    // the atrop bond )

    if (!conf->is3D()) {
      // get the wedge dir for this end of the bond
      // if the first bond 1 has a bondDir, use it
      // if the second bond has a bond dir use the opposite of if
      // if both bonds have a dir, make sure they are different

      std::pair<bool, Bond::BondDir> bondDirResult;

      bondDirResult = getBondDir(bond, atomAndBondVecs[bondAtomIndex]);
      if (!bondDirResult.first) {
        return;
      }

      if (!getAtropIsomerEndVect(atomAndBondVecs[bondAtomIndex], yAxis, zAxis,
                                 conf, bondVecs[bondAtomIndex])) {
        return;
      }

      if (bondDirResult.second == Bond::BEGINWEDGE) {
        bondVecs[bondAtomIndex].y *= 0.707;
        bondVecs[bondAtomIndex].z = fabs(bondVecs[bondAtomIndex].y);
      } else if (bondDirResult.second == Bond::BEGINDASH) {
        bondVecs[bondAtomIndex].y *= 0.707;
        bondVecs[bondAtomIndex].z = -fabs(bondVecs[bondAtomIndex].y);
      }
    } else {  // the conf is 3D
      // to be considered, one or more neighbor bonds must have a wedge or
      // hash

      // find the projection of the bond(s) on this end in the frame of
      // reference's  x=0  plane
      RDGeom::Point3D tempBondVec =
          conf->getAtomPos(
              atomAndBondVecs[bondAtomIndex]
                  .second[0]
                  ->getOtherAtom(atomAndBondVecs[bondAtomIndex].first)
                  ->getIdx()) -
          conf->getAtomPos(atomAndBondVecs[bondAtomIndex].first->getIdx());
      bondVecs[bondAtomIndex] = RDGeom::Point3D(
          0.0, tempBondVec.dotProduct(yAxis), tempBondVec.dotProduct(zAxis));

      if (atomAndBondVecs[bondAtomIndex].second.size() == 2) {
        tempBondVec =
            conf->getAtomPos(
                atomAndBondVecs[bondAtomIndex]
                    .second[1]
                    ->getOtherAtom(atomAndBondVecs[bondAtomIndex].first)
                    ->getIdx()) -
            conf->getAtomPos(atomAndBondVecs[bondAtomIndex].first->getIdx());

        // get the projection of the 2nd bond on the x=0 plane

        RDGeom::Point3D otherBondVec = RDGeom::Point3D(
            0.0, tempBondVec.dotProduct(yAxis), tempBondVec.dotProduct(zAxis));

        // if the first atom is co-linear with the main atrop bond, use
        // the opposite of the 2nd atom

        if (bondVecs[bondAtomIndex].length() < REALLY_SMALL_BOND_LEN) {
          bondVecs[bondAtomIndex] =
              -otherBondVec;  // note - it might still be co-linear-
                              // this is checked below
        } else if (bondVecs[bondAtomIndex].dotProduct(otherBondVec) >
                   REALLY_SMALL_BOND_LEN) {
          BOOST_LOG(rdWarningLog)
              << "Both bonds on one end of an atropisomer are on the same side - atoms are: "
              << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx()
              << std::endl;
          return;
        }
      }

      if (bondVecs[bondAtomIndex].length() < REALLY_SMALL_BOND_LEN) {
        BOOST_LOG(rdWarningLog)
            << "Failed to find a bond on one end of an atropisomer that is NOT co-linear - atoms are: "
            << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx()
            << std::endl;
        return;
      }
    }
  }

  auto crossProduct = bondVecs[1].crossProduct(bondVecs[0]);

  if (crossProduct.x > REALLY_SMALL_BOND_LEN) {
    bond->setStereo(Bond::BondStereo::STEREOATROPCCW);
  } else if (crossProduct.x < -REALLY_SMALL_BOND_LEN) {
    bond->setStereo(Bond::BondStereo::STEREOATROPCW);
  } else {
    BOOST_LOG(rdWarningLog)
        << "The 2 defining bonds for an atropisomer are co-planar - atoms are: "
        << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx() << std::endl;
    return;
  }
}

void cleanupAtropisomerStereoGroups(ROMol &mol) {
  std::vector<StereoGroup> newsgs;
  for (auto sg : mol.getStereoGroups()) {
    std::vector<Atom *> okatoms;
    std::vector<Bond *> okbonds;

    for (auto atom : sg.getAtoms()) {
      bool foundAtrop = false;
      for (auto bndI : boost::make_iterator_range(mol.getAtomBonds(atom))) {
        auto bond = (mol)[bndI];
        if (bond->getStereo() == Bond::BondStereo::STEREOATROPCCW ||
            bond->getStereo() == Bond::BondStereo::STEREOATROPCW) {
          foundAtrop = true;
          if (std::find(okbonds.begin(), okbonds.end(), bond) ==
              okbonds.end()) {
            okbonds.push_back(bond);
          }
        }
      }

      if (!foundAtrop) {
        okatoms.push_back(atom);
      }
    }

    if (okbonds.empty()) {
      newsgs.push_back(sg);
    } else {
      newsgs.emplace_back(sg.getGroupType(), std::move(okatoms),
                          std::move(okbonds));
    }
  }
  mol.setStereoGroups(std::move(newsgs));
}

void detectAtropisomerChirality(ROMol &mol, const Conformer *conf) {
  PRECONDITION(conf == nullptr || &(conf->getOwningMol()) == &mol,
               "conformer does not belong to molecule");

  std::set<Bond *> bondsToTry;

  for (auto bond : mol.bonds()) {
    if (canHaveDirection(*bond) &&
        (bond->getBondDir() == Bond::BondDir::BEGINDASH ||
         bond->getBondDir() == Bond::BondDir::BEGINWEDGE)) {
      for (const auto &nbrBond : mol.atomBonds(bond->getBeginAtom())) {
        if (nbrBond == bond) {
          continue;  // a bond is NOT its own neighbor
        }
        bondsToTry.insert(nbrBond);
      }
    }
  }

  for (auto bondToTry : bondsToTry) {
    if (bondToTry->getBeginAtom()->needsUpdatePropertyCache()) {
      bondToTry->getBeginAtom()->updatePropertyCache(false);
    }
    if (bondToTry->getEndAtom()->needsUpdatePropertyCache()) {
      bondToTry->getEndAtom()->updatePropertyCache(false);
    }
    if (bondToTry->getBondType() != Bond::SINGLE ||
        bondToTry->getStereo() == Bond::BondStereo::STEREOANY ||
        bondToTry->getBeginAtom()->getTotalDegree() < 2 ||
        bondToTry->getEndAtom()->getTotalDegree() < 2 ||
        bondToTry->getBeginAtom()->getTotalDegree() > 3 ||
        bondToTry->getEndAtom()->getTotalDegree() > 3) {
      continue;
    }

    DetectAtropisomerChiralityOneBond(bondToTry, mol, conf);
  }
}
void getAllAtomIdsForStereoGroup(
    const ROMol &mol, const StereoGroup &group,
    std::vector<unsigned int> &atomIds,
    const std::map<int, std::unique_ptr<RDKit::Chirality::WedgeInfoBase>>
        &wedgeBonds) {
  atomIds.clear();
  for (auto &&atom : group.getAtoms()) {
    atomIds.push_back(atom->getIdx());
  }

  for (auto &&bond : group.getBonds()) {
    // figure out which atoms of the bond get wedge/hash indications
    // mark the atom with the wedge/hash

    for (auto atom : {bond->getBeginAtom(), bond->getEndAtom()}) {
      for (const auto atomBond : mol.atomBonds(atom)) {
        if (atomBond->getIdx() == bond->getIdx()) {
          continue;
        }

        if (atomBond->getBondDir() == Bond::BEGINWEDGE ||
            atomBond->getBondDir() == Bond::BEGINDASH ||
            (wedgeBonds.find(atomBond->getIdx()) != wedgeBonds.end() &&
             (wedgeBonds.at(atomBond->getIdx())->getType()) ==
                 Chirality::WedgeInfoType::WedgeInfoTypeAtropisomer)) {
          if (std::find(atomIds.begin(), atomIds.end(), atom->getIdx()) ==
              atomIds.end()) {
            atomIds.push_back(atom->getIdx());
          }
        }
      }
    }
  }
}

bool WedgeBondFromAtropisomerOneBondNoConf(
    Bond *bond, const ROMol &mol,
    std::map<int, std::unique_ptr<RDKit::Chirality::WedgeInfoBase>>
        &wedgeBonds) {
  PRECONDITION(bond, "no bond");

  AtropAtomAndBondVec atomAndBondVecs[2];
  if (!getAtropisomerAtomsAndBonds(bond, atomAndBondVecs, mol)) {
    return false;  // not an atropisomer
  }

  //  make sure we do not have wiggle bonds

  for (auto atomAndBondVec : atomAndBondVecs) {
    for (auto endBond : atomAndBondVec.second) {
      if (endBond->getBondDir() == Bond::UNKNOWN) {
        return false;  // not an atropisomer)
      }
    }
  }

  // first see if any candidate bond is already set to a wedge or hash
  // if so, we will use that bond as a wedge or hash

  std::vector<int> useBondsAtEnd[2];
  bool foundBondDir = false;

  for (unsigned int whichEnd = 0; whichEnd < 2; ++whichEnd) {
    for (unsigned int whichBond = 0;
         whichBond < atomAndBondVecs[whichEnd].second.size(); ++whichBond) {
      auto bondDir = atomAndBondVecs[whichEnd].second[whichBond]->getBondDir();

      // see if it is a wedge or hash and its origin is the atom in the
      // main bond

      if ((bondDir == Bond::BEGINWEDGE || bondDir == Bond::BEGINDASH) &&
          atomAndBondVecs[whichEnd].second[whichBond]->getBeginAtom() ==
              atomAndBondVecs[whichEnd].first &&
          canHaveDirection(*bond)) {
        useBondsAtEnd[whichEnd].push_back(whichBond);
        foundBondDir = true;
      }
    }
  }

  if (foundBondDir) {
    for (unsigned int whichEnd = 0; whichEnd < 2; ++whichEnd) {
      for (unsigned int whichBondIndex = 0;
           whichBondIndex < useBondsAtEnd[whichEnd].size(); ++whichBondIndex) {
        atomAndBondVecs[whichEnd]
            .second[useBondsAtEnd[whichEnd][whichBondIndex]]
            ->setBondDir(getBondDirForAtropisomerNoConf(
                bond->getStereo(), whichEnd,
                useBondsAtEnd[whichEnd][whichBondIndex]));
      }
    }

    return true;
  }

  // did not find a good bond dir - pick one to use
  // we would like to have one that is not in a ring, and will be a wedge

  const RingInfo *ri = bond->getOwningMol().getRingInfo();

  int bestBondEnd = -1, bestBondNumber = -1;
  bool bestBondIsSingle = false;
  unsigned int bestRingCount = INT_MAX;
  Bond::BondDir bestBondDir = Bond::BondDir::NONE;
  for (unsigned int whichEnd = 0; whichEnd < 2; ++whichEnd) {
    for (unsigned int whichBond = 0;
         whichBond < atomAndBondVecs[whichEnd].second.size(); ++whichBond) {
      auto bondToTry = atomAndBondVecs[whichEnd].second[whichBond];

      if (!canHaveDirection(*bondToTry) ||
          wedgeBonds.find(bondToTry->getIdx()) != wedgeBonds.end()) {
        continue;  // must be a single OR aromatic bond and not already
                   // spoken for by a chiral center
      }

      if (bondToTry->getBondDir() != Bond::BondDir::NONE) {
        if (bondToTry->getBeginAtom()->getIdx() ==
            atomAndBondVecs[whichEnd].first->getIdx()) {
          BOOST_LOG(rdWarningLog)
              << "Wedge or hash bond found on atropisomer where not expected - atoms are: "
              << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx()
              << std::endl;
          return false;
        } else {
          continue;  // wedge or hash bond affecting the OTHER atom
                     // = perhaps a chiral center
        }
      }
      auto ringCount = ri->numBondRings(bondToTry->getIdx());
      if (ringCount > bestRingCount) {
        continue;
      }

      else if (ringCount < bestRingCount) {
        bestBondEnd = whichEnd;
        bestBondNumber = whichBond;
        bestRingCount = ringCount;
        bestBondIsSingle = (bondToTry->getBondType() == Bond::BondType::SINGLE);
        bestBondDir = getBondDirForAtropisomerNoConf(bond->getStereo(),
                                                     whichEnd, whichBond);
      } else if (bestBondIsSingle &&
                 bondToTry->getBondType() != Bond::BondType::SINGLE) {
        continue;

      } else if (!bestBondIsSingle &&
                 bondToTry->getBondType() == Bond::BondType::SINGLE) {
        bestBondEnd = whichEnd;
        bestBondNumber = whichBond;
        bestRingCount = ringCount;
        bestBondIsSingle = true;
        bestBondDir = getBondDirForAtropisomerNoConf(bond->getStereo(),
                                                     whichEnd, whichBond);

      } else {
        auto bondDir = getBondDirForAtropisomerNoConf(bond->getStereo(),
                                                      whichEnd, whichBond);
        if (bestBondDir == Bond::BondDir::NONE ||
            (bestBondDir == Bond::BondDir::BEGINDASH &&
             bondDir == Bond::BondDir::BEGINWEDGE)) {
          bestBondEnd = whichEnd;
          bestBondNumber = whichBond;
          bestRingCount = ringCount;
          bestBondIsSingle =
              (bondToTry->getBondType() == Bond::BondType::SINGLE);
          bestBondDir = bondDir;
        }
      }
    }
  }

  if (bestBondEnd >= 0)  // we found a good one
  {
    // make sure the atoms on the bond are in the right order for the
    // wedge/hash the atom on the end of the main bond must be listed
    // first for the wedge/has bond

    auto bestBond = atomAndBondVecs[bestBondEnd].second[bestBondNumber];
    if (bestBond->getBeginAtom() != atomAndBondVecs[bestBondEnd].first) {
      bestBond->setEndAtom(bestBond->getBeginAtom());
      bestBond->setBeginAtom(atomAndBondVecs[bestBondEnd].first);
    }

    bestBond->setBondDir(bestBondDir);

    auto newWedgeInfo = std::unique_ptr<RDKit::Chirality::WedgeInfoBase>(
        new RDKit::Chirality::WedgeInfoAtropisomer(bond->getIdx(),
                                                   bestBondDir));
    wedgeBonds[bestBond->getIdx()] = std::move(newWedgeInfo);
  } else {
    BOOST_LOG(rdWarningLog)
        << "Failed to find a good bond to set as UP or DOWN for an atropisomer - atoms are: "
        << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx() << std::endl;
    return false;
  }

  return true;
}

bool WedgeBondFromAtropisomerOneBond2d(
    Bond *bond, const ROMol &mol, const Conformer *conf,
    std::map<int, std::unique_ptr<RDKit::Chirality::WedgeInfoBase>>
        &wedgeBonds) {
  PRECONDITION(bond, "no bond");

  AtropAtomAndBondVec atomAndBondVecs[2];
  if (!getAtropisomerAtomsAndBonds(bond, atomAndBondVecs, mol)) {
    return false;  // not an atropisomer
  }

  //  make sure we do not have wiggle bonds

  for (auto atomAndBondVec : atomAndBondVecs) {
    for (auto endBond : atomAndBondVec.second) {
      if (endBond->getBondDir() == Bond::UNKNOWN) {
        return false;  // not an atropisomer)
      }
    }
  }

  // create a frame of reference that has its X-axis along the atrop bond

  RDGeom::Point3D xAxis, yAxis, zAxis;

  if (!getBondFrameOfReference(bond, conf, xAxis, yAxis, zAxis)) {
    // connot percieve atroisomer bond

    BOOST_LOG(rdWarningLog)
        << "Cound not get a frame of reference for an atropisomer bond - atoms are: "
        << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx() << std::endl;
    return false;
  }

  RDGeom::Point3D bondVecs[2];  // one bond vector from each end of the
                                // potential atropisome bond

  for (int bondAtomIndex = 0; bondAtomIndex < 2; ++bondAtomIndex) {
    // find a vector to represent the lowest numbered atom on each end
    // this vector is NOT the bond vector, but is y-value in the bond
    // frame or reference

    if (!getAtropIsomerEndVect(atomAndBondVecs[bondAtomIndex], yAxis, zAxis,
                               conf, bondVecs[bondAtomIndex])) {
      return false;
    }

    if (bondVecs[bondAtomIndex].length() < REALLY_SMALL_BOND_LEN) {
      // did not find a non-colinear bond

      BOOST_LOG(rdWarningLog)
          << "Failed to get a representative vector for the defining bond of an atropisomer - atoms are: "
          << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx()
          << std::endl;
      return false;
    }
  }

  // first see if any candidate bond is already set to a wedge or hash
  // if so, we will use that bond as a wedge or hash

  std::vector<int> useBondsAtEnd[2];
  bool foundBondDir = false;

  for (unsigned int whichEnd = 0; whichEnd < 2; ++whichEnd) {
    for (unsigned int whichBond = 0;
         whichBond < atomAndBondVecs[whichEnd].second.size(); ++whichBond) {
      auto bondDir = atomAndBondVecs[whichEnd].second[whichBond]->getBondDir();

      // see if it is a wedge or hash and its origin is the atom in the
      // main bond

      if ((bondDir == Bond::BEGINWEDGE || bondDir == Bond::BEGINDASH) &&
          atomAndBondVecs[whichEnd].second[whichBond]->getBeginAtom() ==
              atomAndBondVecs[whichEnd].first &&
          canHaveDirection(*bond)) {
        useBondsAtEnd[whichEnd].push_back(whichBond);
        foundBondDir = true;
      }
    }
  }

  if (foundBondDir) {
    for (unsigned int whichEnd = 0; whichEnd < 2; ++whichEnd) {
      for (unsigned int whichBondIndex = 0;
           whichBondIndex < useBondsAtEnd[whichEnd].size(); ++whichBondIndex) {
        atomAndBondVecs[whichEnd]
            .second[useBondsAtEnd[whichEnd][whichBondIndex]]
            ->setBondDir(getBondDirForAtropisomer2d(
                bondVecs, bond->getStereo(), whichEnd,
                useBondsAtEnd[whichEnd][whichBondIndex]));
      }
    }

    return true;
  }

  // did not find a good bond dir - pick one to use
  // we would like to have one that is in a ring, and will favor it being a
  // wedge

  // We favor rings here because wedging non-ring bonds makes it too likely that
  // we'll end up accidentally creating new atropisomeric bonds. This was github
  // issue 7371

  const RingInfo *ri = bond->getOwningMol().getRingInfo();

  int bestBondEnd = -1, bestBondNumber = -1;
  bool bestBondIsSingle = false;
  unsigned int bestRingCount = INT_MAX;
  unsigned int largestRingSize = 0;
  Bond::BondDir bestBondDir = Bond::BondDir::NONE;
  for (unsigned int whichEnd = 0; whichEnd < 2; ++whichEnd) {
    for (unsigned int whichBond = 0;
         whichBond < atomAndBondVecs[whichEnd].second.size(); ++whichBond) {
      auto bondToTry = atomAndBondVecs[whichEnd].second[whichBond];

      if (!canHaveDirection(*bondToTry) ||
          wedgeBonds.find(bondToTry->getIdx()) != wedgeBonds.end()) {
        continue;  // must be a single OR aromatic bond and not already
                   // spoken for by a chiral center
      }

      if (bondToTry->getBondDir() != Bond::BondDir::NONE) {
        if (bondToTry->getBeginAtom()->getIdx() ==
            atomAndBondVecs[whichEnd].first->getIdx()) {
          if (bondToTry->getBondDir() == Bond::BEGINWEDGE ||
              bondToTry->getBondDir() == Bond::BEGINDASH) {
            BOOST_LOG(rdWarningLog)
                << "Wedge or hash bond found on atropisomer where not expected - atoms are: "
                << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx()
                << std::endl;
            return false;
          } else {
            continue;  // probably a slash up or down for a double bond
          }
        } else {
          continue;  // wedge or hash bond affecting the OTHER atom
                     // = perhaps a chiral center
        }
      }
      auto ringCount = ri->numBondRings(bondToTry->getIdx());
      unsigned int ringSize = 0;
      if (!ringCount) {
        ringCount = 10;
      } else {
        // we're going to prefer to put wedges in larger rings, but don't want
        // to end up wedging macrocyles if it's avoidable.
        ringSize = ri->minBondRingSize(bondToTry->getIdx());
        if (ringSize > 8) {
          ringSize = 0;
        }
      }
      if (ringCount > bestRingCount) {
        continue;
      } else if (ringCount < bestRingCount || ringSize > largestRingSize) {
        bestBondEnd = whichEnd;
        bestBondNumber = whichBond;
        bestRingCount = ringCount;
        largestRingSize = ringSize;
        bestBondIsSingle = (bondToTry->getBondType() == Bond::BondType::SINGLE);
        bestBondDir = getBondDirForAtropisomer2d(bondVecs, bond->getStereo(),
                                                 whichEnd, whichBond);
      } else if (bestBondIsSingle &&
                 bondToTry->getBondType() != Bond::BondType::SINGLE) {
        continue;

      } else if (!bestBondIsSingle &&
                 bondToTry->getBondType() == Bond::BondType::SINGLE) {
        bestBondEnd = whichEnd;
        bestBondNumber = whichBond;
        bestRingCount = ringCount;
        bestBondIsSingle = true;
        bestBondDir = getBondDirForAtropisomer2d(bondVecs, bond->getStereo(),
                                                 whichEnd, whichBond);

      } else {
        auto bondDir = getBondDirForAtropisomer2d(bondVecs, bond->getStereo(),
                                                  whichEnd, whichBond);
        if (bestBondDir == Bond::BondDir::NONE ||
            (bestBondDir == Bond::BondDir::BEGINDASH &&
             bondDir == Bond::BondDir::BEGINWEDGE)) {
          bestBondEnd = whichEnd;
          bestBondNumber = whichBond;
          bestRingCount = ringCount;
          bestBondIsSingle =
              (bondToTry->getBondType() == Bond::BondType::SINGLE);
          bestBondDir = bondDir;
        }
      }
    }
  }

  if (bestBondEnd >= 0) {
    // we found a good one
    // make sure the atoms on the bond are in the right order for the
    // wedge/hash the atom on the end of the main bond must be listed
    // first for the wedge/has bond

    auto bestBond = atomAndBondVecs[bestBondEnd].second[bestBondNumber];
    if (bestBond->getBeginAtom() != atomAndBondVecs[bestBondEnd].first) {
      bestBond->setEndAtom(bestBond->getBeginAtom());
      bestBond->setBeginAtom(atomAndBondVecs[bestBondEnd].first);
    }
    bestBond->setBondDir(bestBondDir);

    auto newWedgeInfo = std::unique_ptr<RDKit::Chirality::WedgeInfoBase>(
        new RDKit::Chirality::WedgeInfoAtropisomer(bond->getIdx(),
                                                   bestBondDir));
    wedgeBonds[bestBond->getIdx()] = std::move(newWedgeInfo);

  } else {
    BOOST_LOG(rdWarningLog)
        << "Failed to find a good bond to set as UP or DOWN for an atropisomer - atoms are: "
        << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx() << std::endl;
    return false;
  }

  return true;
}

bool WedgeBondFromAtropisomerOneBond3d(
    Bond *bond, const ROMol &mol, const Conformer *conf,
    std::map<int, std::unique_ptr<RDKit::Chirality::WedgeInfoBase>>
        &wedgeBonds) {
  PRECONDITION(bond, "bad bond");

  AtropAtomAndBondVec atomAndBondVecs[2];
  if (!getAtropisomerAtomsAndBonds(bond, atomAndBondVecs, mol)) {
    return false;  // not an atropisomer
  }

  //  make sure we do not have wiggle bonds

  for (auto atomAndBondVecs : atomAndBondVecs) {
    for (auto endBond : atomAndBondVecs.second) {
      if (endBond->getBondDir() == Bond::UNKNOWN) {
        return false;  // not an atropisomer)
      }
    }
  }

  // first see if any candidate bond is already set to a wedge or hash
  // if so, we will use that bond as a wedge or hash

  std::vector<Bond *> useBonds;

  for (unsigned int whichEnd = 0; whichEnd < 2; ++whichEnd) {
    for (unsigned int whichBond = 0;
         whichBond < atomAndBondVecs[whichEnd].second.size(); ++whichBond) {
      auto bond = atomAndBondVecs[whichEnd].second[whichBond];
      auto bondDir = bond->getBondDir();

      // see if it is a wedge or hash and its origin is the atom in the
      // main bond

      if ((bondDir == Bond::BEGINWEDGE || bondDir == Bond::BEGINDASH) &&
          bond->getBeginAtom() == atomAndBondVecs[whichEnd].first &&
          canHaveDirection(*bond)) {
        useBonds.push_back(bond);
      }
    }
  }

  // the following may seem redundant, since we just found the useBonds
  // based on their bond dir PRESENCE, but this endures that the values are
  // correct.

  if (useBonds.size() > 0) {
    for (auto useBond : useBonds) {
      useBond->setBondDir(getBondDirForAtropisomer3d(useBond, conf));
    }

    return true;
  }

  // did not find a used bond dir - pick one to use
  // we would like to have one that is not in a ring, and will be a dash

  const RingInfo *ri = bond->getOwningMol().getRingInfo();

  Bond *bestBond = nullptr;
  int bestBondEnd = -1;
  unsigned int bestRingCount = UINT_MAX;
  unsigned int largestRingSize = 0;
  Bond::BondDir bestBondDir = Bond::BondDir::NONE;
  bool bestBondIsSingle = false;
  for (unsigned int whichEnd = 0; whichEnd < 2; ++whichEnd) {
    for (unsigned int whichBond = 0;
         whichBond < atomAndBondVecs[whichEnd].second.size(); ++whichBond) {
      auto bondToTry = atomAndBondVecs[whichEnd].second[whichBond];

      // cannot use a bond that is not single, nor if it is already slated
      // to be used for a chiral center

      if (!canHaveDirection(*bondToTry) ||
          wedgeBonds.find(bond->getIdx()) != wedgeBonds.end()) {
        continue;  // must be a single bond and not already spoken
                   // for by a chiral center
      }

      // make sure the atoms on the bond are in the right order for the
      // wedge/hash the atom on the end of the main bond must be listed
      // first

      if (bondToTry->getBeginAtom() != atomAndBondVecs[whichEnd].first) {
        bondToTry->setEndAtom(bondToTry->getBeginAtom());
        bondToTry->setBeginAtom(atomAndBondVecs[whichEnd].first);
      }

      if (bondToTry->getBondDir() != Bond::BondDir::NONE) {
        if (bondToTry->getBeginAtom()->getIdx() ==
            atomAndBondVecs[whichEnd].first->getIdx()) {
          BOOST_LOG(rdWarningLog)
              << "Wedge or hash bond found on atropisomer where not expected - atoms are: "
              << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx()
              << std::endl;
          return false;
        } else {
          continue;  // wedge or hash bond affecting the OTHER atom
                     // = perhaps a chiral center
        }
      }
      auto ringCount = ri->numBondRings(bondToTry->getIdx());
      unsigned int ringSize = 0;
      if (!ringCount) {
        ringCount = 10;
      } else {
        // we're going to prefer to put wedges in larger rings, but don't want
        // to end up wedging macrocyles if it's avoidable.
        ringSize = ri->minBondRingSize(bondToTry->getIdx());
        if (ringSize > 8) {
          ringSize = 0;
        }
      }
      if (ringCount > bestRingCount) {
        continue;
      } else if (ringCount < bestRingCount || ringSize > largestRingSize) {
        bestBond = bondToTry;
        bestBondEnd = whichEnd;
        bestRingCount = ringCount;
        largestRingSize = ringSize;
        bestBondIsSingle = (bondToTry->getBondType() == Bond::BondType::SINGLE);
        bestBondDir = getBondDirForAtropisomer3d(bondToTry, conf);
      } else if (bestBondIsSingle &&
                 bondToTry->getBondType() != Bond::BondType::SINGLE) {
        continue;
      } else if (!bestBondIsSingle &&
                 bondToTry->getBondType() == Bond::BondType::SINGLE) {
        bestBondEnd = whichEnd;
        bestBond = bondToTry;
        bestRingCount = ringCount;
        bestBondIsSingle = true;
        bestBondDir = getBondDirForAtropisomer3d(bondToTry, conf);
      } else {
        auto bondDir = getBondDirForAtropisomer3d(bondToTry, conf);
        if (bestBondDir == Bond::BondDir::NONE ||
            (bestBondDir == Bond::BondDir::BEGINDASH &&
             bondDir == Bond::BondDir::BEGINWEDGE)) {
          bestBond = bondToTry;
          bestBondEnd = whichEnd;
          bestRingCount = ringCount;
          bestBondIsSingle =
              (bondToTry->getBondType() == Bond::BondType::SINGLE);

          bestBondDir = bondDir;
        }
      }
    }
  }

  if (bestBond != nullptr) {
    // we found a good one

    // make sure the atoms on the bond are in the right order for the
    // wedge/hash the atom on the end of the main bond must be listed
    // first for the wedge/has bond

    if (bestBond->getBeginAtom() != atomAndBondVecs[bestBondEnd].first) {
      bestBond->setEndAtom(bestBond->getBeginAtom());
      bestBond->setBeginAtom(atomAndBondVecs[bestBondEnd].first);
    }
    bestBond->setBondDir(bestBondDir);
    auto newWedgeInfo = std::unique_ptr<RDKit::Chirality::WedgeInfoBase>(
        new RDKit::Chirality::WedgeInfoAtropisomer(bond->getIdx(),
                                                   bestBondDir));

    wedgeBonds[bestBond->getIdx()] = std::move(newWedgeInfo);
  } else {
    BOOST_LOG(rdWarningLog)
        << "Failed to find a good bond to set as UP or DOWN for an atropisomer - atoms are: "
        << bond->getBeginAtomIdx() << " " << bond->getEndAtomIdx() << std::endl;
    return false;
  }

  return true;
}

void wedgeBondsFromAtropisomers(
    const ROMol &mol, const Conformer *conf,
    std::map<int, std::unique_ptr<RDKit::Chirality::WedgeInfoBase>>
        &wedgeBonds) {
  PRECONDITION(conf == nullptr || &(conf->getOwningMol()) == &mol,
               "conformer does not belong to molecule");

  // WedgeBondFromAtropisomerOneBond 2d/3d requires ring bond counts
  if (!mol.getRingInfo()->isSssrOrBetter()) {
    RDKit::MolOps::findSSSR(mol);
  }

  for (auto bond : mol.bonds()) {
    auto bondStereo = bond->getStereo();

    if (bond->getBondType() != Bond::BondType::SINGLE ||
        (bondStereo != Bond::BondStereo::STEREOATROPCW &&
         bondStereo != Bond::BondStereo::STEREOATROPCCW) ||
        bond->getBeginAtom()->getTotalDegree() < 2 ||
        bond->getEndAtom()->getTotalDegree() < 2 ||
        bond->getBeginAtom()->getTotalDegree() > 3 ||
        bond->getEndAtom()->getTotalDegree() > 3) {
      continue;
    }

    if (conf) {
      if (conf->is3D()) {
        WedgeBondFromAtropisomerOneBond3d(bond, mol, conf, wedgeBonds);
      } else {
        WedgeBondFromAtropisomerOneBond2d(bond, mol, conf, wedgeBonds);
      }
    } else {  // no conformer
      WedgeBondFromAtropisomerOneBondNoConf(bond, mol, wedgeBonds);
    }
  }
}

bool doesMolHaveAtropisomers(const ROMol &mol) {
  for (auto bond : mol.bonds()) {
    auto bondStereo = bond->getStereo();

    if (bondStereo == Bond::BondStereo::STEREOATROPCW ||
        bondStereo == Bond::BondStereo::STEREOATROPCCW) {
      return true;
    }
  }
  return false;
}
}  // namespace Atropisomers
}  // namespace RDKit