From ca33dce11113b64bf90895138e72ccbcf0887a4c Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Tue, 16 Aug 2022 14:14:26 -0400 Subject: [PATCH] Fetch molecule names from PubChem PUG interface Fix #342 Signed-off-by: Geoff Hutchison --- .../molecularpropertiesdialog.cpp | 68 ++++++++++++++++++- .../molecularpropertiesdialog.h | 13 ++++ .../molecularpropertiesdialog.ui | 37 +++++++--- 3 files changed, 107 insertions(+), 11 deletions(-) diff --git a/avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.cpp b/avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.cpp index 48be6f5ae5..26cf67ed57 100644 --- a/avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.cpp +++ b/avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.cpp @@ -7,8 +7,13 @@ #include "ui_molecularpropertiesdialog.h" #include +#include #include +#include +#include +#include + using Avogadro::QtGui::Molecule; namespace Avogadro::QtPlugins { @@ -20,6 +25,10 @@ MolecularPropertiesDialog::MolecularPropertiesDialog(QtGui::Molecule* mol, { m_ui->setupUi(this); + m_network = new QNetworkAccessManager(this); + connect(m_network, SIGNAL(finished(QNetworkReply*)), this, + SLOT(replyFinished(QNetworkReply*))); + setMolecule(mol); } @@ -51,6 +60,7 @@ void MolecularPropertiesDialog::updateLabels() if (m_molecule) { updateMassLabel(); updateFormulaLabel(); + updateName(); m_ui->atomCountLabel->setText(QString::number(m_molecule->atomCount())); m_ui->bondCountLabel->setText(QString::number(m_molecule->bondCount())); } else { @@ -61,6 +71,62 @@ void MolecularPropertiesDialog::updateLabels() } } +void MolecularPropertiesDialog::updateName() +{ + QString name = tr("(pending)", "asking server for molecule name"); + + if (!m_molecule || m_molecule->atomCount() == 0) { + m_ui->moleculeNameLabel->clear(); + return; + } + + m_ui->moleculeNameLabel->setText(name); // while we wait + + // InChI is intentionally designed to avoid issues with URL encoding + std::string smiles; + Io::FileFormatManager::instance().writeString(*m_molecule, smiles, "smi"); + QString smilesString = QString::fromStdString(smiles); + smilesString.remove(QRegExp("\\s+.*")); + QString requestURL = + QString("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/smiles/" + + QUrl::toPercentEncoding(smilesString) + "/property/IUPACName/TXT"); + + // qDebug() << "Requesting" << requestURL; + + m_network->get(QNetworkRequest(QUrl(requestURL))); + + // TODO: fetch and parse JSON eventually + // https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/smiles/…/json +} + +void MolecularPropertiesDialog::replyFinished(QNetworkReply* reply) +{ + // Read in all the data + if (!reply->isReadable()) { + reply->deleteLater(); + m_ui->moleculeNameLabel->setText(tr("unknown molecule")); + return; + } + + // check if the data came through + QByteArray data = reply->readAll(); + if (data.contains("Error report") || data.contains("

")) { + reply->deleteLater(); + m_ui->moleculeNameLabel->setText(tr("unknown molecule")); + return; + } + + QString name = QString(data).trimmed().toLower(); + if (!name.isEmpty()) { + m_ui->moleculeNameLabel->setText(name); + // TODO: set the name in the molecule + } else { + m_ui->moleculeNameLabel->setText(tr("unknown molecule")); + } + + reply->deleteLater(); +} + void MolecularPropertiesDialog::updateMassLabel() { double mass = 0.0; @@ -90,4 +156,4 @@ void MolecularPropertiesDialog::moleculeDestroyed() updateLabels(); } -} // namespace Avogadro +} // namespace Avogadro::QtPlugins diff --git a/avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.h b/avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.h index fc1b16ebc2..f5cb30f721 100644 --- a/avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.h +++ b/avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.h @@ -8,6 +8,10 @@ #include +// Forward declarations +class QNetworkAccessManager; +class QNetworkReply; + namespace Avogadro { namespace QtGui { @@ -44,6 +48,7 @@ public slots: void setMolecule(QtGui::Molecule* mol); private slots: + void updateName(); void updateLabels(); void updateMassLabel(); void updateFormulaLabel(); @@ -52,6 +57,14 @@ private slots: private: QtGui::Molecule* m_molecule; Ui::MolecularPropertiesDialog* m_ui; + + QString m_inchi; + QNetworkAccessManager *m_network; + bool m_nameRequestPending; + +private slots: + void replyFinished(QNetworkReply*); + }; } // namespace QtPlugins diff --git a/avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.ui b/avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.ui index 65733db5a5..cff87ed91d 100644 --- a/avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.ui +++ b/avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.ui @@ -6,8 +6,8 @@ 0 0 - 316 - 107 + 405 + 183 @@ -19,62 +19,79 @@ QFormLayout::ExpandingFieldsGrow - + + Qt::AlignHCenter|Qt::AlignTop + + + + + TODO + + + + Molecular Mass (g/mol): - + TODO - + Chemical Formula: - + TODO - + Number of Atoms: - + TODO - + Number of Bonds: - + TODO + + + + Molecule Name: + + +