| 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 <iostream> |
| 7 |
|
| 8 |
#include "apbsdialog.h" |
| 9 |
#include "apbsoutputdialog.h" |
| 10 |
#include "ui_apbsdialog.h" |
| 11 |
|
| 12 |
#include <QFileDialog> |
| 13 |
#include <QMessageBox> |
| 14 |
#include <QProcess> |
| 15 |
|
| 16 |
#include <avogadro/molequeue/inputgenerator.h> |
| 17 |
#include <avogadro/qtgui/molecule.h> |
| 18 |
#include <avogadro/qtgui/utilities.h> |
| 19 |
|
| 20 |
namespace Avogadro::QtPlugins { |
| 21 |
|
| 22 |
using MoleQueue::InputGenerator; |
| 23 |
|
| 24 |
ApbsDialog::ApbsDialog(QWidget* parent_) |
| 25 |
: QDialog(parent_), m_ui(new Ui::ApbsDialog), m_molecule(nullptr), |
| 26 |
m_inputGenerator( |
| 27 |
new InputGenerator(QCoreApplication::applicationDirPath() + "/../" + |
| 28 |
QtGui::Utilities::libraryDirectory() + |
| 29 |
"/avogadro2/scripts/inputGenerators/apbs.py")), |
| 30 |
m_loadStructureFile(false), m_loadCubeFile(false) |
| 31 |
{ |
| 32 |
m_ui->setupUi(this); |
| 33 |
|
| 34 |
connect(m_ui->closeButton, SIGNAL(clicked()), this, SLOT(reject())); |
| 35 |
connect(m_ui->openPdbFileButton, SIGNAL(clicked()), this, |
| 36 |
SLOT(openPdbFile())); |
| 37 |
connect(m_ui->openPqrFileButton, SIGNAL(clicked()), this, |
| 38 |
SLOT(openPqrFile())); |
| 39 |
connect(m_ui->runApbsButton, SIGNAL(clicked()), this, SLOT(runApbs())); |
| 40 |
connect(m_ui->runPdb2PqrButton, SIGNAL(clicked()), this, SLOT(runPdb2Pqr())); |
| 41 |
connect(m_ui->saveInputFileButton, SIGNAL(clicked()), this, |
| 42 |
SLOT(saveInputFile())); |
| 43 |
} |
| 44 |
|
| 45 |
ApbsDialog::~ApbsDialog() |
| 46 |
{ |
| 47 |
delete m_ui; |
| 48 |
delete m_inputGenerator; |
| 49 |
} |
| 50 |
|
| 51 |
void ApbsDialog::setMolecule(QtGui::Molecule* molecule) |
| 52 |
{ |
| 53 |
if (molecule != m_molecule) { |
| 54 |
m_molecule = molecule; |
| 55 |
|
| 56 |
// clear values from previous runs |
| 57 |
m_ui->pdbFileLineEdit->clear(); |
| 58 |
m_ui->pqrFileLineEdit->clear(); |
| 59 |
m_ui->textEdit->clear(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
QString ApbsDialog::pqrFileName() const |
| 64 |
{ |
| 65 |
if (m_ui->generateFromPdbButton->isChecked()) |
| 66 |
return m_generatedPqrFileName; |
| 67 |
else |
| 68 |
return m_ui->pqrFileLineEdit->text(); |
| 69 |
} |
| 70 |
|
| 71 |
QString ApbsDialog::cubeFileName() const |
| 72 |
{ |
| 73 |
return m_cubeFileName; |
| 74 |
} |
| 75 |
|
| 76 |
void ApbsDialog::openPdbFile() |
| 77 |
{ |
| 78 |
QString fileName = QFileDialog::getOpenFileName( |
| 79 |
qobject_cast<QWidget*>(parent()), tr("Open PDB File"), QString(), |
| 80 |
tr("PDB Files (*.pdb)")); |
| 81 |
if (!fileName.isEmpty()) { |
| 82 |
m_ui->pdbFileLineEdit->setText(fileName); |
| 83 |
m_ui->generateFromPdbButton->setChecked(true); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
void ApbsDialog::openPqrFile() |
| 88 |
{ |
| 89 |
QString fileName = QFileDialog::getOpenFileName( |
| 90 |
qobject_cast<QWidget*>(parent()), tr("Open PQR File"), QString(), |
| 91 |
tr("PQR Files (*.pqr)")); |
| 92 |
if (!fileName.isEmpty()) { |
| 93 |
m_ui->pqrFileLineEdit->setText(fileName); |
| 94 |
m_ui->loadFromPqrButton->setChecked(true); |
| 95 |
} |
| 96 |
|
| 97 |
updatePreviewTextImmediately(); |
| 98 |
} |
| 99 |
|
| 100 |
void ApbsDialog::runApbs() |
| 101 |
{ |
| 102 |
saveInputFile("apbs.in"); |
| 103 |
|
| 104 |
// run apbs |
| 105 |
QStringList arguments; |
| 106 |
arguments.append("apbs.in"); |
| 107 |
|
| 108 |
QProcess process; |
| 109 |
process.start("apbs", arguments); |
| 110 |
|
| 111 |
// FIXME: show progress dialog |
| 112 |
process.waitForFinished(); |
| 113 |
|
| 114 |
if (process.exitStatus() == QProcess::NormalExit) { |
| 115 |
m_cubeFileName = "pot-PE0.dx"; |
| 116 |
|
| 117 |
ApbsOutputDialog dialog(this); |
| 118 |
connect(&dialog, SIGNAL(accepted()), this, SLOT(accept())); |
| 119 |
int code = dialog.exec(); |
| 120 |
if (code == QDialog::Accepted) { |
| 121 |
m_loadStructureFile = dialog.loadStructureFile(); |
| 122 |
m_loadCubeFile = dialog.loadCubeFile(); |
| 123 |
} else { |
| 124 |
m_loadStructureFile = false; |
| 125 |
m_loadCubeFile = false; |
| 126 |
} |
| 127 |
} else { |
| 128 |
m_loadStructureFile = false; |
| 129 |
m_loadCubeFile = false; |
| 130 |
|
| 131 |
QMessageBox::critical(this, tr("Error"), |
| 132 |
tr("Error running APBS: %1") |
| 133 |
.arg(process.readAllStandardError().constData())); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void ApbsDialog::runPdb2Pqr() |
| 138 |
{ |
| 139 |
QString pdbFileName = m_ui->pdbFileLineEdit->text(); |
| 140 |
if (pdbFileName.isEmpty()) { |
| 141 |
QMessageBox::critical(this, tr("Error"), tr("Please specify PDB file")); |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
QString pqrFileName_ = QFileInfo(pdbFileName).baseName() + ".pqr"; |
| 146 |
QString forceFieldName = m_ui->forceFieldComboBox->currentText(); |
| 147 |
|
| 148 |
// run pdb2pqr |
| 149 |
QStringList arguments; |
| 150 |
arguments.append("--ff"); |
| 151 |
arguments.append(forceFieldName); |
| 152 |
arguments.append(pdbFileName); |
| 153 |
arguments.append(pqrFileName_); |
| 154 |
|
| 155 |
QProcess process; |
| 156 |
process.start("pdb2pqr", arguments); |
| 157 |
|
| 158 |
process.waitForFinished(); |
| 159 |
|
| 160 |
if (process.exitStatus() == QProcess::NormalExit) { |
| 161 |
QMessageBox::information(this, tr("Success"), |
| 162 |
tr("Generated %1").arg(pqrFileName_)); |
| 163 |
m_generatedPqrFileName = pqrFileName_; |
| 164 |
updatePreviewTextImmediately(); |
| 165 |
} else { |
| 166 |
QMessageBox::critical(this, tr("Error"), tr("Error running PDB2PQR")); |
| 167 |
m_generatedPqrFileName.clear(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
void ApbsDialog::saveInputFile() |
| 172 |
{ |
| 173 |
QString fileName = QFileDialog::getSaveFileName( |
| 174 |
this, tr("Save APBS Input File"), "apbs.in", tr("APBS Input (*.in)")); |
| 175 |
if (!fileName.isEmpty()) { |
| 176 |
saveInputFile(fileName); |
| 177 |
|
| 178 |
QMessageBox::information(this, tr("Success"), |
| 179 |
tr("Input file written to '%1'").arg(fileName)); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
void ApbsDialog::saveInputFile(const QString& fileName) |
| 184 |
{ |
| 185 |
QString contents = m_inputGenerator->fileContents("apbs.in"); |
| 186 |
|
| 187 |
QFile file(fileName); |
| 188 |
if (!file.open(QFile::WriteOnly)) { |
| 189 |
QMessageBox::critical(this, tr("Error"), |
| 190 |
tr("Cannot save file %1.").arg(fileName)); |
| 191 |
return; |
| 192 |
} |
| 193 |
file.write(contents.toLocal8Bit()); |
| 194 |
file.close(); |
| 195 |
} |
| 196 |
|
| 197 |
void ApbsDialog::updatePreviewTextImmediately() |
| 198 |
{ |
| 199 |
QString pqrFileName_; |
| 200 |
if (m_ui->generateFromPdbButton->isChecked()) |
| 201 |
pqrFileName_ = m_generatedPqrFileName; |
| 202 |
else |
| 203 |
pqrFileName_ = m_ui->pqrFileLineEdit->text(); |
| 204 |
|
| 205 |
QJsonObject inputOptions; |
| 206 |
QJsonObject options; |
| 207 |
options["mainFile"] = pqrFileName_; |
| 208 |
options["Input File"] = pqrFileName_; |
| 209 |
options["Calculation"] = QString("mg-auto"); |
| 210 |
inputOptions["options"] = options; |
| 211 |
QJsonObject settings; |
| 212 |
inputOptions["settings"] = settings; |
| 213 |
bool success = m_inputGenerator->generateInput(inputOptions, *m_molecule); |
| 214 |
|
| 215 |
if (!success) { |
| 216 |
// FIXME: show in a dialog |
| 217 |
std::cerr << "errors: " << std::endl; |
| 218 |
foreach (const QString& error, m_inputGenerator->errorList()) { |
| 219 |
std::cerr << " " << error.toStdString() << std::endl; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
m_ui->textEdit->setText(m_inputGenerator->fileContents("apbs.in")); |
| 224 |
} |
| 225 |
} // namespace Avogadro::QtPlugins |