Skip to content

Commit

Permalink
Merge pull request #1726 from ghutchis/add-atom-charge-labels
Browse files Browse the repository at this point in the history
Add support for atomic partial charge labels
  • Loading branch information
ghutchis authored Oct 11, 2024
2 parents 69d7958 + 709eece commit c27a681
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions avogadro/qtplugins/label/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ avogadro_plugin(LabelEditor
)

target_link_libraries(Label LINK_PRIVATE Avogadro::Rendering)
target_link_libraries(Label PRIVATE Avogadro::Calc)

target_link_libraries(LabelEditor
LINK_PRIVATE
Avogadro::Rendering
Expand Down
48 changes: 47 additions & 1 deletion avogadro/qtplugins/label/label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <iostream>
#include <sstream>

// for partial charges
#include <avogadro/calc/chargemanager.h>

#include <avogadro/core/elements.h>
#include <avogadro/core/residue.h>
#include <avogadro/qtgui/colorbutton.h>
Expand Down Expand Up @@ -67,7 +70,8 @@ struct LayerLabel : Core::LayerData
Name = 2,
Custom = 4,
Ordinal = 8,
UniqueID = 16
UniqueID = 16,
PartialCharge = 32
};
unsigned short atomOptions;
unsigned short residueOptions;
Expand Down Expand Up @@ -164,6 +168,8 @@ struct LayerLabel : Core::LayerData
int(LabelOptions::Ordinal));
atom->addItem(QObject::tr("Element & ID"),
int(LabelOptions::Name | LabelOptions::UniqueID));
atom->addItem(QObject::tr("Partial Charge", "atomic partial charge"),
int(LabelOptions::PartialCharge));
atom->addItem(QObject::tr("Custom"), int(LabelOptions::Custom));

// check for current option based on item data
Expand Down Expand Up @@ -282,6 +288,42 @@ void Label::processResidue(const Core::Molecule& molecule,
}
}

QString partialCharge(Molecule* molecule, int atom)
{
// TODO: we need to track type and/or calling the charge calculator
float charge = 0.0;
std::set<std::string> types = molecule->partialChargeTypes();
if (types.size() > 0) {
auto first = types.cbegin();
MatrixX charges = molecule->partialCharges((*first));
charge = charges(atom, 0);
} else {
// find something
const auto options =
Calc::ChargeManager::instance().identifiersForMolecule(*molecule);
if (options.size() > 0) {
// look for GFN2 or AM1BCC, then MMFF94 then Gasteiger
std::string type;
if (options.find("GFN2") != options.end())
type = "GFN2";
else if (options.find("am1bcc") != options.end())
type = "am1bcc";
else if (options.find("mmff94") != options.end())
type = "mmff94";
else if (options.find("gasteiger") != options.end())
type = "gasteiger";
else
type = *options.begin();

MatrixX charges =
Calc::ChargeManager::instance().partialCharges(type, *molecule);
charge = charges(atom, 0);
}
}
// e.g. '-0.12' => 5 characters
return QString("%L1").arg(charge, 5, 'f', 2);
}

void Label::processAtom(const Core::Molecule& molecule,
Rendering::GroupNode& node, size_t layer)
{
Expand All @@ -305,6 +347,10 @@ void Label::processAtom(const Core::Molecule& molecule,
auto& interface = m_layerManager.getSetting<LayerLabel>(layer);
std::string text = "";

if (interface.atomOptions & LayerLabel::LabelOptions::PartialCharge) {
QString charge = partialCharge(const_cast<Molecule*>(&molecule), i);
text += charge.toStdString();
}
if (interface.atomOptions & LayerLabel::LabelOptions::Custom) {
text += (text == "" ? "" : " / ") + atom.label();
}
Expand Down

0 comments on commit c27a681

Please sign in to comment.