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 "SWAlgorithmPlugin.h"
23 
24 #include <U2Algorithm/AlignmentAlgorithmsRegistry.h>
25 #include <U2Algorithm/CudaGpuRegistry.h>
26 #include <U2Algorithm/OpenCLGpuRegistry.h>
27 #include <U2Algorithm/SmithWatermanTaskFactoryRegistry.h>
28 #include <U2Algorithm/SubstMatrixRegistry.h>
29 
30 #include <U2Core/AppContext.h>
31 #include <U2Core/AppResources.h>
32 #include <U2Core/DNASequence.h>
33 #include <U2Core/GAutoDeleteList.h>
34 #include <U2Core/Log.h>
35 #include <U2Core/MultipleSequenceAlignment.h>
36 #include <U2Core/MultipleSequenceAlignmentObject.h>
37 #include <U2Core/U2SafePoints.h>
38 
39 #include <U2Gui/GUIUtils.h>
40 #include <U2Gui/OptionsPanel.h>
41 
42 #include <U2Lang/QueryDesignerRegistry.h>
43 
44 #include <U2Test/GTest.h>
45 #include <U2Test/GTestFrameworkComponents.h>
46 #include <U2Test/XMLTestFormat.h>
47 
48 #include <U2View/ADVConstants.h>
49 #include <U2View/ADVSequenceObjectContext.h>
50 #include <U2View/ADVUtils.h>
51 #include <U2View/AnnotatedDNAView.h>
52 #include <U2View/MSAEditor.h>
53 
54 #include "PairwiseAlignmentSmithWatermanGUIExtension.h"
55 #include "SWAlgorithmTask.h"
56 #include "SWQuery.h"
57 #include "SWTaskFactory.h"
58 #include "SWWorker.h"
59 #include "SmithWatermanTests.h"
60 
61 namespace U2 {
62 
U2_PLUGIN_INIT_FUNC()63 extern "C" Q_DECL_EXPORT Plugin *U2_PLUGIN_INIT_FUNC() {
64     return new SWAlgorithmPlugin();
65 }
66 
SWAlgorithmPlugin()67 SWAlgorithmPlugin::SWAlgorithmPlugin()
68     : Plugin(tr("Optimized Smith-Waterman "), tr("Various implementations of Smith-Waterman algorithm")) {
69     // initializing ADV context
70     if (AppContext::getMainWindow()) {
71         ctxADV = new SWAlgorithmADVContext(this);
72         ctxADV->init();
73     }
74 
75     LocalWorkflow::SWWorkerFactory::init();
76 
77     QDActorPrototypeRegistry *qdpr = AppContext::getQDActorProtoRegistry();
78     qdpr->registerProto(new SWQDActorFactory());
79 
80     // Smith-Waterman algorithm tests
81     GTestFormatRegistry *tfr = AppContext::getTestFramework()->getTestFormatRegistry();
82     XMLTestFormat *xmlTestFormat = qobject_cast<XMLTestFormat *>(tfr->findFormat("XML"));
83     assert(xmlTestFormat != nullptr);
84 
85     U2::GAutoDeleteList<U2::XMLTestFactory> *l = new U2::GAutoDeleteList<U2::XMLTestFactory>(this);
86     l->qlist = SWAlgorithmTests::createTestFactories();
87 
88     foreach (XMLTestFactory *f, l->qlist) {
89         bool res = xmlTestFormat->registerTestFactory(f);
90         Q_UNUSED(res);
91         assert(res);
92     }
93 
94     AlignmentAlgorithmsRegistry *par = AppContext::getAlignmentAlgorithmsRegistry();
95     SmithWatermanTaskFactoryRegistry *swar = AppContext::getSmithWatermanTaskFactoryRegistry();
96 
97     coreLog.trace("Registering classic SW implementation");
98     swar->registerFactory(new SWTaskFactory(SW_classic), QString("Classic 2"));  // ADV search register
99     par->registerAlgorithm(new SWPairwiseAlignmentAlgorithm());
100     regDependedIMPLFromOtherPlugins();
101 
102     coreLog.trace("Registering SSE2 SW implementation");
103     swar->registerFactory(new SWTaskFactory(SW_sse2), QString("SSE2"));
104     par->getAlgorithm("Smith-Waterman")->addAlgorithmRealization(new PairwiseAlignmentSmithWatermanTaskFactory(SW_sse2), new PairwiseAlignmentSmithWatermanGUIExtensionFactory(SW_sse2), "SSE2");
105 
106     this->connect(AppContext::getPluginSupport(), SIGNAL(si_allStartUpPluginsLoaded()), SLOT(regDependedIMPLFromOtherPlugins()));
107 }
108 
createTestFactories()109 QList<XMLTestFactory *> SWAlgorithmTests::createTestFactories() {
110     QList<XMLTestFactory *> res;
111     res.append(GTest_SmithWatermnan::createFactory());
112     res.append(GTest_SmithWatermnanPerf::createFactory());
113     return res;
114 }
115 
116 // SLOT
regDependedIMPLFromOtherPlugins()117 void SWAlgorithmPlugin::regDependedIMPLFromOtherPlugins() {
118     SmithWatermanTaskFactoryRegistry *swar = AppContext::getSmithWatermanTaskFactoryRegistry();
119     AlignmentAlgorithmsRegistry *par = AppContext::getAlignmentAlgorithmsRegistry();
120     Q_UNUSED(swar);
121     Q_UNUSED(par);
122 
123 #ifdef SW2_BUILD_WITH_CUDA
124     if (!AppContext::getCudaGpuRegistry()->empty()) {
125         coreLog.trace("Registering CUDA SW implementation");
126         swar->registerFactory(new SWTaskFactory(SW_cuda), QString("CUDA"));
127         par->getAlgorithm("Smith-Waterman")->addAlgorithmRealization(new PairwiseAlignmentSmithWatermanTaskFactory(SW_cuda), new PairwiseAlignmentSmithWatermanGUIExtensionFactory(SW_cuda), "CUDA");
128     }
129 #endif
130 
131 #ifdef SW2_BUILD_WITH_OPENCL
132     if (!AppContext::getOpenCLGpuRegistry()->empty()) {
133         coreLog.trace("Registering OpenCL SW implementation");
134         swar->registerFactory(new SWTaskFactory(SW_opencl), QString("OPENCL"));
135         par->getAlgorithm("Smith-Waterman")->addAlgorithmRealization(new PairwiseAlignmentSmithWatermanTaskFactory(SW_opencl), new PairwiseAlignmentSmithWatermanGUIExtensionFactory(SW_opencl), "OPENCL");
136     }
137 #endif
138 }
139 
SWAlgorithmADVContext(QObject * p)140 SWAlgorithmADVContext::SWAlgorithmADVContext(QObject *p)
141     : GObjectViewWindowContext(p, ANNOTATED_DNA_VIEW_FACTORY_ID), dialogConfig() {
142 }
143 
initViewContext(GObjectView * view)144 void SWAlgorithmADVContext::initViewContext(GObjectView *view) {
145     AnnotatedDNAView *av = qobject_cast<AnnotatedDNAView *>(view);
146     assert(av != nullptr);
147     ADVGlobalAction *a = new ADVGlobalAction(av, QIcon(":core/images/sw.png"), tr("Find pattern [Smith-Waterman]..."), 15);
148     a->setObjectName("find_pattern_smith_waterman_action");
149 
150     a->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_F));
151     a->setShortcutContext(Qt::WindowShortcut);
152     av->getWidget()->addAction(a);
153 
154     connect(a, SIGNAL(triggered()), SLOT(sl_search()));
155 }
156 
sl_search()157 void SWAlgorithmADVContext::sl_search() {
158     GObjectViewAction *action = qobject_cast<GObjectViewAction *>(sender());
159     assert(0 != action);
160 
161     AnnotatedDNAView *av = qobject_cast<AnnotatedDNAView *>(action->getObjectView());
162     assert(av != nullptr);
163 
164     ADVSequenceObjectContext *seqCtx = av->getActiveSequenceContext();
165     SmithWatermanDialogController::run(av->getWidget(), seqCtx, &dialogConfig);
166 }
167 
SWPairwiseAlignmentAlgorithm()168 SWPairwiseAlignmentAlgorithm::SWPairwiseAlignmentAlgorithm()
169     : AlignmentAlgorithm(PairwiseAlignment,
170                          "Smith-Waterman",
171                          AlignmentAlgorithmsRegistry::tr("Smith-Waterman"),
172                          new PairwiseAlignmentSmithWatermanTaskFactory(SW_classic),
173                          new PairwiseAlignmentSmithWatermanGUIExtensionFactory(SW_classic),
174                          "SW_classic") {
175 }
176 
checkAlphabet(const DNAAlphabet * alphabet) const177 bool SWPairwiseAlignmentAlgorithm::checkAlphabet(const DNAAlphabet *alphabet) const {
178     SAFE_POINT(nullptr != alphabet, "Alphabet is NULL.", false);
179     SubstMatrixRegistry *matrixReg = AppContext::getSubstMatrixRegistry();
180     SAFE_POINT(matrixReg, "SubstMatrixRegistry is NULL.", false);
181     QStringList matrixList = matrixReg->selectMatrixNamesByAlphabet(alphabet);
182     return !matrixList.isEmpty();
183 }
184 
185 }  // namespace U2
186