This repository has been archived by the owner on Sep 1, 2021. It is now read-only.
/
molecule.cpp
1988 lines (1741 loc) · 58.9 KB
/
molecule.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
/**********************************************************************
Molecule - Molecule class derived from the base Primitive class
Copyright (C) 2007 Donald Ephraim Curtis
Copyright (C) 2008-2009 Marcus D. Hanwell
This file is part of the Avogadro molecular editor project.
For more information, see <http://avogadro.cc/>
Avogadro is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Avogadro is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
**********************************************************************/
#include "config.h"
#include "molecule.h"
#include "atom.h"
#include "bond.h"
#include "cube.h"
#include "fragment.h"
#include "mesh.h"
#include "obeigenconv.h"
#include "primitivelist.h"
#include "residue.h"
#include "zmatrix.h"
#include <Eigen/Geometry>
#include <Eigen/LeastSquares>
#include <vector>
#include <openbabel/mol.h>
#include <openbabel/math/vector3.h>
#include <openbabel/griddata.h>
#include <openbabel/grid.h>
#include <openbabel/generic.h>
#include <openbabel/forcefield.h>
#include <openbabel/obiter.h>
#include <QtCore/QDir>
#include <QtCore/QDebug>
#include <QtCore/QVariant>
#include <QtCore/QVector>
namespace Avogadro{
using std::vector;
using Eigen::Vector3d;
class MoleculePrivate {
public:
MoleculePrivate() : farthestAtom(0), invalidGeomInfo(true),
invalidRings(true), invalidGroupIndices(true),
obmol(0), obunitcell(0),
obvibdata(0), obdosdata(0),
obelectronictransitiondata(0)
{}
// These are logically cached variables and thus are marked as mutable.
// Const objects should be logically constant (and not mutable)
// http://www.highprogrammer.com/alan/rants/mutable.html
mutable Eigen::Vector3d center;
mutable Eigen::Vector3d normalVector;
mutable double radius;
mutable Atom * farthestAtom;
mutable bool invalidGeomInfo;
mutable bool invalidRings;
mutable bool invalidGroupIndices;
mutable std::vector<double> energies;
// std::vector used over QVector due to index issues, QVector uses ints
std::vector<Cube *> cubes;
std::vector<Mesh *> meshes;
std::vector<Residue *> residues;
std::vector<Fragment *> rings;
std::vector<ZMatrix *> zMatrix;
// Used to store the index based list (not unique ids)
QList<Cube *> cubeList;
QList<Mesh *> meshList;
QList<Residue *> residueList;
QList<Fragment *> ringList;
QList<ZMatrix *> zMatrixList;
// Our OpenBabel OBMol object
OpenBabel::OBMol * obmol;
// Our OpenBabel OBUnitCell object (if any)
OpenBabel::OBUnitCell * obunitcell;
// Our OpenBabel OBVibrationData object (if any)
// TODO: Cache an OBMol, in which case the vib. data (and others)
// won't be necessary
OpenBabel::OBVibrationData * obvibdata;
OpenBabel::OBDOSData * obdosdata;
OpenBabel::OBElectronicTransitionData *
obelectronictransitiondata;
};
Molecule::Molecule(QObject *parent) : Primitive(MoleculeType, parent),
d_ptr(new MoleculePrivate),
m_atomPos(0),
m_currentConformer(0),
m_estimatedDipoleMoment(true),
m_dipoleMoment(0),
m_invalidPartialCharges(true),
m_invalidAromaticity(true),
m_lock(new QReadWriteLock)
{
connect(this, SIGNAL(updated()), this, SLOT(updatePrimitive()));
// Assign a default path and file name to new molecules.
m_fileName = QDir::homePath() + '/' +
tr("untitled", "Name of a new, untitled molecule file") +
".cml";
}
Molecule::Molecule(const Molecule &other) :
Primitive(MoleculeType, other.parent()), d_ptr(new MoleculePrivate),
m_atomPos(0), m_dipoleMoment(0), m_invalidPartialCharges(true),
m_invalidAromaticity(true), m_lock(new QReadWriteLock)
{
*this = other;
connect(this, SIGNAL(updated()), this, SLOT(updatePrimitive()));
}
Molecule::~Molecule()
{
// Need to iterate through all atoms/bonds and destroy them
disconnect(this, 0);
blockSignals(true);
clear();
delete m_lock;
delete d_ptr;
}
void Molecule::setFileName(const QString& name)
{
m_fileName = name;
}
QString Molecule::fileName() const
{
return m_fileName;
}
Atom *Molecule::addAtom()
{
// Add an atom with the next unique id
return addAtom(m_atoms.size());
}
// do some fancy footwork when we add an atom previously created
Atom *Molecule::addAtom(unsigned long id)
{
Q_D(const Molecule);
d->invalidGeomInfo = true;
Atom *atom = new Atom(this);
if (!m_atomPos) {
m_atomConformers.resize(1);
m_atomConformers[0] = new vector<Vector3d>;
m_atomPos = m_atomConformers[0];
m_atomPos->reserve(100);
}
if(id >= m_atoms.size()) {
m_atoms.resize(id+1,0);
m_atomPos->resize(id+1, Vector3d::Zero());
}
m_atoms[id] = atom;
// Does this still want to have the same index as before somehow?
m_atomList.push_back(atom);
atom->setId(id);
atom->setIndex(m_atomList.size()-1);
// now that the id is correct, emit the signal
connect(atom, SIGNAL(updated()), this, SLOT(updateAtom()));
d->invalidGroupIndices = true;
emit atomAdded(atom);
return atom;
}
Atom *Molecule::addAtom(int atomicNum, const Eigen::Vector3d &pos)
{
// Add a new atom with the next unique id
const unsigned long newId = m_atoms.size();
Atom *newAtom = this->addAtom(newId);
newAtom->m_atomicNumber = atomicNum;
(*m_atomPos)[newId] = pos;
return newAtom;
}
Atom *Molecule::addAtom(const Atom &other)
{
// Add a new atom with the next unique id
Atom *newAtom = this->addAtom(m_atoms.size());
*newAtom = other;
return newAtom;
}
void Molecule::setAtomPos(unsigned long id, const Eigen::Vector3d& vec)
{
Q_D(const Molecule);
if (id < m_atomPos->size()) {
(*m_atomPos)[id] = vec;
d->invalidGeomInfo = true;
}
}
void Molecule::setAtomPos(unsigned long id, const Eigen::Vector3d *vec)
{
if (vec)
setAtomPos(id, *vec);
}
void Molecule::removeAtom(Atom *atom)
{
Q_D(const Molecule);
if(atom && atom->parent() == this) {
// When deleting an atom this also implicitly deletes any bonds to the atom
foreach (unsigned long bond, atom->bonds()) {
removeBond(bond);
}
m_atoms[atom->id()] = 0;
// 1 based arrays stored/shown to user
int index = atom->index();
m_atomList.removeAt(index);
for (int i = index; i < m_atomList.size(); ++i)
m_atomList[i]->setIndex(i);
atom->deleteLater();
disconnect(atom, SIGNAL(updated()), this, SLOT(updateAtom()));
d->invalidGroupIndices = true;
emit atomRemoved(atom);
}
}
void Molecule::removeAtom(unsigned long id)
{
removeAtom(atomById(id));
}
Bond *Molecule::addBond()
{
return addBond(m_bonds.size());
}
Bond *Molecule::addBond(unsigned long id)
{
Q_D(Molecule);
Bond *bond = new Bond(this);
d->invalidRings = true;
m_invalidPartialCharges = true;
m_invalidAromaticity = true;
if(id >= m_bonds.size())
m_bonds.resize(id+1,0);
m_bonds[id] = bond;
m_bondList.push_back(bond);
bond->setId(id);
bond->setIndex(m_bondList.size()-1);
// now that the id is correct, emit the signal
connect(bond, SIGNAL(updated()), this, SLOT(updateBond()));
emit bondAdded(bond);
return(bond);
}
Bond *Molecule::addBond(unsigned long beginAtomId, unsigned long endAtomId,
short order)
{
// Add a new bond with the next unique id
Bond *newBond = this->addBond(m_bonds.size());
newBond->setAtoms(beginAtomId, endAtomId, order);
return newBond;
}
Bond *Molecule::addBond(Atom *beginAtom, Atom *endAtom, short order)
{
// Add a new bond with the next unique id
Bond *newBond = this->addBond(m_bonds.size());
newBond->setAtoms(beginAtom->m_id, endAtom->m_id, order);
return newBond;
}
void Molecule::removeBond(Bond *bond)
{
if(bond && bond->parent() == this) {
removeBond(bond->id());
}
}
void Molecule::removeBond(unsigned long id)
{
if (id < m_bonds.size()) {
Q_D(Molecule);
if (m_bonds[id] == 0)
return;
d->invalidRings = true;
m_invalidPartialCharges = true;
m_invalidAromaticity = true;
Bond *bond = m_bonds[id];
m_bonds[id] = 0;
// Delete the bond from the list and reorder the remaining bonds
int index = bond->index();
m_bondList.removeAt(index);
for (int i = index; i < m_bondList.size(); ++i) {
m_bondList[i]->setIndex(i);
}
// Also delete the bond from the attached atoms
if (m_atoms.size() > bond->beginAtomId()) {
if (m_atoms[bond->beginAtomId()])
m_atoms[bond->beginAtomId()]->removeBond(id);
}
if (m_atoms.size() > bond->endAtomId()) {
if (m_atoms[bond->endAtomId()])
m_atoms[bond->endAtomId()]->removeBond(id);
}
disconnect(bond, SIGNAL(updated()), this, SLOT(updateBond()));
emit bondRemoved(bond);
bond->deleteLater();
}
}
Residue *Molecule::residue(int index)
{
Q_D(Molecule);
if (index >= 0 && index < d->residueList.size())
return d->residueList[index];
else
return 0;
}
const Residue *Molecule::residue(int index) const
{
Q_D(const Molecule);
if (index >= 0 && index < d->residueList.size())
return d->residueList[index];
else
return 0;
}
Residue *Molecule::residueById(unsigned long id) const
{
Q_D(const Molecule);
if(id < d->residues.size())
return d->residues[id];
else
return 0;
}
Cube *Molecule::cube(int index) const
{
Q_D(const Molecule);
if (index >= 0 && index < d->cubeList.size())
return d->cubeList[index];
else
return 0;
}
Cube *Molecule::cubeById(unsigned long id) const
{
Q_D(const Molecule);
if(id < d->cubes.size())
return d->cubes[id];
else
return 0;
}
Cube *Molecule::addCube()
{
Q_D(const Molecule);
return addCube(d->cubes.size());
}
Cube *Molecule::addCube(unsigned long id)
{
Q_D(Molecule);
Cube *cube = new Cube(this);
if(id >= d->cubes.size())
d->cubes.resize(id+1,0);
d->cubes[id] = cube;
// Does this still want to have the same index as before somehow?
d->cubeList.push_back(cube);
cube->setId(id);
cube->setIndex(d->cubeList.size()-1);
// now that the id is correct, emit the signal
connect(cube, SIGNAL(updated()), this, SLOT(updatePrimitive()));
emit primitiveAdded(cube);
return(cube);
}
void Molecule::removeCube(Cube *cube)
{
Q_D(Molecule);
if(cube && cube->parent() == this) {
d->cubes[cube->id()] = 0;
// 0 based arrays stored/shown to user
int index = cube->index();
d->cubeList.removeAt(index);
for (int i = index; i < d->cubeList.size(); ++i)
d->cubeList[i]->setIndex(i);
cube->deleteLater();
disconnect(cube, SIGNAL(updated()), this, SLOT(updatePrimitive()));
emit primitiveRemoved(cube);
}
}
void Molecule::removeCube(unsigned long id)
{
Q_D(Molecule);
if (id < d->cubes.size())
removeCube(d->cubes[id]);
}
Mesh * Molecule::addMesh()
{
Q_D(const Molecule);
return addMesh(d->meshes.size());
}
Mesh * Molecule::addMesh(unsigned long id)
{
Q_D(Molecule);
Mesh *mesh = new Mesh(this);
if (id >= d->meshes.size())
d->meshes.resize(id+1,0);
d->meshes[id] = mesh;
d->meshList.push_back(mesh);
mesh->setId(id);
mesh->setIndex(d->meshList.size()-1);
// now that the id is correct, emit the signal
connect(mesh, SIGNAL(updated()), this, SLOT(updatePrimitive()));
emit primitiveAdded(mesh);
return(mesh);
}
void Molecule::removeMesh(Mesh *mesh)
{
Q_D(Molecule);
if(mesh && mesh->parent() == this) {
d->meshes[mesh->id()] = 0;
// 0 based arrays stored/shown to user
int index = mesh->index();
d->meshList.removeAt(index);
for (int i = index; i < d->meshList.size(); ++i)
d->meshList[i]->setIndex(i);
mesh->deleteLater();
disconnect(mesh, SIGNAL(updated()), this, SLOT(updatePrimitive()));
emit primitiveRemoved(mesh);
}
}
void Molecule::removeMesh(unsigned long id)
{
Q_D(Molecule);
if (id < d->meshes.size())
removeMesh(d->meshes[id]);
}
Mesh * Molecule::mesh(int index) const
{
Q_D(const Molecule);
if (index >= 0 && index < d->meshList.size())
return d->meshList[index];
else
return 0;
}
Mesh * Molecule::meshById(unsigned long id) const
{
Q_D(const Molecule);
if(id < d->meshes.size())
return d->meshes[id];
else
return 0;
}
Residue * Molecule::addResidue()
{
Q_D(const Molecule);
return addResidue(d->residues.size());
}
Residue * Molecule::addResidue(unsigned long id)
{
Q_D(Molecule);
Residue *residue = new Residue(this);
if (id >= d->residues.size())
d->residues.resize(id+1,0);
d->residues[id] = residue;
d->residueList.push_back(residue);
residue->setId(id);
residue->setIndex(d->residueList.size()-1);
// now that the id is correct, emit the signal
connect(residue, SIGNAL(updated()), this, SLOT(updatePrimitive()));
emit primitiveAdded(residue);
return(residue);
}
void Molecule::removeResidue(Residue *residue)
{
Q_D(Molecule);
if(residue && residue->parent() == this) {
d->residues[residue->id()] = 0;
// 0 based arrays stored/shown to user
int index = residue->index();
d->residueList.removeAt(index);
for (int i = index; i < d->residueList.size(); ++i) {
d->residueList[i]->setIndex(i);
}
residue->deleteLater();
disconnect(residue, SIGNAL(updated()), this, SLOT(updatePrimitive()));
emit primitiveRemoved(residue);
}
}
void Molecule::removeResidue(unsigned long id)
{
Q_D(Molecule);
if (id < d->residues.size())
removeResidue(d->residues[id]);
}
Fragment * Molecule::addRing()
{
Q_D(const Molecule);
return addRing(d->rings.size());
}
Fragment * Molecule::addRing(unsigned long id)
{
Q_D(Molecule);
Fragment *ring = new Fragment(this);
if (id >= d->rings.size())
d->rings.resize(id+1,0);
d->rings[id] = ring;
d->ringList.push_back(ring);
ring->setId(id);
ring->setIndex(d->ringList.size()-1);
// now that the id is correct, emit the signal
connect(ring, SIGNAL(updated()), this, SLOT(updatePrimitive()));
// emit primitiveAdded(ring);
return(ring);
}
void Molecule::removeRing(Fragment *ring)
{
Q_D(Molecule);
if(ring && ring->parent() == this) {
d->rings[ring->id()] = 0;
// 0 based arrays stored/shown to user
int index = ring->index();
d->ringList.removeAt(index);
for (int i = index; i < d->ringList.size(); ++i) {
d->ringList[i]->setIndex(i);
}
ring->deleteLater();
disconnect(ring, SIGNAL(updated()), this, SLOT(updatePrimitive()));
// emit primitiveRemoved(ring);
}
}
void Molecule::removeRing(unsigned long id)
{
Q_D(Molecule);
if (id < d->rings.size())
removeRing(d->rings[id]);
}
ZMatrix * Molecule::addZMatrix()
{
Q_D(Molecule);
ZMatrix *zmatrix = new ZMatrix(this);
d->zMatrixList.push_back(zmatrix);
return zmatrix;
}
void Molecule::removeZMatrix(ZMatrix *zmatrix)
{
Q_D(Molecule);
if (zmatrix) {
d->zMatrixList.removeAll(zmatrix);
delete zmatrix;
}
}
ZMatrix * Molecule::zMatrix(int index) const
{
Q_D(const Molecule);
if (index < d->zMatrixList.size())
return d->zMatrixList.at(index);
else
return 0;
}
QList<ZMatrix *> Molecule::zMatrices() const
{
Q_D(const Molecule);
return d->zMatrixList;
}
unsigned int Molecule::numZMatrices() const
{
Q_D(const Molecule);
return d->zMatrixList.size();
}
void Molecule::addHydrogens(Atom *a,
const QList<unsigned long> &atomIds,
const QList<unsigned long> &bondIds)
{
if (atomIds.size() != bondIds.size()) {
qDebug() << "Error, addHydrogens called with atom & bond id lists of different size!";
}
// Construct an OBMol, call AddHydrogens and translate the changes
OpenBabel::OBMol obmol = OBMol();
if (a) {
OpenBabel::OBAtom *obatom = obmol.GetAtom(a->index()+1);
// Set implicit valence for unusual elements not handled by OpenBabel
// PR#2803076
switch (obatom->GetAtomicNum()) {
case 3:
case 11:
case 19:
case 37:
case 55:
case 85:
case 87:
obatom->SetImplicitValence(1);
obatom->SetHyb(1);
obmol.SetImplicitValencePerceived();
break;
case 4:
case 12:
case 20:
case 38:
case 56:
case 88:
obatom->SetImplicitValence(2);
obatom->SetHyb(2);
obmol.SetImplicitValencePerceived();
break;
case 84: // Po
obatom->SetImplicitValence(2);
obatom->SetHyb(3);
obmol.SetImplicitValencePerceived();
break;
default: // do nothing
break;
}
obmol.AddHydrogens(obatom);
}
else
obmol.AddHydrogens();
// All new atoms in the OBMol must be the additional hydrogens
unsigned int numberAtoms = numAtoms();
int j = 0;
for (unsigned int i = numberAtoms+1; i <= obmol.NumAtoms(); ++i, ++j) {
if (obmol.GetAtom(i)->IsHydrogen()) {
OpenBabel::OBAtom *obatom = obmol.GetAtom(i);
Atom *atom;
if (atomIds.isEmpty())
atom = addAtom();
else if (j < atomIds.size())
atom = addAtom(atomIds.at(j));
else {
qDebug() << "Error - not enough unique ids in addHydrogens.";
break;
}
atom->setOBAtom(obatom);
// Get the neighbor atom
OpenBabel::OBBondIterator iter;
OpenBabel::OBAtom *next = obatom->BeginNbrAtom(iter);
Bond *bond;
if (bondIds.isEmpty())
bond = addBond();
else // Already confirmed by atom ids
bond = addBond(bondIds.at(j));
bond->setEnd(Molecule::atom(atom->index()));
bond->setBegin(Molecule::atom(next->GetIdx()-1));
}
}
for (unsigned int i = 1; i <= numberAtoms; ++i) {
// Warning -- OB atom index off-by-one here
atom(i-1)->setPartialCharge(obmol.GetAtom(i)->GetPartialCharge());
}
}
void Molecule::removeHydrogens(Atom *atom)
{
if (atom) {
// Delete any connected hydrogen atoms
QList<unsigned long> neighbors = atom->neighbors();
foreach (unsigned long a, neighbors) {
Atom *nbrAtom = atomById(a);
// we need to check if the atom still exists
if (nbrAtom) {
if (nbrAtom->isHydrogen()) {
removeAtom(a);
}
}
else {
qDebug() << "Error, atom advertising deleted neighbor" << atom->id()
<< a;
}
}
}
// Delete all of the hydrogens
else {
foreach (Atom *atom, m_atomList) {
if (atom->isHydrogen()) {
removeAtom(atom);
}
}
}
}
void Molecule::setDipoleMoment(const Eigen::Vector3d &moment)
{
if(!m_dipoleMoment)
m_dipoleMoment = new Vector3d(moment);
else
*m_dipoleMoment = moment;
m_estimatedDipoleMoment = false;
}
Eigen::Vector3d Molecule::dipoleMoment(bool *estimate) const
{
if (m_dipoleMoment && !m_estimatedDipoleMoment) {
if (estimate != NULL) // passed this as an argument
*estimate = false; // genuine calculated dipole moment
return *m_dipoleMoment;
}
else {
// Calculate a new estimate (e.g., the geometry changed
Vector3d dipoleMoment(0.0, 0.0, 0.0);
foreach (Atom *a, atoms())
dipoleMoment += *a->pos() * a->partialCharge();
// convert from electrons * Angstrom to Debye
// (1.602176487×10−19 C / electron) * (1.0e-10 m/Ang / 3.33564e-30 C/m)
// use the negative to go from positive to negative charge (Chemistry)
dipoleMoment *= -4.8032046729977;
if (estimate)
*estimate = true;
m_estimatedDipoleMoment = true;
return dipoleMoment;
}
}
void Molecule::calculatePartialCharges() const
{
if (numAtoms() < 1 || !m_invalidPartialCharges) {
return;
}
OpenBabel::OBMol obmol = OBMol();
for (unsigned int i = 0; i < numAtoms(); ++i) {
// Warning: OB off-by-one index
atom(i)->setPartialCharge(obmol.GetAtom(i+1)->GetPartialCharge());
}
m_invalidPartialCharges = false;
}
void Molecule::calculateAromaticity() const
{
if (numBonds() < 1 || !m_invalidAromaticity)
return;
OpenBabel::OBMol obmol = OBMol();
for (unsigned int i = 0; i < obmol.NumBonds(); ++i) {
bond(i)->setAromaticity(obmol.GetBond(i)->IsAromatic());
}
m_invalidAromaticity = false;
}
void Molecule::calculateGroupIndices() const
{
Q_D(const Molecule);
if(d->invalidGroupIndices) {
QVector<unsigned int> group_number; // numbers of atoms in each group
QVector<int> group_ele; // elements of each group
QVector<unsigned int> atomGroupNumber;
atomGroupNumber.resize(numAtoms());
for (unsigned int i = 0;
i < numAtoms() && static_cast<int>(i) < atomGroupNumber.size();
++i) {
bool match = false;
for (int j=0; j < group_number.size(); ++j) {
if ((atom(i)->atomicNumber()) == group_ele.at(j)) {
group_number[j] += 1;
atomGroupNumber[i] = group_number[j];
match = true;
}
}
if (!match) {
group_ele.push_back(atom(i)->atomicNumber());
group_number.push_back(1);
atomGroupNumber[i] = 1;
}
}
for (unsigned int i = 0;
i < numAtoms() && static_cast<int>(i) < atomGroupNumber.size();
++i) {
bool match = false;
for (int j=0; j<group_number.size(); ++j) {
if ((atom(i)->atomicNumber()) == group_ele.at(j) &&
(group_number.at(j) == 1)) {
match = true;
}
}
if (match) {
atom(i)->setGroupIndex(0);
} else {
atom(i)->setGroupIndex(atomGroupNumber.at(i));
}
}
d->invalidGroupIndices = false;
}
}
unsigned int Molecule::numAtoms() const
{
return m_atomList.size();
}
unsigned int Molecule::numBonds() const
{
return m_bondList.size();
}
unsigned int Molecule::numCubes() const
{
Q_D(const Molecule);
return d->cubeList.size();
}
unsigned int Molecule::numMeshes() const
{
Q_D(const Molecule);
return d->meshList.size();
}
unsigned int Molecule::numResidues() const
{
Q_D(const Molecule);
return d->residueList.size();
}
unsigned int Molecule::numRings() const
{
Q_D(const Molecule);
return d->ringList.size();
}
void Molecule::updateMolecule()
{
Q_D(Molecule);
d->invalidGeomInfo = true;
emit moleculeChanged();
emit updated();
}
void Molecule::updatePrimitive()
{
Q_D(Molecule);
Primitive *primitive = qobject_cast<Primitive *>(sender());
d->invalidGeomInfo = true;
emit primitiveUpdated(primitive);
}
void Molecule::updateAtom()
{
Q_D(Molecule);
Atom *atom = qobject_cast<Atom *>(sender());
d->invalidGeomInfo = true;
d->invalidGroupIndices = true;
emit atomUpdated(atom);
}
void Molecule::updateBond()
{
Bond *bond = qobject_cast<Bond *>(sender());
emit bondUpdated(bond);
}
void Molecule::update()
{
emit updated();
}
Bond* Molecule::bond(unsigned long id1, unsigned long id2)
{
// Take two atom IDs and see if we have a bond between the two
if (atomById(id1)) {
QList<unsigned long> bonds = atomById(id1)->bonds();
foreach (unsigned long id, bonds) {
Bond *bond = bondById(id);
if (bond) {
if (bond->otherAtom(id1) == id2) {
return bond;
}
}
}
}
return 0;
}
Bond* Molecule::bond(const Atom *a1, const Atom *a2)
{
if (a1 && a2) {
return bond(a1->id(), a2->id());
}
else {
return 0;
}
}
bool Molecule::addConformer(const std::vector<Eigen::Vector3d> &conformer,
unsigned int index)
{
if (conformer.size() != m_atomPos->size())
return false;
if (m_atomConformers.size() < index+1) {
unsigned int size = m_atomConformers.size();
// If there is a gap between the current last conformer and the new index, pad it
for (unsigned int i = size; i <= index; ++i)
m_atomConformers.push_back( new vector<Vector3d>(m_atomPos->size()) );
}
*m_atomConformers[index] = conformer;
return true;
}
vector<Vector3d> * Molecule::addConformer(unsigned int index)
{
if (index < m_atomConformers.size())
return m_atomConformers[index];
else {
unsigned int size = m_atomConformers.size();
m_atomConformers.resize(index+1);
for (unsigned int i = size; i <= index; ++i)
m_atomConformers[i] = new vector<Vector3d>(m_atomPos->size());
return m_atomConformers[index];
}
}
vector<Vector3d> * Molecule::conformer(unsigned int index)
{
if (index && index < m_atomConformers.size())
return m_atomConformers[index];