Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch molecule names from PubChem PUG interface #1103

Merged
merged 1 commit into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
#include "ui_molecularpropertiesdialog.h"

#include <avogadro/core/elements.h>
#include <avogadro/io/fileformatmanager.h>
#include <avogadro/qtgui/molecule.h>

#include <QtCore/QRegExp>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>

using Avogadro::QtGui::Molecule;

namespace Avogadro::QtPlugins {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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("<h1>")) {
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;
Expand Down Expand Up @@ -90,4 +156,4 @@ void MolecularPropertiesDialog::moleculeDestroyed()
updateLabels();
}

} // namespace Avogadro
} // namespace Avogadro::QtPlugins
13 changes: 13 additions & 0 deletions avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#include <QtWidgets/QDialog>

// Forward declarations
class QNetworkAccessManager;
class QNetworkReply;

namespace Avogadro {

namespace QtGui {
Expand Down Expand Up @@ -44,6 +48,7 @@ public slots:
void setMolecule(QtGui::Molecule* mol);

private slots:
void updateName();
void updateLabels();
void updateMassLabel();
void updateFormulaLabel();
Expand All @@ -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
Expand Down
37 changes: 27 additions & 10 deletions avogadro/qtplugins/molecularproperties/molecularpropertiesdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>316</width>
<height>107</height>
<width>405</width>
<height>183</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -19,62 +19,79 @@
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
</property>
<item row="0" column="0">
<property name="formAlignment">
<set>Qt::AlignHCenter|Qt::AlignTop</set>
</property>
<item row="0" column="1">
<widget class="QLabel" name="moleculeNameLabel">
<property name="text">
<string>TODO</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Molecular Mass (g/mol):</string>
</property>
</widget>
</item>
<item row="0" column="1">
<item row="1" column="1">
<widget class="QLabel" name="molMassLabel">
<property name="text">
<string>TODO</string>
</property>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Chemical Formula:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<item row="2" column="1">
<widget class="QLabel" name="formulaLabel">
<property name="text">
<string>TODO</string>
</property>
</widget>
</item>
<item row="2" column="0">
<item row="3" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Number of Atoms:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QLabel" name="atomCountLabel">
<property name="text">
<string>TODO</string>
</property>
</widget>
</item>
<item row="3" column="0">
<item row="4" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Number of Bonds:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<item row="4" column="1">
<widget class="QLabel" name="bondCountLabel">
<property name="text">
<string>TODO</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Molecule Name:</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down