Debian Codegraph @

avogadro/qtplugins/insertdna/insertdna.cpp

raw
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 "insertdna.h"
7 #include "ui_insertdnadialog.h"
8
9 #include <avogadro/qtgui/fileformatdialog.h>
10 #include <avogadro/qtgui/molecule.h>
11 #include <avogadro/qtgui/rwmolecule.h>
12
13 #include <avogadro/io/fileformat.h>
14 #include <avogadro/io/fileformatmanager.h>
15
16 #include <QAction>
17 #include <QButtonGroup>
18 #include <QMessageBox>
19 #include <QProgressDialog>
20
21 #include <nlohmann/json.hpp>
22
23 using json = nlohmann::json;
24 using Avogadro::Io::FileFormat;
25 using Avogadro::QtGui::FileFormatDialog;
26
27 namespace Avogadro::QtPlugins {
28
29 class InsertDNADialog : public QDialog, public Ui::InsertDNADialog
30 {
31 public:
32 InsertDNADialog(QWidget* parent = nullptr) : QDialog(parent)
33 {
34 setWindowFlags(Qt::Dialog | Qt::Tool);
35 setupUi(this);
36 }
37 };
38
39 InsertDna::InsertDna(QObject* p)
40 : Avogadro::QtGui::ExtensionPlugin(p), m_molecule(nullptr), m_reader(nullptr),
41 m_dialog(nullptr)
42 {
43 auto* action = new QAction(tr("DNA/RNA…"), this);
44 action->setProperty("menu priority", 870);
45 connect(action, SIGNAL(triggered()), SLOT(showDialog()));
46
47 m_actions.append(action);
48 }
49
50 InsertDna::~InsertDna()
51 {
52 delete m_reader;
53 }
54
55 QList<QAction*> InsertDna::actions() const
56 {
57 return m_actions;
58 }
59
60 QStringList InsertDna::menuPath(QAction*) const
61 {
62 return QStringList() << tr("&Build") << tr("&Insert");
63 }
64
65 void InsertDna::setMolecule(QtGui::Molecule* mol)
66 {
67 m_molecule = mol;
68 }
69
70 void InsertDna::showDialog()
71 {
72 if (m_molecule == nullptr)
73 return;
74
75 // check to see if FASTA format is available from Open Babel
76 QWidget* parentAsWidget = qobject_cast<QWidget*>(parent());
77 const FileFormat::Operations ops = FileFormat::Read | FileFormat::String;
78 const FileFormat* fmt = FileFormatDialog::findFileFormat(
79 parentAsWidget, tr("Insert DNA/RNA…"), QString("file.fasta"), ops);
80
81 if (fmt == nullptr) {
82 return;
83 } else {
84 m_reader = fmt->newInstance();
85 }
86
87 // Prompt user for input:
88 if (m_dialog == nullptr) {
89 constructDialog();
90 }
91 m_dialog->show();
92 m_dialog->raise();
93 m_dialog->activateWindow();
94 }
95
96 void InsertDna::constructDialog()
97 {
98 if (m_dialog == nullptr) {
99 m_dialog = new InsertDNADialog(qobject_cast<QWidget*>(parent()));
100
101 auto* numStrands = new QButtonGroup(m_dialog);
102 numStrands->addButton(m_dialog->singleStrandRadio, 0);
103 numStrands->addButton(m_dialog->doubleStrandRadio, 1);
104 numStrands->setExclusive(true);
105
106 connect(m_dialog->insertButton, SIGNAL(clicked()), this,
107 SLOT(performInsert()));
108
109 connect(m_dialog->bpCombo, SIGNAL(currentIndexChanged(int)), this,
110 SLOT(updateBPTurns(int)));
111
112 connect(m_dialog->typeComboBox, SIGNAL(currentIndexChanged(int)), this,
113 SLOT(changeNucleicType(int)));
114
115 // Set the nucleic buttons to update the sequence
116 foreach (const QToolButton* child, m_dialog->findChildren<QToolButton*>()) {
117 connect(child, SIGNAL(clicked()), this, SLOT(updateText()));
118 }
119 connect(m_dialog, SIGNAL(destroyed()), this, SLOT(dialogDestroyed()));
120 }
121 m_dialog->sequenceText->setPlainText(QString());
122 }
123
124 void InsertDna::updateText()
125 {
126 auto* button = qobject_cast<QToolButton*>(sender());
127 if (button) {
128 QString sequenceText = m_dialog->sequenceText->toPlainText();
129 sequenceText += button->text();
130
131 m_dialog->sequenceText->setPlainText(sequenceText);
132 }
133 }
134
135 void InsertDna::updateBPTurns(int type)
136 {
137 switch (type) {
138 case 0: // A-DNA
139 m_dialog->bpTurnsSpin->setValue(11.0);
140 break;
141 case 1: // B-DNA
142 m_dialog->bpTurnsSpin->setValue(10.5);
143 break;
144 case 2: // Z-DNA
145 m_dialog->bpTurnsSpin->setValue(12.0);
146 break;
147 default:
148 // anything the user wants
149 break;
150 }
151 }
152
153 void InsertDna::changeNucleicType(int type)
154 {
155 if (type == 1) { // RNA
156 m_dialog->bpCombo->setCurrentIndex(3); // other
157 m_dialog->bpTurnsSpin->setValue(11.0); // standard RNA
158 m_dialog->singleStrandRadio->setChecked(true);
159 m_dialog->singleStrandRadio->setEnabled(false);
160 m_dialog->doubleStrandRadio->setEnabled(false);
161 m_dialog->toolButton_TU->setText(tr("U", "uracil"));
162 m_dialog->toolButton_TU->setToolTip(tr("Uracil"));
163 return;
164 }
165 // DNA
166 m_dialog->singleStrandRadio->setEnabled(true);
167 m_dialog->doubleStrandRadio->setEnabled(true);
168 m_dialog->toolButton_TU->setText(tr("T", "thymine"));
169 m_dialog->toolButton_TU->setToolTip(tr("Thymine"));
170 }
171
172 void InsertDna::performInsert()
173 {
174 if (m_dialog == nullptr || m_molecule == nullptr || m_reader == nullptr)
175 return;
176
177 QString sequence = m_dialog->sequenceText->toPlainText().toLower();
178 bool dna = (m_dialog->typeComboBox->currentIndex() == 0);
179 if (sequence.isEmpty())
180 return; // also nothing to do
181 // Add DNA/RNA tag for FASTA
182 sequence = '>' + m_dialog->typeComboBox->currentText() + '\n' + sequence;
183
184 // options
185 // if DNA, check if the user wants single-strands
186 json options;
187 json arguments;
188
189 // if it's DNA, allow single-stranded
190 if (dna && m_dialog->singleStrandRadio->isChecked())
191 arguments.push_back("-a1");
192
193 // Add the number of turns
194 QString turns = QString("-at %1").arg(m_dialog->bpTurnsSpin->value());
195 arguments.push_back(turns.toStdString());
196
197 options["arguments"] = arguments;
198 options["format"] = "pdb";
199
200 QProgressDialog progDlg;
201 progDlg.setModal(true);
202 progDlg.setWindowTitle(tr("Insert Molecule…"));
203 progDlg.setLabelText(tr("Generating 3D molecule…"));
204 progDlg.setRange(0, 0);
205 progDlg.setValue(0);
206 progDlg.show();
207
208 QtGui::Molecule newMol;
209 m_reader->setOptions(options.dump());
210 m_reader->readString(sequence.toStdString(), newMol);
211 m_molecule->undoMolecule()->appendMolecule(newMol, "Insert Molecule");
212 emit requestActiveTool("Manipulator");
213 }
214
215 void InsertDna::dialogDestroyed()
216 {
217 m_dialog = nullptr;
218 }
219
220 } // namespace Avogadro::QtPlugins