-
-
Notifications
You must be signed in to change notification settings - Fork 403
/
testobconv_writers.py
4125 lines (3746 loc) · 123 KB
/
testobconv_writers.py
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
"""Test WriteFile() and WriteString() for each of the supported formats
These tests started as a way to verify that the OBConversion Index is
correctly reset to 1 for each format.
"""
import os
import sys
import unittest
import tempfile
from openbabel import openbabel as ob
import re
# Set the following to enable a workaround so the tests work on older
# versions of Open Babel.
ENABLE_WORKAROUND = 0
# Some of the formats embed the version in the output
VERSION = ob.OBReleaseVersion()
# Most of the tests use an OBMol made from this phenol structure
_default_conv = ob.OBConversion()
_default_conv.SetInAndOutFormats("sdf", "smi")
PHENOL_SDF = """\
phenol
OpenBabel01151914482D
7 7 0 0 0 0 0 0 0 0999 V2000
1.5846 -0.0249 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.5703 0.9755 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.4295 1.4882 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.3031 1.0004 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.3175 -0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-1.0005 0.0051 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
1 6 2 0 0 0 0
1 2 1 0 0 0 0
2 3 2 0 0 0 0
3 4 1 0 0 0 0
4 5 2 0 0 0 0
5 6 1 0 0 0 0
6 7 1 0 0 0 0
M END
$$$$
"""
# Some of the tests use a reaction.
_rxn_conv = ob.OBConversion()
_rxn_conv.SetInAndOutFormats("rxn", "rxn")
_alchemy_mol = ob.OBMol()
ALCHEMY_RXN = """\
$RXN
lead_to_gold
OpenBabel
1 1
$MOL
OpenBabel01151916222D
1 0 0 0 0 0 0 0 0 0999 V2000
0.0000 10.0000 0.0000 Pb 0 0 0 0 0 15 0 0 0 0 0 0
M END
$MOL
OpenBabel01151916222D
1 0 0 0 0 0 0 0 0 0999 V2000
0.0000 11.0000 0.0000 Au 0 0 0 0 0 0 0 0 0 0 0 0
M END
"""
if not _rxn_conv.ReadString(_alchemy_mol, ALCHEMY_RXN):
if ENABLE_WORKAROUND:
# For some reason this record fails under Open Babel 2.4.1
sys.stderr.write("Unable to parse RXN record? Reaction tests will fail.\n")
else:
raise AssertionError("Cannot parse RXN record")
# Some of the tests pass in a SMILES string
_smi_conv = ob.OBConversion()
_smi_conv.SetInAndOutFormats("smi", "smi")
def get_mol(test_case, mol):
if mol is None:
# Always make a new molecule so the tests don't
# interfere with each other
mol = ob.OBMol()
assert _default_conv.ReadString(mol, PHENOL_SDF)
return mol
if isinstance(mol, str):
# Parse it as a SMILES string with optional title
terms = mol.split(None, 1)
if len(terms) == 1:
smiles = terms[0]
title = "unknown"
elif len(terms) == 2:
smiles = terms[0]
title = terms[1]
else:
raise AssertionError(mol)
mol = ob.OBMol()
if not _smi_conv.ReadString(mol, smiles):
test_case.fail("Cannot parse SMILES %r" % (smiles,))
mol.SetTitle(title)
return mol
# Must have passed in a molecule. Return it.
return mol
# Create a new OBConversion for the given format.
# Optionally pass in the options to set.
def get_converter(test_case, output_format, options=None):
conv = ob.OBConversion()
if not conv.SetInAndOutFormats("smi", output_format):
test_case.fail("Cannot set output format %r" % (output_format,))
if options:
# Can pass in a dictionary ...
if isinstance(options, dict):
for k, v in options.items():
conv.AddOption(k, ob.OBConversion.OUTOPTIONS, v)
else:
# ... or an iterable
for k in options:
conv.AddOption(k, ob.OBConversion.OUTOPTIONS)
if ENABLE_WORKAROUND:
conv.SetOutputIndex(1)
return conv
def save_to_pasteboard(text):
# This test suite was developed on a Mac.
# This code copies the text to the paste buffer,
# which I can then use as the expected text.
import subprocess
p = subprocess.Popen(["pbcopy"],
stdin=subprocess.PIPE)
p.stdin.write(text.encode("utf8"))
p.stdin.close()
p.wait()
def test_write_string(test_case, mol, conv, expected_output, normalize):
output = conv.WriteString(mol)
### Debugging output
if 0:
print("===")
print(output)
print("===")
if 0:
save_to_pasteboard(output)
# Apply normalizations to both sides
if normalize is not None:
output = normalize(output)
expected_output = normalize(expected_output)
test_case.assertMultiLineEqual(output.replace("\r\n", "\n"), expected_output.replace("\r\n", "\n"))
if type(u"") == type(""):
# Python 3
def test_binary_write_string(test_case, mol, conv, expected_output, normalize):
# I think 'surrogateescape' is the right way to handle this
output = conv.WriteString(mol).encode("utf8", "surrogateescape")
if normalize:
output = normalize(output)
expected_output = normalize(expected_output)
## print("===", repr(output))
test_case.assertEqual(output, expected_output)
else:
# Python 2
def test_binary_write_string(test_case, mol, conv, expected_output, normalize):
output = conv.WriteString(mol)
## print("===", repr(output))
if normalize:
output = normalize(output)
expected_output = normalize(expected_output)
test_case.assertEqual(output, expected_output)
def test_write_file(test_case, mol, conv, expected_output, normalize):
temp_file_object = tempfile.NamedTemporaryFile(delete=False) # we will delete it manually
temp_filename = temp_file_object.name
if os.name == 'nt':
temp_file_object.close() # Can't write to open file on Windows so we have to close it (but this could lead to a race condition if someone else uses the same temporary file name)
try:
test_case.assertTrue(conv.WriteFile(mol, temp_filename))
conv.CloseOutFile() # we can't delete it on Windows otherwise
with open(temp_filename) as f:
output = f.read()
finally:
temp_file_object.close()
os.remove(temp_filename)
if 0:
save_to_pasteboard(output)
if normalize is not None:
output = normalize(output)
expected_output = normalize(expected_output)
test_case.assertMultiLineEqual(output.replace("\r\n", "\n"), expected_output.replace("\r\n", "\n"))
def test_binary_write_file(test_case, mol, conv, expected_output, normalize):
temp_file_object = tempfile.NamedTemporaryFile()
temp_filename = temp_file_object.name
try:
test_case.assertTrue(conv.WriteFile(mol, temp_filename))
with open(temp_filename, "rb") as f:
output = f.read()
finally:
temp_file_object.close()
## print("==", repr(output))
if normalize is not None:
output = normalize(output)
expected_output = normalize(expected_output)
test_case.assertEqual(output, expected_output)
def test_write_multi_file(test_case, mols, conv, expected_output, normalize):
temp_file_object = tempfile.NamedTemporaryFile()
temp_filename = temp_file_object.name
n = len(mols)
test_case.assertGreater(n, 0, "must have at least one molecule")
last = n-1
try:
for i, mol in enumerate(mols):
conv.SetLast(i == last)
if i == 0:
test_case.assertTrue(conv.WriteFile(mol, temp_filename))
else:
test_case.assertTrue(conv.Write(mol))
with open(temp_filename) as f:
output = f.read()
finally:
temp_file_object.close()
if 0:
save_to_pasteboard(output)
if normalize is not None:
output = normalize(output)
expected_output = normalize(expected_output)
test_case.assertMultiLineEqual(output, expected_output)
class WriteMixin(object):
def assertWriters(self, output_format, expected_output, options=None, mol=None, normalize=None):
mol = get_mol(self, mol)
conv = get_converter(self, output_format, options)
test_write_string(self, mol, conv, expected_output, normalize)
test_write_file(self, mol, conv, expected_output, normalize)
def assertWriteString(self, output_format, expected_output, options=None, mol=None, normalize=None):
mol = get_mol(self, mol)
conv = get_converter(self, output_format, options)
test_write_string(self, mol, conv, expected_output, normalize)
def assertWriteFile(self, output_format, expected_output, options=None, mol=None, normalize=None):
mol = get_mol(self, mol)
conv = get_converter(self, output_format, options)
test_write_file(self, mol, conv, expected_output, normalize)
# Write 1 or more molecule to a file
def assertWriteMultiFile(self, output_format, expected_output, options=None, mols=None, normalize=None):
if mols is None:
# Get two of the default molecules
mols = [get_mol(self, None), get_mol(self, None)]
conv = get_converter(self, output_format, options)
test_write_multi_file(self, mols, conv, expected_output, normalize)
def assertBinaryWriters(self, output_format, expected_output, options=None, mol=None, normalize=None):
mol = get_mol(self, mol)
conv = get_converter(self, output_format, options)
test_binary_write_string(self, mol, conv, expected_output, normalize)
test_binary_write_file(self, mol, conv, expected_output, normalize)
def assertBinaryWriteString(self, output_format, expected_output, options=None, mol=None, normalize=None):
mol = get_mol(self, mol)
conv = get_converter(self, output_format, options)
test_binary_write_string(self, mol, conv, expected_output, normalize)
def assertBinaryWriteFile(self, output_format, expected_output, options=None, mol=None, normalize=None):
mol = get_mol(self, mol)
conv = get_converter(self, output_format, options)
test_binary_write_file(self, mol, conv, expected_output, normalize)
# acesin -- ACES input format [Write-only]
class TestACES(unittest.TestCase, WriteMixin):
fmt = "acesin"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
phenol
C 1.58460 -0.02490 0.00000
C 1.57030 0.97550 0.00000
C 2.42950 1.48820 0.00000
C 3.30310 1.00040 0.00000
C 3.31750 -0.00000 0.00000
C 0.00000 0.00000 0.00000
O -1.00050 0.00510 0.00000
*ACES2(__ADD_SETUP_HERE__)
""")
# adf -- ADF cartesian input format [Write-only]
class TestADF(unittest.TestCase, WriteMixin):
fmt = "adf"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
TITLE phenol
CHARGE 0 0
Number of atoms
7
ATOMS Cartesian
C 1.58460 -0.02490 0.00000
C 1.57030 0.97550 0.00000
C 2.42950 1.48820 0.00000
C 3.30310 1.00040 0.00000
C 3.31750 -0.00000 0.00000
C 0.00000 0.00000 0.00000
O -1.00050 0.00510 0.00000
End
Basis
End
Geometry
End
""")
# alc -- Alchemy format
class TestALC(unittest.TestCase, WriteMixin):
fmt = "alc"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
7 ATOMS, 7 BONDS, 0 CHARGES
1 C2 1.5846 -0.0249 0.0000 0.0000
2 C2 1.5703 0.9755 0.0000 0.0000
3 C2 2.4295 1.4882 0.0000 0.0000
4 C2 3.3031 1.0004 0.0000 0.0000
5 C2 3.3175 -0.0000 0.0000 0.0000
6 C2 0.0000 0.0000 0.0000 0.0000
7 O3 -1.0005 0.0051 0.0000 0.0000
1 1 6 DOUBLE
2 1 2 SINGLE
3 2 3 DOUBLE
4 3 4 SINGLE
5 4 5 DOUBLE
6 5 6 SINGLE
7 6 7 SINGLE
""")
## # ascii -- ASCII format [Write-only]
## # XXX Doesn't look good
## class TestASCII(unittest.TestCase, WriteMixin):
## fmt = "ascii"
## maxDiff = None
## def test_default(self):
## self.assertWriters(self.fmt, """\
## """)
# bgf -- MSI BGF format
class TestBGF(unittest.TestCase, WriteMixin):
fmt = "bgf"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
BIOGRF 200
DESCRP phenol
FORCEFIELD DREIDING
FORMAT ATOM (a6,1x,i5,1x,a5,1x,a3,1x,a1,1x,a5,3f10.5,1x,a5,i3,i2,1x,f8.5)
HETATM 1 C1 RES A 444 1.58460 -0.02490 0.00000 C_R 3 0 0.04203
HETATM 2 C2 RES A 444 1.57030 0.97550 0.00000 C_R 3 0 0.00328
HETATM 3 C3 RES A 444 2.42950 1.48820 0.00000 C_R 3 0 0.00021
HETATM 4 C4 RES A 444 3.30310 1.00040 0.00000 C_R 3 0 0.00328
HETATM 5 C5 RES A 444 3.31750 -0.00000 0.00000 C_R 3 0 0.04203
HETATM 6 C6 RES A 444 0.00000 0.00000 0.00000 C_R 3 0 0.19575
HETATM 7 O7 RES A 444 -1.00050 0.00510 0.00000 O_3 2 0 -0.28657
FORMAT CONECT (a6,12i6)
CONECT 1 6 2
ORDER 1 2 1
CONECT 2 1 3
ORDER 2 1 2
CONECT 3 2 4
ORDER 3 2 1
CONECT 4 3 5
ORDER 4 1 2
CONECT 5 4 6
ORDER 5 2 1
CONECT 6 1 5 7
ORDER 6 2 1 1
CONECT 7 6
ORDER 7 1
END
""")
# box -- Dock 3.5 Box format
class TestBOX(unittest.TestCase, WriteMixin):
fmt = "box"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
HEADER CORNERS OF BOX
REMARK CENTER (X Y Z) 1.159 0.732 0.000
REMARK DIMENSIONS (X Y Z) 6.318 3.513 2.000
ATOM 1 DUA BOX 1 -2.000 -1.025 -1.000
ATOM 2 DUA BOX 1 4.317 -1.025 -1.000
ATOM 3 DUA BOX 1 4.317 -1.025 1.000
ATOM 4 DUA BOX 1 -2.000 -1.025 1.000
ATOM 5 DUA BOX 1 -2.000 2.488 -1.000
ATOM 6 DUA BOX 1 4.317 2.488 -1.000
ATOM 7 DUA BOX 1 4.317 2.488 1.000
ATOM 8 DUA BOX 1 -2.000 2.488 1.000
CONECT 1 2 4 5
CONECT 2 1 3 6
CONECT 3 2 4 7
CONECT 4 1 3 8
CONECT 5 1 6 8
CONECT 6 2 5 7
CONECT 7 3 6 8
CONECT 8 4 5 7
""")
# bs -- Ball and Stick format
class TestBS(unittest.TestCase, WriteMixin):
fmt = "bs"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
phenol
7
C 1.5846 -0.0249 0.0000 6 2
C 1.5703 0.9755 0.0000 1 3
C 2.4295 1.4882 0.0000 2 4
C 3.3031 1.0004 0.0000 3 5
C 3.3175 -0.0000 0.0000 4 6
C 0.0000 0.0000 0.0000 1 5 7
O -1.0005 0.0051 0.0000 6
""")
# c3d1 -- Chem3D Cartesian 1 format
class TestC3D1(unittest.TestCase, WriteMixin):
fmt = "c3d1"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
7
C 1 1.5846 -0.0249 0.0000 2 6 2
C 2 1.5703 0.9755 0.0000 2 1 3
C 3 2.4295 1.4882 0.0000 2 2 4
C 4 3.3031 1.0004 0.0000 2 3 5
C 5 3.3175 -0.0000 0.0000 2 4 6
C 6 0.0000 0.0000 0.0000 2 1 5 7
O 7 -1.0005 0.0051 0.0000 6 6
""")
# c3d2 -- Chem3D Cartesian 2 format
class TestC3D2(unittest.TestCase, WriteMixin):
fmt = "c3d2"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
7
C 1 1.5846 -0.0249 0.0000 2 6 2
C 2 1.5703 0.9755 0.0000 2 1 3
C 3 2.4295 1.4882 0.0000 2 2 4
C 4 3.3031 1.0004 0.0000 2 3 5
C 5 3.3175 -0.0000 0.0000 2 4 6
C 6 0.0000 0.0000 0.0000 2 1 5 7
O 7 -1.0005 0.0051 0.0000 82 6
""")
# cac -- CAChe MolStruct format [Write-only]
class TestCAC(unittest.TestCase, WriteMixin):
fmt = "cac"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
molstruct88_Apr_30_1993_11:02:29 <molecule> 0x1d00
Written by Molecular Editor on <date>
Using data dictionary 9/9/93 4:47 AM
Version 6
local_transform
0.100000 0.000000 0.000000 0.000000
0.000000 0.100000 0.000000 0.000000
0.000000 0.000000 0.100000 0.000000
0.000000 0.000000 0.000000 1.000000
object_class atom
property xyz_coordinates MoleculeEditor angstrom 6 3 FLOAT
property anum MoleculeEditor unit 0 1 INTEGER
property sym MoleculeEditor noUnit 0 2 STRING
property chrg MoleculeEditor charge_au 0 1 INTEGER
property rflag MoleculeEditor noUnit 0 1 HEX
ID xyz_coordinates anum sym chrg rflag
1 1.584600 -0.024900 0.000000 6 C 0 0x7052
2 1.570300 0.975500 0.000000 6 C 0 0x7052
3 2.429500 1.488200 0.000000 6 C 0 0x7052
4 3.303100 1.000400 0.000000 6 C 0 0x7052
5 3.317500 -0.000000 0.000000 6 C 0 0x7052
6 0.000000 0.000000 0.000000 6 C 0 0x7052
7 -1.000500 0.005100 0.000000 8 O 0 0x7052
property_flags:
object_class bond
property rflag MoleculeEditor noUnit 0 1 HEX
property type MoleculeEditor noUnit 0 1 NAME
property bond_order MoleculeEditor noUnit 4 1 FLOAT
ID rflag type bond_order
1 0x7005 double
2 0x7005 single
3 0x7005 double
4 0x7005 single
5 0x7005 double
6 0x7005 single
7 0x7005 single
property_flags:
object_class connector
property dflag MoleculeEditor noUnit 0 1 HEX
property objCls1 MoleculeEditor noUnit 0 1 NAME
property objCls2 MoleculeEditor noUnit 0 1 NAME
property objID1 MoleculeEditor noUnit 0 1 INTEGER
property objID2 MoleculeEditor noUnit 0 1 INTEGER
ID dflag objCls1 objCls2 objID1 objID2
1 0xa1 atom bond 1 1
2 0xa1 atom bond 6 1
3 0xa1 atom bond 1 2
4 0xa1 atom bond 2 2
5 0xa1 atom bond 2 3
6 0xa1 atom bond 3 3
7 0xa1 atom bond 3 4
8 0xa1 atom bond 4 4
9 0xa1 atom bond 4 5
10 0xa1 atom bond 5 5
11 0xa1 atom bond 5 6
12 0xa1 atom bond 6 6
13 0xa1 atom bond 6 7
14 0xa1 atom bond 7 7
property_flags:
""")
# caccrt -- Cacao Cartesian format
class TestCACCRT(unittest.TestCase, WriteMixin):
fmt = "caccrt"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
phenol
7 DIST 0 0 0
CELL 1.,1.,1.,90.,90.,90.
C 1.5846, -0.0249, 0.0000
C 1.5703, 0.9755, 0.0000
C 2.4295, 1.4882, 0.0000
C 3.3031, 1.0004, 0.0000
C 3.3175, -0.0000, 0.0000
C 0.0000, 0.0000, 0.0000
O -1.0005, 0.0051, 0.0000
""")
# cache -- CAChe MolStruct format [Write-only]
class TestCACHE(unittest.TestCase, WriteMixin):
fmt = "cache"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
molstruct88_Apr_30_1993_11:02:29 <molecule> 0x1d00
Written by Molecular Editor on <date>
Using data dictionary 9/9/93 4:47 AM
Version 6
local_transform
0.100000 0.000000 0.000000 0.000000
0.000000 0.100000 0.000000 0.000000
0.000000 0.000000 0.100000 0.000000
0.000000 0.000000 0.000000 1.000000
object_class atom
property xyz_coordinates MoleculeEditor angstrom 6 3 FLOAT
property anum MoleculeEditor unit 0 1 INTEGER
property sym MoleculeEditor noUnit 0 2 STRING
property chrg MoleculeEditor charge_au 0 1 INTEGER
property rflag MoleculeEditor noUnit 0 1 HEX
ID xyz_coordinates anum sym chrg rflag
1 1.584600 -0.024900 0.000000 6 C 0 0x7052
2 1.570300 0.975500 0.000000 6 C 0 0x7052
3 2.429500 1.488200 0.000000 6 C 0 0x7052
4 3.303100 1.000400 0.000000 6 C 0 0x7052
5 3.317500 -0.000000 0.000000 6 C 0 0x7052
6 0.000000 0.000000 0.000000 6 C 0 0x7052
7 -1.000500 0.005100 0.000000 8 O 0 0x7052
property_flags:
object_class bond
property rflag MoleculeEditor noUnit 0 1 HEX
property type MoleculeEditor noUnit 0 1 NAME
property bond_order MoleculeEditor noUnit 4 1 FLOAT
ID rflag type bond_order
1 0x7005 double
2 0x7005 single
3 0x7005 double
4 0x7005 single
5 0x7005 double
6 0x7005 single
7 0x7005 single
property_flags:
object_class connector
property dflag MoleculeEditor noUnit 0 1 HEX
property objCls1 MoleculeEditor noUnit 0 1 NAME
property objCls2 MoleculeEditor noUnit 0 1 NAME
property objID1 MoleculeEditor noUnit 0 1 INTEGER
property objID2 MoleculeEditor noUnit 0 1 INTEGER
ID dflag objCls1 objCls2 objID1 objID2
1 0xa1 atom bond 1 1
2 0xa1 atom bond 6 1
3 0xa1 atom bond 1 2
4 0xa1 atom bond 2 2
5 0xa1 atom bond 2 3
6 0xa1 atom bond 3 3
7 0xa1 atom bond 3 4
8 0xa1 atom bond 4 4
9 0xa1 atom bond 4 5
10 0xa1 atom bond 5 5
11 0xa1 atom bond 5 6
12 0xa1 atom bond 6 6
13 0xa1 atom bond 6 7
14 0xa1 atom bond 7 7
property_flags:
""")
# cacint -- Cacao Internal format [Write-only]
class TestCACINT(unittest.TestCase, WriteMixin):
fmt = "cacint"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
# TITLE
EL
0.,0.,0., C
1,2, C 1.001, 90.000,269.181
2,3, C 1.001,120.006, 90.000
3,4, C 1.001,119.997, 49.094
4,5, C 1.001,120.003, -0.000
1,6, C 1.585, 90.000, -0.000
6,7, O 1.001,179.392,180.000
""")
# can -- Canonical SMILES format
class TestCAN(unittest.TestCase, WriteMixin):
fmt = "can"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
Oc1ccccc1\tphenol
""")
# cdjson -- ChemDoodle JSON
class TestCDJSON(unittest.TestCase, WriteMixin):
fmt = "cdjson"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
{
"m": [
{
"a": [
{
"x": 31.692,
"y": -0.498
},
{
"x": 31.406,
"y": 19.51
},
{
"x": 48.59,
"y": 29.764
},
{
"x": 66.062,
"y": 20.008
},
{
"x": 66.35,
"y": -0.0
},
{
"x": 0.0,
"y": 0.0
},
{
"x": -20.009999999999999,
"y": 0.10200000000000001,
"l": 8
}
],
"b": [
{
"b": 0,
"e": 5,
"o": 2
},
{
"b": 0,
"e": 1
},
{
"b": 1,
"e": 2,
"o": 2
},
{
"b": 2,
"e": 3
},
{
"b": 3,
"e": 4,
"o": 2
},
{
"b": 4,
"e": 5
},
{
"b": 5,
"e": 6
}
]
}
]
}""")
## # cdxml -- ChemDraw CDXML format
## XXX fails on an unpatched system
## class TestCDXML(unittest.TestCase, WriteMixin):
## fmt = "cdxml"
## maxDiff = None
## def test_default(self):
## self.assertWriters(self.fmt, """\
## <fragment>
## <n id="1" p="0.000000 0.000000"/>
## <n id="2" p="0.000000 0.000000"/>
## <n id="3" p="0.000000 0.000000"/>
## <n id="4" p="0.000000 0.000000"/>
## <n id="5" p="0.000000 0.000000"/>
## <n id="6" p="0.000000 0.000000"/>
## <n id="7" p="0.000000 0.000000" Element="8"/>
## <b B="1" E="2"/>
## <b B="2" E="3" Order="2"/>
## <b B="3" E="4"/>
## <b B="4" E="5" Order="2"/>
## <b B="5" E="6"/>
## <b B="1" E="6" Order="2"/>
## <b B="6" E="7"/>
## </fragment>
## """)
# cht -- Chemtool format [Write-only]
class TestCHT(unittest.TestCase, WriteMixin):
fmt = "cht"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
Chemtool Version 1.4
geometry 165 55
bonds 7
79 -1 0 0 1
79 -1 79 49 0
79 49 121 74 1
121 74 165 50 0
165 50 166 0 1
166 0 0 0 0
0 0 -50 0 0
atoms 1
-50 0 O -1
splines 0
""")
# cif -- Crystallographic Information File
class TestCIF(unittest.TestCase, WriteMixin):
fmt = "cif"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
# CIF file generated by openbabel %(VERSION)s, see https://openbabel.org
data_I
_chemical_name_common 'phenol'
loop_
_atom_site_label
_atom_site_type_symbol
_atom_site_fract_x
_atom_site_fract_y
_atom_site_fract_z
_atom_site_occupancy
C0 C 1.58460 -0.02490 0.00000 1.000
C1 C 1.57030 0.97550 0.00000 1.000
C2 C 2.42950 1.48820 0.00000 1.000
C3 C 3.30310 1.00040 0.00000 1.000
C4 C 3.31750 -0.00000 0.00000 1.000
C5 C 0.00000 0.00000 0.00000 1.000
O6 O -1.00050 0.00510 0.00000 1.000
""" % dict(VERSION=VERSION))
## # ck -- ChemKin format
## XXX I don't know why this fails
## class TestCK(unittest.TestCase, WriteMixin):
## fmt = "ck"
## maxDiff = None
## def test_default(self):
## self.assertWriters(self.fmt, """\
## """)
# cml -- Chemical Markup Language
class TestCML(unittest.TestCase, WriteMixin):
fmt = "cml"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
<?xml version="1.0"?>
<molecule id="phenol" xmlns="http://www.xml-cml.org/schema">
<atomArray>
<atom id="a1" elementType="C" hydrogenCount="1" x2="1.584600" y2="-0.024900"/>
<atom id="a2" elementType="C" hydrogenCount="1" x2="1.570300" y2="0.975500"/>
<atom id="a3" elementType="C" hydrogenCount="1" x2="2.429500" y2="1.488200"/>
<atom id="a4" elementType="C" hydrogenCount="1" x2="3.303100" y2="1.000400"/>
<atom id="a5" elementType="C" hydrogenCount="1" x2="3.317500" y2="-0.000000"/>
<atom id="a6" elementType="C" hydrogenCount="0" x2="0.000000" y2="0.000000"/>
<atom id="a7" elementType="O" hydrogenCount="1" x2="-1.000500" y2="0.005100"/>
</atomArray>
<bondArray>
<bond atomRefs2="a1 a6" order="2"/>
<bond atomRefs2="a1 a2" order="1"/>
<bond atomRefs2="a2 a3" order="2"/>
<bond atomRefs2="a3 a4" order="1"/>
<bond atomRefs2="a4 a5" order="2"/>
<bond atomRefs2="a5 a6" order="1"/>
<bond atomRefs2="a6 a7" order="1"/>
</bondArray>
</molecule>
""")
def test_multimol_default(self):
# Write two phenols.
# When there are 2 or more molecules then each molecule
# is wrapped in a <cml> element.
self.assertWriteMultiFile("cml", """\
<?xml version="1.0"?>
<cml xmlns="http://www.xml-cml.org/schema">
<molecule id="phenol">
<atomArray>
<atom id="a1" elementType="C" hydrogenCount="1" x2="1.584600" y2="-0.024900"/>
<atom id="a2" elementType="C" hydrogenCount="1" x2="1.570300" y2="0.975500"/>
<atom id="a3" elementType="C" hydrogenCount="1" x2="2.429500" y2="1.488200"/>
<atom id="a4" elementType="C" hydrogenCount="1" x2="3.303100" y2="1.000400"/>
<atom id="a5" elementType="C" hydrogenCount="1" x2="3.317500" y2="-0.000000"/>
<atom id="a6" elementType="C" hydrogenCount="0" x2="0.000000" y2="0.000000"/>
<atom id="a7" elementType="O" hydrogenCount="1" x2="-1.000500" y2="0.005100"/>
</atomArray>
<bondArray>
<bond atomRefs2="a1 a6" order="2"/>
<bond atomRefs2="a1 a2" order="1"/>
<bond atomRefs2="a2 a3" order="2"/>
<bond atomRefs2="a3 a4" order="1"/>
<bond atomRefs2="a4 a5" order="2"/>
<bond atomRefs2="a5 a6" order="1"/>
<bond atomRefs2="a6 a7" order="1"/>
</bondArray>
</molecule>
<molecule id="phenol">
<atomArray>
<atom id="a1" elementType="C" hydrogenCount="1" x2="1.584600" y2="-0.024900"/>
<atom id="a2" elementType="C" hydrogenCount="1" x2="1.570300" y2="0.975500"/>
<atom id="a3" elementType="C" hydrogenCount="1" x2="2.429500" y2="1.488200"/>
<atom id="a4" elementType="C" hydrogenCount="1" x2="3.303100" y2="1.000400"/>
<atom id="a5" elementType="C" hydrogenCount="1" x2="3.317500" y2="-0.000000"/>
<atom id="a6" elementType="C" hydrogenCount="0" x2="0.000000" y2="0.000000"/>
<atom id="a7" elementType="O" hydrogenCount="1" x2="-1.000500" y2="0.005100"/>
</atomArray>
<bondArray>
<bond atomRefs2="a1 a6" order="2"/>
<bond atomRefs2="a1 a2" order="1"/>
<bond atomRefs2="a2 a3" order="2"/>
<bond atomRefs2="a3 a4" order="1"/>
<bond atomRefs2="a4 a5" order="2"/>
<bond atomRefs2="a5 a6" order="1"/>
<bond atomRefs2="a6 a7" order="1"/>
</bondArray>
</molecule>
</cml>
""")
## # cmlr -- CML Reaction format
## XXX I don't know why the result is the empty string
## class TestCMLR(unittest.TestCase, WriteMixin):
## fmt = "cmlr"
## maxDiff = None
## def test_default(self):
## self.assertWriters(self.fmt, """\
## """, mol=_alchemy_mol)
# com -- Gaussian 98/03 Input [Write-only]
class TestCOM(unittest.TestCase, WriteMixin):
fmt = "com"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
!Put Keywords Here, check Charge and Multiplicity.
#
phenol
0 1
C 1.58460 -0.02490 0.00000
C 1.57030 0.97550 0.00000
C 2.42950 1.48820 0.00000
C 3.30310 1.00040 0.00000
C 3.31750 -0.00000 0.00000
C 0.00000 0.00000 0.00000
O -1.00050 0.00510 0.00000
""")
## # confabreport -- Confab report format [Write-only]
## XXX no conformations
## class TestCONFABREPORT(unittest.TestCase, WriteMixin):
## fmt = "confabreport"
## maxDiff = None
## def test_default(self):
## self.assertWriters(self.fmt, """\
## """)
# CONFIG -- DL-POLY CONFIG
class TestCONFIG(unittest.TestCase, WriteMixin):
fmt = "CONFIG"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
phenol
0 0
C 1 6
1.584600000000000 -0.024900000000000 0.000000000000000
C 2 6
1.570300000000000 0.975500000000000 0.000000000000000
C 3 6
2.429500000000000 1.488200000000000 0.000000000000000
C 4 6
3.303100000000000 1.000400000000000 0.000000000000000
C 5 6
3.317500000000000 -0.000000000000000 0.000000000000000
C 6 6
0.000000000000000 0.000000000000000 0.000000000000000
O 7 8
-1.000500000000000 0.005100000000000 0.000000000000000
""")
# CONTCAR -- VASP format
class TestCONTCAR(unittest.TestCase, WriteMixin):
fmt = "CONTCAR"
maxDiff = None
def test_default(self):
self.assertWriters(self.fmt, """\
phenol
1.000
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
C O
6 1
Cartesian
1.5846000000000000085 -0.0248999999999999985 0.0000000000000000000
1.5703000000000000291 0.9755000000000000338 0.0000000000000000000
2.4294999999999999929 1.4881999999999999673 0.0000000000000000000
3.3031000000000001471 1.0003999999999999559 0.0000000000000000000
3.3174999999999998934 -0.0000000000000000000 0.0000000000000000000
0.0000000000000000000 0.0000000000000000000 0.0000000000000000000
-1.0004999999999999449 0.0051000000000000004 0.0000000000000000000
""")
# CONTFF -- MDFF format
class TestCONTFF(unittest.TestCase, WriteMixin):
fmt = "CONTFF"
maxDiff = None
def test_default(self):