File: pyrogen.py

package info (click to toggle)
coot 1.1.15%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 198,064 kB
  • sloc: cpp: 487,366; python: 34,637; ansic: 26,046; lisp: 22,737; sh: 13,097; makefile: 2,663; awk: 441; xml: 188; csh: 14
file content (1214 lines) | stat: -rw-r--r-- 50,105 bytes parent folder | download
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
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
# Copyright 2012 by the University of Oxford
# Copyright 2014, 2015 by Medical Research Council
# Author: Paul Emsley
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA

from __future__ import print_function

"""
pyrogen contains tools for converting molecules to mmcif restraints dictionaries,
utilities for retrival, extraction and depction.
"""

import sys
import os
import copy
from subprocess import call
from rdkit import Chem
from rdkit.Chem import AllChem

import coot_git

# Hello from 20170701, library resolution problems?
# $ otool -L .libs/_pyrogen_swig.so -> @rpath substition... how does that work?
import pyrogen_swig as pysw # relies on above rdkit.Chem import AllChem (but ideally should not)
import pyrogen_boost        # ditto
import atom_types

from optparse import OptionParser

import tautomer

import urllib

from jay_util import *

global pyrogen_version
pyrogen_version = "0.0-pre"

global run_mogul
global smiles_dict
run_mogul = False
smiles_dict = False

def make_mogul_ins_file(mogul_ins_file_name, mogul_out_file_name, sdf_file_name):
   f = open(mogul_ins_file_name, 'w')
   if f:
     f.write('mogul molecule file ')
     f.write(sdf_file_name)
     f.write('\n')
     f.write('mogul output   file ')
     f.write(mogul_out_file_name)
     f.write('\n')
     f.write('mogul output distribution all on\n')
     f.write('bond all\n')
     f.write('angle all\n')
#      f.write('torsion all\n')
#      f.write('ring all\n')
     f.write('config output format CSV\n')
     f.write('config output items fragment_type atom_indices query_value nhits mean median sd z-score dmin\n')
     f.write('config search all filter exclude_solvents\n')
     f.write('config output invalid_fragments exclude\n')
     f.close()
   return f


# return True for good, False for bad/not-run
#
def execute_mogul(sdf_file_name, mogul_ins_file_name, mogul_out_file_name):
   f = make_mogul_ins_file(mogul_ins_file_name, mogul_out_file_name, sdf_file_name)
   if f:
      # print 'now run mogul using ins file %s' % mogul_ins_file_name
      if run_mogul:
          state = call(['mogul', '-ins', mogul_ins_file_name])
          return (state == 0)
      else:
          return False
   else:
      return False

def atom_name_from_atomic_number_and_count(element, count, inc):
    name = element
    name += str(count+inc)
    return name

def add_atom_names(mol):
    nz = {}
    atom_names = []
    for atom in mol.GetAtoms():
       try:
          n = atom.GetProp('name')
          atom_names.append(n)
       except KeyError:

          # we want to generate a name that is not already in the atom_names list

          z = atom.GetAtomicNum()
          if z in nz:
             nz[z]  = nz[z] + 1
          else:
             nz[z] = 1;
          ele = atom.GetSymbol().upper()
          # we add inc argument, which gets added to count (nz[z]) in case that we
          # already have a name that matches (previous) return from call to
          # atom_name_from_atomic_number_and_count()
          #
          inc = 0
          name = atom_name_from_atomic_number_and_count(ele, nz[z], inc)
          p_name = pad_atom_name(name, ele)
          # print('c.f.', name, " atom names", atom_names)
          while p_name in atom_names :
             inc += 1
             name = atom_name_from_atomic_number_and_count(ele, nz[z], inc)
             p_name = pad_atom_name(name, ele)
          # print(atom, 'made-name', p_name, ":")

          atom.SetProp("name", p_name)
          atom_names.append(p_name)
    return atom_names

def convert_to_coot_bond_type(rdkit_type):
    out_type = 'single'
    if (rdkit_type == Chem.rdchem.BondType.SINGLE):
        out_type = 'single'
    if (rdkit_type == Chem.rdchem.BondType.AROMATIC):
        out_type = 'aromatic'
    if (rdkit_type == Chem.rdchem.BondType.DOUBLE):
        out_type = 'double'
    if (rdkit_type == Chem.rdchem.BondType.TRIPLE):
        out_type = 'triple'
    if (rdkit_type == Chem.rdchem.BondType.ONEANDAHALF):
        out_type = 'deloc'
    return out_type

def pad_atom_name(name, element):

    padded = name
    if (len(element) == 1):
        if (len(name) == 2):
            padded = ' ' + name + ' '
        if (len(name) == 3):
            padded = ' ' + name
    if (len(element) == 2):
        if (len(name) == 2):
            padded = name + '  '
        if (len(name) == 3):
            padded = name + ' '
    return padded

def is_smiles_file(file_name):
    bits = file_name.rsplit(".")
    if len(bits) > 1:
        return bits[1] == 'smi'
    else:
        return False

def is_comp_id(comp_id):
   return len(comp_id) == 3

def is_mdl_file(file_name):
    bits = file_name.rsplit(".")
    if (len(bits) < 2):
       return False
    else:
       idx = len(bits) - 1
       if (bits[idx] == 'mol'):
          return True
       else:
          if (bits[idx] == 'mdl'):
             return True
          else:
             return False


# return the contents of file_name
def read_file(file_name):
    f = open(file_name)
    r = f.read()
    f.close()
    return r


# return False or a file_name
#
# downloaded file is put in the CCD directory as CCD/x/xyz.cif
#
def get_pdbe_cif_for_comp_id(comp_id):

   try:
      url = 'ftp://ftp.ebi.ac.uk/pub/databases/msd/pdbechem/files/mmcif/' + comp_id + '.cif'
      url = 'https://www.ebi.ac.uk/pdbe/static/files/pdbechem_v2/'        + comp_id + '.cif'
      CCD_dir = 'CCD'
      # file_name = "PDBe-" + comp_id + ".cif"
      first_char = comp_id[0]
      try:
         sub_dir = os.path.join(CCD_dir, first_char)
         file_name = os.path.join(sub_dir, comp_id + ".cif")
         if not os.path.isdir(CCD_dir):
            os.mkdir(CCD_dir)
         if not os.path.isdir(sub_dir):
            os.mkdir(sub_dir)
         if os.path.isfile(file_name):
            return file_name
         else:
            url = 'ftp://ftp.ebi.ac.uk/pub/databases/msd/pdbechem/files/mmcif/' + comp_id + '.cif'
            url = 'https://www.ebi.ac.uk/pdbe/static/files/pdbechem_v2/'        + comp_id + '.cif'
            status = urllib.urlretrieve(url, file_name)
            print('urllib.urllib returned with status', status)
            return file_name

      except OSError as e:
         print(e)
         print("Failed: Can't ftp from", url, "and write file", file_name)

   except IOError as e:
      print(e)
      print("Failed: Can't ftp from", url, "and write file", file_name)

def fetch(comp_id):
    return get_pdbe_cif_for_comp_id(comp_id)

def MolFromFetchedCode(code):
   f = get_pdbe_cif_for_comp_id(code)
   m = pyrogen_boost.MolFromPDBXr(f, code)
   m.Compute2DCoords()
   return m

def make_restraints_for_bond_orders(mol):
    restraints = {}
    bond_list = []
    for bond in mol.GetBonds():
        type = bond.GetBondType()
        coot_bond_type = convert_to_coot_bond_type(type)
        at_1 = bond.GetBeginAtom()
        at_2 = bond.GetEndAtom()
        name_1 = at_1.GetProp('name')
        name_2 = at_2.GetProp('name')
        item = [name_1, name_2, coot_bond_type, 1.0, 1.0]
        bond_list.append(item)
    restraints['_chem_comp_bond'] = bond_list
    restraints['_chem_comp'] = [mol.GetProp('comp_id'),
                                mol.GetProp('comp_id'),
                                mol.GetProp('name'),
                                'non-polymer',
                                mol.GetNumAtoms(),
                                mol.GetNumAtoms(),
                                '.']
    return restraints


# return True if mogul is not run or mogul exe is in place.
# return False if mogul is expected but not found.
def test_for_mogul():
   global run_mogul
   if run_mogul:
      mogol_exe = which('mogul')
      if (mogol_exe == None):
         print("mogul not found in path")
         print("Try: pyrogen --no-mogul")
         return False
      else:
         return True
   else:
      return True # OK, really

# this can throw a TypeError
#
def get_smiles_from_comp_id(comp_id):
   global smiles_dict
   if (not smiles_dict):
      read_smiles_tab('smiles.tab')
   return smiles_dict[comp_id]

# return a dictionary or False (if the file does not exist)
# (can this go inside get_smiles_from_comp_id?)
#
def read_smiles_tab(file_name):
    global smiles_dict
    try:
       smiles_dict = {}
       f = open(file_name)
       lines = f.readlines()
       for line in lines:
           bits = line.rstrip().rsplit()
           smiles_dict[bits[0]] = bits[2]
       f.close()
       return True
    except IOError as e:
       smiles_dict = True # we've tested for it
       return False

# return a pair, the smiles string and the molecule name (which might be blank)
#
def get_smiles_from_file(file_name):
    if not os.path.exists(file_name):
       return False,False
    else:
        f = open(file_name)
        smi_line = f.readline()
        parts = smi_line.split()
        f.close()
        return parts[0], ' '.join(parts[1:])

def make_picture(mol, conf_id, comp_id, output_postfix):

   output_file_name = comp_id + "-" + output_postfix + '.png'
   make_picture_to_file(mol, conf_id, output_file_name)

def make_picture_to_file(mol, conf_id, output_file_name):

   # need to install Pillow (I did it with pip)

   try:
      from rdkit.Chem import Draw
      # The import of Image may change depending on how it was provided.
      # What about pillow? Hmm. Not sure of the details.
      # import Image
      from PIL import Image as Image
      state = Draw.MolToFile(mol, size=(300,300), fileName=output_file_name, confId=conf_id)
      # print 'INFO:: wrote PNG   "' + output_file_name + '"'

      # img = Draw.MolToImage(mol, fitImage=True, size=(900,900))
      # img2 = img.resize((300, 300), Image.ANTIALIAS)
      # img2.save(output_file_name + "resampled.png")

      # testing MolDraw2D code (squiggly mess)
      if True:
         conf_id = AllChem.Compute2DCoords(mol)
         drawer = Draw.MolDraw2DCairo(300,300)
         drawer.DrawMolecule(mol, confId=conf_id)
         drawer.FinishDrawing()
         # c = drawer.GetDrawingText()
   except ImportError as e:
      print('ImportError:', e)
   except ValueError as e:
      print('ValueError in make_picture():', e)

def make_restraints_from_smiles(smiles_string, comp_id, compound_name, mogul_dir, name_stub, pdb_out_file_name, mmcif_dict_name, quartet_planes, quartet_hydrogen_planes, use_mmff, match_atom_names_to_dict_flag, comp_id_list_for_names_match, dict_file_for_names_match):

   if not test_for_mogul():
      # return False
      exit(1)
   m = Chem.MolFromSmiles(smiles_string)
   if compound_name:
       m.SetProp('_Name', compound_name)
   do_hydrogen_atoms_shift = True
   do_minimization = True
   embed_and_optimize = True
   return make_restraints(m, comp_id, mogul_dir, name_stub, pdb_out_file_name, mmcif_dict_name, quartet_planes, quartet_hydrogen_planes, use_mmff, do_minimization, embed_and_optimize, match_atom_names_to_dict_flag, comp_id_list_for_names_match, dict_file_for_names_match, do_hydrogen_atoms_shift)

# return the molecule and return value from make_restraints
#
def make_restraints_from_mdl(mol_file_name, comp_id, mogul_dir, name_stub, pdb_out_file_name, mmcif_dict_name, quartet_planes, quartet_hydrogen_planes, use_mmff, match_atom_names_to_dict_flag, comp_id_list_for_names_match, dict_files_for_names_match):

   if (not (test_for_mogul())):
      # return False, False
      exit(1)

   if not os.path.exists(mol_file_name):
      print("No such file:", mol_file_name)
      exit(1)

   do_hydrogen_atoms_shift = False
   compound_name = '.'
   m = Chem.MolFromMolFile(mol_file_name)
   do_minimization = True
   embed_and_optimize = True
   return m, make_restraints(m, comp_id, mogul_dir, name_stub, pdb_out_file_name, mmcif_dict_name,
                             quartet_planes, quartet_hydrogen_planes, use_mmff, do_minimization, embed_and_optimize,
                             match_atom_names_to_dict_flag, comp_id_list_for_names_match,
                             dict_files_for_names_match, do_hydrogen_atoms_shift)


# return a list of (mol, comp_id) pairs for every ligand in the cif
# file.  Often only one of course.
#
def make_restraints_from_mmcif_dict(cif_file_name_in, comp_id, mogul_dir,
                                    output_dir, output_postfix,
                                    quartet_planes, quartet_hydrogen_planes, use_mmff, do_minimization,
                                    pdb_out_file_name, mmcif_restraints_out_file_name,
                                    do_hydrogen_atoms_shift):

   if not test_for_mogul():
       return [(None, None)]

   if comp_id == "TRY_ALL_COMP_IDS":
      types = pysw.types_from_mmcif_dictionary(cif_file_name_in)
      l = []
      for r_type in types:

         file_name_stub = r_type + "-" + output_postfix
         if options.output_dir != ".":
            file_name_stub = os.path.join(options.output_dir, file_name_stub)

         pdb_out_file_name_local              = file_name_stub + ".pdb"
         mmcif_restraints_out_file_name_local = file_name_stub + ".cif"
         #
         t_mol = make_restraints_from_mmcif_dict_single(cif_file_name_in, r_type, mogul_dir,
                                                        output_postfix,
                                                        quartet_planes,
                                                        quartet_hydrogen_planes, use_mmff, do_minimization,
                                                        pdb_out_file_name_local,
                                                        mmcif_restraints_out_file_name_local,
                                                        do_hydrogen_atoms_shift)
         l.append((t_mol, r_type))
      return l
   else:
       # just the one
       m = make_restraints_from_mmcif_dict_single(cif_file_name_in, comp_id, mogul_dir, output_postfix,
                                                  quartet_planes, quartet_hydrogen_planes, use_mmff, do_minimization,
                                                  pdb_out_file_name, mmcif_restraints_out_file_name,
                                                  do_hydrogen_atoms_shift)
       return [(m, comp_id)]

# return a mol, given a sensible comp_id.
#
# Return None on failure
#
def make_restraints_from_mmcif_dict_single(cif_file_name_in, comp_id, mogul_dir, output_postfix,
                                           quartet_planes, quartet_hydrogen_planes, use_mmff, do_minimization,
                                           pdb_out_file_name, mmcif_restraints_out_file_name,
                                           do_hydrogen_atoms_shift):

   if not test_for_mogul():
       return [(None, None)]

   mogul_file_name_stub = comp_id + '-' + output_postfix # file component of files within mogul_dir

   m = pyrogen_boost.rdkit_mol_chem_comp_pdbx(cif_file_name_in, comp_id)

   if False:  # debugging
      for atom in m.GetAtoms():
         try:
            name    = atom.GetProp('name')
            cipcode = atom.GetProp('_CIPCode')
            print('DEBUG:: make_restraints_from_mmcif_dict_single atom', \
               atom, 'name', name, 'cip-code ', cipcode)
         except KeyError as e:
            print('DEBUG:: pyrogen.py:: make_restraints_from_mmcif_dict_single atom', \
               atom, " with name ", name, ' has no _CIPCode property')
         try:
            r = atom.GetProp('_CIPRank')
            print(atom, "DEBUG:: pyrogen.py:: make_restraints_from_mmcif_dict_single atom cip rank", r)
         except KeyError as e:
            print('DEBUG:: pyrogen.py:: make_restraints_from_mmcif_dict_single atom', \
               atom, "has no _CIPRank")
            pass

   if False:  # debug
      n = m.GetNumConformers()
      print("debug in make_restraints_from_mmcif_dict_single() n conformers:", n)
      if n == 1:
         conf_id = 0
         conf = m.GetConformer(conf_id)
         print(dir(conf))
         n_atoms = conf.GetNumAtoms()
         positions = conf.GetPositions()
         for pos in positions:
            print("debug::   post-rdkit_mol_chem_comp_pdbx()", pos)

   # maybe user didn't select the correct comp_id for the given dictionary mmcif
   if m.GetNumAtoms() == 0:
      print('No atoms for comp_id', comp_id)
      return False
   else :

      name = ''
      try:
         name = m.GetProp('_Name')
      except KeyError:
         print('caught KeyError in make_restraints_from_mmcif_dict_single() trying GetProp _Name')

      embed_and_optimize = False # start from the given coordinates
      # print('############################ make_restraints_from_mmcif_dict_single() calling make_restraints()')
      # print('############################ make_restraints_from_mmcif_dict_single() with args ',
      #       use_mmff, do_minimization, embed_and_optimize)
      return make_restraints(m, comp_id, mogul_dir, mogul_file_name_stub,
                             pdb_out_file_name, mmcif_restraints_out_file_name,
                             quartet_planes, quartet_hydrogen_planes, use_mmff, do_minimization, embed_and_optimize,
                             False, False, False,
                             do_hydrogen_atoms_shift)


def n_hydrogens(mol):
    n_H = 0
    for atom in mol.GetAtoms():
        if atom.GetAtomicNum() == 1:
            n_H += 1
    return n_H

def checked_mkdir(dirname):
    if not os.path.exists(dirname):
       os.makedirs(dirname)
    else:
       if os.path.isdir(dirname):
          pass # this happens most of the time, I imagine
       else:
          print('Stop:: File', dirname, 'exists but is not a directory')


# return sane_H_mol
#
def make_restraints(m, comp_id, mogul_dir, mogul_file_name_stub, pdb_out_file_name, mmcif_dict_name,
                    quartet_planes, quartet_hydrogen_planes, use_mmff, do_minimization, embed_and_optimize,
                    match_atom_names_to_dict_flag,
                    comp_id_list_for_names_match,
                    dict_files_for_names_match, do_hydrogen_atoms_shift):

   # test here (or in calling functions) if m is sane (i.e. is an rdkit molecule)

   if not isinstance(m, Chem.rdchem.Mol):
      print('ERROR:: not a molecule')
      return False

   n_attempts = 20 * m.GetNumAtoms() # default is 10 * number of atoms.

   # pH-dependent protonation or deprotonation
   #
   # do_hydrogen_atoms_shift = True, now passed as an argument, user configuration

   try:
      compound_name = m.GetProp('_Name');
   except KeyError:
      # this happens all the time when we start from a SMILES, users don't need to see it.
      # print 'caught key error in trying to get _Name in make_restraints() for m'
      compound_name = '.'
   except AttributeError as e:
      # Do we need to see this? Perhaps make_restraints() needs to return a status.
      # print 'AttributeError: problem with molecule in make_restraints()', e, ' on object:', m
      return

   m_H = m
   if n_hydrogens(m) == 0:
       m_H = AllChem.AddHs(m)

   if do_hydrogen_atoms_shift:
      # simple sane pH H-exchanges
      sane_H_mol = pyrogen_boost.hydrogen_transformations(m_H)
      # print >>file('sane_H.mol','w+'),Chem.MolToMolBlock(sane_H_mol)
      AllChem.AssignStereochemistry(sane_H_mol, force=True, cleanIt=True)
   else:
      sane_H_mol = m_H

   AllChem.AssignStereochemistry(sane_H_mol);

   n_confs = sane_H_mol.GetNumConformers()
   # print("debug:: there are", n_confs, "conformers in sane_H_mol")

   # conf_id = AllChem.EmbedMolecule(sane_H_mol, maxAttempts=n_attempts)

   if do_minimization:

      if use_mmff:

         if embed_and_optimize:

            conf_id = AllChem.EmbedMolecule(sane_H_mol, AllChem.ETKDG())
            AllChem.MMFFOptimizeMolecule(sane_H_mol, confId=conf_id)

         if False:  # debugging output
            ba = pyrogen_boost.mmff_bonds_and_angles(sane_H_mol) # uses _forcefield_ of the molecule
            n_bonds = ba.bonds_size()
            if n_bonds > 0:
               for i_bond in range(n_bonds):
                  bond = ba.get_bond(i_bond)
                  print(bond.get_idx_1(), bond.get_idx_2(), bond.get_type(), \
                        bond.get_resting_bond_length(), bond.get_sigma())
            n_angles = ba.angles_size()
            if n_angles > 0:
                for i_angle in range(n_angles):
                    angle = ba.get_angle(i_angle)
                    print(angle.get_idx_1(), angle.get_idx_2(), angle.get_idx_3(), \
                          angle.get_resting_angle(), angle.get_sigma())

      else:
         AllChem.UFFOptimizeMolecule(sane_H_mol, confId=conf_id)

   atom_names = add_atom_names(sane_H_mol)
   all_set = atom_types.set_monomer_library_atom_types(sane_H_mol)  # has deloc bonds now, potentially

   # debug sane_H_mol
   if True:
      molblock = Chem.MolToMolBlock(sane_H_mol)
      # print >> file("sane_H_mol.mol",'w'), molblock
      print(molblock, file=open("sane_H_mol.mol",'w'))

   if (all_set != True):
      return False
   else:

      sane_H_mol.SetProp('comp_id', comp_id)
      sane_H_mol.SetProp('name', compound_name)

      sd_local = mogul_file_name_stub + ".sdf"
      sdf_file_name       = os.path.join(mogul_dir, mogul_file_name_stub + '-mogul.sdf')
      mogul_ins_file_name = os.path.join(mogul_dir, mogul_file_name_stub + '-mogul.ins')
      mogul_out_file_name = os.path.join(mogul_dir, mogul_file_name_stub + '-mogul.out')
      Chem.AllChem.ComputeGasteigerCharges(sane_H_mol)

      moguled_mol = pyrogen_boost.mogulify(sane_H_mol) # Nitro bond orders (and other things?)
      if not os.path.isdir(mogul_dir):
          checked_mkdir(mogul_dir)
          if os.path.isdir(mogul_dir):
              mb = Chem.MolToMolBlock(moguled_mol)
              # print >> file(sdf_file_name,'w'), mb
              print(m, file=open(sdf_file_name,'w'))
      else:
          mb = Chem.MolToMolBlock(moguled_mol)
          # print >> file(sdf_file_name,'w'), mb
          print(mb, file=open(sdf_file_name,'w'))


      bor = make_restraints_for_bond_orders(sane_H_mol)

      # print out the set types:
      print_atom_props = False
      if print_atom_props:
          print('--- Atom Props ---')
      for atom in sane_H_mol.GetAtoms():
         charge = atom.GetProp('_GasteigerCharge') # string?
         name   = atom.GetProp('name')
         try:
            atom_type   = atom.GetProp('type_energy')
            is_aromatic = atom.GetIsAromatic()
            hybrid      = atom.GetHybridization()
            f_charge    = float(charge)
            if print_atom_props:
                print("  atom: %s %s type: %s arom: %s hybrid: %s charge: %6.3f" % (name, atom.GetSymbol(),
                                                                                    atom_type.ljust(4),
                                                                                    str(is_aromatic).ljust(5),
                                                                                    str(hybrid).rjust(3),
                                                                                    f_charge))
         except KeyError:
            print("miss", name, atom.GetSymbol(), charge)

      #
      replace_with_mmff_b_a_restraints = False
      if use_mmff:
          replace_with_mmff_b_a_restraints = True

      # execute_mogul() tests if mogul is executable
      #
      mogul_state = execute_mogul(sdf_file_name, mogul_ins_file_name, mogul_out_file_name)
      if mogul_state:

         # Here we need to think about matching to reference
         # dictionary of amino acids (for standard atom names).
         # That function takes a dictionary and a mmdb::Residue.
         # How does that fit in here?
         #
         restraints = pysw.mogul_out_to_mmcif_dict_by_mol(mogul_out_file_name, comp_id,
                                                          compound_name, sane_H_mol, bor,
                                                          mmcif_dict_name, # not used
                                                          quartet_planes,
                                                          quartet_hydrogen_planes,
                                                          replace_with_mmff_b_a_restraints)


         # match_atom_names_to_dict_flag, comp_id_list_for_names_match, dict_file_for_names_match
         if match_atom_names_to_dict_flag:

             restraints = atom_match_dictionary(restraints, sane_H_mol,
                                                comp_id_list_for_names_match,
                                                dict_files_for_names_match)

         pysw.write_restraints(restraints, mmcif_dict_name)
         pysw.regularize_and_write_pdb(sane_H_mol, restraints, comp_id, pdb_out_file_name)

      else:

          # mogul failed or was not in the path:

          if run_mogul == False:

              print('--- "no mogul" path ---')

              # ... but that's OK if we told pyrogen to run without mogul

              # sane_H_mol:
              # print(Chem.MolToMolBlock(sane_H_mol), file=file('debug_sane_H.mol','w+'))

              # debug
              if False:
                 for atom in sane_H_mol.GetAtoms():
                    try:
                       r = atom.GetProp('_CIPRank')
                       print(atom, "DEBUG:: pyrogen.py::make_restraints(): atom CIP rank", r)
                    except KeyError as e:
                       print('DEBUG:: pyrogen.py::make_restraints() atom', atom, "has no _CIPRank")

              do_regularize = True
              if not do_minimization:
                 do_regularize = False

              restraints = pysw.mmcif_dict_from_mol(comp_id, compound_name, sane_H_mol,
                                                    do_minimization,
                                                    mmcif_dict_name,
                                                    quartet_planes, quartet_hydrogen_planes,
                                                    replace_with_mmff_b_a_restraints)

              if restraints == None:
                  print("No restraints")
                  return True # hacked in value

              if match_atom_names_to_dict_flag:

                  restraints = atom_match_dictionary(restraints, sane_H_mol,
                                                     comp_id_list_for_names_match,
                                                     dict_files_for_names_match)
                  pysw.write_restraints(restraints, mmcif_dict_name)

              pysw.write_pdb_from_mol(sane_H_mol, comp_id, pdb_out_file_name)

          else:
              # ... but not if we wanted to use mogul.
              # (We get here if there is a license error for mogul)
              exit(1)

      return sane_H_mol

def atom_match_dictionary(restraints, sane_H_mol, comp_id_list_for_names_match, dict_files_for_names_match):

    template_comp_ids = ['CYS', 'ASP', 'GLU',        'HIS', 'ILE', 'LYS', 'LEU', 'MET',
                         'ASN', 'PRO', 'GLN', 'ARG', 'SER', 'THR', 'VAL', 'TRP', 'TYR',
                         'G',   'A',     'C',   'U',    'GLC', 'MAN']

    if isinstance(comp_id_list_for_names_match, str):
        template_comp_ids = comp_id_list_for_names_match.split(',')

    template_cif_dict_files_names = []
    if isinstance(dict_files_for_names_match, str):
        template_cif_dict_files_names = dict_files_for_names_match.split(',')
        # don't use my set of comp_ids then
        template_comp_ids = []

    success,new_restraints,at_name_list = pysw.match_restraints_to_dictionaries(restraints,
                                                                                template_comp_ids,
                                                                                template_cif_dict_files_names)
    if success:
        for at_name in at_name_list:
            # print at_name
            pass
        n = len(sane_H_mol.GetAtoms())
        if len(restraints['_chem_comp_atom']) == n:
            restraints = new_restraints
            for iat in range(n):
                name = sane_H_mol.GetAtomWithIdx(iat).GetProp('name')
                if name != restraints['_chem_comp_atom'][iat][0]:
                    # print "   changing name from", name, "to", restraints['_chem_comp_atom'][iat][0]
                    sane_H_mol.GetAtomWithIdx(iat).SetProp('name', restraints['_chem_comp_atom'][iat][0]);

    return restraints

def simple_make(mmcif_file_name, comp_id):

   global run_mogul
   run_mogul = False
   mmcif_restraints_out_file_name = comp_id + "-pyrogen.cif"
   pdb_fn = comp_id + "-pyrogen.pdb"
   do_hydrogen_atoms_shift = True
   do_minimization = True
   mol_pairs = make_restraints_from_mmcif_dict(mmcif_file_name,
                                               comp_id,
                                               ".", ".", '.', 'postfix', False, True, True, do_minimization,
                                               pdb_fn, mmcif_restraints_out_file_name, do_hydrogen_atoms_shift)
   for mol_info in mol_pairs:
      (mol, comp_id) = mol_info
   if not mol:
      print('No molecule')


def png_from_mmcif_file(mmcif_file_name_in, comp_id, png_file_name):
   m = pyrogen_boost.rdkit_mol_chem_comp_pdbx(mmcif_file_name_in, comp_id)
   conf_id = 0
   n = m.GetNumConformers()
   if n == 0:
      conf_id = AllChem.Compute2DCoords(m)
   conf = m.GetConformer(conf_id)
   mol_for_drawing = Chem.RemoveHs(m, implicitOnly=False)
   if conf.Is3D():
      # print '3D path'
      conf2D_id = AllChem.Compute2DCoords(mol_for_drawing)
      make_picture_to_file(mol_for_drawing, conf2D_id, png_file_name)
   else:
      # print 'Non-3D path'
      make_picture_to_file(mol_for_drawing, -1, png_file_name)

def coot_png_from_mmcif_file(mmcif_file_name_in, comp_id, png_file_name, n_pixels=300, BackgroundColor=None):
   # change the name of cairo_png_depict
   # BackgroundColor is a hash colour, for example use '#ffffff' for white
   #
   return pyrogen_boost.cairo_png_depict(mmcif_file_name_in, comp_id, png_file_name, n_pixels, BackgroundColor)

def depict(mol, iconf = -1, npx=300, highlightAtoms=[], highlightBonds=None, highlightAtomColours=None, highlightBondColours=None):
    import IPython

    # import Image
    # import ImageFont
    from PIL import Image
    from PIL import ImageFont
    import PIL
    import PIL.ImageDraw
    import io
    try:
       n_confs = mol.GetNumConformers()
       if n_confs == 0:
           iconf = mol.Compute2DCoords()
       s = pyrogen_boost.cairo_png_depict_to_string(mol, iconf, highlightAtoms, highlightBonds, highlightAtomColours, highlightBondColours, npx)
       if len(s) > 0:
          sio = io.BytesIO(s)
          im = Image.open(sio)
          bo = io.BytesIO()
          im.save(bo, 'png')
          IPython.display.display(IPython.display.Image(bo.getvalue()))
       else:
          print('Null image')
    except AttributeError as e:
        # maybe mol was not a RDKit molecule
        print('ERROR::', e)

def coot_depict_to_png_string(mol, iconf=-1, n_px=300, highlightAtoms=[], highlightBonds=None, highlightAtomColours=None, highlightBondColours=None):
       s = pyrogen_boost.cairo_png_depict_to_string(mol, iconf, highlightAtoms, highlightBonds, highlightAtomColours, highlightBondColours, n_px)
       return s


def coot_depict_to_svg_string(mol, iconf=-1, n_px=300, highlightAtoms=[], highlightBonds=None, highlightAtomColours=None, highlightBondColours=None):
       s = pyrogen_boost.cairo_svg_depict_to_string(mol, iconf, highlightAtoms, highlightBonds, highlightAtomColours, highlightBondColours, n_px)
       return s

# make MolFromPDBXr available in pyrogen
def MolFromPDBXr(cif_file_name, comp_id):
    return pyrogen_boost.MolFromPDBXr(cif_file_name, comp_id)

def MolsToGridImage(mols, mols_per_row=3, sub_image_size=(200,200), legends=None):

    # return None on failure to find the font.
    # Locate the ttf font file on disk somewhere and make a truetype
    # font from that (if you can)
    def find_font():

       import distutils
       import distutils.sysconfig
       prfx=distutils.sysconfig.PREFIX

       # this ttf file is hard-coded and copied from dials base/share/fonts
       # we need to install that directory as part of coot and find
       # the file in the installation
       font_file_base_name = "Vera.ttf" # VeraMono
       dir_1 = os.path.join(prfx,  'share')
       dir_2 = os.path.join(dir_1, 'coot')
       dir_3 = os.path.join(dir_2, 'fonts')
       font_file="/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf"
       if os.path.isfile(font_file):
          font=ImageFont.truetype(font_file, 16)
          return font
       else:
          return None

    import IPython
    # import Image
    # import ImageFont
    from PIL import Image
    from PIL import ImageFont
    import PIL
    import PIL.ImageDraw
    import io
    n_rows=(len(mols) // mols_per_row)
    if len(mols) > n_rows*mols_per_row:
       n_rows += 1
    full_size=(sub_image_size[0]*mols_per_row, sub_image_size[1]*n_rows)
    composite=composite = Image.new('RGBA', full_size)
    font=find_font()

    for count,mol in enumerate(mols):
        try:
            # print('mol:', mol)
            s = pyrogen_boost.cairo_png_depict_to_string(mol, -1, None, None, None, None, sub_image_size[0])
            sio = io.BytesIO(s)
            im = Image.open(sio)
            # add label if possible
            try:
               if font:
                  lab = legends[count]
                  text_x_pos = sub_image_size[0]/2 - 10
                  if text_x_pos < 0:
                     text_x_pos = 0
                  text_y_pos = 0.9 * sub_image_size[0]
                  min_to_fit = 15
                  if sub_image_size[1]-text_y_pos < min_to_fit:
                     text_y_pos = sub_image_size[1] - min_to_fit
                  draw = PIL.ImageDraw.Draw(im)
                  # we don't have this attribute (multiline_text). Hmm. Maybe old version of PIL?
                  # draw.multiline_text((text_x_pos, text_y_pos), lab, fill=(10,10,10,255), font=font, align="center")
                  draw.text((text_x_pos, text_y_pos), lab, fill=(10,10,10,255), font=font)
                  # debug print(text_x_pos, text_y_pos)
            except TypeError as e:
               # labels was not a list
               pass
            except NameError as e:
                print(e, "in legends", legends)
            except IOError as e:
               print("something label-related:", e)
            region = im.crop((0,0,sub_image_size[0], sub_image_size[0]))
            i_row = count // mols_per_row
            i_col = count - i_row * mols_per_row
            x_off = i_col * sub_image_size[0]
            y_off = i_row * sub_image_size[0]
            composite.paste(region,(x_off, y_off, x_off+sub_image_size[0], y_off+sub_image_size[0]))
        except ValueError as e:
            print(e, 'count ', count, " for mol", mol)
        except IOError as e:
            print(e, 'count ', count, " for mol", mol)
        except Exception as e:
            print(e, 'count ', count, " for mol", mol)
    b = io.BytesIO()
    composite.save(b, 'png')
    IPython.display.display(IPython.display.Image(b.getvalue()))

def prep(mol):
   m_sh = Chem.RemoveHs(mol)
   m_sh.Compute2DCoords()
   Chem.Kekulize(m_sh)
   return m_sh

def d(mol, npx=300):
   m_sh = prep(mol)
   depict(m_sh, npx=npx)

def score_and_print_tautomers(mol, comp_id, output_postfix, do_drawings):

    results = tautomer.enumerate_tautomers(mol)
    for i in range(len(results)):
        m = results[i]
        s = Chem.MolToSmiles(m)
        print("comp_id :", comp_id, ": SMILES", s, 'score:', tautomer.tautomer_score(m))
        if do_drawings:
            file_name = comp_id + '-tautomer-' + str(i)
            file_name += '-' + options.output_postfix + '.png'
            n = m.GetNumConformers()
            conf_id = 0
            if n == 0:
                conf_id = AllChem.Compute2DCoords(m)
            conf = m.GetConformer(conf_id)
            if conf.Is3D():
                mol_for_drawing = Chem.RemoveHs(m, implicitOnly=False)
                conf2D_id = AllChem.Compute2DCoords(mol_for_drawing)
                make_picture_to_file(mol_for_drawing, conf2D_id, file_name)
            else:
                make_picture_to_file(m, -1, file_name)


if __name__ == "__main__":

    def smiles_and_name_from(smi_raw):
       extension = os.path.splitext(smi_raw)[1]
       smiles_string = ''
       name=''
       if extension == '.smi' or extension == '.smiles':
           if not os.path.exists(smi_raw):
               print("File not found:", smi_raw)
               exit(1)
           else:
               smiles_string,name = get_smiles_from_file(smi_raw)
       else:
         smiles_string = smi_raw
       return smiles_string,name

    parser = OptionParser(usage='pyrogen [options] file-or-SMILES'+
                          '\n       if file-or-SMILES has extension ".smi" or ".smiles" ' +
                          'then it is treated as a file')
    parser.add_option("-c", "--mmcif", dest="mmcif_file_name",
                      help="Make restraints from input mmcif FILE", metavar="FILE")
    parser.add_option("-m", "--mol", dest="sdf_file",
                      help="Make restraints from input sdf/mol FILE", metavar="FILE")
    parser.add_option("-r", "--residue-type", dest="comp_id", default='default',
                      help="Create restraints for this type. Default is LIG")
    parser.add_option("-4", "--quartet-planes", dest="quartet_planes",
                      default=False,
                      help="Use 4-atom plane restraints,\n                    " +
                      "forces --quartet-hydrogens", action="store_true")
    parser.add_option("-H", "--quartet-hydrogens", dest="quartet_hydrogen_planes",
                      default=False,
                      help="Use 4-atom hydrogen plane restraints",
                      action="store_true")
    parser.add_option("-b", "--no-shift-hydrogen-atoms", dest="do_hydrogen_atoms_shift",
                      default=True, help="Stop addition or deletion of Hydrogen atoms for formally-charged atoms",
                      action="store_false")
    parser.add_option("-n", "--no-mogul", dest="use_mogul", default=False, action="store_false",
                      help='Don\'t run CSD Mogul to update bond and angle restraints')
    parser.add_option("-N", '--name', dest='compound_name', default=False,
                      help='Compound name')
    parser.add_option('-S', '--smiles', dest="show_smiles",
                      default=False, action="store_true", help="Write the SMILES for the input molecule")
    parser.add_option("-t", "--tautomers", dest="show_tautomers",
                      default=False, action="store_true",
                      help='Show SMILES for tautomers, don\'t generate restraints')
    parser.add_option("-T", '--tmp-directory', dest='mogul_dir',
                      help='Directory into which the tmp files (e.g. for mogul) are written',
                      default='pyrogen-mogul')
    parser.add_option("-d", '--directory', dest='output_dir',
                      help='Directory into which the output files (e.g. mmCIF and PDB) are written',
                      default='.')
    parser.add_option('-o', '--output-postfix', default='pyrogen',
                      dest='output_postfix',
                      help='string to add to output file names, default is "pyrogen"')
    parser.add_option('-p', '--picture', dest='drawing',
                      help='Additionally output a chemical diagram PNG',
                      action='store_true', default=False)
    parser.add_option('-P', '--preserve-input-coordinates', dest='preserve_input_coordinates',
                      help="Preserve the inputput coordinates (if possible)",
                      action='store_true', default=False)
    parser.add_option('-v', '--version', dest='show_version', default=False,
                      action='store_true', help='Print version information')
    # parser.add_option('-M', '--MMFF', dest='use_mmff', default=False,                       # true now.
    #                  action='store_true', help='Use MMFF fallbacks for bonds and angles')
    parser.add_option('-a', '--no-match-vs-reference-dictionaries', default=False,
                      action='store_true', dest='no_match_names_flag',
                      help="Don't match atom names vs. dictionary molecules (default False)")
    parser.add_option('-R', '--reference-dictionary-files', dest='dict_files_for_names_match',
                      help='Try to match the atom names of the output molecule '+
                      'to this dictionary in these files (comma-separated list)', default=False)
    parser.add_option('-C', '--reference-dictionary-comp-ids', dest='comp_id_list_for_names_match',
                      help='Try to match the atom names of the output molecule to these comp-ids' +
                      ' (comma-separated list)',
                      default=False)
    parser.add_option('-w', '--wwPDB', default=False, dest="wwPDB", action="store_true",
                      help='Fetch the wwPDB ligand definition and use that')
    parser.add_option('-f', dest="fetch", default=False,
                      help='(Just) fetch from the PDBe the CCD entry for the given compound-id')
    parser.add_option("-q", "--quiet",
                      action="store_false", dest="verbose", default=True,
                      help="print less messages")

    (options, args) = parser.parse_args()
    # print('DEBUG:: options:', options)
    # print('DEBUG:: args:', args)

    if len(sys.argv) == 0:
       print("Usage: pyrogen --help")

    if options.show_version:
       print('pyrogen-' + pyrogen_version, "revision", coot_git.revision_count())

    if options.fetch:
        fetch(options.fetch)

    comp_id = options.comp_id
    if options.comp_id == 'default':
        comp_id = 'LIG'
    if options.mmcif_file_name != None:
        if options.comp_id == 'default':
            comp_id = 'TRY_ALL_COMP_IDS'

    file_name_stub = comp_id + '-' + options.output_postfix

    if options.output_dir != ".":
       file_name_stub = os.path.join(options.output_dir, file_name_stub)

    pdb_out_file_name              = file_name_stub + '.pdb'
    mmcif_restraints_out_file_name = file_name_stub + '.cif'

    # this is a bit ugly, perhaps. this value is inspected inside
    # the following functions
    # (run_mogul is a global, default no)
    if options.use_mogul == True:
       run_mogul = True
    if run_mogul:
       if len(options.mogul_dir) > 0:
          if options.mogul_dir[0] == '-':
             print('Stop:: you probably didn\'t mean that you wanted',options.mogul_dir, 'as your tmp directory.')
             exit(1)
          checked_mkdir(options.mogul_dir)

    if options.show_tautomers or options.show_smiles:

        # ------------------------ Tautomers and SMILES  ---------------------------------------------

        mol = False
        if len(args) > 0:
            smi_raw = args[0]
            smiles,compound_name = smiles_and_name_from(smi_raw)
            mol = Chem.MolFromSmiles(smiles)
        else:
            if options.sdf_file != None:
                mol = Chem.MolFromMolFile(options.sdf_file)
            else:
                if options.mmcif_file_name != None:
                    types = pysw.types_from_mmcif_dictionary(options.mmcif_file_name)
                    print('-- tautomer mode: mmcif file types:', types)
                    for type in types:
                        mol_local = pyrogen_boost.rdkit_mol_chem_comp_pdbx(options.mmcif_file_name, type)
                        score_and_print_tautomers(mol_local, type, options.output_postfix, options.drawing)

        if mol:
           if options.show_tautomers:
              score_and_print_tautomers(mol, comp_id, options.output_postfix, options.drawing)
           if options.show_smiles:
              s = Chem.MolToSmiles(mol);
              print(s)

    else:

        # ------------------------ dict-build-mode ---------------------------------------------------

        mmcif_file_name = options.mmcif_file_name
        use_mmff = True
        do_minimization = True
        if options.preserve_input_coordinates == True:
           do_minimization = False
        # shall we go get the dictionary?
        if options.wwPDB:
           mmcif_file_name = get_pdbe_cif_for_comp_id(comp_id)
           if os.path.isfile(mmcif_file_name):
              pass # good
           else:
              print("Missing downloaded file for comp-id:",  comp_id)
              exit(2)

        # JED mode for hydrogen planes
        #
        quartet_hydrogen_planes = options.quartet_hydrogen_planes
        if options.quartet_planes:
            quartet_hydrogen_planes = True

        match_names_flag = True
        if options.no_match_names_flag:
            match_names_flag = False


        if mmcif_file_name:
            mol_pairs = make_restraints_from_mmcif_dict(mmcif_file_name,
                                                        comp_id,
                                                        options.mogul_dir,
                                                        options.output_dir,
                                                        options.output_postfix,
                                                        options.quartet_planes,
                                                        quartet_hydrogen_planes,
                                                        use_mmff, do_minimization,
                                                        pdb_out_file_name,
                                                        mmcif_restraints_out_file_name,
                                                        options.do_hydrogen_atoms_shift)

            # this needs to be in a try block, I suppose, for example if the mmcif file
            # does not exist.

            for mol_info in mol_pairs:
                (mol, comp_id) = mol_info
                if not mol:
                    print('No molecule')
                else:
                    # Happy path
                    if options.drawing:
                        # make_picture() by default draws the first conformer in the given molecule.
                        # For mol, that is a 3D conformer.  We want to draw a nice 2D diagram
                        #
                        mol_for_drawing = Chem.RemoveHs(mol, implicitOnly=False)
                        conf2D_id = AllChem.Compute2DCoords(mol_for_drawing)
                        conf = mol.GetConformer(conf2D_id)
                        Chem.WedgeMolBonds(mol_for_drawing, conf)
                        make_picture(mol_for_drawing, conf2D_id, comp_id, options.output_postfix)

        else:

            if options.sdf_file != None:
                (mol, results) = make_restraints_from_mdl(options.sdf_file, comp_id,
                                                          options.mogul_dir, file_name_stub,
                                                          pdb_out_file_name,
                                                          mmcif_restraints_out_file_name,
                                                          options.quartet_planes,
                                                          quartet_hydrogen_planes,
                                                          use_mmff,
                                                          match_names_flag,
                                                          options.comp_id_list_for_names_match,
                                                          options.dict_files_for_names_match)
                if options.drawing:
                    make_picture(mol, -1, comp_id, options.output_postfix)

            else:

                if len(args) > 0:
                    smi_raw = args[0]
                    smiles,compound_name_from_file = smiles_and_name_from(smi_raw)
                    compound_name=False
                    if len(compound_name_from_file) > 0:
                       compound_name = compound_name_from_file
                    print("#####", compound_name, options.compound_name)
                    if isinstance(options.compound_name, str):
                         compound_name = options.compound_name
                    status = make_restraints_from_smiles(smiles, comp_id, compound_name,
                                                         options.mogul_dir, file_name_stub,
                                                         pdb_out_file_name,
                                                         mmcif_restraints_out_file_name,
                                                         options.quartet_planes,
                                                         quartet_hydrogen_planes,
                                                         use_mmff,
                                                         match_names_flag,
                                                         options.comp_id_list_for_names_match,
                                                         options.dict_files_for_names_match)
                    if options.drawing:
                        mol = Chem.MolFromSmiles(smiles)
                        make_picture(mol, -1, comp_id, options.output_postfix)