Debian Codegraph @

avogadro/qtplugins/plotconformer/plotconformer.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 "plotconformer.h"
7
8 #include <QAction>
9 #include <QMessageBox>
10 #include <QProcess>
11 #include <QString>
12 #include <QVBoxLayout>
13 #include <QHBoxLayout>
14 #include <QLabel>
15 #include <QComboBox>
16 #include <QLineEdit>
17
18 #include <avogadro/core/array.h>
19 #include <avogadro/io/fileformatmanager.h>
20 #include <avogadro/qtgui/molecule.h>
21 #include <avogadro/qtgui/chartdialog.h>
22 #include <avogadro/qtgui/chartwidget.h>
23
24 using Avogadro::QtGui::Molecule;
25
26 namespace Avogadro::QtPlugins {
27
28 constexpr double HartreeToKcal = 627.5094740631;
29 constexpr double EvToKcal = 23.06054;
30 constexpr double KcalToKJ = 4.184; // by definition
31
32 using Core::Array;
33
34 PlotConformer::PlotConformer(QObject* parent_)
35 : Avogadro::QtGui::ExtensionPlugin(parent_), m_actions(QList<QAction*>()),
36 m_molecule(nullptr), m_displayDialogAction(new QAction(this))
37 {
38 m_displayDialogAction->setText(tr("Plot Conformer Data…"));
39 connect(m_displayDialogAction, &QAction::triggered, this,
40 &PlotConformer::displayDialog);
41 m_actions.push_back(m_displayDialogAction);
42 m_displayDialogAction->setProperty("menu priority", -890);
43
44 updateActions();
45 }
46
47 PlotConformer::~PlotConformer() = default;
48
49 QList<QAction*> PlotConformer::actions() const
50 {
51 return m_actions;
52 }
53
54 QStringList PlotConformer::menuPath(QAction*) const
55 {
56 return QStringList() << tr("&Analyze");
57 }
58
59 void PlotConformer::setMolecule(QtGui::Molecule* mol)
60 {
61 if (m_molecule == mol)
62 return;
63
64 if (m_molecule)
65 m_molecule->disconnect(this);
66
67 m_molecule = mol;
68
69 if (m_molecule)
70 connect(m_molecule, SIGNAL(changed(uint)), SLOT(moleculeChanged(uint)));
71
72 updateActions();
73 }
74
75 void PlotConformer::moleculeChanged(unsigned int c)
76 {
77 Q_ASSERT(m_molecule == qobject_cast<Molecule*>(sender()));
78
79 auto changes = static_cast<Molecule::MoleculeChanges>(c);
80
81 if (changes & Molecule::Added || changes & Molecule::Removed)
82 updateActions();
83 }
84
85 void PlotConformer::updateActions()
86 {
87 // Disable everything for nullptr molecules.
88 if (!m_molecule) {
89 foreach (QAction* action, m_actions)
90 action->setEnabled(false);
91 return;
92 }
93
94 // Only display the actions if multimolecule.
95 if (m_molecule->coordinate3dCount() > 1) {
96 foreach (QAction* action, m_actions)
97 action->setEnabled(true);
98 } else {
99 foreach (QAction* action, m_actions)
100 action->setEnabled(false);
101 }
102 }
103
104 void PlotConformer::clicked(float x, float y, Qt::KeyboardModifiers modifiers)
105 {
106 // switch to the closest conformer to x
107 int conformer = static_cast<int>(x);
108 if (conformer < 0)
109 conformer = 0;
110 if (conformer >= m_molecule->coordinate3dCount())
111 conformer = m_molecule->coordinate3dCount() - 1;
112 m_currentFrame = conformer;
113 m_molecule->setCoordinate3d(conformer);
114 m_molecule->emitChanged(Molecule::Atoms);
115 updatePlot();
116 }
117
118 void PlotConformer::displayDialog()
119 {
120 bool hasEnergies = (m_molecule->hasData("energies"));
121 // RMSD forces for each coordinate set
122 bool hasForces = (m_molecule->hasData("forces"));
123 // and velocities for MD
124 bool hasVelocities = (m_molecule->hasData("velocities"));
125
126 if (!m_dialog) {
127 // Create the dialog
128 m_dialog.reset(new QDialog(qobject_cast<QWidget*>(this->parent())));
129 m_dialog->setWindowTitle(tr("Conformer Analysis"));
130 m_dialog->resize(600, 500);
131
132 // Create main layout
133 QVBoxLayout* mainLayout = new QVBoxLayout(m_dialog.get());
134
135 // Create chart widget
136 m_chartWidget = new QtGui::ChartWidget(m_dialog.get());
137 connect(m_chartWidget, &QtGui::ChartWidget::clicked, this,
138 &PlotConformer::clicked);
139 mainLayout->addWidget(m_chartWidget);
140
141 // Create property selection layout
142 QHBoxLayout* propertyLayout = new QHBoxLayout();
143 QLabel* propertyLabel = new QLabel(tr("Plot Type:"), m_dialog.get());
144 m_propertyCombo = new QComboBox(m_dialog.get());
145 m_propertyCombo->addItem(tr("RMSD"), "rmsd");
146 if (hasEnergies)
147 m_propertyCombo->addItem(tr("Energy"), "energy");
148 if (hasForces)
149 m_propertyCombo->addItem(tr("Forces"), "forces");
150 if (hasVelocities)
151 m_propertyCombo->addItem(tr("Velocities"), "velocities");
152
153 propertyLayout->addWidget(propertyLabel);
154 propertyLayout->addWidget(m_propertyCombo);
155 propertyLayout->addStretch();
156 mainLayout->addLayout(propertyLayout);
157
158 // Create energy conversion layout
159 QHBoxLayout* conversionLayout = new QHBoxLayout();
160 QLabel* conversionLabel = new QLabel(tr("Energy Units:"), m_dialog.get());
161 m_unitsCombo = new QComboBox(m_dialog.get());
162 m_unitsCombo->addItem(QStringLiteral("Hartree"), HartreeToKcal);
163 m_unitsCombo->addItem(QStringLiteral("eV"), EvToKcal);
164 m_unitsCombo->addItem(QStringLiteral("kcal/mol"), 1.0);
165 m_unitsCombo->addItem(QStringLiteral("kJ/mol"), KcalToKJ);
166 if (!hasEnergies)
167 m_unitsCombo->setEnabled(false);
168
169 QLabel* targetLabel = new QLabel(tr("to"), m_dialog.get());
170 m_targetUnitsCombo = new QComboBox(m_dialog.get());
171 m_targetUnitsCombo->addItem(QStringLiteral("kcal/mol"), 1.0);
172 m_targetUnitsCombo->addItem(QStringLiteral("kJ/mol"), KcalToKJ);
173 m_targetUnitsCombo->addItem(QStringLiteral("eV"), 1.0 / EvToKcal);
174 m_targetUnitsCombo->addItem(QStringLiteral("Hartree"), 1.0 / HartreeToKcal);
175 if (!hasEnergies)
176 m_targetUnitsCombo->setEnabled(false);
177
178 conversionLayout->addWidget(conversionLabel);
179 conversionLayout->addWidget(m_unitsCombo);
180 conversionLayout->addWidget(targetLabel);
181 conversionLayout->addWidget(m_targetUnitsCombo);
182 conversionLayout->addStretch();
183 mainLayout->addLayout(conversionLayout);
184
185 // Connect signals for updates
186 connect(m_propertyCombo,
187 QOverload<int>::of(&QComboBox::currentIndexChanged), this,
188 &PlotConformer::updatePlot);
189 connect(m_unitsCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
190 this, &PlotConformer::updatePlot);
191 connect(m_targetUnitsCombo,
192 QOverload<int>::of(&QComboBox::currentIndexChanged), this,
193 &PlotConformer::updatePlot);
194 }
195
196 updatePlot();
197 m_dialog->show();
198 m_dialog->raise();
199 m_dialog->activateWindow();
200 }
201
202 void PlotConformer::updatePlot()
203 {
204 if (!m_molecule || !m_chartWidget)
205 return;
206
207 DataSeries xData, yData;
208
209 QString plotType = m_propertyCombo->currentData().toString();
210
211 if (plotType == "rmsd") {
212 generateRmsdCurve(xData, yData);
213 } else if (plotType == "energy" && m_molecule->hasData("energies")) {
214 generateEnergyCurve(xData, yData);
215 } else if (plotType == "forces" && m_molecule->hasData("forces")) {
216 generateForcesCurve(xData, yData);
217 } else if (plotType == "velocities" && m_molecule->hasData("velocities")) {
218 generateVelocitiesCurve(xData, yData);
219 }
220
221 // Now generate a plot with the data
222 float min = *std::min_element(yData.begin(), yData.end());
223 float max = *std::max_element(yData.begin(), yData.end());
224
225 const char* xTitle = "Frame";
226 QString yTitle;
227
228 if (plotType == "rmsd") {
229 yTitle = tr("RMSD (Å)");
230 } else if (plotType == "energy" && m_molecule->hasData("energies")) {
231 QString targetUnit = m_targetUnitsCombo->currentText();
232 yTitle = tr("Relative Energy (%1)").arg(targetUnit);
233 } else if (plotType == "forces" && m_molecule->hasData("forces")) {
234 // TODO: Add units
235 yTitle = tr("Forces (N)");
236 } else if (plotType == "velocities" && m_molecule->hasData("velocities")) {
237 yTitle = tr("Velocities (m/s)");
238 }
239
240 m_chartWidget->clearPlots();
241 m_chartWidget->setShowPoints(true);
242 m_chartWidget->setLegendLocation(QtGui::ChartWidget::LegendLocation::None);
243 m_chartWidget->addPlot(xData, yData, QtGui::color4ub{ 255, 0, 0, 255 });
244
245 // Add a marker for the current frame
246 if (m_currentFrame >= 0 && m_currentFrame < static_cast<int>(xData.size())) {
247 DataSeries markerX = { xData[m_currentFrame] };
248 DataSeries markerY = { yData[m_currentFrame] };
249 m_chartWidget->addPlot(markerX, markerY,
250 QtGui::color4ub{ 255, 165, 0, 255 });
251 }
252
253 // make sure to pad the axes slightly
254 m_chartWidget->setXAxisLimits(
255 -0.1, static_cast<float>(m_molecule->coordinate3dCount()) - 0.9);
256 m_chartWidget->setYAxisLimits(min, max * 1.1f);
257 m_chartWidget->setXAxisTitle(xTitle);
258 m_chartWidget->setYAxisTitle(yTitle);
259 }
260
261 void PlotConformer::generateRmsdCurve(DataSeries& x, DataSeries& y)
262 {
263 if (!m_molecule)
264 return;
265
266 m_molecule->setCoordinate3d(0);
267 Array<Vector3> ref = m_molecule->atomPositions3d();
268
269 for (int i = 0; i < m_molecule->coordinate3dCount(); ++i) {
270 m_molecule->setCoordinate3d(i);
271 Array<Vector3> positions = m_molecule->atomPositions3d();
272 double sum = 0;
273 for (size_t j = 0; j < positions.size(); ++j) {
274 sum += (positions[j][0] - ref[j][0]) * (positions[j][0] - ref[j][0]) +
275 (positions[j][1] - ref[j][1]) * (positions[j][1] - ref[j][1]) +
276 (positions[j][2] - ref[j][2]) * (positions[j][2] - ref[j][2]);
277 }
278 sum = sqrt(sum / m_molecule->coordinate3dCount());
279 x.push_back(i);
280 y.push_back(sum);
281 }
282 }
283
284 void PlotConformer::generateEnergyCurve(DataSeries& x, DataSeries& y)
285 {
286 // plot relative energies so get the minimum first
287 if (m_molecule == nullptr || !m_molecule->hasData("energies")) {
288 return;
289 }
290
291 std::vector<double> energies = m_molecule->data("energies").toList();
292 // calculate the minimum
293 double minEnergy = std::numeric_limits<double>::max();
294 for (double e : energies) {
295 minEnergy = std::min(minEnergy, e);
296 }
297
298 // Get conversion factors
299 double fromFactor = m_unitsCombo->currentData().toDouble();
300 double toFactor = m_targetUnitsCombo->currentData().toDouble();
301
302 // okay, now loop through to generate the curve
303 for (int entry = 0; entry < energies.size(); entry++) {
304 double relativeE = energies[entry] - minEnergy;
305 // Convert: first to kcal/mol, then to target units
306 relativeE = relativeE * fromFactor * toFactor;
307
308 x.push_back(static_cast<double>(entry));
309 y.push_back(relativeE);
310 }
311 }
312
313 void PlotConformer::generateForcesCurve(DataSeries& x, DataSeries& y)
314 {
315 if (m_molecule == nullptr || !m_molecule->hasData("forces")) {
316 return;
317 }
318
319 std::vector<double> forces = m_molecule->data("forces").toList();
320
321 // okay, now loop through to generate the curve
322 for (int entry = 0; entry < forces.size(); entry++) {
323 // TODO : Add units
324 x.push_back(static_cast<double>(entry));
325 y.push_back(forces[entry]);
326 }
327 }
328
329 void PlotConformer::generateVelocitiesCurve(DataSeries& x, DataSeries& y)
330 {
331 if (m_molecule == nullptr || !m_molecule->hasData("velocities")) {
332 return;
333 }
334
335 std::vector<double> velocities = m_molecule->data("velocities").toList();
336
337 // okay, now loop through to generate the curve
338 for (int entry = 0; entry < velocities.size(); entry++) {
339 // TODO : Add units
340 x.push_back(static_cast<double>(entry));
341 y.push_back(velocities[entry]);
342 }
343 }
344
345 } // namespace Avogadro::QtPlugins