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 "MsaObjectTests.h"
23 
24 #include <QDomElement>
25 
26 #include <U2Core/DNASequenceObject.h>
27 #include <U2Core/DNASequenceUtils.h>
28 #include <U2Core/DocumentModel.h>
29 #include <U2Core/MultipleSequenceAlignmentObject.h>
30 #include <U2Core/U2SafePoints.h>
31 
32 namespace U2 {
33 
34 const QString GTest_CompareTwoMsa::DOC1_ATTR = "doc1";
35 const QString GTest_CompareTwoMsa::DOC2_ATTR = "doc2";
36 
init(XMLTestFormat *,const QDomElement & element)37 void GTest_CompareTwoMsa::init(XMLTestFormat *, const QDomElement &element) {
38     docContextName = element.attribute(DOC1_ATTR);
39     if (docContextName.isEmpty()) {
40         failMissingValue(DOC1_ATTR);
41         return;
42     }
43 
44     secondDocContextName = element.attribute(DOC2_ATTR);
45     if (secondDocContextName.isEmpty()) {
46         failMissingValue(DOC2_ATTR);
47         return;
48     }
49 }
50 
report()51 Task::ReportResult GTest_CompareTwoMsa::report() {
52     Document *doc1 = getContext<Document>(this, docContextName);
53     CHECK_EXT(nullptr != doc1, setError(QString("document not found: %1").arg(docContextName)), ReportResult_Finished);
54 
55     const QList<GObject *> objs1 = doc1->getObjects();
56     CHECK_EXT(1 == objs1.size(), setError(QString("document '%1' contains several objects: the comparison not implemented").arg(docContextName)), ReportResult_Finished);
57 
58     MultipleSequenceAlignmentObject *msa1 = qobject_cast<MultipleSequenceAlignmentObject *>(objs1.first());
59     CHECK_EXT(nullptr != msa1, setError(QString("document '%1' contains an incorrect object: expected '%2', got '%3'").arg(docContextName).arg(GObjectTypes::MULTIPLE_SEQUENCE_ALIGNMENT).arg(objs1.first()->getGObjectType())), ReportResult_Finished);
60 
61     Document *doc2 = getContext<Document>(this, secondDocContextName);
62     CHECK_EXT(nullptr != doc2, setError(QString("document not found: %1").arg(secondDocContextName)), ReportResult_Finished);
63 
64     const QList<GObject *> objs2 = doc2->getObjects();
65     CHECK_EXT(1 == objs2.size(), setError(QString("document '%1' contains several objects: the comparison not implemented").arg(secondDocContextName)), ReportResult_Finished);
66 
67     MultipleSequenceAlignmentObject *msa2 = qobject_cast<MultipleSequenceAlignmentObject *>(objs2.first());
68     CHECK_EXT(nullptr != msa2, setError(QString("document '%1' contains an incorrect object: expected '%2', got '%3'").arg(secondDocContextName).arg(GObjectTypes::MULTIPLE_SEQUENCE_ALIGNMENT).arg(objs2.first()->getGObjectType())), ReportResult_Finished);
69 
70     const qint64 rowsNumber1 = msa1->getNumRows();
71     const qint64 rowsNumber2 = msa2->getNumRows();
72     CHECK_EXT(rowsNumber1 == rowsNumber2,
73               setError(QString("The rows numbers differ: the object '%1' from the document '%2' contains %3 rows, the object '%4' from the document '%5' contains %6 rows")
74                            .arg(msa1->getGObjectName())
75                            .arg(docContextName)
76                            .arg(rowsNumber1)
77                            .arg(msa2->getGObjectName())
78                            .arg(secondDocContextName)
79                            .arg(rowsNumber2)),
80               ReportResult_Finished);
81 
82     for (int i = 0; i < rowsNumber1; i++) {
83         const MultipleSequenceAlignmentRow row1 = msa1->getMsaRow(i);
84         const MultipleSequenceAlignmentRow row2 = msa2->getMsaRow(i);
85         const bool areEqual = row1->isRowContentEqual(row2);
86         CHECK_EXT(areEqual, setError(QString("The rows with number %1 differ from each other").arg(i)), ReportResult_Finished);
87     }
88 
89     return ReportResult_Finished;
90 }
91 
createTestFactories()92 QList<XMLTestFactory *> MsaObjectTests::createTestFactories() {
93     QList<XMLTestFactory *> res;
94     res.append(GTest_CompareTwoMsa::createFactory());
95     return res;
96 }
97 
98 }  // namespace U2
99