forked from openbabel/openbabel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmol.cpp
4394 lines (3797 loc) · 136 KB
/
mol.cpp
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
/**********************************************************************
mol.cpp - Handle molecules.
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
Some portions Copyright (C) 2001-2008 by Geoffrey R. Hutchison
Some portions Copyright (C) 2003 by Michael Banck
This file is part of the Open Babel project.
For more information, see <http://openbabel.org/>
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 version 2 of the License.
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.
***********************************************************************/
#include <openbabel/babelconfig.h>
#include <openbabel/mol.h>
#include <openbabel/bond.h>
#include <openbabel/ring.h>
#include <openbabel/rotamer.h>
#include <openbabel/phmodel.h>
#include <openbabel/bondtyper.h>
#include <openbabel/obiter.h>
#include <openbabel/builder.h>
#include <openbabel/kekulize.h>
#include <openbabel/internalcoord.h>
#include <openbabel/math/matrix3x3.h>
#include <openbabel/obfunctions.h>
#include <openbabel/elements.h>
#include <openbabel/stereo/tetrahedral.h>
#include <openbabel/stereo/cistrans.h>
#include <sstream>
#include <set>
using namespace std;
namespace OpenBabel
{
extern bool SwabInt;
extern THREAD_LOCAL OBPhModel phmodel;
extern THREAD_LOCAL OBAromaticTyper aromtyper;
extern THREAD_LOCAL OBAtomTyper atomtyper;
extern THREAD_LOCAL OBBondTyper bondtyper;
/** \class OBMol mol.h <openbabel/mol.h>
\brief Molecule Class
The most important class in Open Babel is OBMol, or the molecule class.
The OBMol class is designed to store all the basic information
associated with a molecule, to make manipulations on the connection
table of a molecule facile, and to provide member functions which
automatically perceive information about a molecule. A guided tour
of the OBMol class is a good place to start.
An OBMol class can be declared:
\code
OBMol mol;
\endcode
For example:
\code
#include <iostream.h>
#include <openbabel/mol.h>
#include <openbabel/obconversion.h>
int main(int argc,char **argv)
{
OBConversion conv(&cin,&cout);
if(conv.SetInAndOutFormats("SDF","MOL2"))
{
OBMol mol;
if(conv.Read(&mol))
...manipulate molecule
conv->Write(&mol);
}
return(1);
}
\endcode
will read in a molecule in SD file format from stdin
(or the C++ equivalent cin) and write a MOL2 format file out
to standard out. Additionally, The input and output formats can
be altered using the OBConversion class
Once a molecule has been read into an OBMol (or created via other methods)
the atoms and bonds
can be accessed by the following methods:
\code
OBAtom *atom;
atom = mol.GetAtom(5); //random access of an atom
\endcode
or
\code
OBBond *bond;
bond = mol.GetBond(14); //random access of a bond
\endcode
or
\code
FOR_ATOMS_OF_MOL(atom, mol) // iterator access (see OBMolAtomIter)
\endcode
or
\code
FOR_BONDS_OF_MOL(bond, mol) // iterator access (see OBMolBondIter)
\endcode
It is important to note that atom arrays currently begin at 1 and bond arrays
begin at 0. Requesting atom 0 (\code
OBAtom *atom = mol.GetAtom(0); \endcode
will result in an error, but
\code
OBBond *bond = mol.GetBond(0);
\endcode
is perfectly valid.
Note that this is expected to change in the near future to simplify coding
and improve efficiency.
The ambiguity of numbering issues and off-by-one errors led to the use
of iterators in Open Babel. An iterator is essentially just a pointer, but
when used in conjunction with Standard Template Library (STL) vectors
it provides an unambiguous way to loop over arrays. OBMols store their
atom and bond information in STL vectors. Since vectors are template
based, a vector of any user defined type can be declared. OBMols declare
vector<OBAtom*> and vector<OBBond*> to store atom and bond information.
Iterators are then a natural way to loop over the vectors of atoms and bonds.
A variety of predefined iterators have been created to simplify
common looping requests (e.g., looping over all atoms in a molecule,
bonds to a given atom, etc.)
\code
#include <openbabel/obiter.h>
...
#define FOR_ATOMS_OF_MOL(a,m) for( OBMolAtomIter a(m); a; ++a )
#define FOR_BONDS_OF_MOL(b,m) for( OBMolBondIter b(m); b; ++b )
#define FOR_NBORS_OF_ATOM(a,p) for( OBAtomAtomIter a(p); a; ++a )
#define FOR_BONDS_OF_ATOM(b,p) for( OBAtomBondIter b(p); b; ++b )
#define FOR_RESIDUES_OF_MOL(r,m) for( OBResidueIter r(m); r; ++r )
#define FOR_ATOMS_OF_RESIDUE(a,r) for( OBResidueAtomIter a(r); a; ++a )
...
\endcode
These convenience functions can be used like so:
\code
#include <openbabel/obiter.h>
#include <openbabel/mol.h>
OBMol mol;
double exactMass = 0.0;
FOR_ATOMS_OF_MOL(a, mol)
{
exactMass += a->GetExactMass();
}
\endcode
Note that with these convenience macros, the iterator "a" (or
whichever name you pick) is declared for you -- you do not need to
do it beforehand.
*/
//
// OBMol member functions
//
void OBMol::SetTitle(const char *title)
{
_title = title;
Trim(_title);
}
void OBMol::SetTitle(std::string &title)
{
_title = title;
Trim(_title);
}
const char *OBMol::GetTitle(bool replaceNewlines) const
{
if (!replaceNewlines || _title.find('\n')== string::npos )
return(_title.c_str());
//Only multiline titles use the following to replace newlines by spaces
static string title;
title=_title;
string::size_type j;
for ( ; (j = title.find_first_of( "\n\r" )) != string::npos ; ) {
title.replace( j, 1, " ");
}
return(title.c_str());
}
bool SortVVInt(const vector<int> &a,const vector<int> &b)
{
return(a.size() > b.size());
}
bool SortAtomZ(const pair<OBAtom*,double> &a, const pair<OBAtom*,double> &b)
{
return (a.second < b.second);
}
double OBMol::GetAngle( OBAtom* a, OBAtom* b, OBAtom* c)
{
return a->GetAngle( b, c );
}
double OBMol::GetTorsion(int a,int b,int c,int d)
{
return(GetTorsion((OBAtom*)_vatom[a-1],
(OBAtom*)_vatom[b-1],
(OBAtom*)_vatom[c-1],
(OBAtom*)_vatom[d-1]));
}
void OBMol::SetTorsion(OBAtom *a,OBAtom *b,OBAtom *c, OBAtom *d, double ang)
{
vector<int> tor;
vector<int> atoms;
obErrorLog.ThrowError(__FUNCTION__,
"Ran OpenBabel::SetTorsion", obAuditMsg);
tor.push_back(a->GetCoordinateIdx());
tor.push_back(b->GetCoordinateIdx());
tor.push_back(c->GetCoordinateIdx());
tor.push_back(d->GetCoordinateIdx());
FindChildren(atoms, b->GetIdx(), c->GetIdx());
int j;
for (j = 0 ; (unsigned)j < atoms.size() ; j++ )
atoms[j] = (atoms[j] - 1) * 3;
double v2x,v2y,v2z;
double radang,m[9];
double x,y,z,mag,rotang,sn,cs,t,tx,ty,tz;
//calculate the torsion angle
// TODO: fix this calculation for periodic systems
radang = CalcTorsionAngle(a->GetVector(),
b->GetVector(),
c->GetVector(),
d->GetVector()) / RAD_TO_DEG;
//
// now we have the torsion angle (radang) - set up the rot matrix
//
//find the difference between current and requested
rotang = ang - radang;
sn = sin(rotang);
cs = cos(rotang);
t = 1 - cs;
v2x = _c[tor[1]] - _c[tor[2]];
v2y = _c[tor[1]+1] - _c[tor[2]+1];
v2z = _c[tor[1]+2] - _c[tor[2]+2];
//normalize the rotation vector
mag = sqrt(SQUARE(v2x)+SQUARE(v2y)+SQUARE(v2z));
x = v2x/mag;
y = v2y/mag;
z = v2z/mag;
//set up the rotation matrix
m[0]= t*x*x + cs;
m[1] = t*x*y + sn*z;
m[2] = t*x*z - sn*y;
m[3] = t*x*y - sn*z;
m[4] = t*y*y + cs;
m[5] = t*y*z + sn*x;
m[6] = t*x*z + sn*y;
m[7] = t*y*z - sn*x;
m[8] = t*z*z + cs;
//
//now the matrix is set - time to rotate the atoms
//
tx = _c[tor[1]];
ty = _c[tor[1]+1];
tz = _c[tor[1]+2];
vector<int>::iterator i;
for (i = atoms.begin(); i != atoms.end(); ++i)
{
j = *i;
_c[j] -= tx;
_c[j+1] -= ty;
_c[j+2]-= tz;
x = _c[j]*m[0] + _c[j+1]*m[1] + _c[j+2]*m[2];
y = _c[j]*m[3] + _c[j+1]*m[4] + _c[j+2]*m[5];
z = _c[j]*m[6] + _c[j+1]*m[7] + _c[j+2]*m[8];
_c[j] = x;
_c[j+1] = y;
_c[j+2] = z;
_c[j] += tx;
_c[j+1] += ty;
_c[j+2] += tz;
}
}
double OBMol::GetTorsion(OBAtom *a,OBAtom *b,OBAtom *c,OBAtom *d)
{
if (!IsPeriodic())
{
return(CalcTorsionAngle(a->GetVector(),
b->GetVector(),
c->GetVector(),
d->GetVector()));
}
else
{
vector3 v1, v2, v3, v4;
// Wrap the atomic positions in a continuous chain that makes sense based on the unit cell
// Start by extracting the absolute Cartesian coordinates
v1 = a->GetVector();
v2 = b->GetVector();
v3 = c->GetVector();
v4 = d->GetVector();
// Then redefine the positions based on proximity to the previous atom
// to build a continuous chain of expanded Cartesian coordinates
OBUnitCell *unitCell = (OBUnitCell * ) GetData(OBGenericDataType::UnitCell);
v2 = unitCell->UnwrapCartesianNear(v2, v1);
v3 = unitCell->UnwrapCartesianNear(v3, v2);
v4 = unitCell->UnwrapCartesianNear(v4, v3);
return(CalcTorsionAngle(v1, v2, v3, v4));
}
}
void OBMol::ContigFragList(std::vector<std::vector<int> >&cfl)
{
int j;
OBAtom *atom;
OBBond *bond;
vector<OBAtom*>::iterator i;
vector<OBBond*>::iterator k;
OBBitVec used,curr,next,frag;
vector<int> tmp;
used.Resize(NumAtoms()+1);
curr.Resize(NumAtoms()+1);
next.Resize(NumAtoms()+1);
frag.Resize(NumAtoms()+1);
while ((unsigned)used.CountBits() < NumAtoms())
{
curr.Clear();
frag.Clear();
for (atom = BeginAtom(i);atom;atom = NextAtom(i))
if (!used.BitIsSet(atom->GetIdx()))
{
curr.SetBitOn(atom->GetIdx());
break;
}
frag |= curr;
while (!curr.IsEmpty())
{
next.Clear();
for (j = curr.NextBit(-1);j != curr.EndBit();j = curr.NextBit(j))
{
atom = GetAtom(j);
for (bond = atom->BeginBond(k);bond;bond = atom->NextBond(k))
if (!used.BitIsSet(bond->GetNbrAtomIdx(atom)))
next.SetBitOn(bond->GetNbrAtomIdx(atom));
}
used |= curr;
used |= next;
frag |= next;
curr = next;
}
tmp.clear();
frag.ToVecInt(tmp);
cfl.push_back(tmp);
}
sort(cfl.begin(),cfl.end(),SortVVInt);
}
void OBMol::FindAngles()
{
//if already has data return
if(HasData(OBGenericDataType::AngleData))
return;
//get new data and attach it to molecule
OBAngleData *angles = new OBAngleData;
angles->SetOrigin(perceived);
SetData(angles);
OBAngle angle;
OBAtom *b;
int unique_angle;
unique_angle = 0;
FOR_ATOMS_OF_MOL(atom, this) {
if(atom->GetAtomicNum() == OBElements::Hydrogen)
continue;
b = (OBAtom*) &*atom;
FOR_NBORS_OF_ATOM(a, b) {
FOR_NBORS_OF_ATOM(c, b) {
if(&*a == &*c) {
unique_angle = 1;
continue;
}
if (unique_angle) {
angle.SetAtoms((OBAtom*)b, (OBAtom*)&*a, (OBAtom*)&*c);
angles->SetData(angle);
angle.Clear();
}
}
unique_angle = 0;
}
}
return;
}
void OBMol::FindTorsions()
{
//if already has data return
if(HasData(OBGenericDataType::TorsionData))
return;
//get new data and attach it to molecule
OBTorsionData *torsions = new OBTorsionData;
torsions->SetOrigin(perceived);
SetData(torsions);
OBTorsion torsion;
vector<OBBond*>::iterator bi1,bi2,bi3;
OBBond* bond;
OBAtom *a,*b,*c,*d;
//loop through all bonds generating torsions
for(bond = BeginBond(bi1);bond;bond = NextBond(bi1))
{
b = bond->GetBeginAtom();
c = bond->GetEndAtom();
if(b->GetAtomicNum() == OBElements::Hydrogen || c->GetAtomicNum() == OBElements::Hydrogen)
continue;
for(a = b->BeginNbrAtom(bi2);a;a = b->NextNbrAtom(bi2))
{
if(a == c)
continue;
for(d = c->BeginNbrAtom(bi3);d;d = c->NextNbrAtom(bi3))
{
if ((d == b) || (d == a))
continue;
torsion.AddTorsion(a,b,c,d);
}
}
//add torsion to torsionData
if(torsion.GetSize())
torsions->SetData(torsion);
torsion.Clear();
}
return;
}
void OBMol::FindLargestFragment(OBBitVec &lf)
{
int j;
OBAtom *atom;
OBBond *bond;
vector<OBAtom*>::iterator i;
vector<OBBond*>::iterator k;
OBBitVec used,curr,next,frag;
lf.Clear();
while ((unsigned)used.CountBits() < NumAtoms())
{
curr.Clear();
frag.Clear();
for (atom = BeginAtom(i);atom;atom = NextAtom(i))
if (!used.BitIsSet(atom->GetIdx()))
{
curr.SetBitOn(atom->GetIdx());
break;
}
frag |= curr;
while (!curr.IsEmpty())
{
next.Clear();
for (j = curr.NextBit(-1);j != curr.EndBit();j = curr.NextBit(j))
{
atom = GetAtom(j);
for (bond = atom->BeginBond(k);bond;bond = atom->NextBond(k))
if (!used.BitIsSet(bond->GetNbrAtomIdx(atom)))
next.SetBitOn(bond->GetNbrAtomIdx(atom));
}
used |= curr;
used |= next;
frag |= next;
curr = next;
}
if (lf.IsEmpty() || lf.CountBits() < frag.CountBits())
lf = frag;
}
}
//! locates all atoms for which there exists a path to 'end'
//! without going through 'bgn'
//! children must not include 'end'
void OBMol::FindChildren(vector<OBAtom*> &children,OBAtom *bgn,OBAtom *end)
{
OBBitVec used,curr,next;
used |= bgn->GetIdx();
used |= end->GetIdx();
curr |= end->GetIdx();
children.clear();
int i;
OBAtom *atom,*nbr;
vector<OBBond*>::iterator j;
for (;;)
{
next.Clear();
for (i = curr.NextBit(-1);i != curr.EndBit();i = curr.NextBit(i))
{
atom = GetAtom(i);
for (nbr = atom->BeginNbrAtom(j);nbr;nbr = atom->NextNbrAtom(j))
if (!used[nbr->GetIdx()])
{
children.push_back(nbr);
next |= nbr->GetIdx();
used |= nbr->GetIdx();
}
}
if (next.IsEmpty())
break;
curr = next;
}
}
//! locates all atoms for which there exists a path to 'second'
//! without going through 'first'
//! children must not include 'second'
void OBMol::FindChildren(vector<int> &children,int first,int second)
{
int i;
OBBitVec used,curr,next;
used.SetBitOn(first);
used.SetBitOn(second);
curr.SetBitOn(second);
OBAtom *atom;
while (!curr.IsEmpty())
{
next.Clear();
for (i = curr.NextBit(-1);i != curr.EndBit();i = curr.NextBit(i))
{
atom = GetAtom(i);
FOR_BONDS_OF_ATOM (bond, atom)
if (!used.BitIsSet(bond->GetNbrAtomIdx(atom)))
next.SetBitOn(bond->GetNbrAtomIdx(atom));
}
used |= next;
curr = next;
}
used.SetBitOff(first);
used.SetBitOff(second);
used.ToVecInt(children);
}
/*! \brief Calculates the graph theoretical distance (GTD) of each atom.
*
* Creates a vector (indexed from zero) containing, for each atom
* in the molecule, the number of bonds plus one to the most
* distant non-H atom.
*
* For example, for the molecule H3CC(=O)Cl the GTD value for C1
* would be 3, as the most distant non-H atom (either Cl or O) is
* 2 bonds away.
*
* Since the GTD measures the distance to non-H atoms, the GTD values
* for terminal H atoms tend to be larger than for non-H terminal atoms.
* In the example above, the GTD values for the H atoms are all 4.
*/
bool OBMol::GetGTDVector(vector<int> >d)
//calculates the graph theoretical distance for every atom
//and puts it into gtd
{
gtd.clear();
gtd.resize(NumAtoms());
int gtdcount,natom;
OBBitVec used,curr,next;
OBAtom *atom,*atom1;
OBBond *bond;
vector<OBAtom*>::iterator i;
vector<OBBond*>::iterator j;
next.Clear();
for (atom = BeginAtom(i);atom;atom = NextAtom(i))
{
gtdcount = 0;
used.Clear();
curr.Clear();
used.SetBitOn(atom->GetIdx());
curr.SetBitOn(atom->GetIdx());
while (!curr.IsEmpty())
{
next.Clear();
for (natom = curr.NextBit(-1);natom != curr.EndBit();natom = curr.NextBit(natom))
{
atom1 = GetAtom(natom);
for (bond = atom1->BeginBond(j);bond;bond = atom1->NextBond(j))
if (!used.BitIsSet(bond->GetNbrAtomIdx(atom1)) && !curr.BitIsSet(bond->GetNbrAtomIdx(atom1)))
if (bond->GetNbrAtom(atom1)->GetAtomicNum() != OBElements::Hydrogen)
next.SetBitOn(bond->GetNbrAtomIdx(atom1));
}
used |= next;
curr = next;
gtdcount++;
}
gtd[atom->GetIdx()-1] = gtdcount;
}
return(true);
}
/*!
**\brief Calculates a set of graph invariant indexes using
** the graph theoretical distance, number of connected heavy atoms,
** aromatic boolean, ring boolean, atomic number, and
** summation of bond orders connected to the atom.
** Vector is indexed from zero
*/
void OBMol::GetGIVector(vector<unsigned int> &vid)
{
vid.clear();
vid.resize(NumAtoms()+1);
vector<int> v;
GetGTDVector(v);
int i;
OBAtom *atom;
vector<OBAtom*>::iterator j;
for (i=0,atom = BeginAtom(j);atom;atom = NextAtom(j),++i)
{
vid[i] = (unsigned int)v[i];
vid[i] += (unsigned int)(atom->GetHvyDegree()*100);
vid[i] += (unsigned int)(((atom->IsAromatic()) ? 1 : 0)*1000);
vid[i] += (unsigned int)(((atom->IsInRing()) ? 1 : 0)*10000);
vid[i] += (unsigned int)(atom->GetAtomicNum()*100000);
vid[i] += (unsigned int)(atom->GetImplicitHCount()*10000000);
}
}
static bool OBComparePairSecond(const pair<OBAtom*,unsigned int> &a,const pair<OBAtom*,unsigned int> &b)
{
return(a.second < b.second);
}
static bool OBComparePairFirst(const pair<OBAtom*,unsigned int> &a,const pair<OBAtom*,unsigned int> &b)
{
return(a.first->GetIdx() < b.first->GetIdx());
}
//! counts the number of unique symmetry classes in a list
static void ClassCount(vector<pair<OBAtom*,unsigned int> > &vp,unsigned int &count)
{
count = 0;
vector<pair<OBAtom*,unsigned int> >::iterator k;
sort(vp.begin(),vp.end(),OBComparePairSecond);
#if 0 // original version
unsigned int id=0; // [ejk] appease gcc's bogus "might be undef'd" warning
for (k = vp.begin();k != vp.end();++k)
{
if (k == vp.begin())
{
id = k->second;
k->second = count = 0;
}
else
if (k->second != id)
{
id = k->second;
k->second = ++count;
}
else
k->second = count;
}
count++;
#else // get rid of warning, moves test out of loop, returns 0 for empty input
k = vp.begin();
if (k != vp.end())
{
unsigned int id = k->second;
k->second = 0;
++k;
for (;k != vp.end(); ++k)
{
if (k->second != id)
{
id = k->second;
k->second = ++count;
}
else
k->second = count;
}
++count;
}
else
{
// [ejk] thinks count=0 might be OK for an empty list, but orig code did
//++count;
}
#endif
}
//! creates a new vector of symmetry classes base on an existing vector
//! helper routine to GetGIDVector
static void CreateNewClassVector(vector<pair<OBAtom*,unsigned int> > &vp1,vector<pair<OBAtom*,unsigned int> > &vp2)
{
int m,id;
OBAtom *nbr;
vector<OBBond*>::iterator j;
vector<unsigned int>::iterator k;
vector<pair<OBAtom*,unsigned int> >::iterator i;
sort(vp1.begin(),vp1.end(),OBComparePairFirst);
vp2.clear();
for (i = vp1.begin();i != vp1.end();++i)
{
vector<unsigned int> vtmp;
for (nbr = i->first->BeginNbrAtom(j);nbr;nbr = i->first->NextNbrAtom(j))
vtmp.push_back(vp1[nbr->GetIdx()-1].second);
sort(vtmp.begin(),vtmp.end(),OBCompareUnsigned);
for (id=i->second,m=100,k = vtmp.begin();k != vtmp.end();++k,m*=100)
id += *k * m;
vp2.push_back(pair<OBAtom*,unsigned int> (i->first,id));
}
}
/*!
**\brief Calculates a set of symmetry identifiers for a molecule.
** Atoms with the same symmetry ID are symmetrically equivalent.
** Vector is indexed from zero
*/
void OBMol::GetGIDVector(vector<unsigned int> &vgid)
{
vector<unsigned int> vgi;
GetGIVector(vgi); //get vector of graph invariants
int i;
OBAtom *atom;
vector<OBAtom*>::iterator j;
vector<pair<OBAtom*,unsigned int> > vp1,vp2;
for (i=0,atom = BeginAtom(j);atom;atom = NextAtom(j),++i)
vp1.push_back(pair<OBAtom*,unsigned int> (atom,vgi[i]));
unsigned int nclass1,nclass2; //number of classes
ClassCount(vp1,nclass1);
if (nclass1 < NumAtoms())
{
for (i = 0;i < 100;++i) //sanity check - shouldn't ever hit this number
{
CreateNewClassVector(vp1,vp2);
ClassCount(vp2,nclass2);
vp1 = vp2;
if (nclass1 == nclass2)
break;
nclass1 = nclass2;
}
}
vgid.clear();
sort(vp1.begin(),vp1.end(),OBComparePairFirst);
vector<pair<OBAtom*,unsigned int> >::iterator k;
for (k = vp1.begin();k != vp1.end();++k)
vgid.push_back(k->second);
}
unsigned int OBMol::NumHvyAtoms()
{
OBAtom *atom;
vector<OBAtom*>::iterator(i);
unsigned int count = 0;
for(atom = this->BeginAtom(i);atom;atom = this->NextAtom(i))
{
if (atom->GetAtomicNum() != OBElements::Hydrogen)
count++;
}
return(count);
}
unsigned int OBMol::NumRotors(bool sampleRingBonds)
{
OBRotorList rl;
rl.FindRotors(*this, sampleRingBonds);
return rl.Size();
}
//! Returns a pointer to the atom after a safety check
//! 0 < idx <= NumAtoms
OBAtom *OBMol::GetAtom(int idx) const
{
if ((unsigned)idx < 1 || (unsigned)idx > NumAtoms())
{
obErrorLog.ThrowError(__FUNCTION__, "Requested Atom Out of Range", obDebug);
return nullptr;
}
return((OBAtom*)_vatom[idx-1]);
}
OBAtom *OBMol::GetAtomById(unsigned long id) const
{
if (id >= _atomIds.size()) {
obErrorLog.ThrowError(__FUNCTION__, "Requested atom with invalid id.", obDebug);
return nullptr;
}
return((OBAtom*)_atomIds[id]);
}
OBAtom *OBMol::GetFirstAtom() const
{
return _vatom.empty() ? nullptr : (OBAtom*)_vatom[0];
}
//! Returns a pointer to the bond after a safety check
//! 0 <= idx < NumBonds
OBBond *OBMol::GetBond(int idx) const
{
if (idx < 0 || (unsigned)idx >= NumBonds())
{
obErrorLog.ThrowError(__FUNCTION__, "Requested Bond Out of Range", obDebug);
return nullptr;
}
return((OBBond*)_vbond[idx]);
}
OBBond *OBMol::GetBondById(unsigned long id) const
{
if (id >= _bondIds.size()) {
obErrorLog.ThrowError(__FUNCTION__, "Requested bond with invalid id.", obDebug);
return nullptr;
}
return((OBBond*)_bondIds[id]);
}
OBBond *OBMol::GetBond(int bgn, int end) const
{
return(GetBond(GetAtom(bgn),GetAtom(end)));
}
OBBond *OBMol::GetBond(OBAtom *bgn,OBAtom *end) const
{
OBAtom *nbr;
vector<OBBond*>::iterator i;
if (!bgn || !end) return nullptr;
for (nbr = bgn->BeginNbrAtom(i);nbr;nbr = bgn->NextNbrAtom(i))
if (nbr == end)
return((OBBond *)*i);
return nullptr; //just to keep the SGI compiler happy
}
OBResidue *OBMol::GetResidue(int idx) const
{
if (idx < 0 || (unsigned)idx >= NumResidues())
{
obErrorLog.ThrowError(__FUNCTION__, "Requested Residue Out of Range", obDebug);
return nullptr;
}
return (_residue[idx]);
}
std::vector<OBInternalCoord*> OBMol::GetInternalCoord()
{
if (_internals.empty())
{
_internals.push_back(nullptr);
for(unsigned int i = 1; i <= NumAtoms(); ++i)
{
_internals.push_back(new OBInternalCoord);
}
CartesianToInternal(_internals, *this);
}
return _internals;
}
//! Implements <a href="http://qsar.sourceforge.net/dicts/blue-obelisk/index.xhtml#findSmallestSetOfSmallestRings">blue-obelisk:findSmallestSetOfSmallestRings</a>.
vector<OBRing*> &OBMol::GetSSSR()
{
if (!HasSSSRPerceived())
FindSSSR();
OBRingData *rd = nullptr;
if (!HasData("SSSR")) {
rd = new OBRingData();
rd->SetAttribute("SSSR");
SetData(rd);
}
rd = (OBRingData *) GetData("SSSR");
rd->SetOrigin(perceived);
return(rd->GetData());
}
vector<OBRing*> &OBMol::GetLSSR()
{
if (!HasLSSRPerceived())
FindLSSR();
OBRingData *rd = nullptr;
if (!HasData("LSSR")) {
rd = new OBRingData();
rd->SetAttribute("LSSR");
SetData(rd);
}
rd = (OBRingData *) GetData("LSSR");
rd->SetOrigin(perceived);
return(rd->GetData());
}
double OBMol::GetMolWt(bool implicitH)
{
double molwt=0.0;
OBAtom *atom;
vector<OBAtom*>::iterator i;
double hmass = OBElements::GetMass(1);
for (atom = BeginAtom(i);atom;atom = NextAtom(i)) {
molwt += atom->GetAtomicMass();
if (implicitH)
molwt += atom->GetImplicitHCount() * hmass;
}
return(molwt);
}
double OBMol::GetExactMass(bool implicitH)
{
double mass=0.0;
OBAtom *atom;
vector<OBAtom*>::iterator i;
double hmass = OBElements::GetExactMass(1, 1);
for (atom = BeginAtom(i); atom; atom = NextAtom(i)) {
mass += atom->GetExactMass();
if (implicitH)
mass += atom->GetImplicitHCount() * hmass;
}
return(mass);
}
//! Stochoimetric formula in spaced format e.g. C 4 H 6 O 1
//! No pair data is stored. Normally use without parameters: GetSpacedFormula()
//! \since version 2.1
string OBMol::GetSpacedFormula(int ones, const char* sp, bool implicitH)
{
//Default ones=0, sp=" ".
//Using ones=1 and sp="" will give unspaced formula (and no pair data entry)
// These are the atomic numbers of the elements in alphabetical order, plus
// pseudo atomic numbers for D, T isotopes.
const int NumElements = 118 + 2;
const int alphabetical[NumElements] = {
89, 47, 13, 95, 18, 33, 85, 79, 5, 56, 4, 107, 83, 97, 35, 6, 20, 48,