/
HMMBuildDialogController.cpp
388 lines (333 loc) · 12.7 KB
/
HMMBuildDialogController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
* UGENE - Integrated Bioinformatics Tools.
* Copyright (C) 2008-2024 UniPro <ugene@unipro.ru>
* http://ugene.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "HMMBuildDialogController.h"
#include <QMessageBox>
#include <QPushButton>
#include <U2Core/AppContext.h>
#include <U2Core/BaseDocumentFormats.h>
#include <U2Core/Counter.h>
#include <U2Core/DNAAlphabet.h>
#include <U2Core/DocumentModel.h>
#include <U2Core/FileFilters.h>
#include <U2Core/IOAdapter.h>
#include <U2Core/IOAdapterUtils.h>
#include <U2Core/LoadDocumentTask.h>
#include <U2Core/MsaObject.h>
#include <U2Core/U2OpStatusUtils.h>
#include <U2Gui/HelpButton.h>
#include <U2Gui/LastUsedDirHelper.h>
#include <U2Gui/SaveDocumentController.h>
#include <U2Gui/U2FileDialog.h>
#include "HMMIO.h"
#include "TaskLocalStorage.h"
#include "hmmer2/funcs.h"
namespace U2 {
HMMBuildDialogController::HMMBuildDialogController(const QString& _pn, const Msa& _ma, QWidget* p)
: QDialog(p),
ma(_ma->getCopy()),
profileName(_pn),
saveController(nullptr) {
setupUi(this);
new HelpButton(this, buttonBox, "65930810");
buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Build"));
buttonBox->button(QDialogButtonBox::Cancel)->setText(tr("Close"));
if (!ma->isEmpty()) {
msaFileButton->setHidden(true);
msaFileEdit->setHidden(true);
msaFileLabel->setHidden(true);
}
initSaveController();
okButton = buttonBox->button(QDialogButtonBox::Ok);
cancelButton = buttonBox->button(QDialogButtonBox::Cancel);
connect(msaFileButton, SIGNAL(clicked()), SLOT(sl_msaFileClicked()));
connect(okButton, SIGNAL(clicked()), SLOT(sl_okClicked()));
task = nullptr;
}
void HMMBuildDialogController::sl_msaFileClicked() {
LastUsedDirHelper lod;
lod.url = U2FileDialog::getOpenFileName(this, tr("Select file with alignment"), lod, FileFilters::createFileFilterByObjectTypes({GObjectTypes::MULTIPLE_SEQUENCE_ALIGNMENT}));
if (lod.url.isEmpty()) {
return;
}
msaFileEdit->setText(QFileInfo(lod.url).absoluteFilePath());
}
void HMMBuildDialogController::sl_okClicked() {
if (task != nullptr) {
accept(); // go to background
return;
}
// prepare model
UHMMBuildSettings s;
s.name = profileName;
QString errMsg;
QString inFile = msaFileEdit->text();
if (ma->isEmpty() && (inFile.isEmpty() || !QFileInfo(inFile).exists())) {
errMsg = tr("Incorrect alignment file!");
msaFileEdit->setFocus();
}
QString outFile = saveController->getSaveFileName();
if (outFile.isEmpty() && errMsg.isEmpty()) {
errMsg = tr("Incorrect HMM file!");
resultFileEdit->setFocus();
}
if (expertGroup->isChecked() && errMsg.isEmpty()) {
s.name = nameEdit->text().trimmed();
s.strategy = P7_LS_CONFIG;
if (hmmfsButton->isChecked()) {
s.strategy = P7_FS_CONFIG;
} else if (hmmsButton->isChecked()) {
s.strategy = P7_BASE_CONFIG;
} else if (hmmswButton->isChecked()) {
s.strategy = P7_SW_CONFIG;
}
}
if (!errMsg.isEmpty()) {
QMessageBox::critical(this, tr("Error"), errMsg);
return;
}
task = ma->isEmpty() ? new HMMBuildToFileTask(inFile, outFile, s) : new HMMBuildToFileTask(ma, outFile, s);
task->setReportingEnabled(true);
connect(task, SIGNAL(si_stateChanged()), SLOT(sl_onStateChanged()));
connect(task, SIGNAL(si_progressChanged()), SLOT(sl_onProgressChanged()));
AppContext::getTaskScheduler()->registerTopLevelTask(task);
statusLabel->setText(tr("Starting build process"));
// update buttons
okButton->setText(tr("Hide"));
cancelButton->setText(tr("Cancel"));
// new default behavior: hide dialog and use taskview to track the progress and results
accept(); // go to background
}
void HMMBuildDialogController::reject() {
if (task != nullptr) {
task->cancel();
}
QDialog::reject();
}
void HMMBuildDialogController::sl_onStateChanged() {
auto t = qobject_cast<Task*>(sender());
assert(task != nullptr);
if (task != t || t->getState() != Task::State_Finished) {
return;
}
task->disconnect(this);
const TaskStateInfo& si = task->getStateInfo();
if (si.hasError()) {
statusLabel->setText(tr("HMM build finished with errors: %1").arg(si.getError()));
} else if (task->isCanceled()) {
statusLabel->setText(tr("HMM build canceled"));
} else {
statusLabel->setText(tr("HMM build finished successfully!"));
}
okButton->setText(tr("Build"));
cancelButton->setText(tr("Close"));
AppContext::getTaskScheduler()->disconnect(this);
task = nullptr;
}
void HMMBuildDialogController::sl_onProgressChanged() {
assert(task == sender());
statusLabel->setText(tr("Progress: %1%").arg(task->getProgress()));
}
void HMMBuildDialogController::initSaveController() {
SaveDocumentControllerConfig config;
config.defaultDomain = HMMIO::HMM_ID;
config.defaultFormatId = HMMIO::HMM_ID;
config.fileDialogButton = resultFileButton;
config.fileNameEdit = resultFileEdit;
config.parentWidget = this;
config.saveTitle = tr("Select file with HMM profile");
SaveDocumentController::SimpleFormatsInfo formats;
formats.addFormat(HMMIO::HMM_ID, tr("HMM models"), QStringList(HMMIO::HMM_EXT));
saveController = new SaveDocumentController(config, formats, this);
}
//////////////////////////////////////////////////////////////////////////
// HMMBuildTask
HMMBuildToFileTask::HMMBuildToFileTask(const QString& inFile, const QString& _outFile, const UHMMBuildSettings& s)
: Task("", TaskFlag_ReportingIsSupported),
settings(s),
outFile(_outFile),
loadTask(nullptr),
buildTask(nullptr) {
setTaskName(tr("Build HMM profile '%1' -> '%2'").arg(QFileInfo(inFile).fileName()).arg(QFileInfo(outFile).fileName()));
setVerboseLogMode(true);
assert(!(inFile.isEmpty() || outFile.isEmpty()));
// todo: support all formats with alignments!
DocumentFormatConstraints c;
c.checkRawData = true;
c.supportedObjectTypes += GObjectTypes::MULTIPLE_SEQUENCE_ALIGNMENT;
c.rawData = IOAdapterUtils::readFileHeader(inFile);
c.addFlagToExclude(DocumentFormatFlag_CannotBeCreated);
QList<DocumentFormatId> formats = AppContext::getDocumentFormatRegistry()->selectFormats(c);
if (formats.isEmpty()) {
stateInfo.setError(tr("Error reading alignment file"));
return;
}
DocumentFormatId alnFormat = formats.first();
loadTask = new LoadDocumentTask(alnFormat, inFile, AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById(IOAdapterUtils::url2io(inFile)), QVariantMap());
addSubTask(loadTask);
}
HMMBuildToFileTask::HMMBuildToFileTask(const Msa& _ma, const QString& _outFile, const UHMMBuildSettings& s)
: Task("", TaskFlags_FOSCOE | TaskFlag_ReportingIsSupported),
settings(s), outFile(_outFile), ma(_ma->getCopy()), loadTask(nullptr), buildTask(nullptr) {
setTaskName(tr("Build HMM profile to '%1'").arg(QFileInfo(outFile).fileName()));
setVerboseLogMode(true);
assert(!(outFile.isEmpty() || ma->isEmpty()));
if (settings.name.isEmpty()) {
QFileInfo fi(outFile);
settings.name = fi.baseName(); // FIXME temporary workaround
}
buildTask = new HMMBuildTask(settings, ma);
addSubTask(buildTask);
}
QList<Task*> HMMBuildToFileTask::onSubTaskFinished(Task* subTask) {
QList<Task*> res;
if (hasError() || isCanceled()) {
return res;
}
if (subTask == loadTask) {
assert(ma->isEmpty());
Document* doc = loadTask->getDocument();
// assert(doc);
if (!doc) {
stateInfo.setError(tr("Incorrect input file"));
return res;
}
QList<GObject*> list = doc->findGObjectByType(GObjectTypes::MULTIPLE_SEQUENCE_ALIGNMENT);
if (list.isEmpty()) {
stateInfo.setError(tr("Alignment object not found!"));
} else {
auto msa = qobject_cast<MsaObject*>(list.first());
const Msa ma = msa->getAlignment();
if (settings.name.isEmpty()) {
settings.name = msa->getGObjectName() == MA_OBJECT_NAME ? doc->getName() : msa->getGObjectName();
}
buildTask = new HMMBuildTask(settings, ma);
res.append(buildTask);
}
}
return res;
}
void HMMBuildToFileTask::run() {
TaskLocalData::createHMMContext(getTaskId(), true);
_run();
TaskLocalData::freeHMMContext(getTaskId());
}
void HMMBuildToFileTask::_run() {
if (stateInfo.hasError()) {
return;
}
assert(buildTask != nullptr);
assert(buildTask->getState() == Task::State_Finished);
if (buildTask->getStateInfo().hasError()) {
stateInfo.setError(buildTask->getStateInfo().getError());
return;
}
plan7_s* hmm = buildTask->getHMM();
assert(hmm != nullptr);
IOAdapterFactory* iof;
iof = AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById(IOAdapterUtils::url2io(outFile));
// todo: write command line too!
HMMIO::writeHMM2(iof, outFile, stateInfo, hmm);
}
QString HMMBuildToFileTask::generateReport() const {
QString res;
res += "<table>";
res += "<tr><td width=200><b>" + tr("Source alignment") + "</b></td><td>" + (loadTask == nullptr ? settings.name : loadTask->getURL().getURLString()) + "</td></tr>";
res += "<tr><td><b>" + tr("Profile name") + "</b></td><td>" + settings.name + "</td></tr>";
if (hasError()) {
res += "<tr><td width=200><b>" + tr("Task was not finished") + "</b></td><td></td></tr>";
res += "</table>";
return res;
}
res += "<tr><td><b>" + tr("Profile file") + "</b></td><td>" + outFile + "</td></tr>";
res += "<tr><td><b>" + tr("Expert options") + "</b></td><td>";
switch (settings.strategy) {
case P7_BASE_CONFIG:
res += "-g";
break;
case P7_LS_CONFIG:
res += tr("none");
break;
case P7_FS_CONFIG:
res += "-f";
break;
case P7_SW_CONFIG:
res += "-s";
break;
}
res += "</td></tr>";
res += "</table>";
return res;
}
HMMBuildTask::HMMBuildTask(const UHMMBuildSettings& s, const Msa& _ma)
: Task("", TaskFlag_None), ma(_ma->getCopy()), settings(s), hmm(nullptr) {
GCOUNTER(cvar, "HMMBuildTask");
setTaskName(tr("Build HMM profile '%1'").arg(s.name));
}
HMMBuildTask::~HMMBuildTask() {
if (hmm != nullptr) {
FreePlan7(hmm);
}
}
void HMMBuildTask::run() {
TaskLocalData::createHMMContext(getTaskId(), true);
_run();
TaskLocalData::freeHMMContext(getTaskId());
}
void HMMBuildTask::_run() {
if (ma->getRowCount() == 0) {
stateInfo.setError(tr("Multiple alignment is empty"));
return;
}
if (ma->getLength() == 0) {
stateInfo.setError(tr("Multiple alignment is of 0 length"));
return;
}
// todo: check HMM for RAW alphabet!
if (ma->getAlphabet()->isRaw()) {
stateInfo.setError(tr("Invalid alphabet! Only amino and nucleic alphabets are supported"));
return;
}
// everything ok here: fill msa
msa_struct* msa = MSAAlloc(ma->getRowCount(), ma->getLength());
if (msa == nullptr) {
stateInfo.setError(tr("Error creating MSA structure"));
return;
}
U2OpStatus2Log os;
for (int i = 0; i < ma->getRowCount(); i++) {
const MsaRow& row = ma->getRow(i);
QByteArray seq = row->toByteArray(os, ma->getLength());
free(msa->aseq[i]);
msa->aseq[i] = sre_strdup(seq.constData(), seq.size());
QByteArray name = row->getName().toLatin1();
msa->sqname[i] = sre_strdup(name.constData(), name.size());
msa->wgt[i] = 1.0; // default weights
}
int atype = ma->getAlphabet()->isNucleic() ? hmmNUCLEIC : hmmAMINO;
try {
hmm = UHMMBuild::build(msa, atype, settings, stateInfo);
} catch (const HMMException& e) {
stateInfo.setError(e.error);
}
assert(hmm != nullptr || stateInfo.hasError());
MSAFree(msa);
}
} // namespace U2