| 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 "fetchpdb.h" |
| 7 |
|
| 8 |
#include <avogadro/io/fileformatmanager.h> |
| 9 |
#include <avogadro/qtgui/molecule.h> |
| 10 |
|
| 11 |
#include <QAction> |
| 12 |
#include <QtCore/QDir> |
| 13 |
#include <QtCore/QFile> |
| 14 |
#include <QtNetwork/QNetworkAccessManager> |
| 15 |
#include <QtNetwork/QNetworkReply> |
| 16 |
#include <QtWidgets/QInputDialog> |
| 17 |
#include <QtWidgets/QMessageBox> |
| 18 |
#include <QtWidgets/QProgressDialog> |
| 19 |
|
| 20 |
namespace Avogadro::QtPlugins { |
| 21 |
|
| 22 |
FetchPDB::FetchPDB(QObject* parent_) |
| 23 |
: ExtensionPlugin(parent_), m_action(new QAction(this)), m_molecule(nullptr), |
| 24 |
m_network(nullptr), m_progressDialog(nullptr) |
| 25 |
{ |
| 26 |
m_action->setEnabled(true); |
| 27 |
m_action->setText("Fetch from &PDB…"); |
| 28 |
m_action->setProperty("menu priority", 180); |
| 29 |
connect(m_action, SIGNAL(triggered()), SLOT(showDialog())); |
| 30 |
} |
| 31 |
|
| 32 |
FetchPDB::~FetchPDB() {} |
| 33 |
|
| 34 |
QList<QAction*> FetchPDB::actions() const |
| 35 |
{ |
| 36 |
return QList<QAction*>() << m_action; |
| 37 |
} |
| 38 |
|
| 39 |
QStringList FetchPDB::menuPath(QAction*) const |
| 40 |
{ |
| 41 |
return QStringList() << tr("&File") << tr("&Import"); |
| 42 |
} |
| 43 |
|
| 44 |
void FetchPDB::setMolecule(QtGui::Molecule* mol) |
| 45 |
{ |
| 46 |
m_molecule = mol; |
| 47 |
} |
| 48 |
|
| 49 |
bool FetchPDB::readMolecule(QtGui::Molecule& mol) |
| 50 |
{ |
| 51 |
if (m_moleculeData.isEmpty() || m_moleculeName.isEmpty()) |
| 52 |
return false; |
| 53 |
|
| 54 |
bool readOK = Io::FileFormatManager::instance().readFile( |
| 55 |
mol, m_tempFileName.toStdString(), "pdb"); |
| 56 |
if (readOK) // worked, so set the filename |
| 57 |
mol.setData("name", m_moleculeName.toStdString()); |
| 58 |
else |
| 59 |
// if it didn't read, show a dialog |
| 60 |
QMessageBox::warning( |
| 61 |
qobject_cast<QWidget*>(parent()), tr("Fetch PDB"), |
| 62 |
tr("Could not read the PDB molecule: %1").arg(m_moleculeName)); |
| 63 |
|
| 64 |
return readOK; |
| 65 |
} |
| 66 |
|
| 67 |
void FetchPDB::showDialog() |
| 68 |
{ |
| 69 |
// Prompt for a chemical structure name |
| 70 |
bool ok; |
| 71 |
QString pdbCode = QInputDialog::getText( |
| 72 |
qobject_cast<QWidget*>(parent()), tr("PDB Code"), |
| 73 |
tr("Chemical structure to download."), QLineEdit::Normal, "", &ok); |
| 74 |
|
| 75 |
if (!ok || pdbCode.isEmpty()) |
| 76 |
return; |
| 77 |
|
| 78 |
// check if the PDB code matches the expected format |
| 79 |
if (pdbCode.length() != 4) { |
| 80 |
QMessageBox::warning(qobject_cast<QWidget*>(parent()), |
| 81 |
tr("Invalid PDB Code"), |
| 82 |
tr("The PDB code must be exactly 4 characters long.")); |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
// first character should be 1-9 |
| 87 |
if (!pdbCode.at(0).isDigit() || pdbCode.at(0).toLatin1() == '0') { |
| 88 |
QMessageBox::warning( |
| 89 |
qobject_cast<QWidget*>(parent()), tr("Invalid PDB Code"), |
| 90 |
tr("The first character of the PDB code must be 1-9.")); |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
if (!m_network) { |
| 95 |
m_network = new QNetworkAccessManager(this); |
| 96 |
connect(m_network, SIGNAL(finished(QNetworkReply*)), this, |
| 97 |
SLOT(replyFinished(QNetworkReply*))); |
| 98 |
} |
| 99 |
|
| 100 |
// Hard coding the PDB download URL |
| 101 |
m_network->get(QNetworkRequest( |
| 102 |
QUrl("https://files.rcsb.org/download/" + pdbCode + ".pdb"))); |
| 103 |
|
| 104 |
if (!m_progressDialog) { |
| 105 |
m_progressDialog = new QProgressDialog(qobject_cast<QWidget*>(parent())); |
| 106 |
} |
| 107 |
|
| 108 |
m_moleculeName = pdbCode; |
| 109 |
m_progressDialog->setLabelText(tr("Querying for %1").arg(pdbCode)); |
| 110 |
m_progressDialog->setRange(0, 0); |
| 111 |
m_progressDialog->show(); |
| 112 |
} |
| 113 |
|
| 114 |
void FetchPDB::replyFinished(QNetworkReply* reply) |
| 115 |
{ |
| 116 |
m_progressDialog->hide(); |
| 117 |
// Read in all the data |
| 118 |
if (!reply->isReadable()) { |
| 119 |
QMessageBox::warning(qobject_cast<QWidget*>(parent()), |
| 120 |
tr("Network Download Failed"), |
| 121 |
tr("Network timeout or other error.")); |
| 122 |
reply->deleteLater(); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
m_moleculeData = reply->readAll(); |
| 127 |
|
| 128 |
// Check if the file was successfully downloaded |
| 129 |
if (m_moleculeData.contains("Not Found") || |
| 130 |
m_moleculeData.contains("Error report") || |
| 131 |
m_moleculeData.contains("Page not found (404)")) { |
| 132 |
QMessageBox::warning( |
| 133 |
qobject_cast<QWidget*>(parent()), tr("Network Download Failed"), |
| 134 |
tr("Specified molecule could not be found: %1").arg(m_moleculeName)); |
| 135 |
reply->deleteLater(); |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
m_tempFileName = |
| 140 |
QDir::tempPath() + QDir::separator() + m_moleculeName + ".pdb"; |
| 141 |
QFile out(m_tempFileName); |
| 142 |
if (!out.open(QIODevice::WriteOnly)) { |
| 143 |
QMessageBox::warning(qobject_cast<QWidget*>(parent()), tr("Error"), |
| 144 |
tr("Cannot save file %1.").arg(m_tempFileName)); |
| 145 |
return; |
| 146 |
} |
| 147 |
out.write(m_moleculeData); |
| 148 |
out.close(); |
| 149 |
|
| 150 |
emit moleculeReady(1); |
| 151 |
reply->deleteLater(); |
| 152 |
} |
| 153 |
|
| 154 |
} // namespace Avogadro::QtPlugins |