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 "SecStructPredictTests.h"
23 
24 #include <QDomElement>
25 
26 #include <U2Algorithm/SecStructPredictAlgRegistry.h>
27 #include <U2Algorithm/SecStructPredictTask.h>
28 
29 #include <U2Core/AnnotationTableObject.h>
30 #include <U2Core/AppContext.h>
31 #include <U2Core/DNASequenceObject.h>
32 #include <U2Core/U2DbiRegistry.h>
33 #include <U2Core/U2OpStatusUtils.h>
34 #include <U2Core/U2SafePoints.h>
35 
36 namespace U2 {
37 
38 #define SEQ_ATTR "seq"
39 #define SEQ_NAME_ATTR "seq-name"
40 #define INDEX_ATTR "index"
41 #define OUTPUT_SEQ_ATTR "output-seq"
42 #define ALG_NAME_ATTR "algorithm-name"
43 
init(XMLTestFormat *,const QDomElement & el)44 void GTest_SecStructPredictAlgorithm::init(XMLTestFormat *, const QDomElement &el) {
45     inputSeq = el.attribute(SEQ_ATTR);
46     if (inputSeq.isEmpty()) {
47         failMissingValue(SEQ_ATTR);
48         return;
49     }
50 
51     outputSeq = el.attribute(OUTPUT_SEQ_ATTR);
52     if (outputSeq.isEmpty()) {
53         failMissingValue(OUTPUT_SEQ_ATTR);
54         return;
55     }
56 
57     algName = el.attribute(ALG_NAME_ATTR);
58     if (algName.isEmpty()) {
59         failMissingValue(ALG_NAME_ATTR);
60         return;
61     }
62 }
63 
prepare()64 void GTest_SecStructPredictAlgorithm::prepare() {
65     SecStructPredictAlgRegistry *sspr = AppContext::getSecStructPredictAlgRegistry();
66     if (!sspr->hadRegistered(algName)) {
67         stateInfo.setError(QString(tr("Algorithm named %1 not found")).arg(algName));
68         return;
69     }
70     SecStructPredictTaskFactory *factory = sspr->getAlgorithm(algName);
71     task = factory->createTaskInstance(inputSeq.toLatin1());
72     addSubTask(task);
73 }
74 
report()75 Task::ReportResult GTest_SecStructPredictAlgorithm::report() {
76     const QByteArray &output = task->getSSFormatResults();
77 
78     if (output != outputSeq) {
79         stateInfo.setError(tr("Output sec struct sequence is incorrect"));
80         return ReportResult_Finished;
81     }
82 
83     return ReportResult_Finished;
84 }
85 
86 ///////////////////////////////////////////////////////////////////////////////////////////
87 
init(XMLTestFormat *,const QDomElement & el)88 void GTest_SecStructPredictTask::init(XMLTestFormat *, const QDomElement &el) {
89     seqName = el.attribute(SEQ_NAME_ATTR);
90     if (seqName.isEmpty()) {
91         failMissingValue(SEQ_NAME_ATTR);
92         return;
93     }
94 
95     algName = el.attribute(ALG_NAME_ATTR);
96     if (algName.isEmpty()) {
97         failMissingValue(ALG_NAME_ATTR);
98         return;
99     }
100 
101     resultsTableContextName = el.attribute(INDEX_ATTR);
102     if (resultsTableContextName.isEmpty()) {
103         failMissingValue(INDEX_ATTR);
104         return;
105     }
106 }
107 
prepare()108 void GTest_SecStructPredictTask::prepare() {
109     U2SequenceObject *mySequence = getContext<U2SequenceObject>(this, seqName);
110     if (mySequence == nullptr) {
111         stateInfo.setError(QString("error can't cast to sequence from GObject"));
112         return;
113     }
114     SecStructPredictAlgRegistry *sspr = AppContext::getSecStructPredictAlgRegistry();
115     if (!sspr->hadRegistered(algName)) {
116         stateInfo.setError(QString(tr("Algorithm named %1 not found")).arg(algName));
117         return;
118     }
119     SecStructPredictTaskFactory *factory = sspr->getAlgorithm(algName);
120     QByteArray seqData = mySequence->getWholeSequenceData(stateInfo);
121     task = factory->createTaskInstance(seqData);
122     CHECK_OP(stateInfo, );
123     addSubTask(task);
124 }
125 
report()126 Task::ReportResult GTest_SecStructPredictTask::report() {
127     if (task != nullptr && task->hasError()) {
128         stateInfo.setError(task->getError());
129     } else if (!resultsTableContextName.isEmpty()) {
130         QList<SharedAnnotationData> results = task->getResults();
131         U2OpStatusImpl os;
132         const U2DbiRef dbiRef = AppContext::getDbiRegistry()->getSessionTmpDbiRef(os);
133         SAFE_POINT_OP(os, ReportResult_Finished);
134         aObj = new AnnotationTableObject(resultsTableContextName, dbiRef);
135         aObj->addAnnotations(results);
136         addContext(resultsTableContextName, aObj);
137         contextAdded = true;
138     }
139 
140     return ReportResult_Finished;
141 }
142 
cleanup()143 void GTest_SecStructPredictTask::cleanup() {
144     if (contextAdded) {
145         removeContext(resultsTableContextName);
146     }
147 
148     XmlTest::cleanup();
149 }
150 ///////////////////////////////////////////////////////////////////////////////////////////
151 
createTestFactories()152 QList<XMLTestFactory *> SecStructPredictTests::createTestFactories() {
153     QList<XMLTestFactory *> res;
154     res.append(GTest_SecStructPredictAlgorithm::createFactory());
155     res.append(GTest_SecStructPredictTask::createFactory());
156     return res;
157 }
158 
159 }  // namespace U2
160