-
Notifications
You must be signed in to change notification settings - Fork 31
/
introduction.cc
1687 lines (1389 loc) · 55.3 KB
/
introduction.cc
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
// An introduction to LillyMol.
// The most central object is the Molecule object. It has a large
// number of member functions, with many overloaded names.
// Most often a Molecule is introduced to a programme via
// a data_source_and_type<Molecule> producer object, which
// iteratively returns a newly allocated Molecule from an
// input stream.
// Alternatively, you can instantiate a Molecule, and either
// add atoms to it, or build it from a smiles string.
// A Molecule object contains a vector of Atoms, and these atoms
// can be iterated via a C++ range for loop. But almost all
// times an Atom is passed from the Molecule it will be const.
// The reason for this is that if certain atomic properties are
// altered, then certain molecular properties would need to be
// either changed or invalidated. For example, if an atom were
// to be assigned a new isotopic value, the smiles for the molecule
// must be recomputed.
// In reality, Molecule is lazy, and will only recompute the smiles
// if requested. Changing the isotope just invalidates the smiles.
// Similariy, removing a bond between atoms would need to invalidate
// fragment membership, ring membership, symmetry, aromaticity...
// But again, those quantities will only be recomputed if needed.
// One question that comes up is why is there so little use of
// standard data structures like std::string, std::vector etc.
// LillyMol began development in 1995, and at that time these
// concepts either did not exist or were poorly standardized.
// Over many years we fought with incompatible implementations
// of various things and in order to preserve sanity, implemented
// our own versions of things that offered cross-platform
// stability. Today the C++ world is in better shape.
// Nothing here raises exceptions. Generally LillyMol is
// thread safe. There are a great many optional settings that
// are stored in file scope static variables. Those are
// clearly not thread safe, but are usually set just once.
// With care, LillyMol has been successfully used in several
// multi-threaded applications.
#include <algorithm>
#include <iostream>
#include <memory>
#include <vector>
#include "google/protobuf/text_format.h"
#include "Molecule_Lib/molecule.h"
#include "Molecule_Lib/output.h"
#include "Molecule_Lib/path.h"
#include "Molecule_Lib/substructure.h"
#include "Molecule_Lib/target.h"
namespace lillymol_introcution {
using std::cerr;
// An empty molecule has no atoms or bonds, but can have a name.
void
DemoEmptyMolecule() {
Molecule mol;
mol.set_name("foo");
// Should have zero atoms and molecular weight.
cerr << "Empty molecule '" << mol.name() << "' has " << mol.natoms()
<< " atoms, amw " << mol.molecular_weight() << '\n';
cerr << "Is the molecule empty " << mol.empty() << '\n';
}
// Using operator << with a string concatenates to the molecule name.
void
DemoStringConcat() {
Molecule mol;
mol << "hello";
cerr << "Name updated to '" << mol.name() << "'\n";
mol << " world";
cerr << "Name updated to '" << mol.name() << "'\n";
}
// When presented with an invalid smiles, build_from_smiles will fail.
void
DemoCannotParseBadSmiles() {
Molecule mol;
if (mol.build_from_smiles("invalid")) {
cerr << "Building from bad smiles succeeded, this should not happen.\n";
} else {
cerr << "Bad smiles cannot be parsed, good outcome.\n";
}
}
void
DemoAtomicNumbers() {
Molecule mol;
if (!mol.build_from_smiles("C")) {
cerr << "Unable to build methane!\n";
return;
}
mol.set_name("methane");
cerr << mol.name() << " has " << mol.natoms() << " atoms\n";
// There are three ways to find the atomic number of atom atom.
// Ask the molecule for the atomic number of a given atom.
// We only have one atom in the molecule, so it is atom number 0.
cerr << "Molecule's atomic number 0 " << mol.atomic_number(0) << '\n';
// We can fetch an Atom and ask the atom for its atomic number.
const Atom& a = mol.atom(0);
cerr << "Atom's atomic number " << a.atomic_number() << '\n';
// We can ask the atom for its Element and query the atomic number
const Element* e = a.element();
cerr << "Element's atomic number " << e->atomic_number() << '\n';
}
// Return a newly created molecule built from `smiles`.
// The molecule name is not set.
// If smiles interpretation fails, an empty molecule is returned.
// A better approach would be to use std::optional.
// Note that this may silently invoke a copy operation.
Molecule
MolFromSmiles(const char* smiles) {
Molecule result;
if (!result.build_from_smiles(smiles)) {
cerr << "Invalid smiles '" << smiles << "'\n";
return Molecule();
}
return result;
}
// A very nice interface, but beware of copy operations happening.
std::optional<Molecule>
OptionalMolFromSmiles(const char* smiles) {
Molecule result;
if (!result.build_from_smiles(smiles)) {
cerr << "Invalid smiles '" << smiles << "'\n";
return std::nullopt;
}
return result;
}
void
DemoOptionalMolFromSmiles() {
std::optional<Molecule> m1 = OptionalMolFromSmiles("foo");
if (m1) { // should not happen.
cerr << "OptionalMolFromSmiles suceeded for invalid smiles!!\n";
} else {
cerr << "Good, OptionalMolFromSmiles failed with invalid input\n";
}
std::optional<Molecule> m2 = OptionalMolFromSmiles("C1C2CC3CC1CC(C2)C3");
if (! m2) {
cerr << "OptionalMolFromSmiles did not parse adamantane\n";
return;
}
cerr << "Adamantane has " << m2->nrings() << " SSSR rings\n";
}
// In LillyMol, there is a periodic table datastructure which contains
// an Element object for all elements known to the system. We can
// fetch pointers to those elements via various means.
// For historical reasons, to get an element from a two letter element
// we must use get_element_from_symbol_no_case_conversion. This should
// be fixed. We had some early mdl files where the symbols were all
// uppercase.
void
DemoElements() {
const Element* carbon = get_element_from_atomic_number(6);
const Element* uranium = get_element_from_symbol_no_case_conversion("U");
const Element* chlorine = get_element_from_symbol_no_case_conversion("Cl");
const Element* iron = get_element_from_atomic_number(26);
cerr << "Carbon is " << carbon->symbol() << " atnum " << carbon->atomic_number() << '\n';
cerr << "normal isotope " << carbon->normal_isotope() << '\n';
cerr << "atomic_mass " << carbon->atomic_mass() << '\n';
cerr << "exact_mass " << carbon->exact_mass() << '\n';
cerr << "normal_valence " << carbon->normal_valence() << '\n';
cerr << "number_alternate_valences " << carbon->number_alternate_valences() << '\n';
cerr << "carbon organic " << carbon->organic() << '\n';
cerr << "uranium organic " << uranium->organic() << '\n';
cerr << "carbon needs_square_brackets " << carbon->needs_square_brackets() << '\n';
cerr << "uranium needs_square_brackets " << uranium->needs_square_brackets() << '\n';
cerr << "carbon is_halogen " << carbon->is_halogen() << '\n';
cerr << "uranium is_halogen " << uranium->is_halogen() << '\n';
cerr << "chlorine is_halogen " << chlorine->is_halogen() << '\n';
cerr << "carbon is_in_periodic_table " << carbon->is_in_periodic_table() << '\n';
cerr << "uranium is_in_periodic_table " << uranium->is_in_periodic_table() << '\n';
cerr << "carbon is_metal " << carbon->is_metal() << '\n';
cerr << "iron is_metal " << iron->is_metal() << '\n';
// Computing the number of pi electrons and lone pairs from an element is
// complex and you are better off asking a Molecule for the pi electron
// or lone pair count on a given atom.
}
void
DemoAtomIteration() {
Molecule mol = MolFromSmiles("CCC");
// Loop through the atoms in `mol`, writing the atomic number.
// Note that the iterator returns a pointer to an Atom.
for (const Atom* atom : mol) {
cerr << " atomic_number " << atom->atomic_number() << '\n';
}
// Or we can iterate based on atom numbers. This time we
// can get a reference to an atom.
const int matoms = mol.natoms();
for (int i = 0; i < matoms; ++i) {
const Atom& atom = mol.atom(i);
cerr << " atomic number " << atom.atomic_number() << '\n';
}
}
void
DemoBondIteration() {
Molecule mol = MolFromSmiles("CC");
for (const Bond* b : mol.bond_list()) {
cerr << *b << '\n';
}
}
// Molecules have an operator= overload and operator ==
void
DemoAssignment() {
Molecule mol1 = MolFromSmiles("Oc1ccccc1");
Molecule mol2 = mol1;
cerr << "Smiles " << mol1.unique_smiles() << ' ' << mol2.unique_smiles() << '\n';
// The operator == method is efficient, in that it only computes
// the unique smiles if needed.
cerr << "Same? " << (mol1 == mol2) << '\n';
// Add a carbon atom to each molecule.
mol1.add(get_element_from_atomic_number(6));
cerr << "Same after adding " << (mol1 == mol2) << '\n';
mol2.add(get_element_from_atomic_number(6));
cerr << "Same after adding to both " << (mol1 == mol2) << '\n';
}
// The natoms method is overloaded so that if an atomic number, or
// Element, is given the number of atoms of that type is returned.
void
DemoNatoms() {
Molecule mol = MolFromSmiles("OCN");
cerr << "Molecule contains " << mol.natoms() << " atoms\n";
cerr << "Oxygen atoms " << mol.natoms(8) << '\n';
const Element* fluorine = get_element_from_atomic_number(9);
cerr << "Molecule does not contain F " << mol.natoms(fluorine) << '\n';
}
// There are a wide variety of Molecule member functions for
// getting and setting isotopes.
void
DemoIsotopes() {
Molecule mol = MolFromSmiles("CC");
mol.set_isotope(0, 1);
mol.set_isotope(1, 2);
// Isotopes can be retrieved by querying either the molecule or the atom.
cerr << "Isotope on atom 0 " << mol.isotope(0) << '\n';
for (const Atom* atom : mol) {
cerr << " isotope on atom " << atom->isotope() << '\n';
}
// Isotopes are arbitrary, unsigned, numbers.
mol.set_isotope(0, 999);
mol.set_isotope(1, std::numeric_limits<uint32_t>::max());
cerr << "Ridiculous isotopes " << mol.smiles() << '\n';
// We should be able to build a new molecule from `mol`
// and their unique smiles should be the same.
Molecule round_trip;
round_trip.build_from_smiles(mol.smiles());
cerr << "Same? " << (mol.unique_smiles() == round_trip.unique_smiles()) << '\n';
// Remove all isotopes.
// unset_isotopes() does the same thing.
mol.transform_to_non_isotopic_form();
}
void
DemoIsotopesMolecularWeight() {
Molecule mol = MolFromSmiles("OCN");
float amw = mol.molecular_weight();
cerr << "Non isotopic molecular weight " << amw << '\n';
mol.set_isotope(0, 1);
amw = mol.molecular_weight();
cerr << "With isotopes, molecular weight is zero " << amw << '\n';
amw = mol.molecular_weight_ignore_isotopes();
cerr << "If isotopes are ignored, amw " << amw << '\n';
// Isotope 1 gets counted as contributing 1.0 to the amw.
amw = mol.molecular_weight_count_isotopes();
cerr << "If isotopes included " << amw << '\n';
}
// Not all atoms need to have an atom map number. It is
// really just another arbitrary number that can be attached
// to an atom. They have particular meaning when dealing with
// reactions.
void
DemoAtomMapNumbers() {
Molecule mol = MolFromSmiles("[CH3:5]-C");
// Atom map numbers are not shown in a unique smiles. Atom map numbers
// do not affect unique smiles formation.
// Isotopes do and so they are included.
cerr << "Smiles " << mol.smiles() << " usmi " << mol.unique_smiles() << '\n';
// THe atom map number is a property of the Atom, so can be queried at
// that level.
for (const Atom* atom : mol) {
cerr << "Atom map " << atom->atom_map() << '\n';
}
mol.reset_all_atom_map_numbers();
// Note that removing the atom map numbers also removes the
// implicit hydrogens known flag if possible - if the valence
// is OK.
cerr << "After removing atom map numbers " << mol.smiles() << '\n';
// For many functions which return an atom number, returning INVALID_ATOM_NUMBER
// is a common failure mode.
atom_number_t twelve = mol.atom_with_atom_map_number(12);
if (twelve != INVALID_ATOM_NUMBER) {
cerr << "HUH, found atom map number 12\n";
}
}
void
DemoImplicitAndExplicitHydrogens() {
Molecule mol = MolFromSmiles("OCN");
// Note that the ncon method, the number of connections to
// an atom, does NOT reflect implicit Hydrogens.
int matoms = mol.natoms();
cerr << "Molecule begins with " << matoms << " atoms\n";
for (int i = 0; i < matoms; ++i) {
cerr << "Atom " << i << " ncon " << mol.ncon(i) << " connections has " << mol.hcount(i) << " Hydrogens\n";
}
// Convert implicit Hydrogens to explicit.
// Note that the hcount for the existing atoms, natoms, should
// be unchaged, because the hcount() method includes both
// explicit and implicit Hydrogens.
// but now that the formerly implicit Hydrogens are now
// explicit members of the connection table, the ncon values
// for the existing atoms will change.
cerr << "After implicit Hydrogens become explicit\n";
mol.make_implicit_hydrogens_explicit();
for (int i = 0; i < matoms; ++i) {
cerr << "Atom " << i << " ncon " << mol.ncon(i) << " connections has " << mol.hcount(i) << " Hydrogens\n";
}
cerr << "Molecule now contains " << mol.natoms() << " atoms\n";
}
// One of the most troublesome parts of LillyMol is the
// implicit hydrogens known attribute. If a smiles begins with
// C-[CH]-C where the centre Carbon is deficient one Hydrogen
// what happens when we add a Carbon atom to it, thereby
// satisfying the valence.
void
DemoImplicitHydrogensKnown() {
Molecule mol = MolFromSmiles("C-[CH]-C");
cerr << "Valence? " << mol.valence_ok() << '\n';
// Indeed the problem is with atom 1.
for (int i = 0; i < mol.natoms(); ++i) {
cerr << " atom " << i << " valence " << mol.valence_ok(i) << '\n';
}
// Add a carbon atom.
// First fetch a pointer to the Element with atomic number 6.
// Then call mol.add() with a pointer to the Element.
// Internally, mol will instantiate an Atom which has
// that element.
const Element* carbon = get_element_from_atomic_number(6);
mol.add(carbon);
// The molecule now has two fragments.
cerr << "Molecule has " << mol.number_fragments() << " fragments\n";
// Add a bond from this last atom added to the middle atom to satisfy the valence.
// The newly added atom will always have the highest atom number.
mol.add_bond(1, 3, SINGLE_BOND);
cerr << "Now " << mol.number_fragments() << " fragments\n";
// Is the valence ok now?
cerr << "After atom addition valence? " << mol.valence_ok() << '\n';
// But the smiles shows that the centre atom still has its
// implicit Hydrogens known attribute set. Even though it
// is no longer necessary.
cerr << "Smiles now " << mol.smiles() << '\n';
cerr << "Is the value fixed " << mol.implicit_hydrogens_known(1) << '\n';
// Remove the implicit Hydrogen known attribute.
// Now the smiles does not have the square brackets.
mol.set_implicit_hydrogens_known(1, 0);
cerr << "Smiles now " << mol.smiles() << '\n';
cerr << "Is the value fixed " << mol.implicit_hydrogens_known(1) << '\n';
// This function can also be used, it examines all atoms and if possible
// unsets the implicit hydrogen known flag.
// In this case, the Carbon naturally has 3 Hydrogens, so the square
// brackets are not necessary.
Molecule mol2 = MolFromSmiles("C[CH3]");
cerr << "Before removing unnecessary square brackets " << mol2.smiles() << '\n';
mol2.unset_unnecessary_implicit_hydrogens_known_values();
cerr << "After removing unnecessary square brackets " << mol2.smiles() << '\n';
// If an implicit hydrogen flag is causing a valence error, this can
// alleviate that problem.
mol.build_from_smiles("C[C]C");
cerr << "Before removing valence problem hydrogens known " << mol.smiles() <<
" valence? " << mol.valence_ok() << '\n';
mol.remove_hydrogens_known_flag_to_fix_valence_errors();
cerr << "After removing valence problem hydrogens known " << mol.smiles() <<
" valence? " << mol.valence_ok() << '\n';
}
// A data structure commonly encountered in LillyMol is a
// Set_of_Atoms. Despite the name, it is a vector of atom
// numbers, and there is no enforcement of uniqueness.
// It was a poor choice of name.
// Several Molecule member functions either consume or
// return Set_of_Atoms.
// Generally the ordering of the atoms in a Set_of_Atoms is
// unimportant. With an important exception.
// Rings inherit from a Set_of_Atoms, but they have the
// extra property that the atoms are ordered in a way
// that describes a circuit around the ring.
void
DemoSetOfAtoms() {
Set_of_Atoms s{0, 5, 2};
cerr << "Set contains " << s.size() << " atoms " << s << '\n';
// Duplicate atom numbers are OK in a Set_of_Atoms, but
// functions consuming them may be unhappy with duplicate
// entries.
s << 2;
cerr << "See the dup " << s << '\n';
// Remove the dup.
s.chop();
s.add_if_not_already_present(2);
cerr << "Dup not added " << s << '\n';
// These have a convenient `scatter` operation.
std::unique_ptr<int[]> arbitrary = std::make_unique<int[]>(10);
std::fill_n(arbitrary.get(), 10, 0);
// For every atom number in `s`, set the corresponding array index to '2'.
s.set_vector(arbitrary.get(), 2);
// this also works with a std::vector.
// There are many gather type operations associated with the
// Set_of_Atoms class. See the header for info.
}
// Random smiles are useful for testing. Algorithms should not
// depend on the order of the smiles.
void
DemoRandomSmiles() {
// Caffeine
Molecule mol = MolFromSmiles("CN1C=NC2=C1C(=O)N(C(=O)N2C)C");
// Note that we make a copy of the unique smiles.
// Generally we get a reference to the unique smiles,
// IWString& starting_unique_smiles = mol.unique_smiles()
// But in this case the smiles of `mol` will be destroyed when the
// random smiles are formed below, and the reference would
// be invalid.
const IWString starting_unique_smiles = mol.unique_smiles();
for (int i = 0; i < 10; ++i) {
cerr << " random smiles " << mol.random_smiles() << '\n';
Molecule hopefully_the_same;
hopefully_the_same.build_from_smiles(mol.random_smiles());
cerr << "Same? " << (starting_unique_smiles == hopefully_the_same.unique_smiles()) << '\n';
}
}
//
void
DemoRemoveAtom() {
Molecule mol = MolFromSmiles("CCCCCCCCCCCCC");
cerr << "Before atom removals, molecule contains " << mol.natoms() << ' ' << mol.smiles() << '\n';
// Any atom can be removed with remove_atom.
mol.remove_atom(0);
cerr << "Molecule now contains " << mol.natoms() << " atoms " << mol.smiles() << "\n";
// Atom removal may lead to multiple fragments
mol.remove_atom(1);
cerr << "Molecule now contains " << mol.natoms() << " atoms " << mol.smiles() << "\n";
// If you wish to remove multiple atoms, that can be done one at a time.
// It will be most efficient if you iterate from high to low atom numbers.
// for (int i = mol.natoms() - 1; i >= 0; --i) {
// if (some condition) {
// mol.remove_atom(i);
// }
// }
// If you have an array with the atoms to be removed set, use that.
const int matoms = mol.natoms();
std::unique_ptr<int[]> to_remove = std::make_unique<int[]>(matoms);
std::fill_n(to_remove.get(), matoms, 0);
for (int i = 0; i < matoms; i += 2) {
to_remove[i] = 1;
}
mol.remove_atoms(to_remove.get());
cerr << "After removing every second atom " << mol.smiles() << '\n';
}
void
DemoFormalCharges() {
Molecule mol = MolFromSmiles("CC[N+H3]");
cerr << "Does the molecule have formal charges " << mol.has_formal_charges() <<
" net " << mol.net_formal_charge() << '\n';
cerr << "Charge " << mol.formal_charge(2) << '\n';
cerr << "Charge on atom " << mol.atom(2).formal_charge() << '\n';
mol.set_formal_charge(2, 0);
cerr << "After reset " << mol.formal_charge(2) << '\n';
}
// Partial charges are a property of the molecule only.
// The molecule starts with the partial charge being null.
// The first time you set the partial charge on any atom,
// the partial charge array is allocated and set to zero.
void
DemoPartialCharges() {
Molecule mol = MolFromSmiles("CC");
cerr << "Should be no partial charges " << mol.has_partial_charges() << '\n';
mol.set_partial_charge(1, 0.22);
cerr << "After setting charge on atom 1 charge on 0 " << mol.partial_charge(0) << '\n';
// Removing an atom will preserve partial charges.
mol.remove_atom(0);
cerr << "Charges present now? " << mol.has_partial_charges() << '\n';
// Adding an atom will see that atom enter with zero partial charge.
mol.add(get_element_from_atomic_number(6));
cerr << "Partial charge on newly added atom " << mol.partial_charge(1) << '\n';
// Partial charges can be dropped.
mol.invalidate_partial_charges();
// Some partial charge types are available.
mol.compute_Abraham_partial_charges();
cerr <<"Partial charge type " << mol.partial_charge_type() << '\n';
}
void
DemoNcon() {
Molecule mol = MolFromSmiles("CC(C)(C)C");
// The molecule is
// [C:2]
// |
// |
// [C:0] -- [C:1] -- [C:4]
// |
// |
// [C:3]
// Atoms have different connectivity.
// We can get the degree of any atom by asking either the
// molecule, or the atom.
// In addition get a smarts equivalent for that atom - this
// is mostly useful for debugging.
const int matoms = mol.natoms();
for (int i = 0; i < matoms; ++i) {
const Atom& atom = mol.atom(i);
cerr << " atom " << i << " has " << mol.ncon(i) << " connections, atom "
<< atom.ncon() << " type " << mol.smarts_equivalent_for_atom(i) << '\n';
}
}
void
DemoBondIterationAroundAtom() {
Molecule mol = MolFromSmiles("CC(C)(C)C");
// For each atom, enumerate the connected atoms.
// Iteration is over Bond objects associated with each atom.
// The Bond contains two atom numbers, we use the bond->other
// method to determine what the other atom is.
const int matoms = mol.natoms();
for (int i = 0; i < matoms; ++i) {
const Atom& atom = mol.atom(i);
for (const Bond* bond : atom) {
cerr << "atom " << i << " bonded to " << bond->other(i) <<
" type " << bond->btype() << '\n';
}
}
}
// It is possible to iterate through the connections to an atom
// by iterating through the Molecule. It is more efficient and elegant
// to iterate via the Atom rather than via the Molecule.
void
DemoBondIterationMolecule() {
Molecule mol = MolFromSmiles("CO");
int ncon = mol.ncon(0);
for (int i = 0; i < ncon; ++i) {
atom_number_t j = mol.other(0, i);
cerr << "Atom 0 is bonded to " << j << '\n';
}
}
// While it is possible to get the connections to an
// atom, this is usually less efficient than iterating,
// since iterating does not require the creation of a
// Set_of_Atoms object.
void
DemoConnections() {
Molecule mol = MolFromSmiles("C(C)O");
const Set_of_Atoms connected = mol.connections(0);
cerr << " atoms connected to 0 " << connected << '\n';
}
// A common operation is the need to add all atoms and bonds
// from one molecule to another.
// There are variants of add_molecule that allow for
// excluding certain atoms from the added Molecule.
void
DemoAddMolecule() {
Molecule mol1 = MolFromSmiles("CC");
Molecule mol2 = MolFromSmiles("NN");
cerr << "mol1 contains " << mol1.natoms() << " atoms\n";
mol1.add_molecule(&mol2);
cerr << "mol1 contains " << mol1.natoms() << " atoms and " <<
mol1.number_fragments() << " fragments\n";
mol1.add_bond(1, 2, SINGLE_BOND);
cerr << "After bond addition " << mol1.number_fragments() << " fragments\n";
}
// An existing bond type can be changed.
// Note that the symbols SINGLE_BOND, DOUBLE_BOND, DOUBLE_BOND
// should not be interpreted as ints. The bits in these
// are used by LillyMol for various internal purposes.
void
DemoChangeBond() {
Molecule mol = MolFromSmiles("CC");
cerr << "Initial smiles " << mol.smiles() << '\n';
const std::vector<int> bonds{SINGLE_BOND, DOUBLE_BOND, TRIPLE_BOND};
for (auto btype : bonds) {
mol.set_bond_type_between_atoms(0, 1, btype);
cerr << "New bond " << mol.smiles() << '\n';
}
}
// It is possible to change the element associated with an atom.
// Existing bonds are preserved.
// It is up to you to ensure that the resulting chemistry is valid.
void
DemoChangeElement() {
Molecule mol = MolFromSmiles("CCC");
// Change second atom to Fluorine!
mol.set_atomic_number(1, 9);
cerr << "Doubly bonded Fluorine! " << mol.smiles() << '\n';
cerr << "ok valence? " << mol.valence_ok() << '\n';
}
// Start with ethane, verify one fragment, then remove the bond
// to generate two methanes.
void
DemoFragments() {
Molecule mol = MolFromSmiles("CC");
cerr << "Methane has " << mol.number_fragments() << " fragments\n";
cerr << "Ethane contains only one edge " << mol.nedges() << '\n';
// there are only two atoms, remove the bond between them.
mol.remove_bond_between_atoms(0, 1);
// We should have two fragments now.
cerr << "After bond removal " << mol.number_fragments() << " fragments\n";
// Fragment membership is only known by the Molecule.
// Fragments are assigned sequential numbers, starting with zero.
// We can ask the molecule, in which fragment is the i'th atom?
// Atom's do not know anything about fragment membership.
const int matoms = mol.natoms();
for (int i = 0; i < matoms; ++i) {
cerr << " atom " << i << " in fragment " << mol.fragment_membership(i) << '\n';
// const Atom& atom = mol.atom(i);
// atom.fragment_membership() CANNOT WORK
}
// we can ask the molecule how many atoms are in each fragment.
const int nf = mol.number_fragments();
for (int i = 0; i < nf; ++i) {
cerr << " Fragment " << i << " contains " << mol.atoms_in_fragment(i) << " atoms\n";
}
// To determine if two atoms are in the same fragment, compare
// mol.fragment_membership(i) == mol.fragment_membership(j);
// Put the molecule back by adding a bond
mol.add_bond(0, 1, SINGLE_BOND);
cerr << "After recreating nfrag " << mol.number_fragments() << '\n';
cerr << "Same frag " << (mol.fragment_membership(0) == mol.fragment_membership(1)) << '\n';
}
// when we have a multi-fragment molecule, we can create
// molecules from each fragment.
void
DemoCreateComponents() {
Molecule mol = MolFromSmiles("C.C.C.C.C.C.C.C.C");
resizable_array_p<Molecule> fragments;
mol.create_components(fragments);
cerr << "Created " << fragments.size() << " fragments from " << mol.smiles() << '\n';
// The fragments should all have the same smiles.
// Note that we cannot use 'const Molecule* m' here because generating
// a smiles is a potentially non-const operation - see lazy evaluation.
for (Molecule* m : fragments) {
if (m->unique_smiles() != "C") {
cerr << "Invalid fragment unique smiles\n";
}
}
// We can remove a fragment by fragment number
mol.delete_fragment(2);
// Or remove whatever fragment contains atom 3
mol.remove_fragment_containing_atom(3);
}
// Normally getting the largest fragment is straightforward and
// yields the expected result.
void
DemoReduceToLargestFragment() {
Molecule mol = MolFromSmiles("CC.C");
mol.reduce_to_largest_fragment();
cerr << "after reduce_to_largest_fragment have " << mol.natoms() << " atoms " << mol.smiles() << '\n';
// There are however cases where just choosing the largest
// fragment is not the best choice. Prefer using
// reduce_to_largest_fragment_carefully() which has
// heuristics that help avoid some common problems.
}
// create_subset creates a new molecule that contains
// just some of the atoms in the starting molecule.
// Pass an int*
void
DemoCreateSubset() {
Molecule mol = MolFromSmiles("c1ccccc1OC");
const int matoms = mol.natoms();
std::unique_ptr<int[]> subset = std::make_unique<int[]>(matoms);
std::fill_n(subset.get(), matoms, 0);
// The first 6 atoms are the benzene ring. That is the subset.
// Mark the first 6 entries with a non-zero integer.
constexpr int kOne = 1;
std::fill_n(subset.get(), 6, kOne);
Molecule benzene;
mol.create_subset(benzene, subset.get(), kOne);
cerr << "Should be benzene " << benzene.unique_smiles() << '\n';
}
void
DemoDistanceMatrix() {
Molecule mol = MolFromSmiles("CCCCCCCCCC");
cerr << "Longest path " << mol.longest_path() << '\n';
const int matoms = mol.natoms();
for (int i = 0; i < matoms; ++i) {
for (int j = i + 1; j < matoms; ++j) {
int d = mol.bonds_between(i, j);
if (d == 1) {
cerr << " atoms " << i << " and " << j << " are bonded\n";
} else {
cerr << " atoms " << i << " and " << j << " are " << d << " bonds apart\n";
}
}
}
}
void
DemoRing() {
// Adjacent three and four membered rings.
Molecule mol = MolFromSmiles("C1CCC1C1CC1");
// We should get two rings.
cerr << "Molecule contains " << mol.nrings() << " rings\n";
// As listed here, the ring will have no status for aromaticity,
// because aromaticity has not been determined.
const int nr = mol.nrings();
for (int i = 0; i < nr; ++i) {
const Ring* ring = mol.ringi(i);
cerr << " ring " << i << " contains " << ring->size() << " atoms\n";
cerr << " atoms " << *ring << '\n';
}
// We can check whether atoms are in the same ring
cerr << "same ring 0,1 " << mol.in_same_ring(0, 1) << '\n';
cerr << "same ring 3,4 " << mol.in_same_ring(3, 4) << '\n';
cerr << "same ring 4,5 " << mol.in_same_ring(4, 5) << '\n';
// We can ask the molecule about the ring membership of an atom.
// Note that Atom objects do not know anything about their ring status.
const int matoms = mol.natoms();
for (int i = 0; i < matoms; ++i) {
cerr << " Atom " << i << ' ' << mol.smarts_equivalent_for_atom(i) <<
" in " << mol.nrings(i) << " rings, ring bond count " << mol.ring_bond_count(i) << '\n';
}
// Sometimes it is convenient to just get all the ring membership values in
// an array - saves the overhead of querying the Molecule multiple times.
// If `mol` is changed however, this pointer becomes invalid.
const int * ring_membership = mol.ring_membership();
// Querying the molecule for each atom is however safe if the molecule is
// being altered.
int ring_atoms = 0;
for (int i = 0; i < matoms; ++i) {
if (ring_membership[i] > 0) {
++ring_atoms;
}
}
cerr << ring_atoms << " of " << matoms << " atoms in a ring\n";
// If you have your own array, you can call mol.ring_membership(your_array) and
// it will be filled with the ring membership of each atom. Here if the
// molecule gets changed, you are on your own.
}
// There is a convenient Ring iterator.
void
DemoRingIteration () {
// Cubane has 6 faces, but 5 sssr rings.
Molecule mol = MolFromSmiles("C12C3C4C1C5C2C3C45");
cerr << "Cubane has " << mol.nrings() << " SSSR rings\n";
cerr << "Cubane has " << mol.non_sssr_rings() << " non SSSR rings\n";
for (const Ring* r : mol.sssr_rings()) {
cerr << " cubane ring " << *r << '\n';
}
int non_ssr_rings = mol.non_sssr_rings();
for (int i = 0; i < non_ssr_rings; ++i) {
cerr << " cubane NON SSSR " << *mol.non_sssr_ring(i) << '\n';
}
}
void
DemoRingSystem() {
// Fused 3 and 4 membered rings.
// C ---- C *
// | | \ *
// | | C *
// | | / *
// C ---- C *
Molecule mol = MolFromSmiles("C12CCC1C2");
// Contains 2 rings.
cerr << "Molecule contains " << mol.nrings() << " rings\n";
// The two rings must have the same fused system identifier
// Generally rings are sorted by size so the smallest rings should be first.
for (const Ring* r : mol.sssr_rings()) {
cerr << *r << '\n';
cerr << "fused rings:fused_system_identifier " << r->fused_system_identifier() << '\n';
cerr << "is_fused " << r->is_fused() << '\n';
}
cerr << "Atoms in same ring system " << mol.in_same_ring_system(1, 2) << '\n';
// only write a failure - cut down on uninteresting output.
const int matoms = mol.natoms();
for (int i = 0; i < matoms; ++i) {
for (int j = i + 1; j < matoms; ++j) {
if (! mol.in_same_ring_system(i, j)) {
cerr << "Atoms " << i << " and " << j << " not in same ring system\n";
}
}
}
// For each ring, we can fetch pointers to the Ring's attached.
// In this case, where will be two atoms in common between the two rings.
const int nr = mol.nrings();
for (int i = 0; i < nr; ++i) {
const Ring* ri = mol.ringi(i);
const int nfused = ri->fused_ring_neighbours();
for (int j = 0; j < nfused; ++j) {
const Ring* nbr = ri->fused_neighbour(j);
cerr << "ring " << *ri << " fused to " << *nbr << '\n';
}
}
// Two separated rings will have different fused_system_identifier
mol.build_from_smiles("C1CC1C1CC1");
for (const Ring* r : mol.sssr_rings()) {
cerr << "separated rings: fused_system_identifier " <<r->fused_system_identifier() << '\n';
cerr << "is_fused " << r->is_fused() << '\n';
}
}
void
DemoAreBonded() {
Molecule mol = MolFromSmiles("CC.C");
const int matoms = mol.natoms();
for (int i = 0; i < matoms; ++i) {
for (int j = i + 1; j < matoms; ++j) {
if (mol.are_bonded(i, j)) {
cerr << "Atoms " << i << " and " << j << " bonded\n";
} else {
cerr << "Atoms " << i << " and " << j << " not bonded\n";
}
}
}
}
// A LillyMol smiles extension is to allow coordinates in smiles.
// {{x,y,z}} is appended after each atom.
// This is more compact than a .sdf representation, and allows
// molecules to be processed one per line.
// Now shown here are the member functions
// set_bond_length, set_bond_angle and set_dihedral which are used
// to alter the geometry of a Molecule.
// rotate_atoms can rotate the whole molecule.
void
DemoCoordinatesInAtoms() {
Molecule mol = MolFromSmiles("C{{-1,1,0}}C{{0,0,0}}C{{1,0,0}}C{{2,1,1}}");
cerr << "Distance between 0 1 " << mol.distance_between_atoms(0, 1) << '\n';
cerr << "Bond angle 0 1 2 " << (mol.bond_angle(0, 1, 2) * RAD2DEG) << '\n';
cerr << "Dihedral angle 0 1 2 3 " << (mol.dihedral_angle(0, 1, 2, 3) * RAD2DEG) << '\n';
// Note that the distances above are geometric distances.
// Topological distances are frequently of interest.
const int matoms = mol.natoms();
for (int i = 0; i < matoms; ++i) {
for (int j = i + 1; j < matoms; ++j) {
cerr << "Bonds between " << i << " and " << j << ' ' << mol.bonds_between(i, j) << '\n';
}
}
// We can ask the molecule or an individual atom for their coordinates.
// Internally, the Molecule fetches the Atom.
cerr << "Atom 0 at " << mol.x(0) << ',' << mol.y(0) << ',' << mol.z(0) << '\n';
const Atom& atom = mol.atom(0);
cerr << "Atom 0 at " << atom.x() << ',' << atom.y() << ',' << atom.z() << '\n';
// We can get the spatial extremities of the molecule.
coord_t xmin, xmax, ymin, ymax, zmin, zmax;
mol.spatial_extremeties(xmin, xmax, ymin, ymax, zmin, zmax);
cerr << "X btw " << xmin << " and " << xmax << '\n';
// Move the molecule to a quadrant of space.
mol.translate_atoms(-xmin, -ymin, -zmin);