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 "ConvertSnpeffVariationsToAnnotationsWorker.h"
23 
24 #include <U2Core/AnnotationTableObject.h>
25 #include <U2Core/AppContext.h>
26 #include <U2Core/BaseDocumentFormats.h>
27 #include <U2Core/Formatters.h>
28 #include <U2Core/GUrlUtils.h>
29 #include <U2Core/L10n.h>
30 #include <U2Core/TaskSignalMapper.h>
31 #include <U2Core/U2SafePoints.h>
32 
33 #include <U2Designer/DelegateEditors.h>
34 
35 #include <U2Formats/ConvertSnpeffVariationsToAnnotationsTask.h>
36 
37 #include <U2Lang/ActorPrototypeRegistry.h>
38 #include <U2Lang/BaseActorCategories.h>
39 #include <U2Lang/BaseAttributes.h>
40 #include <U2Lang/BasePorts.h>
41 #include <U2Lang/BaseSlots.h>
42 #include <U2Lang/BaseTypes.h>
43 #include <U2Lang/URLAttribute.h>
44 #include <U2Lang/WorkflowEnv.h>
45 #include <U2Lang/WorkflowMonitor.h>
46 
47 namespace U2 {
48 namespace LocalWorkflow {
49 
50 static const QString IN_VARIATIONS_URL_PORT_ID = "in-variations-url";
51 
ConvertSnpeffVariationsToAnnotationsPrompter(Actor * actor)52 ConvertSnpeffVariationsToAnnotationsPrompter::ConvertSnpeffVariationsToAnnotationsPrompter(Actor *actor)
53     : PrompterBase<ConvertSnpeffVariationsToAnnotationsPrompter>(actor) {
54 }
55 
composeRichDoc()56 QString ConvertSnpeffVariationsToAnnotationsPrompter::composeRichDoc() {
57     IntegralBusPort *input = qobject_cast<IntegralBusPort *>(target->getPort(IN_VARIATIONS_URL_PORT_ID));
58     SAFE_POINT(nullptr != input, "No input port", "");
59     const Actor *producer = input->getProducer(BaseSlots::URL_SLOT().getId());
60     const QString unsetStr = "<font color='red'>" + tr("unset") + "</font>";
61     const QString producerName = (nullptr != producer) ? producer->getLabel() : unsetStr;
62     return tr("Parses information in variations from <u>%1</u> into annotations.").arg(producerName);
63 }
64 
65 const QString ConvertSnpeffVariationsToAnnotationsFactory::ACTOR_ID = "convert-snpeff-variations-to-annotations";
66 
ConvertSnpeffVariationsToAnnotationsFactory()67 ConvertSnpeffVariationsToAnnotationsFactory::ConvertSnpeffVariationsToAnnotationsFactory()
68     : DomainFactory(ACTOR_ID) {
69 }
70 
createWorker(Actor * actor)71 Worker *ConvertSnpeffVariationsToAnnotationsFactory::createWorker(Actor *actor) {
72     return new ConvertSnpeffVariationsToAnnotationsWorker(actor);
73 }
74 
init()75 void ConvertSnpeffVariationsToAnnotationsFactory::init() {
76     QList<PortDescriptor *> ports;
77     {
78         Descriptor inDesc(IN_VARIATIONS_URL_PORT_ID,
79                           ConvertSnpeffVariationsToAnnotationsWorker::tr("Input URL"),
80                           ConvertSnpeffVariationsToAnnotationsWorker::tr("Input variation file URL."));
81 
82         QMap<Descriptor, DataTypePtr> inType;
83         inType[BaseSlots::URL_SLOT()] = BaseTypes::STRING_TYPE();
84 
85         ports << new PortDescriptor(inDesc, DataTypePtr(new MapDataType(ACTOR_ID + "-in", inType)), true /*input*/);
86     }
87 
88     DocumentFormatConstraints constraints;
89     constraints.supportedObjectTypes.insert(GObjectTypes::ANNOTATION_TABLE);
90     constraints.addFlagToSupport(DocumentFormatFlag_SupportWriting);
91     constraints.addFlagToExclude(DocumentFormatFlag_CannotBeCreated);
92     QList<DocumentFormatId> supportedFormats = AppContext::getDocumentFormatRegistry()->selectFormats(constraints);
93 
94     QList<Attribute *> attributes;
95     {
96         attributes << new Attribute(BaseAttributes::URL_OUT_ATTRIBUTE(), BaseTypes::STRING_TYPE(), false, "");
97 
98         const DocumentFormatId format = (supportedFormats.contains(BaseDocumentFormats::PLAIN_GENBANK) ? BaseDocumentFormats::PLAIN_GENBANK : supportedFormats.first());
99         Attribute *documentFormatAttribute = new Attribute(BaseAttributes::DOCUMENT_FORMAT_ATTRIBUTE(), BaseTypes::STRING_TYPE(), false, format);
100         documentFormatAttribute->addRelation(new FileExtensionRelation(BaseAttributes::URL_OUT_ATTRIBUTE().getId()));
101         attributes << documentFormatAttribute;
102     }
103 
104     Descriptor desc(ACTOR_ID,
105                     ConvertSnpeffVariationsToAnnotationsWorker::tr("Convert SnpEff Variations to Annotations"),
106                     ConvertSnpeffVariationsToAnnotationsWorker::tr("Parses information, added to variations by SnpEff, into standard annotations."));
107     ActorPrototype *proto = new IntegralBusActorPrototype(desc, ports, attributes);
108     proto->setPrompter(new ConvertSnpeffVariationsToAnnotationsPrompter(nullptr));
109     WorkflowEnv::getProtoRegistry()->registerProto(BaseActorCategories::CATEGORY_VARIATION_ANALYSIS(), proto);
110 
111     QMap<QString, PropertyDelegate *> delegates;
112     {
113         DelegateTags tags;
114         tags.set(DelegateTags::PLACEHOLDER_TEXT, ConvertSnpeffVariationsToAnnotationsWorker::tr("Produced from the input file name"));
115         delegates[BaseAttributes::URL_OUT_ATTRIBUTE().getId()] = new URLDelegate(tags, "", "");
116 
117         QVariantMap map;
118         for (const DocumentFormatId &formatId : qAsConst(supportedFormats)) {
119             map[formatId] = formatId;
120         }
121         auto formatDelegate = new ComboBoxDelegate(map);
122         formatDelegate->setItemTextFormatter(QSharedPointer<StringFormatter>(new DocumentNameByIdFormatter()));
123         formatDelegate->setSortFlag(true);
124         delegates[BaseAttributes::DOCUMENT_FORMAT_ATTRIBUTE().getId()] = formatDelegate;
125     }
126     proto->setEditor(new DelegateEditor(delegates));
127 
128     DomainFactory *localDomain = WorkflowEnv::getDomainRegistry()->getById(LocalDomainFactory::ID);
129     localDomain->registerEntry(new ConvertSnpeffVariationsToAnnotationsFactory());
130 }
131 
ConvertSnpeffVariationsToAnnotationsWorker(Actor * actor)132 ConvertSnpeffVariationsToAnnotationsWorker::ConvertSnpeffVariationsToAnnotationsWorker(Actor *actor)
133     : BaseWorker(actor),
134       input(nullptr) {
135 }
136 
init()137 void ConvertSnpeffVariationsToAnnotationsWorker::init() {
138     input = ports.value(IN_VARIATIONS_URL_PORT_ID);
139 }
140 
tick()141 Task *ConvertSnpeffVariationsToAnnotationsWorker::tick() {
142     if (input->hasMessage()) {
143         return createTask(getMessageAndSetupScriptValues(input));
144     } else if (input->isEnded()) {
145         setDone();
146     }
147     return nullptr;
148 }
149 
cleanup()150 void ConvertSnpeffVariationsToAnnotationsWorker::cleanup() {
151 }
152 
sl_taskFinished(Task * task)153 void ConvertSnpeffVariationsToAnnotationsWorker::sl_taskFinished(Task *task) {
154     LoadConvertAndSaveSnpeffVariationsToAnnotationsTask *convertTask = qobject_cast<LoadConvertAndSaveSnpeffVariationsToAnnotationsTask *>(task);
155     SAFE_POINT(nullptr != convertTask, L10N::nullPointerError("LoadConvertAndSaveSnpeffVariationsToAnnotationsTask"), );
156     CHECK(!convertTask->hasError() && !convertTask->isCanceled(), );
157     monitor()->addOutputFile(convertTask->getResultUrl(), getActorId());
158 }
159 
createTask(const Message & message)160 Task *ConvertSnpeffVariationsToAnnotationsWorker::createTask(const Message &message) {
161     QVariantMap data = message.getData().toMap();
162     const QString variationsFileurl = data[BaseSlots::URL_SLOT().getId()].toString();
163     const QString formatId = actor->getParameter(BaseAttributes::DOCUMENT_FORMAT_ATTRIBUTE().getId())->getAttributeValue<QString>(context);
164     QString annotationsFileUrl = actor->getParameter(BaseAttributes::URL_OUT_ATTRIBUTE().getId())->getAttributeValue<QString>(context);
165     if (annotationsFileUrl.isEmpty()) {
166         annotationsFileUrl = context->getMetadataStorage().get(message.getMetadataId()).getFileUrl();
167         const GUrl sourceUrl = GUrlUtils::changeFileExt(annotationsFileUrl, formatId);
168         annotationsFileUrl = GUrlUtils::rollFileName(context->workingDir() + sourceUrl.baseFileName() + "_variants." + sourceUrl.completeFileSuffix(), "_");
169     }
170     Task *task = new LoadConvertAndSaveSnpeffVariationsToAnnotationsTask(variationsFileurl, context->getDataStorage()->getDbiRef(), annotationsFileUrl, formatId);
171     connect(new TaskSignalMapper(task), SIGNAL(si_taskFinished(Task *)), SLOT(sl_taskFinished(Task *)));
172     return task;
173 }
174 
175 }  // namespace LocalWorkflow
176 }  // namespace U2
177