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 <AppContextImpl.h>
23 #include <AppSettingsImpl.h>
24 #include <ConsoleLogDriver.h>
25 #include <DocumentFormatRegistryImpl.h>
26 #include <IOAdapterRegistryImpl.h>
27 #include <PluginSupportImpl.h>
28 #include <ServiceRegistryImpl.h>
29 #include <SettingsImpl.h>
30 #include <TaskSchedulerImpl.h>
31 #include <crash_handler/CrashHandler.h>
32 
33 #include <U2Algorithm/AlignmentAlgorithmsRegistry.h>
34 #include <U2Algorithm/AssemblyConsensusAlgorithmRegistry.h>
35 #include <U2Algorithm/CDSearchTaskFactoryRegistry.h>
36 #include <U2Algorithm/CudaGpuRegistry.h>
37 #include <U2Algorithm/DnaAssemblyAlgRegistry.h>
38 #include <U2Algorithm/GenomeAssemblyRegistry.h>
39 #include <U2Algorithm/MSAConsensusAlgorithmRegistry.h>
40 #include <U2Algorithm/MSADistanceAlgorithmRegistry.h>
41 #include <U2Algorithm/MolecularSurfaceFactoryRegistry.h>
42 
43 #ifdef OPENCL_SUPPORT
44 #    include <U2Algorithm/OpenCLGpuRegistry.h>
45 #endif
46 
47 #include <U2Algorithm/PWMConversionAlgorithmRegistry.h>
48 #include <U2Algorithm/PhyTreeGeneratorRegistry.h>
49 #include <U2Algorithm/RepeatFinderTaskFactoryRegistry.h>
50 #include <U2Algorithm/SWMulAlignResultNamesTagsRegistry.h>
51 #include <U2Algorithm/SWResultFilterRegistry.h>
52 #include <U2Algorithm/SecStructPredictAlgRegistry.h>
53 #include <U2Algorithm/SmithWatermanTaskFactoryRegistry.h>
54 #include <U2Algorithm/SplicedAlignmentTaskRegistry.h>
55 #include <U2Algorithm/StructuralAlignmentAlgorithmRegistry.h>
56 #include <U2Algorithm/SubstMatrixRegistry.h>
57 
58 #include <U2Core/AnnotationSettings.h>
59 #include <U2Core/AppFileStorage.h>
60 #include <U2Core/AutoAnnotationsSupport.h>
61 #include <U2Core/BundleInfo.h>
62 #include <U2Core/CMDLineCoreOptions.h>
63 #include <U2Core/CMDLineRegistry.h>
64 #include <U2Core/CMDLineUtils.h>
65 #include <U2Core/ConsoleShutdownTask.h>
66 #include <U2Core/DBXRefRegistry.h>
67 #include <U2Core/DNAAlphabetRegistryImpl.h>
68 #include <U2Core/DNATranslation.h>
69 #include <U2Core/DataBaseRegistry.h>
70 #include <U2Core/DataPathRegistry.h>
71 #include <U2Core/ExternalToolRegistry.h>
72 #include <U2Core/LoadRemoteDocumentTask.h>
73 #include <U2Core/Log.h>
74 #include <U2Core/ResourceTracker.h>
75 #include <U2Core/ScriptingToolRegistry.h>
76 #include <U2Core/TaskStarter.h>
77 #include <U2Core/Timer.h>
78 #include <U2Core/U2DbiRegistry.h>
79 #include <U2Core/U2OpStatusUtils.h>
80 #include <U2Core/UserApplicationsSettings.h>
81 #include <U2Core/Version.h>
82 #include <U2Core/VirtualFileSystem.h>
83 
84 #include <U2Formats/DocumentFormatUtils.h>
85 
86 #include <U2Lang/LocalDomain.h>
87 #include <U2Lang/QueryDesignerRegistry.h>
88 #include <U2Lang/WorkflowEnvImpl.h>
89 
90 #include <U2Test/GTestFrameworkComponents.h>
91 
92 #include "UgeneContextWrapper.h"
93 
94 int ARGC = 0;
95 
96 namespace U2 {
97 
setDataSearchPaths()98 static void setDataSearchPaths() {
99     // set search paths for data files
100     QStringList dataSearchPaths;
101     const char *relativeDataDir = "/data";
102     const char *relativeDevDataDir = "/../../data";
103 
104     if (QDir(AppContext::getWorkingDirectoryPath() + relativeDataDir).exists()) {
105         dataSearchPaths.push_back(AppContext::getWorkingDirectoryPath() + relativeDataDir);
106     } else if (QDir(AppContext::getWorkingDirectoryPath() + relativeDevDataDir).exists()) {
107         coreLog.info(AppContextImpl::tr("Added path: %1").arg(AppContext::getWorkingDirectoryPath() + relativeDevDataDir));
108         dataSearchPaths.push_back(AppContext::getWorkingDirectoryPath() + relativeDevDataDir);
109 #ifdef Q_OS_DARWIN
110     } else {
111         QString dir = BundleInfo::getDataSearchPath();
112         if (!dir.isEmpty()) {
113             dataSearchPaths.push_back(dir);
114         }
115 #endif
116     }
117 
118 #if (defined(Q_OS_UNIX)) && defined(UGENE_DATA_DIR)
119     // using folder which is set during installation process on Linux
120     const QString ugene_data_dir(UGENE_DATA_DIR);
121     if (QDir(ugene_data_dir).exists()) {
122         dataSearchPaths.push_back(QString(UGENE_DATA_DIR));
123     }
124 #endif
125 
126     if (dataSearchPaths.empty()) {
127         dataSearchPaths.push_back("/");
128     }
129 
130     QDir::setSearchPaths(PATH_PREFIX_DATA, dataSearchPaths);
131     // now data files may be opened using QFile( "data:some_data_file" )
132 }
133 
setSearchPaths()134 static void setSearchPaths() {
135     setDataSearchPaths();
136 }
137 
UgeneContextWrapper(const QString & workingDirectoryPath)138 UgeneContextWrapper::UgeneContextWrapper(const QString &workingDirectoryPath)
139     : app(ARGC, nullptr) {
140     if (!Version::checkBuildAndRuntimeVersions()) {
141         return;
142     }
143     GTIMER(c1, t1, "initialization of UGENE Context");
144 
145     appContext = AppContextImpl::getApplicationContext();
146     appContext->setWorkingDirectoryPath(workingDirectoryPath);
147     QCoreApplication::addLibraryPath(workingDirectoryPath);
148 
149     setSearchPaths();
150     cmdLineRegistry = new CMDLineRegistry(QStringList());
151     appContext->setCMDLineRegistry(cmdLineRegistry);
152 
153     globalSettings = new SettingsImpl(QSettings::SystemScope);
154     appContext->setGlobalSettings(globalSettings);
155 
156     settings = new SettingsImpl(QSettings::UserScope);
157     appContext->setSettings(settings);
158 
159     appSettings = new AppSettingsImpl();
160     appContext->setAppSettings(appSettings);
161 
162     userAppSettings = AppContext::getAppSettings()->getUserAppsSettings();
163 
164     ConsoleLogDriver logs;
165     coreLog.details(AppContextImpl::tr("UGENE script environment initialization started"));
166     coreLog.trace(QString("UGENE script environment run at dir %1").arg(workingDirectoryPath));
167 
168     resTrack = new ResourceTracker();
169     appContext->setResourceTracker(resTrack);
170 
171     ts = new TaskSchedulerImpl(appSettings->getAppResourcePool());
172     appContext->setTaskScheduler(ts);
173 
174     asr = new AnnotationSettingsRegistry(DocumentFormatUtils::predefinedSettings());
175     appContext->setAnnotationSettingsRegistry(asr);
176 
177     tf = new TestFramework();
178     appContext->setTestFramework(tf);
179 
180     rfr = new RepeatFinderTaskFactoryRegistry();
181     appContext->setRepeatFinderTaskFactoryRegistry(rfr);
182 
183     str = new ScriptingToolRegistry();
184     appContext->setScriptingToolRegistry(str);
185 
186     qpr = new QDActorPrototypeRegistry();
187     appContext->setQDActorFactoryRegistry(qpr);
188 
189     phyreg = new PhyTreeGeneratorRegistry();
190     appContext->setPhyTreeGeneratorRegistry(phyreg);
191 
192     etr = new ExternalToolRegistry();
193     appContext->setExternalToolRegistry(etr);
194 
195     dbiRegistry = new U2DbiRegistry();
196     appContext->setDbiRegistry(dbiRegistry);
197 
198     dfr = new DocumentFormatRegistryImpl();
199     appContext->setDocumentFormatRegistry(dfr);
200 
201     psp = new PluginSupportImpl();
202     appContext->setPluginSupport(psp);
203 
204     sreg = new ServiceRegistryImpl();
205     appContext->setServiceRegistry(sreg);
206 
207     io = new IOAdapterRegistryImpl();
208     appContext->setIOAdapterRegistry(io);
209 
210     dtr = new DNATranslationRegistry();
211     appContext->setDNATranslationRegistry(dtr);
212 
213     dal = new DNAAlphabetRegistryImpl(dtr);
214     appContext->setDNAAlphabetRegistry(dal);
215 
216     dbxrr = new DBXRefRegistry();
217     appContext->setDBXRefRegistry(dbxrr);
218 
219     msaConsReg = new MSAConsensusAlgorithmRegistry();
220     appContext->setMSAConsensusAlgorithmRegistry(msaConsReg);
221 
222     msaDistReg = new MSADistanceAlgorithmRegistry();
223     appContext->setMSADistanceAlgorithmRegistry(msaDistReg);
224 
225     assemblyConsReg = new AssemblyConsensusAlgorithmRegistry();
226     appContext->setAssemblyConsensusAlgorithmRegistry(assemblyConsReg);
227 
228     pwmConvReg = new PWMConversionAlgorithmRegistry();
229     appContext->setPWMConversionAlgorithmRegistry(pwmConvReg);
230 
231     smr = new SubstMatrixRegistry();
232     appContext->setSubstMatrixRegistry(smr);
233 
234     swar = new SmithWatermanTaskFactoryRegistry();
235     appContext->setSmithWatermanTaskFactoryRegistry(swar);
236 
237     msfr = new MolecularSurfaceFactoryRegistry();
238     appContext->setMolecularSurfaceFactoryRegistry(msfr);
239 
240     swrfr = new SWResultFilterRegistry();
241     appContext->setSWResultFilterRegistry(swrfr);
242 
243     sspar = new SecStructPredictAlgRegistry();
244     appContext->setSecStructPedictAlgRegistry(sspar);
245 
246     cgr = new CudaGpuRegistry();
247     appContext->setCudaGpuRegistry(cgr);
248 
249     alignmentAlgorithmRegistry = new AlignmentAlgorithmsRegistry();
250     appContext->setAlignmentAlgorithmsRegistry(alignmentAlgorithmRegistry);
251 
252 #ifdef OPENCL_SUPPORT
253     oclgr = new OpenCLGpuRegistry();
254     appContext->setOpenCLGpuRegistry(oclgr);
255 #endif
256 
257     rdc = new RecentlyDownloadedCache();
258     appContext->setRecentlyDownloadedCache(rdc);
259 
260     vfsReg = new VirtualFileSystemRegistry();
261     appContext->setVirtualFileSystemRegistry(vfsReg);
262 
263     Workflow::WorkflowEnv::init(new Workflow::WorkflowEnvImpl());
264     Workflow::WorkflowEnv::getDomainRegistry()->registerEntry(
265         new LocalWorkflow::LocalDomainFactory());
266 
267     assemblyReg = new DnaAssemblyAlgRegistry();
268     appContext->setDnaAssemblyAlgRegistry(assemblyReg);
269 
270     genomeAssemblyReg = new GenomeAssemblyAlgRegistry();
271     appContext->setGenomeAssemblyAlgRegistry(genomeAssemblyReg);
272 
273     dbr = new DataBaseRegistry();
274     appContext->setDataBaseRegistry(dbr);
275 
276     cdsfr = new CDSearchFactoryRegistry();
277     appContext->setCDSearchFactoryRegistry(cdsfr);
278 
279     saar = new StructuralAlignmentAlgorithmRegistry();
280     appContext->setStructuralAlignmentAlgorithmRegistry(saar);
281 
282     splicedAlignmentTaskRegistry = new SplicedAlignmentTaskRegistry();
283     appContext->setSplicedAlignmentTaskRegistry(splicedAlignmentTaskRegistry);
284 
285     workflowScriptRegistry = new WorkflowScriptRegistry();
286     appContext->setWorkflowScriptRegistry(workflowScriptRegistry);
287 
288     appFileStorage = new AppFileStorage();
289     U2OpStatusImpl os;
290     appFileStorage->init(os);
291     if (os.hasError()) {
292         coreLog.error(os.getError());
293         delete appFileStorage;
294     } else {
295         appContext->setAppFileStorage(appFileStorage);
296     }
297 
298     dpr = new U2DataPathRegistry();
299     appContext->setDataPathRegistry(dpr);
300 
301     GCOUNTER(cvar, "U2Script is ready");
302 
303     t1.stop();
304     QObject::connect(psp, SIGNAL(si_allStartUpPluginsLoaded()), &app, SLOT(quit()));
305     app.exec();
306 }
307 
~UgeneContextWrapper()308 UgeneContextWrapper::~UgeneContextWrapper() {
309     Workflow::WorkflowEnv::shutdown();
310 
311     delete dpr;
312     appContext->setDataPathRegistry(nullptr);
313 
314     delete genomeAssemblyReg;
315     appContext->setGenomeAssemblyAlgRegistry(nullptr);
316 
317     delete vfsReg;
318     appContext->setVirtualFileSystemRegistry(nullptr);
319 
320     delete rdc;
321     appContext->setRecentlyDownloadedCache(nullptr);
322 
323     delete swrfr;
324     appContext->setSWResultFilterRegistry(nullptr);
325 
326     delete swar;
327     appContext->setSmithWatermanTaskFactoryRegistry(nullptr);
328 
329     delete msfr;
330     appContext->setMolecularSurfaceFactoryRegistry(nullptr);
331 
332     delete smr;
333     appContext->setSubstMatrixRegistry(nullptr);
334 
335     delete sreg;
336     appContext->setServiceRegistry(nullptr);
337 
338     delete psp;
339     appContext->setPluginSupport(nullptr);
340 
341     delete dal;
342     appContext->setDNAAlphabetRegistry(nullptr);
343 
344     delete dbxrr;
345     appContext->setDBXRefRegistry(nullptr);
346 
347     delete dtr;
348     appContext->setDNATranslationRegistry(nullptr);
349 
350     delete io;
351     appContext->setIOAdapterRegistry(nullptr);
352 
353     delete dfr;
354     appContext->setDocumentFormatRegistry(nullptr);
355 
356     delete dbiRegistry;
357     appContext->setDbiRegistry(nullptr);
358 
359     delete ts;
360     appContext->setTaskScheduler(nullptr);
361 
362     delete asr;
363     appContext->setAnnotationSettingsRegistry(nullptr);
364 
365     delete resTrack;
366     appContext->setResourceTracker(nullptr);
367 
368     delete cgr;
369     appContext->setCudaGpuRegistry(nullptr);
370 
371 #ifdef OPENCL_SUPPORT
372     delete oclgr;
373     appContext->setOpenCLGpuRegistry(nullptr);
374 #endif
375 
376     delete appSettings;
377     appContext->setAppSettings(nullptr);
378 
379     delete settings;
380     appContext->setSettings(nullptr);
381 
382     delete globalSettings;
383     appContext->setGlobalSettings(nullptr);
384 
385     delete sspar;
386     appContext->setSecStructPedictAlgRegistry(nullptr);
387 
388     appContext->setExternalToolRegistry(nullptr);
389     delete etr;
390 
391     delete str;
392     appContext->setScriptingToolRegistry(nullptr);
393 
394     delete msaConsReg;
395     appContext->setMSAConsensusAlgorithmRegistry(nullptr);
396 
397     delete msaDistReg;
398     appContext->setMSADistanceAlgorithmRegistry(nullptr);
399 
400     delete assemblyConsReg;
401     appContext->setAssemblyConsensusAlgorithmRegistry(nullptr);
402 
403     appContext->setPWMConversionAlgorithmRegistry(nullptr);
404     delete pwmConvReg;
405 
406     delete dbr;
407     appContext->setDataBaseRegistry(nullptr);
408 
409     appContext->setQDActorFactoryRegistry(nullptr);
410     delete qpr;
411 
412     delete cdsfr;
413     appContext->setCDSearchFactoryRegistry(nullptr);
414 
415     appContext->setStructuralAlignmentAlgorithmRegistry(nullptr);
416     delete saar;
417 
418     delete splicedAlignmentTaskRegistry;
419     appContext->setSplicedAlignmentTaskRegistry(nullptr);
420 
421     appContext->setAlignmentAlgorithmsRegistry(nullptr);
422     delete alignmentAlgorithmRegistry;
423 }
424 
processTask(Task * task)425 int UgeneContextWrapper::processTask(Task *task) {
426     coreLog.info(QObject::tr("%1-bit version of UGENE started")
427                      .arg(Version::appArchitecture));
428     ts->registerTopLevelTask(task);
429     QObject::connect(ts, SIGNAL(si_topLevelTaskUnregistered(Task *)), &app, SLOT(quit()));
430     return app.exec();
431 }
432 
433 // TODO: fix this dummy check
isAppContextInitialized()434 bool UgeneContextWrapper::isAppContextInitialized() {
435     return AppContextImpl::getApplicationContext()->getWorkingDirectoryPath().isEmpty();
436 }
437 
438 }  // namespace U2
439