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

Add select by residue #634

Merged
merged 1 commit into from
Jun 26, 2021
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
72 changes: 71 additions & 1 deletion avogadro/qtplugins/select/select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

#include "select.h"

#include <avogadro/core/residue.h>
#include <avogadro/qtgui/molecule.h>
#include <avogadro/qtgui/periodictableview.h>

#include <QtCore/QRegularExpression>
#include <QtCore/QRegularExpressionMatch>
#include <QtGui/QKeySequence>
#include <QtWidgets/QAction>
#include <QtWidgets/QInputDialog>
Expand Down Expand Up @@ -59,6 +62,10 @@ Select::Select(QObject* parent_)
action = new QAction(tr("Select by Atom Index..."), this);
connect(action, SIGNAL(triggered()), SLOT(selectAtomIndex()));
m_actions.append(action);

action = new QAction(tr("Select by Residue..."), this);
connect(action, SIGNAL(triggered()), SLOT(selectResidue()));
m_actions.append(action);
}

Select::~Select()
Expand Down Expand Up @@ -150,7 +157,7 @@ void Select::selectAtomIndex()
return;

auto list = text.simplified().split(',');
foreach (QString item, list) {
foreach (const QString item, list) {
// check if it's a range
if (item.contains('-')) {
auto range = item.split('-');
Expand All @@ -173,6 +180,69 @@ void Select::selectAtomIndex()
m_molecule->emitChanged(Molecule::Atoms);
}

void Select::selectResidue()
{
if (!m_molecule)
return;

bool ok;
QString text = QInputDialog::getText(
qobject_cast<QWidget*>(parent()), tr("Select Atoms by Residue"),
tr("Residues to Select:"), QLineEdit::Normal, QString(), &ok);

if (!ok || text.isEmpty())
return;

auto list = text.simplified().split(',');
foreach (const QString item, list) {
const QString label = item.simplified(); // get rid of whitespace
// check if it's a number - select that residue index
bool ok;
int index = label.toInt(&ok);
if (ok) {
auto residueList = m_molecule->residues();
if (index >= 1 && index < residueList.size()) {
auto residue = residueList[index];
for (auto atom : residue.residueAtoms()) {
atom.setSelected(true);
}
} // index makes sense
continue;
}

// okay it's not just a number, so see if it's HIS57, etc.
QRegularExpression re("([a-zA-Z]+)([0-9]+)");
QRegularExpressionMatch match = re.match(label);
if (match.hasMatch()) {
QString name = match.captured(1);
int index = match.captured(2).toInt();

auto residueList = m_molecule->residues();
if (index >= 1 && index < residueList.size()) {
auto residue = residueList[index];
if (name == residue.residueName().c_str()) {
for (auto atom : residue.residueAtoms()) {
atom.setSelected(true);
}
} // check if name matches specified (e.g. HIS57 is really a HIS)
} // index makes sense
} else {
// standard residue name
for (auto residue : m_molecule->residues()) {
if (label == residue.residueName().c_str()) {
// select the atoms of the residue
for (auto atom : residue.residueAtoms()) {
atom.setSelected(true);
}
} // residue matches label
} // for(residues)
continue;
} // 3-character labels
}

m_molecule->emitChanged(Molecule::Atoms);
}

void Select::invertSelection()
{
if (m_molecule) {
Expand Down
1 change: 1 addition & 0 deletions avogadro/qtplugins/select/select.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private slots:
void selectElement();
void selectAtomIndex();
void selectElement(int element);
void selectResidue();

private:
QList<QAction*> m_actions;
Expand Down