Debian Codegraph @

avogadro/qtplugins/closecontacts/closecontacts.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "closecontacts.h"
7
8 #include <avogadro/core/array.h>
9 #include <avogadro/core/atom.h>
10 #include <avogadro/core/bond.h>
11 #include <avogadro/core/elements.h>
12 #include <avogadro/core/neighborperceiver.h>
13 #include <avogadro/core/residue.h>
14 #include <avogadro/qtgui/molecule.h>
15 #include <avogadro/rendering/widelinegeometry.h>
16 #include <avogadro/rendering/geometrynode.h>
17 #include <avogadro/rendering/groupnode.h>
18
19 #include <QtCore/QSettings>
20 #include <QtWidgets/QDoubleSpinBox>
21 #include <QtWidgets/QFormLayout>
22 #include <QtWidgets/QTabWidget>
23 #include <QtWidgets/QVBoxLayout>
24 #include <QtWidgets/QWidget>
25
26 namespace Avogadro::QtPlugins {
27
28 using Core::Array;
29 using Core::Atom;
30 using Core::Bond;
31 using Core::NeighborPerceiver;
32 using QtGui::Molecule;
33 using QtGui::PluginLayerManager;
34 using Rendering::GeometryNode;
35 using Rendering::WideLineGeometry;
36
37 CloseContacts::CloseContacts(QObject* p) : ScenePlugin(p)
38 {
39 m_layerManager = PluginLayerManager(m_name);
40
41 QSettings settings;
42 m_maximumDistances = {
43 settings.value("closeContacts/maximumDistance0", 2.0).toDouble(),
44 settings.value("closeContacts/maximumDistance1", 4.0).toDouble(),
45 settings.value("closeContacts/maximumDistance2", 4.0).toDouble()
46 };
47 auto contactColor =
48 settings.value("closeContacts/lineColor0", QColor(128, 128, 128))
49 .value<QColor>();
50 auto saltBColor =
51 settings.value("closeContacts/lineColor1", QColor(192, 0, 255))
52 .value<QColor>();
53 auto repulsiveColor =
54 settings.value("closeContacts/lineColor2", QColor(255, 64, 64))
55 .value<QColor>();
56 m_lineColors = {
57 Vector3ub(contactColor.red(), contactColor.green(), contactColor.blue()),
58 Vector3ub(saltBColor.red(), saltBColor.green(), saltBColor.blue()),
59 Vector3ub(repulsiveColor.red(), repulsiveColor.green(),
60 repulsiveColor.blue())
61 };
62 m_lineWidths = { settings.value("closeContacts/lineWidth0", 2.0).toFloat(),
63 settings.value("closeContacts/lineWidth1", 5.0).toFloat(),
64 settings.value("closeContacts/lineWidth2", 5.0).toFloat() };
65 }
66
67 static bool checkPairNot1213(const Molecule& molecule, Index i, Index n)
68 {
69 static Array<Index> bondedCache;
70 static Index lastIndex;
71 static bool lastIndexValid = false;
72
73 if (!lastIndexValid || lastIndex != i) {
74 bondedCache.clear();
75 for (const Bond* b : molecule.bonds(i))
76 bondedCache.push_back(b->atom1().index() == i ? b->atom2().index()
77 : b->atom1().index());
78 lastIndex = i;
79 lastIndexValid = true;
80 }
81
82 for (const Bond* b : molecule.bonds(n)) {
83 Index m = (b->atom1().index() == n ? b->atom2() : b->atom1()).index();
84 if (m == i) // exclude 1-2 pairs
85 return false;
86 for (Index bn : bondedCache)
87 if (bn == m) // exclude 1-3 pairs
88 return false;
89 }
90 return true;
91 }
92
93 void addChargedAtom(Array<Vector3>& positions, Array<signed char>& charges,
94 Array<Index>& residues, const Molecule& molecule,
95 Index residueId, Atom atom, double charge)
96 {
97 auto pos = molecule.atomPosition3d(atom.index());
98 if (molecule.formalCharge(atom.index()) != 0) {
99 for (Index i = 0; i < positions.size(); i++) {
100 if ((positions[i] - pos).norm() < 0.00001) {
101 residues[i] = residueId;
102 return;
103 }
104 }
105 }
106 positions.push_back(pos);
107 charges.push_back(charge);
108 residues.push_back(residueId);
109 }
110
111 void CloseContacts::process(const Molecule& molecule,
112 Rendering::GroupNode& node)
113 {
114 // Add general contacts
115 NeighborPerceiver perceiver(molecule.atomPositions3d(),
116 m_maximumDistances[0]);
117 std::vector<bool> isAtomEnabled(molecule.atomCount());
118 for (Index i = 0; i < molecule.atomCount(); ++i)
119 isAtomEnabled[i] = m_layerManager.atomEnabled(i);
120
121 auto* geometry = new GeometryNode;
122 node.addChild(geometry);
123 std::array<WideLineGeometry*, 3> lineGroups;
124 for (Index type = 0; type < 3; type++) {
125 lineGroups[type] = new WideLineGeometry;
126 lineGroups[type]->identifier().molecule = &molecule;
127 lineGroups[type]->identifier().type = Rendering::BondType;
128 geometry->addDrawable(lineGroups[type]);
129 }
130
131 Array<Index> neighbors;
132 for (Index i = 0; i < molecule.atomCount(); ++i) {
133 if (!isAtomEnabled[i])
134 continue;
135 Vector3 pos = molecule.atomPosition3d(i);
136 perceiver.getNeighborsInclusiveInPlace(neighbors, pos);
137 for (Index n : neighbors) {
138 if (n <= i) // check each pair only once
139 continue;
140 if (!isAtomEnabled[n])
141 continue;
142 if (!checkPairNot1213(molecule, i, n))
143 continue;
144
145 Vector3 npos = molecule.atomPosition3d(n);
146 double distance = (npos - pos).norm();
147 if (distance < m_maximumDistances[0])
148 lineGroups[0]->addDashedLine(
149 pos.cast<float>(), npos.cast<float>(), m_lineColors[0],
150 m_lineWidths[0] * WideLineGeometry::lineWidthScale, 8);
151 }
152 }
153
154 // Add charged atoms
155 Array<Vector3> positions;
156 Array<signed char> charges;
157 Array<Index> residues;
158 for (Index i = 0; i < molecule.atomCount(); ++i) {
159 if (molecule.formalCharge(i) != 0) {
160 if (!isAtomEnabled[i])
161 continue;
162 positions.push_back(molecule.atomPosition3d(i));
163 charges.push_back(molecule.formalCharge(i));
164 residues.push_back(Index(0) - 1);
165 }
166 }
167
168 // Add predicted charged atoms from residues
169 for (const auto& r : molecule.residues()) {
170 if (!r.residueName().compare("LYS")) {
171 addChargedAtom(positions, charges, residues, molecule, r.residueId(),
172 r.atomByName("NZ"), 1.0);
173 } else if (!r.residueName().compare("ARG")) {
174 addChargedAtom(positions, charges, residues, molecule, r.residueId(),
175 r.atomByName("NE"), 1.0);
176 addChargedAtom(positions, charges, residues, molecule, r.residueId(),
177 r.atomByName("NH1"), 1.0);
178 addChargedAtom(positions, charges, residues, molecule, r.residueId(),
179 r.atomByName("NH2"), 1.0);
180 } else if (!r.residueName().compare("HIS")) {
181 addChargedAtom(positions, charges, residues, molecule, r.residueId(),
182 r.atomByName("ND1"), 1.0);
183 } else if (!r.residueName().compare("ASP")) {
184 addChargedAtom(positions, charges, residues, molecule, r.residueId(),
185 r.atomByName("OD1"), -1.0);
186 addChargedAtom(positions, charges, residues, molecule, r.residueId(),
187 r.atomByName("OD2"), -1.0);
188 } else if (!r.residueName().compare("GLU")) {
189 addChargedAtom(positions, charges, residues, molecule, r.residueId(),
190 r.atomByName("OE1"), -1.0);
191 addChargedAtom(positions, charges, residues, molecule, r.residueId(),
192 r.atomByName("OE2"), -1.0);
193 }
194 }
195
196 // detect contacts among them
197 NeighborPerceiver ionPerceiver(
198 positions, std::max(m_maximumDistances[1], m_maximumDistances[2]));
199 for (Index i = 0; i < positions.size(); ++i) {
200 const Vector3& pos = positions[i];
201 ionPerceiver.getNeighborsInclusiveInPlace(neighbors, pos);
202 for (Index n : neighbors) {
203 if (n <= i) // check each pair only once
204 continue;
205 if (residues[n] == residues[i] && residues[i] != Index(0) - 1)
206 continue; // ignore intra-residue interactions
207
208 Vector3 npos = positions[n];
209 double distance = (npos - pos).norm();
210
211 if (charges[i] * charges[n] > 0.0 && distance < m_maximumDistances[2])
212 lineGroups[2]->addDashedLine(
213 pos.cast<float>(), npos.cast<float>(), m_lineColors[2],
214 m_lineWidths[2] * WideLineGeometry::lineWidthScale, 8);
215 else if (distance < m_maximumDistances[1])
216 lineGroups[1]->addDashedLine(
217 pos.cast<float>(), npos.cast<float>(), m_lineColors[1],
218 m_lineWidths[1] * WideLineGeometry::lineWidthScale, 8);
219 }
220 }
221 }
222
223 QWidget* CloseContacts::setupWidget()
224 {
225 auto* widget = new QWidget(qobject_cast<QWidget*>(this->parent()));
226 auto* v = new QVBoxLayout;
227 auto* tabs = new QTabWidget;
228
229 for (Index i = 0; i < 3; i++) {
230 // maximum distance
231 auto* distance_spin = new QDoubleSpinBox;
232 distance_spin->setRange(1.5, 10.0);
233 distance_spin->setSingleStep(0.1);
234 distance_spin->setDecimals(1);
235 distance_spin->setSuffix(" Å");
236 distance_spin->setValue(m_maximumDistances[i]);
237 QObject::connect(
238 distance_spin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
239 [this, i](float distance) { return setMaximumDistance(distance, i); });
240
241 // line width
242 auto* lineWidth_spin = new QDoubleSpinBox;
243 lineWidth_spin->setRange(1.0, 10.0);
244 lineWidth_spin->setSingleStep(0.5);
245 lineWidth_spin->setDecimals(1);
246 lineWidth_spin->setValue(m_lineWidths[i]);
247 QObject::connect(lineWidth_spin,
248 QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
249 [this, i](float width) { return setLineWidth(width, i); });
250
251 auto* form = new QFormLayout;
252 form->addRow(QObject::tr("Maximum distance:"), distance_spin);
253 form->addRow(QObject::tr("Line width:"), lineWidth_spin);
254
255 auto* page = new QWidget;
256 page->setLayout(form);
257
258 switch (i) {
259 case 0:
260 tabs->addTab(page, tr("Contact"));
261 break;
262 case 1:
263 tabs->addTab(page, tr("Salt Bridge"));
264 break;
265 case 2:
266 tabs->addTab(page, tr("Repulsive"));
267 break;
268 default:;
269 }
270 }
271
272 v->addWidget(tabs);
273 v->addStretch(1);
274 widget->setLayout(v);
275 return widget;
276 }
277
278 void CloseContacts::setMaximumDistance(float maximumDistance, Index index)
279 {
280 m_maximumDistances[index] = maximumDistance;
281 emit drawablesChanged();
282
283 QSettings settings;
284 settings.setValue(QString("closeContacts/maximumDistance%1").arg(index),
285 maximumDistance);
286 }
287
288 void CloseContacts::setLineWidth(float width, Index index)
289 {
290 m_lineWidths[index] = width;
291 emit drawablesChanged();
292
293 QSettings settings;
294 settings.setValue(QString("closeContacts/lineWidth%1").arg(index), width);
295 }
296
297 } // namespace Avogadro::QtPlugins