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 "Peak2GeneTask.h"
23 
24 #include <QDir>
25 
26 #include <U2Core/AnnotationTableObject.h>
27 #include <U2Core/AppContext.h>
28 #include <U2Core/AppSettings.h>
29 #include <U2Core/BaseDocumentFormats.h>
30 #include <U2Core/Counter.h>
31 #include <U2Core/DocumentModel.h>
32 #include <U2Core/DocumentUtils.h>
33 #include <U2Core/GObjectTypes.h>
34 #include <U2Core/GObjectUtils.h>
35 #include <U2Core/GUrlUtils.h>
36 #include <U2Core/IOAdapter.h>
37 #include <U2Core/IOAdapterUtils.h>
38 #include <U2Core/L10n.h>
39 #include <U2Core/LoadDocumentTask.h>
40 #include <U2Core/SaveDocumentTask.h>
41 #include <U2Core/TextObject.h>
42 #include <U2Core/U2OpStatusUtils.h>
43 #include <U2Core/U2SafePoints.h>
44 #include <U2Core/UserApplicationsSettings.h>
45 
46 #include "Gene2PeakFormatLoader.h"
47 #include "Peak2GeneFormatLoader.h"
48 #include "Peak2GeneSupport.h"
49 
50 namespace U2 {
51 
52 const QString Peak2GeneTask::BASE_DIR_NAME("peak2gene_tmp");
53 const QString Peak2GeneTask::BASE_SUBDIR_NAME("peak2gene");
54 const QString Peak2GeneTask::TREAT_NAME("treatment");
55 
Peak2GeneTask(const Peak2GeneSettings & settings,Workflow::DbiDataStorage * storage,const QList<Workflow::SharedDbiDataHandler> & treatAnn)56 Peak2GeneTask::Peak2GeneTask(const Peak2GeneSettings &settings, Workflow::DbiDataStorage *storage, const QList<Workflow::SharedDbiDataHandler> &treatAnn)
57     : ExternalToolSupportTask("Peak2gene annotation", TaskFlag_CollectChildrenWarnings), settings(settings), storage(storage), treatAnn(treatAnn), treatDoc(nullptr), genesAto(nullptr), peaksAto(nullptr), treatTask(nullptr), etTask(nullptr) {
58     GCOUNTER(cvar, "NGS:Peak2GeneTask");
59     SAFE_POINT_EXT(nullptr != storage, setError(L10N::nullPointerError("workflow data storage")), );
60 }
61 
~Peak2GeneTask()62 Peak2GeneTask::~Peak2GeneTask() {
63     cleanup();
64 }
65 
cleanup()66 void Peak2GeneTask::cleanup() {
67     treatAnn.clear();
68 
69     delete treatDoc;
70     treatDoc = nullptr;
71     delete genesAto;
72     genesAto = nullptr;
73     delete peaksAto;
74     peaksAto = nullptr;
75 
76     //remove tmp files
77     QString tmpDirPath = AppContext::getAppSettings()->getUserAppsSettings()->getCurrentProcessTemporaryDirPath(BASE_DIR_NAME);
78     QDir tmpDir(tmpDirPath);
79     if (tmpDir.exists()) {
80         foreach (QString file, tmpDir.entryList()) {
81             tmpDir.remove(file);
82         }
83     }
84 }
85 
prepare()86 void Peak2GeneTask::prepare() {
87     UserAppsSettings *appSettings = AppContext::getAppSettings()->getUserAppsSettings();
88     workingDir = appSettings->createCurrentProcessTemporarySubDir(stateInfo, BASE_DIR_NAME);
89     CHECK_OP(stateInfo, );
90 
91     treatDoc = createDoc(treatAnn, TREAT_NAME);
92     CHECK_OP(stateInfo, );
93 
94     treatTask = new SaveDocumentTask(treatDoc);
95     addSubTask(treatTask);
96 }
97 
createDoc(const QList<Workflow::SharedDbiDataHandler> & annData,const QString & name)98 Document *Peak2GeneTask::createDoc(const QList<Workflow::SharedDbiDataHandler> &annData, const QString &name) {
99     Document *doc = nullptr;
100 
101     QString docUrl = workingDir + "/" + name + ".bed";
102 
103     DocumentFormat *bedFormat = AppContext::getDocumentFormatRegistry()->getFormatById(BaseDocumentFormats::BED);
104     CHECK_EXT(nullptr != bedFormat, stateInfo.setError("NULL bed format"), doc);
105 
106     doc = bedFormat->createNewLoadedDocument(
107         IOAdapterUtils::get(BaseIOAdapters::LOCAL_FILE), docUrl, stateInfo);
108     CHECK_OP(stateInfo, doc);
109     doc->setDocumentOwnsDbiResources(false);
110 
111     QList<AnnotationTableObject *> annTables = Workflow::StorageUtils::getAnnotationTableObjects(storage, annData);
112     foreach (AnnotationTableObject *annTable, annTables) {
113         doc->addObject(annTable);
114     }
115 
116     return doc;
117 }
118 
onSubTaskFinished(Task * subTask)119 QList<Task *> Peak2GeneTask::onSubTaskFinished(Task *subTask) {
120     QList<Task *> result;
121     CHECK(!subTask->isCanceled(), result);
122     CHECK(!subTask->hasError(), result);
123 
124     if (treatTask == subTask) {
125         QStringList args = settings.getArguments(treatDoc->getURLString());
126 
127         etTask = new ExternalToolRunTask(Peak2GeneSupport::ET_PEAK2GENE_ID, args, new ExternalToolLogParser(), workingDir);
128         setListenerForTask(etTask);
129         result << etTask;
130     } else if (subTask == etTask) {
131         //read annotations
132         genesUrl = workingDir + "/" + Peak2GeneSettings::DEFAULT_NAME + "_gene_annotation.txt";
133         peaksUrl = workingDir + "/" + Peak2GeneSettings::DEFAULT_NAME + "_peaks_annotation.txt";
134     }
135 
136     return result;
137 }
138 
run()139 void Peak2GeneTask::run() {
140     QScopedPointer<IOAdapter> geneAdapter(AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById(BaseIOAdapters::LOCAL_FILE)->createIOAdapter());
141     CHECK_EXT(geneAdapter->open(genesUrl, IOAdapterMode_Read), L10N::errorOpeningFileRead(genesUrl), );
142 
143     Gene2PeakFormatLoader geneLoader(stateInfo, geneAdapter.data());
144     QList<SharedAnnotationData> geneAnnotations = geneLoader.loadAnnotations();
145     CHECK_OP(stateInfo, );
146 
147     genesAto = new AnnotationTableObject("gene2peak", storage->getDbiRef());
148     genesAto->addAnnotations(geneAnnotations);
149 
150     QScopedPointer<IOAdapter> peakAdapter(AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById(BaseIOAdapters::LOCAL_FILE)->createIOAdapter());
151     CHECK_EXT(peakAdapter->open(peaksUrl, IOAdapterMode_Read), L10N::errorOpeningFileRead(peaksUrl), );
152 
153     Peak2GeneFormatLoader peakLoader(stateInfo, peakAdapter.data());
154     QList<SharedAnnotationData> peakAnnotations = peakLoader.loadAnnotations();
155     CHECK_OP(stateInfo, );
156 
157     peaksAto = new AnnotationTableObject("peak2gene", storage->getDbiRef());
158     peaksAto->addAnnotations(peakAnnotations);
159 }
160 
getSettings() const161 const Peak2GeneSettings &Peak2GeneTask::getSettings() const {
162     return settings;
163 }
164 
getGenes() const165 AnnotationTableObject *Peak2GeneTask::getGenes() const {
166     return genesAto;
167 }
168 
getPeaks() const169 AnnotationTableObject *Peak2GeneTask::getPeaks() const {
170     return peaksAto;
171 }
172 
getGenesUrl() const173 const QString &Peak2GeneTask::getGenesUrl() const {
174     return genesUrl;
175 }
176 
getPeaksUrl() const177 const QString &Peak2GeneTask::getPeaksUrl() const {
178     return genesUrl;
179 }
180 
181 }    // namespace U2
182