1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #include "ClustalOWorker.h"
23 
24 #include <U2Core/AppContext.h>
25 #include <U2Core/AppResources.h>
26 #include <U2Core/AppSettings.h>
27 #include <U2Core/Log.h>
28 #include <U2Core/UserApplicationsSettings.h>
29 
30 #include <U2Designer/DelegateEditors.h>
31 
32 #include <U2Lang/ActorPrototypeRegistry.h>
33 #include <U2Lang/BaseActorCategories.h>
34 #include <U2Lang/BasePorts.h>
35 #include <U2Lang/BaseSlots.h>
36 #include <U2Lang/BaseTypes.h>
37 #include <U2Lang/IntegralBusModel.h>
38 #include <U2Lang/NoFailTaskWrapper.h>
39 #include <U2Lang/WorkflowEnv.h>
40 
41 #include "ClustalOSupport.h"
42 #include "TaskLocalStorage.h"
43 
44 namespace U2 {
45 namespace LocalWorkflow {
46 
47 /****************************
48  * ClustalOWorkerFactory
49  ****************************/
50 const QString ClustalOWorkerFactory::ACTOR_ID("ClustalO");
51 
52 static const QString NUM_ITERATIONS("num-iterations");
53 static const QString MAX_GT_ITERATIONS("max-guidetree-iterations");
54 static const QString MAX_HMM_ITERATIONS("max-hmm-iterations");
55 static const QString SET_AUTO("set-auto");
56 static const QString EXT_TOOL_PATH("path");
57 static const QString TMP_DIR_PATH("temp-dir");
58 
init()59 void ClustalOWorkerFactory::init() {
60     QList<PortDescriptor *> p;
61     QList<Attribute *> a;
62     Descriptor ind(BasePorts::IN_MSA_PORT_ID(), ClustalOWorker::tr("Input MSA"), ClustalOWorker::tr("Input MSA to process."));
63     Descriptor oud(BasePorts::OUT_MSA_PORT_ID(), ClustalOWorker::tr("ClustalO result MSA"), ClustalOWorker::tr("The result of the ClustalO alignment."));
64 
65     QMap<Descriptor, DataTypePtr> inM;
66     inM[BaseSlots::MULTIPLE_ALIGNMENT_SLOT()] = BaseTypes::MULTIPLE_ALIGNMENT_TYPE();
67     p << new PortDescriptor(ind, DataTypePtr(new MapDataType("clustal.in.msa", inM)), true /*input*/);
68     QMap<Descriptor, DataTypePtr> outM;
69     outM[BaseSlots::MULTIPLE_ALIGNMENT_SLOT()] = BaseTypes::MULTIPLE_ALIGNMENT_TYPE();
70     p << new PortDescriptor(oud, DataTypePtr(new MapDataType("clustal.out.msa", outM)), false /*input*/, true /*multi*/);
71 
72     Descriptor ni(NUM_ITERATIONS, ClustalOWorker::tr("Number of iterations"), ClustalOWorker::tr("Number of (combined guide-tree/HMM) iterations."));
73     Descriptor ngti(MAX_GT_ITERATIONS, ClustalOWorker::tr("Number of guidetree iterations"), ClustalOWorker::tr("Maximum number guidetree iterations."));
74     Descriptor nhmmi(MAX_HMM_ITERATIONS, ClustalOWorker::tr("Number of HMM iterations"), ClustalOWorker::tr("Maximum number of HMM iterations."));
75     Descriptor sa(SET_AUTO, ClustalOWorker::tr("Set auto options"), ClustalOWorker::tr("Set options automatically (might overwrite some of your options)."));
76 
77     Descriptor etp(EXT_TOOL_PATH, ClustalOWorker::tr("Tool path"), ClustalOWorker::tr("Path to the ClustalO tool."
78                                                                                       "<p>The default path can be set in the UGENE application settings."));
79 
80     Descriptor tdp(TMP_DIR_PATH, ClustalOWorker::tr("Temporary folder"), ClustalOWorker::tr("Folder to store temporary files."));
81 
82     a << new Attribute(ni, BaseTypes::NUM_TYPE(), false, QVariant(1));
83     a << new Attribute(ngti, BaseTypes::NUM_TYPE(), false, QVariant(0));
84     a << new Attribute(nhmmi, BaseTypes::NUM_TYPE(), false, QVariant(0));
85     a << new Attribute(sa, BaseTypes::BOOL_TYPE(), false, QVariant(false));
86 
87     a << new Attribute(etp, BaseTypes::STRING_TYPE(), true, QVariant("Default"));
88     a << new Attribute(tdp, BaseTypes::STRING_TYPE(), true, QVariant("Default"));
89 
90     Descriptor desc(ACTOR_ID, ClustalOWorker::tr("Align with ClustalO"), ClustalOWorker::tr("Aligns multiple sequence alignments (MSAs) supplied with ClustalO."
91                                                                                             "<p>ClustalO is a general purpose multiple sequence alignment program for proteins."
92                                                                                             "Visit <a href=\"http://www.clustal.org/omega\">http://www.clustal.org/omega</a> to learn more about it."));
93 
94     ActorPrototype *proto = new IntegralBusActorPrototype(desc, p, a);
95 
96     QMap<QString, PropertyDelegate *> delegates;
97 
98     {
99         QVariantMap m;
100         m["minimum"] = int(1);
101         m["maximum"] = int(1000);
102         delegates[NUM_ITERATIONS] = new SpinBoxDelegate(m);
103     }
104     {
105         QVariantMap m;
106         m["minimum"] = int(0);
107         m["maximum"] = int(1000);
108         delegates[MAX_GT_ITERATIONS] = new SpinBoxDelegate(m);
109     }
110     {
111         QVariantMap m;
112         m["minimum"] = int(0);
113         m["maximum"] = int(1000);
114         delegates[MAX_HMM_ITERATIONS] = new SpinBoxDelegate(m);
115     }
116 
117     delegates[EXT_TOOL_PATH] = new URLDelegate("", "executable", false, false, false);
118     delegates[TMP_DIR_PATH] = new URLDelegate("", "TmpDir", false, true);
119 
120     proto->setEditor(new DelegateEditor(delegates));
121     proto->setPrompter(new ClustalOPrompter());
122     proto->setIconPath(":external_tool_support/images/clustalo.png");
123     proto->addExternalTool(ClustalOSupport::ET_CLUSTALO_ID, EXT_TOOL_PATH);
124     WorkflowEnv::getProtoRegistry()->registerProto(BaseActorCategories::CATEGORY_ALIGNMENT(), proto);
125 
126     DomainFactory *localDomain = WorkflowEnv::getDomainRegistry()->getById(LocalDomainFactory::ID);
127     localDomain->registerEntry(new ClustalOWorkerFactory());
128 }
129 
130 /****************************
131 * ClustalOPrompter
132 ****************************/
ClustalOPrompter(Actor * p)133 ClustalOPrompter::ClustalOPrompter(Actor *p)
134     : PrompterBase<ClustalOPrompter>(p) {
135 }
composeRichDoc()136 QString ClustalOPrompter::composeRichDoc() {
137     IntegralBusPort *input = qobject_cast<IntegralBusPort *>(target->getPort(BasePorts::IN_MSA_PORT_ID()));
138     Actor *producer = input->getProducer(BasePorts::IN_MSA_PORT_ID());
139     QString producerName = producer ? tr(" from %1").arg(producer->getLabel()) : "";
140     QString doc = tr("Aligns each MSA supplied <u>%1</u> with \"<u>ClustalO</u>\".")
141                       .arg(producerName);
142 
143     return doc;
144 }
145 /****************************
146 * ClustalOWorker
147 ****************************/
ClustalOWorker(Actor * a)148 ClustalOWorker::ClustalOWorker(Actor *a)
149     : BaseWorker(a), input(nullptr), output(nullptr) {
150 }
151 
init()152 void ClustalOWorker::init() {
153     input = ports.value(BasePorts::IN_MSA_PORT_ID());
154     output = ports.value(BasePorts::OUT_MSA_PORT_ID());
155 }
156 
tick()157 Task *ClustalOWorker::tick() {
158     if (input->hasMessage()) {
159         Message inputMessage = getMessageAndSetupScriptValues(input);
160         if (inputMessage.isEmpty()) {
161             output->transit();
162             return nullptr;
163         }
164         cfg.numIterations = actor->getParameter(NUM_ITERATIONS)->getAttributeValue<int>(context);
165         cfg.maxGuideTreeIterations = actor->getParameter(MAX_GT_ITERATIONS)->getAttributeValue<int>(context);
166         cfg.maxHMMIterations = actor->getParameter(MAX_HMM_ITERATIONS)->getAttributeValue<int>(context);
167         cfg.setAutoOptions = actor->getParameter(SET_AUTO)->getAttributeValue<bool>(context);
168         cfg.numberOfProcessors = AppContext::getAppSettings()->getAppResourcePool()->getIdealThreadCount();
169 
170         QString path = actor->getParameter(EXT_TOOL_PATH)->getAttributeValue<QString>(context);
171         if (QString::compare(path, "default", Qt::CaseInsensitive) != 0) {
172             AppContext::getExternalToolRegistry()->getById(ClustalOSupport::ET_CLUSTALO_ID)->setPath(path);
173         }
174         path = actor->getParameter(TMP_DIR_PATH)->getAttributeValue<QString>(context);
175         if (QString::compare(path, "default", Qt::CaseInsensitive) != 0) {
176             AppContext::getAppSettings()->getUserAppsSettings()->setUserTemporaryDirPath(path);
177         }
178         QVariantMap qm = inputMessage.getData().toMap();
179         SharedDbiDataHandler msaId = qm.value(BaseSlots::MULTIPLE_ALIGNMENT_SLOT().getId()).value<SharedDbiDataHandler>();
180         QScopedPointer<MultipleSequenceAlignmentObject> msaObj(StorageUtils::getMsaObject(context->getDataStorage(), msaId));
181         SAFE_POINT(!msaObj.isNull(), "NULL MSA Object!", nullptr);
182         const MultipleSequenceAlignment msa = msaObj->getMultipleAlignment();
183 
184         if (msa->isEmpty()) {
185             algoLog.error(tr("An empty MSA '%1' has been supplied to ClustalO.").arg(msa->getName()));
186             return nullptr;
187         }
188         ClustalOSupportTask *supportTask = new ClustalOSupportTask(msa, GObjectReference(), cfg);
189         supportTask->addListeners(createLogListeners());
190         Task *t = new NoFailTaskWrapper(supportTask);
191         connect(t, SIGNAL(si_stateChanged()), SLOT(sl_taskFinished()));
192         return t;
193     } else if (input->isEnded()) {
194         setDone();
195         output->setEnded();
196     }
197     return nullptr;
198 }
199 
sl_taskFinished()200 void ClustalOWorker::sl_taskFinished() {
201     auto wrapper = qobject_cast<NoFailTaskWrapper *>(sender());
202     CHECK(wrapper->isFinished(), );
203     auto clustalOTask = qobject_cast<ClustalOSupportTask *>(wrapper->originalTask());
204     if (clustalOTask->isCanceled()) {
205         return;
206     }
207     if (clustalOTask->hasError()) {
208         coreLog.error(clustalOTask->getError());
209         return;
210     }
211 
212     SAFE_POINT(output != nullptr, "NULL output!", );
213     SharedDbiDataHandler msaId = context->getDataStorage()->putAlignment(clustalOTask->getResultAlignment());
214     QVariantMap msgData;
215     msgData[BaseSlots::MULTIPLE_ALIGNMENT_SLOT().getId()] = qVariantFromValue<SharedDbiDataHandler>(msaId);
216     output->put(Message(BaseTypes::MULTIPLE_ALIGNMENT_TYPE(), msgData));
217     algoLog.info(tr("Aligned %1 with ClustalO").arg(clustalOTask->getResultAlignment()->getName()));
218 }
219 
cleanup()220 void ClustalOWorker::cleanup() {
221 }
222 
ClustalOWorkerFactory()223 ClustalOWorkerFactory::ClustalOWorkerFactory()
224     : DomainFactory(ACTOR_ID) {
225 }
226 
createWorker(Actor * actor)227 Worker *ClustalOWorkerFactory::createWorker(Actor *actor) {
228     return new ClustalOWorker(actor);
229 }
230 
231 }    //namespace LocalWorkflow
232 }    //namespace U2
233